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, $wgProto,
41 $wgCookiePrefix, $wgResourceLoaderMaxQueryLength, $wgLegacyJavaScriptGlobals;
43 // Pre-process information
44 $separatorTransTable = $wgContLang->separatorTransformTable();
45 $separatorTransTable = $separatorTransTable ?
$separatorTransTable : array();
46 $compactSeparatorTransTable = array(
47 implode( "\t", array_keys( $separatorTransTable ) ),
48 implode( "\t", $separatorTransTable ),
50 $digitTransTable = $wgContLang->digitTransformTable();
51 $digitTransTable = $digitTransTable ?
$digitTransTable : array();
52 $compactDigitTransTable = array(
53 implode( "\t", array_keys( $digitTransTable ) ),
54 implode( "\t", $digitTransTable ),
56 $mainPage = Title
::newMainPage();
58 // Build wgNamespaceIds
59 // A complete key-value pair object mapping localized, canonical and aliases for namespaces
60 // to their numerical ids (case insensitive and with underscores)
61 $namespaceIds = $wgContLang->getNamespaceIds();
62 foreach( MWNamespace
::getCanonicalNamespaces() as $index => $name ) {
63 $nsIds[$wgContLang->lc( $name )] = $index;
66 // Build list of variables
68 'wgLoadScript' => $wgLoadScript,
69 'debug' => $context->getDebug(),
70 'skin' => $context->getSkin(),
71 'stylepath' => $wgStylePath,
72 'wgUrlProtocols' => wfUrlProtocols(),
73 'wgArticlePath' => $wgArticlePath,
74 'wgScriptPath' => $wgScriptPath,
75 'wgScriptExtension' => $wgScriptExtension,
76 'wgScript' => $wgScript,
77 'wgVariantArticlePath' => $wgVariantArticlePath,
78 'wgActionPaths' => $wgActionPaths,
79 'wgServer' => $wgServer,
80 'wgUserLanguage' => $context->getLanguage(),
81 'wgContentLanguage' => $wgContLang->getCode(),
82 'wgVersion' => $wgVersion,
83 'wgEnableAPI' => $wgEnableAPI,
84 'wgEnableWriteAPI' => $wgEnableWriteAPI,
85 'wgDefaultDateFormat' => $wgContLang->getDefaultDateFormat(),
86 'wgMonthNames' => $wgContLang->getMonthNamesArray(),
87 'wgMonthNamesShort' => $wgContLang->getMonthAbbreviationsArray(),
88 'wgSeparatorTransformTable' => $compactSeparatorTransTable,
89 'wgDigitTransformTable' => $compactDigitTransTable,
90 'wgMainPageTitle' => $mainPage ?
$mainPage->getPrefixedText() : null,
91 'wgFormattedNamespaces' => $wgContLang->getFormattedNamespaces(),
92 'wgNamespaceIds' => $namespaceIds,
93 'wgSiteName' => $wgSitename,
94 'wgFileExtensions' => array_values( $wgFileExtensions ),
95 'wgDBname' => $wgDBname,
96 // This sucks, it is only needed on Special:Upload, but I could
97 // not find a way to add vars only for a certain module
98 'wgFileCanRotate' => BitmapHandler
::canRotate(),
99 'wgAvailableSkins' => Skin
::getSkinNames(),
100 'wgExtensionAssetsPath' => $wgExtensionAssetsPath,
101 'wgProto' => $wgProto,
102 // MediaWiki sets cookies to have this prefix by default
103 'wgCookiePrefix' => $wgCookiePrefix,
104 'wgResourceLoaderMaxQueryLength' => $wgResourceLoaderMaxQueryLength,
105 'wgLegacyJavaScriptGlobals' => $wgLegacyJavaScriptGlobals,
107 if ( $wgUseAjax && $wgEnableMWSuggest ) {
108 $vars['wgMWSuggestTemplate'] = SearchEngine
::getMWSuggestTemplate();
111 wfRunHooks( 'ResourceLoaderGetConfigVars', array( &$vars ) );
117 * Gets registration code for all modules
119 * @param $context ResourceLoaderContext object
120 * @return String: JavaScript code for registering all modules with the client loader
122 public static function getModuleRegistrations( ResourceLoaderContext
$context ) {
123 global $wgCacheEpoch;
124 wfProfileIn( __METHOD__
);
127 $registrations = array();
128 $resourceLoader = $context->getResourceLoader();
129 foreach ( $resourceLoader->getModuleNames() as $name ) {
130 $module = $resourceLoader->getModule( $name );
131 // Support module loader scripts
132 $loader = $module->getLoaderScript();
133 if ( $loader !== false ) {
134 $deps = $module->getDependencies();
135 $group = $module->getGroup();
136 $version = wfTimestamp( TS_ISO_8601_BASIC
,
137 $module->getModifiedTime( $context ) );
138 $out .= ResourceLoader
::makeCustomLoaderScript( $name, $version, $deps, $group, $loader );
140 // Automatically register module
142 // getModifiedTime() is supposed to return a UNIX timestamp, but it doesn't always
143 // seem to do that, and custom implementations might forget. Coerce it to TS_UNIX
144 $moduleMtime = wfTimestamp( TS_UNIX
, $module->getModifiedTime( $context ) );
145 $mtime = max( $moduleMtime, wfTimestamp( TS_UNIX
, $wgCacheEpoch ) );
146 // Modules without dependencies or a group pass two arguments (name, timestamp) to
147 // mw.loader.register()
148 if ( !count( $module->getDependencies() && $module->getGroup() === null ) ) {
149 $registrations[] = array( $name, $mtime );
151 // Modules with dependencies but no group pass three arguments
152 // (name, timestamp, dependencies) to mw.loader.register()
153 else if ( $module->getGroup() === null ) {
154 $registrations[] = array(
155 $name, $mtime, $module->getDependencies() );
157 // Modules with dependencies pass four arguments (name, timestamp, dependencies, group)
158 // to mw.loader.register()
160 $registrations[] = array(
161 $name, $mtime, $module->getDependencies(), $module->getGroup() );
165 $out .= ResourceLoader
::makeLoaderRegisterScript( $registrations );
167 wfProfileOut( __METHOD__
);
174 * @param $context ResourceLoaderContext
177 public function getScript( ResourceLoaderContext
$context ) {
178 global $IP, $wgLoadScript, $wgLegacyJavaScriptGlobals;
180 $out = file_get_contents( "$IP/resources/startup.js" );
181 if ( $context->getOnly() === 'scripts' ) {
184 $modules = array( 'jquery', 'mediawiki' );
185 wfRunHooks( 'ResourceLoaderGetStartupModules', array( &$modules ) );
187 // Get the latest version
189 foreach ( $modules as $moduleName ) {
190 $version = max( $version,
191 $context->getResourceLoader()->getModule( $moduleName )->getModifiedTime( $context )
194 // Build load query for StartupModules
196 'modules' => ResourceLoader
::makePackedModulesString( $modules ),
198 'lang' => $context->getLanguage(),
199 'skin' => $context->getSkin(),
200 'debug' => $context->getDebug() ?
'true' : 'false',
201 'version' => wfTimestamp( TS_ISO_8601_BASIC
, $version )
203 // Ensure uniform query order
207 $configuration = $this->getConfig( $context );
208 $registrations = self
::getModuleRegistrations( $context );
209 $out .= "var startUp = function() {\n" .
210 "\tmw.config = new " . Xml
::encodeJsCall( 'mw.Map', array( $wgLegacyJavaScriptGlobals ) ) . "\n" .
211 "\t$registrations\n" .
212 "\t" . Xml
::encodeJsCall( 'mw.config.set', array( $configuration ) ) .
215 // Conditional script injection
216 $scriptTag = Html
::linkedScript( $wgLoadScript . '?' . wfArrayToCGI( $query ) );
217 $out .= "if ( isCompatible() ) {\n" .
218 "\t" . Xml
::encodeJsCall( 'document.write', array( $scriptTag ) ) .
220 "delete isCompatible;";
227 * @param $context ResourceLoaderContext
228 * @return array|mixed
230 public function getModifiedTime( ResourceLoaderContext
$context ) {
231 global $IP, $wgCacheEpoch;
233 $hash = $context->getHash();
234 if ( isset( $this->modifiedTime
[$hash] ) ) {
235 return $this->modifiedTime
[$hash];
238 // Call preloadModuleInfo() on ALL modules as we're about
239 // to call getModifiedTime() on all of them
240 $loader = $context->getResourceLoader();
241 $loader->preloadModuleInfo( $loader->getModuleNames(), $context );
243 $this->modifiedTime
[$hash] = filemtime( "$IP/resources/startup.js" );
244 // ATTENTION!: Because of the line above, this is not going to cause
245 // infinite recursion - think carefully before making changes to this
247 $time = wfTimestamp( TS_UNIX
, $wgCacheEpoch );
248 foreach ( $loader->getModuleNames() as $name ) {
249 $module = $loader->getModule( $name );
250 $time = max( $time, $module->getModifiedTime( $context ) );
252 return $this->modifiedTime
[$hash] = $time;
260 public function getGroup() {