3 * Module for resource loader initialization.
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.
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.
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
21 * @author Trevor Parscal
22 * @author Roan Kattouw
25 class ResourceLoaderStartUpModule
extends ResourceLoaderModule
{
27 /* Protected Members */
29 protected $modifiedTime = array();
30 protected $targets = array( 'desktop', 'mobile' );
32 /* Protected Methods */
35 * @param $context ResourceLoaderContext
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,
48 $mainPage = Title
::newMainPage();
51 * Namespace related preparation
52 * - wgNamespaceIds: Key-value pairs of all localized, canonical and aliases for namespaces.
53 * - wgCaseSensitiveNamespaces: Array of namespaces that are case-sensitive.
55 $namespaceIds = $wgContLang->getNamespaceIds();
56 $caseSensitiveNamespaces = array();
57 foreach ( MWNamespace
::getCanonicalNamespaces() as $index => $name ) {
58 $namespaceIds[$wgContLang->lc( $name )] = $index;
59 if ( !MWNamespace
::isCapitalized( $index ) ) {
60 $caseSensitiveNamespaces[] = $index;
64 // Build list of variables
66 'wgLoadScript' => $wgLoadScript,
67 'debug' => $context->getDebug(),
68 'skin' => $context->getSkin(),
69 'stylepath' => $wgStylePath,
70 'wgUrlProtocols' => wfUrlProtocols(),
71 'wgArticlePath' => $wgArticlePath,
72 'wgScriptPath' => $wgScriptPath,
73 'wgScriptExtension' => $wgScriptExtension,
74 'wgScript' => $wgScript,
75 'wgSearchType' => $wgSearchType,
76 'wgVariantArticlePath' => $wgVariantArticlePath,
77 // Force object to avoid "empty" associative array from
78 // becoming [] instead of {} in JS (bug 34604)
79 'wgActionPaths' => (object)$wgActionPaths,
80 'wgServer' => $wgServer,
81 'wgUserLanguage' => $context->getLanguage(),
82 'wgContentLanguage' => $wgContLang->getCode(),
83 'wgVersion' => $wgVersion,
84 'wgEnableAPI' => $wgEnableAPI,
85 'wgEnableWriteAPI' => $wgEnableWriteAPI,
86 'wgMainPageTitle' => $mainPage->getPrefixedText(),
87 'wgFormattedNamespaces' => $wgContLang->getFormattedNamespaces(),
88 'wgNamespaceIds' => $namespaceIds,
89 'wgContentNamespaces' => MWNamespace
::getContentNamespaces(),
90 'wgSiteName' => $wgSitename,
91 'wgFileExtensions' => array_values( array_unique( $wgFileExtensions ) ),
92 'wgDBname' => $wgDBname,
93 // This sucks, it is only needed on Special:Upload, but I could
94 // not find a way to add vars only for a certain module
95 'wgFileCanRotate' => BitmapHandler
::canRotate(),
96 'wgAvailableSkins' => Skin
::getSkinNames(),
97 'wgExtensionAssetsPath' => $wgExtensionAssetsPath,
98 // MediaWiki sets cookies to have this prefix by default
99 'wgCookiePrefix' => $wgCookiePrefix,
100 'wgResourceLoaderMaxQueryLength' => $wgResourceLoaderMaxQueryLength,
101 'wgCaseSensitiveNamespaces' => $caseSensitiveNamespaces,
102 'wgLegalTitleChars' => Title
::convertByteClassToUnicodeClass( Title
::legalChars() ),
103 'wgResourceLoaderStorageVersion' => $wgResourceLoaderStorageVersion,
104 'wgResourceLoaderStorageEnabled' => $wgResourceLoaderStorageEnabled,
107 wfRunHooks( 'ResourceLoaderGetConfigVars', array( &$vars ) );
113 * Gets registration code for all modules
115 * @param $context ResourceLoaderContext object
116 * @return String: JavaScript code for registering all modules with the client loader
118 public static function getModuleRegistrations( ResourceLoaderContext
$context ) {
119 global $wgCacheEpoch;
120 wfProfileIn( __METHOD__
);
123 $registrations = array();
124 $resourceLoader = $context->getResourceLoader();
125 $target = $context->getRequest()->getVal( 'target', 'desktop' );
128 $out .= ResourceLoader
::makeLoaderSourcesScript( $resourceLoader->getSources() );
131 foreach ( $resourceLoader->getModuleNames() as $name ) {
132 $module = $resourceLoader->getModule( $name );
133 $moduleTargets = $module->getTargets();
134 if ( !in_array( $target, $moduleTargets ) ) {
137 $deps = $module->getDependencies();
138 $group = $module->getGroup();
139 $source = $module->getSource();
140 // Support module loader scripts
141 $loader = $module->getLoaderScript();
142 if ( $loader !== false ) {
143 $version = wfTimestamp( TS_ISO_8601_BASIC
,
144 $module->getModifiedTime( $context ) );
145 $out .= ResourceLoader
::makeCustomLoaderScript( $name, $version, $deps, $group, $source, $loader );
149 // Automatically register module
150 // getModifiedTime() is supposed to return a UNIX timestamp, but it doesn't always
151 // seem to do that, and custom implementations might forget. Coerce it to TS_UNIX
152 $moduleMtime = wfTimestamp( TS_UNIX
, $module->getModifiedTime( $context ) );
153 $mtime = max( $moduleMtime, wfTimestamp( TS_UNIX
, $wgCacheEpoch ) );
155 if ( !count( $deps ) && $group === null && $source === 'local' ) {
156 // Modules without dependencies, a group or a foreign source pass two arguments (name, timestamp) to
157 // mw.loader.register()
158 $registrations[] = array( $name, $mtime );
159 } elseif ( $group === null && $source === 'local' ) {
160 // Modules with dependencies but no group or foreign source pass three arguments
161 // (name, timestamp, dependencies) to mw.loader.register()
162 $registrations[] = array( $name, $mtime, $deps );
163 } elseif ( $source === 'local' ) {
164 // Modules with a group but no foreign source pass four arguments (name, timestamp, dependencies, group)
165 // to mw.loader.register()
166 $registrations[] = array( $name, $mtime, $deps, $group );
168 // Modules with a foreign source pass five arguments (name, timestamp, dependencies, group, source)
169 // to mw.loader.register()
170 $registrations[] = array( $name, $mtime, $deps, $group, $source );
173 $out .= ResourceLoader
::makeLoaderRegisterScript( $registrations );
175 wfProfileOut( __METHOD__
);
184 public function isRaw() {
189 * Get the load URL of the startup modules.
191 * This is a helper for getScript(), but can also be called standalone, such
192 * as when generating an AppCache manifest.
194 * @param $context ResourceLoaderContext
197 public static function getStartupModulesUrl( ResourceLoaderContext
$context ) {
199 $moduleNames = array( 'jquery', 'mediawiki' );
200 wfRunHooks( 'ResourceLoaderGetStartupModules', array( &$moduleNames ) );
202 // Get the latest version
203 $loader = $context->getResourceLoader();
205 foreach ( $moduleNames as $moduleName ) {
206 $version = max( $version,
207 $loader->getModule( $moduleName )->getModifiedTime( $context )
212 'modules' => ResourceLoader
::makePackedModulesString( $moduleNames ),
214 'lang' => $context->getLanguage(),
215 'skin' => $context->getSkin(),
216 'debug' => $context->getDebug() ?
'true' : 'false',
217 'version' => wfTimestamp( TS_ISO_8601_BASIC
, $version )
219 // Ensure uniform query order
221 return wfAppendQuery( wfScript( 'load' ), $query );
225 * @param $context ResourceLoaderContext
228 public function getScript( ResourceLoaderContext
$context ) {
229 global $IP, $wgLegacyJavaScriptGlobals;
231 $out = file_get_contents( "$IP/resources/startup.js" );
232 if ( $context->getOnly() === 'scripts' ) {
235 $configuration = $this->getConfig( $context );
236 $registrations = self
::getModuleRegistrations( $context );
237 $registrations = str_replace( "\n", "\n\t", trim( $registrations ) ); // fix indentation
238 $out .= "var startUp = function() {\n" .
239 "\tmw.config = new " . Xml
::encodeJsCall( 'mw.Map', array( $wgLegacyJavaScriptGlobals ) ) . "\n" .
240 "\t$registrations\n" .
241 "\t" . Xml
::encodeJsCall( 'mw.config.set', array( $configuration ) ) .
244 // Conditional script injection
245 $scriptTag = Html
::linkedScript( self
::getStartupModulesUrl( $context ) );
246 $out .= "if ( isCompatible() ) {\n" .
247 "\t" . Xml
::encodeJsCall( 'document.write', array( $scriptTag ) ) .
249 "delete isCompatible;";
258 public function supportsURLLoading() {
263 * @param $context ResourceLoaderContext
264 * @return array|mixed
266 public function getModifiedTime( ResourceLoaderContext
$context ) {
267 global $IP, $wgCacheEpoch;
269 $hash = $context->getHash();
270 if ( isset( $this->modifiedTime
[$hash] ) ) {
271 return $this->modifiedTime
[$hash];
274 // Call preloadModuleInfo() on ALL modules as we're about
275 // to call getModifiedTime() on all of them
276 $loader = $context->getResourceLoader();
277 $loader->preloadModuleInfo( $loader->getModuleNames(), $context );
279 $this->modifiedTime
[$hash] = filemtime( "$IP/resources/startup.js" );
280 // ATTENTION!: Because of the line above, this is not going to cause
281 // infinite recursion - think carefully before making changes to this
283 $time = wfTimestamp( TS_UNIX
, $wgCacheEpoch );
284 foreach ( $loader->getModuleNames() as $name ) {
285 $module = $loader->getModule( $name );
286 $time = max( $time, $module->getModifiedTime( $context ) );
288 $this->modifiedTime
[$hash] = $time;
289 return $this->modifiedTime
[$hash];
297 public function getGroup() {