/** * Implements mediaWiki.util library */ ( function ( mw, $ ) { 'use strict'; // Local cache and alias var util = { /** * Initialisation * (don't call before document ready) */ init: function () { var profile, $tocTitle, $tocToggleLink, hideTocCookie; /* 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-' : profile.platform === 'win' // Chrome on Windows // (both alt- and alt-shift work, but alt-f triggers Chrome wrench menu // which alt-shift-f does not) ? 'alt-shift-' // Chrome on other (Ubuntu?) : 'alt-' ); // 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 2.x and later } else if ( profile.name === 'firefox' && profile.versionBase > '1' ) { util.tooltipAccessKeyPrefix = 'alt-shift-'; } /* Fill $content var */ util.$content = ( function () { var $content, 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 ( var 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 $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 str string 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 str string 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 str String: Page name to get the link for. * @return String: Location for a page with name of 'str' or boolean false on error. */ wikiGetlink: function ( str ) { return mw.config.get( 'wgArticlePath' ).replace( '$1', util.wikiUrlencode( typeof str === 'string' ? str : mw.config.get( 'wgPageName' ) ) ); }, /** * 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