MediaWiki:Synchronizer.js

var Centralizer = {

init: function { Centralizer.makeTable; },

makeTable: function { var table = $( ' ' ); var row = $( ' ' ); var headerWiki = $( ' ' ).text( 'Wiki' ); var headerLink = $( ' ' ).text( 'Link' ); var headerStatus = $( ' ' ).text( 'Status' ); var headerAction = $( ' ' ).text( 'Action' ); row.append( headerWiki, headerLink, headerStatus, headerAction ); table.append( row ); $( '#centralizer' ).html( table );

Centralizer.getLocalData; },

getLocalData: function { var local = { wiki: mw.config.get( 'wgDBname' ), title: mw.config.get( 'wgPageName' ), status: 'Local' };		new mw.Api.get( {			formatversion: 2,			action: 'query',			prop: 'revisions',			rvprop: 'content|timestamp',			rvslots: 'main',			titles: local.title		} ).done( function ( data ) {			var revision = data.query.pages[0].revisions[0];			local.content = revision.slots.main.content;			local.time = new Date( revision.timestamp );			Centralizer.makeRow( local );			Centralizer.getRemoteData( local );		} ); },

getRemoteData: function ( local ) { var wikidataID = mw.config.get( 'wgWikibaseItemId' ); $.get( '//www.wikidata.org/wiki/Special:EntityData/' + wikidataID + '.json' ).done( function ( data ) {			var entities = data.entities;			var entity = entities[ wikidataID ];			var siteLinks = entity.sitelinks;			var siteLink;			var remote = {				status: 'Loading'			};			var row;			for ( var key in siteLinks ) {				siteLink = siteLinks[ key ];				remote.wiki = siteLink.site;				remote.title = siteLink.title;				remote.url = siteLink.url;				remote.api = siteLink.url.replace( /\/wiki\/.+/, '/w/api.php' ); // Hacky but efficient				Centralizer.makeRow( remote );				Centralizer.updateStatus( remote, local );			}		} ); },

updateStatus: function ( remote, local ) { new mw.ForeignApi( remote.api ).get( {			formatversion: 2,			action: 'query',			prop: 'revisions',			rvprop: 'content|timestamp',			rvslots: 'main',			titles: remote.title		} ).done( function ( data ) {			//console.log( data );			var revision = data.query.pages[0].revisions[0];			var content = revision.slots.main.content;			var time = new Date( revision.timestamp );			if ( content === local.content ) {				remote.status = 'Updated';			} else if ( time < local.time ) {				remote.status = 'Outdated';			} else {				remote.status = 'Forked';			}			Centralizer.makeRow( remote );		} ); },

update: function ( event ) { var data = event.data; var local = data.local; var remote = data.remote; var promise = Centralizer.updateRemote( remote, local ); promise.then( function ( data ) {			console.log( data );		} ); },

updateRemote: function ( remote, local ) { return new mw.ForeignApi( remote.api ).edit( remote.title, function ( revision ) {			return {				text: local.content,				summary: 'Update to latest',				assert: 'user'			};		} ); },

makeRow: function ( data ) { var color, $button; switch ( data.status ) { case 'Local': color = '#aff'; break; case 'Updated': color = '#afa'; break; case 'Outdated': color = '#ffa'; $button = $( ' ' ).text( 'Update' ); break; case 'Forked': color = '#faa'; break; }		var $link = $( '' ).text( data.title ).attr( 'href', data.url ); var $td1 = $( ' ' ).text( data.wiki ); var $td2 = $( ' ' ).html( $link ); var $td3 = $( ' ' ).text( data.status ).css( 'background-color', color ); var $td4 = $( ' ' ).html( $button ); var $table = $( '#centralizer table' ); var $row = $table.find( '.' + data.wiki ); if ( $row.length ) { $row.empty.append( $td1, $td2, $td3, $td4 ); } else { $row = $( ' ' ).addClass( data.wiki ).append( $td1, $td2, $td3, $td4 ); $table.append( $row ); }	} };

mw.loader.using( 'mediawiki.ForeignApi', Centralizer.init );