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