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' ); row.append( headerWiki, headerLink, headerStatus ); table.append( row ); $( '#centralizer' ).html( table ); },

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

new mw.Api.get( {				formatversion: 2,				action: 'query',				prop: 'revisions',				rvprop: 'content|timestamp',				rvslots: 'main',				titles: mw.config.get( 'wgPageName' )			} ).done( function ( data ) {				//console.log( data );				var localData = data.query.pages[0].revisions[0];				var siteData, siteName, siteURL, siteTitle, siteAPI;				var link;				var row, cellWiki, cellLink, cellStatus;				for ( var siteID in sitelinks ) {					siteData = sitelinks[ siteID ];					siteName = siteData.site;					siteLink = siteData.url;					siteTitle = siteData.title;					link = $( '' ).text( siteTitle ).attr( 'href', siteLink );					cellWiki = $( ' ' ).text( siteID );					cellLink = $( '  ' ).html( link );					cellStatus = $( '  ' ).text( 'Loading' );					row = $( '  ' ).append( cellWiki, cellLink, cellStatus );					$( '#centralizer table' ).append( row );					Centralizer.getStatus( siteData, localData, cellStatus ); }			} );		} );	},

getStatus: function ( remoteData, localData, cellStatus ) { var remoteWiki = remoteData.site; var remoteLink = remoteData.url; var remoteTitle = remoteData.title; var remoteAPI = remoteLink.replace( /\/wiki\/.+/, '/w/api.php' ); // Hacky but efficient

var localWiki = mw.config.get( 'wgDBname' ); var localContent = localData.slots.main.content; var localTimestamp = localData.timestamp; var localTime = new Date( localTimestamp );

var status = 'Local'; var color = '#aff'; if ( remoteWiki === localWiki ) { cellStatus.text( status ).css( 'background-color', color ); return; }

new mw.ForeignApi( remoteAPI ).get( {			formatversion: 2,			action: 'query',			prop: 'revisions',			rvprop: 'content|timestamp',			rvslots: 'main',			titles: remoteTitle		} ).done( function ( data ) {			//console.log( data );			var remoteRevision = data.query.pages[0].revisions[0];			var remoteContent = remoteRevision.slots.main.content;			var remoteTimestamp = remoteRevision.timestamp;			var remoteTime = new Date( remoteTimestamp );

if ( localContent === remoteContent ) { status = 'Updated'; color = '#afa'; } else if ( localTime > remoteTime ) { status = 'Outdated'; color = '#ffa'; } else { status = 'Forked'; color = '#faa'; }

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

$( Centralizer.init );