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, $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();
59 * Namespace related preparation
60 * - wgNamespaceIds: Key-value pairs of all localized, canonical and aliases for namespaces.
61 * - wgCaseSensitiveNamespaces: Array of namespaces that are case-sensitive.
63 $namespaceIds = $wgContLang->getNamespaceIds();
64 $caseSensitiveNamespaces = array();
65 foreach( MWNamespace
::getCanonicalNamespaces() as $index => $name ) {
66 $namespaceIds[$wgContLang->lc( $name )] = $index;
67 if ( !MWNamespace
::isCapitalized( $index ) ) {
68 $caseSensitiveNamespaces[] = $index;
72 // Build list of variables
74 'wgLoadScript' => $wgLoadScript,
75 'debug' => $context->getDebug(),
76 'skin' => $context->getSkin(),
77 'stylepath' => $wgStylePath,
78 'wgUrlProtocols' => wfUrlProtocols(),
79 'wgArticlePath' => $wgArticlePath,
80 'wgScriptPath' => $wgScriptPath,
81 'wgScriptExtension' => $wgScriptExtension,
82 'wgScript' => $wgScript,
83 'wgVariantArticlePath' => $wgVariantArticlePath,
84 'wgActionPaths' => $wgActionPaths,
85 'wgServer' => $wgServer,
86 'wgUserLanguage' => $context->getLanguage(),
87 'wgContentLanguage' => $wgContLang->getCode(),
88 'wgVersion' => $wgVersion,
89 'wgEnableAPI' => $wgEnableAPI,
90 'wgEnableWriteAPI' => $wgEnableWriteAPI,
91 'wgDefaultDateFormat' => $wgContLang->getDefaultDateFormat(),
92 'wgMonthNames' => $wgContLang->getMonthNamesArray(),
93 'wgMonthNamesShort' => $wgContLang->getMonthAbbreviationsArray(),
94 'wgSeparatorTransformTable' => $compactSeparatorTransTable,
95 'wgDigitTransformTable' => $compactDigitTransTable,
96 'wgMainPageTitle' => $mainPage ?
$mainPage->getPrefixedText() : null,
97 'wgFormattedNamespaces' => $wgContLang->getFormattedNamespaces(),
98 'wgNamespaceIds' => $namespaceIds,
99 'wgSiteName' => $wgSitename,
100 'wgFileExtensions' => array_values( $wgFileExtensions ),
101 'wgDBname' => $wgDBname,
102 // This sucks, it is only needed on Special:Upload, but I could
103 // not find a way to add vars only for a certain module
104 'wgFileCanRotate' => BitmapHandler
::canRotate(),
105 'wgAvailableSkins' => Skin
::getSkinNames(),
106 'wgExtensionAssetsPath' => $wgExtensionAssetsPath,
107 // MediaWiki sets cookies to have this prefix by default
108 'wgCookiePrefix' => $wgCookiePrefix,
109 'wgResourceLoaderMaxQueryLength' => $wgResourceLoaderMaxQueryLength,
110 'wgLegacyJavaScriptGlobals' => $wgLegacyJavaScriptGlobals,
111 'wgCaseSensitiveNamespaces' => $caseSensitiveNamespaces,
113 if ( $wgUseAjax && $wgEnableMWSuggest ) {
114 $vars['wgMWSuggestTemplate'] = SearchEngine
::getMWSuggestTemplate();
117 wfRunHooks( 'ResourceLoaderGetConfigVars', array( &$vars ) );
123 * Gets registration code for all modules
125 * @param $context ResourceLoaderContext object
126 * @return String: JavaScript code for registering all modules with the client loader
128 public static function getModuleRegistrations( ResourceLoaderContext
$context ) {
129 global $wgCacheEpoch;
130 wfProfileIn( __METHOD__
);
133 $registrations = array();
134 $resourceLoader = $context->getResourceLoader();
137 $out .= ResourceLoader
::makeLoaderSourcesScript( $resourceLoader->getSources() );
140 foreach ( $resourceLoader->getModuleNames() as $name ) {
141 $module = $resourceLoader->getModule( $name );
142 // Support module loader scripts
143 $loader = $module->getLoaderScript();
144 if ( $loader !== false ) {
145 $deps = $module->getDependencies();
146 $group = $module->getGroup();
147 $source = $module->getSource();
148 $version = wfTimestamp( TS_ISO_8601_BASIC
,
149 $module->getModifiedTime( $context ) );
150 $out .= ResourceLoader
::makeCustomLoaderScript( $name, $version, $deps, $group, $source, $loader );
152 // Automatically register module
154 // getModifiedTime() is supposed to return a UNIX timestamp, but it doesn't always
155 // seem to do that, and custom implementations might forget. Coerce it to TS_UNIX
156 $moduleMtime = wfTimestamp( TS_UNIX
, $module->getModifiedTime( $context ) );
157 $mtime = max( $moduleMtime, wfTimestamp( TS_UNIX
, $wgCacheEpoch ) );
158 // Modules without dependencies, a group or a foreign source pass two arguments (name, timestamp) to
159 // mw.loader.register()
160 if ( !count( $module->getDependencies() && $module->getGroup() === null && $module->getSource() === 'local' ) ) {
161 $registrations[] = array( $name, $mtime );
163 // Modules with dependencies but no group or foreign source pass three arguments
164 // (name, timestamp, dependencies) to mw.loader.register()
165 elseif ( $module->getGroup() === null && $module->getSource() === 'local' ) {
166 $registrations[] = array(
167 $name, $mtime, $module->getDependencies() );
169 // Modules with a group but no foreign source pass four arguments (name, timestamp, dependencies, group)
170 // to mw.loader.register()
171 else if ( $module->getSource() === 'local' ) {
172 $registrations[] = array(
173 $name, $mtime, $module->getDependencies(), $module->getGroup() );
175 // Modules with a foreign source pass five arguments (name, timestamp, dependencies, group, source)
176 // to mw.loader.register()
178 $registrations[] = array(
179 $name, $mtime, $module->getDependencies(), $module->getGroup(), $module->getSource() );
183 $out .= ResourceLoader
::makeLoaderRegisterScript( $registrations );
185 wfProfileOut( __METHOD__
);
192 * @param $context ResourceLoaderContext
195 public function getScript( ResourceLoaderContext
$context ) {
196 global $IP, $wgLoadScript, $wgLegacyJavaScriptGlobals;
198 $out = file_get_contents( "$IP/resources/startup.js" );
199 if ( $context->getOnly() === 'scripts' ) {
202 $modules = array( 'jquery', 'mediawiki' );
203 wfRunHooks( 'ResourceLoaderGetStartupModules', array( &$modules ) );
205 // Get the latest version
207 foreach ( $modules as $moduleName ) {
208 $version = max( $version,
209 $context->getResourceLoader()->getModule( $moduleName )->getModifiedTime( $context )
212 // Build load query for StartupModules
214 'modules' => ResourceLoader
::makePackedModulesString( $modules ),
216 'lang' => $context->getLanguage(),
217 'skin' => $context->getSkin(),
218 'debug' => $context->getDebug() ?
'true' : 'false',
219 'version' => wfTimestamp( TS_ISO_8601_BASIC
, $version )
221 // Ensure uniform query order
225 $configuration = $this->getConfig( $context );
226 $registrations = self
::getModuleRegistrations( $context );
227 $out .= "var startUp = function() {\n" .
228 "\tmw.config = new " . Xml
::encodeJsCall( 'mw.Map', array( $wgLegacyJavaScriptGlobals ) ) . "\n" .
229 "\t$registrations\n" .
230 "\t" . Xml
::encodeJsCall( 'mw.config.set', array( $configuration ) ) .
233 // Conditional script injection
234 $scriptTag = Html
::linkedScript( $wgLoadScript . '?' . wfArrayToCGI( $query ) );
235 $out .= "if ( isCompatible() ) {\n" .
236 "\t" . Xml
::encodeJsCall( 'document.write', array( $scriptTag ) ) .
238 "delete isCompatible;";
245 * @param $context ResourceLoaderContext
246 * @return array|mixed
248 public function getModifiedTime( ResourceLoaderContext
$context ) {
249 global $IP, $wgCacheEpoch;
251 $hash = $context->getHash();
252 if ( isset( $this->modifiedTime
[$hash] ) ) {
253 return $this->modifiedTime
[$hash];
256 // Call preloadModuleInfo() on ALL modules as we're about
257 // to call getModifiedTime() on all of them
258 $loader = $context->getResourceLoader();
259 $loader->preloadModuleInfo( $loader->getModuleNames(), $context );
261 $this->modifiedTime
[$hash] = filemtime( "$IP/resources/startup.js" );
262 // ATTENTION!: Because of the line above, this is not going to cause
263 // infinite recursion - think carefully before making changes to this
265 $time = wfTimestamp( TS_UNIX
, $wgCacheEpoch );
266 foreach ( $loader->getModuleNames() as $name ) {
267 $module = $loader->getModule( $name );
268 $time = max( $time, $module->getModifiedTime( $context ) );
270 return $this->modifiedTime
[$hash] = $time;
278 public function getGroup() {