X-Git-Url: https://scripts.mit.edu/gitweb/autoinstallsdev/mediawiki.git/blobdiff_plain/19e297c21b10b1b8a3acad5e73fc71dcb35db44a..6932310fd58ebef145fa01eb76edf7150284d8ea:/resources/src/mediawiki/mediawiki.util.js diff --git a/resources/src/mediawiki/mediawiki.util.js b/resources/src/mediawiki/mediawiki.util.js new file mode 100644 index 00000000..fb34a890 --- /dev/null +++ b/resources/src/mediawiki/mediawiki.util.js @@ -0,0 +1,648 @@ +( function ( mw, $ ) { + 'use strict'; + + var util; + + /** + * Encode the string like PHP's rawurlencode + * @ignore + * + * @param {string} str String to be encoded. + * @return {string} Encoded string + */ + function rawurlencode( 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' ); + } + + /** + * Private helper function used by util.escapeId*() + * @ignore + * + * @param {string} str String to be encoded + * @param {string} mode Encoding mode, see documentation for $wgFragmentMode + * in DefaultSettings.php + * @return {string} Encoded string + */ + function escapeIdInternal( str, mode ) { + str = String( str ); + + switch ( mode ) { + case 'html5': + return str.replace( / /g, '_' ); + case 'html5-legacy': + str = str.replace( /[ \t\n\r\f_'"&#%]+/g, '_' ) + .replace( /^_+|_+$/, '' ); + if ( str === '' ) { + str = '_'; + } + return str; + case 'legacy': + return rawurlencode( str.replace( / /g, '_' ) ) + .replace( /%3A/g, ':' ) + .replace( /%/g, '.' ); + default: + throw new Error( 'Unrecognized ID escaping mode ' + mode ); + } + } + + /** + * Utility library + * @class mw.util + * @singleton + */ + util = { + + /* Main body */ + + /** + * Encode the string like PHP's rawurlencode + * + * @param {string} str String to be encoded. + * @return {string} Encoded string + */ + rawurlencode: rawurlencode, + + /** + * Encode string into HTML id compatible form suitable for use in HTML + * Analog to PHP Sanitizer::escapeIdForAttribute() + * + * @since 1.30 + * + * @param {string} str String to encode + * @return {string} Encoded string + */ + escapeIdForAttribute: function ( str ) { + var mode = mw.config.get( 'wgFragmentMode' )[ 0 ]; + + return escapeIdInternal( str, mode ); + }, + + /** + * Encode string into HTML id compatible form suitable for use in links + * Analog to PHP Sanitizer::escapeIdForLink() + * + * @since 1.30 + * + * @param {string} str String to encode + * @return {string} Encoded string + */ + escapeIdForLink: function ( str ) { + var mode = mw.config.get( 'wgFragmentMode' )[ 0 ]; + + return escapeIdInternal( str, mode ); + }, + + /** + * 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. + * @return {string} Encoded string + */ + 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( /%7E/g, '~' ) + .replace( /%3A/g, ':' ); + }, + + /** + * Get the link to a page name (relative to `wgServer`), + * + * @param {string|null} [pageName=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 `pageName` + */ + getUrl: function ( pageName, params ) { + var titleFragmentStart, url, query, + fragment = '', + title = typeof pageName === 'string' ? pageName : mw.config.get( 'wgPageName' ); + + // Find any fragment + titleFragmentStart = title.indexOf( '#' ); + if ( titleFragmentStart !== -1 ) { + fragment = title.slice( titleFragmentStart + 1 ); + // Exclude the fragment from the page name + title = title.slice( 0, titleFragmentStart ); + } + + // Produce query string + if ( params ) { + query = $.param( params ); + } + if ( query ) { + url = title ? + util.wikiScript() + '?title=' + util.wikiUrlencode( title ) + '&' + query : + util.wikiScript() + '?' + query; + } else { + url = mw.config.get( 'wgArticlePath' ) + .replace( '$1', util.wikiUrlencode( title ).replace( /\$/g, '$$$$' ) ); + } + + // Append the encoded fragment + if ( fragment.length ) { + url += '#' + util.escapeIdForLink( fragment ); + } + + return url; + }, + + /** + * Get address to a script in the wiki root. + * For index.php use `mw.config.get( 'wgScript' )`. + * + * @since 1.18 + * @param {string} str Name of script (e.g. 'api'), defaults to 'index' + * @return {string} Address to script (e.g. '/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 + '.php'; + } + }, + + /** + * Append a new style block to the head and return the CSSStyleSheet object. + * Use .ownerNode to access the `