Merge "Rename wfIsHipHop() to wfIsHHVM()"
[lhc/web/wiklou.git] / includes / resourceloader / ResourceLoaderStartUpModule.php
1 <?php
2 /**
3 * Module for resource loader initialization.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @author Trevor Parscal
22 * @author Roan Kattouw
23 */
24
25 class ResourceLoaderStartUpModule extends ResourceLoaderModule {
26
27 /* Protected Members */
28
29 protected $modifiedTime = array();
30 protected $targets = array( 'desktop', 'mobile' );
31
32 /* Protected Methods */
33
34 /**
35 * @param $context ResourceLoaderContext
36 * @return array
37 */
38 protected function getConfig( $context ) {
39 global $wgLoadScript, $wgScript, $wgStylePath, $wgScriptExtension,
40 $wgArticlePath, $wgScriptPath, $wgServer, $wgContLang,
41 $wgVariantArticlePath, $wgActionPaths, $wgVersion,
42 $wgEnableAPI, $wgEnableWriteAPI, $wgDBname,
43 $wgSitename, $wgFileExtensions, $wgExtensionAssetsPath,
44 $wgCookiePrefix, $wgResourceLoaderMaxQueryLength,
45 $wgResourceLoaderStorageEnabled, $wgResourceLoaderStorageVersion;
46
47 $mainPage = Title::newMainPage();
48
49 /**
50 * Namespace related preparation
51 * - wgNamespaceIds: Key-value pairs of all localized, canonical and aliases for namespaces.
52 * - wgCaseSensitiveNamespaces: Array of namespaces that are case-sensitive.
53 */
54 $namespaceIds = $wgContLang->getNamespaceIds();
55 $caseSensitiveNamespaces = array();
56 foreach ( MWNamespace::getCanonicalNamespaces() as $index => $name ) {
57 $namespaceIds[$wgContLang->lc( $name )] = $index;
58 if ( !MWNamespace::isCapitalized( $index ) ) {
59 $caseSensitiveNamespaces[] = $index;
60 }
61 }
62
63 // Build list of variables
64 $vars = array(
65 'wgLoadScript' => $wgLoadScript,
66 'debug' => $context->getDebug(),
67 'skin' => $context->getSkin(),
68 'stylepath' => $wgStylePath,
69 'wgUrlProtocols' => wfUrlProtocols(),
70 'wgArticlePath' => $wgArticlePath,
71 'wgScriptPath' => $wgScriptPath,
72 'wgScriptExtension' => $wgScriptExtension,
73 'wgScript' => $wgScript,
74 'wgVariantArticlePath' => $wgVariantArticlePath,
75 // Force object to avoid "empty" associative array from
76 // becoming [] instead of {} in JS (bug 34604)
77 'wgActionPaths' => (object)$wgActionPaths,
78 'wgServer' => $wgServer,
79 'wgUserLanguage' => $context->getLanguage(),
80 'wgContentLanguage' => $wgContLang->getCode(),
81 'wgVersion' => $wgVersion,
82 'wgEnableAPI' => $wgEnableAPI,
83 'wgEnableWriteAPI' => $wgEnableWriteAPI,
84 'wgMainPageTitle' => $mainPage->getPrefixedText(),
85 'wgFormattedNamespaces' => $wgContLang->getFormattedNamespaces(),
86 'wgNamespaceIds' => $namespaceIds,
87 'wgSiteName' => $wgSitename,
88 'wgFileExtensions' => array_values( array_unique( $wgFileExtensions ) ),
89 'wgDBname' => $wgDBname,
90 // This sucks, it is only needed on Special:Upload, but I could
91 // not find a way to add vars only for a certain module
92 'wgFileCanRotate' => BitmapHandler::canRotate(),
93 'wgAvailableSkins' => Skin::getSkinNames(),
94 'wgExtensionAssetsPath' => $wgExtensionAssetsPath,
95 // MediaWiki sets cookies to have this prefix by default
96 'wgCookiePrefix' => $wgCookiePrefix,
97 'wgResourceLoaderMaxQueryLength' => $wgResourceLoaderMaxQueryLength,
98 'wgCaseSensitiveNamespaces' => $caseSensitiveNamespaces,
99 'wgLegalTitleChars' => Title::convertByteClassToUnicodeClass( Title::legalChars() ),
100 'wgResourceLoaderStorageVersion' => $wgResourceLoaderStorageVersion,
101 'wgResourceLoaderStorageEnabled' => $wgResourceLoaderStorageEnabled,
102 );
103
104 wfRunHooks( 'ResourceLoaderGetConfigVars', array( &$vars ) );
105
106 return $vars;
107 }
108
109 /**
110 * Gets registration code for all modules
111 *
112 * @param $context ResourceLoaderContext object
113 * @return String: JavaScript code for registering all modules with the client loader
114 */
115 public static function getModuleRegistrations( ResourceLoaderContext $context ) {
116 global $wgCacheEpoch;
117 wfProfileIn( __METHOD__ );
118
119 $out = '';
120 $registrations = array();
121 $resourceLoader = $context->getResourceLoader();
122 $target = $context->getRequest()->getVal( 'target', 'desktop' );
123
124 // Register sources
125 $out .= ResourceLoader::makeLoaderSourcesScript( $resourceLoader->getSources() );
126
127 // Register modules
128 foreach ( $resourceLoader->getModuleNames() as $name ) {
129 $module = $resourceLoader->getModule( $name );
130 $moduleTargets = $module->getTargets();
131 if ( !in_array( $target, $moduleTargets ) ) {
132 continue;
133 }
134 $deps = $module->getDependencies();
135 $group = $module->getGroup();
136 $source = $module->getSource();
137 // Support module loader scripts
138 $loader = $module->getLoaderScript();
139 if ( $loader !== false ) {
140 $version = wfTimestamp( TS_ISO_8601_BASIC,
141 $module->getModifiedTime( $context ) );
142 $out .= ResourceLoader::makeCustomLoaderScript( $name, $version, $deps, $group, $source, $loader );
143 continue;
144 }
145
146 // Automatically register module
147 // getModifiedTime() is supposed to return a UNIX timestamp, but it doesn't always
148 // seem to do that, and custom implementations might forget. Coerce it to TS_UNIX
149 $moduleMtime = wfTimestamp( TS_UNIX, $module->getModifiedTime( $context ) );
150 $mtime = max( $moduleMtime, wfTimestamp( TS_UNIX, $wgCacheEpoch ) );
151 // Modules without dependencies, a group or a foreign source pass two arguments (name, timestamp) to
152 // mw.loader.register()
153 if ( !count( $deps ) && $group === null && $source === 'local' ) {
154 $registrations[] = array( $name, $mtime );
155 }
156 // Modules with dependencies but no group or foreign source pass three arguments
157 // (name, timestamp, dependencies) to mw.loader.register()
158 elseif ( $group === null && $source === 'local' ) {
159 $registrations[] = array( $name, $mtime, $deps );
160 }
161 // Modules with a group but no foreign source pass four arguments (name, timestamp, dependencies, group)
162 // to mw.loader.register()
163 elseif ( $source === 'local' ) {
164 $registrations[] = array( $name, $mtime, $deps, $group );
165 }
166 // Modules with a foreign source pass five arguments (name, timestamp, dependencies, group, source)
167 // to mw.loader.register()
168 else {
169 $registrations[] = array( $name, $mtime, $deps, $group, $source );
170 }
171 }
172 $out .= ResourceLoader::makeLoaderRegisterScript( $registrations );
173
174 wfProfileOut( __METHOD__ );
175 return $out;
176 }
177
178 /* Methods */
179
180 /**
181 * @return bool
182 */
183 public function isRaw() {
184 return true;
185 }
186
187 /**
188 * @param $context ResourceLoaderContext
189 * @return string
190 */
191 public function getScript( ResourceLoaderContext $context ) {
192 global $IP, $wgLegacyJavaScriptGlobals;
193
194 $out = file_get_contents( "$IP/resources/startup.js" );
195 if ( $context->getOnly() === 'scripts' ) {
196
197 // The core modules:
198 $moduleNames = array( 'jquery', 'mediawiki' );
199 wfRunHooks( 'ResourceLoaderGetStartupModules', array( &$moduleNames ) );
200
201 // Get the latest version
202 $loader = $context->getResourceLoader();
203 $version = 0;
204 foreach ( $moduleNames as $moduleName ) {
205 $version = max( $version,
206 $loader->getModule( $moduleName )->getModifiedTime( $context )
207 );
208 }
209 // Build load query for StartupModules
210 $query = array(
211 'modules' => ResourceLoader::makePackedModulesString( $moduleNames ),
212 'only' => 'scripts',
213 'lang' => $context->getLanguage(),
214 'skin' => $context->getSkin(),
215 'debug' => $context->getDebug() ? 'true' : 'false',
216 'version' => wfTimestamp( TS_ISO_8601_BASIC, $version )
217 );
218 // Ensure uniform query order
219 ksort( $query );
220
221 // Startup function
222 $configuration = $this->getConfig( $context );
223 $registrations = self::getModuleRegistrations( $context );
224 $registrations = str_replace( "\n", "\n\t", trim( $registrations ) ); // fix indentation
225 $out .= "var startUp = function() {\n" .
226 "\tmw.config = new " . Xml::encodeJsCall( 'mw.Map', array( $wgLegacyJavaScriptGlobals ) ) . "\n" .
227 "\t$registrations\n" .
228 "\t" . Xml::encodeJsCall( 'mw.config.set', array( $configuration ) ) .
229 "};\n";
230
231 // Conditional script injection
232 $scriptTag = Html::linkedScript( wfAppendQuery( wfScript( 'load' ), $query ) );
233 $out .= "if ( isCompatible() ) {\n" .
234 "\t" . Xml::encodeJsCall( 'document.write', array( $scriptTag ) ) .
235 "}\n" .
236 "delete isCompatible;";
237 }
238
239 return $out;
240 }
241
242 /**
243 * @return bool
244 */
245 public function supportsURLLoading() {
246 return false;
247 }
248
249 /**
250 * @param $context ResourceLoaderContext
251 * @return array|mixed
252 */
253 public function getModifiedTime( ResourceLoaderContext $context ) {
254 global $IP, $wgCacheEpoch;
255
256 $hash = $context->getHash();
257 if ( isset( $this->modifiedTime[$hash] ) ) {
258 return $this->modifiedTime[$hash];
259 }
260
261 // Call preloadModuleInfo() on ALL modules as we're about
262 // to call getModifiedTime() on all of them
263 $loader = $context->getResourceLoader();
264 $loader->preloadModuleInfo( $loader->getModuleNames(), $context );
265
266 $this->modifiedTime[$hash] = filemtime( "$IP/resources/startup.js" );
267 // ATTENTION!: Because of the line above, this is not going to cause
268 // infinite recursion - think carefully before making changes to this
269 // code!
270 $time = wfTimestamp( TS_UNIX, $wgCacheEpoch );
271 foreach ( $loader->getModuleNames() as $name ) {
272 $module = $loader->getModule( $name );
273 $time = max( $time, $module->getModifiedTime( $context ) );
274 }
275 return $this->modifiedTime[$hash] = $time;
276 }
277
278 /* Methods */
279
280 /**
281 * @return string
282 */
283 public function getGroup() {
284 return 'startup';
285 }
286 }