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