Whitelist the <wbr> element.
[mediawiki.git] / includes / resourceloader / ResourceLoaderStartUpModule.php
blob861ff18c2919752cd1c8b09a8fafc4994f2b41bb
1 <?php
2 /**
3 * Module for resource loader initialization.
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.
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.
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
20 * @file
21 * @author Trevor Parscal
22 * @author Roan Kattouw
25 class ResourceLoaderStartUpModule extends ResourceLoaderModule {
27 /* Protected Members */
29 protected $modifiedTime = array();
30 protected $targets = array( 'desktop', 'mobile' );
32 /* Protected Methods */
34 /**
35 * @param $context ResourceLoaderContext
36 * @return array
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;
46 $mainPage = Title::newMainPage();
48 /**
49 * Namespace related preparation
50 * - wgNamespaceIds: Key-value pairs of all localized, canonical and aliases for namespaces.
51 * - wgCaseSensitiveNamespaces: Array of namespaces that are case-sensitive.
53 $namespaceIds = $wgContLang->getNamespaceIds();
54 $caseSensitiveNamespaces = array();
55 foreach ( MWNamespace::getCanonicalNamespaces() as $index => $name ) {
56 $namespaceIds[$wgContLang->lc( $name )] = $index;
57 if ( !MWNamespace::isCapitalized( $index ) ) {
58 $caseSensitiveNamespaces[] = $index;
62 // Build list of variables
63 $vars = array(
64 'wgLoadScript' => $wgLoadScript,
65 'debug' => $context->getDebug(),
66 'skin' => $context->getSkin(),
67 'stylepath' => $wgStylePath,
68 'wgUrlProtocols' => wfUrlProtocols(),
69 'wgArticlePath' => $wgArticlePath,
70 'wgScriptPath' => $wgScriptPath,
71 'wgScriptExtension' => $wgScriptExtension,
72 'wgScript' => $wgScript,
73 'wgVariantArticlePath' => $wgVariantArticlePath,
74 // Force object to avoid "empty" associative array from
75 // becoming [] instead of {} in JS (bug 34604)
76 'wgActionPaths' => (object)$wgActionPaths,
77 'wgServer' => $wgServer,
78 'wgUserLanguage' => $context->getLanguage(),
79 'wgContentLanguage' => $wgContLang->getCode(),
80 'wgVersion' => $wgVersion,
81 'wgEnableAPI' => $wgEnableAPI,
82 'wgEnableWriteAPI' => $wgEnableWriteAPI,
83 'wgMainPageTitle' => $mainPage->getPrefixedText(),
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,
100 wfRunHooks( 'ResourceLoaderGetConfigVars', array( &$vars ) );
102 return $vars;
106 * Gets registration code for all modules
108 * @param $context ResourceLoaderContext object
109 * @return String: JavaScript code for registering all modules with the client loader
111 public static function getModuleRegistrations( ResourceLoaderContext $context ) {
112 global $wgCacheEpoch;
113 wfProfileIn( __METHOD__ );
115 $out = '';
116 $registrations = array();
117 $resourceLoader = $context->getResourceLoader();
118 $target = $context->getRequest()->getVal( 'target', 'desktop' );
120 // Register sources
121 $out .= ResourceLoader::makeLoaderSourcesScript( $resourceLoader->getSources() );
123 // Register modules
124 foreach ( $resourceLoader->getModuleNames() as $name ) {
125 $module = $resourceLoader->getModule( $name );
126 $moduleTargets = $module->getTargets();
127 if ( !in_array( $target, $moduleTargets ) ) {
128 continue;
130 $deps = $module->getDependencies();
131 $group = $module->getGroup();
132 $source = $module->getSource();
133 // Support module loader scripts
134 $loader = $module->getLoaderScript();
135 if ( $loader !== false ) {
136 $version = wfTimestamp( TS_ISO_8601_BASIC,
137 $module->getModifiedTime( $context ) );
138 $out .= ResourceLoader::makeCustomLoaderScript( $name, $version, $deps, $group, $source, $loader );
139 continue;
142 // Automatically register module
143 // getModifiedTime() is supposed to return a UNIX timestamp, but it doesn't always
144 // seem to do that, and custom implementations might forget. Coerce it to TS_UNIX
145 $moduleMtime = wfTimestamp( TS_UNIX, $module->getModifiedTime( $context ) );
146 $mtime = max( $moduleMtime, wfTimestamp( TS_UNIX, $wgCacheEpoch ) );
147 // Modules without dependencies, a group or a foreign source pass two arguments (name, timestamp) to
148 // mw.loader.register()
149 if ( !count( $deps ) && $group === null && $source === 'local' ) {
150 $registrations[] = array( $name, $mtime );
152 // Modules with dependencies but no group or foreign source pass three arguments
153 // (name, timestamp, dependencies) to mw.loader.register()
154 elseif ( $group === null && $source === 'local' ) {
155 $registrations[] = array( $name, $mtime, $deps );
157 // Modules with a group but no foreign source pass four arguments (name, timestamp, dependencies, group)
158 // to mw.loader.register()
159 elseif ( $source === 'local' ) {
160 $registrations[] = array( $name, $mtime, $deps, $group );
162 // Modules with a foreign source pass five arguments (name, timestamp, dependencies, group, source)
163 // to mw.loader.register()
164 else {
165 $registrations[] = array( $name, $mtime, $deps, $group, $source );
168 $out .= ResourceLoader::makeLoaderRegisterScript( $registrations );
170 wfProfileOut( __METHOD__ );
171 return $out;
174 /* Methods */
177 * @return bool
179 public function isRaw() {
180 return true;
184 * @param $context ResourceLoaderContext
185 * @return string
187 public function getScript( ResourceLoaderContext $context ) {
188 global $IP, $wgLegacyJavaScriptGlobals;
190 $out = file_get_contents( "$IP/resources/startup.js" );
191 if ( $context->getOnly() === 'scripts' ) {
193 // The core modules:
194 $moduleNames = array( 'jquery', 'mediawiki' );
195 wfRunHooks( 'ResourceLoaderGetStartupModules', array( &$moduleNames ) );
197 // Get the latest version
198 $loader = $context->getResourceLoader();
199 $version = 0;
200 foreach ( $moduleNames as $moduleName ) {
201 $version = max( $version,
202 $loader->getModule( $moduleName )->getModifiedTime( $context )
205 // Build load query for StartupModules
206 $query = array(
207 'modules' => ResourceLoader::makePackedModulesString( $moduleNames ),
208 'only' => 'scripts',
209 'lang' => $context->getLanguage(),
210 'skin' => $context->getSkin(),
211 'debug' => $context->getDebug() ? 'true' : 'false',
212 'version' => wfTimestamp( TS_ISO_8601_BASIC, $version )
214 // Ensure uniform query order
215 ksort( $query );
217 // Startup function
218 $configuration = $this->getConfig( $context );
219 $registrations = self::getModuleRegistrations( $context );
220 $registrations = str_replace( "\n", "\n\t", trim( $registrations ) ); // fix indentation
221 $out .= "var startUp = function() {\n" .
222 "\tmw.config = new " . Xml::encodeJsCall( 'mw.Map', array( $wgLegacyJavaScriptGlobals ) ) . "\n" .
223 "\t$registrations\n" .
224 "\t" . Xml::encodeJsCall( 'mw.config.set', array( $configuration ) ) .
225 "};\n";
227 // Conditional script injection
228 $scriptTag = Html::linkedScript( wfAppendQuery( wfScript( 'load' ), $query ) );
229 $out .= "if ( isCompatible() ) {\n" .
230 "\t" . Xml::encodeJsCall( 'document.write', array( $scriptTag ) ) .
231 "}\n" .
232 "delete isCompatible;";
235 return $out;
239 * @return bool
241 public function supportsURLLoading() {
242 return false;
246 * @param $context ResourceLoaderContext
247 * @return array|mixed
249 public function getModifiedTime( ResourceLoaderContext $context ) {
250 global $IP, $wgCacheEpoch;
252 $hash = $context->getHash();
253 if ( isset( $this->modifiedTime[$hash] ) ) {
254 return $this->modifiedTime[$hash];
257 // Call preloadModuleInfo() on ALL modules as we're about
258 // to call getModifiedTime() on all of them
259 $loader = $context->getResourceLoader();
260 $loader->preloadModuleInfo( $loader->getModuleNames(), $context );
262 $this->modifiedTime[$hash] = filemtime( "$IP/resources/startup.js" );
263 // ATTENTION!: Because of the line above, this is not going to cause
264 // infinite recursion - think carefully before making changes to this
265 // code!
266 $time = wfTimestamp( TS_UNIX, $wgCacheEpoch );
267 foreach ( $loader->getModuleNames() as $name ) {
268 $module = $loader->getModule( $name );
269 $time = max( $time, $module->getModifiedTime( $context ) );
271 return $this->modifiedTime[$hash] = $time;
274 /* Methods */
277 * @return string
279 public function getGroup() {
280 return 'startup';