Jump to content

User:Ale jrb/Scripts/csdcheck.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Ale jrb (talk | contribs) at 00:22, 19 December 2008 (..). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
var csdCheckEnable = true;

// CSD thing
if ((wgAction == 'delete') && (csdCheckEnable == true)) { 
function runCsd() {
	var list = document.getElementById('wpDeleteReasonList');
    var td = document.createElement('td');
    td.setAttribute('id', 'csdCheck');
    document.getElementById('wpReason').parentNode.appendChild(td);
	document.getElementById('csdCheck').innerHTML = 'Checking for CSD tags...';
	
	ajaxPageContent(wgPageName);
    
	
	
	function ajaxDone(content) {
		// reasons_regex taken from existing sysop script, because it works well. :)
		var reasons_regex = {
			 'G10': // copied from [[User:Ilmari Karonen]]'s script, covers more bases than the others since it's more important (this script should blank the summary in _all_ cases where the other script does
		/\{\{\s*(msg:\s*)?(template:\s*)?(db-a(p|tk)?|((csd|db)[-:])?(a6|attack(pages?|template)?|g10))\s*[|}]/i,
		// other regexes are simpler and cover probably 99% of actual tags.
			 'G1': /\{\{\s*(db-|csd[-:])?(nonsense|ns|g1)\s*\}\}/i,
			 'G2': /\{\{\s*(db-|csd[-:])?(test|g2)\s*\}\}/i,
			 'G3': /\{\{\s*(db-|csd[-:])?(vandalism|vandal|vand|g3)\s*\}\}/i,
			 'G4': /\{\{\s*((db-|csd[-:])?(repost|g4)|deleteagain)\s*\}\}/i,
			 'G5': /\{\{\s*(db-|csd[-:])?(banned|ban|g5)\s*\}\}/i,
			 'G6': /\{\{\s*(db-|csd[-:])?(maintenance|move|histmerge|g6)\s*\}\}/i, // db-move and db-histmerge should be covered elsewhere if possible
			 'G7': /\{\{\s*(db-|csd[-:])?(author|blanked|g7)\s*\}\}/i,
			 'G8': /\{\{\s*(db-|csd[-:])?(talk|g8|redirnone|r1)\s*\}\}/i,
			 // G9 is for office, there is no tag.
			 // G10 is covered above
			 'G11': /\{\{\s*(db-|csd[-:])?(spam|ad|advert|g11)\s*\}\}/i,
			 'G12': /\{\{\s*(db-|csd[-:])?(copyvio|cv|cvio|g12|a8)\s*(\}\}|\|)/i,
		 
			 'A1': /\{\{\s*(db-|csd[-:])?((no)?context|a1)\s*\}\}/i,
			 'A2': /\{\{\s*(db-|csd[-:])?(foreign|a2)\s*\}\}/i,
			 'A3': /\{\{\s*(db-|csd[-:])?((no)?content|a3|contact|a4)\s*\}\}/i,
			 // A4 = A3
			 'A5': /\{\{\s*(db-|csd[-:])?(transwiki|a5)\s*\}\}/i,
			 // A6 = G10
			 'A7 (bio)': /\{\{\s*(nn-|db-|csd[-:])?(a7|notability|bio|person)\s*\}\}/i,
			 'A7 (web)': /\{\{\s*(nn-|db-|csd[-:])?(web(site)?|inter(net|tubes))\s*\}\}/i,
			 'A7 (group)': /\{\{\s*(db-|csd[-:]|nn-)?(band|club|music|inc|corp|comp(any)?)\s*\}\}/i,
					  // TODO separate for music/band?
			 // A8 = G12
			 'A9': /\{\{\s*(db-|csd[-:]|nn-)?(a9|album)\s*\}\}/i,
		 
			 'C1': /(\{\{\s*(db-|csd[-:])?(catempty|emptycat|c1)\s*\}\}|empty category)/i,
			 'U1': /\{\{\s*(db-|csd[-:])?(userreq|u1|user)\s*\}\}/i,
			 'I2': /\{\{\s*(db-|csd[-:])?(noimage|i2)\s*\}\}/i,
		 
			 // no point in including the other image categories here, this will only work
			 // when deleting a description page with no image
		 
			 // R1 = G8
			 'R2': /\{\{\s*(db-|csd[-:])?(rediruser|r2)\s*\}\}/i,
			 'R3': /\{\{\s*(db-|csd[-:])?(redirtypo|r3)\s*\}\}/i,
		 
			 'dummy': /this will never be reached, it is there to allow every entry above to end in a comma/
		  }
			
			
		for (var category in reasons_regex) {
			if (reasons_regex[category].test(content)) {
				var option = findCSD(category);
				list.selectedIndex = option.index;
				break;
			}
		}
		
		document.getElementById('csdCheck').parentNode.removeChild(document.getElementById('csdCheck'));
		
		function findCSD(s) { // s e.g. "U1", "G10"
			for(var i=0;i<list.options.length;i++) {
			   var o=list.options[i];
			   if (o.text.indexOf("CSD "+s+":") == 0) return o;
			   if (o.text.indexOf(s+":") == 0) return o;
			   //TODO fine-tuning for A7 and G6
			}
		  }

	}
	
	function ajaxPageContent(page) {
		var page_request = false;
		
		if (window.XMLHttpRequest)
			page_request = new XMLHttpRequest();
		else if (window.ActiveXObject){ 
			try {
			page_request = new ActiveXObject("Msxml2.XMLHTTP");
			} 
			catch (e){
				try{
				page_request = new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch (e){}
			}
		}
		else
		{
			return false;
		}
		
		var url = wgServer + '/w/api.php';
		url = url + '?action=query&prop=revisions&titles='+page+'&rvlimit=1&rvprop=content&format=txtfm'
		
		page_request.onreadystatechange = function() {
			if (page_request.readyState == 4) {
				 if (page_request.status == 200) {
					var page_content = page_request.responseText;
					var pos = page_content.indexOf('[*] =&gt;');
					page_content = page_content.substr(pos, 250);
					
					ajaxDone(page_content);
				 } else {
					document.getElementById('csdCheck').innerHTML = 'AJAX request failed.';
					return false;
				 }
			  }
		}
		//page_request.overrideMimeType('text/xml');
		page_request.open('GET', url, true);
		page_request.send(null);
	}
} 
hookEvent('load', runCsd);
}