( function ( mw, $ ) { 'use strict'; /** * Utility library * @class mw.util * @singleton */ var util = { /** * Initialisation * (don't call before document ready) */ init: function () { util.$content = ( function () { var i, l, $node, selectors; selectors = [ // The preferred standard is class "mw-body". // You may also use class "mw-body mw-body-primary" if you use // mw-body in multiple locations. Or class "mw-body-primary" if // you use mw-body deeper in the DOM. '.mw-body-primary', '.mw-body', // If the skin has no such class, fall back to the parser output '#mw-content-text', // Should never happen... well, it could if someone is not finished writing a // skin and has not yet inserted bodytext yet. 'body' ]; for ( i = 0, l = selectors.length; i < l; i++ ) { $node = $( selectors[i] ); if ( $node.length ) { return $node.first(); } } // Preserve existing customized value in case it was preset return util.$content; }() ); }, /* Main body */ /** * Encode the string like PHP's rawurlencode * * @param {string} str String to be encoded. */ rawurlencode: function ( str ) { str = String( str ); return encodeURIComponent( str ) .replace( /!/g, '%21' ).replace( /'/g, '%27' ).replace( /\(/g, '%28' ) .replace( /\)/g, '%29' ).replace( /\*/g, '%2A' ).replace( /~/g, '%7E' ); }, /** * Encode page titles for use in a URL * * We want / and : to be included as literal characters in our title URLs * as they otherwise fatally break the title. * * The others are decoded because we can, it's prettier and matches behaviour * of `wfUrlencode` in PHP. * * @param {string} str String to be encoded. */ wikiUrlencode: function ( str ) { return util.rawurlencode( str ) .replace( /%20/g, '_' ) // wfUrlencode replacements .replace( /%3B/g, ';' ) .replace( /%40/g, '@' ) .replace( /%24/g, '$' ) .replace( /%21/g, '!' ) .replace( /%2A/g, '*' ) .replace( /%28/g, '(' ) .replace( /%29/g, ')' ) .replace( /%2C/g, ',' ) .replace( /%2F/g, '/' ) .replace( /%3A/g, ':' ); }, /** * Get the link to a page name (relative to `wgServer`), * * @param {string|null} [str=wgPageName] Page name * @param {Object} [params] A mapping of query parameter names to values, * e.g. `{ action: 'edit' }` * @return {string} Url of the page with name of `str` */ getUrl: function ( str, params ) { var url = mw.config.get( 'wgArticlePath' ).replace( '$1', util.wikiUrlencode( typeof str === 'string' ? str : mw.config.get( 'wgPageName' ) ) ); if ( params && !$.isEmptyObject( params ) ) { url += ( url.indexOf( '?' ) !== -1 ? '&' : '?' ) + $.param( params ); } return url; }, /** * Get address to a script in the wiki root. * For index.php use `mw.config.get( 'wgScript' )`. * * @since 1.18 * @param str string Name of script (eg. 'api'), defaults to 'index' * @return string Address to script (eg. '/w/api.php' ) */ wikiScript: function ( str ) { str = str || 'index'; if ( str === 'index' ) { return mw.config.get( 'wgScript' ); } else if ( str === 'load' ) { return mw.config.get( 'wgLoadScript' ); } else { return mw.config.get( 'wgScriptPath' ) + '/' + str + mw.config.get( 'wgScriptExtension' ); } }, /** * Append a new style block to the head and return the CSSStyleSheet object. * Use .ownerNode to access the `