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