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, $wgCookiePrefix;
42 // Pre-process information
43 $separatorTransTable = $wgContLang->separatorTransformTable();
44 $separatorTransTable = $separatorTransTable ?
$separatorTransTable : array();
45 $compactSeparatorTransTable = array(
46 implode( "\t", array_keys( $separatorTransTable ) ),
47 implode( "\t", $separatorTransTable ),
49 $digitTransTable = $wgContLang->digitTransformTable();
50 $digitTransTable = $digitTransTable ?
$digitTransTable : array();
51 $compactDigitTransTable = array(
52 implode( "\t", array_keys( $digitTransTable ) ),
53 implode( "\t", $digitTransTable ),
55 $mainPage = Title
::newMainPage();
57 // Build list of variables
59 'wgLoadScript' => $wgLoadScript,
60 'debug' => $context->getDebug(),
61 'skin' => $context->getSkin(),
62 'stylepath' => $wgStylePath,
63 'wgUrlProtocols' => wfUrlProtocols(),
64 'wgArticlePath' => $wgArticlePath,
65 'wgScriptPath' => $wgScriptPath,
66 'wgScriptExtension' => $wgScriptExtension,
67 'wgScript' => $wgScript,
68 'wgVariantArticlePath' => $wgVariantArticlePath,
69 'wgActionPaths' => $wgActionPaths,
70 'wgServer' => $wgServer,
71 'wgUserLanguage' => $context->getLanguage(),
72 'wgContentLanguage' => $wgContLang->getCode(),
73 'wgVersion' => $wgVersion,
74 'wgEnableAPI' => $wgEnableAPI,
75 'wgEnableWriteAPI' => $wgEnableWriteAPI,
76 'wgDefaultDateFormat' => $wgContLang->getDefaultDateFormat(),
77 'wgMonthNames' => $wgContLang->getMonthNamesArray(),
78 'wgMonthNamesShort' => $wgContLang->getMonthAbbreviationsArray(),
79 'wgSeparatorTransformTable' => $compactSeparatorTransTable,
80 'wgDigitTransformTable' => $compactDigitTransTable,
81 'wgMainPageTitle' => $mainPage ?
$mainPage->getPrefixedText() : null,
82 'wgFormattedNamespaces' => $wgContLang->getFormattedNamespaces(),
83 'wgNamespaceIds' => $wgContLang->getNamespaceIds(),
84 'wgSiteName' => $wgSitename,
85 'wgFileExtensions' => array_values( $wgFileExtensions ),
86 'wgDBname' => $wgDBname,
87 // This sucks, it is only needed on Special:Upload, but I could
88 // not find a way to add vars only for a certain module
89 'wgFileCanRotate' => BitmapHandler
::canRotate(),
90 'wgAvailableSkins' => Skin
::getSkinNames(),
91 'wgExtensionAssetsPath' => $wgExtensionAssetsPath,
92 'wgProto' => $wgProto,
93 // mediawiki sets cookies to have this prefix by default
94 'wgCookiePrefix' => $wgCookiePrefix,
96 if ( $wgUseAjax && $wgEnableMWSuggest ) {
97 $vars['wgMWSuggestTemplate'] = SearchEngine
::getMWSuggestTemplate();
100 wfRunHooks( 'ResourceLoaderGetConfigVars', array( &$vars ) );
106 * Gets registration code for all modules
108 * @param $context ResourceLoaderContext object
109 * @return String: JavaScript code for registering all modules with the client loader
111 public static function getModuleRegistrations( ResourceLoaderContext
$context ) {
112 global $wgCacheEpoch;
113 wfProfileIn( __METHOD__
);
116 $registrations = array();
117 $resourceLoader = $context->getResourceLoader();
118 foreach ( $resourceLoader->getModuleNames() as $name ) {
119 $module = $resourceLoader->getModule( $name );
120 // Support module loader scripts
121 $loader = $module->getLoaderScript();
122 if ( $loader !== false ) {
123 $deps = $module->getDependencies();
124 $group = $module->getGroup();
125 $version = wfTimestamp( TS_ISO_8601_BASIC
,
126 $module->getModifiedTime( $context ) );
127 $out .= ResourceLoader
::makeCustomLoaderScript( $name, $version, $deps, $group, $loader );
129 // Automatically register module
131 // getModifiedTime() is supposed to return a UNIX timestamp, but it doesn't always
132 // seem to do that, and custom implementations might forget. Coerce it to TS_UNIX
133 $moduleMtime = wfTimestamp( TS_UNIX
, $module->getModifiedTime( $context ) );
134 $mtime = max( $moduleMtime, wfTimestamp( TS_UNIX
, $wgCacheEpoch ) );
135 // Modules without dependencies or a group pass two arguments (name, timestamp) to
136 // mw.loader.register()
137 if ( !count( $module->getDependencies() && $module->getGroup() === null ) ) {
138 $registrations[] = array( $name, $mtime );
140 // Modules with dependencies but no group pass three arguments
141 // (name, timestamp, dependencies) to mw.loader.register()
142 else if ( $module->getGroup() === null ) {
143 $registrations[] = array(
144 $name, $mtime, $module->getDependencies() );
146 // Modules with dependencies pass four arguments (name, timestamp, dependencies, group)
147 // to mw.loader.register()
149 $registrations[] = array(
150 $name, $mtime, $module->getDependencies(), $module->getGroup() );
154 $out .= ResourceLoader
::makeLoaderRegisterScript( $registrations );
156 wfProfileOut( __METHOD__
);
162 public function getScript( ResourceLoaderContext
$context ) {
163 global $IP, $wgLoadScript;
165 $out = file_get_contents( "$IP/resources/startup.js" );
166 if ( $context->getOnly() === 'scripts' ) {
169 $modules = array( 'jquery', 'mediawiki' );
170 wfRunHooks( 'ResourceLoaderGetStartupModules', array( &$modules ) );
172 // Get the latest version
174 foreach ( $modules as $moduleName ) {
175 $version = max( $version,
176 $context->getResourceLoader()->getModule( $moduleName )->getModifiedTime( $context )
179 // Build load query for StartupModules
181 'modules' => implode( '|', $modules ),
183 'lang' => $context->getLanguage(),
184 'skin' => $context->getSkin(),
185 'debug' => $context->getDebug() ?
'true' : 'false',
186 'version' => wfTimestamp( TS_ISO_8601_BASIC
, $version )
188 // Ensure uniform query order
192 $configuration = $this->getConfig( $context );
193 $registrations = self
::getModuleRegistrations( $context );
194 $out .= "var startUp = function() {\n" .
195 "\t$registrations\n" .
196 "\t" . Xml
::encodeJsCall( 'mediaWiki.config.set', array( $configuration ) ) .
199 // Conditional script injection
200 $scriptTag = Html
::linkedScript( $wgLoadScript . '?' . wfArrayToCGI( $query ) );
201 $out .= "if ( isCompatible() ) {\n" .
202 "\t" . Xml
::encodeJsCall( 'document.write', array( $scriptTag ) ) .
204 "delete isCompatible;";
210 public function getModifiedTime( ResourceLoaderContext
$context ) {
211 global $IP, $wgCacheEpoch;
213 $hash = $context->getHash();
214 if ( isset( $this->modifiedTime
[$hash] ) ) {
215 return $this->modifiedTime
[$hash];
218 // Call preloadModuleInfo() on ALL modules as we're about
219 // to call getModifiedTime() on all of them
220 $loader = $context->getResourceLoader();
221 $loader->preloadModuleInfo( $loader->getModuleNames(), $context );
223 $this->modifiedTime
[$hash] = filemtime( "$IP/resources/startup.js" );
224 // ATTENTION!: Because of the line above, this is not going to cause
225 // infinite recursion - think carefully before making changes to this
227 $time = wfTimestamp( TS_UNIX
, $wgCacheEpoch );
228 foreach ( $loader->getModuleNames() as $name ) {
229 $module = $loader->getModule( $name );
230 $time = max( $time, $module->getModifiedTime( $context ) );
232 return $this->modifiedTime
[$hash] = $time;
237 public function getGroup() {