Merge "MWExceptionHandler::handleError: Set log severity based on error level"
[lhc/web/wiklou.git] / resources / src / startup.js
1 /**
2 * Code in this file MUST work on even the most ancient of browsers!
3 *
4 * This file is where we decide whether to initialise the modern run-time.
5 */
6
7 /* global mw, $VARS, $CODE */
8
9 var mwPerformance = ( window.performance && performance.mark ) ? performance : {
10 mark: function () {}
11 },
12 // Define now() here to ensure valid comparison with mediaWikiLoadEnd (T153819).
13 mwNow = ( function () {
14 var perf = window.performance,
15 navStart = perf && perf.timing && perf.timing.navigationStart;
16 return navStart && typeof perf.now === 'function' ?
17 function () { return navStart + perf.now(); } :
18 function () { return +new Date(); };
19 }() ),
20 // eslint-disable-next-line no-unused-vars
21 mediaWikiLoadStart = mwNow();
22
23 mwPerformance.mark( 'mwLoadStart' );
24
25 /**
26 * See <https://www.mediawiki.org/wiki/Compatibility#Browsers>
27 *
28 * Capabilities required for modern run-time:
29 * - DOM Level 4 & Selectors API Level 1
30 * - HTML5 & Web Storage
31 * - DOM Level 2 Events
32 * - JSON
33 *
34 * Browsers we support in our modern run-time (Grade A):
35 * - Chrome 4+
36 * - IE 9+
37 * - Firefox 3.5+
38 * - Safari 5+
39 * - Opera 10.5+
40 * - Mobile Safari (iOS 4+)
41 * - Android 2.0+
42 *
43 * Browsers we support in our no-javascript run-time (Grade C):
44 * - Chrome 1+
45 * - IE 6+
46 * - Firefox 3+
47 * - Safari 3+
48 * - Opera 10+
49 * - WebOS < 1.5
50 * - PlayStation
51 * - Symbian-based browsers
52 * - NetFront-based browser
53 * - Opera Mini
54 * - Nokia's Ovi Browser
55 * - MeeGo's browser
56 * - Google Glass
57 * - UC Mini (speed mode on)
58 *
59 * Other browsers that pass the check are considered Grade X.
60 *
61 * @param {string} [str] User agent, defaults to navigator.userAgent
62 * @return {boolean} User agent is compatible with MediaWiki JS
63 */
64 function isCompatible( str ) {
65 var ua = str || navigator.userAgent;
66 return !!(
67 // http://caniuse.com/#feat=queryselector
68 'querySelector' in document &&
69
70 // http://caniuse.com/#feat=namevalue-storage
71 // https://developer.blackberry.com/html5/apis/v1_0/localstorage.html
72 // https://blog.whatwg.org/this-week-in-html-5-episode-30
73 'localStorage' in window &&
74
75 // http://caniuse.com/#feat=addeventlistener
76 'addEventListener' in window &&
77
78 // http://caniuse.com/#feat=json
79 // https://phabricator.wikimedia.org/T141344#2784065
80 ( window.JSON && JSON.stringify && JSON.parse ) &&
81
82 // Hardcoded exceptions for browsers that pass the requirement but we don't want to
83 // support in the modern run-time.
84 // Note: Please extend the regex instead of adding new ones
85 !(
86 ua.match( /webOS\/1\.[0-4]|SymbianOS|Series60|NetFront|Opera Mini|S40OviBrowser|MeeGo|Android.+Glass|^Mozilla\/5\.0 .+ Gecko\/$|googleweblight/ ) ||
87 ua.match( /PlayStation/i )
88 )
89 );
90 }
91
92 // Conditional script injection
93 ( function () {
94 var NORLQ, script;
95 if ( !isCompatible() ) {
96 // Undo class swapping in case of an unsupported browser.
97 // See ResourceLoaderClientHtml::getDocumentAttributes().
98 document.documentElement.className = document.documentElement.className
99 .replace( /(^|\s)client-js(\s|$)/, '$1client-nojs$2' );
100
101 NORLQ = window.NORLQ || [];
102 while ( NORLQ.length ) {
103 NORLQ.shift()();
104 }
105 window.NORLQ = {
106 push: function ( fn ) {
107 fn();
108 }
109 };
110
111 // Clear and disable the other queue
112 window.RLQ = {
113 // No-op
114 push: function () {}
115 };
116
117 return;
118 }
119
120 /**
121 * The $CODE and $VARS placeholders are substituted in ResourceLoaderStartUpModule.php.
122 */
123 function startUp() {
124 mw.config = new mw.Map( $VARS.wgLegacyJavaScriptGlobals );
125
126 $CODE.registrations();
127
128 mw.config.set( $VARS.configuration );
129
130 // Must be after mw.config.set because these callbacks may use mw.loader which
131 // needs to have values 'skin', 'debug' etc. from mw.config.
132 // eslint-disable-next-line vars-on-top
133 var RLQ = window.RLQ || [];
134 while ( RLQ.length ) {
135 RLQ.shift()();
136 }
137 window.RLQ = {
138 push: function ( fn ) {
139 fn();
140 }
141 };
142
143 // Clear and disable the other queue
144 window.NORLQ = {
145 // No-op
146 push: function () {}
147 };
148 }
149
150 script = document.createElement( 'script' );
151 script.src = $VARS.baseModulesUri;
152 script.onload = script.onreadystatechange = function () {
153 if ( !script.readyState || /loaded|complete/.test( script.readyState ) ) {
154 // Clean up
155 script.onload = script.onreadystatechange = null;
156 script = null;
157 // Callback
158 startUp();
159 }
160 };
161 document.getElementsByTagName( 'head' )[ 0 ].appendChild( script );
162 }() );