Jump to content

User:PerfektesChaos/js/autoBackup/r.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/// User:PerfektesChaos/js/autoBackup/r.js
/// 2019-07-01 PerfektesChaos@de.wikipedia
/// Fingerprint:#0#2860E0F5#
/// @license:CC-by-sa/4.0
///<nowiki>
(function(mw,$){
"use strict";
var VERSION=2.3,BAK="autoBackup";
if(typeof mw.libs[BAK]!=="object"||!mw.libs[BAK]){
mw.libs[BAK]={};}
mw.libs[BAK].type=BAK;
BAK=mw.libs[BAK];
BAK.doc="[[w:en:User:PerfektesChaos/js/"+BAK+"]]";
BAK.vsn=VERSION;
BAK.cnf={maxAge:72,maxHist:5,maxPages:10,maxRev:3,mid:5,msec:300000};
BAK.disk={self:"AutoBackupPerfectChaos",stick:"newest|subject"};
BAK.gui={};
BAK.util={};
if(!typeof BAK.opt||typeof BAK.opt!=="object"){
BAK.opt={};}
BAK.cnf.text={
"BAKself":{"en":"AutoBackup",
"de":"AutoBackup"},
"IntJSONparse":{"en":"Internal ERROR -- JSON.parse",
"de":"Interner FEHLER -- JSON.parse"},
"NoLSavail":{"en":"No localStorage available",
"de":"Kein localStorage zugreifbar"},
"otherPages":{"en":"Other Pages",
"de":"Andere Seiten"},
"pending":{"en":"Recover from pending abort?",
"de":"Wiederherstellung nach Abbruch?"},
"setItemExcept":{"en":"ERROR setting localStorage",
"de":"FEHLER beim Setzen im localStorage"},
"thisPage":{"en":"this page",
"de":"diese Seite"},
"VanishedLS":{"en":"ERROR: localStorage vanished",
"de":"FEHLER: localStorage verschwunden"},
"WriteCrash":{"en":"Crash on localStorage write attempt",
"de":"Schreibversuch auf localStorage versagt"}
};
BAK.cnf.translang={
"de":"de",
"de-at":"de",
"de-ch":"de",
"de-formal":"de",
"als":"de",
"bar":"de",
"dsb":"de",
"frr":"de",
"gsw":"de",
"hsb":"de",
"ksh":"de",
"lb":"de",
"nds":"de",
"pdc":"de",
"pdt":"de",
"pfl":"de",
"sli":"de",
"stq":"de",
"vmf":"de"
};
BAK.cnf.favorite=function(){
var s;
if(!this.slang){
s=mw.config.get("wgUserLanguage").toLowerCase();
s=this.translang[s];
if(s){
this.slang=s;
}else{
this.slang="en";}}
};
BAK.cnf.feature=function(apply){
var e,r;
if(!this.slang){
this.favorite();}
e=this.text[apply];
if(e){
r=e[this.slang];
if(!r){
r=e.en;
if(!r){
r="???"+apply+"???";}}
}else{
r="***"+apply+"***";}
return r;
};
BAK.cnf.fetch=function(){
if(!this.load){
if(typeof BAK.opt==="object"){
if(typeof BAK.opt.maxAge==="number"){
if(BAK.opt.maxAge>=0){
this.maxAge=Math.floor(BAK.opt.maxAge);}}
if(typeof BAK.opt.maxHist==="number"){
if(BAK.opt.maxHist>=1){
this.maxHist=Math.ceil(BAK.opt.maxHist);}}
if(typeof BAK.opt.maxPages==="number"){
if(BAK.opt.maxPages>=1){
this.maxPages=Math.ceil(BAK.opt.maxPages);}}
if(typeof BAK.opt.maxRev==="number"){
if(BAK.opt.maxRev>=1){
this.maxRev=Math.ceil(BAK.opt.maxRev);}}
if(BAK.learn){
if(typeof BAK.opt.mid==="number"){
if(BAK.opt.mid>0){
this.mid=BAK.opt.mid;
}else{
this.mid=false;}}
if(this.mid){
this.msec=this.mid*60000;
}else{
this.msec=false;}
if(BAK.opt.portlet){
BAK.gui.facility();}}}
this.load=true;}
};
BAK.disk.fetch=function(){
var s;
BAK.pages=false;
if(typeof window.localStorage==="object"){
BAK.family();
s=window.localStorage.getItem(this.storage);
if(s){
try{
BAK.pages=JSON.parse(s);
}catch(e){
BAK.gui.flag("IntJSONparse",false);
window.localStorage.setItem(this.storage,"");}}
}else{
BAK.gui.flag("NoLSavail",false);
this.lock=true;}
};
BAK.disk.flush=function(){
var store=false,s;
if(typeof BAK.pages==="object"){
for(s in BAK.pages){
store=true;
break;}}
if(!store){
this.pages=false;}
if(typeof window.localStorage==="object"){
if(BAK.pages){
store=JSON.stringify(BAK.pages);
if(store==="{}"){
store="";}
}else{
store="";}
try{
window.localStorage.setItem(this.storage,store);
s=window.localStorage.getItem(this.storage);
if(s!==store){
BAK.gui.flag("WriteCrash",false);}
}catch(e){
BAK.gui.flag("setItemExcept",false);}
window.localStorage.setItem(this.storage,store);
}else{
BAK.gui.flag("VanishedLS",false);}
};
BAK.gui.face=function(apply){
var s;
if(!this.$div){
mw.util.addCSS(".cn-fundraiser-banner,#mw-js-message,#siteNotice,#fundraising\n{display: none ! important;}");
this.$div=$("<div class='AutoBackupDiv' />");
if(!this.$top){
this.$top=$("#mw-content-text");
if(this.$top.length){
this.$top.prepend(this.$div);
}else{
this.$top=$("#bodyContent");
if(!this.$top.length){
this.$top=$("#article");
if(!this.$top.length){
this.$top=$("#content");
if(!this.$top.length){
this.$top=BAK.$page;}}}
this.$top.before(this.$div);}}}
if(apply){
if(this.$msg){
this.$msg.empty();
}else{
this.$msg=$("<div class='AutoBackupMsg' />");
this.$div.prepend(this.$msg);}
if(this.$msg){
s="display:   block;font-size: 120%;";
this.$msg.attr("style",s);
this.$msg.append(apply);}
}else{
if(this.$msg){
this.$msg.attr("style","display: none;");}}
};
BAK.gui.facility=function(){
var portlet,$portlet;
if(!BAK.cnf.self){
BAK.cnf.self=BAK.cnf.feature("BAKself");}
portlet=mw.util.addPortletLink("p-cactions","#",BAK.cnf.self,"ca-"+BAK.type);
$portlet=$(portlet);
$portlet.click(BAK.fresh);
$portlet.attr({title:BAK.type+" "+BAK.vsn});
};
BAK.gui.feed=function(){
var r=false,$form,$ta;
if(this.leading){
this.leading=false;
this.$editform=false;
this.$textarea=false;
$form=BAK.$page.find("#editform");
if($form){
$ta=BAK.$page.find("#wpTextbox2");
if($ta.length){
r=$ta.val();}
$ta=$form.find("#wpTextbox1");
if($ta.length){
if(!$ta.attr("readonly")){
this.$editform=$form;
this.$textarea=$ta;}}}}
if(this.$textarea&&!r){
if(window.wikEd&&typeof window.wikEd==="object"&&!window.wikEd.disabled&&window.wikEd.turnedOn&&window.wikEd.useWikEd&&window.wikEd.UpdateTextarea){
window.wikEd.UpdateTextarea();}
r=this.$textarea.val();}
return r;
};
BAK.gui.fence=function(apply){
this.$div.attr("style",(apply?"border: solid 1px #606060; border-bottom: solid 5px #606060; padding: 0.5em; margin-bottom: 2em;":null));
};
BAK.gui.fiat=function(action,add){
var r;
switch(action){
case "ALL":
case "SHOW":
r="<span style='font-size: 200%; font-weight: bold; color: #008000;'>*</span>";
break;
case "DELETE":
r="<span style='font-size: 200%; font-weight: bold; color: #FF0000;'>X</span>";
break;
default:
r="";}
if(r){
r="\n &nbsp; \n<button type='button' id='autoBackup"+action+(add?add:"")+"'>"+r+"</button>";}
return r;
};
BAK.gui.fill=function(){
var page=BAK.pages[BAK.pageID],revs=false,i,n,s;
BAK.gui.$msg.find("#autoBackupALL").remove();
if(BAK.gui.$box){
BAK.gui.$box.remove();}
if(page){
BAK.gui.$box=$("<div />");
BAK.gui.$msg.after(BAK.gui.$box);
if(!BAK.revID){
BAK.revID=mw.config.get("wgCurRevisionId");}
for(i in page){
if(BAK.disk.stick.indexOf(i)<0){
s=page[i].newest+" "+i;
if(revs){
revs.push(s);
}else{
revs=[s];}}}
n=revs.length;
revs.sort();
for(i=revs.length-1;i>=0;i--){
BAK.gui.folder(revs[i],page);}
BAK.gui.fence(true);
}else{
BAK.gui.face(false);}
};
BAK.gui.finish=function(access){
var $btn=this.$editform.find("#"+access);
if($btn.length){
$btn.click(BAK.fresh);}
};
BAK.gui.flag=function(apply,action){
var s;
if(!BAK.cnf.self){
BAK.cnf.self=BAK.cnf.feature("BAKself");}
s=BAK.cnf.feature(apply);
s="<div id='autoBackup' style='border: solid "+(action?2:5)+"px #FF0000; padding: 0.5em;"+(action?"":" color: #FF0000;")+"'>\n"+"<span style='font-size: larger; font-weight: bold;'>"+BAK.cnf.self+"</span> &nbsp;\n"+(action?"":"<span class='error'>")+s+(action?"\n":"</span>\n")+this.fiat(action)+"</div>\n";
this.face(s);
if(action){
this.form(action,"");}
};
BAK.gui.folder=function(access,album){
var e=access.split(" "),k=e[1],s="autoBackupR"+k,i,w,$div,$textarea;
this.$box.append("<div id='"+s+"' />");
$div=this.$box.find("#"+s);
if(parseInt(k,10)===BAK.revID){
s=k+" ("+BAK.cnf.feature("thisPage")+")";
}else{
s=mw.config.get("wgArticlePath");
s="<a href='"+s.replace(/\$1/,"Special:PermanentLink/")+k+"' target='_blank'>"+k+"</a>";}
s="<h4>oldid="+s+"</h4>";
s=s+this.fiat("DELETE",k);
$div.append(s);
this.form("DELETE",k);
e=album[k].history;
for(i=0;i<e.length;i++){
k=e[i];
w=k[1];
s="<h5>"+BAK.util.focus(k[0])+"</h5><p>"+w.length+" bytes</p>";
$div.append(s);
$textarea=$("<textarea rows='5' readonly />");
$textarea.val(w);
$div.append($textarea);}
this.full();
};
BAK.gui.form=function(action,add){
var fun=false,sign,$btn;
switch(action){
case "ALL":
fun=this.fill;
break;
case "DELETE":
sign="DELETE"+add;
fun=function(){BAK.fixed(add);};
BAK[sign]=fun;
break;
case "SHOW":
break;}
if(fun){
$btn=this.$top.find("#autoBackup"+action+add);
if($btn.length){
$btn.click(fun);}}
};
BAK.gui.full=function(){
var e,g,i,p,s,u,$x;
if(BAK.pages){
g=false;
s=""+BAK.pageID;
for(i in BAK.pages){
if(i!==s){
p=BAK.pages[i];
if(p.subject){
if(!this.reSpace){
this.reSpace=new RegExp(" +","g");}
e=" "+p.subject.replace(this.reSpace,"_");
}else{
e="";}
e=p.newest+" "+i+e;
if(g){
g.push(e);
}else{
g=[e];}}}
if(g){
s="<h3>"+BAK.cnf.feature("otherPages")+"</h3>\n<ul id='autoBackupPageList' />";
this.$div.append(s);
$x=this.$div.find("#autoBackupPageList");
u="<a target='_blank' href='"+mw.config.get("wgScript")+"?";
g.sort();
for(i=g.length-1;i>=0;i--){
e=g[i];
p=e.split(" ");
s=p[1];
if(s.substr(0,2)==="0_"){
s=s.substr(2);
s=u+"title="+mw.util.wikiUrlencode(s)+"&redirect=no'>"+s+"</a> ";
}else{
s="curid="+u+"curid="+s+"'>"+s+"</a> ";
if(p[2]){
s=s+"("+p[2].replace(/_/g," ")
.replace(/</g,"&lt;")+") ";}}
s="<li>"+s+BAK.util.focus(p[0])+"</li>";
$x.append(s);}}
this.fence(true);}
};
BAK.gui.further=function(){
if(this.$editform){
this.finish("wpDiff");
this.finish("wpPreview");
this.finish("wpSave");
this.$editform.find(".mw-summary").focusin(BAK.fresh);}
};
BAK.util.figure=function(ask){
return(/^[0-9]+$/).test(ask);
};
BAK.util.flat=function(album,amount,arrange){
var e,i,k,s;
if(arrange){
k=arrange.length-amount;
if(k>0){
arrange.sort();
for(i=0;i<=k;i++){
e=arrange[i];
s=e.split(" ");
delete album[s[1]];}}}
};
BAK.util.focus=function(appoint){
var r=appoint+" UTC",s=typeof BAK.cnf.justify,g;
if(s!=="boolean"&&s!=="number"){
BAK.cnf.justify=false;
if(typeof mw.user==="object"){
if(mw.user.options){
s=mw.user.options.get("timecorrection");
if(s){
g=/\|?([0-9]+)$/.exec(s);
if(g){
BAK.cnf.justify=parseInt(g[1],10);}}}}}
if(BAK.cnf.justify===0){
r=appoint;
}else if(typeof BAK.cnf.justify==="number"){
g=/^([0-9]+)-([01][0-9])-([0-3][0-9])T([0-2][0-9]):([0-6][0-9]):([0-6][0-9])$/.exec(appoint);
if(g){
r=Date.UTC(parseInt(g[1],10),parseInt(g[2],10)-1,parseInt(g[3],10),parseInt(g[4],10),parseInt(g[5],10)+BAK.cnf.justify,parseInt(g[6],10));
r=this.format(new Date(r));}}
return r;
};
BAK.util.format=function(appoint){
var r=appoint.getUTCFullYear()+"-",i=appoint.getUTCMonth()+1;
if(i<10){
i="0"+i;}
r=r+i+"-";
i=appoint.getUTCDate();
if(i<10){
i="0"+i;}
r=r+i+"T";
i=appoint.getUTCHours();
if(i<10){
i="0"+i;}
r=r+i+":";
i=appoint.getUTCMinutes();
if(i<10){
i="0"+i;}
r=r+i+":";
i=appoint.getUTCSeconds();
if(i<10){
i="0"+i;}
return r+i;
};
BAK.family=function(){
var env;
if(!this.pageID){
env=mw.config.get(["wgArticleId","wgPageName"]);
this.pageID=env.wgArticleId;
this.pageName=env.wgPageName;
if(this.pageID===0){
this.pageID="0_"+this.pageName.replace(/~/,"%7E");}}
};
BAK.fast=function(){
var r=true,s=window.sessionStorage.getItem(this.disk.storage),q;
if(s){
r=(s.length>1);
if(r){
if(!this.pageID){
this.pageID=mw.config.get("wgArticleId");}
r=(s.indexOf("~"+this.pageID+"~")>=0);
if(!r){
if(s.indexOf("~0_")>=0){
if(!BAK.pageName){
BAK.pageName=mw.config.get("wgPageName");}
q="0_"+BAK.pageName.replace(/~/,"%7E");
if(s.indexOf("~"+q+"~")>=0){
r=true;
this.later=true;}}}}}
return r;
};
BAK.filter=function(){
var lazy=true,store="~",hist,page,pid,revs,rid,sift,stamp;
this.cnf.fetch();
if(this.cnf.maxAge){
if(!this.now){
this.now=new Date();}
sift=Date.UTC(this.now.getUTCFullYear(),this.now.getUTCMonth(),this.now.getUTCDate(),this.now.getUTCHours()-this.cnf.maxAge,0,0);
sift=this.util.format(new Date(sift));
}else{
sift="1970-01-01T00:00:00";}
for(pid in this.pages){
page=this.pages[pid];
if(page.newest<sift){
delete this.pages[pid];
}else{
revs=false;
for(rid in page){
if(this.disk.stick.indexOf(rid)<0){
stamp=page[rid].newest;
if(stamp>sift){
stamp=stamp+" "+rid;
if(revs){
revs.push(stamp);
}else{
revs=[stamp];}
}else{
delete page[rid];}}}
if(revs){
page=page.newest+" "+pid;
if(hist){
hist.push(page);
}else{
hist=[page];}
this.util.flat(page,this.cnf.maxRev,revs);
store=store+pid+"~";
lazy=false;
}else{
delete this.pages[pid];}}}
this.util.flat(this.pages,this.cnf.maxPages,hist);
window.sessionStorage.setItem(this.disk.storage,store);
if(lazy&&this.gui.$div){
this.gui.fence();}
};
BAK.finalize=function(already,add){
var k=add.length,r=add,i;
for(i=k;i>0;i--){
if(add.charCodeAt(i-1)>32){
break;}}
if(i>1){
if(i<k){
r=add.substr(0,i);}
if(already){
if(i===already.length){
if(r===already){
r=false;}}}
}else{
r=false;}
return r;
};
BAK.find=function(){
var q=new mw.Api(),w={action:"query",
"continue":"",
pageids:this.pageID,prop:"revisions",rvlimit:3,rvuser:mw.util.wikiUrlencode(mw.config.get("wgUserName"))
};
if(this.later){
delete w.pageids;
w.titles=this.pageName;
q.get(w).done(this.founder);
}else{
q.get(w).done(this.found);}
};
BAK.first=function(){
this.$editform=false;
this.disk.fetch();
if(this.pages){
if(this.pages[this.pageID]){
this.find();}}
if(typeof mw.user==="object"){
if(mw.user.options&&mw.user.options.get("uselivepreview")){
this.livePreview=true;
$(mw).bind("LivePreviewDone",this.fresh);}}
this.gui.leading=true;
mw.hook("wikipage.content").add(this.fresh);
};
BAK.fixed=function(apply){
var p;
this.gui.$top.find("#autoBackupR"+apply).remove();
if(this.pages){
p=this.pages[BAK.pageID];
if(p){
if(p[apply]){
delete p[apply];
this.disk.launch=true;
this.flush();
if(!this.pages){
this.gui.fence(false);
}else if(!this.pages[BAK.pageID]){
this.gui.face(false);}}}}
};
BAK.flush=function(){
if(this.disk.launch){
if(this.pages){
this.filter();}
this.disk.flush();}
};
BAK.folder=function(){
BAK.gui.face();
if(!BAK.gui.$msg){
if(!BAK.cnf.self){
BAK.cnf.self=BAK.cnf.feature("BAKself");}
BAK.gui.$div.prepend("<h2>"+BAK.cnf.self+"</h2>");}
BAK.gui.fill();
BAK.gui.filter();
BAK.gui.full();
};
BAK.follow=function(){
var h,i,p;
this.disk.fetch();
if(this.later){
this.find();
}else if(this.pages){
p=this.pages[this.pageID];
if(p){
this.revID=mw.config.get("wgCurRevisionId");
for(h in p){
i=parseInt(h,10);
if(i){
if(this.revID>i){
this.find();
break;
}else if(this.revID===i){
this.gui.flag("pending","ALL");
break;}}}}}
};
BAK.found=function(arrived){
var query=(typeof arrived==="object"),learnt=false,i;
if(query){
query=arrived.query;
if(query){
query=query.pages[BAK.pageID];
if(query){
query=query.revisions;
if(query){
for(i=0;i<query.length;i++){
if(query[i].revid===BAK.revID){
learnt=true;
delete BAK.pages[BAK.pageID];
BAK.disk.launch=true;
break;}}}}}}
BAK.flush();
if(!learnt){
BAK.gui.flag("pending","ALL");}
};
BAK.founder=function(arrived){
var query=(typeof arrived==="object"),learnt=false;
if(query){
query=arrived.query;
if(query){
query=query.pages[BAK.pageID];
if(query){
BAK.pageID="0_"+BAK.pageName.replace(/~/,"%7E");
delete BAK.pages[BAK.pageID];
BAK.disk.launch=true;
learnt=true;}}}
BAK.flush();
if(!learnt){
BAK.gui.flag("pending","ALL");}
};
BAK.fresh=function(){
var shot=BAK.gui.feed(),page,revs,stamp;
if(shot){
BAK.family();
BAK.revID=mw.config.get("wgCurRevisionId");
if(!BAK.pages){
BAK.disk.fetch();}
if(!BAK.pages){
BAK.pages={};}
if(!BAK.pages[BAK.pageID]){
BAK.pages[BAK.pageID]={};}
page=BAK.pages[BAK.pageID];
if(!page[BAK.revID]){
page[BAK.revID]={};}
revs=page[BAK.revID].history;
if(revs){
shot=BAK.finalize(revs[0][1],shot);
if(shot){
BAK.now=new Date();
stamp=BAK.util.format(BAK.now);
BAK.cnf.fetch();
if(revs.length>=BAK.cnf.maxHist-1){
revs.pop();}
revs.unshift([stamp,shot]);}
}else{
shot=BAK.finalize(false,shot);
if(shot){
BAK.now=new Date();
stamp=BAK.util.format(BAK.now);
revs=[[stamp,shot]];}}
if(shot){
page[BAK.revID].history=revs;
page[BAK.revID].newest=stamp;
page.newest=stamp;
page.subject=BAK.pageName;
BAK.pages[BAK.pageID]=page;
BAK.disk.launch=true;
BAK.flush();}
if(BAK.cnf.msec){
if(BAK.timeoutID){
window.clearTimeout(BAK.timeoutID);}
BAK.timeoutID=window.setTimeout(BAK.fresh,BAK.cnf.msec);}}
};
BAK.further=function(){
if(!this.learnt){
this.learnt=true;
if(this.gui.$editform){
this.gui.further();
this.cnf.fetch();
if(this.cnf.msec){
window.setTimeout(this.fresh,this.cnf.msec);}}}
};
BAK.fire=function(){
var env=mw.config.get(["wgAction","wgIsArticle","wgUserName"]);
BAK.disk.storage=BAK.disk.self+" "+env.wgUserName;
if(env.wgIsArticle){
if(env.wgAction==="view"&&BAK.fast()){
BAK.follow();}
}else{
BAK.start=mw.util.getParamValue("action");
if(BAK.start){
BAK.learn=true;
if("|edit|submit|".indexOf(BAK.start)>0){
mw.hook("wikipage.content").add(BAK.firing);}}}
};
BAK.firing=function($all){
BAK.$page=$all;
switch(BAK.start){
case "submit":
BAK.gui.leading=true;
BAK.fresh();
BAK.further();
break;
case "edit":
BAK.first();
BAK.further();}
};
function first(){
var signature="ext.gadget."+BAK.type,rls;
if(mw.loader.getState(signature)!=="ready"){
rls={};
rls[signature]="ready";
mw.loader.state(rls);
if(mw.config.get("wgNamespaceNumber")>=0){
mw.loader.using(["user","mediawiki.api","mediawiki.user","mediawiki.util"],BAK.fire);}}}
first();
}(window.mediaWiki,window.jQuery));
/// EOF</nowiki>autoBackup/r.js