( function ( mw, $ ) { 'use strict'; /** * Utility library * @class mw.util * @singleton */ var util = { /** * Initialisation * (don't call before document ready) */ init: function () { var profile; /* Set tooltipAccessKeyPrefix */ profile = $.client.profile(); // Opera on any platform if ( profile.name === 'opera' ) { util.tooltipAccessKeyPrefix = 'shift-esc-'; // Chrome on any platform } else if ( profile.name === 'chrome' ) { util.tooltipAccessKeyPrefix = ( profile.platform === 'mac' // Chrome on Mac ? 'ctrl-option-' // Chrome on Windows or Linux // (both alt- and alt-shift work, but alt with E, D, F etc does not // work since they are browser shortcuts) : 'alt-shift-' ); // Non-Windows Safari with webkit_version > 526 } else if ( profile.platform !== 'win' && profile.name === 'safari' && profile.layoutVersion > 526 ) { util.tooltipAccessKeyPrefix = 'ctrl-alt-'; // Firefox 14+ on Mac } else if ( profile.platform === 'mac' && profile.name === 'firefox' && profile.versionNumber >= 14 ) { util.tooltipAccessKeyPrefix = 'ctrl-option-'; // Safari/Konqueror on any platform, or any browser on Mac // (but not Safari on Windows) } else if ( !( profile.platform === 'win' && profile.name === 'safari' ) && ( profile.name === 'safari' || profile.platform === 'mac' || profile.name === 'konqueror' ) ) { util.tooltipAccessKeyPrefix = 'ctrl-'; // Firefox/Iceweasel 2.x and later } else if ( ( profile.name === 'firefox' || profile.name === 'iceweasel' ) && profile.versionBase > '1' ) { util.tooltipAccessKeyPrefix = 'alt-shift-'; } /* Fill $content var */ util.$content = ( function () { var i, l, $content, selectors; selectors = [ // The preferred standard for setting $content (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 want $content to be deeper // in the dom than mw-body '.mw-body-primary', '.mw-body', /* Legacy fallbacks for setting the content */ // Vector, Monobook, Chick, etc... based skins '#bodyContent', // Modern based skins '#mw_contentholder', // Standard, CologneBlue '#article', // #content is present on almost all if not all skins. Most skins (the above cases) // have #content too, but as an outer wrapper instead of the article text container. // The skins that don't have an outer wrapper do have #content for everything // so it's a good fallback '#content', // If nothing better is found fall back to our bodytext div that is guaranteed to be here '#mw-content-text', // Should never happen... well, it could if someone is not finished writing a skin and has // not inserted bodytext yet. But in any case should always exist 'body' ]; for ( i = 0, l = selectors.length; i < l; i++ ) { $content = $( selectors[i] ).first(); if ( $content.length ) { return $content; } } // Make sure we don't unset util.$content if it was preset and we don't find anything return util.$content; } )(); // Table of contents toggle mw.hook( 'wikipage.content' ).add( function () { var $tocTitle, $tocToggleLink, hideTocCookie; $tocTitle = $( '#toctitle' ); $tocToggleLink = $( '#togglelink' ); // Only add it if there is a TOC and there is no toggle added already if ( $( '#toc' ).length && $tocTitle.length && !$tocToggleLink.length ) { hideTocCookie = $.cookie( 'mw_hidetoc' ); $tocToggleLink = $( '' ) .text( mw.msg( 'hidetoc' ) ) .click( function ( e ) { e.preventDefault(); util.toggleToc( $(this) ); } ); $tocTitle.append( $tocToggleLink .wrap( '' ) .parent() .prepend( ' [' ) .append( '] ' ) ); if ( hideTocCookie === '1' ) { util.toggleToc( $tocToggleLink ); } } } ); }, /* 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 * * @param {string} str String to be encoded. */ wikiUrlencode: function ( str ) { return util.rawurlencode( str ) .replace( /%20/g, '_' ).replace( /%3A/g, ':' ).replace( /%2F/g, '/' ); }, /** * Get the link to a page name (relative to `wgServer`), * * @param {string} str Page name to get the link for. * @param {Object} params A mapping of query parameter names to values, * e.g. { action: 'edit' }. Optional. * @return {string} Location for a page with name of `str` or boolean false on error. */ 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 ? '&' : '?'; url += $.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 `