MediaWiki:Synchronizer.js

var Centralizer = {

init: function { Centralizer.makeTable; Centralizer.makeRows; },

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 ); },

makeRows: function { var wikidataID = mw.config.get( 'wgWikibaseItemId' ); $.get( '//www.wikidata.org/wiki/Special:EntityData/' + wikidataID + '.json' ).done( function ( data ) {			//console.log( data );			var entities = data.entities;			var entity = entities[ wikidataID ];			var sitelinks = entity.sitelinks;

var local = { wiki: mw.config.get( 'wgDBname' ), title: mw.config.get( 'wgPageName' ) };

new mw.Api.get( {				formatversion: 2,				action: 'query',				prop: 'revisions',				rvprop: 'content|timestamp',				rvslots: 'main',				titles: local.title			} ).done( function ( data ) {				//console.log( data );				var revision = data.query.pages[0].revisions[0];				local.content = revision.slots.main.content;				local.time = new Date( revision.timestamp );

var remote = {}; var link; var row, cellWiki, cellLink, cellStatus; for ( var remoteID in sitelinks ) { sitelink = sitelinks[ remoteID ]; remote.wiki = sitelink.site; remote.title = sitelink.title; remote.url = sitelink.url; remote.api = sitelink.url.replace( /\/wiki\/.+/, '/w/api.php' ); // Hacky but efficient

link = $( '' ).text( remote.title ).attr( 'href', remote.url );

cellWiki = $( ' ' ).text( remote.wiki ); cellLink = $( ' ' ).html( link ); cellStatus = $( ' ' ).text( 'Loading' ); cellAction = $( ' ' ); row = $( ' ' ).append( cellWiki, cellLink, cellStatus, cellAction ); $( '#centralizer table' ).append( row ); Centralizer.getStatus( remote, local, cellStatus ); }			} );		} );	},

getStatus: function ( remote, local, cell ) { var status = 'Local'; var color = '#aff'; if ( remote.wiki === local.wiki ) { cell.text( status ).css( 'background-color', color ); return; }

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];			remote.content = revision.slots.main.content;			remote.time = new Date( revision.timestamp );

if ( local.content === remote.content ) { status = 'Updated'; color = '#afa'; } else if ( local.time > remote.time ) { status = 'Outdated'; color = '#ffa'; } else { status = 'Forked'; color = '#faa'; }

cell.text( status ).css( 'background-color', color ); } );	} };

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