Bug 34604 - [mw.config] wgActionPaths should be an object instead of a numeral array...
[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,
41 $wgCookiePrefix, $wgResourceLoaderMaxQueryLength;
42
43 $mainPage = Title::newMainPage();
44
45 /**
46 * Namespace related preparation
47 * - wgNamespaceIds: Key-value pairs of all localized, canonical and aliases for namespaces.
48 * - wgCaseSensitiveNamespaces: Array of namespaces that are case-sensitive.
49 */
50 $namespaceIds = $wgContLang->getNamespaceIds();
51 $caseSensitiveNamespaces = array();
52 foreach( MWNamespace::getCanonicalNamespaces() as $index => $name ) {
53 $namespaceIds[$wgContLang->lc( $name )] = $index;
54 if ( !MWNamespace::isCapitalized( $index ) ) {
55 $caseSensitiveNamespaces[] = $index;
56 }
57 }
58
59 // Build list of variables
60 $vars = array(
61 'wgLoadScript' => $wgLoadScript,
62 'debug' => $context->getDebug(),
63 'skin' => $context->getSkin(),
64 'stylepath' => $wgStylePath,
65 'wgUrlProtocols' => wfUrlProtocols(),
66 'wgArticlePath' => $wgArticlePath,
67 'wgScriptPath' => $wgScriptPath,
68 'wgScriptExtension' => $wgScriptExtension,
69 'wgScript' => $wgScript,
70 'wgVariantArticlePath' => $wgVariantArticlePath,
71 // Force object to avoid "empty" associative array from
72 // becoming [] instead of {} in JS (bug 34604)
73 'wgActionPaths' => (object)$wgActionPaths,
74 'wgServer' => $wgServer,
75 'wgUserLanguage' => $context->getLanguage(),
76 'wgContentLanguage' => $wgContLang->getCode(),
77 'wgVersion' => $wgVersion,
78 'wgEnableAPI' => $wgEnableAPI,
79 'wgEnableWriteAPI' => $wgEnableWriteAPI,
80 'wgDefaultDateFormat' => $wgContLang->getDefaultDateFormat(),
81 'wgMonthNames' => $wgContLang->getMonthNamesArray(),
82 'wgMonthNamesShort' => $wgContLang->getMonthAbbreviationsArray(),
83 'wgMainPageTitle' => $mainPage ? $mainPage->getPrefixedText() : null,
84 'wgFormattedNamespaces' => $wgContLang->getFormattedNamespaces(),
85 'wgNamespaceIds' => $namespaceIds,
86 'wgSiteName' => $wgSitename,
87 'wgFileExtensions' => array_values( $wgFileExtensions ),
88 'wgDBname' => $wgDBname,
89 // This sucks, it is only needed on Special:Upload, but I could
90 // not find a way to add vars only for a certain module
91 'wgFileCanRotate' => BitmapHandler::canRotate(),
92 'wgAvailableSkins' => Skin::getSkinNames(),
93 'wgExtensionAssetsPath' => $wgExtensionAssetsPath,
94 // MediaWiki sets cookies to have this prefix by default
95 'wgCookiePrefix' => $wgCookiePrefix,
96 'wgResourceLoaderMaxQueryLength' => $wgResourceLoaderMaxQueryLength,
97 'wgCaseSensitiveNamespaces' => $caseSensitiveNamespaces,
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
122 // Register sources
123 $out .= ResourceLoader::makeLoaderSourcesScript( $resourceLoader->getSources() );
124
125 // Register modules
126 foreach ( $resourceLoader->getModuleNames() as $name ) {
127 $module = $resourceLoader->getModule( $name );
128 // Support module loader scripts
129 $loader = $module->getLoaderScript();
130 if ( $loader !== false ) {
131 $deps = $module->getDependencies();
132 $group = $module->getGroup();
133 $source = $module->getSource();
134 $version = wfTimestamp( TS_ISO_8601_BASIC,
135 $module->getModifiedTime( $context ) );
136 $out .= ResourceLoader::makeCustomLoaderScript( $name, $version, $deps, $group, $source, $loader );
137 }
138 // Automatically register module
139 else {
140 // getModifiedTime() is supposed to return a UNIX timestamp, but it doesn't always
141 // seem to do that, and custom implementations might forget. Coerce it to TS_UNIX
142 $moduleMtime = wfTimestamp( TS_UNIX, $module->getModifiedTime( $context ) );
143 $mtime = max( $moduleMtime, wfTimestamp( TS_UNIX, $wgCacheEpoch ) );
144 // Modules without dependencies, a group or a foreign source pass two arguments (name, timestamp) to
145 // mw.loader.register()
146 if ( !count( $module->getDependencies() && $module->getGroup() === null && $module->getSource() === 'local' ) ) {
147 $registrations[] = array( $name, $mtime );
148 }
149 // Modules with dependencies but no group or foreign source pass three arguments
150 // (name, timestamp, dependencies) to mw.loader.register()
151 elseif ( $module->getGroup() === null && $module->getSource() === 'local' ) {
152 $registrations[] = array(
153 $name, $mtime, $module->getDependencies() );
154 }
155 // Modules with a group but no foreign source pass four arguments (name, timestamp, dependencies, group)
156 // to mw.loader.register()
157 elseif ( $module->getSource() === 'local' ) {
158 $registrations[] = array(
159 $name, $mtime, $module->getDependencies(), $module->getGroup() );
160 }
161 // Modules with a foreign source pass five arguments (name, timestamp, dependencies, group, source)
162 // to mw.loader.register()
163 else {
164 $registrations[] = array(
165 $name, $mtime, $module->getDependencies(), $module->getGroup(), $module->getSource() );
166 }
167 }
168 }
169 $out .= ResourceLoader::makeLoaderRegisterScript( $registrations );
170
171 wfProfileOut( __METHOD__ );
172 return $out;
173 }
174
175 /* Methods */
176
177 /**
178 * @param $context ResourceLoaderContext
179 * @return string
180 */
181 public function getScript( ResourceLoaderContext $context ) {
182 global $IP, $wgLoadScript, $wgLegacyJavaScriptGlobals;
183
184 $out = file_get_contents( "$IP/resources/startup.js" );
185 if ( $context->getOnly() === 'scripts' ) {
186
187 // The core modules:
188 $modules = array( 'jquery', 'mediawiki' );
189 wfRunHooks( 'ResourceLoaderGetStartupModules', array( &$modules ) );
190
191 // Get the latest version
192 $version = 0;
193 foreach ( $modules as $moduleName ) {
194 $version = max( $version,
195 $context->getResourceLoader()->getModule( $moduleName )->getModifiedTime( $context )
196 );
197 }
198 // Build load query for StartupModules
199 $query = array(
200 'modules' => ResourceLoader::makePackedModulesString( $modules ),
201 'only' => 'scripts',
202 'lang' => $context->getLanguage(),
203 'skin' => $context->getSkin(),
204 'debug' => $context->getDebug() ? 'true' : 'false',
205 'version' => wfTimestamp( TS_ISO_8601_BASIC, $version )
206 );
207 // Ensure uniform query order
208 ksort( $query );
209
210 // Startup function
211 $configuration = $this->getConfig( $context );
212 $registrations = self::getModuleRegistrations( $context );
213 $out .= "var startUp = function() {\n" .
214 "\tmw.config = new " . Xml::encodeJsCall( 'mw.Map', array( $wgLegacyJavaScriptGlobals ) ) . "\n" .
215 "\t$registrations\n" .
216 "\t" . Xml::encodeJsCall( 'mw.config.set', array( $configuration ) ) .
217 "};\n";
218
219 // Conditional script injection
220 $scriptTag = Html::linkedScript( $wgLoadScript . '?' . wfArrayToCGI( $query ) );
221 $out .= "if ( isCompatible() ) {\n" .
222 "\t" . Xml::encodeJsCall( 'document.write', array( $scriptTag ) ) .
223 "}\n" .
224 "delete isCompatible;";
225 }
226
227 return $out;
228 }
229
230 /**
231 * @return bool
232 */
233 public function supportsURLLoading() {
234 return false;
235 }
236
237 /**
238 * @param $context ResourceLoaderContext
239 * @return array|mixed
240 */
241 public function getModifiedTime( ResourceLoaderContext $context ) {
242 global $IP, $wgCacheEpoch;
243
244 $hash = $context->getHash();
245 if ( isset( $this->modifiedTime[$hash] ) ) {
246 return $this->modifiedTime[$hash];
247 }
248
249 // Call preloadModuleInfo() on ALL modules as we're about
250 // to call getModifiedTime() on all of them
251 $loader = $context->getResourceLoader();
252 $loader->preloadModuleInfo( $loader->getModuleNames(), $context );
253
254 $this->modifiedTime[$hash] = filemtime( "$IP/resources/startup.js" );
255 // ATTENTION!: Because of the line above, this is not going to cause
256 // infinite recursion - think carefully before making changes to this
257 // code!
258 $time = wfTimestamp( TS_UNIX, $wgCacheEpoch );
259 foreach ( $loader->getModuleNames() as $name ) {
260 $module = $loader->getModule( $name );
261 $time = max( $time, $module->getModifiedTime( $context ) );
262 }
263 return $this->modifiedTime[$hash] = $time;
264 }
265
266 /* Methods */
267
268 /**
269 * @return string
270 */
271 public function getGroup() {
272 return 'startup';
273 }
274 }