wgNamespaceIds in JavaScript didn't include canonical namespaces. Adding them to...
[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 wgNamespaceIds
59 // A complete key-value pair object mapping localized, canonical and aliases for namespaces
60 // to their numerical ids (case insensitive and with underscores)
61 $namespaceIds = $wgContLang->getNamespaceIds();
62 foreach( MWNamespace::getCanonicalNamespaces() as $index => $name ) {
63 $nsIds[$wgContLang->lc( $name )] = $index;
64 }
65
66 // Build list of variables
67 $vars = array(
68 'wgLoadScript' => $wgLoadScript,
69 'debug' => $context->getDebug(),
70 'skin' => $context->getSkin(),
71 'stylepath' => $wgStylePath,
72 'wgUrlProtocols' => wfUrlProtocols(),
73 'wgArticlePath' => $wgArticlePath,
74 'wgScriptPath' => $wgScriptPath,
75 'wgScriptExtension' => $wgScriptExtension,
76 'wgScript' => $wgScript,
77 'wgVariantArticlePath' => $wgVariantArticlePath,
78 'wgActionPaths' => $wgActionPaths,
79 'wgServer' => $wgServer,
80 'wgUserLanguage' => $context->getLanguage(),
81 'wgContentLanguage' => $wgContLang->getCode(),
82 'wgVersion' => $wgVersion,
83 'wgEnableAPI' => $wgEnableAPI,
84 'wgEnableWriteAPI' => $wgEnableWriteAPI,
85 'wgDefaultDateFormat' => $wgContLang->getDefaultDateFormat(),
86 'wgMonthNames' => $wgContLang->getMonthNamesArray(),
87 'wgMonthNamesShort' => $wgContLang->getMonthAbbreviationsArray(),
88 'wgSeparatorTransformTable' => $compactSeparatorTransTable,
89 'wgDigitTransformTable' => $compactDigitTransTable,
90 'wgMainPageTitle' => $mainPage ? $mainPage->getPrefixedText() : null,
91 'wgFormattedNamespaces' => $wgContLang->getFormattedNamespaces(),
92 'wgNamespaceIds' => $namespaceIds,
93 'wgSiteName' => $wgSitename,
94 'wgFileExtensions' => array_values( $wgFileExtensions ),
95 'wgDBname' => $wgDBname,
96 // This sucks, it is only needed on Special:Upload, but I could
97 // not find a way to add vars only for a certain module
98 'wgFileCanRotate' => BitmapHandler::canRotate(),
99 'wgAvailableSkins' => Skin::getSkinNames(),
100 'wgExtensionAssetsPath' => $wgExtensionAssetsPath,
101 'wgProto' => $wgProto,
102 // MediaWiki sets cookies to have this prefix by default
103 'wgCookiePrefix' => $wgCookiePrefix,
104 'wgResourceLoaderMaxQueryLength' => $wgResourceLoaderMaxQueryLength,
105 'wgLegacyJavaScriptGlobals' => $wgLegacyJavaScriptGlobals,
106 );
107 if ( $wgUseAjax && $wgEnableMWSuggest ) {
108 $vars['wgMWSuggestTemplate'] = SearchEngine::getMWSuggestTemplate();
109 }
110
111 wfRunHooks( 'ResourceLoaderGetConfigVars', array( &$vars ) );
112
113 return $vars;
114 }
115
116 /**
117 * Gets registration code for all modules
118 *
119 * @param $context ResourceLoaderContext object
120 * @return String: JavaScript code for registering all modules with the client loader
121 */
122 public static function getModuleRegistrations( ResourceLoaderContext $context ) {
123 global $wgCacheEpoch;
124 wfProfileIn( __METHOD__ );
125
126 $out = '';
127 $registrations = array();
128 $resourceLoader = $context->getResourceLoader();
129 foreach ( $resourceLoader->getModuleNames() as $name ) {
130 $module = $resourceLoader->getModule( $name );
131 // Support module loader scripts
132 $loader = $module->getLoaderScript();
133 if ( $loader !== false ) {
134 $deps = $module->getDependencies();
135 $group = $module->getGroup();
136 $version = wfTimestamp( TS_ISO_8601_BASIC,
137 $module->getModifiedTime( $context ) );
138 $out .= ResourceLoader::makeCustomLoaderScript( $name, $version, $deps, $group, $loader );
139 }
140 // Automatically register module
141 else {
142 // getModifiedTime() is supposed to return a UNIX timestamp, but it doesn't always
143 // seem to do that, and custom implementations might forget. Coerce it to TS_UNIX
144 $moduleMtime = wfTimestamp( TS_UNIX, $module->getModifiedTime( $context ) );
145 $mtime = max( $moduleMtime, wfTimestamp( TS_UNIX, $wgCacheEpoch ) );
146 // Modules without dependencies or a group pass two arguments (name, timestamp) to
147 // mw.loader.register()
148 if ( !count( $module->getDependencies() && $module->getGroup() === null ) ) {
149 $registrations[] = array( $name, $mtime );
150 }
151 // Modules with dependencies but no group pass three arguments
152 // (name, timestamp, dependencies) to mw.loader.register()
153 else if ( $module->getGroup() === null ) {
154 $registrations[] = array(
155 $name, $mtime, $module->getDependencies() );
156 }
157 // Modules with dependencies pass four arguments (name, timestamp, dependencies, group)
158 // to mw.loader.register()
159 else {
160 $registrations[] = array(
161 $name, $mtime, $module->getDependencies(), $module->getGroup() );
162 }
163 }
164 }
165 $out .= ResourceLoader::makeLoaderRegisterScript( $registrations );
166
167 wfProfileOut( __METHOD__ );
168 return $out;
169 }
170
171 /* Methods */
172
173 /**
174 * @param $context ResourceLoaderContext
175 * @return string
176 */
177 public function getScript( ResourceLoaderContext $context ) {
178 global $IP, $wgLoadScript, $wgLegacyJavaScriptGlobals;
179
180 $out = file_get_contents( "$IP/resources/startup.js" );
181 if ( $context->getOnly() === 'scripts' ) {
182
183 // The core modules:
184 $modules = array( 'jquery', 'mediawiki' );
185 wfRunHooks( 'ResourceLoaderGetStartupModules', array( &$modules ) );
186
187 // Get the latest version
188 $version = 0;
189 foreach ( $modules as $moduleName ) {
190 $version = max( $version,
191 $context->getResourceLoader()->getModule( $moduleName )->getModifiedTime( $context )
192 );
193 }
194 // Build load query for StartupModules
195 $query = array(
196 'modules' => ResourceLoader::makePackedModulesString( $modules ),
197 'only' => 'scripts',
198 'lang' => $context->getLanguage(),
199 'skin' => $context->getSkin(),
200 'debug' => $context->getDebug() ? 'true' : 'false',
201 'version' => wfTimestamp( TS_ISO_8601_BASIC, $version )
202 );
203 // Ensure uniform query order
204 ksort( $query );
205
206 // Startup function
207 $configuration = $this->getConfig( $context );
208 $registrations = self::getModuleRegistrations( $context );
209 $out .= "var startUp = function() {\n" .
210 "\tmw.config = new " . Xml::encodeJsCall( 'mw.Map', array( $wgLegacyJavaScriptGlobals ) ) . "\n" .
211 "\t$registrations\n" .
212 "\t" . Xml::encodeJsCall( 'mw.config.set', array( $configuration ) ) .
213 "};\n";
214
215 // Conditional script injection
216 $scriptTag = Html::linkedScript( $wgLoadScript . '?' . wfArrayToCGI( $query ) );
217 $out .= "if ( isCompatible() ) {\n" .
218 "\t" . Xml::encodeJsCall( 'document.write', array( $scriptTag ) ) .
219 "}\n" .
220 "delete isCompatible;";
221 }
222
223 return $out;
224 }
225
226 /**
227 * @param $context ResourceLoaderContext
228 * @return array|mixed
229 */
230 public function getModifiedTime( ResourceLoaderContext $context ) {
231 global $IP, $wgCacheEpoch;
232
233 $hash = $context->getHash();
234 if ( isset( $this->modifiedTime[$hash] ) ) {
235 return $this->modifiedTime[$hash];
236 }
237
238 // Call preloadModuleInfo() on ALL modules as we're about
239 // to call getModifiedTime() on all of them
240 $loader = $context->getResourceLoader();
241 $loader->preloadModuleInfo( $loader->getModuleNames(), $context );
242
243 $this->modifiedTime[$hash] = filemtime( "$IP/resources/startup.js" );
244 // ATTENTION!: Because of the line above, this is not going to cause
245 // infinite recursion - think carefully before making changes to this
246 // code!
247 $time = wfTimestamp( TS_UNIX, $wgCacheEpoch );
248 foreach ( $loader->getModuleNames() as $name ) {
249 $module = $loader->getModule( $name );
250 $time = max( $time, $module->getModifiedTime( $context ) );
251 }
252 return $this->modifiedTime[$hash] = $time;
253 }
254
255 /* Methods */
256
257 /**
258 * @return string
259 */
260 public function getGroup() {
261 return 'startup';
262 }
263 }