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