Resource loader minor changes. Fix for r73668 etc.
[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 /* Protected Members */
25
26 protected $modifiedTime = array();
27
28 /* Protected Methods */
29
30 protected function getConfig( $context ) {
31 global $wgLoadScript, $wgScript, $wgStylePath, $wgScriptExtension,
32 $wgArticlePath, $wgScriptPath, $wgServer, $wgContLang, $wgBreakFrames,
33 $wgVariantArticlePath, $wgActionPaths, $wgUseAjax, $wgVersion,
34 $wgEnableAPI, $wgEnableWriteAPI, $wgDBname, $wgEnableMWSuggest,
35 $wgSitename, $wgFileExtensions;
36
37 // Pre-process information
38 $separatorTransTable = $wgContLang->separatorTransformTable();
39 $separatorTransTable = $separatorTransTable ? $separatorTransTable : array();
40 $compactSeparatorTransTable = array(
41 implode( "\t", array_keys( $separatorTransTable ) ),
42 implode( "\t", $separatorTransTable ),
43 );
44 $digitTransTable = $wgContLang->digitTransformTable();
45 $digitTransTable = $digitTransTable ? $digitTransTable : array();
46 $compactDigitTransTable = array(
47 implode( "\t", array_keys( $digitTransTable ) ),
48 implode( "\t", $digitTransTable ),
49 );
50 $mainPage = Title::newMainPage();
51
52 // Build list of variables
53 $vars = array(
54 'wgLoadScript' => $wgLoadScript,
55 'debug' => $context->getDebug(),
56 'skin' => $context->getSkin(),
57 'stylepath' => $wgStylePath,
58 'wgUrlProtocols' => wfUrlProtocols(),
59 'wgArticlePath' => $wgArticlePath,
60 'wgScriptPath' => $wgScriptPath,
61 'wgScriptExtension' => $wgScriptExtension,
62 'wgScript' => $wgScript,
63 'wgVariantArticlePath' => $wgVariantArticlePath,
64 'wgActionPaths' => $wgActionPaths,
65 'wgServer' => $wgServer,
66 'wgUserLanguage' => $context->getLanguage(),
67 'wgContentLanguage' => $wgContLang->getCode(),
68 'wgBreakFrames' => $wgBreakFrames,
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' => $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 return $vars;
89 }
90
91 /**
92 * Gets registration code for all modules
93 *
94 * @param $context ResourceLoaderContext object
95 * @return String: JavaScript code for registering all modules with the client loader
96 */
97 public static function getModuleRegistrations( ResourceLoaderContext $context ) {
98 global $wgCacheEpoch;
99 wfProfileIn( __METHOD__ );
100
101 $out = '';
102 $registrations = array();
103 foreach ( $context->getResourceLoader()->getModules() as $name => $module ) {
104 // Support module loader scripts
105 if ( ( $loader = $module->getLoaderScript() ) !== false ) {
106 $deps = $module->getDependencies();
107 $group = $module->getGroup();
108 $version = wfTimestamp( TS_ISO_8601_BASIC,
109 round( $module->getModifiedTime( $context ), -2 ) );
110 $out .= ResourceLoader::makeCustomLoaderScript( $name, $version, $deps, $group, $loader );
111 }
112 // Automatically register module
113 else {
114 $mtime = max( $module->getModifiedTime( $context ), wfTimestamp( TS_UNIX, $wgCacheEpoch ) );
115 // Modules without dependencies or a group pass two arguments (name, timestamp) to
116 // mediaWiki.loader.register()
117 if ( !count( $module->getDependencies() && $module->getGroup() === null ) ) {
118 $registrations[] = array( $name, $mtime );
119 }
120 // Modules with dependencies but no group pass three arguments
121 // (name, timestamp, dependencies) to mediaWiki.loader.register()
122 else if ( $module->getGroup() === null ) {
123 $registrations[] = array(
124 $name, $mtime, $module->getDependencies() );
125 }
126 // Modules with dependencies pass four arguments (name, timestamp, dependencies, group)
127 // to mediaWiki.loader.register()
128 else {
129 $registrations[] = array(
130 $name, $mtime, $module->getDependencies(), $module->getGroup() );
131 }
132 }
133 }
134 $out .= ResourceLoader::makeLoaderRegisterScript( $registrations );
135
136 wfProfileOut( __METHOD__ );
137 return $out;
138 }
139
140 /* Methods */
141
142 public function getScript( ResourceLoaderContext $context ) {
143 global $IP, $wgLoadScript;
144
145 $out = file_get_contents( "$IP/resources/startup.js" );
146 if ( $context->getOnly() === 'scripts' ) {
147 // Build load query for jquery and mediawiki modules
148 $query = array(
149 'modules' => implode( '|', array( 'jquery', 'mediawiki' ) ),
150 'only' => 'scripts',
151 'lang' => $context->getLanguage(),
152 'skin' => $context->getSkin(),
153 'debug' => $context->getDebug() ? 'true' : 'false',
154 'version' => wfTimestamp( TS_ISO_8601_BASIC, round( max(
155 $context->getResourceLoader()->getModule( 'jquery' )->getModifiedTime( $context ),
156 $context->getResourceLoader()->getModule( 'mediawiki' )->getModifiedTime( $context )
157 ), -2 ) )
158 );
159 // Ensure uniform query order
160 ksort( $query );
161
162 // Startup function
163 $configuration = FormatJson::encode( $this->getConfig( $context ) );
164 $registrations = self::getModuleRegistrations( $context );
165 $out .= "var startUp = function() {\n" .
166 "\t$registrations\n" .
167 "\tmediaWiki.config.set( $configuration );" .
168 "\n};";
169
170 // Conditional script injection
171 $scriptTag = Xml::escapeJsString(
172 Html::linkedScript( $wgLoadScript . '?' . wfArrayToCGI( $query ) ) );
173 $out .= "if ( isCompatible() ) {\n" .
174 "\tdocument.write( '$scriptTag' );\n" .
175 "}\n" .
176 "delete isCompatible;";
177 }
178
179 return $out;
180 }
181
182 public function getModifiedTime( ResourceLoaderContext $context ) {
183 global $IP, $wgCacheEpoch;
184
185 $hash = $context->getHash();
186 if ( isset( $this->modifiedTime[$hash] ) ) {
187 return $this->modifiedTime[$hash];
188 }
189 $this->modifiedTime[$hash] = filemtime( "$IP/resources/startup.js" );
190
191 // ATTENTION!: Because of the line above, this is not going to cause
192 // infinite recursion - think carefully before making changes to this
193 // code!
194 $time = wfTimestamp( TS_UNIX, $wgCacheEpoch );
195 foreach ( $context->getResourceLoader()->getModules() as $module ) {
196 $time = max( $time, $module->getModifiedTime( $context ) );
197 }
198 return $this->modifiedTime[$hash] = $time;
199 }
200
201 public function getFlip( $context ) {
202 global $wgContLang;
203
204 return $wgContLang->getDir() !== $context->getDirection();
205 }
206
207 /* Methods */
208
209 public function getGroup() {
210 return 'startup';
211 }
212 }