MediaWiki:Synchronizer.js

/** * Synchronizer is a tool for synchronizing Lua modules and other pages across Wikimedia wikis * Documentation: https://www.mediawiki.org/wiki/Synchronizer * Author: User:Sophivorus * License: CC-BY-SA-3.0 */ var Synchronizer = {

init: function { Synchronizer.initTable; $( window ).on( 'hashchange', Synchronizer.initTable );

Synchronizer.initDiffDialog; },

initTable: function { var id = location.hash; if ( !id ) { return console.log( 'Hash in URL not found.'); }		var $div = $( id ); if ( !$div.length ) { return console.log( 'Div with ID ' + id + ' not found.'); }		var table = new Synchronizer.Table( id ); table.init; },

Table: function ( id ) {

this.id = id;

/**		 * Wrapper div created by Module:Synchronizer that will hold the table * @type {Object} jQuery object containing the wrapper div */		this.$div = $( id );

/**		 * Data about the master module * @type {Object} Map from data key to data value */		this.master = {};

this.init = function {

this.$div.text( 'Loading...' );

// Get master page var page = this.$div.data( 'page' ).trim; if ( !page ) { return this.error( 'Page not set.' ); }

// Get master wiki var master = this.$div.data( 'master' ); if ( !master ) { master = mw.config.get( 'wgWikiID' ); }

// Set basic master data this.master = { wiki: master, page: page, status: 'Master', };

// Get wikidata data this.getWikidataData; };

this.getWikidataData = function { new mw.ForeignApi( '//www.wikidata.org/w/api.php' ).get( {				action: 'wbgetentities',				sites: this.master.wiki,				props: 'info|sitelinks/urls',				normalize: 1,				titles: this.master.page			} ).done( function ( data ) {				var entity = Object.values( data.entities )[0];				if ( entity.hasOwnProperty( 'missing' ) ) {					return this.error( 'No Wikidata item associated to ' + this.master.page );				}				var sitelink = entity.sitelinks[ this.master.wiki ]; // Save the master sitelink				delete entity.sitelinks[ this.master.wiki ]; // Then remove it				if ( !entity.sitelinks ) {					return this.error( 'No page associated to ' + this.master.page );				}				this.master.url = sitelink.url;				this.master.api = sitelink.url.replace( /\/wiki\/.+/, '/w/api.php' );				this.master.item = entity.id;				this.master.sitelinks = entity.sitelinks;				this.makeTable;				this.getMasterData;			} ); };		this.getMasterData = function { var api = new mw.ForeignApi( this.master.api ); api.get( {				formatversion: 2,				action: 'query',				prop: 'revisions',				rvprop: 'sha1|content',				rvslots: 'main',				titles: this.master.page			} ).done( function ( data ) {				var revision = data.query.pages[0].revisions[0];				this.master.sha1 = revision.sha1;				this.master.content = revision.slots.main.content;				// We need to do a separate API call to get 500 revisions				// because "content" is considered an "expensive" property				// so if we request all together we only get 50 revisions				api.get( { formatversion: 2, action: 'query', prop: 'revisions', rvprop: 'sha1|ids', rvlimit: 'max', titles: this.master.page } ).done( function ( data ) { var page = data.query.pages[0]; this.master.revisions = page.revisions; var sitelink; for ( var key in this.master.sitelinks ) { sitelink = this.master.sitelinks[ key ]; var module = { wiki: sitelink.site, page: sitelink.title, url: sitelink.url, api: sitelink.url.replace( /\/wiki\/.+/, '/w/api.php' ) };						this.updateStatus( module ); }				} );			} );		},		this.updateStatus = function ( module ) { new mw.ForeignApi( module.api ).get( {				formatversion: 2,				action: 'query',				prop: 'revisions|info',				rvprop: 'sha1',				inprop: 'protection',				meta: 'sitenifo',				siprop: 'namespaces', // @todo Use this to show lock icons for protected namespaces				titles: module.page			} ).done( function ( data ) {				//console.log( data );				var page = data.query.pages[0];				var revision = page.revisions[0];				module.sha1 = revision.sha1;				var hashes = this.master.revisions.map( function ( revision ) { return revision.sha1; } );				if ( module.sha1 === this.master.sha1 ) {					module.status = 'Updated';				} else {					this.master.revisions.forEach( function ( revision, key ) { if ( revision.sha1 === module.sha1 ) { module.status = key + ' revision' + ( key === 1 ? '' : 's' ) + ' behind'; return; }					} );					if ( !module.status ) {						module.status = 'Forked';					}				}				if ( page.protection.length ) {					module.protection = page.protection;				}				this.updateRow( module );			} ); };		this.updateRow = function ( module ) { var color, title, $button; if ( module.status === 'Updated' ) { color = '#afa'; title = 'The code of this module is the same as the master module.'; } else if ( module.status.includes( 'behind' ) ) { color = '#ffa'; title = 'The code of this module is behind that of the master module.'; $button = $( ' ' ).text( 'Update' ).click( function  {					$( this ).prop( 'disabled', true );					this.showDiff( module );				} ); } else if ( module.status === 'Forked' ) { color = '#faa'; title = 'The code of this module has diverged from the master module.'; $button = $( ' ' ).text( 'Update' ).click( function  {					$( this ).prop( 'disabled', true );					this.showDiff( module );				} ); }			var masterName = this.master.page.substring( this.master.page.indexOf( ':' ) + 1 ); var moduleName = module.page.substring( module.page.indexOf( ':' ) + 1 ); var $alert; if ( masterName !== moduleName ) { $alert = new OO.ui.IconWidget( {					icon: 'alert',					title: "This module is called '" + moduleName + "' rather than '" + masterName + "'. This may break dependencies between synchronized modules."				} ).$element; $alert.css( {					'cursor': 'help',					'margin-left': '.5em',					'vertical-align': 'top'				} ); }			var $lock; if ( module.protection ) { var level; for ( var protection of module.protection ) { if ( protection.type === 'edit' ) { level = protection.level; }				}				if ( level ) { $lock = new OO.ui.IconWidget( {						icon: 'lock',						title: "This page is protected. Only '" + level + "' users may edit it."					} ).$element; $lock.css( {						'cursor': 'help',						'margin-left': '.5em',						'vertical-align': 'top'					} ); }			}			var $link = $( '' ).text( module.page ).attr( 'href', module.url ); var $td1 = $( ' ' ).text( module.wiki ); var $td2 = $( ' ' ).append( $link, $lock, $alert ); var $td3 = $( ' ' ).text( module.status ).attr( 'title', title ).css( { 'background-color': color, 'cursor': 'help' } ); var $td4 = $( ' ' ).append( $button ); var $div = this.$div; var $row = $div.find( '.' + module.wiki ); $row.empty.append( $td1, $td2, $td3, $td4 ); };		this.showDiff = function ( module ) { new mw.ForeignApi( module.api ).post( {				'formatversion': 2,				'action': 'compare',				'fromtitle': module.page,				'toslots': 'main',				'totext-main': this.master.content			} ).done( function ( data ) {				var diff = data.compare.body;				module.diff = diff;				var windowManager = new OO.ui.WindowManager;				$( document.body ).append( windowManager.$element );				var dialog = new this.DiffDialog( { data: module } );				windowManager.addWindows( [ dialog ] );				windowManager.openWindow( dialog );			} ); };		this.update = function ( module ) { module.status = 'Updating...'; this.updateRow( module ); return new mw.ForeignApi( module.api ).edit( module.page, function ( revision ) {				var prefix = this.getInterwikiPrefix( module );				var master = prefix + ':' + this.master.page;				var summary = 'Update from master using Synchronizer';				return {					text: this.master.content,					summary: summary,					assert: 'user'				};			} ).done( function ( data ) {				//console.log( data );				module.status = 'Updated';				module.content = this.master.content;				this.updateRow( module );			} ).fail( function ( error ) {				//console.log( error );				switch ( error ) {					case 'protectednamespace-interface':					case 'protectednamespace':					case 'customcssjsprotected':					case 'cascadeprotected':					case 'protectedpage':					case 'permissiondenied':						module.status = 'No permission';						break;					case 'assertuserfailed':						module.status = 'Not logged-in'; break; default: module.status = 'Failed'; break; }				this.updateRow( module ); } );		};		this.makeTable = function {			var $table = $( ' '; Synchronizer.DiffDialog.super.prototype.initialize.apply( this, arguments ); this.content = new OO.ui.PanelLayout( { padded: true, expanded: false } ); this.content.$element.append( table ); this.$body.append( this.content.$element ); };		Synchronizer.DiffDialog.prototype.getActionProcess = function ( action ) { if ( action ) { var dialog = this; var module = dialog.data; if ( action === 'save' ) { Synchronizer.update( module ); }			   if ( action === 'cancel' ) { Synchronizer.updateRow( module ); }		       return new OO.ui.Process( function  {		            dialog.close;		        } ); }		   return Synchronizer.DiffDialog.super.prototype.getActionProcess.call( this, action ); };	},

/**	 * Make error message */	error: function ( message ) { if ( Synchronizer.$div && Synchronizer.$div.length ) { Synchronizer.$div.addClass( 'error' ).text( message ); } else { console.log( message ); }	} };

mw.loader.using( [	'oojs-ui-core',	'oojs-ui-widgets',	'oojs-ui-windows',	'oojs-ui.styles.icons-alerts',	'oojs-ui.styles.icons-moderation',	'mediawiki.diff.styles',	'mediawiki.ForeignApi', ], Synchronizer.init );