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 link, button; var row, cellWiki, cellLink, cellStatus; for ( var id in sitelinks ) { sitelink = sitelinks[ id ]; var remote = { wiki: sitelink.site, title: sitelink.title, url: sitelink.url, api: sitelink.url.replace( /\/wiki\/.+/, '/w/api.php' ) // Hacky but efficient };

link = $( '' ).text( remote.title ).attr( 'href', remote.url ); button = $( ' ' ).text( 'Update' ).click( { local: local, remote: remote }, Centralizer.update );

cellWiki = $( ' ' ).text( remote.wiki ); cellLink = $( ' ' ).html( link ); cellStatus = $( ' ' ).text( 'Loading' ); cellAction = $( ' ' ).html( button ); 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 ); } );	},

update: function ( event ) { var data = event.data; var local = data.local; var remote = data.remote; console.log( data ); new mw.ForeignApi( remote.api ).edit( remote.title, function ( revision ) {			return {				text: local.content,				summary: 'Update to latest',				assert: 'user'			};		} ).done( function ( data ) {			console.log( data );		} ); } };

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