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