Fixed Doxygen incompatible JSDoc style comments (bad Trevor!) as per some comments...
[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 foreach ( $context->getResourceLoader()->getModules() as $name => $module ) {
105 // Support module loader scripts
106 $loader = $module->getLoaderScript();
107 if ( $loader !== false ) {
108 $deps = $module->getDependencies();
109 $group = $module->getGroup();
110 $version = wfTimestamp( TS_ISO_8601_BASIC,
111 round( $module->getModifiedTime( $context ), -2 ) );
112 $out .= ResourceLoader::makeCustomLoaderScript( $name, $version, $deps, $group, $loader );
113 }
114 // Automatically register module
115 else {
116 $mtime = max( $module->getModifiedTime( $context ), wfTimestamp( TS_UNIX, $wgCacheEpoch ) );
117 // Modules without dependencies or a group pass two arguments (name, timestamp) to
118 // mediaWiki.loader.register()
119 if ( !count( $module->getDependencies() && $module->getGroup() === null ) ) {
120 $registrations[] = array( $name, $mtime );
121 }
122 // Modules with dependencies but no group pass three arguments
123 // (name, timestamp, dependencies) to mediaWiki.loader.register()
124 else if ( $module->getGroup() === null ) {
125 $registrations[] = array(
126 $name, $mtime, $module->getDependencies() );
127 }
128 // Modules with dependencies pass four arguments (name, timestamp, dependencies, group)
129 // to mediaWiki.loader.register()
130 else {
131 $registrations[] = array(
132 $name, $mtime, $module->getDependencies(), $module->getGroup() );
133 }
134 }
135 }
136 $out .= ResourceLoader::makeLoaderRegisterScript( $registrations );
137
138 wfProfileOut( __METHOD__ );
139 return $out;
140 }
141
142 /* Methods */
143
144 public function getScript( ResourceLoaderContext $context ) {
145 global $IP, $wgLoadScript;
146
147 $out = file_get_contents( "$IP/resources/startup.js" );
148 if ( $context->getOnly() === 'scripts' ) {
149 // Build load query for jquery and mediawiki modules
150 $query = array(
151 'modules' => implode( '|', array( 'jquery', 'mediawiki' ) ),
152 'only' => 'scripts',
153 'lang' => $context->getLanguage(),
154 'skin' => $context->getSkin(),
155 'debug' => $context->getDebug() ? 'true' : 'false',
156 'version' => wfTimestamp( TS_ISO_8601_BASIC, round( max(
157 $context->getResourceLoader()->getModule( 'jquery' )->getModifiedTime( $context ),
158 $context->getResourceLoader()->getModule( 'mediawiki' )->getModifiedTime( $context )
159 ), -2 ) )
160 );
161 // Ensure uniform query order
162 ksort( $query );
163
164 // Startup function
165 $configuration = $this->getConfig( $context );
166 $registrations = self::getModuleRegistrations( $context );
167 $out .= "var startUp = function() {\n" .
168 "\t$registrations\n" .
169 "\t" . Xml::encodeJsCall( 'mediaWiki.config.set', array( $configuration ) ) .
170 "};\n";
171
172 // Conditional script injection
173 $scriptTag = Html::linkedScript( $wgLoadScript . '?' . wfArrayToCGI( $query ) );
174 $out .= "if ( isCompatible() ) {\n" .
175 "\t" . Xml::encodeJsCall( 'document.write', array( $scriptTag ) ) .
176 "}\n" .
177 "delete isCompatible;";
178 }
179
180 return $out;
181 }
182
183 public function getModifiedTime( ResourceLoaderContext $context ) {
184 global $IP, $wgCacheEpoch;
185
186 $hash = $context->getHash();
187 if ( isset( $this->modifiedTime[$hash] ) ) {
188 return $this->modifiedTime[$hash];
189 }
190 $this->modifiedTime[$hash] = filemtime( "$IP/resources/startup.js" );
191
192 // ATTENTION!: Because of the line above, this is not going to cause
193 // infinite recursion - think carefully before making changes to this
194 // code!
195 $time = wfTimestamp( TS_UNIX, $wgCacheEpoch );
196 foreach ( $context->getResourceLoader()->getModules() as $module ) {
197 $time = max( $time, $module->getModifiedTime( $context ) );
198 }
199 return $this->modifiedTime[$hash] = $time;
200 }
201
202 public function getFlip( $context ) {
203 global $wgContLang;
204
205 return $wgContLang->getDir() !== $context->getDirection();
206 }
207
208 /* Methods */
209
210 public function getGroup() {
211 return 'startup';
212 }
213 }