Use wfMessage() so that we can check message existence in content language instead...
[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, $wgVersion,
39 $wgEnableAPI, $wgEnableWriteAPI, $wgDBname, $wgEnableMWSuggest,
40 $wgSitename, $wgFileExtensions, $wgExtensionAssetsPath, $wgProto,
41 $wgCookiePrefix, $wgResourceLoaderMaxQueryLength, $wgLegacyJavaScriptGlobals;
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' => $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,
98 );
99 if ( $wgUseAjax && $wgEnableMWSuggest ) {
100 $vars['wgMWSuggestTemplate'] = SearchEngine::getMWSuggestTemplate();
101 }
102
103 wfRunHooks( 'ResourceLoaderGetConfigVars', array( &$vars ) );
104
105 return $vars;
106 }
107
108 /**
109 * Gets registration code for all modules
110 *
111 * @param $context ResourceLoaderContext object
112 * @return String: JavaScript code for registering all modules with the client loader
113 */
114 public static function getModuleRegistrations( ResourceLoaderContext $context ) {
115 global $wgCacheEpoch;
116 wfProfileIn( __METHOD__ );
117
118 $out = '';
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 );
131 }
132 // Automatically register module
133 else {
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 );
142 }
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() );
148 }
149 // Modules with dependencies pass four arguments (name, timestamp, dependencies, group)
150 // to mw.loader.register()
151 else {
152 $registrations[] = array(
153 $name, $mtime, $module->getDependencies(), $module->getGroup() );
154 }
155 }
156 }
157 $out .= ResourceLoader::makeLoaderRegisterScript( $registrations );
158
159 wfProfileOut( __METHOD__ );
160 return $out;
161 }
162
163 /* Methods */
164
165 /**
166 * @param $context ResourceLoaderContext
167 * @return string
168 */
169 public function getScript( ResourceLoaderContext $context ) {
170 global $IP, $wgLoadScript, $wgLegacyJavaScriptGlobals;
171
172 $out = file_get_contents( "$IP/resources/startup.js" );
173 if ( $context->getOnly() === 'scripts' ) {
174
175 // The core modules:
176 $modules = array( 'jquery', 'mediawiki' );
177 wfRunHooks( 'ResourceLoaderGetStartupModules', array( &$modules ) );
178
179 // Get the latest version
180 $version = 0;
181 foreach ( $modules as $moduleName ) {
182 $version = max( $version,
183 $context->getResourceLoader()->getModule( $moduleName )->getModifiedTime( $context )
184 );
185 }
186 // Build load query for StartupModules
187 $query = array(
188 'modules' => implode( '|', $modules ),
189 'only' => 'scripts',
190 'lang' => $context->getLanguage(),
191 'skin' => $context->getSkin(),
192 'debug' => $context->getDebug() ? 'true' : 'false',
193 'version' => wfTimestamp( TS_ISO_8601_BASIC, $version )
194 );
195 // Ensure uniform query order
196 ksort( $query );
197
198 // Startup function
199 $configuration = $this->getConfig( $context );
200 $registrations = self::getModuleRegistrations( $context );
201 $out .= "var startUp = function() {\n" .
202 "\tmw.config = new " . Xml::encodeJsCall( 'mw.Map', array( $wgLegacyJavaScriptGlobals ) ) . "\n" .
203 "\t$registrations\n" .
204 "\t" . Xml::encodeJsCall( 'mw.config.set', array( $configuration ) ) .
205 "};\n";
206
207 // Conditional script injection
208 $scriptTag = Html::linkedScript( $wgLoadScript . '?' . wfArrayToCGI( $query ) );
209 $out .= "if ( isCompatible() ) {\n" .
210 "\t" . Xml::encodeJsCall( 'document.write', array( $scriptTag ) ) .
211 "}\n" .
212 "delete isCompatible;";
213 }
214
215 return $out;
216 }
217
218 /**
219 * @param $context ResourceLoaderContext
220 * @return array|mixed
221 */
222 public function getModifiedTime( ResourceLoaderContext $context ) {
223 global $IP, $wgCacheEpoch;
224
225 $hash = $context->getHash();
226 if ( isset( $this->modifiedTime[$hash] ) ) {
227 return $this->modifiedTime[$hash];
228 }
229
230 // Call preloadModuleInfo() on ALL modules as we're about
231 // to call getModifiedTime() on all of them
232 $loader = $context->getResourceLoader();
233 $loader->preloadModuleInfo( $loader->getModuleNames(), $context );
234
235 $this->modifiedTime[$hash] = filemtime( "$IP/resources/startup.js" );
236 // ATTENTION!: Because of the line above, this is not going to cause
237 // infinite recursion - think carefully before making changes to this
238 // code!
239 $time = wfTimestamp( TS_UNIX, $wgCacheEpoch );
240 foreach ( $loader->getModuleNames() as $name ) {
241 $module = $loader->getModule( $name );
242 $time = max( $time, $module->getModifiedTime( $context ) );
243 }
244 return $this->modifiedTime[$hash] = $time;
245 }
246
247 /* Methods */
248
249 /**
250 * @return string
251 */
252 public function getGroup() {
253 return 'startup';
254 }
255 }