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;
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 'wgSeparatorTransformTable' => $compactSeparatorTransTable,
77 'wgDigitTransformTable' => $compactDigitTransTable,
78 'wgMainPageTitle' => $mainPage ?
$mainPage->getPrefixedText() : null,
79 'wgFormattedNamespaces' => $wgContLang->getFormattedNamespaces(),
80 'wgNamespaceIds' => $wgContLang->getNamespaceIds(),
81 'wgSiteName' => $wgSitename,
82 'wgFileExtensions' => array_values( $wgFileExtensions ),
83 'wgDBname' => $wgDBname,
84 // This sucks, it is only needed on Special:Upload, but I could
85 // not find a way to add vars only for a certain module
86 'wgFileCanRotate' => BitmapHandler
::canRotate(),
87 'wgAvailableSkins' => Skin
::getSkinNames(),
88 'wgExtensionAssetsPath' => $wgExtensionAssetsPath,
90 if ( $wgUseAjax && $wgEnableMWSuggest ) {
91 $vars['wgMWSuggestTemplate'] = SearchEngine
::getMWSuggestTemplate();
94 wfRunHooks( 'ResourceLoaderGetConfigVars', array( &$vars ) );
100 * Gets registration code for all modules
102 * @param $context ResourceLoaderContext object
103 * @return String: JavaScript code for registering all modules with the client loader
105 public static function getModuleRegistrations( ResourceLoaderContext
$context ) {
106 global $wgCacheEpoch;
107 wfProfileIn( __METHOD__
);
110 $registrations = array();
111 $resourceLoader = $context->getResourceLoader();
112 foreach ( $resourceLoader->getModuleNames() as $name ) {
113 $module = $resourceLoader->getModule( $name );
114 // Support module loader scripts
115 $loader = $module->getLoaderScript();
116 if ( $loader !== false ) {
117 $deps = $module->getDependencies();
118 $group = $module->getGroup();
119 $version = wfTimestamp( TS_ISO_8601_BASIC
,
120 round( $module->getModifiedTime( $context ), -2 ) );
121 $out .= ResourceLoader
::makeCustomLoaderScript( $name, $version, $deps, $group, $loader );
123 // Automatically register module
125 // getModifiedTime() is supposed to return a UNIX timestamp, but it doesn't always
126 // seem to do that, and custom implementations might forget. Coerce it to TS_UNIX
127 $moduleMtime = wfTimestamp( TS_UNIX
, $module->getModifiedTime( $context ) );
128 $mtime = max( $moduleMtime, wfTimestamp( TS_UNIX
, $wgCacheEpoch ) );
129 // Modules without dependencies or a group pass two arguments (name, timestamp) to
130 // mediaWiki.loader.register()
131 if ( !count( $module->getDependencies() && $module->getGroup() === null ) ) {
132 $registrations[] = array( $name, $mtime );
134 // Modules with dependencies but no group pass three arguments
135 // (name, timestamp, dependencies) to mediaWiki.loader.register()
136 else if ( $module->getGroup() === null ) {
137 $registrations[] = array(
138 $name, $mtime, $module->getDependencies() );
140 // Modules with dependencies pass four arguments (name, timestamp, dependencies, group)
141 // to mediaWiki.loader.register()
143 $registrations[] = array(
144 $name, $mtime, $module->getDependencies(), $module->getGroup() );
148 $out .= ResourceLoader
::makeLoaderRegisterScript( $registrations );
150 wfProfileOut( __METHOD__
);
156 public function getScript( ResourceLoaderContext
$context ) {
157 global $IP, $wgLoadScript;
159 $out = file_get_contents( "$IP/resources/startup.js" );
160 if ( $context->getOnly() === 'scripts' ) {
161 // Build load query for jquery and mediawiki modules
163 'modules' => implode( '|', array( 'jquery', 'mediawiki' ) ),
165 'lang' => $context->getLanguage(),
166 'skin' => $context->getSkin(),
167 'debug' => $context->getDebug() ?
'true' : 'false',
168 'version' => wfTimestamp( TS_ISO_8601_BASIC
, round( max(
169 $context->getResourceLoader()->getModule( 'jquery' )->getModifiedTime( $context ),
170 $context->getResourceLoader()->getModule( 'mediawiki' )->getModifiedTime( $context )
173 // Ensure uniform query order
177 $configuration = $this->getConfig( $context );
178 $registrations = self
::getModuleRegistrations( $context );
179 $out .= "var startUp = function() {\n" .
180 "\t$registrations\n" .
181 "\t" . Xml
::encodeJsCall( 'mediaWiki.config.set', array( $configuration ) ) .
184 // Conditional script injection
185 $scriptTag = Html
::linkedScript( $wgLoadScript . '?' . wfArrayToCGI( $query ) );
186 $out .= "if ( isCompatible() ) {\n" .
187 "\t" . Xml
::encodeJsCall( 'document.write', array( $scriptTag ) ) .
189 "delete isCompatible;";
195 public function getModifiedTime( ResourceLoaderContext
$context ) {
196 global $IP, $wgCacheEpoch;
198 $hash = $context->getHash();
199 if ( isset( $this->modifiedTime
[$hash] ) ) {
200 return $this->modifiedTime
[$hash];
203 // Call preloadModuleInfo() on ALL modules as we're about
204 // to call getModifiedTime() on all of them
205 $loader = $context->getResourceLoader();
206 $loader->preloadModuleInfo( $loader->getModuleNames(), $context );
208 $this->modifiedTime
[$hash] = filemtime( "$IP/resources/startup.js" );
209 // ATTENTION!: Because of the line above, this is not going to cause
210 // infinite recursion - think carefully before making changes to this
212 $time = wfTimestamp( TS_UNIX
, $wgCacheEpoch );
213 foreach ( $loader->getModuleNames() as $name ) {
214 $module = $loader->getModule( $name );
215 $time = max( $time, $module->getModifiedTime( $context ) );
217 return $this->modifiedTime
[$hash] = $time;
221 * @param $context ResourceLoaderContext
224 public function getFlip( $context ) {
227 return $wgContLang->getDir() !== $context->getDirection();
232 public function getGroup() {