2 subclasses of ResourceLoaderWikiModule implement a duplicate version of getFlip...
[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
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 'wgSeparatorTransformTable' => $compactSeparatorTransTable,
77 'wgDigitTransformTable' => $compactDigitTransTable,
78 'wgMainPageTitle' => $mainPage ? $mainPage->getPrefixedText() : null,
79 'wgFormattedNamespaces' => $wgContLang->getFormattedNamespaces(),
80 'wgNamespaceIds' => $wgContLang->getNamespaceIds(),
81 'wgSiteName' => $wgSitename,
82 'wgFileExtensions' => array_values( $wgFileExtensions ),
83 'wgDBname' => $wgDBname,
84 // This sucks, it is only needed on Special:Upload, but I could
85 // not find a way to add vars only for a certain module
86 'wgFileCanRotate' => BitmapHandler::canRotate(),
87 'wgAvailableSkins' => Skin::getSkinNames(),
88 'wgExtensionAssetsPath' => $wgExtensionAssetsPath,
89 );
90 if ( $wgUseAjax && $wgEnableMWSuggest ) {
91 $vars['wgMWSuggestTemplate'] = SearchEngine::getMWSuggestTemplate();
92 }
93
94 wfRunHooks( 'ResourceLoaderGetConfigVars', array( &$vars ) );
95
96 return $vars;
97 }
98
99 /**
100 * Gets registration code for all modules
101 *
102 * @param $context ResourceLoaderContext object
103 * @return String: JavaScript code for registering all modules with the client loader
104 */
105 public static function getModuleRegistrations( ResourceLoaderContext $context ) {
106 global $wgCacheEpoch;
107 wfProfileIn( __METHOD__ );
108
109 $out = '';
110 $registrations = array();
111 $resourceLoader = $context->getResourceLoader();
112 foreach ( $resourceLoader->getModuleNames() as $name ) {
113 $module = $resourceLoader->getModule( $name );
114 // Support module loader scripts
115 $loader = $module->getLoaderScript();
116 if ( $loader !== false ) {
117 $deps = $module->getDependencies();
118 $group = $module->getGroup();
119 $version = wfTimestamp( TS_ISO_8601_BASIC,
120 round( $module->getModifiedTime( $context ), -2 ) );
121 $out .= ResourceLoader::makeCustomLoaderScript( $name, $version, $deps, $group, $loader );
122 }
123 // Automatically register module
124 else {
125 // getModifiedTime() is supposed to return a UNIX timestamp, but it doesn't always
126 // seem to do that, and custom implementations might forget. Coerce it to TS_UNIX
127 $moduleMtime = wfTimestamp( TS_UNIX, $module->getModifiedTime( $context ) );
128 $mtime = max( $moduleMtime, wfTimestamp( TS_UNIX, $wgCacheEpoch ) );
129 // Modules without dependencies or a group pass two arguments (name, timestamp) to
130 // mediaWiki.loader.register()
131 if ( !count( $module->getDependencies() && $module->getGroup() === null ) ) {
132 $registrations[] = array( $name, $mtime );
133 }
134 // Modules with dependencies but no group pass three arguments
135 // (name, timestamp, dependencies) to mediaWiki.loader.register()
136 else if ( $module->getGroup() === null ) {
137 $registrations[] = array(
138 $name, $mtime, $module->getDependencies() );
139 }
140 // Modules with dependencies pass four arguments (name, timestamp, dependencies, group)
141 // to mediaWiki.loader.register()
142 else {
143 $registrations[] = array(
144 $name, $mtime, $module->getDependencies(), $module->getGroup() );
145 }
146 }
147 }
148 $out .= ResourceLoader::makeLoaderRegisterScript( $registrations );
149
150 wfProfileOut( __METHOD__ );
151 return $out;
152 }
153
154 /* Methods */
155
156 public function getScript( ResourceLoaderContext $context ) {
157 global $IP, $wgLoadScript;
158
159 $out = file_get_contents( "$IP/resources/startup.js" );
160 if ( $context->getOnly() === 'scripts' ) {
161 // Build load query for jquery and mediawiki modules
162 $query = array(
163 'modules' => implode( '|', array( 'jquery', 'mediawiki' ) ),
164 'only' => 'scripts',
165 'lang' => $context->getLanguage(),
166 'skin' => $context->getSkin(),
167 'debug' => $context->getDebug() ? 'true' : 'false',
168 'version' => wfTimestamp( TS_ISO_8601_BASIC, round( max(
169 $context->getResourceLoader()->getModule( 'jquery' )->getModifiedTime( $context ),
170 $context->getResourceLoader()->getModule( 'mediawiki' )->getModifiedTime( $context )
171 ), -2 ) )
172 );
173 // Ensure uniform query order
174 ksort( $query );
175
176 // Startup function
177 $configuration = $this->getConfig( $context );
178 $registrations = self::getModuleRegistrations( $context );
179 $out .= "var startUp = function() {\n" .
180 "\t$registrations\n" .
181 "\t" . Xml::encodeJsCall( 'mediaWiki.config.set', array( $configuration ) ) .
182 "};\n";
183
184 // Conditional script injection
185 $scriptTag = Html::linkedScript( $wgLoadScript . '?' . wfArrayToCGI( $query ) );
186 $out .= "if ( isCompatible() ) {\n" .
187 "\t" . Xml::encodeJsCall( 'document.write', array( $scriptTag ) ) .
188 "}\n" .
189 "delete isCompatible;";
190 }
191
192 return $out;
193 }
194
195 public function getModifiedTime( ResourceLoaderContext $context ) {
196 global $IP, $wgCacheEpoch;
197
198 $hash = $context->getHash();
199 if ( isset( $this->modifiedTime[$hash] ) ) {
200 return $this->modifiedTime[$hash];
201 }
202
203 // Call preloadModuleInfo() on ALL modules as we're about
204 // to call getModifiedTime() on all of them
205 $loader = $context->getResourceLoader();
206 $loader->preloadModuleInfo( $loader->getModuleNames(), $context );
207
208 $this->modifiedTime[$hash] = filemtime( "$IP/resources/startup.js" );
209 // ATTENTION!: Because of the line above, this is not going to cause
210 // infinite recursion - think carefully before making changes to this
211 // code!
212 $time = wfTimestamp( TS_UNIX, $wgCacheEpoch );
213 foreach ( $loader->getModuleNames() as $name ) {
214 $module = $loader->getModule( $name );
215 $time = max( $time, $module->getModifiedTime( $context ) );
216 }
217 return $this->modifiedTime[$hash] = $time;
218 }
219
220 /**
221 * @param $context ResourceLoaderContext
222 * @return bool
223 */
224 public function getFlip( $context ) {
225 global $wgContLang;
226
227 return $wgContLang->getDir() !== $context->getDirection();
228 }
229
230 /* Methods */
231
232 public function getGroup() {
233 return 'startup';
234 }
235 }