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 list of variables
60 'wgLoadScript' => $wgLoadScript,
61 'debug' => $context->getDebug(),
62 'skin' => $context->getSkin(),
63 'stylepath' => $wgStylePath,
64 'wgUrlProtocols' => wfUrlProtocols(),
65 'wgArticlePath' => $wgArticlePath,
66 'wgScriptPath' => $wgScriptPath,
67 'wgScriptExtension' => $wgScriptExtension,
68 'wgScript' => $wgScript,
69 'wgVariantArticlePath' => $wgVariantArticlePath,
70 'wgActionPaths' => $wgActionPaths,
71 'wgServer' => $wgServer,
72 'wgUserLanguage' => $context->getLanguage(),
73 'wgContentLanguage' => $wgContLang->getCode(),
74 'wgVersion' => $wgVersion,
75 'wgEnableAPI' => $wgEnableAPI,
76 'wgEnableWriteAPI' => $wgEnableWriteAPI,
77 'wgDefaultDateFormat' => $wgContLang->getDefaultDateFormat(),
78 'wgMonthNames' => $wgContLang->getMonthNamesArray(),
79 'wgMonthNamesShort' => $wgContLang->getMonthAbbreviationsArray(),
80 'wgSeparatorTransformTable' => $compactSeparatorTransTable,
81 'wgDigitTransformTable' => $compactDigitTransTable,
82 'wgMainPageTitle' => $mainPage ?
$mainPage->getPrefixedText() : null,
83 'wgFormattedNamespaces' => $wgContLang->getFormattedNamespaces(),
84 'wgNamespaceIds' => $wgContLang->getNamespaceIds(),
85 'wgSiteName' => $wgSitename,
86 'wgFileExtensions' => array_values( $wgFileExtensions ),
87 'wgDBname' => $wgDBname,
88 // This sucks, it is only needed on Special:Upload, but I could
89 // not find a way to add vars only for a certain module
90 'wgFileCanRotate' => BitmapHandler
::canRotate(),
91 'wgAvailableSkins' => Skin
::getSkinNames(),
92 'wgExtensionAssetsPath' => $wgExtensionAssetsPath,
93 'wgProto' => $wgProto,
94 // MediaWiki sets cookies to have this prefix by default
95 'wgCookiePrefix' => $wgCookiePrefix,
96 'wgResourceLoaderMaxQueryLength' => $wgResourceLoaderMaxQueryLength,
97 'wgLegacyJavaScriptGlobals' => $wgLegacyJavaScriptGlobals,
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();
121 foreach ( $resourceLoader->getModuleNames() as $name ) {
122 $module = $resourceLoader->getModule( $name );
123 // Support module loader scripts
124 $loader = $module->getLoaderScript();
125 if ( $loader !== false ) {
126 $deps = $module->getDependencies();
127 $group = $module->getGroup();
128 $version = wfTimestamp( TS_ISO_8601_BASIC
,
129 $module->getModifiedTime( $context ) );
130 $out .= ResourceLoader
::makeCustomLoaderScript( $name, $version, $deps, $group, $loader );
132 // Automatically register module
134 // getModifiedTime() is supposed to return a UNIX timestamp, but it doesn't always
135 // seem to do that, and custom implementations might forget. Coerce it to TS_UNIX
136 $moduleMtime = wfTimestamp( TS_UNIX
, $module->getModifiedTime( $context ) );
137 $mtime = max( $moduleMtime, wfTimestamp( TS_UNIX
, $wgCacheEpoch ) );
138 // Modules without dependencies or a group pass two arguments (name, timestamp) to
139 // mw.loader.register()
140 if ( !count( $module->getDependencies() && $module->getGroup() === null ) ) {
141 $registrations[] = array( $name, $mtime );
143 // Modules with dependencies but no group pass three arguments
144 // (name, timestamp, dependencies) to mw.loader.register()
145 else if ( $module->getGroup() === null ) {
146 $registrations[] = array(
147 $name, $mtime, $module->getDependencies() );
149 // Modules with dependencies pass four arguments (name, timestamp, dependencies, group)
150 // to mw.loader.register()
152 $registrations[] = array(
153 $name, $mtime, $module->getDependencies(), $module->getGroup() );
157 $out .= ResourceLoader
::makeLoaderRegisterScript( $registrations );
159 wfProfileOut( __METHOD__
);
165 public function getScript( ResourceLoaderContext
$context ) {
166 global $IP, $wgLoadScript, $wgLegacyJavaScriptGlobals;
168 $out = file_get_contents( "$IP/resources/startup.js" );
169 if ( $context->getOnly() === 'scripts' ) {
172 $modules = array( 'jquery', 'mediawiki' );
173 wfRunHooks( 'ResourceLoaderGetStartupModules', array( &$modules ) );
175 // Get the latest version
177 foreach ( $modules as $moduleName ) {
178 $version = max( $version,
179 $context->getResourceLoader()->getModule( $moduleName )->getModifiedTime( $context )
182 // Build load query for StartupModules
184 'modules' => implode( '|', $modules ),
186 'lang' => $context->getLanguage(),
187 'skin' => $context->getSkin(),
188 'debug' => $context->getDebug() ?
'true' : 'false',
189 'version' => wfTimestamp( TS_ISO_8601_BASIC
, $version )
191 // Ensure uniform query order
195 $configuration = $this->getConfig( $context );
196 $registrations = self
::getModuleRegistrations( $context );
197 $out .= "var startUp = function() {\n" .
198 "\tmw.config = new " . Xml
::encodeJsCall( 'mw.Map', array( $wgLegacyJavaScriptGlobals ) ) . "\n" .
199 "\t$registrations\n" .
200 "\t" . Xml
::encodeJsCall( 'mw.config.set', array( $configuration ) ) .
203 // Conditional script injection
204 $scriptTag = Html
::linkedScript( $wgLoadScript . '?' . wfArrayToCGI( $query ) );
205 $out .= "if ( isCompatible() ) {\n" .
206 "\t" . Xml
::encodeJsCall( 'document.write', array( $scriptTag ) ) .
208 "delete isCompatible;";
214 public function getModifiedTime( ResourceLoaderContext
$context ) {
215 global $IP, $wgCacheEpoch;
217 $hash = $context->getHash();
218 if ( isset( $this->modifiedTime
[$hash] ) ) {
219 return $this->modifiedTime
[$hash];
222 // Call preloadModuleInfo() on ALL modules as we're about
223 // to call getModifiedTime() on all of them
224 $loader = $context->getResourceLoader();
225 $loader->preloadModuleInfo( $loader->getModuleNames(), $context );
227 $this->modifiedTime
[$hash] = filemtime( "$IP/resources/startup.js" );
228 // ATTENTION!: Because of the line above, this is not going to cause
229 // infinite recursion - think carefully before making changes to this
231 $time = wfTimestamp( TS_UNIX
, $wgCacheEpoch );
232 foreach ( $loader->getModuleNames() as $name ) {
233 $module = $loader->getModule( $name );
234 $time = max( $time, $module->getModifiedTime( $context ) );
236 return $this->modifiedTime
[$hash] = $time;
241 public function getGroup() {