| 1 | 'use strict'; |
| 2 | |
| 3 | const slice = Array.prototype.slice; |
| 4 | |
| 5 | // Apply site-level data |
| 6 | mw.config.set( require( './config.json' ) ); |
| 7 | |
| 8 | require( './log.js' ); |
| 9 | |
| 10 | /** |
| 11 | * @class mw.Message |
| 12 | * @classdesc Describes a translateable text or HTML string. Similar to the Message class in MediaWiki PHP. |
| 13 | * |
| 14 | * @example |
| 15 | * var obj, str; |
| 16 | * mw.messages.set( { |
| 17 | * 'hello': 'Hello world', |
| 18 | * 'hello-user': 'Hello, $1!', |
| 19 | * 'welcome-user': 'Welcome back to $2, $1! Last visit by $1: $3', |
| 20 | * 'so-unusual': 'You will find: $1' |
| 21 | * } ); |
| 22 | * |
| 23 | * obj = mw.message( 'hello' ); |
| 24 | * mw.log( obj.text() ); |
| 25 | * // Hello world |
| 26 | * |
| 27 | * obj = mw.message( 'hello-user', 'John Doe' ); |
| 28 | * mw.log( obj.text() ); |
| 29 | * // Hello, John Doe! |
| 30 | * |
| 31 | * obj = mw.message( 'welcome-user', 'John Doe', 'Wikipedia', '2 hours ago' ); |
| 32 | * mw.log( obj.text() ); |
| 33 | * // Welcome back to Wikipedia, John Doe! Last visit by John Doe: 2 hours ago |
| 34 | * |
| 35 | * // Using mw.msg shortcut, always in "text' format. |
| 36 | * str = mw.msg( 'hello-user', 'John Doe' ); |
| 37 | * mw.log( str ); |
| 38 | * // Hello, John Doe! |
| 39 | * |
| 40 | * // Different formats |
| 41 | * obj = mw.message( 'so-unusual', 'Time "after" <time>' ); |
| 42 | * |
| 43 | * mw.log( obj.text() ); |
| 44 | * // You will find: Time "after" <time> |
| 45 | * |
| 46 | * mw.log( obj.escaped() ); |
| 47 | * // You will find: Time "after" <time> |
| 48 | * |
| 49 | * @constructor |
| 50 | * @description Object constructor for messages. The constructor is not publicly accessible; |
| 51 | * use {@link mw.message} instead. |
| 52 | * @param {mw.Map} map Message store |
| 53 | * @param {string} key |
| 54 | * @param {Array} [parameters] |
| 55 | */ |
| 56 | function Message( map, key, parameters ) { |
| 57 | this.map = map; |
| 58 | this.key = key; |
| 59 | this.parameters = parameters || []; |
| 60 | } |
| 61 | |
| 62 | Message.prototype = /** @lends mw.Message.prototype */ { |
| 63 | /** |
| 64 | * Get parsed contents of the message. |
| 65 | * |
| 66 | * The default parser does simple $N replacements and nothing else. |
| 67 | * This may be overridden to provide a more complex message parser. |
| 68 | * The primary override is in the mediawiki.jqueryMsg module. |
| 69 | * |
| 70 | * This function will not be called for nonexistent messages. |
| 71 | * For internal use by mediawiki.jqueryMsg only |
| 72 | * |
| 73 | * @private |
| 74 | * @param {string} format |
| 75 | * @return {string} Parsed message |
| 76 | */ |
| 77 | parser: function ( format ) { |
| 78 | let text = this.map.get( this.key ); |
| 79 | |
| 80 | // Apply qqx formatting. |
| 81 | // |
| 82 | // - Keep this synchronised with LanguageQqx/MessageCache in PHP. |
| 83 | // - Keep this synchronised with mw.jqueryMsg.Parser#getAst. |
| 84 | // |
| 85 | // Unlike LanguageQqx in PHP, this doesn't replace unconditionally. |
| 86 | // It replaces non-existent messages, and messages that were exported by |
| 87 | // load.php as "(key)" in qqx formatting. Some extensions export other data |
| 88 | // via their message blob (T222944). |
| 89 | if ( |
| 90 | mw.config.get( 'wgUserLanguage' ) === 'qqx' && |
| 91 | ( !text || text === '(' + this.key + ')' ) |
| 92 | ) { |
| 93 | text = '(' + this.key + '$*)'; |
| 94 | } |
| 95 | text = mw.format( text, ...this.parameters ); |
| 96 | if ( format === 'parse' ) { |
| 97 | // We don't know how to parse anything, so escape it all |
| 98 | text = mw.html.escape( text ); |
| 99 | } |
| 100 | return text; |
| 101 | }, |
| 102 | |
| 103 | /** |
| 104 | * Add (does not replace) parameters for `$N` placeholder values. |
| 105 | * |
| 106 | * @param {Array} parameters |
| 107 | * @return {mw.Message} |
| 108 | * @chainable |
| 109 | */ |
| 110 | params: function ( parameters ) { |
| 111 | this.parameters.push( ...parameters ); |
| 112 | return this; |
| 113 | }, |
| 114 | |
| 115 | /** |
| 116 | * Convert message object to a string using the "text"-format . |
| 117 | * |
| 118 | * This exists for implicit string type casting only. |
| 119 | * Do not call this directly. Use mw.Message#text() instead, one of the |
| 120 | * other format methods. |
| 121 | * |
| 122 | * @private |
| 123 | * @param {string} [format="text"] Internal parameter. Uses "text" if called |
| 124 | * implicitly through string casting. |
| 125 | * @return {string} Message in the given format, or `⧼key⧽` if the key |
| 126 | * does not exist. |
| 127 | */ |
| 128 | toString: function ( format ) { |
| 129 | if ( !this.exists() ) { |
| 130 | // Make sure qqx works for non-existent messages, see parser() above. |
| 131 | if ( mw.config.get( 'wgUserLanguage' ) !== 'qqx' ) { |
| 132 | // Use ⧼key⧽ as text if key does not exist |
| 133 | // Err on the side of safety, ensure that the output |
| 134 | // is always html safe in the event the message key is |
| 135 | // missing, since in that case its highly likely the |
| 136 | // message key is user-controlled. |
| 137 | // '⧼' is used instead of '<' to side-step any |
| 138 | // double-escaping issues. |
| 139 | // (Keep synchronised with Message::toString() in PHP.) |
| 140 | return '⧼' + mw.html.escape( this.key ) + '⧽'; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | if ( !format ) { |
| 145 | format = 'text'; |
| 146 | } |
| 147 | |
| 148 | if ( format === 'plain' || format === 'text' || format === 'parse' ) { |
| 149 | return this.parser( format ); |
| 150 | } |
| 151 | |
| 152 | // Format: 'escaped' (including for any invalid format, default to safe escape) |
| 153 | return mw.html.escape( this.parser( 'escaped' ) ); |
| 154 | }, |
| 155 | |
| 156 | /** |
| 157 | * Parse message as wikitext and return HTML. |
| 158 | * |
| 159 | * If jqueryMsg is loaded, this transforms text and parses a subset of supported wikitext |
| 160 | * into HTML. Without jqueryMsg, it is equivalent to {@link mw.Message#escaped}. |
| 161 | * |
| 162 | * @return {string} String form of parsed message |
| 163 | */ |
| 164 | parse: function () { |
| 165 | return this.toString( 'parse' ); |
| 166 | }, |
| 167 | |
| 168 | /** |
| 169 | * Return message plainly. |
| 170 | * |
| 171 | * This substitutes parameters, but otherwise does not transform the |
| 172 | * message content. |
| 173 | * |
| 174 | * @return {string} String form of plain message |
| 175 | */ |
| 176 | plain: function () { |
| 177 | return this.toString( 'plain' ); |
| 178 | }, |
| 179 | |
| 180 | /** |
| 181 | * Format message with text transformations applied. |
| 182 | * |
| 183 | * If jqueryMsg is loaded, `{{`-transformation is done for supported |
| 184 | * magic words such as `{{plural:}}`, `{{gender:}}`, and `{{int:}}`. |
| 185 | * Without jqueryMsg, it is equivalent to {@link mw.Message#plain}. |
| 186 | * |
| 187 | * @return {string} String form of text message |
| 188 | */ |
| 189 | text: function () { |
| 190 | return this.toString( 'text' ); |
| 191 | }, |
| 192 | |
| 193 | /** |
| 194 | * Format message and return as escaped text in HTML. |
| 195 | * |
| 196 | * This is equivalent to the #text format, which is then HTML-escaped. |
| 197 | * |
| 198 | * @return {string} String form of html escaped message |
| 199 | */ |
| 200 | escaped: function () { |
| 201 | return this.toString( 'escaped' ); |
| 202 | }, |
| 203 | |
| 204 | /** |
| 205 | * Check if a message exists. Equivalent to {@link mw.Map.exists}. |
| 206 | * |
| 207 | * @return {boolean} |
| 208 | */ |
| 209 | exists: function () { |
| 210 | return this.map.exists( this.key ); |
| 211 | } |
| 212 | }; |
| 213 | |
| 214 | /** |
| 215 | * @class mw |
| 216 | * @singleton |
| 217 | * @borrows mediawiki.inspect.runReports as inspect |
| 218 | */ |
| 219 | |
| 220 | /** |
| 221 | * Empty object for third-party libraries, for cases where you don't |
| 222 | * want to add a new global, or the global is bad and needs containment |
| 223 | * or wrapping. |
| 224 | * |
| 225 | * @type {Object} |
| 226 | */ |
| 227 | mw.libs = {}; |
| 228 | |
| 229 | /** |
| 230 | * OOUI widgets specific to MediaWiki. |
| 231 | * Initially empty. To expand the amount of available widgets the `mediawiki.widget` module can be loaded. |
| 232 | * |
| 233 | * @namespace mw.widgets |
| 234 | * @example |
| 235 | * mw.loader.using('mediawiki.widget').then(() => { |
| 236 | * OO.ui.getWindowManager().addWindows( [ new mw.widget.AbandonEditDialog() ] ); |
| 237 | * }); |
| 238 | */ |
| 239 | mw.widgets = {}; |
| 240 | |
| 241 | /** |
| 242 | * Generates a ResourceLoader report using the |
| 243 | * {@link mediawiki.inspect.js.html|mediawiki.inspect module}. |
| 244 | * |
| 245 | * @ignore |
| 246 | */ |
| 247 | mw.inspect = function ( ...reports ) { |
| 248 | // Lazy-load |
| 249 | mw.loader.using( 'mediawiki.inspect', () => { |
| 250 | mw.inspect.runReports( ...reports ); |
| 251 | } ); |
| 252 | }; |
| 253 | |
| 254 | /** |
| 255 | * Replace `$*` with a list of parameters for `uselang=qqx` support. |
| 256 | * |
| 257 | * @private |
| 258 | * @since 1.33 |
| 259 | * @param {string} formatString Format string |
| 260 | * @param {Array} parameters Values for $N replacements |
| 261 | * @return {string} Transformed format string |
| 262 | */ |
| 263 | mw.internalDoTransformFormatForQqx = function ( formatString, parameters ) { |
| 264 | if ( formatString.indexOf( '$*' ) !== -1 ) { |
| 265 | let replacement = ''; |
| 266 | if ( parameters.length ) { |
| 267 | replacement = ': ' + parameters.map( ( _, i ) => '$' + ( i + 1 ) ).join( ', ' ); |
| 268 | } |
| 269 | return formatString.replace( '$*', replacement ); |
| 270 | } |
| 271 | return formatString; |
| 272 | }; |
| 273 | |
| 274 | /** |
| 275 | * Encode page titles in a way that matches `wfUrlencode` in PHP. |
| 276 | * |
| 277 | * @see mw.util#wikiUrlencode |
| 278 | * @private |
| 279 | * @param {string} str |
| 280 | * @return {string} |
| 281 | */ |
| 282 | mw.internalWikiUrlencode = function ( str ) { |
| 283 | return encodeURIComponent( String( str ) ) |
| 284 | .replace( /'/g, '%27' ) |
| 285 | .replace( /%20/g, '_' ) |
| 286 | .replace( /%3B/g, ';' ) |
| 287 | .replace( /%40/g, '@' ) |
| 288 | .replace( /%24/g, '$' ) |
| 289 | .replace( /%2C/g, ',' ) |
| 290 | .replace( /%2F/g, '/' ) |
| 291 | .replace( /%3A/g, ':' ); |
| 292 | }; |
| 293 | |
| 294 | /** |
| 295 | * Format a string. Replace $1, $2 ... $N with positional arguments. |
| 296 | * |
| 297 | * Used by {@link mw.Message#parse}. |
| 298 | * |
| 299 | * @memberof mw |
| 300 | * @since 1.25 |
| 301 | * @param {string} formatString Format string |
| 302 | * @param {...Mixed} parameters Values for $N replacements |
| 303 | * @return {string} Formatted string |
| 304 | */ |
| 305 | mw.format = function ( formatString, ...parameters ) { |
| 306 | formatString = mw.internalDoTransformFormatForQqx( formatString, parameters ); |
| 307 | return formatString.replace( /\$(\d+)/g, ( str, match ) => { |
| 308 | const index = parseInt( match, 10 ) - 1; |
| 309 | return parameters[ index ] !== undefined ? parameters[ index ] : '$' + match; |
| 310 | } ); |
| 311 | }; |
| 312 | |
| 313 | // Expose Message constructor |
| 314 | mw.Message = Message; |
| 315 | |
| 316 | /** |
| 317 | * Get a message object. |
| 318 | * |
| 319 | * Shortcut for `new mw.Message( mw.messages, key, parameters )`. |
| 320 | * |
| 321 | * @memberof mw |
| 322 | * @see {@link mw.Message} |
| 323 | * @param {string} key Key of message to get |
| 324 | * @param {...Mixed} parameters Values for $N replacements |
| 325 | * @return {mw.Message} |
| 326 | */ |
| 327 | mw.message = function ( key ) { |
| 328 | const parameters = slice.call( arguments, 1 ); |
| 329 | return new Message( mw.messages, key, parameters ); |
| 330 | }; |
| 331 | |
| 332 | /** |
| 333 | * Get a message string using the (default) 'text' format. |
| 334 | * |
| 335 | * Shortcut for `mw.message( key, parameters... ).text()`. |
| 336 | * |
| 337 | * @memberof mw |
| 338 | * @see {@link mw.Message} |
| 339 | * @param {string} key Key of message to get |
| 340 | * @param {...any} parameters Values for $N replacements |
| 341 | * @return {string} |
| 342 | */ |
| 343 | mw.msg = function ( key, ...parameters ) { |
| 344 | // Shortcut must process text transformations by default |
| 345 | // if mediawiki.jqueryMsg is loaded. (T46459) |
| 346 | // eslint-disable-next-line mediawiki/msg-doc |
| 347 | return mw.message( key, ...parameters ).text(); |
| 348 | }; |
| 349 | |
| 350 | /** |
| 351 | * Convenience method for loading and accessing the |
| 352 | * {@link mw.notification.notify|mw.notification module}. |
| 353 | * |
| 354 | * @memberof mw |
| 355 | * @param {HTMLElement|HTMLElement[]|jQuery|mw.Message|string} message |
| 356 | * @param {Object} [options] See mw.notification#defaults for the defaults. |
| 357 | * @return {jQuery.Promise} |
| 358 | */ |
| 359 | mw.notify = function ( message, options ) { |
| 360 | // Lazy load |
| 361 | return mw.loader.using( 'mediawiki.notification' ).then( () => mw.notification.notify( message, options ) ); |
| 362 | }; |
| 363 | |
| 364 | const trackCallbacks = $.Callbacks( 'memory' ); |
| 365 | let trackHandlers = []; |
| 366 | |
| 367 | /** |
| 368 | * Track an analytic event. |
| 369 | * |
| 370 | * This method provides a generic means for MediaWiki JavaScript code to capture state |
| 371 | * information for analysis. Each logged event specifies a string topic name that describes |
| 372 | * the kind of event that it is. Topic names consist of dot-separated path components, |
| 373 | * arranged from most general to most specific. Each path component should have a clear and |
| 374 | * well-defined purpose. |
| 375 | * |
| 376 | * Data handlers are registered via `mw.trackSubscribe`, and receive the full set of |
| 377 | * events that match their subscription, including buffered events that fired before the handler |
| 378 | * was subscribed. |
| 379 | * |
| 380 | * @memberof mw |
| 381 | * @param {string} topic Topic name |
| 382 | * @param {Object|number|string} [data] Data describing the event. |
| 383 | */ |
| 384 | mw.track = function ( topic, data ) { |
| 385 | mw.trackQueue.push( { topic: topic, data: data } ); |
| 386 | trackCallbacks.fire( mw.trackQueue ); |
| 387 | }; |
| 388 | |
| 389 | /** |
| 390 | * Register a handler for subset of analytic events, specified by topic. |
| 391 | * |
| 392 | * Handlers will be called once for each tracked event, including for any buffered events that |
| 393 | * fired before the handler was subscribed. The callback is passed a `topic` string, and optional |
| 394 | * `data` event object. The `this` value for the callback is a plain object with `topic` and |
| 395 | * `data` properties set to those same values. |
| 396 | * |
| 397 | * @example |
| 398 | * // To monitor all topics for debugging |
| 399 | * mw.trackSubscribe( '', console.log ); |
| 400 | * |
| 401 | * @example |
| 402 | * // To subscribe to any of `foo.*`, e.g. both `foo.bar` and `foo.quux` |
| 403 | * mw.trackSubscribe( 'foo.', console.log ); |
| 404 | * |
| 405 | * @memberof mw |
| 406 | * @param {string} topic Handle events whose name starts with this string prefix |
| 407 | * @param {Function} callback Handler to call for each matching tracked event |
| 408 | * @param {string} callback.topic |
| 409 | * @param {Object} [callback.data] |
| 410 | */ |
| 411 | mw.trackSubscribe = function ( topic, callback ) { |
| 412 | let seen = 0; |
| 413 | function handler( trackQueue ) { |
| 414 | for ( ; seen < trackQueue.length; seen++ ) { |
| 415 | const event = trackQueue[ seen ]; |
| 416 | if ( event.topic.indexOf( topic ) === 0 ) { |
| 417 | callback.call( event, event.topic, event.data ); |
| 418 | } |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | trackHandlers.push( [ handler, callback ] ); |
| 423 | trackCallbacks.add( handler ); |
| 424 | }; |
| 425 | |
| 426 | /** |
| 427 | * Stop handling events for a particular handler. |
| 428 | * |
| 429 | * @memberof mw |
| 430 | * @param {Function} callback |
| 431 | */ |
| 432 | mw.trackUnsubscribe = function ( callback ) { |
| 433 | trackHandlers = trackHandlers.filter( ( fns ) => { |
| 434 | if ( fns[ 1 ] === callback ) { |
| 435 | trackCallbacks.remove( fns[ 0 ] ); |
| 436 | // Ensure the tuple is removed to avoid holding on to closures |
| 437 | return false; |
| 438 | } |
| 439 | return true; |
| 440 | } ); |
| 441 | }; |
| 442 | |
| 443 | // Notify subscribers of any mw.trackQueue.push() calls |
| 444 | // from the startup module before mw.track() is defined. |
| 445 | trackCallbacks.fire( mw.trackQueue ); |
| 446 | |
| 447 | /** |
| 448 | * @namespace Hooks |
| 449 | * @description Registry and firing of events. |
| 450 | * |
| 451 | * MediaWiki has various interface components that are extended, enhanced |
| 452 | * or manipulated in some other way by extensions, gadgets and even |
| 453 | * in core itself. |
| 454 | * |
| 455 | * This framework helps streamlining the timing of when these other |
| 456 | * code paths fire their plugins (instead of using document-ready, |
| 457 | * which can and should be limited to firing only once). |
| 458 | * |
| 459 | * Features like navigating to other wiki pages, previewing an edit |
| 460 | * and editing itself – without a refresh – can then retrigger these |
| 461 | * hooks accordingly to ensure everything still works as expected. |
| 462 | * See {@link Hook}. |
| 463 | * |
| 464 | * Example usage: |
| 465 | * ``` |
| 466 | * mw.hook( 'wikipage.content' ).add( fn ).remove( fn ); |
| 467 | * mw.hook( 'wikipage.content' ).fire( $content ); |
| 468 | * ``` |
| 469 | * |
| 470 | * Handlers can be added and fired for arbitrary event names at any time. The same |
| 471 | * event can be fired multiple times. The last run of an event is memorized |
| 472 | * (similar to `$(document).ready` and `$.Deferred().done`). |
| 473 | * This means if an event is fired, and a handler added afterwards, the added |
| 474 | * function will be fired right away with the last given event data. |
| 475 | * |
| 476 | * Like Deferreds and Promises, the {@link mw.hook} object is both detachable and chainable. |
| 477 | * Thus allowing flexible use and optimal maintainability and authority control. |
| 478 | * You can pass around the `add` and/or `fire` method to another piece of code |
| 479 | * without it having to know the event name (or {@link mw.hook} for that matter). |
| 480 | * |
| 481 | * ``` |
| 482 | * var h = mw.hook( 'bar.ready' ); |
| 483 | * new mw.Foo( .. ).fetch( { callback: h.fire } ); |
| 484 | * ``` |
| 485 | * |
| 486 | * The function signature for hooks can be considered [stable](https://www.mediawiki.org/wiki/Special:MyLanguage/Stable_interface_policy/Frontend). |
| 487 | * See available global events below. |
| 488 | */ |
| 489 | |
| 490 | const hooks = Object.create( null ); |
| 491 | |
| 492 | /** |
| 493 | * Create an instance of {@link Hook}. |
| 494 | * |
| 495 | * @example |
| 496 | * const hook = mw.hook( 'name' ); |
| 497 | * hook.add( () => alert( 'Hook was fired' ) ); |
| 498 | * hook.fire(); |
| 499 | * |
| 500 | * @param {string} name Name of hook. |
| 501 | * @return {Hook} |
| 502 | */ |
| 503 | mw.hook = function ( name ) { |
| 504 | return hooks[ name ] || ( hooks[ name ] = ( function () { |
| 505 | let memory; |
| 506 | const fns = []; |
| 507 | function rethrow( e ) { |
| 508 | setTimeout( () => { |
| 509 | throw e; |
| 510 | } ); |
| 511 | } |
| 512 | /** |
| 513 | * @class Hook |
| 514 | * @classdesc An instance of a hook, created via [mw.hook method]{@link mw.hook}. |
| 515 | * @global |
| 516 | * @hideconstructor |
| 517 | */ |
| 518 | return { |
| 519 | /** |
| 520 | * Register a hook handler. |
| 521 | * |
| 522 | * @param {...Function} handler Function to bind. |
| 523 | * @memberof Hook |
| 524 | * @return {Hook} |
| 525 | */ |
| 526 | add: function () { |
| 527 | for ( let i = 0; i < arguments.length; i++ ) { |
| 528 | fns.push( arguments[ i ] ); |
| 529 | if ( memory ) { |
| 530 | try { |
| 531 | arguments[ i ].apply( null, memory ); |
| 532 | } catch ( e ) { |
| 533 | rethrow( e ); |
| 534 | } |
| 535 | } |
| 536 | } |
| 537 | return this; |
| 538 | }, |
| 539 | /** |
| 540 | * Unregister a hook handler. |
| 541 | * |
| 542 | * @param {...Function} handler Function to unbind. |
| 543 | * @memberof Hook |
| 544 | * @return {Hook} |
| 545 | */ |
| 546 | remove: function () { |
| 547 | for ( let i = 0; i < arguments.length; i++ ) { |
| 548 | let j; |
| 549 | while ( ( j = fns.indexOf( arguments[ i ] ) ) !== -1 ) { |
| 550 | fns.splice( j, 1 ); |
| 551 | } |
| 552 | } |
| 553 | return this; |
| 554 | }, |
| 555 | /** |
| 556 | * Call hook handlers with data. |
| 557 | * |
| 558 | * @memberof Hook |
| 559 | * @param {...any} data |
| 560 | * @return {Hook} |
| 561 | * @chainable |
| 562 | */ |
| 563 | fire: function () { |
| 564 | for ( let i = 0; i < fns.length; i++ ) { |
| 565 | try { |
| 566 | fns[ i ].apply( null, arguments ); |
| 567 | } catch ( e ) { |
| 568 | rethrow( e ); |
| 569 | } |
| 570 | } |
| 571 | memory = slice.call( arguments ); |
| 572 | return this; |
| 573 | } |
| 574 | }; |
| 575 | }() ) ); |
| 576 | }; |
| 577 | |
| 578 | /** |
| 579 | * HTML construction helper functions. |
| 580 | * |
| 581 | * @example |
| 582 | * var Html, output; |
| 583 | * |
| 584 | * Html = mw.html; |
| 585 | * output = Html.element( 'div', {}, new Html.Raw( |
| 586 | * Html.element( 'img', { src: '<' } ) |
| 587 | * ) ); |
| 588 | * mw.log( output ); // <div><img src="<"/></div> |
| 589 | * |
| 590 | * @namespace mw.html |
| 591 | */ |
| 592 | |
| 593 | function escapeCallback( s ) { |
| 594 | switch ( s ) { |
| 595 | case '\'': |
| 596 | return '''; |
| 597 | case '"': |
| 598 | return '"'; |
| 599 | case '<': |
| 600 | return '<'; |
| 601 | case '>': |
| 602 | return '>'; |
| 603 | case '&': |
| 604 | return '&'; |
| 605 | } |
| 606 | } |
| 607 | mw.html = { |
| 608 | /** |
| 609 | * Escape a string for HTML. |
| 610 | * |
| 611 | * Converts special characters to HTML entities. |
| 612 | * |
| 613 | * @example |
| 614 | * mw.html.escape( '< > \' & "' ); |
| 615 | * // Returns < > ' & " |
| 616 | * |
| 617 | * @param {string} s The string to escape |
| 618 | * @return {string} HTML |
| 619 | */ |
| 620 | escape: function ( s ) { |
| 621 | return s.replace( /['"<>&]/g, escapeCallback ); |
| 622 | }, |
| 623 | |
| 624 | /** |
| 625 | * Create an HTML element string, with safe escaping. |
| 626 | * |
| 627 | * @param {string} name The tag name. |
| 628 | * @param {Object} [attrs] An object with members mapping element names to values |
| 629 | * @param {string|mw.html.Raw|null} [contents=null] The contents of the element. |
| 630 | * |
| 631 | * - string: Text to be escaped. |
| 632 | * - null: The element is treated as void with short closing form, e.g. `<br/>`. |
| 633 | * - this.Raw: The raw value is directly included. |
| 634 | * @return {string} HTML |
| 635 | */ |
| 636 | element: function ( name, attrs, contents ) { |
| 637 | let s = '<' + name; |
| 638 | |
| 639 | if ( attrs ) { |
| 640 | for ( const attrName in attrs ) { |
| 641 | let v = attrs[ attrName ]; |
| 642 | // Convert name=true, to name=name |
| 643 | if ( v === true ) { |
| 644 | v = attrName; |
| 645 | // Skip name=false |
| 646 | } else if ( v === false ) { |
| 647 | continue; |
| 648 | } |
| 649 | s += ' ' + attrName + '="' + this.escape( String( v ) ) + '"'; |
| 650 | } |
| 651 | } |
| 652 | if ( contents === undefined || contents === null ) { |
| 653 | // Self close tag |
| 654 | s += '/>'; |
| 655 | return s; |
| 656 | } |
| 657 | // Regular open tag |
| 658 | s += '>'; |
| 659 | if ( typeof contents === 'string' ) { |
| 660 | // Escaped |
| 661 | s += this.escape( contents ); |
| 662 | } else if ( typeof contents === 'number' || typeof contents === 'boolean' ) { |
| 663 | // Convert to string |
| 664 | s += String( contents ); |
| 665 | } else if ( contents instanceof this.Raw ) { |
| 666 | // Raw HTML inclusion |
| 667 | s += contents.value; |
| 668 | } else { |
| 669 | throw new Error( 'Invalid content type' ); |
| 670 | } |
| 671 | s += '</' + name + '>'; |
| 672 | return s; |
| 673 | }, |
| 674 | |
| 675 | /** |
| 676 | * @classdesc Wrapper object for raw HTML. Can be used with {@link mw.html.element}. |
| 677 | * @class mw.html.Raw |
| 678 | * @param {string} value |
| 679 | * @example |
| 680 | * const raw = new mw.html.Raw( 'Text' ); |
| 681 | * mw.html.element( 'div', { class: 'html' }, raw ); |
| 682 | */ |
| 683 | Raw: function ( value ) { |
| 684 | this.value = value; |
| 685 | } |
| 686 | }; |
| 687 | |
| 688 | /** |
| 689 | * Schedule a function to run once the page is ready (DOM loaded). |
| 690 | * |
| 691 | * @since 1.5.8 |
| 692 | * @memberof window |
| 693 | * @param {Function} fn |
| 694 | */ |
| 695 | window.addOnloadHook = function ( fn ) { |
| 696 | $( () => { |
| 697 | fn(); |
| 698 | } ); |
| 699 | }; |
| 700 | |
| 701 | const loadedScripts = {}; |
| 702 | |
| 703 | /** |
| 704 | * Import a script using an absolute URI. |
| 705 | * |
| 706 | * @since 1.12.2 |
| 707 | * @memberof window |
| 708 | * @param {string} url |
| 709 | * @return {HTMLElement|null} Script tag, or null if it was already imported before |
| 710 | */ |
| 711 | window.importScriptURI = function ( url ) { |
| 712 | if ( loadedScripts[ url ] ) { |
| 713 | return null; |
| 714 | } |
| 715 | loadedScripts[ url ] = true; |
| 716 | return mw.loader.addScriptTag( url ); |
| 717 | }; |
| 718 | |
| 719 | /** |
| 720 | * Import a local JS content page, for use by user scripts and site-wide scripts. |
| 721 | * |
| 722 | * Note that if the same title is imported multiple times, it will only |
| 723 | * be loaded and executed once. |
| 724 | * |
| 725 | * @since 1.12.2 |
| 726 | * @memberof window |
| 727 | * @param {string} title |
| 728 | * @return {HTMLElement|null} Script tag, or null if it was already imported before |
| 729 | */ |
| 730 | window.importScript = function ( title ) { |
| 731 | return window.importScriptURI( |
| 732 | mw.config.get( 'wgScript' ) + '?title=' + mw.internalWikiUrlencode( title ) + |
| 733 | '&action=raw&ctype=text/javascript' |
| 734 | ); |
| 735 | }; |
| 736 | |
| 737 | /** |
| 738 | * Import a local CSS content page, for use by user scripts and site-wide scripts. |
| 739 | * |
| 740 | * @since 1.12.2 |
| 741 | * @memberof window |
| 742 | * @param {string} title |
| 743 | * @return {HTMLElement} Link tag |
| 744 | */ |
| 745 | window.importStylesheet = function ( title ) { |
| 746 | return mw.loader.addLinkTag( |
| 747 | mw.config.get( 'wgScript' ) + '?title=' + mw.internalWikiUrlencode( title ) + |
| 748 | '&action=raw&ctype=text/css' |
| 749 | ); |
| 750 | }; |
| 751 | |
| 752 | /** |
| 753 | * Import a stylesheet using an absolute URI. |
| 754 | * |
| 755 | * @since 1.12.2 |
| 756 | * @memberof window |
| 757 | * @param {string} url |
| 758 | * @param {string} media |
| 759 | * @return {HTMLElement} Link tag |
| 760 | */ |
| 761 | window.importStylesheetURI = function ( url, media ) { |
| 762 | return mw.loader.addLinkTag( url, media ); |
| 763 | }; |
| 764 | |
| 765 | /** |
| 766 | * Get the names of all registered ResourceLoader modules. |
| 767 | * |
| 768 | * @memberof mw.loader |
| 769 | * @return {string[]} |
| 770 | */ |
| 771 | mw.loader.getModuleNames = function () { |
| 772 | return Object.keys( mw.loader.moduleRegistry ); |
| 773 | }; |
| 774 | |
| 775 | /** |
| 776 | * Execute a function after one or more modules are ready. |
| 777 | * |
| 778 | * Use this method if you need to dynamically control which modules are loaded |
| 779 | * and/or when they loaded (instead of declaring them as dependencies directly |
| 780 | * on your module.) |
| 781 | * |
| 782 | * This uses the same loader as for regular module dependencies. This means |
| 783 | * ResourceLoader will not re-download or re-execute a module for the second |
| 784 | * time if something else already needed it. And the same browser HTTP cache, |
| 785 | * and localStorage are checked before considering to fetch from the network. |
| 786 | * And any on-going requests from other dependencies or using() calls are also |
| 787 | * automatically re-used. |
| 788 | * |
| 789 | * Example of inline dependency on OOjs: |
| 790 | * ``` |
| 791 | * mw.loader.using( 'oojs', function () { |
| 792 | * OO.compare( [ 1 ], [ 1 ] ); |
| 793 | * } ); |
| 794 | * ``` |
| 795 | * |
| 796 | * Example of inline dependency obtained via `require()`: |
| 797 | * ``` |
| 798 | * mw.loader.using( [ 'mediawiki.util' ], function ( require ) { |
| 799 | * var util = require( 'mediawiki.util' ); |
| 800 | * } ); |
| 801 | * ``` |
| 802 | * |
| 803 | * Since MediaWiki 1.23 this returns a promise. |
| 804 | * |
| 805 | * Since MediaWiki 1.28 the promise is resolved with a `require` function. |
| 806 | * |
| 807 | * @memberof mw.loader |
| 808 | * @param {string|Array} dependencies Module name or array of modules names the |
| 809 | * callback depends on to be ready before executing |
| 810 | * @param {Function} [ready] Callback to execute when all dependencies are ready |
| 811 | * @param {Function} [error] Callback to execute if one or more dependencies failed |
| 812 | * @return {jQuery.Promise} With a `require` function |
| 813 | */ |
| 814 | mw.loader.using = function ( dependencies, ready, error ) { |
| 815 | const deferred = $.Deferred(); |
| 816 | |
| 817 | // Allow calling with a single dependency as a string |
| 818 | if ( !Array.isArray( dependencies ) ) { |
| 819 | dependencies = [ dependencies ]; |
| 820 | } |
| 821 | |
| 822 | if ( ready ) { |
| 823 | deferred.done( ready ); |
| 824 | } |
| 825 | if ( error ) { |
| 826 | deferred.fail( error ); |
| 827 | } |
| 828 | |
| 829 | try { |
| 830 | // Resolve entire dependency map |
| 831 | dependencies = mw.loader.resolve( dependencies ); |
| 832 | } catch ( e ) { |
| 833 | return deferred.reject( e ).promise(); |
| 834 | } |
| 835 | |
| 836 | mw.loader.enqueue( |
| 837 | dependencies, |
| 838 | () => { |
| 839 | deferred.resolve( mw.loader.require ); |
| 840 | }, |
| 841 | deferred.reject |
| 842 | ); |
| 843 | |
| 844 | return deferred.promise(); |
| 845 | }; |
| 846 | |
| 847 | /** |
| 848 | * Load a script by URL. |
| 849 | * |
| 850 | * @example |
| 851 | * mw.loader.getScript( |
| 852 | * 'https://example.org/x-1.0.0.js' |
| 853 | * ) |
| 854 | * .then( function () { |
| 855 | * // Script succeeded. You can use X now. |
| 856 | * }, function ( e ) { |
| 857 | * // Script failed. X is not avaiable |
| 858 | * mw.log.error( e.message ); // => "Failed to load script" |
| 859 | * } ); |
| 860 | * } ); |
| 861 | * |
| 862 | * @memberof mw.loader |
| 863 | * @param {string} url Script URL |
| 864 | * @return {jQuery.Promise} Resolved when the script is loaded |
| 865 | */ |
| 866 | mw.loader.getScript = function ( url ) { |
| 867 | return $.ajax( url, { dataType: 'script', cache: true } ) |
| 868 | .catch( () => { |
| 869 | throw new Error( 'Failed to load script' ); |
| 870 | } ); |
| 871 | }; |
| 872 | |
| 873 | // Skeleton user object, extended by the 'mediawiki.user' module. |
| 874 | /** |
| 875 | * @namespace mw.user |
| 876 | * @ignore |
| 877 | */ |
| 878 | mw.user = { |
| 879 | /** |
| 880 | * Map of user preferences and their values. |
| 881 | * |
| 882 | * @type {mw.Map} |
| 883 | */ |
| 884 | options: new mw.Map(), |
| 885 | /** |
| 886 | * Map of retrieved user tokens. |
| 887 | * |
| 888 | * @type {mw.Map} |
| 889 | */ |
| 890 | tokens: new mw.Map() |
| 891 | }; |
| 892 | |
| 893 | mw.user.options.set( require( './user.json' ) ); |
| 894 | |
| 895 | // Process callbacks for modern browsers (Grade A) that require modules. |
| 896 | const queue = window.RLQ; |
| 897 | // Replace temporary RLQ implementation from startup.js with the |
| 898 | // final implementation that also processes callbacks that can |
| 899 | // require modules. It must also support late arrivals of |
| 900 | // plain callbacks. (T208093) |
| 901 | window.RLQ = { |
| 902 | push: function ( entry ) { |
| 903 | if ( typeof entry === 'function' ) { |
| 904 | entry(); |
| 905 | } else { |
| 906 | mw.loader.using( entry[ 0 ], entry[ 1 ] ); |
| 907 | } |
| 908 | } |
| 909 | }; |
| 910 | while ( queue[ 0 ] ) { |
| 911 | window.RLQ.push( queue.shift() ); |
| 912 | } |
| 913 | |
| 914 | /** |
| 915 | * Replace document.write/writeln with basic html parsing that appends |
| 916 | * to the `<body>` to avoid blanking pages. Added JavaScript will not run. |
| 917 | * |
| 918 | * @ignore |
| 919 | * @deprecated since 1.26 |
| 920 | */ |
| 921 | [ 'write', 'writeln' ].forEach( ( func ) => { |
| 922 | mw.log.deprecate( document, func, function () { |
| 923 | $( document.body ).append( $.parseHTML( slice.call( arguments ).join( '' ) ) ); |
| 924 | }, 'Use jQuery or mw.loader.load instead.', 'document.' + func ); |
| 925 | } ); |
| 926 | |
| 927 | // Load other files in the package |
| 928 | require( './errorLogger.js' ); |