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