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