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