User:Frietjes/addcheckforunknownparameters.js
Appearance
From Wikipedia, the free encyclopedia
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump.
This code will be executed when previewing this page.
This code will be executed when previewing this page.
Documentation for this user script can be added at User:Frietjes/addcheckforunknownparameters.
//<nowiki> jQuery(document).ready(function($){ if(mw.config.get('wgNamespaceNumber')!=-1&&document.getElementsByName('wpTextbox1')[0]){ mw.loader.using(['mediawiki.util']).done(function(){ varportletlink=mw.util.addPortletLink('p-tb','#','Chk4unkwn','t-cfu'); $(portletlink).click(function(e){ e.preventDefault(); wpBuildCheckForUnknownParameters(); }); }); } // -------------------------------------------------------------------------------- // functionwpBuildCheckForUnknownParameters() { varmycontent=document.getElementById('wpTextbox1'); // Copy the contents of the text window so we can modify it without problems varmytxt=mycontent.value; varmytxt2=mycontent.value; // Make sure we haven't already done this before if(mytxt.search(/\{\{#invoke:[Cc]heck[_ ]+for[_ ]+unknown[_ ]+parameters/g)>=0){ alert('Already added, aborting ...'); return; } // Remove {PAGENAME}, {BASEPAGENAME}, ... mytxt=mytxt.replace(/\{\{(?:PAGENAME|BASEPAGENAME|FULLPAGENAME|CURRENTYEAR)\}\}/g,''); // Remove <nowiki /> and <span /> mytxt=mytxt.replace(/<(?:span|nowiki)[ ]*\/>/gi,''); // Excaped table markup mytxt=mytxt.replace(/([\r\n])\{(\{\{)(!\}\})/g,'1ドル2ドル(3ドル'); mytxt=mytxt.replace(/([\r\n]\{\{!)(\}\})\}[\t ]*([\r\n])/g,'1ドル)2ドル3ドル'); // Replace some braces mytxt=mytxt.replace(/([^\{])\{([^\{])/g,'1ドル{2ドル'); mytxt=mytxt.replace(/([^\}])\}([^\}])/g,'1ドル}2ドル'); // Remove newlines and tabs which confuse the regexp search mytxt=mytxt.replace(/[\s]/gm,' '); // Compress whitespace mytxt=mytxt.replace(/[\s][\s]+/gm,' '); // Remove some HTML comments mytxt=mytxt.replace(/<!--(?:[^>]|[^-]>|[^-]->)*-->/gm,''); // Remove some includeonly tags mytxt=mytxt.replace(/<\/?includeonly>/gm,''); // Remove some noinclude sections mytxt=mytxt.replace(/<noinclude>(?:[^<>]|<\/?[^n][^<>]*>)*<\/noinclude>/gm,''); // Compress more whitespace mytxt=mytxt.replace(/\|[\s]+/g,'|'); mytxt=mytxt.replace(/(\{\{[^\{\}\|]*)[\s]+(\||\}\})/g,'1ドル2ドル'); // Remove wikilinks to make patterns less complicated mytxt=mytxt.replace(/\[\[[^\[\]\{\}]*\]\]/g,''); // Add a leading and trailing newline to make pattern matches less complicated mytxt='\n'+mytxt+'\n'; // Avoid false matches mytxt2=mytxt2.replace(/(\{\{\{[^\{\}\|]*)/g,'1ドル₳₳'); mytxt2=mytxt2.replace(/[\s]+₳/g,'₳'); // Now start extracting the parameters varplist=[];varklist={};varloopcount=0; while((mytxt.search(/[^{}]/gm)>=0)&&(loopcount<16)){ varp=mytxt.match(/(\{\{\{[^{}\|]*\|?[^\{\}\|=]*\}\}\})/g); if(p){ for(varj=0;j<p.length;++j){ p[j]=p[j].replace(/\{\{\{([^{}\|]*)\|?[^\{\}\|=]*\}\}\}/g,'1ドル'); p[j]=p[j].replace(/^[\s]+/,''); p[j]=p[j].replace(/[\s]+$/,''); p[j]=p[j].replace(/([\(\)])/g,'\\1ドル'); varr=newRegExp("\\{\\{\\{[\\s]*"+p[j].replace(/\?/g,'\\?')+"\\|?[^\\{\\}\\|=]*\\}\\}\\}",'g'); mytxt=mytxt.replace(r,''); p[j]=p[j].replace(/\\/g,''); if(klist[p[j]]==undefined){ klist[p[j]]=mytxt2.indexOf('{{{'+p[j]+'₳₳'); plist.push(p[j]); } } // Remove templates, parserfunctions, and other double brace expressions mytxt=mytxt.replace(/([^\{])\{\{[^{}]*\}\}/gm,'1ドル'); mytxt=mytxt.replace(/\{\{[^{}]*\}\}([^\}])/gm,'1ドル'); mytxt=mytxt.replace(/([^\{])\{\{(?:[\r\n]|[^{}]|\{\{[^{}]*\}\})*\}\}/gm,'1ドル'); mytxt=mytxt.replace(/\{\{(?:[\r\n]|[^{}]|\{\{[^{}]*\}\})*\}\}([^\}])/gm,'1ドル'); } loopcount++; } if(mytxt.search(/[\{\}]/gm)>=0){ alert('Did not finish processing: '+mytxt); }else{ alert('Found '+plist.length+' unique keys'); } varalphasort=confirm('Sort parameters alphabetically?'); // Sort if(alphasort===true){ plist.sort(function(a,b){ aa=a.replace(/([^0-9])([0-9][^0-9]*)$/,'10ドル2ドル'); bb=b.replace(/([^0-9])([0-9][^0-9]*)$/,'10ドル2ドル'); returnaa.toLowerCase().localeCompare(bb.toLowerCase())}); }else{ plist=plist.sort(function(a,b){returnklist[a]-klist[b]}); } myfullpagename=mw.config.get('wgPageName'); myfullpagename=myfullpagename.replace(/_/g,' '); myfullpagename=myfullpagename.replace(/\/sandbox$/,''); mypagename=myfullpagename.replace(/^Template:/,''); mypagename=mypagename.charAt(0).toLowerCase()+mypagename.slice(1); mypagename=mypagename.replace(/^wikiProject/,'WikiProject'); // Finally, build the blank template mytxt='{'+'{#invoke:' +'Check for unknown parameters|check|unknown='+'{'+'{main other|[' +'[Category:Pages using '+mypagename+' with unknown parameters' +'|_VALUE_{'+'{PAGENAME}'+'}]'+']}' +'}|preview=Page using ['+'['+myfullpagename+']'+'] with ' +'unknown parameter "_VALUE_"|ignoreblank=y'; for(vark=0;k<plist.length;++k){ mytxt=mytxt+"| "+plist[k]+" "; } mytxt=mytxt+"}}"; mycontent.value=mycontent.value+mytxt; // Move up mycontent.value=mycontent.value.replace(/([\r\n \t][\r\n \t]*)(\{\{#invoke:Check for unknown parameters(?:\{\{(?:\{\{[^{}]*\}\}|[^{}])*\}\}|[^{}])*\}\})/gi,'2ドル1ドル'); mycontent.value=mycontent.value.replace(/(<noinclude>(?:<!--[^<>]*-->|[^<>])*<\/noinclude>)(\{\{#invoke:Check for unknown parameters(?:\{\{(?:\{\{[^{}]*\}\}|[^{}])*\}\}|[^{}])*\}\})/gi,'2ドル1ドル'); mycontent.value=mycontent.value.replace(/([\r\n \t][\r\n \t]*)(\{\{#invoke:Check for unknown parameters(?:\{\{(?:\{\{[^{}]*\}\}|[^{}])*\}\}|[^{}])*\}\})/gi,'2ドル1ドル'); mysummary='Adding unknown parameter tracking through [' +'[:Category:Pages using '+mypagename+' with unknown parameters]]' +' using [[Module:check for unknown parameters]]'; vareditsummary=document.getElementsByName('wpSummary')[0]; if(typeofeditsummary=='object'){ if(editsummary.value.indexOf(mysummary)==-1){ if(editsummary.value.match(/[^\*\/\s][^\/\s]?\s*$/)){ editsummary.value+='; '+mysummary; }else{ editsummary.value+=mysummary; } } } } // -------------------------------------------------------------------------------- // }); //</nowiki>