2010-10-19 18:25:42 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
* @author Trevor Parscal
|
|
|
|
|
* @author Roan Kattouw
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
class ResourceLoaderStartUpModule extends ResourceLoaderModule {
|
2010-11-05 18:33:50 +00:00
|
|
|
|
2010-10-19 18:25:42 +00:00
|
|
|
/* Protected Members */
|
|
|
|
|
|
|
|
|
|
protected $modifiedTime = array();
|
|
|
|
|
|
|
|
|
|
/* Protected Methods */
|
|
|
|
|
|
|
|
|
|
protected function getConfig( $context ) {
|
|
|
|
|
global $wgLoadScript, $wgScript, $wgStylePath, $wgScriptExtension,
|
|
|
|
|
$wgArticlePath, $wgScriptPath, $wgServer, $wgContLang, $wgBreakFrames,
|
|
|
|
|
$wgVariantArticlePath, $wgActionPaths, $wgUseAjax, $wgVersion,
|
|
|
|
|
$wgEnableAPI, $wgEnableWriteAPI, $wgDBname, $wgEnableMWSuggest,
|
|
|
|
|
$wgSitename, $wgFileExtensions;
|
|
|
|
|
|
|
|
|
|
// Pre-process information
|
|
|
|
|
$separatorTransTable = $wgContLang->separatorTransformTable();
|
|
|
|
|
$separatorTransTable = $separatorTransTable ? $separatorTransTable : array();
|
|
|
|
|
$compactSeparatorTransTable = array(
|
|
|
|
|
implode( "\t", array_keys( $separatorTransTable ) ),
|
|
|
|
|
implode( "\t", $separatorTransTable ),
|
|
|
|
|
);
|
|
|
|
|
$digitTransTable = $wgContLang->digitTransformTable();
|
|
|
|
|
$digitTransTable = $digitTransTable ? $digitTransTable : array();
|
|
|
|
|
$compactDigitTransTable = array(
|
|
|
|
|
implode( "\t", array_keys( $digitTransTable ) ),
|
|
|
|
|
implode( "\t", $digitTransTable ),
|
|
|
|
|
);
|
|
|
|
|
$mainPage = Title::newMainPage();
|
|
|
|
|
|
|
|
|
|
// Build list of variables
|
|
|
|
|
$vars = array(
|
|
|
|
|
'wgLoadScript' => $wgLoadScript,
|
|
|
|
|
'debug' => $context->getDebug(),
|
|
|
|
|
'skin' => $context->getSkin(),
|
|
|
|
|
'stylepath' => $wgStylePath,
|
|
|
|
|
'wgUrlProtocols' => wfUrlProtocols(),
|
|
|
|
|
'wgArticlePath' => $wgArticlePath,
|
|
|
|
|
'wgScriptPath' => $wgScriptPath,
|
|
|
|
|
'wgScriptExtension' => $wgScriptExtension,
|
|
|
|
|
'wgScript' => $wgScript,
|
|
|
|
|
'wgVariantArticlePath' => $wgVariantArticlePath,
|
|
|
|
|
'wgActionPaths' => $wgActionPaths,
|
|
|
|
|
'wgServer' => $wgServer,
|
|
|
|
|
'wgUserLanguage' => $context->getLanguage(),
|
|
|
|
|
'wgContentLanguage' => $wgContLang->getCode(),
|
|
|
|
|
'wgBreakFrames' => $wgBreakFrames,
|
|
|
|
|
'wgVersion' => $wgVersion,
|
|
|
|
|
'wgEnableAPI' => $wgEnableAPI,
|
|
|
|
|
'wgEnableWriteAPI' => $wgEnableWriteAPI,
|
|
|
|
|
'wgSeparatorTransformTable' => $compactSeparatorTransTable,
|
|
|
|
|
'wgDigitTransformTable' => $compactDigitTransTable,
|
|
|
|
|
'wgMainPageTitle' => $mainPage ? $mainPage->getPrefixedText() : null,
|
|
|
|
|
'wgFormattedNamespaces' => $wgContLang->getFormattedNamespaces(),
|
|
|
|
|
'wgNamespaceIds' => $wgContLang->getNamespaceIds(),
|
|
|
|
|
'wgSiteName' => $wgSitename,
|
|
|
|
|
'wgFileExtensions' => $wgFileExtensions,
|
|
|
|
|
'wgDBname' => $wgDBname,
|
|
|
|
|
);
|
|
|
|
|
if ( $wgContLang->hasVariants() ) {
|
|
|
|
|
$vars['wgUserVariant'] = $wgContLang->getPreferredVariant();
|
|
|
|
|
}
|
|
|
|
|
if ( $wgUseAjax && $wgEnableMWSuggest ) {
|
|
|
|
|
$vars['wgMWSuggestTemplate'] = SearchEngine::getMWSuggestTemplate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $vars;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Gets registration code for all modules
|
|
|
|
|
*
|
|
|
|
|
* @param $context ResourceLoaderContext object
|
|
|
|
|
* @return String: JavaScript code for registering all modules with the client loader
|
|
|
|
|
*/
|
|
|
|
|
public static function getModuleRegistrations( ResourceLoaderContext $context ) {
|
|
|
|
|
global $wgCacheEpoch;
|
|
|
|
|
wfProfileIn( __METHOD__ );
|
|
|
|
|
|
|
|
|
|
$out = '';
|
|
|
|
|
$registrations = array();
|
|
|
|
|
foreach ( $context->getResourceLoader()->getModules() as $name => $module ) {
|
|
|
|
|
// Support module loader scripts
|
* Introduced Xml::encodeJsCall(), to replace the awkward repetitive code that was doing the same thing throughout the resource loader with varying degrees of security and correctness.
* Modified Xml::encodeJsVar() to allow it to pass through JS expressions without encoding, using a special object.
* In ResourceLoader::makeModuleResponse(), renamed $messages to $messagesBlob to make it clear that it's JSON-encoded, not an array.
* Fixed MessageBlobStore to store {} for an empty message array instead of [].
* In ResourceLoader::makeMessageSetScript(), fixed call to non-existent function mediaWiki.msg.set.
* For security, changed the calling convention of makeMessageSetScript() and makeLoaderImplementScript() to require explicit object construction of XmlJsCode() before interpreting their input as JS code.
* Documented several ResourceLoader static functions.
* In ResourceLoaderWikiModule, for readability, reduced the indenting level by flipping some if blocks and adding continue statements.
* In makeCustomLoaderScript(), allow non-numeric $version. The only caller I can find is already sending a non-numeric $version, presumably it was broken. Luckily there aren't any loader scripts in existence, I had to make one to test it.
* wfGetDb -> wfGetDB
* Added an extra line break in the startup module output, for readability.
* In ResourceLoaderStartUpModule::getModuleRegistrations(), fixed another assignment expression
2010-11-04 07:53:37 +00:00
|
|
|
$loader = $module->getLoaderScript();
|
|
|
|
|
if ( $loader !== false ) {
|
2010-10-19 18:25:42 +00:00
|
|
|
$deps = $module->getDependencies();
|
|
|
|
|
$group = $module->getGroup();
|
2010-11-03 07:58:03 +00:00
|
|
|
$version = wfTimestamp( TS_ISO_8601_BASIC,
|
|
|
|
|
round( $module->getModifiedTime( $context ), -2 ) );
|
2010-10-19 18:25:42 +00:00
|
|
|
$out .= ResourceLoader::makeCustomLoaderScript( $name, $version, $deps, $group, $loader );
|
|
|
|
|
}
|
|
|
|
|
// Automatically register module
|
|
|
|
|
else {
|
|
|
|
|
$mtime = max( $module->getModifiedTime( $context ), wfTimestamp( TS_UNIX, $wgCacheEpoch ) );
|
|
|
|
|
// Modules without dependencies or a group pass two arguments (name, timestamp) to
|
|
|
|
|
// mediaWiki.loader.register()
|
|
|
|
|
if ( !count( $module->getDependencies() && $module->getGroup() === null ) ) {
|
|
|
|
|
$registrations[] = array( $name, $mtime );
|
|
|
|
|
}
|
2010-11-03 07:58:03 +00:00
|
|
|
// Modules with dependencies but no group pass three arguments
|
|
|
|
|
// (name, timestamp, dependencies) to mediaWiki.loader.register()
|
2010-10-19 18:25:42 +00:00
|
|
|
else if ( $module->getGroup() === null ) {
|
|
|
|
|
$registrations[] = array(
|
|
|
|
|
$name, $mtime, $module->getDependencies() );
|
|
|
|
|
}
|
|
|
|
|
// Modules with dependencies pass four arguments (name, timestamp, dependencies, group)
|
|
|
|
|
// to mediaWiki.loader.register()
|
|
|
|
|
else {
|
|
|
|
|
$registrations[] = array(
|
|
|
|
|
$name, $mtime, $module->getDependencies(), $module->getGroup() );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$out .= ResourceLoader::makeLoaderRegisterScript( $registrations );
|
|
|
|
|
|
|
|
|
|
wfProfileOut( __METHOD__ );
|
|
|
|
|
return $out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
|
|
public function getScript( ResourceLoaderContext $context ) {
|
|
|
|
|
global $IP, $wgLoadScript;
|
|
|
|
|
|
|
|
|
|
$out = file_get_contents( "$IP/resources/startup.js" );
|
|
|
|
|
if ( $context->getOnly() === 'scripts' ) {
|
|
|
|
|
// Build load query for jquery and mediawiki modules
|
|
|
|
|
$query = array(
|
|
|
|
|
'modules' => implode( '|', array( 'jquery', 'mediawiki' ) ),
|
|
|
|
|
'only' => 'scripts',
|
|
|
|
|
'lang' => $context->getLanguage(),
|
|
|
|
|
'skin' => $context->getSkin(),
|
|
|
|
|
'debug' => $context->getDebug() ? 'true' : 'false',
|
|
|
|
|
'version' => wfTimestamp( TS_ISO_8601_BASIC, round( max(
|
|
|
|
|
$context->getResourceLoader()->getModule( 'jquery' )->getModifiedTime( $context ),
|
|
|
|
|
$context->getResourceLoader()->getModule( 'mediawiki' )->getModifiedTime( $context )
|
|
|
|
|
), -2 ) )
|
|
|
|
|
);
|
|
|
|
|
// Ensure uniform query order
|
|
|
|
|
ksort( $query );
|
|
|
|
|
|
|
|
|
|
// Startup function
|
* Introduced Xml::encodeJsCall(), to replace the awkward repetitive code that was doing the same thing throughout the resource loader with varying degrees of security and correctness.
* Modified Xml::encodeJsVar() to allow it to pass through JS expressions without encoding, using a special object.
* In ResourceLoader::makeModuleResponse(), renamed $messages to $messagesBlob to make it clear that it's JSON-encoded, not an array.
* Fixed MessageBlobStore to store {} for an empty message array instead of [].
* In ResourceLoader::makeMessageSetScript(), fixed call to non-existent function mediaWiki.msg.set.
* For security, changed the calling convention of makeMessageSetScript() and makeLoaderImplementScript() to require explicit object construction of XmlJsCode() before interpreting their input as JS code.
* Documented several ResourceLoader static functions.
* In ResourceLoaderWikiModule, for readability, reduced the indenting level by flipping some if blocks and adding continue statements.
* In makeCustomLoaderScript(), allow non-numeric $version. The only caller I can find is already sending a non-numeric $version, presumably it was broken. Luckily there aren't any loader scripts in existence, I had to make one to test it.
* wfGetDb -> wfGetDB
* Added an extra line break in the startup module output, for readability.
* In ResourceLoaderStartUpModule::getModuleRegistrations(), fixed another assignment expression
2010-11-04 07:53:37 +00:00
|
|
|
$configuration = $this->getConfig( $context );
|
2010-10-19 18:25:42 +00:00
|
|
|
$registrations = self::getModuleRegistrations( $context );
|
2010-11-03 07:58:03 +00:00
|
|
|
$out .= "var startUp = function() {\n" .
|
|
|
|
|
"\t$registrations\n" .
|
* Introduced Xml::encodeJsCall(), to replace the awkward repetitive code that was doing the same thing throughout the resource loader with varying degrees of security and correctness.
* Modified Xml::encodeJsVar() to allow it to pass through JS expressions without encoding, using a special object.
* In ResourceLoader::makeModuleResponse(), renamed $messages to $messagesBlob to make it clear that it's JSON-encoded, not an array.
* Fixed MessageBlobStore to store {} for an empty message array instead of [].
* In ResourceLoader::makeMessageSetScript(), fixed call to non-existent function mediaWiki.msg.set.
* For security, changed the calling convention of makeMessageSetScript() and makeLoaderImplementScript() to require explicit object construction of XmlJsCode() before interpreting their input as JS code.
* Documented several ResourceLoader static functions.
* In ResourceLoaderWikiModule, for readability, reduced the indenting level by flipping some if blocks and adding continue statements.
* In makeCustomLoaderScript(), allow non-numeric $version. The only caller I can find is already sending a non-numeric $version, presumably it was broken. Luckily there aren't any loader scripts in existence, I had to make one to test it.
* wfGetDb -> wfGetDB
* Added an extra line break in the startup module output, for readability.
* In ResourceLoaderStartUpModule::getModuleRegistrations(), fixed another assignment expression
2010-11-04 07:53:37 +00:00
|
|
|
"\t" . Xml::encodeJsCall( 'mediaWiki.config.set', array( $configuration ) ) .
|
|
|
|
|
"};\n";
|
2010-10-19 18:25:42 +00:00
|
|
|
|
|
|
|
|
// Conditional script injection
|
* Introduced Xml::encodeJsCall(), to replace the awkward repetitive code that was doing the same thing throughout the resource loader with varying degrees of security and correctness.
* Modified Xml::encodeJsVar() to allow it to pass through JS expressions without encoding, using a special object.
* In ResourceLoader::makeModuleResponse(), renamed $messages to $messagesBlob to make it clear that it's JSON-encoded, not an array.
* Fixed MessageBlobStore to store {} for an empty message array instead of [].
* In ResourceLoader::makeMessageSetScript(), fixed call to non-existent function mediaWiki.msg.set.
* For security, changed the calling convention of makeMessageSetScript() and makeLoaderImplementScript() to require explicit object construction of XmlJsCode() before interpreting their input as JS code.
* Documented several ResourceLoader static functions.
* In ResourceLoaderWikiModule, for readability, reduced the indenting level by flipping some if blocks and adding continue statements.
* In makeCustomLoaderScript(), allow non-numeric $version. The only caller I can find is already sending a non-numeric $version, presumably it was broken. Luckily there aren't any loader scripts in existence, I had to make one to test it.
* wfGetDb -> wfGetDB
* Added an extra line break in the startup module output, for readability.
* In ResourceLoaderStartUpModule::getModuleRegistrations(), fixed another assignment expression
2010-11-04 07:53:37 +00:00
|
|
|
$scriptTag = Html::linkedScript( $wgLoadScript . '?' . wfArrayToCGI( $query ) );
|
2010-11-03 07:58:03 +00:00
|
|
|
$out .= "if ( isCompatible() ) {\n" .
|
* Introduced Xml::encodeJsCall(), to replace the awkward repetitive code that was doing the same thing throughout the resource loader with varying degrees of security and correctness.
* Modified Xml::encodeJsVar() to allow it to pass through JS expressions without encoding, using a special object.
* In ResourceLoader::makeModuleResponse(), renamed $messages to $messagesBlob to make it clear that it's JSON-encoded, not an array.
* Fixed MessageBlobStore to store {} for an empty message array instead of [].
* In ResourceLoader::makeMessageSetScript(), fixed call to non-existent function mediaWiki.msg.set.
* For security, changed the calling convention of makeMessageSetScript() and makeLoaderImplementScript() to require explicit object construction of XmlJsCode() before interpreting their input as JS code.
* Documented several ResourceLoader static functions.
* In ResourceLoaderWikiModule, for readability, reduced the indenting level by flipping some if blocks and adding continue statements.
* In makeCustomLoaderScript(), allow non-numeric $version. The only caller I can find is already sending a non-numeric $version, presumably it was broken. Luckily there aren't any loader scripts in existence, I had to make one to test it.
* wfGetDb -> wfGetDB
* Added an extra line break in the startup module output, for readability.
* In ResourceLoaderStartUpModule::getModuleRegistrations(), fixed another assignment expression
2010-11-04 07:53:37 +00:00
|
|
|
"\t" . Xml::encodeJsCall( 'document.write', array( $scriptTag ) ) .
|
2010-11-03 07:58:03 +00:00
|
|
|
"}\n" .
|
|
|
|
|
"delete isCompatible;";
|
2010-10-19 18:25:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getModifiedTime( ResourceLoaderContext $context ) {
|
|
|
|
|
global $IP, $wgCacheEpoch;
|
|
|
|
|
|
|
|
|
|
$hash = $context->getHash();
|
|
|
|
|
if ( isset( $this->modifiedTime[$hash] ) ) {
|
|
|
|
|
return $this->modifiedTime[$hash];
|
|
|
|
|
}
|
|
|
|
|
$this->modifiedTime[$hash] = filemtime( "$IP/resources/startup.js" );
|
|
|
|
|
|
2010-11-03 07:58:03 +00:00
|
|
|
// ATTENTION!: Because of the line above, this is not going to cause
|
|
|
|
|
// infinite recursion - think carefully before making changes to this
|
|
|
|
|
// code!
|
2010-10-19 18:25:42 +00:00
|
|
|
$time = wfTimestamp( TS_UNIX, $wgCacheEpoch );
|
|
|
|
|
foreach ( $context->getResourceLoader()->getModules() as $module ) {
|
|
|
|
|
$time = max( $time, $module->getModifiedTime( $context ) );
|
|
|
|
|
}
|
|
|
|
|
return $this->modifiedTime[$hash] = $time;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getFlip( $context ) {
|
|
|
|
|
global $wgContLang;
|
|
|
|
|
|
|
|
|
|
return $wgContLang->getDir() !== $context->getDirection();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
|
|
public function getGroup() {
|
|
|
|
|
return 'startup';
|
|
|
|
|
}
|
|
|
|
|
}
|