User:Zackmann08/AddCheckForClobberedParameters.js: Difference between revisions – Wikipedia

From Wikipedia, the free encyclopedia

Content deleted Content added


Line 80: Line 80:

new_param = new_param + params[j][1] +’; ‘;

new_param = new_param + params[j][1] +’; ‘;

}

}

new_param.replace(/;([^;]*)$/, ‘$1’)

new_param.replace(/;([^;]*)$/, ‘$1’)

mytxt = mytxt + new_param +’\n’;

mytxt = mytxt + new_param +’\n’;

}

}


Revision as of 07:21, 19 November 2025

//<nowiki>
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','#','Chk4Clob','t-cfu');
		$(portletlink).click(function(e) {
			e.preventDefault();
			wpBuildCheckForClobberedParameters();
		});
	});
}
// -------------------------------------------------------------------------------- //
function wpBuildCheckForClobberedParameters()
{
	var already_added = false;
	
	// Send {{clobbered parameters category|template_title}} to the clipboard to make the next step easier
	var template_title = document.title;
	var paste_text = template_title.replace(/Editing\sTemplate\:(.*)\s\-\sWikipedia.*/, '$1');
	paste_text = paste_text.replace(/(.*)\/sandbox/, '{{clobbered parameters category|$1}}');
	
	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 original_text = mycontent.value;
	// Make sure we haven't already done this before
	if(mytxt.search(/\{\{#invoke:[Cc]heck[_ ]+for[_ ]+clobbered[_ ]+parameters/g) >= 0 ) {
  		already_added = true;
  		alert('Already added, attempting to replace. CHECK DIFF CAREFULLY');
	}
	
	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');
  
	// // 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
  
	mytxt = '{' + '{#invoke:'
    + 'Check for clobbered parameters|check\n'
    + '| template = [[' + myfullpagename + ']]\n'
    + '| cat = {' + '{main other|Category:Pages using ' + mypagename + ' with conflicting parameters}}\n';
    
    const alias_regex = /\|\s*\_alias\-map\s*=\s*([^\|]+)/;
	var alias_map = original_text.match(alias_regex);
	var aliases = [];
	if (alias_map[1]) {
		aliases = alias_map[1].split(',');
		for (let i = 0; i < aliases.length; i++) {
			mytxt = mytxt + '| ' + aliases[i].trim().split(':')[0] + '; ' + aliases[i].trim().split(':')[1] + '\n';
		}
	}
	
	const if_empty_regex = /(\{\{if empty\|.*\}\})/gmi;
	// var allMatches = original_text.matchAll(if_empty_regex);
	const allMatches = [...original_text.matchAll(if_empty_regex)];
	

	//TODO: Check for no matches...
	if (allMatches[0] && allMatches[0][0]) {
		for (let i = 0; i<allMatches.length; i++){
			var match = allMatches[i][0];
			match = match.replace(/\|\}\}\}/g, '}}}');
			
			const params = [...match.matchAll(/\{\{\{([\w\s\-]+)\}\}\}/gm)];
			var new_param = '| ';
			for (let j = 0; j<params.length; j++){
				new_param = new_param + params[j][1] +'; ';
			}
			new_param.replace(/;([^;]*)$/gm, '$1');
			mytxt = mytxt + new_param +'\n';
		}
	}
	mytxt = mytxt + "}}";
	
	alert('Clobbered param check added with ' + aliases.length + ' parameters.');

	mycontent.value = mycontent.value + mytxt;
	// Move up
	mycontent.value = mycontent.value.replace(/([\r\n \t][\r\n \t]*)(\{\{#invoke:Check for clobbered parameters(?:\{\{(?:\{\{[^{}]*\}\}|[^{}])*\}\}|[^{}])*\}\})/gi, '$2$1');
	mycontent.value = mycontent.value.replace(/(<noinclude>(?:<!--[^<>]*-->|[^<>])*<\/noinclude>)(\{\{#invoke:Check for clobbered parameters(?:\{\{(?:\{\{[^{}]*\}\}|[^{}])*\}\}|[^{}])*\}\})/gi, '$2$1');
	mycontent.value = mycontent.value.replace(/([\r\n \t][\r\n \t]*)(\{\{#invoke:Check for clobbered parameters(?:\{\{(?:\{\{[^{}]*\}\}|[^{}])*\}\}|[^{}])*\}\})/gi, '$2$1');

	if (already_added) {
  		mysummary = 'Updating clobbered parameter tracking through ['
  			+ '[:Category:Pages using ' + mypagename + ' with conflicting parameters]] using ['
    		+ '[User:Zackmann08/AddCheckForClobberedParameters.js|AddCheckForClobberedParameters]]'
    		+ ' using [[Module:check for clobbered parameters]]';
	} else {
		mysummary = 'Adding clobbered parameter tracking through ['
    		+ '[:Category:Pages using ' + mypagename + ' with conflicting parameters]] using ['
    		+ '[User:Zackmann08/AddCheckForClobberedParameters.js|AddCheckForClobberedParameters]]'
    		+ ' and [[Module:check for clobbered 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>

Leave a Comment

Your email address will not be published. Required fields are marked *

Exit mobile version