User:Frietjes/addcheckforunknownparameters.js: Difference between revisions

Content deleted Content added
remove extra alert
No edit summary
 
(17 intermediate revisions by 3 users not shown)
Line 1:
//<nowiki>
jQuery(document).ready(function($) {
if(mw.config.get('wgNamespaceNumber') != -1 && document.getElementsByName('wpTextbox1')[0]) {
mw.loader.using(['mediawiki.util']).done( function() {
var portletlink = mw.util.addPortletLink('p-tb','#','Chk4unkwn','t-cfu');
$(portletlink).click(function(e) {
e.preventDefault();
wpBuildCheckForUnknownParameters();
});
});
}
// -------------------------------------------------------------------------------- //
Line 15 ⟶ 18:
var mytxt = mycontent.value;
var mytxt2 = 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])\{(\{\{)(!\}\}])/gmg, '\n<!-- UNPROCESSED HERE -->$1$2($3');
mytxt = mytxt.replace(/([\r\n]\{\{!)(\}\})\}[\t ]*([\r\n])/g, '$1)$2$3');
// Replace some braces
mytxt = mytxt.replace(/([^\{])\{([^\{])/g, '$1&#123;$2');
Line 30 ⟶ 43:
// 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';
Line 38 ⟶ 56:
// Now start extracting the parameters
var plist = []; var klist = {}; var loopcount = 0;
while( (mytxt.search(/[^{}]/gm) >= 0) && (loopcount < 1016) ) {
var p = mytxt.match(/(\{\{\{[^{}\|]*\|?[^\{\}\|=]*\}\}\})/g);
if( p ) {
Line 46 ⟶ 64:
p[j] = p[j].replace(/[\s]+$/, '');
p[j] = p[j].replace(/([\(\)])/g, '\\$1');
var r = new RegExp("\\{\\{\\{[\\s]*" + p[j].replace(/\?/g, '\\?') + "\\|?[^\\{\\}\\|=]*\\}\\}\\}", 'g');
mytxt= mytxt.replace(r, '');
p[j] = p[j].replace(/\\/g, '');
Line 55 ⟶ 73:
}
// 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 ) {
mytxt = mytxt.replace(/([\{\}])/gm, '\n<!-- UNPROCESSED HERE -->$1');
alert('Did not finish processing: ' + mytxt);
} else {
Line 68 ⟶ 87:
var alphasort = 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');
return aa.toLowerCase().localeCompare(bb.toLowerCase()) });
} else {
plist = plist.sort(function(a,b) { return klist[a] - klist[b] } );
Line 75 ⟶ 97:
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:'
Line 111 ⟶ 135:
// -------------------------------------------------------------------------------- //
});
//</nowiki>