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