User:AzaToth/testcase.js

From Wikipedia, the free encyclopedia
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.
/*
	Testcase for possible bug (somewhere)
	To use, add following to your monobook.js:
  		importScript('User:AzaToth/morebits.js');
		importScript('User:AzaToth/testcase.js');
	Then to run, click on the testcase button in your action bar.

 */

function testCaseButton() {
	mw.util.addPortletLink( 'p-cactions', "javascript:testcase()", "testcase", "tw-testcase", "Testcase", "");
}
addOnloadHook(testCaseButton);

function testcase() {


	Window = new SimpleWindow( 600, 300 );
	Window.setTitle( "Testing testing" );
	div = document.createElement( 'div' );
	Window.setContent( div );
	Window.display();
	Status.init( div );


	wgPageName = wgPageName.replace( '_', ' ' ); // for queen/king/whatever and country!

	var xmlhttp = sajax_init_object();
	xmlhttp.query = {
		'action': 'query',
		'list': 'imageusage',
		'titles': 'Image:Example.png',
		'iulimit': 50, 
		'format': 'xml'
	};
	var link = document.createElement( 'a' );
	link.setAttribute( 'href', mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?' + QueryString.create( xmlhttp.query ) );
	link.appendChild( document.createTextNode( mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?' + QueryString.create( xmlhttp.query ) ) );
	Status.info( ["going for ", link ] );
	xmlhttp.overrideMimeType('text/xml');		
	xmlhttp.open( 'GET' , mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?' + QueryString.create( xmlhttp.query ), true);
	xmlhttp.onload = function() {

		var xmlDoc = this.responseXML;
		var nsResolver = xmlDoc.createNSResolver( xmlDoc.ownerDocument == null ? xmlDoc.documentElement : xmlDoc.ownerDocument.documentElement);
		var count = xmlDoc.evaluate( 'count(//imageusage/iu)', xmlDoc, nsResolver, XPathResult.ANY_TYPE, null );

		Status.info( "got " + count.numberValue + " nodes" );

		var snapshot = xmlDoc.evaluate('//imageusage/iu/@title', xmlDoc, nsResolver, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null );

		for ( var i = 0; i < snapshot.snapshotLength; ++i ) {
			var title = snapshot.snapshotItem(i).value;
			var xmlhttp = sajax_init_object();
			Status.info( 'title: ' + title );
			xmlhttp.current = title;
			xmlhttp.query = {
				'title': title,
				'action': 'submit'
			};
			xmlhttp.overrideMimeType('text/xml');
			xmlhttp.open( 'GET' , mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/index.php?' + QueryString.create( xmlhttp.query ), true);
			xmlhttp.onload = function() {


				var form = this.responseXML.getElementById('editform');
				var text = form.wpTextbox1.value;
				var old_text = text;

				var re = new RegExp( "(\\[\\[Image:Example.png.*?)(\\]\\])", 'g' ); // simple regexp, might not always work
				text = text.replace( re, "$1\{\{ifdc|Image:Example.png\}\}$2" );
				if( text == old_text ) {
					Status.info( 'No tag for ' + this.current );
					return;
				} else {
					Status.info( 'Added tag for ' + this.current );
				}

			}				
			xmlhttp.send( null );	

		}

	}
	xmlhttp.send( null );
}