User:Glaisher/importSeTranslations.js

From Meta, a Wikimedia project coordination wiki

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/*
Workflow:
 1. Create the newTitle page
 2. Mark for translation without making any changes (remove members/candidates).
 3. Change the configuration vars below.
 4. Start executing by calling initImporter() on Special:BlankPage or some other empty page.
     - Do replace of year when required. See line 43.
 5. Check whether anything broke!
 6. Make the required changes on newTitle page
 7. Mark it for translation with invalidation enabled translation units with changes
 8. Let FuzzyBot handle the rest (might need a dummy edit to make it kick)
*/

// Change these!
var oldTitle = 'Stewards/Elections 2015';
var newTitle = 'Stewards/Elections 2016';
var translationUnitTitle = 'Translations:Stewards/Elections 2015/Page display title';

var api = new mw.Api();

function getMessageTranslations( unitTitle ) {
	return api.get( {
		action: 'query',
		meta: 'messagetranslations',
		format: 'json',
		mttitle: unitTitle
	} );
}

function getSummary( title ) {
	return 'Import from [[' + title + ']]; please see that page for attribution';
}

function getNewUnitTitle( unitTitle ) {
	return unitTitle.replace( oldTitle, newTitle );
}

function doImport( translation ) {
	var newUt = getNewUnitTitle( translation.title );
	api.postWithToken( 'csrf', {
		action: 'edit',
		title: newUt,
		text: translation['*'], // .replace( /2015/g, '2016' )
		summary: getSummary( translation.title ),
		bot: true
	} ).done( function ( data ) {
		$('#mw-content-text').append( 'Saved ' + newUt + '<br/>' );
	} ).fail( function ( data ) {
		console.log( 'Failed saving ' + data );
		$('#mw-content-text').append( 'Failed saving ' + newUt + '<br/>' );
	} );
}

function initImporter() {
	getMessageTranslations( translationUnitTitle )
		.done( function( data ) {
			var messageTranslations = data.query.messagetranslations;
			console.log( messageTranslations );
			$.each( messageTranslations,
				function( i, translation ) {
					// Edit rate: 1 edit/4 seconds - XX this doesn't work?
					setTimeout( doImport( translation ), 4000*i );
				}
			);
		} ).fail( function ( data ) {
			console.log( data );
		} );
}

// initImporter();