MediaWiki:Synchronizer.js

var Centralizer = {

init: function { Centralizer.makeTable; var wikidataID = mw.config.get( 'wgWikibaseItemId' ); Centralizer.getLocalData; },

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

getLocalData: function { 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		} ).then( 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 );		} ).then( function ( data2 ) {			console.log( data2 );		} ); },

getRemoteData: function ( data ) { },

makeRows: function ( data ) { //console.log( data ); var id = mw.config.get( 'wgWikibaseItemId' ); var entities = data.entities; var entity = entities[ id ]; var sitelinks = entity.sitelinks;

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

Centralizer.getLocalData( local ).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 ); } );	},

getWikidataData: function ( wikidataID ) { return $.get( '//www.wikidata.org/wiki/Special:EntityData/' + wikidataID + '.json' ); },

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

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