User:BPositive/alignTranslations.js

From mediawiki.org

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.
mw.loader.using( ['jquery.ui', 'mediawiki.api'], function () {
 
/*
Alright - 
1. This script just tries to align section headers as of now
2. Assuming we have all the translation units with us, and an array sectionHeaders_T, which contains the section headers out of these translation units
	(I can extract these, only when I have the translation units available in this code)
3. The script would match only if section header in source language is present in sectionHeaders_T array.
4. Also, I need to use the revision before FuzzyBot's edit. Will do that later in a few hours
*/

	$(document).ready(function(){

		var translationUnits = [];
		var sectionHeaders = [];
		var sourceSections, translatedSections;

		function alignSectionHeaders(){
			var api_1 = new mw.Api();
			api_1.get ({
			action:'parse',
			prop: 'sections',
			format: 'json',
			page: 'Extension:ParserFunctions'
			}).done ( function( data ) {
				sourceSections = data.parse.sections;
				console.log("Source language sections are:");
				for(var i=0; i < sourceSections.length; i++){
					console.log(sourceSections[i].line);
				}
				var api_2 = new mw.Api();
					api_2.get ({
						action:'parse',
						prop: 'sections',
						format: 'json',
						page: 'Extension:ParserFunctions/pl'
						}).done ( function( data ) {
							translatedSections = data.parse.sections;
							console.log("Translated language sections are:");
							for(var i=0; i < translatedSections.length; i++){
								console.log(translatedSections[i].line);
							}
							if(sourceSections.length != translatedSections.length){
								console.log("Number of sections not equal. Mismatches might happen");
							}	
							else{	//They are equal. This else can be removed to still do some additional checks and match, if possible
								//console.log('fdsdfdsf not equal');
								for(var j=0; j < translatedSections.length; j++) {
									//console.log("sdf" + translatedSections.length)
									for(var k = 0; k < sectionHeaders.length; k++){
						
										if(sourceSections[j].line === sectionHeaders[k].headerText){
											console.log(sourceSections[j].line + " matched with " + translatedSections[j].line);
											break;
										}
									}
								}
							}	
						});
					});
		
		}


		/*By the time this function is called, the translationUnits
		  array is filled with tUnit objects containing the identifier
		  and the definition. This function runs through this array and
		  extracts the units containing section headers into sectionHeaders
		  array.
		*/
		function getSectionHeaders(){
			for(var i = 0; i < translationUnits.length; i++){
				
				var definition = translationUnits[i].definition;
				//console.log("Def is " + definition);
				if( definition.startsWith('==') && definition.endsWith('==') ){
					var sectionHeader = new Object();
					sectionHeader.headerText = definition.replace(/==/g,'').trim();
					sectionHeader.identifier = translationUnits[i].identifier;
					sectionHeaders.push(sectionHeader);
				}
			}
			//console.log(sectionHeaders.length);
		}

		function getTranslationUnits() {
		
		/*API Query: /w/api.php?action=query&list=messagecollection&format=json&
			mcgroup=page-Extension3AParserFunctions&
			mclanguage=en&mcprop=definition*/
			var api = new mw.Api();
			api.get ({
				action:'query',
				list: 'messagecollection',
				format: 'json',
				mcgroup: 'page-Extension:ParserFunctions',
				mclanguage: 'en',
				mcprop: 'definition'
			}).done ( function( data ) {
				var result = data['query']['messagecollection'];
				console.log(result.length);
				for(var i = 1; i < result.length; i++){
					var tUnit = new Object();
					var key = result[i].key;
					tUnit.identifier = key.slice(key.lastIndexOf('/') + 1);
					tUnit.definition = result[i].definition;
					translationUnits.push(tUnit);							
				}
				//console.log(translationUnits.length);
				getSectionHeaders();
				alignSectionHeaders();
			});
		}

		getTranslationUnits();
		
		/*for(var i = 0; i < translationUnits.length; i++){
			console.log(translationUnits[i].identifier);
		}*/

		
		
	});
});