Benutzer:Der Buckesfelder/autobackup.js

aus Wikipedia, der freien Enzyklopädie

Hinweis: Leere nach dem Veröffentlichen den Browser-Cache, um die Änderungen sehen zu können.

  • Firefox/Safari: Umschalttaste drücken und gleichzeitig Aktualisieren anklicken oder entweder Strg+F5 oder Strg+R (⌘+R auf dem Mac) drücken
  • Google Chrome: Umschalttaste+Strg+R (⌘+Umschalttaste+R auf dem Mac) drücken
  • Internet Explorer/Edge: Strg+F5 drücken oder Strg drücken und gleichzeitig Aktualisieren anklicken
  • Opera: Strg+F5
/// en:User:PerfektesChaos/js/autoBackup/r.js
/// 2013-08-23 PerfektesChaos@de.wikipedia
/// Fingerprint:#0#7A0845C6#
/// @license GPL [//www.mediawiki.org/w/COPYING](+GFDL,LGPL,CC-BY-SA)
///<nowiki>
(function(mw,$){
"use strict";
var BAK,vsn=1.3;
if(typeof mw.libs.autoBackup!=="object"||!mw.libs.autoBackup){
mw.libs.autoBackup={};}
BAK=mw.libs.autoBackup;
BAK.doc="[[w:en:User:PerfektesChaos/js/autoBackup]]";
BAK.type="autoBackup";
BAK.vsn=vsn;
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={};}
if(typeof BAK.debugging!=="object"){
BAK.debugging={loud:true,say:"autoBAK"};}
BAK.cnf.text={
"BAKself":{"en":"AutoBackup","de":"AutoBackup"},"IntparseJSON":{"en":"Internal ERROR -- parseJSON","de":"Interner FEHLER -- parseJSON"},"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("IntparseJSON",false);
window.localStorage.setItem(this.storage,"");}}
}else{
BAK.gui.flag("NoLSavail",false);
this.lock=true;}
};
BAK.disk.flush=function(){
var s,store=false;
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=$.toJSON(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=mw.util.$content;}}}
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 script="javascript",shoot=script+":window.mw.libs.autoBackup.fresh();void(0);";
if(!BAK.cnf.self){
BAK.cnf.self=BAK.cnf.feature("BAKself");}
mw.util.addPortletLink("p-cactions",shoot,BAK.cnf.self,"ca-AutoBackup",null,null,null);
};
BAK.gui.feed=function(){
var r=false;
var $form;
var $ta;
if(this.leading){
this.leading=false;
this.$editform=false;
this.$textarea=false;
$form=mw.util.$content.find("#editform");
if($form){
$ta=mw.util.$content.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];
var revs=false;
var 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(" ");
var i;
var k=e[1];
var s="autoBackupR"+k;
var w;
var $div;
var $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;
var sign;
var $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 g;
var r=appoint+" UTC";
var s;
if(BAK.cnf.justify!==false){
if(typeof BAK.cnf.justify!=="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()+"-";
var 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(){
if(!this.pageID){
this.pageID=mw.config.get("wgArticleId");
this.pageName=mw.config.get("wgPageName");
if(this.pageID===0){
this.pageID="0_"+this.pageName.replace(/~/,"%7E");}}
};
BAK.fast=function(){
var q,r=true,s=window.sessionStorage.getItem(this.disk.storage);
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 hist,page,pid,revs,rid,sift,stamp;
var lazy=true;
var store="~";
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 i;
var k=add.length;
var r=add;
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();
var w={action:"query",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){
if(mw.user.options.get("uselivepreview")){
this.livePreview=true;
$(mw).bind("LivePreviewDone",this.fresh);}}}
this.gui.leading=true;
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");
var learnt=false;
var 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");
var 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 page;
var revs;
var shot=BAK.gui.feed();
var 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 s;
BAK.disk.storage=BAK.disk.self+" "+mw.config.get("wgUserName");
if(mw.config.get("wgIsArticle")){
if(mw.config.get("wgAction")==="view"){
if(BAK.fast()){
BAK.follow();}}
}else{
s=mw.util.getParamValue("action");
if(s){
BAK.learn=true;
if(s==="submit"){
BAK.gui.leading=true;
BAK.fresh();
BAK.further();
}else if(s==="edit"){
BAK.first();
BAK.further();}}}
};
if(!BAK.loaded){
BAK.loaded=true;
if(mw.config.get("wgNamespaceNumber")>=0){
$(window.document).ready(function(){
mw.loader.using(["user","mediawiki.api","mediawiki.user","mediawiki.util","jquery.json"],BAK.fire);
});}
mw.loader.state("ext.gadget.autoBackup","ready");}
}(window.mediaWiki,window.jQuery));
/// EOF</nowiki>autoBackup/r.js