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