Emit $wgSearchType as JavaScript config variable
[lhc/web/wiklou.git] / includes / resourceloader / ResourceLoaderStartUpModule.php
1 <?php
2 /**
3 * Module for resource loader initialization.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @author Trevor Parscal
22 * @author Roan Kattouw
23 */
24
25 class ResourceLoaderStartUpModule extends ResourceLoaderModule {
26
27 /* Protected Members */
28
29 protected $modifiedTime = array();
30 protected $targets = array( 'desktop', 'mobile' );
31
32 /* Protected Methods */
33
34 /**
35 * @param $context ResourceLoaderContext
36 * @return array
37 */
38 protected function getConfig( $context ) {
39 global $wgLoadScript, $wgScript, $wgStylePath, $wgScriptExtension,
40 $wgArticlePath, $wgScriptPath, $wgServer, $wgContLang,
41 $wgVariantArticlePath, $wgActionPaths, $wgVersion,
42 $wgEnableAPI, $wgEnableWriteAPI, $wgDBname,
43 $wgSitename, $wgFileExtensions, $wgExtensionAssetsPath,
44 $wgCookiePrefix, $wgResourceLoaderMaxQueryLength,
45 $wgResourceLoaderStorageEnabled, $wgResourceLoaderStorageVersion,
46 $wgSearchType;
47
48 $mainPage = Title::newMainPage();
49
50 /**
51 * Namespace related preparation
52 * - wgNamespaceIds: Key-value pairs of all localized, canonical and aliases for namespaces.
53 * - wgCaseSensitiveNamespaces: Array of namespaces that are case-sensitive.
54 */
55 $namespaceIds = $wgContLang->getNamespaceIds();
56 $caseSensitiveNamespaces = array();
57 foreach ( MWNamespace::getCanonicalNamespaces() as $index => $name ) {
58 $namespaceIds[$wgContLang->lc( $name )] = $index;
59 if ( !MWNamespace::isCapitalized( $index ) ) {
60 $caseSensitiveNamespaces[] = $index;
61 }
62 }
63
64 // Build list of variables
65 $vars = array(
66 'wgLoadScript' => $wgLoadScript,
67 'debug' => $context->getDebug(),
68 'skin' => $context->getSkin(),
69 'stylepath' => $wgStylePath,
70 'wgUrlProtocols' => wfUrlProtocols(),
71 'wgArticlePath' => $wgArticlePath,
72 'wgScriptPath' => $wgScriptPath,
73 'wgScriptExtension' => $wgScriptExtension,
74 'wgScript' => $wgScript,
75 'wgSearchType' => $wgSearchType,
76 'wgVariantArticlePath' => $wgVariantArticlePath,
77 // Force object to avoid "empty" associative array from
78 // becoming [] instead of {} in JS (bug 34604)
79 'wgActionPaths' => (object)$wgActionPaths,
80 'wgServer' => $wgServer,
81 'wgUserLanguage' => $context->getLanguage(),
82 'wgContentLanguage' => $wgContLang->getCode(),
83 'wgVersion' => $wgVersion,
84 'wgEnableAPI' => $wgEnableAPI,
85 'wgEnableWriteAPI' => $wgEnableWriteAPI,
86 'wgMainPageTitle' => $mainPage->getPrefixedText(),
87 'wgFormattedNamespaces' => $wgContLang->getFormattedNamespaces(),
88 'wgNamespaceIds' => $namespaceIds,
89 'wgContentNamespaces' => MWNamespace::getContentNamespaces(),
90 'wgSiteName' => $wgSitename,
91 'wgFileExtensions' => array_values( array_unique( $wgFileExtensions ) ),
92 'wgDBname' => $wgDBname,
93 // This sucks, it is only needed on Special:Upload, but I could
94 // not find a way to add vars only for a certain module
95 'wgFileCanRotate' => BitmapHandler::canRotate(),
96 'wgAvailableSkins' => Skin::getSkinNames(),
97 'wgExtensionAssetsPath' => $wgExtensionAssetsPath,
98 // MediaWiki sets cookies to have this prefix by default
99 'wgCookiePrefix' => $wgCookiePrefix,
100 'wgResourceLoaderMaxQueryLength' => $wgResourceLoaderMaxQueryLength,
101 'wgCaseSensitiveNamespaces' => $caseSensitiveNamespaces,
102 'wgLegalTitleChars' => Title::convertByteClassToUnicodeClass( Title::legalChars() ),
103 'wgResourceLoaderStorageVersion' => $wgResourceLoaderStorageVersion,
104 'wgResourceLoaderStorageEnabled' => $wgResourceLoaderStorageEnabled,
105 );
106
107 wfRunHooks( 'ResourceLoaderGetConfigVars', array( &$vars ) );
108
109 return $vars;
110 }
111
112 /**
113 * Gets registration code for all modules
114 *
115 * @param $context ResourceLoaderContext object
116 * @return String: JavaScript code for registering all modules with the client loader
117 */
118 public static function getModuleRegistrations( ResourceLoaderContext $context ) {
119 global $wgCacheEpoch;
120 wfProfileIn( __METHOD__ );
121
122 $out = '';
123 $registrations = array();
124 $resourceLoader = $context->getResourceLoader();
125 $target = $context->getRequest()->getVal( 'target', 'desktop' );
126
127 // Register sources
128 $out .= ResourceLoader::makeLoaderSourcesScript( $resourceLoader->getSources() );
129
130 // Register modules
131 foreach ( $resourceLoader->getModuleNames() as $name ) {
132 $module = $resourceLoader->getModule( $name );
133 $moduleTargets = $module->getTargets();
134 if ( !in_array( $target, $moduleTargets ) ) {
135 continue;
136 }
137 $deps = $module->getDependencies();
138 $group = $module->getGroup();
139 $source = $module->getSource();
140 // Support module loader scripts
141 $loader = $module->getLoaderScript();
142 if ( $loader !== false ) {
143 $version = wfTimestamp( TS_ISO_8601_BASIC,
144 $module->getModifiedTime( $context ) );
145 $out .= ResourceLoader::makeCustomLoaderScript( $name, $version, $deps, $group, $source, $loader );
146 continue;
147 }
148
149 // Automatically register module
150 // getModifiedTime() is supposed to return a UNIX timestamp, but it doesn't always
151 // seem to do that, and custom implementations might forget. Coerce it to TS_UNIX
152 $moduleMtime = wfTimestamp( TS_UNIX, $module->getModifiedTime( $context ) );
153 $mtime = max( $moduleMtime, wfTimestamp( TS_UNIX, $wgCacheEpoch ) );
154 // Modules without dependencies, a group or a foreign source pass two arguments (name, timestamp) to
155 // mw.loader.register()
156 if ( !count( $deps ) && $group === null && $source === 'local' ) {
157 $registrations[] = array( $name, $mtime );
158 }
159 // Modules with dependencies but no group or foreign source pass three arguments
160 // (name, timestamp, dependencies) to mw.loader.register()
161 elseif ( $group === null && $source === 'local' ) {
162 $registrations[] = array( $name, $mtime, $deps );
163 }
164 // Modules with a group but no foreign source pass four arguments (name, timestamp, dependencies, group)
165 // to mw.loader.register()
166 elseif ( $source === 'local' ) {
167 $registrations[] = array( $name, $mtime, $deps, $group );
168 }
169 // Modules with a foreign source pass five arguments (name, timestamp, dependencies, group, source)
170 // to mw.loader.register()
171 else {
172 $registrations[] = array( $name, $mtime, $deps, $group, $source );
173 }
174 }
175 $out .= ResourceLoader::makeLoaderRegisterScript( $registrations );
176
177 wfProfileOut( __METHOD__ );
178 return $out;
179 }
180
181 /* Methods */
182
183 /**
184 * @return bool
185 */
186 public function isRaw() {
187 return true;
188 }
189
190 /**
191 * Get the load URL of the startup modules.
192 *
193 * This is a helper for getScript(), but can also be called standalone, such
194 * as when generating an AppCache manifest.
195 *
196 * @param $context ResourceLoaderContext
197 * @return string
198 */
199 public static function getStartupModulesUrl( ResourceLoaderContext $context ) {
200 // The core modules:
201 $moduleNames = array( 'jquery', 'mediawiki' );
202 wfRunHooks( 'ResourceLoaderGetStartupModules', array( &$moduleNames ) );
203
204 // Get the latest version
205 $loader = $context->getResourceLoader();
206 $version = 0;
207 foreach ( $moduleNames as $moduleName ) {
208 $version = max( $version,
209 $loader->getModule( $moduleName )->getModifiedTime( $context )
210 );
211 }
212
213 $query = array(
214 'modules' => ResourceLoader::makePackedModulesString( $moduleNames ),
215 'only' => 'scripts',
216 'lang' => $context->getLanguage(),
217 'skin' => $context->getSkin(),
218 'debug' => $context->getDebug() ? 'true' : 'false',
219 'version' => wfTimestamp( TS_ISO_8601_BASIC, $version )
220 );
221 // Ensure uniform query order
222 ksort( $query );
223 return wfAppendQuery( wfScript( 'load' ), $query );
224 }
225
226
227 /**
228 * @param $context ResourceLoaderContext
229 * @return string
230 */
231 public function getScript( ResourceLoaderContext $context ) {
232 global $IP, $wgLegacyJavaScriptGlobals;
233
234 $out = file_get_contents( "$IP/resources/startup.js" );
235 if ( $context->getOnly() === 'scripts' ) {
236
237 // Startup function
238 $configuration = $this->getConfig( $context );
239 $registrations = self::getModuleRegistrations( $context );
240 $registrations = str_replace( "\n", "\n\t", trim( $registrations ) ); // fix indentation
241 $out .= "var startUp = function() {\n" .
242 "\tmw.config = new " . Xml::encodeJsCall( 'mw.Map', array( $wgLegacyJavaScriptGlobals ) ) . "\n" .
243 "\t$registrations\n" .
244 "\t" . Xml::encodeJsCall( 'mw.config.set', array( $configuration ) ) .
245 "};\n";
246
247 // Conditional script injection
248 $scriptTag = Html::linkedScript( self::getStartupModulesUrl( $context ) );
249 $out .= "if ( isCompatible() ) {\n" .
250 "\t" . Xml::encodeJsCall( 'document.write', array( $scriptTag ) ) .
251 "}\n" .
252 "delete isCompatible;";
253 }
254
255 return $out;
256 }
257
258 /**
259 * @return bool
260 */
261 public function supportsURLLoading() {
262 return false;
263 }
264
265 /**
266 * @param $context ResourceLoaderContext
267 * @return array|mixed
268 */
269 public function getModifiedTime( ResourceLoaderContext $context ) {
270 global $IP, $wgCacheEpoch;
271
272 $hash = $context->getHash();
273 if ( isset( $this->modifiedTime[$hash] ) ) {
274 return $this->modifiedTime[$hash];
275 }
276
277 // Call preloadModuleInfo() on ALL modules as we're about
278 // to call getModifiedTime() on all of them
279 $loader = $context->getResourceLoader();
280 $loader->preloadModuleInfo( $loader->getModuleNames(), $context );
281
282 $this->modifiedTime[$hash] = filemtime( "$IP/resources/startup.js" );
283 // ATTENTION!: Because of the line above, this is not going to cause
284 // infinite recursion - think carefully before making changes to this
285 // code!
286 $time = wfTimestamp( TS_UNIX, $wgCacheEpoch );
287 foreach ( $loader->getModuleNames() as $name ) {
288 $module = $loader->getModule( $name );
289 $time = max( $time, $module->getModifiedTime( $context ) );
290 }
291 $this->modifiedTime[$hash] = $time;
292 return $this->modifiedTime[$hash];
293 }
294
295 /* Methods */
296
297 /**
298 * @return string
299 */
300 public function getGroup() {
301 return 'startup';
302 }
303 }