Jump to content

User:HueSatLum/pageInfo.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
( function ( $, mw ) {
  var $siteSub = $( '#siteSub' );
  var articleId = mw.config.get( 'wgArticleId' );
	
	if ( $siteSub.length === 0 ||
			 articleId === 0 ||
			 mw.util.getParamValue( 'printable' ) === 'yes' ||
			 mw.config.get( 'wgIsMainPage' ) ) {
		return;
	}
	
	function plural ( number, word ) {
		number = Math.round( number );
		return number + ' ' + word + ( number === 1 ? '' : 's');
	}

	function formatDate( timestamp ) {
		var then = new Date ( timestamp );
		var now = new Date();
		var age = now.getTime() - then.getTime();
		var conversions = [
			[1e3, 'second'],
			[60, 'minute'],
			[60, 'hour'],
			[24, 'day'],
			[365/12, 'month'],
			[12, 'year']
		];

		if ( age < 1e3 * 60 ) {
			return 'just now';
		}

		for( var i = 0; i < conversions.length; i++ ) {
			age = age / conversions[ i ][ 0 ];
			if( conversions[ i + 1 ] === undefined || age < conversions[ i + 1 ][ 0 ] ) {
				return plural( age, conversions[ i ][ 1 ] ) + ' ago';
			}
		}
	}
	
	var pageInfo = {};
	
	pageInfo.pageType = $( '[id^="ca-nstab-"].selected' ).text() || 'page';
	pageInfo.pageType = pageInfo.pageType.toLowerCase();
	if ( mw.config.get( 'wgNamespaceNumber' ) % 2 === 1) {
		// All talk namespace numbers are odd
		pageInfo.pageType = 'talk page';
	}
	
	var api = new mw.Api();
	api.get( {
		action: 'query',
		prop: [ 'info', 'revisions' ],
		inprop: [ 'protection', 'watchers' ],
		rvprop: [ 'ids', 'timestamp', 'user' ],
		rvlimit: 1,
		pageids: articleId,
		rawcontinue: ''
	} ).done( function ( data ) {
		var rawData = data.query.pages[ articleId ];
		
		pageInfo.lastEditTimestamp = rawData.revisions[ 0 ].timestamp;
		pageInfo.lastEditAgo = formatDate( pageInfo.lastEditTimestamp );
		pageInfo.lastEditUser = rawData.revisions[ 0 ].user;
		pageInfo.lastEditLink = mw.config.get( 'wgScript' ) +
				'?title=' + mw.config.get( 'wgPageName' ) +
				'&diff=' + rawData.revisions[ 0 ].revid +
				'&oldid=' + rawData.revisions[ 0 ].parentid;
		var userLinkPrefix = ( rawData.revisions[ 0 ].anon === undefined ) ? '/wiki/User:' : '/wiki/Special:Contributions/';
		pageInfo.lastEditUserLink = userLinkPrefix + rawData.revisions[ 0 ].user;
		
		pageInfo.lastEditTimestamp = new Date(pageInfo.lastEditTimestamp).toUTCString();
		
		pageInfo.watchers = rawData.watchers || '<30';
	
		api.get( {
			action: 'query',
			prop: 'revisions',
			rvprop: [ 'ids', 'timestamp', 'user' ],
			rvlimit: 1,
			rvdir: 'newer',
			pageids: articleId,
			rawcontinue: ''
		} ).done( function ( data ) {
			var rawOldestData = data.query.pages[ articleId ].revisions[ 0 ];
			
			pageInfo.firstEditTimestamp = rawOldestData.timestamp;
			pageInfo.firstEditAgo = formatDate( pageInfo.firstEditTimestamp );
			pageInfo.firstEditUser = rawOldestData.user;
			pageInfo.firstEditLink = mw.config.get( 'wgScript' ) +
					'?title=' + mw.config.get( 'wgPageName' ) +
					'&oldid=' + rawOldestData.revid;
			var userLinkPrefix = ( rawOldestData.anon === undefined ) ? '/wiki/User:' : '/wiki/Special:Contributions/';
			pageInfo.firstEditUserLink = userLinkPrefix + rawOldestData.user;
			
			pageInfo.firstEditTimestamp = new Date(pageInfo.firstEditTimestamp).toUTCString();
		} ).fail( function ( error ) {
			throw error;
		} ).always( addInfo );
	} ).fail( function ( error ) {
		throw error;
	} );
	
	function addInfo () {
		var infoTemplate = ' This ${pageType} was created <a href="${firstEditLink}" title="${firstEditTimestamp}" class="nopopups">${firstEditAgo}</a> ' +
			'by <a href="${firstEditUserLink}">${firstEditUser}</a> and last edited ' +
			'<a href="${lastEditLink}" title="${lastEditTimestamp}" class="nopopups">${lastEditAgo}</a> by ' +
			'<a href="${lastEditUserLink}">${lastEditUser}</a>, with ${watchers} watchers.';
		
		if ( $siteSub.text().slice( -1 ) !== '.' ) {
			// Add a period after "From Wikipedia, the free encyclopedia"
			// if there's not already one there from another script
			$siteSub.append( '.' );
		}
		
		infoTemplate = infoTemplate.replace( /\${(\w+)}/g, function ( _match, key ) {
			return pageInfo[key];
		} );
		
		$siteSub.append( infoTemplate );
	}
} ) ( jQuery, mediaWiki );