MediaWiki:Common.js

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
/*global mw, JSconfig, importScript, jsMsg, importStylesheet */
/*jshint forin:false, strict:false, onecase:true, laxbreak:true, browser:true, jquery:true */

/**
 * JSconfig
 *
 * If you are a gadget author, you may use 
 * [[MediaWiki:Gadget-SettingsManager.js]] or jquery.jStorage or jquery.cookie
 * and [[MediaWiki:Gadget-SettingsUI.js]] to provide an easy interface.
 * 
 *
 * Global configuration options to enable/disable and configure
 * specific script features from [[MediaWiki:Common.js]] and [[MediaWiki:Monobook.js]]
 * <s>This framework adds config options (saved as cookies) to [[Special:Preferences]]</s>
 * (Site script does not run at [[Special:Preferences]] any more so this functionality has been removed)
 *
 * For a more permanent change you can override the default settings in your
 * [[Special:Mypage/monobook.js]]
 * for Example: JSconfig.keys[loadAutoInformationTemplate] = false;
 *
 * Maintainer: [[User:Dschwen]]
 */
window.JSconfig = {
 prefix: 'jsconfig_',
 keys: {},
 meta: {},
 // Register a new configuration item
 //  * name      : String, internal name
 //  * default_value : String or Boolean (type determines configuration widget)
 //  * description  : String, text appearing next to the widget in the preferences, or an hash-object
 //    containing translations of the description indexed by the language code
 //
 // Access keys through JSconfig.keys[name]
 registerKey: function (name, default_value, description, prefpage) {
  if (JSconfig.keys[name] === undefined) {
   JSconfig.keys[name] = default_value;
  } else {
   // all cookies are read as strings,
   // convert to the type of the default value
   switch (typeof default_value) {
   case 'boolean':
    JSconfig.keys[name] = (JSconfig.keys[name] === 'true');
    break;
   case 'number':
    JSconfig.keys[name] = JSconfig.keys[name] / 1;
    break;
   }
  }
  JSconfig.meta[name] = {
   'description': description[mw.config.get( 'wgUserLanguage' )] || description.en || (typeof description === 'string' && description) || '<i>en</i> translation missing',
   'page': prefpage || 0,
   'default_value': default_value
  };
 },
 readCookies: function () {
  var cookies = document.cookie.split('; ');
  var p = JSconfig.prefix.length;
  var i;
  for (var key = 0; cookies && key < cookies.length; key++) {
   if (cookies[key].substring(0, p) === JSconfig.prefix) {
    i = cookies[key].indexOf('=');
    //alert( cookies[key] + ',' + key + ',' + cookies[key].substring(p,i) );
    JSconfig.keys[cookies[key].substring(p, i)] = cookies[key].substring(i + 1);
   }
  }
 },
 writeCookies: function () {
  var expdate = new Date();
  expdate.setTime(expdate.getTime() + 1000 * 60 * 60 * 24 * 3650); // expires in 3560 days
  for (var key in JSconfig.keys) {
   document.cookie = JSconfig.prefix + key + '=' + JSconfig.keys[key] + '; path=/; expires=' + expdate.toUTCString();
  }
 }
};
JSconfig.readCookies();

mw.loader.using(['mediawiki.util']).then(function () {
/* Begin of mw.loader.using callback */

// Overwriting deprecated functions that have a follower that (also) accepts the same syntax:
window.getParamValue = mw.util.getParamValue;

/**
 * Prepend server (if not already).
 * @example '/something' to 'http://commons.wikimedia.org/something'
 * @example don't touch 'https://commons.wikimedia.org/foo'
 * @example don't touch '//commons.wikimedia.org/bar'
 * @param url {String}
 * @return {String}
 */
mw.util.expandUrl = function ( url ) {
	if ( url.substr( 0, 1 ) === '/' && url.substr( 0, 2 ) !== '//' ) {
		return mw.config.get( 'wgServer' ) + url;
	} else {
		return url;
	}
};

/**
 * Expand protocol-relative urls.
 * @param method {String} CURRENT, RELATIVE, HTTP, HTTPS
 * @return {String}
 */
mw.util.expandProtocol = function ( url, method ) {
  // Not relative right now, return right away
  if ( url.substr( 0, 2 ) !== '//' ) {
    return url;
  }
  method = method || 'CURRENT';
  switch ( method ) {
    case 'CURRENT':
      url = window.location.protocol + url;
      break;
    case 'RELATIVE':
      break;
    case 'HTTP':
      url = 'http:' + url;
      break;
    case 'HTTPS':
      url = 'https:' + url;
      break;
  }
  return url;
};

// Creates action=raw links for JS or CSS gadgets
// Useful for mw.loader.load, which doesn't accept page titles
function rawPageLink( pageName ) {
  return mw.config.get( 'wgServer' ) + mw.config.get( 'wgScript' ) + '?title=' + mw.util.wikiUrlencode(pageName) + '&action=raw&ctype=text/javascript';
}

// Overwriting deprecated functions that don't have an exact followup but can be easily mapped:
window.importScript = function ( page ) {
  if ( typeof page === 'string' && page.length ) {
    mw.loader.load( rawPageLink( page ) );
  }
};

// Wrapper for mw.notify still used by legacy scripts.
function jsMsgAppend( msg ) {
  mw.notify( msg );
}

/**
 * @source www.mediawiki.org/wiki/Snippets/Load_JS_and_CSS_by_URL
 * @revision 2014-05-02
 */
mw.loader.using( ['mediawiki.util', 'mediawiki.notify', 'mediawiki.legacy.wikibits' ], function () {
	var extraCSS = mw.util.getParamValue( 'withCSS' ),
		extraJS = mw.util.getParamValue( 'withJS' ),
		extraModule = mw.util.getParamValue( 'withModule' );
 
	if ( extraCSS ) {
		if ( extraCSS.match( /^MediaWiki:[^&<>=%#]*\.css$/ ) ) {
			importStylesheet( extraCSS );
		} else {
			mw.notify( 'Only pages from the MediaWiki namespace are allowed.', { title: 'Invalid withCSS value' } );
		}
	}
 
	if ( extraJS ) {
		if ( extraJS.match( /^MediaWiki:[^&<>=%#]*\.js$/ ) ) {
			importScript( extraJS );
		} else {
			mw.notify( 'Only pages from the MediaWiki namespace are allowed.', { title: 'Invalid withJS value' } );
		}
	}
 
	if ( extraModule ) {
		if ( /^ext.gadget.[^,\|]+$/.test( extraModule ) ) {
			mw.loader.load( extraModule );
		} else {
			mw.notify( 'Only gadget modules are allowed.', { title: 'Invalid withModule value' } );
		}
	}
} );

/**
 * Hide the filetoc element unless there are previous
 * or next links to display
 */
var filetocElement = document.querySelector('#filetoc');
var prevElement = document.querySelector('#prevnextlinks-prev');
var nextElement = document.querySelector('#prevnextlinks-next');

if (prevElement || nextElement) {
    filetocElement.style.display = 'block';
}

/**
 * ImageAnnotator
 * Globally enabled per
 * http://commons.wikimedia.org/?title=Commons:Village_pump&oldid=26818359#New_interface_feature
 * Maintainer: [[User:Lupo]]
 */
// Not on Special pages, and only if viewing the page
if (mw.config.get( 'wgNamespaceNumber' ) !== -1 && ['view', 'submit'].indexOf(mw.config.get('wgAction') ) !== -1 ) {
 if (typeof ImageAnnotator_disable === 'undefined' || !ImageAnnotator_disable) {
  // Don't even import it if it's disabled.
  mw.loader.load( '/index.php?title=MediaWiki:Gadget-ImageAnnotator.js&action=raw&ctype=text/javascript' ); // Backlink: [[MediaWiki:Gadget-ImageAnnotator.js]]
 }
}

/* Display In other projects header if it has content */
$( function () {
    const wikibaseHeader = document.querySelector( '.wikibase-header' );
    if ( wikibaseHeader ) {
        const nextElement = wikibaseHeader.nextElementSibling;
        if ( nextElement && nextElement.classList.contains( 'wb-otherproject-link' ) ) {
            wikibaseHeader.style.display = '';
        }
    }
} );

/* ADD A URL IN THE FOOTER TO TRAFFIC STATISTICS PAGES */
$(function () {
    var allowedNamespaces = [0, 14];
    var ns = mw.config.get('wgNamespaceNumber');

    if (!allowedNamespaces.includes(ns) || mw.config.get('wgAction') !== 'view') return;

    var pageTitle = mw.config.get('wgPageName');
    var decodedTitle = pageTitle.replace(/_/g, ' ');
    var encodedTitle = encodeURIComponent(decodedTitle).replace(/%20/g, '+');

    var statsURL = 'https://digitalbahairesources.org/pageview-analysis?website_id=6&titles=%5B%22' +
        encodedTitle + '%22%5D&from_year=2024&from_month=7&to_year=2025&to_month=7';

    var $link = $('<a>')
        .attr('href', statsURL)
        .attr('target', '_blank')
        .text('📊 View Traffic Statistics')
        .css({ marginLeft: '1em' });

    var $container = $('#footer-info > div').first();
    if ($container.length) {
        $container.append($link);
    }
});
/* End of mw.loader.using callback */
});