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 */
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;
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 ),
45 $digitTransTable = $wgContLang->digitTransformTable();
46 $digitTransTable = $digitTransTable ?
$digitTransTable : array();
47 $compactDigitTransTable = array(
48 implode( "\t", array_keys( $digitTransTable ) ),
49 implode( "\t", $digitTransTable ),
51 $mainPage = Title
::newMainPage();
53 // Build list of variables
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,
82 if ( $wgContLang->hasVariants() ) {
83 $vars['wgUserVariant'] = $wgContLang->getPreferredVariant();
85 if ( $wgUseAjax && $wgEnableMWSuggest ) {
86 $vars['wgMWSuggestTemplate'] = SearchEngine
::getMWSuggestTemplate();
93 * Gets registration code for all modules
95 * @param $context ResourceLoaderContext object
96 * @return String: JavaScript code for registering all modules with the client loader
98 public static function getModuleRegistrations( ResourceLoaderContext
$context ) {
100 wfProfileIn( __METHOD__
);
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 );
114 // Automatically register module
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 );
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() );
128 // Modules with dependencies pass four arguments (name, timestamp, dependencies, group)
129 // to mediaWiki.loader.register()
131 $registrations[] = array(
132 $name, $mtime, $module->getDependencies(), $module->getGroup() );
136 $out .= ResourceLoader
::makeLoaderRegisterScript( $registrations );
138 wfProfileOut( __METHOD__
);
144 public function getScript( ResourceLoaderContext
$context ) {
145 global $IP, $wgLoadScript;
147 $out = file_get_contents( "$IP/resources/startup.js" );
148 if ( $context->getOnly() === 'scripts' ) {
149 // Build load query for jquery and mediawiki modules
151 'modules' => implode( '|', array( 'jquery', 'mediawiki' ) ),
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 )
161 // Ensure uniform query order
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 ) ) .
172 // Conditional script injection
173 $scriptTag = Html
::linkedScript( $wgLoadScript . '?' . wfArrayToCGI( $query ) );
174 $out .= "if ( isCompatible() ) {\n" .
175 "\t" . Xml
::encodeJsCall( 'document.write', array( $scriptTag ) ) .
177 "delete isCompatible;";
183 public function getModifiedTime( ResourceLoaderContext
$context ) {
184 global $IP, $wgCacheEpoch;
186 $hash = $context->getHash();
187 if ( isset( $this->modifiedTime
[$hash] ) ) {
188 return $this->modifiedTime
[$hash];
190 $this->modifiedTime
[$hash] = filemtime( "$IP/resources/startup.js" );
192 // ATTENTION!: Because of the line above, this is not going to cause
193 // infinite recursion - think carefully before making changes to this
195 $time = wfTimestamp( TS_UNIX
, $wgCacheEpoch );
196 foreach ( $context->getResourceLoader()->getModules() as $module ) {
197 $time = max( $time, $module->getModifiedTime( $context ) );
199 return $this->modifiedTime
[$hash] = $time;
202 public function getFlip( $context ) {
205 return $wgContLang->getDir() !== $context->getDirection();
210 public function getGroup() {