Merge "Update ObjectFactory and ConvertibleTimestamp"
[lhc/web/wiklou.git] / resources / src / startup / startup.js
1 /**
2 * This file is where we decide whether to initialise the Grade A run-time.
3 *
4 * - Beware: This file MUST parse without errors on even the most ancient of browsers!
5 */
6 /* eslint-disable no-implicit-globals */
7 /* global $VARS, $CODE, RLQ:true, NORLQ:true */
8
9 /**
10 * See <https://www.mediawiki.org/wiki/Compatibility#Browsers>
11 *
12 * Capabilities required for modern run-time:
13 * - ECMAScript 5
14 * - DOM Level 4 & Selectors API Level 1
15 * - HTML5 & Web Storage
16 * - DOM Level 2 Events
17 *
18 * Browsers we support in our modern run-time (Grade A):
19 * - Chrome 13+
20 * - IE 11+
21 * - Firefox 4+
22 * - Safari 5+
23 * - Opera 15+
24 * - Mobile Safari 6.0+ (iOS 6+)
25 * - Android 4.1+
26 *
27 * Browsers we support in our no-javascript run-time (Grade C):
28 * - Chrome 1+
29 * - IE 6+
30 * - Firefox 3+
31 * - Safari 3+
32 * - Opera 15+
33 * - Mobile Safari 5.0+ (iOS 4+)
34 * - Android 2.0+
35 * - WebOS < 1.5
36 * - PlayStation
37 * - Symbian-based browsers
38 * - NetFront-based browser
39 * - Opera Mini
40 * - Nokia's Ovi Browser
41 * - MeeGo's browser
42 * - Google Glass
43 * - UC Mini (speed mode on)
44 *
45 * Other browsers that pass the check are considered Grade X.
46 *
47 * @private
48 * @param {string} ua User agent string
49 * @return {boolean} User agent is compatible with MediaWiki JS
50 */
51 function isCompatible( ua ) {
52 return !!(
53 // https://caniuse.com/#feat=es5
54 // https://caniuse.com/#feat=use-strict
55 // https://caniuse.com/#feat=json / https://phabricator.wikimedia.org/T141344#2784065
56 ( function () {
57 'use strict';
58 return !this && Function.prototype.bind && window.JSON;
59 }() ) &&
60
61 // https://caniuse.com/#feat=queryselector
62 'querySelector' in document &&
63
64 // https://caniuse.com/#feat=namevalue-storage
65 // https://developer.blackberry.com/html5/apis/v1_0/localstorage.html
66 // https://blog.whatwg.org/this-week-in-html-5-episode-30
67 'localStorage' in window &&
68
69 // https://caniuse.com/#feat=addeventlistener
70 'addEventListener' in window &&
71
72 // Hardcoded exceptions for browsers that pass the requirement but we don't
73 // want to support in the modern run-time.
74 //
75 // Please extend the regex instead of adding new ones!
76 // And add a test case to startup.test.js
77 !ua.match( /MSIE 10|webOS\/1\.[0-4]|SymbianOS|NetFront|Opera Mini|S40OviBrowser|MeeGo|Android.+Glass|^Mozilla\/5\.0 .+ Gecko\/$|googleweblight|PLAYSTATION|PlayStation/ )
78 );
79 }
80
81 if ( !isCompatible( navigator.userAgent ) ) {
82 // Handle Grade C
83 // Undo speculative Grade A <html> class. See ResourceLoaderClientHtml::getDocumentAttributes().
84 document.documentElement.className = document.documentElement.className
85 .replace( /(^|\s)client-js(\s|$)/, '$1client-nojs$2' );
86
87 // Process any callbacks for Grade C
88 while ( window.NORLQ && NORLQ[ 0 ] ) {
89 NORLQ.shift()();
90 }
91 NORLQ = {
92 push: function ( fn ) {
93 fn();
94 }
95 };
96
97 // Clear and disable the Grade A queue
98 RLQ = {
99 push: function () {}
100 };
101 } else {
102 // Handle Grade A
103
104 if ( window.performance && performance.mark ) {
105 performance.mark( 'mwStartup' );
106 }
107
108 // This embeds mediawiki.js, which defines 'mw' and 'mw.loader'.
109 $CODE.defineLoader();
110
111 /**
112 * The $CODE and $VARS placeholders are substituted in ResourceLoaderStartUpModule.php.
113 */
114 ( function () {
115 /* global mw */
116 mw.config = new mw.Map( $VARS.wgLegacyJavaScriptGlobals );
117
118 $CODE.registrations();
119
120 mw.config.set( $VARS.configuration );
121 // For the current page
122 mw.config.set( window.RLCONF || {} );
123 mw.loader.state( window.RLSTATE || {} );
124 mw.loader.load( window.RLPAGEMODULES || [] );
125
126 // Process RLQ callbacks
127 //
128 // The code in these callbacks could've been exposed from load.php and
129 // requested client-side. Instead, they are pushed by the server directly
130 // (from ResourceLoaderClientHtml and other parts of MediaWiki). This
131 // saves the need for additional round trips. It also allows load.php
132 // to remain stateless and sending personal data in the HTML instead.
133 //
134 // The HTML inline script lazy-defines the 'RLQ' array. Now that we are
135 // processing it, replace it with an implementation where 'push' actually
136 // considers executing the code directly. This is to ensure any late
137 // arrivals will also be processed. Late arrival can happen because
138 // startup.js is executed asynchronously, concurrently with the streaming
139 // response of the HTML.
140 RLQ = window.RLQ || [];
141 RLQ.push = function ( fn ) {
142 if ( typeof fn === 'function' ) {
143 fn();
144 } else {
145 // If the first parameter is not a function, then it is an array
146 // containing a list of required module names and a function.
147 // Do an actual push for now, as this signature is handled
148 // later by mediawiki.base.js.
149 RLQ[ RLQ.length ] = fn;
150 }
151 };
152 while ( RLQ[ 0 ] ) {
153 // Process all values gathered so far
154 RLQ.push( RLQ.shift() );
155 }
156
157 // Clear and disable the Grade C queue
158 NORLQ = {
159 push: function () {}
160 };
161 }() );
162 }