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;
47 $mainPage = Title
::newMainPage();
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.
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;
63 // Build list of variables
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,
104 wfRunHooks( 'ResourceLoaderGetConfigVars', array( &$vars ) );
110 * Gets registration code for all modules
112 * @param $context ResourceLoaderContext object
113 * @return String: JavaScript code for registering all modules with the client loader
115 public static function getModuleRegistrations( ResourceLoaderContext
$context ) {
116 global $wgCacheEpoch;
117 wfProfileIn( __METHOD__
);
120 $registrations = array();
121 $resourceLoader = $context->getResourceLoader();
122 $target = $context->getRequest()->getVal( 'target', 'desktop' );
125 $out .= ResourceLoader
::makeLoaderSourcesScript( $resourceLoader->getSources() );
128 foreach ( $resourceLoader->getModuleNames() as $name ) {
129 $module = $resourceLoader->getModule( $name );
130 $moduleTargets = $module->getTargets();
131 if ( !in_array( $target, $moduleTargets ) ) {
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 );
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 );
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 );
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 );
166 // Modules with a foreign source pass five arguments (name, timestamp, dependencies, group, source)
167 // to mw.loader.register()
169 $registrations[] = array( $name, $mtime, $deps, $group, $source );
172 $out .= ResourceLoader
::makeLoaderRegisterScript( $registrations );
174 wfProfileOut( __METHOD__
);
183 public function isRaw() {
188 * @param $context ResourceLoaderContext
191 public function getScript( ResourceLoaderContext
$context ) {
192 global $IP, $wgLegacyJavaScriptGlobals;
194 $out = file_get_contents( "$IP/resources/startup.js" );
195 if ( $context->getOnly() === 'scripts' ) {
198 $moduleNames = array( 'jquery', 'mediawiki' );
199 wfRunHooks( 'ResourceLoaderGetStartupModules', array( &$moduleNames ) );
201 // Get the latest version
202 $loader = $context->getResourceLoader();
204 foreach ( $moduleNames as $moduleName ) {
205 $version = max( $version,
206 $loader->getModule( $moduleName )->getModifiedTime( $context )
209 // Build load query for StartupModules
211 'modules' => ResourceLoader
::makePackedModulesString( $moduleNames ),
213 'lang' => $context->getLanguage(),
214 'skin' => $context->getSkin(),
215 'debug' => $context->getDebug() ?
'true' : 'false',
216 'version' => wfTimestamp( TS_ISO_8601_BASIC
, $version )
218 // Ensure uniform query order
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 ) ) .
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 ) ) .
236 "delete isCompatible;";
245 public function supportsURLLoading() {
250 * @param $context ResourceLoaderContext
251 * @return array|mixed
253 public function getModifiedTime( ResourceLoaderContext
$context ) {
254 global $IP, $wgCacheEpoch;
256 $hash = $context->getHash();
257 if ( isset( $this->modifiedTime
[$hash] ) ) {
258 return $this->modifiedTime
[$hash];
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 );
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
270 $time = wfTimestamp( TS_UNIX
, $wgCacheEpoch );
271 foreach ( $loader->getModuleNames() as $name ) {
272 $module = $loader->getModule( $name );
273 $time = max( $time, $module->getModifiedTime( $context ) );
275 return $this->modifiedTime
[$hash] = $time;
283 public function getGroup() {