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,
39 $wgEnableAPI, $wgEnableWriteAPI, $wgDBname, $wgEnableMWSuggest,
40 $wgSitename, $wgFileExtensions, $wgExtensionAssetsPath, $wgProto,
41 $wgCookiePrefix, $wgResourceLoaderMaxQueryLength;
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' => MW_VERSION
,
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,
98 if ( $wgUseAjax && $wgEnableMWSuggest ) {
99 $vars['wgMWSuggestTemplate'] = SearchEngine
::getMWSuggestTemplate();
102 wfRunHooks( 'ResourceLoaderGetConfigVars', array( &$vars ) );
108 * Gets registration code for all modules
110 * @param $context ResourceLoaderContext object
111 * @return String: JavaScript code for registering all modules with the client loader
113 public static function getModuleRegistrations( ResourceLoaderContext
$context ) {
114 global $wgCacheEpoch;
115 wfProfileIn( __METHOD__
);
118 $registrations = array();
119 $resourceLoader = $context->getResourceLoader();
120 foreach ( $resourceLoader->getModuleNames() as $name ) {
121 $module = $resourceLoader->getModule( $name );
122 // Support module loader scripts
123 $loader = $module->getLoaderScript();
124 if ( $loader !== false ) {
125 $deps = $module->getDependencies();
126 $group = $module->getGroup();
127 $version = wfTimestamp( TS_ISO_8601_BASIC
,
128 $module->getModifiedTime( $context ) );
129 $out .= ResourceLoader
::makeCustomLoaderScript( $name, $version, $deps, $group, $loader );
131 // Automatically register module
133 // getModifiedTime() is supposed to return a UNIX timestamp, but it doesn't always
134 // seem to do that, and custom implementations might forget. Coerce it to TS_UNIX
135 $moduleMtime = wfTimestamp( TS_UNIX
, $module->getModifiedTime( $context ) );
136 $mtime = max( $moduleMtime, wfTimestamp( TS_UNIX
, $wgCacheEpoch ) );
137 // Modules without dependencies or a group pass two arguments (name, timestamp) to
138 // mw.loader.register()
139 if ( !count( $module->getDependencies() && $module->getGroup() === null ) ) {
140 $registrations[] = array( $name, $mtime );
142 // Modules with dependencies but no group pass three arguments
143 // (name, timestamp, dependencies) to mw.loader.register()
144 else if ( $module->getGroup() === null ) {
145 $registrations[] = array(
146 $name, $mtime, $module->getDependencies() );
148 // Modules with dependencies pass four arguments (name, timestamp, dependencies, group)
149 // to mw.loader.register()
151 $registrations[] = array(
152 $name, $mtime, $module->getDependencies(), $module->getGroup() );
156 $out .= ResourceLoader
::makeLoaderRegisterScript( $registrations );
158 wfProfileOut( __METHOD__
);
164 public function getScript( ResourceLoaderContext
$context ) {
165 global $IP, $wgLoadScript;
167 $out = file_get_contents( "$IP/resources/startup.js" );
168 if ( $context->getOnly() === 'scripts' ) {
171 $modules = array( 'jquery', 'mediawiki' );
172 wfRunHooks( 'ResourceLoaderGetStartupModules', array( &$modules ) );
174 // Get the latest version
176 foreach ( $modules as $moduleName ) {
177 $version = max( $version,
178 $context->getResourceLoader()->getModule( $moduleName )->getModifiedTime( $context )
181 // Build load query for StartupModules
183 'modules' => implode( '|', $modules ),
185 'lang' => $context->getLanguage(),
186 'skin' => $context->getSkin(),
187 'debug' => $context->getDebug() ?
'true' : 'false',
188 'version' => wfTimestamp( TS_ISO_8601_BASIC
, $version )
190 // Ensure uniform query order
194 $configuration = $this->getConfig( $context );
195 $registrations = self
::getModuleRegistrations( $context );
196 $out .= "var startUp = function() {\n" .
197 "\t$registrations\n" .
198 "\t" . Xml
::encodeJsCall( 'mediaWiki.config.set', array( $configuration ) ) .
201 // Conditional script injection
202 $scriptTag = Html
::linkedScript( $wgLoadScript . '?' . wfArrayToCGI( $query ) );
203 $out .= "if ( isCompatible() ) {\n" .
204 "\t" . Xml
::encodeJsCall( 'document.write', array( $scriptTag ) ) .
206 "delete isCompatible;";
212 public function getModifiedTime( ResourceLoaderContext
$context ) {
213 global $IP, $wgCacheEpoch;
215 $hash = $context->getHash();
216 if ( isset( $this->modifiedTime
[$hash] ) ) {
217 return $this->modifiedTime
[$hash];
220 // Call preloadModuleInfo() on ALL modules as we're about
221 // to call getModifiedTime() on all of them
222 $loader = $context->getResourceLoader();
223 $loader->preloadModuleInfo( $loader->getModuleNames(), $context );
225 $this->modifiedTime
[$hash] = filemtime( "$IP/resources/startup.js" );
226 // ATTENTION!: Because of the line above, this is not going to cause
227 // infinite recursion - think carefully before making changes to this
229 $time = wfTimestamp( TS_UNIX
, $wgCacheEpoch );
230 foreach ( $loader->getModuleNames() as $name ) {
231 $module = $loader->getModule( $name );
232 $time = max( $time, $module->getModifiedTime( $context ) );
234 return $this->modifiedTime
[$hash] = $time;
239 public function getGroup() {