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;

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 = $( ' ' ).html( siteID ); cellLink = $( ' ' ).html( link ); cellStatus = $( ' ' ); row = $( ' ' ).append( cellWiki, cellLink, cellStatus ); $( '#centralizer table' ).append( row );

Centralizer.getStatus( siteData, cellStatus ); }		} );	},

getStatus: function ( siteData, cellStatus ) { var siteLink = siteData.url; var siteTitle = siteData.title; var siteAPI = siteLink.replace( /\/wiki\/.+/, '/w/api.php' ); // Hacky but efficient new mw.ForeignApi( siteAPI ).get( {			formatversion: 2,			action: 'query',			prop: 'revisions',			rvprop: 'content|timestamp',			rvslots: 'main',			titles: siteTitle		} ).done( function ( data ) {			console.log( data );			var revision = data.query.pages[0].revisions[0];			//content = revision.slots.main.content;

var timestamp = revision.timestamp; var time = new Date( timestamp ); var timeSince = Centralizer.getTimeSince( time );

cellStatus.html( timeSince + ' ago' ); } );	},

/**	 * Copied from https://stackoverflow.com/a/3177838/809356 */	getTimeSince: function ( date ) { var now = new Date; var diff = now - date; var seconds = Math.floor( diff / 1000 ); var interval = seconds / 31536000; if ( interval > 1 ) { return Math.floor( interval ) + ' years'; }		interval = seconds / 2592000; if ( interval > 1 ) { return Math.floor( interval ) + ' months'; }		interval = seconds / 86400; if ( interval > 1 ) { return Math.floor( interval ) + ' days'; }		interval = seconds / 3600; if ( interval > 1 ) { return Math.floor( interval ) + ' hours'; }		interval = seconds / 60; if ( interval > 1 ) { return Math.floor( interval ) + ' minutes'; }		return Math.floor( seconds ) + ' seconds'; } };

$( Centralizer.init );