Add DROP INDEX support to DatabaseSqlite::replaceVars method
[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
46 $mainPage = Title::newMainPage();
47
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.
52 */
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;
59 }
60 }
61
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( array_unique( $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 'wgLegalTitleChars' => Title::convertByteClassToUnicodeClass( Title::legalChars() ),
99 );
100
101 wfRunHooks( 'ResourceLoaderGetConfigVars', array( &$vars ) );
102
103 return $vars;
104 }
105
106 /**
107 * Gets registration code for all modules
108 *
109 * @param $context ResourceLoaderContext object
110 * @return String: JavaScript code for registering all modules with the client loader
111 */
112 public static function getModuleRegistrations( ResourceLoaderContext $context ) {
113 global $wgCacheEpoch;
114 wfProfileIn( __METHOD__ );
115
116 $out = '';
117 $registrations = array();
118 $resourceLoader = $context->getResourceLoader();
119 $target = $context->getRequest()->getVal( 'target', 'desktop' );
120
121 // Register sources
122 $out .= ResourceLoader::makeLoaderSourcesScript( $resourceLoader->getSources() );
123
124 // Register modules
125 foreach ( $resourceLoader->getModuleNames() as $name ) {
126 $module = $resourceLoader->getModule( $name );
127 $moduleTargets = $module->getTargets();
128 if ( !in_array( $target, $moduleTargets ) ) {
129 continue;
130 }
131 $deps = $module->getDependencies();
132 $group = $module->getGroup();
133 $source = $module->getSource();
134 // Support module loader scripts
135 $loader = $module->getLoaderScript();
136 if ( $loader !== false ) {
137 $version = wfTimestamp( TS_ISO_8601_BASIC,
138 $module->getModifiedTime( $context ) );
139 $out .= ResourceLoader::makeCustomLoaderScript( $name, $version, $deps, $group, $source, $loader );
140 continue;
141 }
142
143 // Automatically register module
144 // getModifiedTime() is supposed to return a UNIX timestamp, but it doesn't always
145 // seem to do that, and custom implementations might forget. Coerce it to TS_UNIX
146 $moduleMtime = wfTimestamp( TS_UNIX, $module->getModifiedTime( $context ) );
147 $mtime = max( $moduleMtime, wfTimestamp( TS_UNIX, $wgCacheEpoch ) );
148 // Modules without dependencies, a group or a foreign source pass two arguments (name, timestamp) to
149 // mw.loader.register()
150 if ( !count( $deps ) && $group === null && $source === 'local' ) {
151 $registrations[] = array( $name, $mtime );
152 }
153 // Modules with dependencies but no group or foreign source pass three arguments
154 // (name, timestamp, dependencies) to mw.loader.register()
155 elseif ( $group === null && $source === 'local' ) {
156 $registrations[] = array( $name, $mtime, $deps );
157 }
158 // Modules with a group but no foreign source pass four arguments (name, timestamp, dependencies, group)
159 // to mw.loader.register()
160 elseif ( $source === 'local' ) {
161 $registrations[] = array( $name, $mtime, $deps, $group );
162 }
163 // Modules with a foreign source pass five arguments (name, timestamp, dependencies, group, source)
164 // to mw.loader.register()
165 else {
166 $registrations[] = array( $name, $mtime, $deps, $group, $source );
167 }
168 }
169 $out .= ResourceLoader::makeLoaderRegisterScript( $registrations );
170
171 wfProfileOut( __METHOD__ );
172 return $out;
173 }
174
175 /* Methods */
176
177 /**
178 * @return bool
179 */
180 public function isRaw() {
181 return true;
182 }
183
184 /**
185 * @param $context ResourceLoaderContext
186 * @return string
187 */
188 public function getScript( ResourceLoaderContext $context ) {
189 global $IP, $wgLegacyJavaScriptGlobals;
190
191 $out = file_get_contents( "$IP/resources/startup.js" );
192 if ( $context->getOnly() === 'scripts' ) {
193
194 // The core modules:
195 $moduleNames = array( 'jquery', 'mediawiki' );
196 wfRunHooks( 'ResourceLoaderGetStartupModules', array( &$moduleNames ) );
197
198 // Get the latest version
199 $loader = $context->getResourceLoader();
200 $version = 0;
201 foreach ( $moduleNames as $moduleName ) {
202 $version = max( $version,
203 $loader->getModule( $moduleName )->getModifiedTime( $context )
204 );
205 }
206 // Build load query for StartupModules
207 $query = array(
208 'modules' => ResourceLoader::makePackedModulesString( $moduleNames ),
209 'only' => 'scripts',
210 'lang' => $context->getLanguage(),
211 'skin' => $context->getSkin(),
212 'debug' => $context->getDebug() ? 'true' : 'false',
213 'version' => wfTimestamp( TS_ISO_8601_BASIC, $version )
214 );
215 // Ensure uniform query order
216 ksort( $query );
217
218 // Startup function
219 $configuration = $this->getConfig( $context );
220 $registrations = self::getModuleRegistrations( $context );
221 $registrations = str_replace( "\n", "\n\t", trim( $registrations ) ); // fix indentation
222 $out .= "var startUp = function() {\n" .
223 "\tmw.config = new " . Xml::encodeJsCall( 'mw.Map', array( $wgLegacyJavaScriptGlobals ) ) . "\n" .
224 "\t$registrations\n" .
225 "\t" . Xml::encodeJsCall( 'mw.config.set', array( $configuration ) ) .
226 "};\n";
227
228 // Conditional script injection
229 $scriptTag = Html::linkedScript( wfAppendQuery( wfScript( 'load' ), $query ) );
230 $out .= "if ( isCompatible() ) {\n" .
231 "\t" . Xml::encodeJsCall( 'document.write', array( $scriptTag ) ) .
232 "}\n" .
233 "delete isCompatible;";
234 }
235
236 return $out;
237 }
238
239 /**
240 * @return bool
241 */
242 public function supportsURLLoading() {
243 return false;
244 }
245
246 /**
247 * @param $context ResourceLoaderContext
248 * @return array|mixed
249 */
250 public function getModifiedTime( ResourceLoaderContext $context ) {
251 global $IP, $wgCacheEpoch;
252
253 $hash = $context->getHash();
254 if ( isset( $this->modifiedTime[$hash] ) ) {
255 return $this->modifiedTime[$hash];
256 }
257
258 // Call preloadModuleInfo() on ALL modules as we're about
259 // to call getModifiedTime() on all of them
260 $loader = $context->getResourceLoader();
261 $loader->preloadModuleInfo( $loader->getModuleNames(), $context );
262
263 $this->modifiedTime[$hash] = filemtime( "$IP/resources/startup.js" );
264 // ATTENTION!: Because of the line above, this is not going to cause
265 // infinite recursion - think carefully before making changes to this
266 // code!
267 $time = wfTimestamp( TS_UNIX, $wgCacheEpoch );
268 foreach ( $loader->getModuleNames() as $name ) {
269 $module = $loader->getModule( $name );
270 $time = max( $time, $module->getModifiedTime( $context ) );
271 }
272 return $this->modifiedTime[$hash] = $time;
273 }
274
275 /* Methods */
276
277 /**
278 * @return string
279 */
280 public function getGroup() {
281 return 'startup';
282 }
283 }