/***********************************************************************************//* Edit counter script *//* author: User:Najzere @ strategywiki.org *//* source: http://strategywiki.org/wiki/User:Najzere/edit counter.js *//* documentation: http://strategywiki.org/wiki/User talk:Najzere/edit counter.js *//* документация на русском: http://www.peeep.us/1f79e75f *//* contact: http://strategywiki.org/wiki/User talk:Najzere *//* license: CC-BY-SA 3.0 (http://creativecommons.org/licenses/by-sa/3.0/) *//***********************************************************************************/// Only add edit count button on user pagesif(mw.config.get('wgTitle')==mw.config.get('wgUserName')&&mw.config.get('wgNamespaceNumber')==2){addOnloadHook(makeEditCountButton);}// Add a new link after "User contributions" in the toolboxfunctionmakeEditCountButton(){if(!document.getElementById('t-contributions')){alert('Expected element not found. Please see http://strategywiki.org/wiki/User_talk:Najzere to have your wiki added.');return;};varnewlist=document.createElement('li');newlist.id="t-editcounter";varnewlink=document.createElement('a');newlink.href='javascript:countEdits()';varlinkname=document.createTextNode('Edit counter');newlink.appendChild(linkname);newlist.appendChild(newlink);document.getElementById('t-contributions').parentNode.insertBefore(newlist,document.getElementById('t-contributions').nextSibling);}functioncountEdits(){// Change edit counter link to let the user know it's thinkingdocument.getElementById('t-editcounter').getElementsByTagName('a')[0].innerHTML='Calculating...';// Set up variablesif(mw.config.get('wgServer')=='http://strategywiki.org'){vareditSummary='Обновлено с помощью [[Участник:Fauust/edit counter.js|edit counter.js]]';vartableTop='[[Счётчик правок|Editcount]]';}else{vareditSummary='Обновлено с помощью [[Участник:Fauust/edit counter.js|edit counter.js]]';vartableTop='<span class="plainlinks">[[Участник:Fauust/edit counter.js|Editcount]]</span>';};if(typeofeditPage=='undefined'){editPage='Edit count'};if(typeoftableClass=='undefined'){tableClass=''};if(typeoftableStyle=='undefined'){tableStyle=''};if(editPage==''){editPage='Edit count'};varskipcaptcha=false;varoldpage=false;varedits;varnamespaces=newArray();varmovelog=newArray();varprotectlog=newArray();varnext;varedittoken;varbrace='{';vartable='{| class="wikitable '+tableClass+'" style="'+tableStyle+'"\n!colspan="2"| '+tableTop+': '+brace+brace+'subst:formatnum:';// Check if the user is autoconfirmedfor(varrightinmw.config.get('wgUserGroups'))if(mw.config.get('wgUserGroups')[right]=='autoconfirmed'){skipcaptcha=true};// Get full edit count, as tracked by MW, add it to tablequeryApi('action=query&meta=userinfo&uiprop=editcount',function(uiResponse){table+=uiResponse.query.userinfo.editcount+'}}';});// Initialize namespace, move log and patrol log arraysqueryApi('action=query&meta=siteinfo&siprop=namespaces',function(siResponse){for(varnsinsiResponse.query.namespaces){if(siResponse.query.namespaces[ns].id>-1){if(siResponse.query.namespaces[ns].id!=namespaces.length)break;if(siResponse.query.namespaces[ns].id==0){namespaces[siResponse.query.namespaces[ns].id]='Main';}else{namespaces[siResponse.query.namespaces[ns].id]=siResponse.query.namespaces[ns].canonical;};movelog[siResponse.query.namespaces[ns].id]=0;protectlog[siResponse.query.namespaces[ns].id]=0;};};});// Fill move log arraynext='';while(next!='stop'){queryApi('action=query&list=logevents&letype=move&leuser='+mw.config.get('wgUserName')+'&lelimit=max&leprop=title'+next,function(moveResponse){for(vareventinmoveResponse.query.logevents){movelog[moveResponse.query.logevents[event].ns]+=1;}if(moveResponse["query-continue"]){next='&lestart='+moveResponse["query-continue"].logevents.lestart;}else{next='stop';};});}// Fill patrol log arraynext='';while(next!='stop'){queryApi('action=query&list=logevents&letype=protect&leuser='+mw.config.get('wgUserName')+'&lelimit=max&leprop=title'+next,function(protectResponse){for(vareventinprotectResponse.query.logevents){protectlog[protectResponse.query.logevents[event].ns]+=1;}if(protectResponse["query-continue"]){next='&lestart='+protectResponse["query-continue"].logevents.lestart;}else{next='stop';};});}// Loop through namespaces counting edits and subtracting page moves and patrolsfor(vari=0;i<namespaces.length;i++){next='';edits=0;while(next!='stop'){queryApi('action=query&list=usercontribs&ucuser='+mw.config.get('wgUserName')+'&uclimit=max&ucdir=newer&ucnamespace='+i+next,function(ucResponse){edits+=ucResponse.query.usercontribs.length;if(ucResponse["query-continue"]){next='&ucstart='+ucResponse["query-continue"].usercontribs.ucstart;}else{next='stop';};});};edits=edits-movelog[i]-protectlog[i];if(edits>0){table+='\n|-\n| '+namespaces[i]+' || '+brace+brace+'subst:formatnum:'+edits+'}}';};};table+='\n|-\n|colspan="2" align="center"| <small>Последнее обновление: '+brace+brace+'subst:#time:F j, Y}}</small>\n|}';// Get edit token and check if we'll be creating a new page (in the case of non-autoconfirmed users)queryApi('action=query&prop=info|revisions&intoken=edit&titles=User:'+mw.config.get('wgUserName')+'/'+editPage,function(propResponse){for(varpageinpropResponse.query.pages){edittoken=propResponse.query.pages[page].edittoken.replace(/\+\\$/g,'%2B%5C');if(propResponse.query.pages[page]["revisions"]){oldpage=true;};};});// If the user is not autoconfirmed and we're creating a new page, kick out of the scriptif(!skipcaptcha&&!oldpage){alert("Sorry, you're not autoconfirmed yet, so captcha is required to make new pages. Go create the page at: User:"+mw.config.get('wgUserName')+"/"+editPage+", then try again.");document.getElementById('t-editcounter').getElementsByTagName('a')[0].innerHTML='Error!';return;};// Edit the target page with the table, change edit count link to reflect the status of the page savequeryApi('action=edit&title=User:'+mw.config.get('wgUserName')+'/'+editPage+'&summary='+editSummary+'&minor=1&recreate=1&bot=1&text='+table+'&token='+edittoken,function(actionResponse){if(actionResponse.edit.result=="Success"){document.getElementById('t-editcounter').getElementsByTagName('a')[0].innerHTML='Done!';}else{document.getElementById('t-editcounter').getElementsByTagName('a')[0].innerHTML='Error!';};});}// Accepts an API query and callback function for handling queries from the main functionfunctionqueryApi(query,callback){try{varxmlRequest=newXMLHttpRequest();}catch(e){try{varxmlRequest=newActiveXObject("Msxml2.XMLHTTP");}catch(e){try{varxmlRequest=newActiveXObject("Microsoft.XMLHTTP");}catch(e){throw"Could not create an XmlHttpRequest";}}}xmlRequest.open('POST',mw.config.get('wgScriptPath')+'/api.php',false);xmlRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");xmlRequest.send(query+'&format=json');varxmlResponse=newFunction("return "+xmlRequest.responseText)();if(!xmlResponse){alert('Problem retrieving information');}else{callback(xmlResponse);};}