Merge "Removed subclassing of PageQueryPage where not needed."
[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,
41 $wgCookiePrefix, $wgResourceLoaderMaxQueryLength;
42
43 $mainPage = Title::newMainPage();
44
45 /**
46 * Namespace related preparation
47 * - wgNamespaceIds: Key-value pairs of all localized, canonical and aliases for namespaces.
48 * - wgCaseSensitiveNamespaces: Array of namespaces that are case-sensitive.
49 */
50 $namespaceIds = $wgContLang->getNamespaceIds();
51 $caseSensitiveNamespaces = array();
52 foreach( MWNamespace::getCanonicalNamespaces() as $index => $name ) {
53 $namespaceIds[$wgContLang->lc( $name )] = $index;
54 if ( !MWNamespace::isCapitalized( $index ) ) {
55 $caseSensitiveNamespaces[] = $index;
56 }
57 }
58
59 // Build list of variables
60 $vars = array(
61 'wgLoadScript' => $wgLoadScript,
62 'debug' => $context->getDebug(),
63 'skin' => $context->getSkin(),
64 'stylepath' => $wgStylePath,
65 'wgUrlProtocols' => wfUrlProtocols(),
66 'wgArticlePath' => $wgArticlePath,
67 'wgScriptPath' => $wgScriptPath,
68 'wgScriptExtension' => $wgScriptExtension,
69 'wgScript' => $wgScript,
70 'wgVariantArticlePath' => $wgVariantArticlePath,
71 // Force object to avoid "empty" associative array from
72 // becoming [] instead of {} in JS (bug 34604)
73 'wgActionPaths' => (object)$wgActionPaths,
74 'wgServer' => $wgServer,
75 'wgUserLanguage' => $context->getLanguage(),
76 'wgContentLanguage' => $wgContLang->getCode(),
77 'wgVersion' => $wgVersion,
78 'wgEnableAPI' => $wgEnableAPI,
79 'wgEnableWriteAPI' => $wgEnableWriteAPI,
80 'wgDefaultDateFormat' => $wgContLang->getDefaultDateFormat(),
81 'wgMonthNames' => $wgContLang->getMonthNamesArray(),
82 'wgMonthNamesShort' => $wgContLang->getMonthAbbreviationsArray(),
83 'wgMainPageTitle' => $mainPage->getPrefixedText(),
84 'wgFormattedNamespaces' => $wgContLang->getFormattedNamespaces(),
85 'wgNamespaceIds' => $namespaceIds,
86 'wgSiteName' => $wgSitename,
87 'wgFileExtensions' => array_values( $wgFileExtensions ),
88 'wgDBname' => $wgDBname,
89 // This sucks, it is only needed on Special:Upload, but I could
90 // not find a way to add vars only for a certain module
91 'wgFileCanRotate' => BitmapHandler::canRotate(),
92 'wgAvailableSkins' => Skin::getSkinNames(),
93 'wgExtensionAssetsPath' => $wgExtensionAssetsPath,
94 // MediaWiki sets cookies to have this prefix by default
95 'wgCookiePrefix' => $wgCookiePrefix,
96 'wgResourceLoaderMaxQueryLength' => $wgResourceLoaderMaxQueryLength,
97 'wgCaseSensitiveNamespaces' => $caseSensitiveNamespaces,
98 );
99 if ( $wgUseAjax && $wgEnableMWSuggest ) {
100 $vars['wgMWSuggestTemplate'] = SearchEngine::getMWSuggestTemplate();
101 }
102
103 wfRunHooks( 'ResourceLoaderGetConfigVars', array( &$vars ) );
104
105 return $vars;
106 }
107
108 /**
109 * Gets registration code for all modules
110 *
111 * @param $context ResourceLoaderContext object
112 * @return String: JavaScript code for registering all modules with the client loader
113 */
114 public static function getModuleRegistrations( ResourceLoaderContext $context ) {
115 global $wgCacheEpoch;
116 wfProfileIn( __METHOD__ );
117
118 $out = '';
119 $registrations = array();
120 $resourceLoader = $context->getResourceLoader();
121
122 // Register sources
123 $out .= ResourceLoader::makeLoaderSourcesScript( $resourceLoader->getSources() );
124
125 // Register modules
126 foreach ( $resourceLoader->getModuleNames() as $name ) {
127 $module = $resourceLoader->getModule( $name );
128 $deps = $module->getDependencies();
129 $group = $module->getGroup();
130 $source = $module->getSource();
131 // Support module loader scripts
132 $loader = $module->getLoaderScript();
133 if ( $loader !== false ) {
134 $version = wfTimestamp( TS_ISO_8601_BASIC,
135 $module->getModifiedTime( $context ) );
136 $out .= ResourceLoader::makeCustomLoaderScript( $name, $version, $deps, $group, $source, $loader );
137 }
138 // Automatically register module
139 else {
140 // getModifiedTime() is supposed to return a UNIX timestamp, but it doesn't always
141 // seem to do that, and custom implementations might forget. Coerce it to TS_UNIX
142 $moduleMtime = wfTimestamp( TS_UNIX, $module->getModifiedTime( $context ) );
143 $mtime = max( $moduleMtime, wfTimestamp( TS_UNIX, $wgCacheEpoch ) );
144 // Modules without dependencies, a group or a foreign source pass two arguments (name, timestamp) to
145 // mw.loader.register()
146 if ( !count( $deps ) && $group === null && $source === 'local' ) {
147 $registrations[] = array( $name, $mtime );
148 }
149 // Modules with dependencies but no group or foreign source pass three arguments
150 // (name, timestamp, dependencies) to mw.loader.register()
151 elseif ( $group === null && $source === 'local' ) {
152 $registrations[] = array( $name, $mtime, $deps );
153 }
154 // Modules with a group but no foreign source pass four arguments (name, timestamp, dependencies, group)
155 // to mw.loader.register()
156 elseif ( $source === 'local' ) {
157 $registrations[] = array( $name, $mtime, $deps, $group );
158 }
159 // Modules with a foreign source pass five arguments (name, timestamp, dependencies, group, source)
160 // to mw.loader.register()
161 else {
162 $registrations[] = array( $name, $mtime, $deps, $group, $source );
163 }
164 }
165 }
166 $out .= ResourceLoader::makeLoaderRegisterScript( $registrations );
167
168 wfProfileOut( __METHOD__ );
169 return $out;
170 }
171
172 /* Methods */
173
174 /**
175 * @param $context ResourceLoaderContext
176 * @return string
177 */
178 public function getScript( ResourceLoaderContext $context ) {
179 global $IP, $wgLoadScript, $wgLegacyJavaScriptGlobals;
180
181 $out = file_get_contents( "$IP/resources/startup.js" );
182 if ( $context->getOnly() === 'scripts' ) {
183
184 // The core modules:
185 $moduleNames = array( 'jquery', 'mediawiki' );
186 wfRunHooks( 'ResourceLoaderGetStartupModules', array( &$moduleNames ) );
187
188 // Get the latest version
189 $loader = $context->getResourceLoader();
190 $version = 0;
191 foreach ( $moduleNames as $moduleName ) {
192 $version = max( $version,
193 $loader->getModule( $moduleName )->getModifiedTime( $context )
194 );
195 }
196 // Build load query for StartupModules
197 $query = array(
198 'modules' => ResourceLoader::makePackedModulesString( $moduleNames ),
199 'only' => 'scripts',
200 'lang' => $context->getLanguage(),
201 'skin' => $context->getSkin(),
202 'debug' => $context->getDebug() ? 'true' : 'false',
203 'version' => wfTimestamp( TS_ISO_8601_BASIC, $version )
204 );
205 // Ensure uniform query order
206 ksort( $query );
207
208 // Startup function
209 $configuration = $this->getConfig( $context );
210 $registrations = self::getModuleRegistrations( $context );
211 $registrations = str_replace( "\n", "\n\t", trim( $registrations ) ); // fix indentation
212 $out .= "var startUp = function() {\n" .
213 "\tmw.config = new " . Xml::encodeJsCall( 'mw.Map', array( $wgLegacyJavaScriptGlobals ) ) . "\n" .
214 "\t$registrations\n" .
215 "\t" . Xml::encodeJsCall( 'mw.config.set', array( $configuration ) ) .
216 "};\n";
217
218 // Conditional script injection
219 $scriptTag = Html::linkedScript( $wgLoadScript . '?' . wfArrayToCGI( $query ) );
220 $out .= "if ( isCompatible() ) {\n" .
221 "\t" . Xml::encodeJsCall( 'document.write', array( $scriptTag ) ) .
222 "}\n" .
223 "delete isCompatible;";
224 }
225
226 return $out;
227 }
228
229 /**
230 * @return bool
231 */
232 public function supportsURLLoading() {
233 return false;
234 }
235
236 /**
237 * @param $context ResourceLoaderContext
238 * @return array|mixed
239 */
240 public function getModifiedTime( ResourceLoaderContext $context ) {
241 global $IP, $wgCacheEpoch;
242
243 $hash = $context->getHash();
244 if ( isset( $this->modifiedTime[$hash] ) ) {
245 return $this->modifiedTime[$hash];
246 }
247
248 // Call preloadModuleInfo() on ALL modules as we're about
249 // to call getModifiedTime() on all of them
250 $loader = $context->getResourceLoader();
251 $loader->preloadModuleInfo( $loader->getModuleNames(), $context );
252
253 $this->modifiedTime[$hash] = filemtime( "$IP/resources/startup.js" );
254 // ATTENTION!: Because of the line above, this is not going to cause
255 // infinite recursion - think carefully before making changes to this
256 // code!
257 $time = wfTimestamp( TS_UNIX, $wgCacheEpoch );
258 foreach ( $loader->getModuleNames() as $name ) {
259 $module = $loader->getModule( $name );
260 $time = max( $time, $module->getModifiedTime( $context ) );
261 }
262 return $this->modifiedTime[$hash] = $time;
263 }
264
265 /* Methods */
266
267 /**
268 * @return string
269 */
270 public function getGroup() {
271 return 'startup';
272 }
273 }