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 $loader = $module->getLoaderScript();
106 if ( $loader !== false ) {
107 $deps = $module->getDependencies();
108 $group = $module->getGroup();
109 $version = wfTimestamp( TS_ISO_8601_BASIC
,
110 round( $module->getModifiedTime( $context ), -2 ) );
111 $out .= ResourceLoader
::makeCustomLoaderScript( $name, $version, $deps, $group, $loader );
113 // Automatically register module
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 );
121 // Modules with dependencies but no group pass three arguments
122 // (name, timestamp, dependencies) to mediaWiki.loader.register()
123 else if ( $module->getGroup() === null ) {
124 $registrations[] = array(
125 $name, $mtime, $module->getDependencies() );
127 // Modules with dependencies pass four arguments (name, timestamp, dependencies, group)
128 // to mediaWiki.loader.register()
130 $registrations[] = array(
131 $name, $mtime, $module->getDependencies(), $module->getGroup() );
135 $out .= ResourceLoader
::makeLoaderRegisterScript( $registrations );
137 wfProfileOut( __METHOD__
);
143 public function getScript( ResourceLoaderContext
$context ) {
144 global $IP, $wgLoadScript;
146 $out = file_get_contents( "$IP/resources/startup.js" );
147 if ( $context->getOnly() === 'scripts' ) {
148 // Build load query for jquery and mediawiki modules
150 'modules' => implode( '|', array( 'jquery', 'mediawiki' ) ),
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 )
160 // Ensure uniform query order
164 $configuration = $this->getConfig( $context );
165 $registrations = self
::getModuleRegistrations( $context );
166 $out .= "var startUp = function() {\n" .
167 "\t$registrations\n" .
168 "\t" . Xml
::encodeJsCall( 'mediaWiki.config.set', array( $configuration ) ) .
171 // Conditional script injection
172 $scriptTag = Html
::linkedScript( $wgLoadScript . '?' . wfArrayToCGI( $query ) );
173 $out .= "if ( isCompatible() ) {\n" .
174 "\t" . Xml
::encodeJsCall( 'document.write', array( $scriptTag ) ) .
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() {