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.
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.
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
19 * @author Trevor Parscal
20 * @author Roan Kattouw
23 class ResourceLoaderStartUpModule
extends ResourceLoaderModule
{
25 /* Protected Members */
27 protected $modifiedTime = array();
29 /* Protected Methods */
32 * @param $context ResourceLoaderContext
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, $wgLegacyJavaScriptGlobals;
43 $mainPage = Title
::newMainPage();
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.
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;
59 // Build list of variables
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 'wgActionPaths' => $wgActionPaths,
72 'wgServer' => $wgServer,
73 'wgUserLanguage' => $context->getLanguage(),
74 'wgContentLanguage' => $wgContLang->getCode(),
75 'wgVersion' => $wgVersion,
76 'wgEnableAPI' => $wgEnableAPI,
77 'wgEnableWriteAPI' => $wgEnableWriteAPI,
78 'wgDefaultDateFormat' => $wgContLang->getDefaultDateFormat(),
79 'wgMonthNames' => $wgContLang->getMonthNamesArray(),
80 'wgMonthNamesShort' => $wgContLang->getMonthAbbreviationsArray(),
81 'wgMainPageTitle' => $mainPage ?
$mainPage->getPrefixedText() : null,
82 'wgFormattedNamespaces' => $wgContLang->getFormattedNamespaces(),
83 'wgNamespaceIds' => $namespaceIds,
84 'wgSiteName' => $wgSitename,
85 'wgFileExtensions' => array_values( $wgFileExtensions ),
86 'wgDBname' => $wgDBname,
87 // This sucks, it is only needed on Special:Upload, but I could
88 // not find a way to add vars only for a certain module
89 'wgFileCanRotate' => BitmapHandler
::canRotate(),
90 'wgAvailableSkins' => Skin
::getSkinNames(),
91 'wgExtensionAssetsPath' => $wgExtensionAssetsPath,
92 // MediaWiki sets cookies to have this prefix by default
93 'wgCookiePrefix' => $wgCookiePrefix,
94 'wgResourceLoaderMaxQueryLength' => $wgResourceLoaderMaxQueryLength,
95 'wgLegacyJavaScriptGlobals' => $wgLegacyJavaScriptGlobals,
96 'wgCaseSensitiveNamespaces' => $caseSensitiveNamespaces,
98 if ( $wgUseAjax && $wgEnableMWSuggest ) {
99 $vars['wgMWSuggestTemplate'] = SearchEngine
::getMWSuggestTemplate();
102 wfRunHooks( 'ResourceLoaderGetConfigVars', array( &$vars ) );
108 * Gets registration code for all modules
110 * @param $context ResourceLoaderContext object
111 * @return String: JavaScript code for registering all modules with the client loader
113 public static function getModuleRegistrations( ResourceLoaderContext
$context ) {
114 global $wgCacheEpoch;
115 wfProfileIn( __METHOD__
);
118 $registrations = array();
119 $resourceLoader = $context->getResourceLoader();
122 $out .= ResourceLoader
::makeLoaderSourcesScript( $resourceLoader->getSources() );
125 foreach ( $resourceLoader->getModuleNames() as $name ) {
126 $module = $resourceLoader->getModule( $name );
127 // Support module loader scripts
128 $loader = $module->getLoaderScript();
129 if ( $loader !== false ) {
130 $deps = $module->getDependencies();
131 $group = $module->getGroup();
132 $source = $module->getSource();
133 $version = wfTimestamp( TS_ISO_8601_BASIC
,
134 $module->getModifiedTime( $context ) );
135 $out .= ResourceLoader
::makeCustomLoaderScript( $name, $version, $deps, $group, $source, $loader );
137 // Automatically register module
139 // getModifiedTime() is supposed to return a UNIX timestamp, but it doesn't always
140 // seem to do that, and custom implementations might forget. Coerce it to TS_UNIX
141 $moduleMtime = wfTimestamp( TS_UNIX
, $module->getModifiedTime( $context ) );
142 $mtime = max( $moduleMtime, wfTimestamp( TS_UNIX
, $wgCacheEpoch ) );
143 // Modules without dependencies, a group or a foreign source pass two arguments (name, timestamp) to
144 // mw.loader.register()
145 if ( !count( $module->getDependencies() && $module->getGroup() === null && $module->getSource() === 'local' ) ) {
146 $registrations[] = array( $name, $mtime );
148 // Modules with dependencies but no group or foreign source pass three arguments
149 // (name, timestamp, dependencies) to mw.loader.register()
150 elseif ( $module->getGroup() === null && $module->getSource() === 'local' ) {
151 $registrations[] = array(
152 $name, $mtime, $module->getDependencies() );
154 // Modules with a group but no foreign source pass four arguments (name, timestamp, dependencies, group)
155 // to mw.loader.register()
156 elseif ( $module->getSource() === 'local' ) {
157 $registrations[] = array(
158 $name, $mtime, $module->getDependencies(), $module->getGroup() );
160 // Modules with a foreign source pass five arguments (name, timestamp, dependencies, group, source)
161 // to mw.loader.register()
163 $registrations[] = array(
164 $name, $mtime, $module->getDependencies(), $module->getGroup(), $module->getSource() );
168 $out .= ResourceLoader
::makeLoaderRegisterScript( $registrations );
170 wfProfileOut( __METHOD__
);
177 * @param $context ResourceLoaderContext
180 public function getScript( ResourceLoaderContext
$context ) {
181 global $IP, $wgLoadScript, $wgLegacyJavaScriptGlobals;
183 $out = file_get_contents( "$IP/resources/startup.js" );
184 if ( $context->getOnly() === 'scripts' ) {
187 $modules = array( 'jquery', 'mediawiki' );
188 wfRunHooks( 'ResourceLoaderGetStartupModules', array( &$modules ) );
190 // Get the latest version
192 foreach ( $modules as $moduleName ) {
193 $version = max( $version,
194 $context->getResourceLoader()->getModule( $moduleName )->getModifiedTime( $context )
197 // Build load query for StartupModules
199 'modules' => ResourceLoader
::makePackedModulesString( $modules ),
201 'lang' => $context->getLanguage(),
202 'skin' => $context->getSkin(),
203 'debug' => $context->getDebug() ?
'true' : 'false',
204 'version' => wfTimestamp( TS_ISO_8601_BASIC
, $version )
206 // Ensure uniform query order
210 $configuration = $this->getConfig( $context );
211 $registrations = self
::getModuleRegistrations( $context );
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 ) ) .
218 // Conditional script injection
219 $scriptTag = Html
::linkedScript( $wgLoadScript . '?' . wfArrayToCGI( $query ) );
220 $out .= "if ( isCompatible() ) {\n" .
221 "\t" . Xml
::encodeJsCall( 'document.write', array( $scriptTag ) ) .
223 "delete isCompatible;";
232 public function supportsURLLoading() {
237 * @param $context ResourceLoaderContext
238 * @return array|mixed
240 public function getModifiedTime( ResourceLoaderContext
$context ) {
241 global $IP, $wgCacheEpoch;
243 $hash = $context->getHash();
244 if ( isset( $this->modifiedTime
[$hash] ) ) {
245 return $this->modifiedTime
[$hash];
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 );
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
257 $time = wfTimestamp( TS_UNIX
, $wgCacheEpoch );
258 foreach ( $loader->getModuleNames() as $name ) {
259 $module = $loader->getModule( $name );
260 $time = max( $time, $module->getModifiedTime( $context ) );
262 return $this->modifiedTime
[$hash] = $time;
270 public function getGroup() {