Add a 'revdelete-selected-file' message on Special:RevisionDelete
[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
155 if ( !count( $deps ) && $group === null && $source === 'local' ) {
156 // Modules without dependencies, a group or a foreign source pass two arguments (name, timestamp) to
157 // mw.loader.register()
158 $registrations[] = array( $name, $mtime );
159 } elseif ( $group === null && $source === 'local' ) {
160 // Modules with dependencies but no group or foreign source pass three arguments
161 // (name, timestamp, dependencies) to mw.loader.register()
162 $registrations[] = array( $name, $mtime, $deps );
163 } elseif ( $source === 'local' ) {
164 // Modules with a group but no foreign source pass four arguments (name, timestamp, dependencies, group)
165 // to mw.loader.register()
166 $registrations[] = array( $name, $mtime, $deps, $group );
167 } else {
168 // Modules with a foreign source pass five arguments (name, timestamp, dependencies, group, source)
169 // to mw.loader.register()
170 $registrations[] = array( $name, $mtime, $deps, $group, $source );
171 }
172 }
173 $out .= ResourceLoader::makeLoaderRegisterScript( $registrations );
174
175 wfProfileOut( __METHOD__ );
176 return $out;
177 }
178
179 /* Methods */
180
181 /**
182 * @return bool
183 */
184 public function isRaw() {
185 return true;
186 }
187
188 /**
189 * Get the load URL of the startup modules.
190 *
191 * This is a helper for getScript(), but can also be called standalone, such
192 * as when generating an AppCache manifest.
193 *
194 * @param $context ResourceLoaderContext
195 * @return string
196 */
197 public static function getStartupModulesUrl( ResourceLoaderContext $context ) {
198 // The core modules:
199 $moduleNames = array( 'jquery', 'mediawiki' );
200 wfRunHooks( 'ResourceLoaderGetStartupModules', array( &$moduleNames ) );
201
202 // Get the latest version
203 $loader = $context->getResourceLoader();
204 $version = 0;
205 foreach ( $moduleNames as $moduleName ) {
206 $version = max( $version,
207 $loader->getModule( $moduleName )->getModifiedTime( $context )
208 );
209 }
210
211 $query = array(
212 'modules' => ResourceLoader::makePackedModulesString( $moduleNames ),
213 'only' => 'scripts',
214 'lang' => $context->getLanguage(),
215 'skin' => $context->getSkin(),
216 'debug' => $context->getDebug() ? 'true' : 'false',
217 'version' => wfTimestamp( TS_ISO_8601_BASIC, $version )
218 );
219 // Ensure uniform query order
220 ksort( $query );
221 return wfAppendQuery( wfScript( 'load' ), $query );
222 }
223
224 /**
225 * @param $context ResourceLoaderContext
226 * @return string
227 */
228 public function getScript( ResourceLoaderContext $context ) {
229 global $IP, $wgLegacyJavaScriptGlobals;
230
231 $out = file_get_contents( "$IP/resources/startup.js" );
232 if ( $context->getOnly() === 'scripts' ) {
233
234 // Startup function
235 $configuration = $this->getConfig( $context );
236 $registrations = self::getModuleRegistrations( $context );
237 $registrations = str_replace( "\n", "\n\t", trim( $registrations ) ); // fix indentation
238 $out .= "var startUp = function() {\n" .
239 "\tmw.config = new " . Xml::encodeJsCall( 'mw.Map', array( $wgLegacyJavaScriptGlobals ) ) . "\n" .
240 "\t$registrations\n" .
241 "\t" . Xml::encodeJsCall( 'mw.config.set', array( $configuration ) ) .
242 "};\n";
243
244 // Conditional script injection
245 $scriptTag = Html::linkedScript( self::getStartupModulesUrl( $context ) );
246 $out .= "if ( isCompatible() ) {\n" .
247 "\t" . Xml::encodeJsCall( 'document.write', array( $scriptTag ) ) .
248 "}\n" .
249 "delete isCompatible;";
250 }
251
252 return $out;
253 }
254
255 /**
256 * @return bool
257 */
258 public function supportsURLLoading() {
259 return false;
260 }
261
262 /**
263 * @param $context ResourceLoaderContext
264 * @return array|mixed
265 */
266 public function getModifiedTime( ResourceLoaderContext $context ) {
267 global $IP, $wgCacheEpoch;
268
269 $hash = $context->getHash();
270 if ( isset( $this->modifiedTime[$hash] ) ) {
271 return $this->modifiedTime[$hash];
272 }
273
274 // Call preloadModuleInfo() on ALL modules as we're about
275 // to call getModifiedTime() on all of them
276 $loader = $context->getResourceLoader();
277 $loader->preloadModuleInfo( $loader->getModuleNames(), $context );
278
279 $this->modifiedTime[$hash] = filemtime( "$IP/resources/startup.js" );
280 // ATTENTION!: Because of the line above, this is not going to cause
281 // infinite recursion - think carefully before making changes to this
282 // code!
283 $time = wfTimestamp( TS_UNIX, $wgCacheEpoch );
284 foreach ( $loader->getModuleNames() as $name ) {
285 $module = $loader->getModule( $name );
286 $time = max( $time, $module->getModifiedTime( $context ) );
287 }
288 $this->modifiedTime[$hash] = $time;
289 return $this->modifiedTime[$hash];
290 }
291
292 /* Methods */
293
294 /**
295 * @return string
296 */
297 public function getGroup() {
298 return 'startup';
299 }
300 }