* Implement MW_VERSION constant in Defines.php and use it in preference to $wgVersion...
[lhc/web/wiklou.git] / includes / resourceloader / ResourceLoaderStartUpModule.php
1 <?php
2 /**
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.
7 *
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.
12 *
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
17 *
18 * @file
19 * @author Trevor Parscal
20 * @author Roan Kattouw
21 */
22
23 class ResourceLoaderStartUpModule extends ResourceLoaderModule {
24
25 /* Protected Members */
26
27 protected $modifiedTime = array();
28
29 /* Protected Methods */
30
31 /**
32 * @param $context ResourceLoaderContext
33 * @return array
34 */
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;
42
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 ),
49 );
50 $digitTransTable = $wgContLang->digitTransformTable();
51 $digitTransTable = $digitTransTable ? $digitTransTable : array();
52 $compactDigitTransTable = array(
53 implode( "\t", array_keys( $digitTransTable ) ),
54 implode( "\t", $digitTransTable ),
55 );
56 $mainPage = Title::newMainPage();
57
58 // Build list of variables
59 $vars = array(
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,
97 );
98 if ( $wgUseAjax && $wgEnableMWSuggest ) {
99 $vars['wgMWSuggestTemplate'] = SearchEngine::getMWSuggestTemplate();
100 }
101
102 wfRunHooks( 'ResourceLoaderGetConfigVars', array( &$vars ) );
103
104 return $vars;
105 }
106
107 /**
108 * Gets registration code for all modules
109 *
110 * @param $context ResourceLoaderContext object
111 * @return String: JavaScript code for registering all modules with the client loader
112 */
113 public static function getModuleRegistrations( ResourceLoaderContext $context ) {
114 global $wgCacheEpoch;
115 wfProfileIn( __METHOD__ );
116
117 $out = '';
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 );
130 }
131 // Automatically register module
132 else {
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 );
141 }
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() );
147 }
148 // Modules with dependencies pass four arguments (name, timestamp, dependencies, group)
149 // to mw.loader.register()
150 else {
151 $registrations[] = array(
152 $name, $mtime, $module->getDependencies(), $module->getGroup() );
153 }
154 }
155 }
156 $out .= ResourceLoader::makeLoaderRegisterScript( $registrations );
157
158 wfProfileOut( __METHOD__ );
159 return $out;
160 }
161
162 /* Methods */
163
164 public function getScript( ResourceLoaderContext $context ) {
165 global $IP, $wgLoadScript;
166
167 $out = file_get_contents( "$IP/resources/startup.js" );
168 if ( $context->getOnly() === 'scripts' ) {
169
170 // The core modules:
171 $modules = array( 'jquery', 'mediawiki' );
172 wfRunHooks( 'ResourceLoaderGetStartupModules', array( &$modules ) );
173
174 // Get the latest version
175 $version = 0;
176 foreach ( $modules as $moduleName ) {
177 $version = max( $version,
178 $context->getResourceLoader()->getModule( $moduleName )->getModifiedTime( $context )
179 );
180 }
181 // Build load query for StartupModules
182 $query = array(
183 'modules' => implode( '|', $modules ),
184 'only' => 'scripts',
185 'lang' => $context->getLanguage(),
186 'skin' => $context->getSkin(),
187 'debug' => $context->getDebug() ? 'true' : 'false',
188 'version' => wfTimestamp( TS_ISO_8601_BASIC, $version )
189 );
190 // Ensure uniform query order
191 ksort( $query );
192
193 // Startup function
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 ) ) .
199 "};\n";
200
201 // Conditional script injection
202 $scriptTag = Html::linkedScript( $wgLoadScript . '?' . wfArrayToCGI( $query ) );
203 $out .= "if ( isCompatible() ) {\n" .
204 "\t" . Xml::encodeJsCall( 'document.write', array( $scriptTag ) ) .
205 "}\n" .
206 "delete isCompatible;";
207 }
208
209 return $out;
210 }
211
212 public function getModifiedTime( ResourceLoaderContext $context ) {
213 global $IP, $wgCacheEpoch;
214
215 $hash = $context->getHash();
216 if ( isset( $this->modifiedTime[$hash] ) ) {
217 return $this->modifiedTime[$hash];
218 }
219
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 );
224
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
228 // code!
229 $time = wfTimestamp( TS_UNIX, $wgCacheEpoch );
230 foreach ( $loader->getModuleNames() as $name ) {
231 $module = $loader->getModule( $name );
232 $time = max( $time, $module->getModifiedTime( $context ) );
233 }
234 return $this->modifiedTime[$hash] = $time;
235 }
236
237 /* Methods */
238
239 public function getGroup() {
240 return 'startup';
241 }
242 }