Followup r74035, add GAID_FOR_UPDATE to the defines for back-compat
[mediawiki.git] / includes / resourceloader / ResourceLoaderStartUpModule.php
blob2f52f427006e5e59a7ee7628ab980676b8148c2d
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.
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
18 * @file
19 * @author Trevor Parscal
20 * @author Roan Kattouw
23 defined( 'MEDIAWIKI' ) || die( 1 );
25 class ResourceLoaderStartUpModule extends ResourceLoaderModule {
26 /* Protected Members */
28 protected $modifiedTime = array();
30 /* Protected Methods */
32 protected function getConfig( $context ) {
33 global $wgLoadScript, $wgScript, $wgStylePath, $wgScriptExtension,
34 $wgArticlePath, $wgScriptPath, $wgServer, $wgContLang, $wgBreakFrames,
35 $wgVariantArticlePath, $wgActionPaths, $wgUseAjax, $wgVersion,
36 $wgEnableAPI, $wgEnableWriteAPI, $wgDBname, $wgEnableMWSuggest,
37 $wgSitename, $wgFileExtensions;
39 // Pre-process information
40 $separatorTransTable = $wgContLang->separatorTransformTable();
41 $separatorTransTable = $separatorTransTable ? $separatorTransTable : array();
42 $compactSeparatorTransTable = array(
43 implode( "\t", array_keys( $separatorTransTable ) ),
44 implode( "\t", $separatorTransTable ),
46 $digitTransTable = $wgContLang->digitTransformTable();
47 $digitTransTable = $digitTransTable ? $digitTransTable : array();
48 $compactDigitTransTable = array(
49 implode( "\t", array_keys( $digitTransTable ) ),
50 implode( "\t", $digitTransTable ),
52 $mainPage = Title::newMainPage();
54 // Build list of variables
55 $vars = array(
56 'wgLoadScript' => $wgLoadScript,
57 'debug' => $context->getDebug(),
58 'skin' => $context->getSkin(),
59 'stylepath' => $wgStylePath,
60 'wgUrlProtocols' => wfUrlProtocols(),
61 'wgArticlePath' => $wgArticlePath,
62 'wgScriptPath' => $wgScriptPath,
63 'wgScriptExtension' => $wgScriptExtension,
64 'wgScript' => $wgScript,
65 'wgVariantArticlePath' => $wgVariantArticlePath,
66 'wgActionPaths' => $wgActionPaths,
67 'wgServer' => $wgServer,
68 'wgUserLanguage' => $context->getLanguage(),
69 'wgContentLanguage' => $wgContLang->getCode(),
70 'wgBreakFrames' => $wgBreakFrames,
71 'wgVersion' => $wgVersion,
72 'wgEnableAPI' => $wgEnableAPI,
73 'wgEnableWriteAPI' => $wgEnableWriteAPI,
74 'wgSeparatorTransformTable' => $compactSeparatorTransTable,
75 'wgDigitTransformTable' => $compactDigitTransTable,
76 'wgMainPageTitle' => $mainPage ? $mainPage->getPrefixedText() : null,
77 'wgFormattedNamespaces' => $wgContLang->getFormattedNamespaces(),
78 'wgNamespaceIds' => $wgContLang->getNamespaceIds(),
79 'wgSiteName' => $wgSitename,
80 'wgFileExtensions' => $wgFileExtensions,
81 'wgDBname' => $wgDBname,
83 if ( $wgContLang->hasVariants() ) {
84 $vars['wgUserVariant'] = $wgContLang->getPreferredVariant();
86 if ( $wgUseAjax && $wgEnableMWSuggest ) {
87 $vars['wgMWSuggestTemplate'] = SearchEngine::getMWSuggestTemplate();
90 return $vars;
93 /**
94 * Gets registration code for all modules
96 * @param $context ResourceLoaderContext object
97 * @return String: JavaScript code for registering all modules with the client loader
99 public static function getModuleRegistrations( ResourceLoaderContext $context ) {
100 global $wgCacheEpoch;
101 wfProfileIn( __METHOD__ );
103 $out = '';
104 $registrations = array();
105 foreach ( $context->getResourceLoader()->getModules() as $name => $module ) {
106 // Support module loader scripts
107 if ( ( $loader = $module->getLoaderScript() ) !== false ) {
108 $deps = $module->getDependencies();
109 $group = $module->getGroup();
110 $version = wfTimestamp( TS_ISO_8601_BASIC, round( $module->getModifiedTime( $context ), -2 ) );
111 $out .= ResourceLoader::makeCustomLoaderScript( $name, $version, $deps, $group, $loader );
113 // Automatically register module
114 else {
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 (name, timestamp, dependencies)
122 // 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()
129 else {
130 $registrations[] = array(
131 $name, $mtime, $module->getDependencies(), $module->getGroup() );
135 $out .= ResourceLoader::makeLoaderRegisterScript( $registrations );
137 wfProfileOut( __METHOD__ );
138 return $out;
141 /* Methods */
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
149 $query = array(
150 'modules' => implode( '|', array( 'jquery', 'mediawiki' ) ),
151 'only' => 'scripts',
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 )
158 ), -2 ) )
160 // Ensure uniform query order
161 ksort( $query );
163 // Startup function
164 $configuration = FormatJson::encode( $this->getConfig( $context ) );
165 $registrations = self::getModuleRegistrations( $context );
166 $out .= "var startUp = function() {\n\t$registrations\n\tmediaWiki.config.set( $configuration );\n};";
168 // Conditional script injection
169 $scriptTag = Xml::escapeJsString( Html::linkedScript( $wgLoadScript . '?' . wfArrayToCGI( $query ) ) );
170 $out .= "if ( isCompatible() ) {\n\tdocument.write( '$scriptTag' );\n}\ndelete isCompatible;";
173 return $out;
176 public function getModifiedTime( ResourceLoaderContext $context ) {
177 global $IP, $wgCacheEpoch;
179 $hash = $context->getHash();
180 if ( isset( $this->modifiedTime[$hash] ) ) {
181 return $this->modifiedTime[$hash];
183 $this->modifiedTime[$hash] = filemtime( "$IP/resources/startup.js" );
185 // ATTENTION!: Because of the line above, this is not going to cause infinite recursion - think carefully
186 // before making changes to this code!
187 $time = wfTimestamp( TS_UNIX, $wgCacheEpoch );
188 foreach ( $context->getResourceLoader()->getModules() as $module ) {
189 $time = max( $time, $module->getModifiedTime( $context ) );
191 return $this->modifiedTime[$hash] = $time;
194 public function getFlip( $context ) {
195 global $wgContLang;
197 return $wgContLang->getDir() !== $context->getDirection();
200 /* Methods */
202 public function getGroup() {
203 return 'startup';