3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
19 * @author Trevor Parscal
20 * @author Roan Kattouw
23 class ResourceLoaderStartUpModule
extends ResourceLoaderModule
{
25 /* Protected Members */
27 protected $modifiedTime = array();
29 /* Protected Methods */
32 * @param $context ResourceLoaderContext
35 protected function getConfig( $context ) {
36 global $wgLoadScript, $wgScript, $wgStylePath, $wgScriptExtension,
37 $wgArticlePath, $wgScriptPath, $wgServer, $wgContLang,
38 $wgVariantArticlePath, $wgActionPaths, $wgUseAjax, $wgVersion,
39 $wgEnableAPI, $wgEnableWriteAPI, $wgDBname, $wgEnableMWSuggest,
40 $wgSitename, $wgFileExtensions, $wgExtensionAssetsPath,
41 $wgCookiePrefix, $wgResourceLoaderMaxQueryLength;
43 $mainPage = Title
::newMainPage();
46 * Namespace related preparation
47 * - wgNamespaceIds: Key-value pairs of all localized, canonical and aliases for namespaces.
48 * - wgCaseSensitiveNamespaces: Array of namespaces that are case-sensitive.
50 $namespaceIds = $wgContLang->getNamespaceIds();
51 $caseSensitiveNamespaces = array();
52 foreach( MWNamespace
::getCanonicalNamespaces() as $index => $name ) {
53 $namespaceIds[$wgContLang->lc( $name )] = $index;
54 if ( !MWNamespace
::isCapitalized( $index ) ) {
55 $caseSensitiveNamespaces[] = $index;
59 // Build list of variables
61 'wgLoadScript' => $wgLoadScript,
62 'debug' => $context->getDebug(),
63 'skin' => $context->getSkin(),
64 'stylepath' => $wgStylePath,
65 'wgUrlProtocols' => wfUrlProtocols(),
66 'wgArticlePath' => $wgArticlePath,
67 'wgScriptPath' => $wgScriptPath,
68 'wgScriptExtension' => $wgScriptExtension,
69 'wgScript' => $wgScript,
70 'wgVariantArticlePath' => $wgVariantArticlePath,
71 // Force object to avoid "empty" associative array from
72 // becoming [] instead of {} in JS (bug 34604)
73 'wgActionPaths' => (object)$wgActionPaths,
74 'wgServer' => $wgServer,
75 'wgUserLanguage' => $context->getLanguage(),
76 'wgContentLanguage' => $wgContLang->getCode(),
77 'wgVersion' => $wgVersion,
78 'wgEnableAPI' => $wgEnableAPI,
79 'wgEnableWriteAPI' => $wgEnableWriteAPI,
80 'wgDefaultDateFormat' => $wgContLang->getDefaultDateFormat(),
81 'wgMonthNames' => $wgContLang->getMonthNamesArray(),
82 'wgMonthNamesShort' => $wgContLang->getMonthAbbreviationsArray(),
83 'wgMainPageTitle' => $mainPage->getPrefixedText(),
84 'wgFormattedNamespaces' => $wgContLang->getFormattedNamespaces(),
85 'wgNamespaceIds' => $namespaceIds,
86 'wgSiteName' => $wgSitename,
87 'wgFileExtensions' => array_values( $wgFileExtensions ),
88 'wgDBname' => $wgDBname,
89 // This sucks, it is only needed on Special:Upload, but I could
90 // not find a way to add vars only for a certain module
91 'wgFileCanRotate' => BitmapHandler
::canRotate(),
92 'wgAvailableSkins' => Skin
::getSkinNames(),
93 'wgExtensionAssetsPath' => $wgExtensionAssetsPath,
94 // MediaWiki sets cookies to have this prefix by default
95 'wgCookiePrefix' => $wgCookiePrefix,
96 'wgResourceLoaderMaxQueryLength' => $wgResourceLoaderMaxQueryLength,
97 'wgCaseSensitiveNamespaces' => $caseSensitiveNamespaces,
99 if ( $wgUseAjax && $wgEnableMWSuggest ) {
100 $vars['wgMWSuggestTemplate'] = SearchEngine
::getMWSuggestTemplate();
103 wfRunHooks( 'ResourceLoaderGetConfigVars', array( &$vars ) );
109 * Gets registration code for all modules
111 * @param $context ResourceLoaderContext object
112 * @return String: JavaScript code for registering all modules with the client loader
114 public static function getModuleRegistrations( ResourceLoaderContext
$context ) {
115 global $wgCacheEpoch;
116 wfProfileIn( __METHOD__
);
119 $registrations = array();
120 $resourceLoader = $context->getResourceLoader();
123 $out .= ResourceLoader
::makeLoaderSourcesScript( $resourceLoader->getSources() );
126 foreach ( $resourceLoader->getModuleNames() as $name ) {
127 $module = $resourceLoader->getModule( $name );
128 $deps = $module->getDependencies();
129 $group = $module->getGroup();
130 $source = $module->getSource();
131 // Support module loader scripts
132 $loader = $module->getLoaderScript();
133 if ( $loader !== false ) {
134 $version = wfTimestamp( TS_ISO_8601_BASIC
,
135 $module->getModifiedTime( $context ) );
136 $out .= ResourceLoader
::makeCustomLoaderScript( $name, $version, $deps, $group, $source, $loader );
138 // Automatically register module
140 // getModifiedTime() is supposed to return a UNIX timestamp, but it doesn't always
141 // seem to do that, and custom implementations might forget. Coerce it to TS_UNIX
142 $moduleMtime = wfTimestamp( TS_UNIX
, $module->getModifiedTime( $context ) );
143 $mtime = max( $moduleMtime, wfTimestamp( TS_UNIX
, $wgCacheEpoch ) );
144 // Modules without dependencies, a group or a foreign source pass two arguments (name, timestamp) to
145 // mw.loader.register()
146 if ( !count( $deps ) && $group === null && $source === 'local' ) {
147 $registrations[] = array( $name, $mtime );
149 // Modules with dependencies but no group or foreign source pass three arguments
150 // (name, timestamp, dependencies) to mw.loader.register()
151 elseif ( $group === null && $source === 'local' ) {
152 $registrations[] = array( $name, $mtime, $deps );
154 // Modules with a group but no foreign source pass four arguments (name, timestamp, dependencies, group)
155 // to mw.loader.register()
156 elseif ( $source === 'local' ) {
157 $registrations[] = array( $name, $mtime, $deps, $group );
159 // Modules with a foreign source pass five arguments (name, timestamp, dependencies, group, source)
160 // to mw.loader.register()
162 $registrations[] = array( $name, $mtime, $deps, $group, $source );
166 $out .= ResourceLoader
::makeLoaderRegisterScript( $registrations );
168 wfProfileOut( __METHOD__
);
175 * @param $context ResourceLoaderContext
178 public function getScript( ResourceLoaderContext
$context ) {
179 global $IP, $wgLoadScript, $wgLegacyJavaScriptGlobals;
181 $out = file_get_contents( "$IP/resources/startup.js" );
182 if ( $context->getOnly() === 'scripts' ) {
185 $moduleNames = array( 'jquery', 'mediawiki' );
186 wfRunHooks( 'ResourceLoaderGetStartupModules', array( &$moduleNames ) );
188 // Get the latest version
189 $loader = $context->getResourceLoader();
191 foreach ( $moduleNames as $moduleName ) {
192 $version = max( $version,
193 $loader->getModule( $moduleName )->getModifiedTime( $context )
196 // Build load query for StartupModules
198 'modules' => ResourceLoader
::makePackedModulesString( $moduleNames ),
200 'lang' => $context->getLanguage(),
201 'skin' => $context->getSkin(),
202 'debug' => $context->getDebug() ?
'true' : 'false',
203 'version' => wfTimestamp( TS_ISO_8601_BASIC
, $version )
205 // Ensure uniform query order
209 $configuration = $this->getConfig( $context );
210 $registrations = self
::getModuleRegistrations( $context );
211 $registrations = str_replace( "\n", "\n\t", trim( $registrations ) ); // fix indentation
212 $out .= "var startUp = function() {\n" .
213 "\tmw.config = new " . Xml
::encodeJsCall( 'mw.Map', array( $wgLegacyJavaScriptGlobals ) ) . "\n" .
214 "\t$registrations\n" .
215 "\t" . Xml
::encodeJsCall( 'mw.config.set', array( $configuration ) ) .
218 // Conditional script injection
219 $scriptTag = Html
::linkedScript( $wgLoadScript . '?' . wfArrayToCGI( $query ) );
220 $out .= "if ( isCompatible() ) {\n" .
221 "\t" . Xml
::encodeJsCall( 'document.write', array( $scriptTag ) ) .
223 "delete isCompatible;";
232 public function supportsURLLoading() {
237 * @param $context ResourceLoaderContext
238 * @return array|mixed
240 public function getModifiedTime( ResourceLoaderContext
$context ) {
241 global $IP, $wgCacheEpoch;
243 $hash = $context->getHash();
244 if ( isset( $this->modifiedTime
[$hash] ) ) {
245 return $this->modifiedTime
[$hash];
248 // Call preloadModuleInfo() on ALL modules as we're about
249 // to call getModifiedTime() on all of them
250 $loader = $context->getResourceLoader();
251 $loader->preloadModuleInfo( $loader->getModuleNames(), $context );
253 $this->modifiedTime
[$hash] = filemtime( "$IP/resources/startup.js" );
254 // ATTENTION!: Because of the line above, this is not going to cause
255 // infinite recursion - think carefully before making changes to this
257 $time = wfTimestamp( TS_UNIX
, $wgCacheEpoch );
258 foreach ( $loader->getModuleNames() as $name ) {
259 $module = $loader->getModule( $name );
260 $time = max( $time, $module->getModifiedTime( $context ) );
262 return $this->modifiedTime
[$hash] = $time;
270 public function getGroup() {