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