Jump to content

User:DannyS712 test/Global watchlist i18n.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.
// <nowiki>
/* jshint maxerr: 999 */
/* globals GlobalWatchlist */
$(function (){
var GlobalWatchlistI18n = {};
window.GlobalWatchlistI18n = GlobalWatchlistI18n;

GlobalWatchlistI18n.config = {
	i18nVersion: '1.1',
	scriptVersion: '7.2.3'
};
GlobalWatchlistI18n.run = async function () {
	var results = await GlobalWatchlistI18n.getTranslations();
//	console.log( results );
	var translations = results.query.pages;
	while ( results && results.continue && results.continue.gapcontinue ) {
		results = await GlobalWatchlistI18n.getTranslations( results.continue.gapcontinue );
		translations = translations.concat( results.query.pages );
//		console.log( results, translations );
	}
	console.log( 'Got' );
	GlobalWatchlistI18n.filterTranslations( translations );
};
GlobalWatchlistI18n.filterTranslations = function ( translations ) {
	var actualTranslations = translations.filter( ( page ) => {
		return (
			['/en', 'qqq'].indexOf( page.title.substring( page.title.length - 3, page.title.length ) ) === -1 &&
			page.title.indexOf( 'Page display title' ) === -1
		);
	});
//	console.log( actualTranslations );
	GlobalWatchlistI18n.processTranslations( actualTranslations );
};
GlobalWatchlistI18n.processTranslations = function ( actualTranslations ) {
	var justTranslations = actualTranslations.map( ( page ) => {
		return {
			lang: page.title.replace( /Translations:User:DannyS712\/Global watchlist\/Translate\/\d+\//, '' ),
			unit: parseInt( page.title.replace( /Translations:User:DannyS712\/Global watchlist\/Translate\/|\/.*/g, '' ) ),
			text: page.revisions[ '0' ].slots.main.content,
		};
	});
	console.log( justTranslations );
	GlobalWatchlistI18n.mapTranslations( justTranslations );
};
GlobalWatchlistI18n.mapTranslations = function ( justTranslations ) {
	var byLang = {};
	var tr;
	for ( var iii = 0; iii < justTranslations.length; iii++ ){
		tr = justTranslations[iii];
		if ( byLang[ tr.lang ] === undefined ) byLang[ tr.lang ] = [];
		byLang[ tr.lang ].push( {
			unit: tr.unit,
			text: tr.text,
		});
	}
	console.log( byLang );
	GlobalWatchlistI18n.findKeys( byLang );
};
GlobalWatchlistI18n.findKeys = function ( byLang ) {
	var i18n = window.GlobalWatchlist.i18n.units;
	var keys = Object.keys( i18n );
	var units = Object.values( i18n );
	console.log( keys, units );
	var actualByLang = {};
	var thislang, thiskeys;
	var langMsgs, key, keyNum;
	Object.keys( byLang ).sort().forEach( ( lang ) => {
		thislang = {};
		langMsgs = byLang[ lang ];
		for ( var jjj = 0; jjj < langMsgs.length; jjj++ ) {
			keyNum = units.indexOf( langMsgs[jjj].unit );
//			console.log( langMsgs[jjj], keyNum );
			if ( keyNum !== -1 ) {
				thislang[ keys[ keyNum ] ] = langMsgs[ jjj ].text;
			}
		}
		if ( Object.keys( thislang ).length > 0 ) {
			thiskeys = Object.keys( thislang ).sort();
			actualByLang[ lang ] = {};
			for ( var lll = 0; lll < thiskeys.length; lll++ ) {
				actualByLang[ lang ][ thiskeys[lll] ] = thislang[ thiskeys[ lll ] ];
			}
		}
	});
	console.log( actualByLang );
	GlobalWatchlistI18n.prettyOut( actualByLang );
};
GlobalWatchlistI18n.prettyOut = function ( actualByLang ) {
	var stringy = JSON.stringify( actualByLang, null, ' ' );
	stringy = stringy
		.replace( /'/g, "\\'" )
		.replace( /"/g, "'" )
		.replace( / +'(...?)':/g, '\t$1:' )
		.replace( /\n  '/g, "\n\t\t'" )
		.replace( /'\n/g, "',\n" )
		.replace( /\n }/g, "\n\t}");
	console.log( stringy );
};
GlobalWatchlistI18n.getTranslations = function ( cnt = '' ) {
	return new Promise((resolve) => {
		new mw.ForeignApi(`//meta.wikimedia.org/w/api.php`).get( {
			action: 'query',
			prop: 'revisions',
			rvslots: '*',
			rvprop: 'content',
			generator: 'allpages',
			gapnamespace: 1198,
			gapprefix: 'User:DannyS712/Global watchlist/Translate/',
			gaplimit: 50, 
			formatversion: 2,
			gapcontinue: cnt,
		}).always((response) => {
//			console.log( response );
			resolve(response);
		});
	});
};
} );
mw.loader.using( 'mediawiki.ForeignApi', function () {
	$(document).ready( function () {
		if ( mw.config.get('wgNamespaceNumber') === -1 ){
			var page = mw.config.get('wgCanonicalSpecialPageName');
			if ( page === 'Blankpage'){
				var page2 = mw.config.get( 'wgTitle' ).split('/');
				if ( page2[1] ) {
					if ( page2[1] === 'GlobalWatchlistI18n' ) {
						mw.hook( 'GlobalWatchlistInternal' ).add( window.GlobalWatchlistI18n.run );
						mw.loader.getScript( '/w/index.php?title=User:DannyS712 test/watch.js&action=raw&ctype=text/javascript' ).then( function () {
							GlobalWatchlist.init( 6 ).then( function () {
								console.log( 'Ready to run' );
							} );
						} );
					}
				}
			}
		}
	} );
} );