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