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