SpecialVersion.js – MediaWiki

From Bohemia Interactive Community
Jump to navigation Jump to search
(Sorry about the "Recent Changes" spam, but I need to test in the field - as it were. Nearly there.)
(Groan...)
Line 27: Line 27:
if ( ev[0] ) {
if ( ev[0] ) {
var en;
var en;
if ( exts[ext].firstChild.hasAttribute( "href" ) ) {
if ( exts[ext].firstChild.nodeType === Node.ELEMENT_NODE ) {
en = exts[ext].firstChild.getAttribute( "href" );
en = exts[ext].firstChild.getAttribute( "href" );
en = en.substring( en.indexOf( "Extension:" ) + 10 );
en = en.substring( en.indexOf( "Extension:" ) + 10 );

Revision as of 17:47, 17 June 2014

/*****************************************************************************************
 * JavaScript here will be loaded by MediaWiki:Common.js when viewing "Special:Version".
 *
 *****************************************************************************************/

(function() { // BEGIN WRAPPER
 
/*****************************************************************************************
 * Variables required by all functions below
 */

	var foo = ""; // Every script needs a "foo"

/*****************************************************************************************
 * Check extension versions and indicate available updates.
 * Maintainer: BIKI User:Fred Gandt
 */

	function checkExtensionVersions() {
		var tbls = document.getElementsByTagName( "table" ),
			mw_version = tbls[0].getElementsByTagName( "tr" )[1].getElementsByTagName( "td" )[1].textContent,
			exts = tbls[2].getElementsByTagName( "em" );
			extso = {};
		for ( var ext in exts ) {
			if ( typeof exts[ext] == "object" ) {
				var ev = exts[ext].getElementsByClassName( "mw-version-ext-version" );
				if ( ev[0] ) {
					var en;
					if ( exts[ext].firstChild.nodeType === Node.ELEMENT_NODE ) {
						en = exts[ext].firstChild.getAttribute( "href" );
						en = en.substring( en.indexOf( "Extension:" ) + 10 );
					} else {
						en = exts[ext].textContent;
					}
					extso[en] = [
						exts[ext],
						ev[0].textContent.replace( /[^0-9\.\s]/g, "" ).trim()
					];
					$.ajax( {
						url: "https://www.mediawiki.org/w/api.php",
						data: {
							action: "query",
							prop: "revisions",
							rvprop: "content",
							titles: "Extension:" + encodeURIComponent( en ),
							format: "json"
						},
						dataType: "jsonp",
						success: function( data ) {
							var pgs = data.query.pages;
							for ( var pg in pgs ) {
								if ( pgs[pg].hasOwnProperty( "revisions" ) ) {
									var txt = pgs[pg].revisions[0]["*"],
										ttl = pgs[pg].title.replace( " ", "_" ).replace( "Extension:", "" ),
										curv = extso[ttl][1];
									txt = txt.substring( txt.indexOf( "|version" ) + 8 );
									txt = txt.substring( 0, txt.indexOf( "|" ) ).replace( /[^0-9\.\s]/g, "" ).trim() || "Continuous updates";
									if ( curv != txt ) {
										extso[ttl][0].innerHTML += "<br><span style='color:red;'>" + txt + " available</span>";
									}
								}
							}
						}
					} );
				}
			}
		}
	}
 
/*****************************************************************************************/
 
/*****************************************************************************************
 * Next ***
 */
 
	// code code code
 
/*****************************************************************************************/
 
/*****************************************************************************************
 * Call the functions above
 */
 
	checkExtensionVersions();

}()); // END WRAPPER