User:Zackmann08/scripts/AddCheckForUnknownParameters.js

//<nowiki> // Shamelessly copied from [[User:Frietjes/addcheckforunknownparameters.js]] jQuery(document).ready(function($) { // Disable in article namesp…

//<nowiki>
// Shamelessly copied from [[User:Frietjes/addcheckforunknownparameters.js]]
jQuery(document).ready(function($) {

// Disable in article namespace as it shouldn't ever be used there
if(mw.config.get('wgNamespaceNumber') != 0 && 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();
		});
	});
}
// -------------------------------------------------------------------------------- //
function wpBuildCheckForUnknownParameters()
{
	var already_added = false;
	var has_mapframe = false;
	var has_pushpin = false;
	var has_person_date = false;
	
	// Send {{unknown params category|template_title}} to the clipboard to make the next step easier
	// var template_title = mw.config.get('wgTitle');
	// template_title = template_title.replace(/(.*)\/sandbox/, "$1");
	// var paste_text = "{{unknown params category|" + template_title + "}}";
	
	// var dummy = $('<input>').val(paste_text).appendTo('body').select();
	// document.execCommand('copy');

  var mycontent = document.getElementById('wpTextbox1');
  // Copy the contents of the text window so we can modify it without problems
  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 ) {
	already_added = true;
	alert('Already added, attempting to replace. CHECK DIFF CAREFULLY');
  }
  
  if(mytxt.search(/\{\{#invoke:[Ii]nfobox[_ ]+mapframe/g) >= 0 ) {
	has_mapframe = true;
  }
  if(mytxt.search(/\{\{#invoke:[Ll]ocation[_ ]map\|infobox_pushpin_map/g) >= 0 ) {
	has_pushpin = true;
  }
  
  if(mytxt.search(/\{\{#invoke:[Pp]erson[_ ]+date/g) >= 0 ) {
	has_person_date = true;
  }
  
  // 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&#123;$2');
  mytxt = mytxt.replace(/([^\}])\}([^\}])/g, '$1&#125;$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
  var plist = []; var klist = {}; var loopcount = 0;
  while( (mytxt.search(/[^{}]/gm) >= 0) && (loopcount < 16) ) {
    var p = mytxt.match(/(\{\{\{[^{}\|]*\|?[^\{\}\|=]*\}\}\})/g);
    if( p ) {
       for(var j=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');
           var r = new RegExp("\\{\\{\\{[\\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(has_person_date) {
		if(plist.indexOf('birth_date') === -1) { plist.push('birth_date'); }
		if(plist.indexOf('death_date') === -1) { plist.push('death_date'); }
	}
	if( mytxt.search(/[\{\}]/gm) >= 0 ) {
		alert('Did not finish processing: ' + mytxt);
	}
	var alphasort = confirm('Found ' + plist.length + ' unique keys. Sort parameters alphabetically?');
	// Sort
	if(alphasort === true) {
		plist.sort(function (a, b) { 
			var aa = a.replace(/([^0-9])([0-9][^0-9]*)$/, '$10$2');
			var 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] } );
	}
	var myfullpagename = mw.config.get('wgPageName');
	myfullpagename = myfullpagename.replace(/_/g, ' ');
	myfullpagename = myfullpagename.replace(/\/sandbox$/, '');
	var mypagename = myfullpagename.replace(/^Template:/, '');
	mypagename = mypagename.charAt(0).toLowerCase() + mypagename.slice(1);
	mypagename = mypagename.replace(/^wikiProject/, 'WikiProject');
  
	// Attempt to remove the previous incarnation of {{#invoke:Check for unknown parameters
	if (already_added) {
		mycontent.value = mycontent.value.replace(/{{#invoke:[Cc]heck[_ ]+for[_ ]+unknown[_ ]+parameters[\W\w]*}}\s*<noinclude>/, '<noinclude>');
	}
  
	// Finally, build the blank template
	var mapframe_text = '';
	if (has_mapframe) {
		mapframe_text = '| mapframe_args = y ';
		// Remove any mapframe args that have snuck in because they are included in the template 
		const regex = /^\s*mapframe\-|^mapframe$/; // Matches any param starting with mapframe-
		plist = plist.filter(param => !regex.test(param));
	}
	var pushpin_text = '';
	if (has_pushpin) {
		pushpin_text = '| pushpin_map_args = y ';
		// Remove any pushpin_map args that have snuck in because they are included in the template 
		const regex = /^\s*pushpin_/; // Matches any param starting with pushpin_-
		plist = plist.filter(param => !regex.test(param));
	}
  
	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 '
    + mapframe_text + pushpin_text;
	for(var k=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');

	var mysummary;
	if (already_added) {
		mysummary = 'Updating unknown parameter tracking through ['
		+ '[:Category:Pages using ' + mypagename + ' with unknown parameters]] using ['
		+ '[User:Zackmann08/scripts/AddCheckForUnknownParameters.js|AddCheckForUnknownParameters]]'
		+ ' using [[Module:check for unknown parameters]]';
	} else {
		mysummary = 'Adding unknown parameter tracking through ['
		+ '[:Category:Pages using ' + mypagename + ' with unknown parameters]] using ['
		+ '[User:Zackmann08/scripts/AddCheckForUnknownParameters.js|AddCheckForUnknownParameters]]'
		+ ' and [[Module:check for unknown parameters]]';
	}
  
	var editsummary = document.getElementsByName('wpSummary')[0];
	if(typeof editsummary == 'object') {
		if (editsummary.value.indexOf(mysummary) == -1) {
			if (editsummary.value.match(/[^\*\/\s][^\/\s]?\s*$/)) {
				editsummary.value += '; ' + mysummary;
			} else {
				editsummary.value += mysummary;
			}
		}
	}
}
// -------------------------------------------------------------------------------- //
});
//</nowiki>

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.