Jump to content

User:AzaToth/testcase.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by AzaToth (talk | contribs) at 18:47, 1 June 2007 (testcase). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
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.
// If TwinkleConfig aint exist.
if( typeof( TwinkleConfig ) == 'undefined' ) {
	TwinkleConfig = function() {};
}

/**
 TwinkleConfig.summaryAd (string)
 If ad should be added or not to summary, default [[WP:TWINKLE|TWINKLE]]
 */
if( typeof( TwinkleConfig.summaryAd ) == 'undefined' ) {
	TwinkleConfig.summaryAd = " using [[WP:TWINKLE|TW]]";
}

/**
 TwinkleConfig.toolboxButtons (string)
 If id defined in this array, the button of the action is located inthe toolbox instead of in
 the actions bar.
 */
if( typeof( TwinkleConfig.toolboxButtons ) == 'undefined' ) {
	TwinkleConfig.toolboxButtons = [];
}

function twinkleXfdButton() {
	if( wgNamespaceNumber < 0 || wgCurRevisionId == false ) {
		return;
	}
	addPortletLink( chooseBox( 'tw-xfd', TwinkleConfig.toolboxButtons ), "javascript:testcase()", "testcase", "tw-testcase", "Testcase", "");
}
addOnloadHook(twinkleXfdButton);
function num2order( num ) {
	switch( num ) {
	case 1: return '';
	case 2: return '2nd';
	case 3: return '3rd';
	default: return num + 'th';
	}
}
function createOption( data ) {
	var option = document.createElement( 'option' );
	option.setAttribute( 'value', data.value );
	if( data.selected ) {
		option.setAttribute( 'selected', 'selected' );
	}
	option.setAttribute( 'label', data.label );
	option.appendChild( document.createTextNode( data.label ) );

	return option;
}

function createInput( data ) {
	var div = document.createElement( data.elem || 'span' );
	var input = document.createElement( 'input' );
	input.setAttribute( 'value', data.value );
	input.setAttribute( 'name', data.name );
	input.setAttribute( 'id', data.id );
	input.setAttribute( 'type', data.type );
	if( data.size ) {
		input.setAttribute( 'size', data.size );
	}
	if( data.checked ) {
		input.setAttribute( 'checked', 'checked' );
	}
	if( data.disabled ) {
		input.setAttribute( 'disabled', 'disabled' );
	}
	if( data.event && data.type == 'button' ){
		input.addEventListener( 'click', data.event, true );
	}
	if( data.type == 'button' || data.type == 'submit' || data.type == 'reset' ) {
		div.appendChild( input );
	} else {
		var label = document.createElement( 'label' );
		label.setAttribute( 'for', data.id );
		label.appendChild( document.createTextNode( data.text ) );
		if( [ 'radio', 'checkbox' ].indexOf( data.type ) != -1 ) {
			div.appendChild( input );
			div.appendChild( label );
		} else {
			div.appendChild( label );
			div.appendChild( input );
		}
	}

	return div;
}


function testcase() {


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


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

	Status.status( 'Tagging all pages including said image' );

	var xmlhttp = sajax_init_object();
	xmlhttp.query = {
		'action': 'query',
		'list': 'imageusage',
		'titles': wgPageName,
		'iulimit': 50, // test 500, // Maximum, if it's more than 500 images, ignore the rest for now...
		'format': 'xml'
	};
	var link = document.createElement( 'a' );
	link.setAttribute( 'href', wgServer + wgScriptPath + '/api.php?' + QueryString.create( xmlhttp.query ) );
	link.appendChild( document.createTextNode( wgServer + wgScriptPath + '/api.php?' + QueryString.create( xmlhttp.query ) ) );
	Status.info( ["going for ", link ] );
	xmlhttp.overrideMimeType('text/xml');		
	xmlhttp.open( 'GET' , wgServer + 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();
			xmlhttp.current = title;
			xmlhttp.query = {
				'title': title,
				'action': 'submit'
			};
			xmlhttp.overrideMimeType('text/xml');
			xmlhttp.open( 'GET' , wgServer + 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( "(\\[\\[" + wgPageName + ".*?)(\\]\\])", 'g' ); // simple regexp, might not always work
				text = text.replace( re, "$1\{\{ifdc|" + wgPageName + "\}\}$2" );
				if( text == old_text ) {
					Status.info( 'No tag for ' + this.current );
					return;
				} else {
					Status.info( 'Added tag for ' + this.current );
				}
				var postData = {
					'wpMinoredit': form.wpMinoredit.checked,
					'wpWatchthis': form.wpWatchthis.checked,
					'wpStarttime': form.wpStarttime.value,
					'wpEdittime': form.wpEdittime.value,
					'wpAutoSummary': form.wpAutoSummary.value,
					'wpEditToken': form.wpEditToken.value,
					'wpSummary': "Adding notice of possible deletion of image [[:" +  wgPageName + ']].' + TwinkleConfig.summaryAd,
					'wpTextbox1': text
				};
			}				
			xmlhttp.send( null );	

		}

	}
	xmlhttp.send( null );
}