2010-10-19 18:25:42 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
2015-10-28 03:24:40 +00:00
|
|
|
* Abstraction for ResourceLoader modules that pull from wiki pages.
|
2012-04-30 07:16:10 +00:00
|
|
|
*
|
2010-10-19 18:25:42 +00:00
|
|
|
* 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
|
|
|
|
|
*/
|
|
|
|
|
|
resourceloader: Improve titleInfo docs and simplify title key
* Document the structure of the in-process $titleInfo cache.
Specifically, specify that it is not the value from getTitleInfo(),
but rather a container for zero or more versions of such values.
The reason this is fragmented is because ResourceLoaderContext
is a parameter to most methods and as such, makes everything
variable. Tracked as T99107.
* Make various bits easier to understand by consistently refering
to the container keys as "batchKey", and referring to the internal
keys as "titleKey".
* Centralise title key logic by moving to private method.
* Replace the internal creation of titleKey to be based on LinkTarget
with plain namespace IDs and db keys, instead of invoking the
expensive getPrefixedTitle function which involves quite a lot
of overhead (TitleCodec, GenderCache, Database, Language,
LocalisationCache, ..).
Change-Id: I701e5156ef7815a0e36caefae5871524eff3f688
2018-04-10 17:29:50 +00:00
|
|
|
use MediaWiki\Linker\LinkTarget;
|
2017-02-07 04:49:57 +00:00
|
|
|
use Wikimedia\Rdbms\Database;
|
2017-02-10 18:09:05 +00:00
|
|
|
use Wikimedia\Rdbms\IDatabase;
|
|
|
|
|
|
2010-10-19 18:25:42 +00:00
|
|
|
/**
|
2015-10-28 03:24:40 +00:00
|
|
|
* Abstraction for ResourceLoader modules which pull from wiki pages
|
2011-10-14 08:06:54 +00:00
|
|
|
*
|
|
|
|
|
* This can only be used for wiki pages in the MediaWiki and User namespaces,
|
2018-02-13 00:15:30 +00:00
|
|
|
* because of its dependence on the functionality of Title::isUserConfigPage()
|
|
|
|
|
* and Title::isSiteConfigPage().
|
2015-06-04 01:52:42 +00:00
|
|
|
*
|
|
|
|
|
* This module supports being used as a placeholder for a module on a remote wiki.
|
|
|
|
|
* To do so, getDB() must be overloaded to return a foreign database object that
|
|
|
|
|
* allows local wikis to query page metadata.
|
|
|
|
|
*
|
|
|
|
|
* Safe for calls on local wikis are:
|
|
|
|
|
* - Option getters:
|
|
|
|
|
* - getGroup()
|
|
|
|
|
* - getPages()
|
|
|
|
|
* - Basic methods that strictly involve the foreign database
|
|
|
|
|
* - getDB()
|
|
|
|
|
* - isKnownEmpty()
|
|
|
|
|
* - getTitleInfo()
|
2010-10-19 18:25:42 +00:00
|
|
|
*/
|
2014-10-21 00:43:26 +00:00
|
|
|
class ResourceLoaderWikiModule extends ResourceLoaderModule {
|
2011-10-14 08:06:54 +00:00
|
|
|
|
2014-10-21 00:43:26 +00:00
|
|
|
// Origin defaults to users with sitewide authority
|
2011-02-04 16:39:17 +00:00
|
|
|
protected $origin = self::ORIGIN_USER_SITEWIDE;
|
2011-10-14 08:06:54 +00:00
|
|
|
|
resourceloader: Improve titleInfo docs and simplify title key
* Document the structure of the in-process $titleInfo cache.
Specifically, specify that it is not the value from getTitleInfo(),
but rather a container for zero or more versions of such values.
The reason this is fragmented is because ResourceLoaderContext
is a parameter to most methods and as such, makes everything
variable. Tracked as T99107.
* Make various bits easier to understand by consistently refering
to the container keys as "batchKey", and referring to the internal
keys as "titleKey".
* Centralise title key logic by moving to private method.
* Replace the internal creation of titleKey to be based on LinkTarget
with plain namespace IDs and db keys, instead of invoking the
expensive getPrefixedTitle function which involves quite a lot
of overhead (TitleCodec, GenderCache, Database, Language,
LocalisationCache, ..).
Change-Id: I701e5156ef7815a0e36caefae5871524eff3f688
2018-04-10 17:29:50 +00:00
|
|
|
// In-process cache for title info, structured as an array
|
|
|
|
|
// [
|
|
|
|
|
// <batchKey> // Pipe-separated list of sorted keys from getPages
|
|
|
|
|
// => [
|
|
|
|
|
// <titleKey> => [ // Normalised title key
|
|
|
|
|
// 'page_len' => ..,
|
|
|
|
|
// 'page_latest' => ..,
|
|
|
|
|
// 'page_touched' => ..,
|
|
|
|
|
// ]
|
|
|
|
|
// ]
|
|
|
|
|
// ]
|
|
|
|
|
// @see self::fetchTitleInfo()
|
|
|
|
|
// @see self::makeTitleKey()
|
2016-02-17 09:09:32 +00:00
|
|
|
protected $titleInfo = [];
|
2011-10-14 08:06:54 +00:00
|
|
|
|
2014-10-21 00:43:26 +00:00
|
|
|
// List of page names that contain CSS
|
2016-02-17 09:09:32 +00:00
|
|
|
protected $styles = [];
|
2014-10-21 00:43:26 +00:00
|
|
|
|
|
|
|
|
// List of page names that contain JavaScript
|
2016-02-17 09:09:32 +00:00
|
|
|
protected $scripts = [];
|
2014-10-21 00:43:26 +00:00
|
|
|
|
|
|
|
|
// Group of module
|
|
|
|
|
protected $group;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param array $options For back-compat, this can be omitted in favour of overwriting getPages.
|
|
|
|
|
*/
|
|
|
|
|
public function __construct( array $options = null ) {
|
2015-05-29 20:00:17 +00:00
|
|
|
if ( is_null( $options ) ) {
|
|
|
|
|
return;
|
2014-10-21 00:43:26 +00:00
|
|
|
}
|
2015-05-29 20:00:17 +00:00
|
|
|
|
|
|
|
|
foreach ( $options as $member => $option ) {
|
|
|
|
|
switch ( $member ) {
|
|
|
|
|
case 'styles':
|
|
|
|
|
case 'scripts':
|
|
|
|
|
case 'group':
|
2016-05-02 18:32:20 +00:00
|
|
|
case 'targets':
|
2015-05-29 20:00:17 +00:00
|
|
|
$this->{$member} = $option;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2014-10-21 00:43:26 +00:00
|
|
|
}
|
|
|
|
|
}
|
2011-10-14 08:06:54 +00:00
|
|
|
|
|
|
|
|
/**
|
2012-12-18 08:31:59 +00:00
|
|
|
* Subclasses should return an associative array of resources in the module.
|
|
|
|
|
* Keys should be the title of a page in the MediaWiki or User namespace.
|
|
|
|
|
*
|
|
|
|
|
* Values should be a nested array of options. The supported keys are 'type' and
|
|
|
|
|
* (CSS only) 'media'.
|
|
|
|
|
*
|
|
|
|
|
* For scripts, 'type' should be 'script'.
|
|
|
|
|
*
|
|
|
|
|
* For stylesheets, 'type' should be 'style'.
|
|
|
|
|
* There is an optional media key, the value of which can be the
|
|
|
|
|
* medium ('screen', 'print', etc.) of the stylesheet.
|
|
|
|
|
*
|
2014-04-20 21:33:05 +00:00
|
|
|
* @param ResourceLoaderContext $context
|
2012-12-18 08:31:59 +00:00
|
|
|
* @return array
|
2011-10-14 08:06:54 +00:00
|
|
|
*/
|
2014-10-21 00:43:26 +00:00
|
|
|
protected function getPages( ResourceLoaderContext $context ) {
|
|
|
|
|
$config = $this->getConfig();
|
2016-02-17 09:09:32 +00:00
|
|
|
$pages = [];
|
2014-10-21 00:43:26 +00:00
|
|
|
|
|
|
|
|
// Filter out pages from origins not allowed by the current wiki configuration.
|
|
|
|
|
if ( $config->get( 'UseSiteJs' ) ) {
|
|
|
|
|
foreach ( $this->scripts as $script ) {
|
2016-06-09 19:27:34 +00:00
|
|
|
$pages[$script] = [ 'type' => 'script' ];
|
2014-10-21 00:43:26 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $config->get( 'UseSiteCss' ) ) {
|
|
|
|
|
foreach ( $this->styles as $style ) {
|
2016-06-09 19:27:34 +00:00
|
|
|
$pages[$style] = [ 'type' => 'style' ];
|
2014-10-21 00:43:26 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $pages;
|
|
|
|
|
}
|
2011-10-14 08:06:54 +00:00
|
|
|
|
2014-10-21 00:43:26 +00:00
|
|
|
/**
|
|
|
|
|
* Get group name
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getGroup() {
|
|
|
|
|
return $this->group;
|
|
|
|
|
}
|
2011-10-14 08:06:54 +00:00
|
|
|
|
2011-08-02 15:47:30 +00:00
|
|
|
/**
|
2015-06-04 01:52:42 +00:00
|
|
|
* Get the Database object used in getTitleInfo().
|
|
|
|
|
*
|
2016-09-05 20:21:26 +00:00
|
|
|
* Defaults to the local replica DB. Subclasses may want to override this to return a foreign
|
2015-06-04 01:52:42 +00:00
|
|
|
* database object, or null if getTitleInfo() shouldn't access the database.
|
2011-10-14 08:06:54 +00:00
|
|
|
*
|
2015-06-04 01:52:42 +00:00
|
|
|
* NOTE: This ONLY works for getTitleInfo() and isKnownEmpty(), NOT FOR ANYTHING ELSE.
|
|
|
|
|
* In particular, it doesn't work for getContent() or getScript() etc.
|
2011-10-14 08:06:54 +00:00
|
|
|
*
|
2014-11-03 17:45:27 +00:00
|
|
|
* @return IDatabase|null
|
2011-08-02 15:47:30 +00:00
|
|
|
*/
|
|
|
|
|
protected function getDB() {
|
2016-09-05 19:55:19 +00:00
|
|
|
return wfGetDB( DB_REPLICA );
|
2011-08-02 15:47:30 +00:00
|
|
|
}
|
2011-02-08 23:09:22 +00:00
|
|
|
|
|
|
|
|
/**
|
2016-09-02 08:28:23 +00:00
|
|
|
* @param string $titleText
|
2017-02-28 20:52:17 +00:00
|
|
|
* @param ResourceLoaderContext|null $context (but passing null is deprecated)
|
2011-02-08 23:09:22 +00:00
|
|
|
* @return null|string
|
2017-02-28 20:52:17 +00:00
|
|
|
* @since 1.32 added the $context parameter
|
2011-02-08 23:09:22 +00:00
|
|
|
*/
|
2017-02-28 20:52:17 +00:00
|
|
|
protected function getContent( $titleText, ResourceLoaderContext $context = null ) {
|
2015-06-04 01:52:42 +00:00
|
|
|
$title = Title::newFromText( $titleText );
|
2014-09-23 22:41:03 +00:00
|
|
|
if ( !$title ) {
|
2017-04-01 01:35:09 +00:00
|
|
|
return null; // Bad title
|
2015-06-04 01:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
2017-02-28 20:52:17 +00:00
|
|
|
$content = $this->getContentObj( $title, $context );
|
|
|
|
|
if ( !$content ) {
|
|
|
|
|
return null; // No content found
|
2016-02-11 04:57:03 +00:00
|
|
|
}
|
|
|
|
|
|
2017-02-28 20:52:17 +00:00
|
|
|
$handler = $content->getContentHandler();
|
2014-09-15 05:01:21 +00:00
|
|
|
if ( $handler->isSupportedFormat( CONTENT_FORMAT_CSS ) ) {
|
|
|
|
|
$format = CONTENT_FORMAT_CSS;
|
|
|
|
|
} elseif ( $handler->isSupportedFormat( CONTENT_FORMAT_JAVASCRIPT ) ) {
|
|
|
|
|
$format = CONTENT_FORMAT_JAVASCRIPT;
|
|
|
|
|
} else {
|
2017-04-01 01:35:09 +00:00
|
|
|
return null; // Bad content model
|
* 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
|
|
|
}
|
2014-09-15 05:01:21 +00:00
|
|
|
|
2016-02-11 04:57:03 +00:00
|
|
|
return $content->serialize( $format );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param Title $title
|
2017-02-28 20:52:17 +00:00
|
|
|
* @param ResourceLoaderContext|null $context (but passing null is deprecated)
|
|
|
|
|
* @param int|null $maxRedirects Maximum number of redirects to follow. If
|
|
|
|
|
* null, uses $wgMaxRedirects
|
2016-02-11 04:57:03 +00:00
|
|
|
* @return Content|null
|
2017-02-28 20:52:17 +00:00
|
|
|
* @since 1.32 added the $context and $maxRedirects parameters
|
2016-02-11 04:57:03 +00:00
|
|
|
*/
|
2017-02-28 20:52:17 +00:00
|
|
|
protected function getContentObj(
|
|
|
|
|
Title $title, ResourceLoaderContext $context = null, $maxRedirects = null
|
|
|
|
|
) {
|
|
|
|
|
if ( $context === null ) {
|
|
|
|
|
wfDeprecated( __METHOD__ . ' without a ResourceLoader context', '1.32' );
|
2010-10-19 18:25:42 +00:00
|
|
|
}
|
2017-02-28 20:52:17 +00:00
|
|
|
|
|
|
|
|
$overrideCallback = $context ? $context->getContentOverrideCallback() : null;
|
|
|
|
|
$content = $overrideCallback ? call_user_func( $overrideCallback, $title ) : null;
|
|
|
|
|
if ( $content ) {
|
|
|
|
|
if ( !$content instanceof Content ) {
|
|
|
|
|
$this->getLogger()->error(
|
|
|
|
|
'Bad content override for "{title}" in ' . __METHOD__,
|
|
|
|
|
[ 'title' => $title->getPrefixedText() ]
|
|
|
|
|
);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$revision = Revision::newKnownCurrent( wfGetDB( DB_REPLICA ), $title );
|
|
|
|
|
if ( !$revision ) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
$content = $revision->getContent( Revision::RAW );
|
|
|
|
|
|
|
|
|
|
if ( !$content ) {
|
|
|
|
|
$this->getLogger()->error(
|
|
|
|
|
'Failed to load content of JS/CSS page "{title}" in ' . __METHOD__,
|
|
|
|
|
[ 'title' => $title->getPrefixedText() ]
|
|
|
|
|
);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $content && $content->isRedirect() ) {
|
|
|
|
|
if ( $maxRedirects === null ) {
|
|
|
|
|
$maxRedirects = $this->getConfig()->get( 'MaxRedirects' ) ?: 0;
|
|
|
|
|
}
|
|
|
|
|
if ( $maxRedirects > 0 ) {
|
|
|
|
|
$newTitle = $content->getRedirectTarget();
|
|
|
|
|
return $newTitle ? $this->getContentObj( $newTitle, $context, $maxRedirects - 1 ) : null;
|
|
|
|
|
}
|
2012-10-24 13:00:13 +00:00
|
|
|
}
|
2017-02-28 20:52:17 +00:00
|
|
|
|
2016-02-11 04:57:03 +00:00
|
|
|
return $content;
|
2010-10-19 18:25:42 +00:00
|
|
|
}
|
2011-10-14 08:06:54 +00:00
|
|
|
|
2017-02-28 20:52:17 +00:00
|
|
|
/**
|
|
|
|
|
* @param ResourceLoaderContext $context
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function shouldEmbedModule( ResourceLoaderContext $context ) {
|
|
|
|
|
$overrideCallback = $context->getContentOverrideCallback();
|
|
|
|
|
if ( $overrideCallback && $this->getSource() === 'local' ) {
|
|
|
|
|
foreach ( $this->getPages( $context ) as $page => $info ) {
|
|
|
|
|
$title = Title::newFromText( $page );
|
|
|
|
|
if ( $title && call_user_func( $overrideCallback, $title ) !== null ) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return parent::shouldEmbedModule( $context );
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-21 17:45:20 +00:00
|
|
|
/**
|
2014-04-20 21:33:05 +00:00
|
|
|
* @param ResourceLoaderContext $context
|
2017-04-03 08:24:41 +00:00
|
|
|
* @return string JavaScript code
|
2011-05-21 17:45:20 +00:00
|
|
|
*/
|
2010-10-19 18:25:42 +00:00
|
|
|
public function getScript( ResourceLoaderContext $context ) {
|
|
|
|
|
$scripts = '';
|
2011-02-08 06:34:38 +00:00
|
|
|
foreach ( $this->getPages( $context ) as $titleText => $options ) {
|
* 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
|
|
|
if ( $options['type'] !== 'script' ) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2017-02-28 20:52:17 +00:00
|
|
|
$script = $this->getContent( $titleText, $context );
|
2011-02-08 06:34:38 +00:00
|
|
|
if ( strval( $script ) !== '' ) {
|
2011-07-06 21:48:09 +00:00
|
|
|
$script = $this->validateScriptFile( $titleText, $script );
|
2014-04-12 08:16:42 +00:00
|
|
|
$scripts .= ResourceLoader::makeComment( $titleText ) . $script . "\n";
|
2010-10-19 18:25:42 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $scripts;
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-21 17:45:20 +00:00
|
|
|
/**
|
2014-04-20 21:33:05 +00:00
|
|
|
* @param ResourceLoaderContext $context
|
2011-05-21 17:45:20 +00:00
|
|
|
* @return array
|
|
|
|
|
*/
|
2010-10-19 18:25:42 +00:00
|
|
|
public function getStyles( ResourceLoaderContext $context ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$styles = [];
|
2011-02-08 06:34:38 +00:00
|
|
|
foreach ( $this->getPages( $context ) as $titleText => $options ) {
|
* 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
|
|
|
if ( $options['type'] !== 'style' ) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2017-10-06 22:17:58 +00:00
|
|
|
$media = $options['media'] ?? 'all';
|
2017-02-28 20:52:17 +00:00
|
|
|
$style = $this->getContent( $titleText, $context );
|
2011-02-08 06:34:38 +00:00
|
|
|
if ( strval( $style ) === '' ) {
|
* 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
|
|
|
continue;
|
|
|
|
|
}
|
2010-12-16 19:31:48 +00:00
|
|
|
if ( $this->getFlip( $context ) ) {
|
|
|
|
|
$style = CSSJanus::transform( $style, true, false );
|
|
|
|
|
}
|
2015-10-20 20:13:57 +00:00
|
|
|
$style = MemoizedCallable::call( 'CSSMin::remap',
|
2016-02-17 09:09:32 +00:00
|
|
|
[ $style, false, $this->getConfig()->get( 'ScriptPath' ), true ] );
|
* 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
|
|
|
if ( !isset( $styles[$media] ) ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$styles[$media] = [];
|
2010-10-19 18:25:42 +00:00
|
|
|
}
|
2014-04-12 08:16:42 +00:00
|
|
|
$style = ResourceLoader::makeComment( $titleText ) . $style;
|
ResourceLoader: Refactor style loading
Fixes:
* bug 31676: Work around IE stylesheet limit.
* bug 35562: @import styles broken in modules that combine
multiple stylesheets.
* bug 40498: Don't output empty "@media print { }" blocks.
* bug 40500: Don't ignore media-type for urls in debug mode.
Approach:
* Re-use the same <style> tag so that we stay under the 31
stylesheet limit in IE. Unless the to-be-added css text from
the being-loaded module contains @import, in which case we do
create a new <style> tag and then re-use that one from that
point on (bug 31676).
* Return stylesheets as arrays, instead of a concatenated string.
This fixes bug 35562, because @import only works when at the
top of a stylesheet. By not unconditionally concatenating files
within a module on the server side already, @import will work
in e.g. module 'site' that contains 2 wiki pages.
This is normalized in ResourceLoader::makeCombinedStyles(),
so far only ResourceLoaderWikiModule makes use of this.
Misc. clean up and bug fixes:
* Reducing usage of jQuery() and mw.html.element() where
native DOM would be very simple and faster. Aside from
simplicity and speed, this is also working towards a more
stand-alone ResourceLoader.
* Trim server output a little bit more
- Redundant new line after minify-css (it is now an array, so
no need to keep space afterwards)
- Redundant semi-colon after minify-js if it ends in a colon
* Allow space in styleTest.css.php
* Clean up and extend unit tests to cover for these features
and bug fixes.
* Don't set styleEl.rel = 'stylesheet'; that has no business
on a <style> tag.
* Fix bug in mw.loader's addStyleTag(). It turns out IE6
has an odd security measure that does not allow manipulation
of elements (at least style tags) that are created by a
different script (even if that script was served from the same
domain/origin etc.). We didn't ran into this before because
we only created new style tags, never appended to them. Now
that we do, this came up. Took a while to figure out because
it was created by mediawiki.js but it calls jQuery which did
the actual dom insertion. Odd thing is, we load jquery.js and
mediawiki.js in the same request even...
Without this all css-url related mw.loader tests would fail
in IE6.
* mediawiki.js and mediawiki.test.js now pass jshint again.
Tested (and passing qunit/?module=mediawiki; 123 of 123):
* Chrome 14, 21
* Firefox 3.0, 3.6, 4, 7, 14, 15, 16beta
* IE 6, 7, 8, 9
* Safari 4.0, 5.0, 5.1
* Opera 10.0, 11.1, 11.5, 11.6, 12.0, 12.5beta
* iPhone 3GS / iOS 3.0 / Mobile Safari 4.0
iPhone 4 / iOS 4.0.1 / Mobile Safari 4.0.5
iPhone 4S / iOS 6.0 Beta / Mobile Safari 6.0
Change-Id: I3e8227ddb87fd9441071ca935439fc6467751dab
2012-07-25 21:20:21 +00:00
|
|
|
$styles[$media][] = $style;
|
2010-10-19 18:25:42 +00:00
|
|
|
}
|
|
|
|
|
return $styles;
|
|
|
|
|
}
|
|
|
|
|
|
resourceloader: Enable module content version for data modules
This greatly simplifies logic required to compute module versions.
It also makes it significantly less error-prone.
Since f37cee996e, we support hashes as versions (instead of timestamps).
This means we can build a hash of the content directly, instead of compiling a
large array with all values that may influence the module content somehow.
Benefits:
* Remove all methods and logic related to querying database and disk for
timestamps, revision numbers, definition summaries, cache epochs, and more.
* No longer needlessly invalidate cache as a result of no-op changes to
implementation datails. Due to inclusion of absolute file paths in the
definition summary, cache was always invalidated when moving wikis to newer
MediaWiki branches; even if the module observed no actual changes.
* When changes are reverted within a certain period of time, old caches can now
be re-used. The module would produce the same version hash as before.
Previously when a change was deployed and then reverted, all web clients (even
those that never saw the bad version) would have re-fetch modules because the
version increased.
Updated unit tests to account for the change in version. New default version of
empty test modules is: "mvgTPvXh". For the record, this comes from the base64
encoding of the SHA1 digest of the JSON serialised form of the module content:
> $str = '{"scripts":"","styles":{"css":[]},"messagesBlob":"{}"}';
> echo base64_encode(sha1($str, true));
> FEb3+VuiUm/fOMfod1bjw/te+AQ=
Enabled content versioning for the data modules in MediaWiki core:
* EditToolbarModule
* JqueryMsgModule
* LanguageDataModule
* LanguageNamesModule
* SpecialCharacterDataModule
* UserCSSPrefsModule
* UserDefaultsModule
* UserOptionsModule
The FileModule and base class explicitly disable it for now and keep their
current behaviour of using the definition summary. We may remove it later, but
that requires more performance testing first.
Explicitly disable it in the WikiModule class to avoid breakage when the
default changes.
Ref T98087.
Change-Id: I782df43c50dfcfb7d7592f744e13a3a0430b0dc6
2015-06-02 17:27:23 +00:00
|
|
|
/**
|
|
|
|
|
* Disable module content versioning.
|
|
|
|
|
*
|
|
|
|
|
* This class does not support generating content outside of a module
|
|
|
|
|
* request due to foreign database support.
|
|
|
|
|
*
|
|
|
|
|
* See getDefinitionSummary() for meta-data versioning.
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function enableModuleContentVersion() {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-18 13:34:50 +00:00
|
|
|
/**
|
2014-08-14 19:34:55 +00:00
|
|
|
* @param ResourceLoaderContext $context
|
2014-04-20 21:33:05 +00:00
|
|
|
* @return array
|
2013-10-18 13:34:50 +00:00
|
|
|
*/
|
|
|
|
|
public function getDefinitionSummary( ResourceLoaderContext $context ) {
|
resourceloader: Replace timestamp system with version hashing
Modules now track their version via getVersionHash() instead of getModifiedTime().
== Background ==
While some resources have observeable timestamps (e.g. files stored on disk),
many other resources do not. E.g. config variables, and module definitions.
For static file modules, one can e.g. revert one of more files in a module to a
previous version and not affect the max timestamp.
Wiki modules include pages only if they exist. The user module supports common.js
and skin.js. By default neither exists. If a user has both, and then the
less-recently modified one is deleted, the max-timestamp remains unchanged.
For client-side caching, batch requests use "Math.max" on the relevant timestamps.
Again, if a module changes but another module is more recent (e.g. out-of-order
deployment, or out-of-order discovery), the change would not result in a cache miss.
More scenarios can be found in the associated Phabricator tasks.
== Version hash ==
Previously we virtually mapped these variables to a timestamp by storing the current
time alongside a hash of the value in ObjectCache. Considering the number of
possible request contexts (wikis * modules * users * skins * languages) this doesn't
work well. It results in needless cache invalidation when the first time observation
is purged due to LRU algorithms. It also has other minor bugs leading to fewer
cache hits.
All modules automatically get the benefits of version hashing with this change.
The old getDefinitionMtime() and getHashMtime() have been replaced with dummies
that return 1. These functions are often called from getModifiedTime() in subclasses.
For backward-compatibility, their respective values (definition summary and hash)
are now included in getVersionHash directly.
As examples, the following modules have been updated to use getVersionHash directly.
Other modules still work fine and can be updated later.
* ResourceLoaderFileModule
* ResourceLoaderEditToolbarModule
* ResourceLoaderStartUpModule
* ResourceLoaderWikiModule
The presence of hashes in place of timestamps increases the startup module size on
a default MediaWiki install from 4.4k to 5.8k (after gzip and minification).
== ETag ==
Since timestamps are no longer tracked, we need a different way to implement caching
for cache proxies (e.g. Varnish) and web browsers. Previously we used the
Last-Modified header (in combination with Cache-Control and Expires).
Instead of Last-Modified (and If-Modified-Since), we use ETag (and If-None-Match).
Entity tags (new in HTTP/1.1) are much stricter than Last-Modified by default.
They instruct browsers to allow usage of partial Range requests. Since our responses
are dynamically generated, we need to use the Weak version of ETag.
While this sounds bad, it's no different than Last-Modified. As reassured by
RFC 2616 <http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.3.3> the
specified behaviour behind Last-Modified follows the same "Weak" caching logic as
Entity tags. It's just that entity tags are capable of a stricter mode (whereas
Last-Modified is inherently weak).
== File cache ==
If $wgUseFileCache is enabled, ResourceLoader uses ResourceFileCache to cache
load.php responses. While the blind TTL handling (during the allowed expiry period)
is still maxage/timestamp based, tryRespondNotModified() now requires the caller to
know the expected ETag.
For this to work, the FileCache handling had to be moved from the top of
ResoureLoader::respond() to after the expected ETag is computed.
This also allows us to remove the duplicate tryRespondNotModified() handling since
that's is already handled by ResourceLoader::respond() meanwhile.
== Misc ==
* Remove redundant modifiedTime cache in ResourceLoaderFileModule.
* Change bugzilla references to Phabricator.
* Centralised inclusion of wgCacheEpoch using getDefinitionSummary. Previously this
logic was duplicated in each place the modified timestamp was used.
* It's easy to forget calling the parent class in getDefinitionSummary().
Previously this method only tracked 'class' by default. As such, various
extensions hardcoded that one value instead of calling the parent and extending
the array. To better prevent this in the future, getVersionHash() now asserts
that the '_cacheEpoch' property made it through.
* tests: Don't use getDefinitionSummary() as an API.
Fix ResourceLoaderWikiModuleTest to call getPages properly.
* In tests, the default timestamp used to be 1388534400000 (which is the unix time
of 20140101000000; the unit tests' CacheEpoch). The new version hash of these
modules is "XyCC+PSK", which is the base64 encoded prefix of the SHA1 digest of:
'{"_class":"ResourceLoaderTestModule","_cacheEpoch":"20140101000000"}'
* Add sha1.js library for client-side hash generation.
Compared various different implementations for code size (after minfication/gzip),
and speed (when used for short hexidecimal strings).
https://jsperf.com/sha1-implementations
- CryptoJS <https://code.google.com/p/crypto-js/#SHA-1> (min+gzip: 2.5k)
http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/sha1.js
Chrome: 45k, Firefox: 89k, Safari: 92k
- jsSHA <https://github.com/Caligatio/jsSHA>
https://github.com/Caligatio/jsSHA/blob/3c1d4f2e/src/sha1.js (min+gzip: 1.8k)
Chrome: 65k, Firefox: 53k, Safari: 69k
- phpjs-sha1 <https://github.com/kvz/phpjs> (RL min+gzip: 0.8k)
https://github.com/kvz/phpjs/blob/1eaab15d/functions/strings/sha1.js
Chrome: 200k, Firefox: 280k, Safari: 78k
Modern browsers implement the HTML5 Crypto API. However, this API is asynchronous,
only enabled when on HTTPS in Chromium, and is quite low-level. It requires boilerplate
code to actually use with TextEncoder, ArrayBuffer and Uint32Array. Due this being
needed in the module loader, we'd have to load the fallback regardless. Considering
this is not used in a critical path for performance, it's not worth shipping two
implementations for this optimisation.
May also resolve:
* T44094
* T90411
* T94810
Bug: T94074
Change-Id: Ibb292d2416839327d1807a66c78fd96dac0637d0
2015-04-29 22:53:24 +00:00
|
|
|
$summary = parent::getDefinitionSummary( $context );
|
2016-02-17 09:09:32 +00:00
|
|
|
$summary[] = [
|
2013-10-18 13:34:50 +00:00
|
|
|
'pages' => $this->getPages( $context ),
|
2016-09-08 02:24:49 +00:00
|
|
|
// Includes meta data of current revisions
|
2015-06-04 01:52:42 +00:00
|
|
|
'titleInfo' => $this->getTitleInfo( $context ),
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
resourceloader: Replace timestamp system with version hashing
Modules now track their version via getVersionHash() instead of getModifiedTime().
== Background ==
While some resources have observeable timestamps (e.g. files stored on disk),
many other resources do not. E.g. config variables, and module definitions.
For static file modules, one can e.g. revert one of more files in a module to a
previous version and not affect the max timestamp.
Wiki modules include pages only if they exist. The user module supports common.js
and skin.js. By default neither exists. If a user has both, and then the
less-recently modified one is deleted, the max-timestamp remains unchanged.
For client-side caching, batch requests use "Math.max" on the relevant timestamps.
Again, if a module changes but another module is more recent (e.g. out-of-order
deployment, or out-of-order discovery), the change would not result in a cache miss.
More scenarios can be found in the associated Phabricator tasks.
== Version hash ==
Previously we virtually mapped these variables to a timestamp by storing the current
time alongside a hash of the value in ObjectCache. Considering the number of
possible request contexts (wikis * modules * users * skins * languages) this doesn't
work well. It results in needless cache invalidation when the first time observation
is purged due to LRU algorithms. It also has other minor bugs leading to fewer
cache hits.
All modules automatically get the benefits of version hashing with this change.
The old getDefinitionMtime() and getHashMtime() have been replaced with dummies
that return 1. These functions are often called from getModifiedTime() in subclasses.
For backward-compatibility, their respective values (definition summary and hash)
are now included in getVersionHash directly.
As examples, the following modules have been updated to use getVersionHash directly.
Other modules still work fine and can be updated later.
* ResourceLoaderFileModule
* ResourceLoaderEditToolbarModule
* ResourceLoaderStartUpModule
* ResourceLoaderWikiModule
The presence of hashes in place of timestamps increases the startup module size on
a default MediaWiki install from 4.4k to 5.8k (after gzip and minification).
== ETag ==
Since timestamps are no longer tracked, we need a different way to implement caching
for cache proxies (e.g. Varnish) and web browsers. Previously we used the
Last-Modified header (in combination with Cache-Control and Expires).
Instead of Last-Modified (and If-Modified-Since), we use ETag (and If-None-Match).
Entity tags (new in HTTP/1.1) are much stricter than Last-Modified by default.
They instruct browsers to allow usage of partial Range requests. Since our responses
are dynamically generated, we need to use the Weak version of ETag.
While this sounds bad, it's no different than Last-Modified. As reassured by
RFC 2616 <http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.3.3> the
specified behaviour behind Last-Modified follows the same "Weak" caching logic as
Entity tags. It's just that entity tags are capable of a stricter mode (whereas
Last-Modified is inherently weak).
== File cache ==
If $wgUseFileCache is enabled, ResourceLoader uses ResourceFileCache to cache
load.php responses. While the blind TTL handling (during the allowed expiry period)
is still maxage/timestamp based, tryRespondNotModified() now requires the caller to
know the expected ETag.
For this to work, the FileCache handling had to be moved from the top of
ResoureLoader::respond() to after the expected ETag is computed.
This also allows us to remove the duplicate tryRespondNotModified() handling since
that's is already handled by ResourceLoader::respond() meanwhile.
== Misc ==
* Remove redundant modifiedTime cache in ResourceLoaderFileModule.
* Change bugzilla references to Phabricator.
* Centralised inclusion of wgCacheEpoch using getDefinitionSummary. Previously this
logic was duplicated in each place the modified timestamp was used.
* It's easy to forget calling the parent class in getDefinitionSummary().
Previously this method only tracked 'class' by default. As such, various
extensions hardcoded that one value instead of calling the parent and extending
the array. To better prevent this in the future, getVersionHash() now asserts
that the '_cacheEpoch' property made it through.
* tests: Don't use getDefinitionSummary() as an API.
Fix ResourceLoaderWikiModuleTest to call getPages properly.
* In tests, the default timestamp used to be 1388534400000 (which is the unix time
of 20140101000000; the unit tests' CacheEpoch). The new version hash of these
modules is "XyCC+PSK", which is the base64 encoded prefix of the SHA1 digest of:
'{"_class":"ResourceLoaderTestModule","_cacheEpoch":"20140101000000"}'
* Add sha1.js library for client-side hash generation.
Compared various different implementations for code size (after minfication/gzip),
and speed (when used for short hexidecimal strings).
https://jsperf.com/sha1-implementations
- CryptoJS <https://code.google.com/p/crypto-js/#SHA-1> (min+gzip: 2.5k)
http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/sha1.js
Chrome: 45k, Firefox: 89k, Safari: 92k
- jsSHA <https://github.com/Caligatio/jsSHA>
https://github.com/Caligatio/jsSHA/blob/3c1d4f2e/src/sha1.js (min+gzip: 1.8k)
Chrome: 65k, Firefox: 53k, Safari: 69k
- phpjs-sha1 <https://github.com/kvz/phpjs> (RL min+gzip: 0.8k)
https://github.com/kvz/phpjs/blob/1eaab15d/functions/strings/sha1.js
Chrome: 200k, Firefox: 280k, Safari: 78k
Modern browsers implement the HTML5 Crypto API. However, this API is asynchronous,
only enabled when on HTTPS in Chromium, and is quite low-level. It requires boilerplate
code to actually use with TextEncoder, ArrayBuffer and Uint32Array. Due this being
needed in the module loader, we'd have to load the fallback regardless. Considering
this is not used in a critical path for performance, it's not worth shipping two
implementations for this optimisation.
May also resolve:
* T44094
* T90411
* T94810
Bug: T94074
Change-Id: Ibb292d2416839327d1807a66c78fd96dac0637d0
2015-04-29 22:53:24 +00:00
|
|
|
return $summary;
|
2013-10-18 13:34:50 +00:00
|
|
|
}
|
|
|
|
|
|
2011-05-21 17:45:20 +00:00
|
|
|
/**
|
2014-04-20 21:33:05 +00:00
|
|
|
* @param ResourceLoaderContext $context
|
2011-05-21 17:45:20 +00:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
2011-02-19 17:07:05 +00:00
|
|
|
public function isKnownEmpty( ResourceLoaderContext $context ) {
|
2015-06-04 01:52:42 +00:00
|
|
|
$revisions = $this->getTitleInfo( $context );
|
2014-08-29 06:31:44 +00:00
|
|
|
|
2018-04-24 00:11:27 +00:00
|
|
|
// If a module has dependencies it cannot be empty. An empty array will be cast to false
|
|
|
|
|
if ( $this->getDependencies() ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2015-06-04 01:52:42 +00:00
|
|
|
// For user modules, don't needlessly load if there are no non-empty pages
|
|
|
|
|
if ( $this->getGroup() === 'user' ) {
|
|
|
|
|
foreach ( $revisions as $revision ) {
|
2016-09-08 02:24:49 +00:00
|
|
|
if ( $revision['page_len'] > 0 ) {
|
2015-06-04 01:52:42 +00:00
|
|
|
// At least one non-empty page, module should be loaded
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2014-08-29 06:31:44 +00:00
|
|
|
}
|
2015-06-04 01:52:42 +00:00
|
|
|
return true;
|
2014-08-29 06:31:44 +00:00
|
|
|
}
|
|
|
|
|
|
2017-02-20 22:44:19 +00:00
|
|
|
// T70488: For other modules (i.e. ones that are called in cached html output) only check
|
2015-06-04 01:52:42 +00:00
|
|
|
// page existance. This ensures that, if some pages in a module are temporarily blanked,
|
|
|
|
|
// we don't end omit the module's script or link tag on some pages.
|
|
|
|
|
return count( $revisions ) === 0;
|
2011-02-19 17:07:05 +00:00
|
|
|
}
|
2011-02-18 00:33:45 +00:00
|
|
|
|
resourceloader: Improve titleInfo docs and simplify title key
* Document the structure of the in-process $titleInfo cache.
Specifically, specify that it is not the value from getTitleInfo(),
but rather a container for zero or more versions of such values.
The reason this is fragmented is because ResourceLoaderContext
is a parameter to most methods and as such, makes everything
variable. Tracked as T99107.
* Make various bits easier to understand by consistently refering
to the container keys as "batchKey", and referring to the internal
keys as "titleKey".
* Centralise title key logic by moving to private method.
* Replace the internal creation of titleKey to be based on LinkTarget
with plain namespace IDs and db keys, instead of invoking the
expensive getPrefixedTitle function which involves quite a lot
of overhead (TitleCodec, GenderCache, Database, Language,
LocalisationCache, ..).
Change-Id: I701e5156ef7815a0e36caefae5871524eff3f688
2018-04-10 17:29:50 +00:00
|
|
|
private function setTitleInfo( $batchKey, array $titleInfo ) {
|
|
|
|
|
$this->titleInfo[$batchKey] = $titleInfo;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function makeTitleKey( LinkTarget $title ) {
|
|
|
|
|
// Used for keys in titleInfo.
|
|
|
|
|
return "{$title->getNamespace()}:{$title->getDBkey()}";
|
2016-09-08 03:26:48 +00:00
|
|
|
}
|
|
|
|
|
|
2011-02-19 17:07:05 +00:00
|
|
|
/**
|
2015-06-04 01:52:42 +00:00
|
|
|
* Get the information about the wiki pages for a given context.
|
|
|
|
|
* @param ResourceLoaderContext $context
|
2016-09-08 02:24:49 +00:00
|
|
|
* @return array Keyed by page name
|
2011-02-19 17:07:05 +00:00
|
|
|
*/
|
2014-08-29 06:31:44 +00:00
|
|
|
protected function getTitleInfo( ResourceLoaderContext $context ) {
|
2011-11-02 15:04:34 +00:00
|
|
|
$dbr = $this->getDB();
|
|
|
|
|
if ( !$dbr ) {
|
|
|
|
|
// We're dealing with a subclass that doesn't have a DB
|
2016-02-17 09:09:32 +00:00
|
|
|
return [];
|
2011-11-02 15:04:34 +00:00
|
|
|
}
|
2012-09-05 15:50:13 +00:00
|
|
|
|
2016-09-08 03:26:48 +00:00
|
|
|
$pageNames = array_keys( $this->getPages( $context ) );
|
|
|
|
|
sort( $pageNames );
|
resourceloader: Improve titleInfo docs and simplify title key
* Document the structure of the in-process $titleInfo cache.
Specifically, specify that it is not the value from getTitleInfo(),
but rather a container for zero or more versions of such values.
The reason this is fragmented is because ResourceLoaderContext
is a parameter to most methods and as such, makes everything
variable. Tracked as T99107.
* Make various bits easier to understand by consistently refering
to the container keys as "batchKey", and referring to the internal
keys as "titleKey".
* Centralise title key logic by moving to private method.
* Replace the internal creation of titleKey to be based on LinkTarget
with plain namespace IDs and db keys, instead of invoking the
expensive getPrefixedTitle function which involves quite a lot
of overhead (TitleCodec, GenderCache, Database, Language,
LocalisationCache, ..).
Change-Id: I701e5156ef7815a0e36caefae5871524eff3f688
2018-04-10 17:29:50 +00:00
|
|
|
$batchKey = implode( '|', $pageNames );
|
|
|
|
|
if ( !isset( $this->titleInfo[$batchKey] ) ) {
|
|
|
|
|
$this->titleInfo[$batchKey] = static::fetchTitleInfo( $dbr, $pageNames, __METHOD__ );
|
2016-09-08 03:26:48 +00:00
|
|
|
}
|
2017-02-28 20:52:17 +00:00
|
|
|
|
|
|
|
|
$titleInfo = $this->titleInfo[$batchKey];
|
|
|
|
|
|
|
|
|
|
// Override the title info from the overrides, if any
|
|
|
|
|
$overrideCallback = $context->getContentOverrideCallback();
|
|
|
|
|
if ( $overrideCallback ) {
|
|
|
|
|
foreach ( $pageNames as $page ) {
|
|
|
|
|
$title = Title::newFromText( $page );
|
|
|
|
|
$content = $title ? call_user_func( $overrideCallback, $title ) : null;
|
|
|
|
|
if ( $content !== null ) {
|
|
|
|
|
$titleInfo[$title->getPrefixedText()] = [
|
|
|
|
|
'page_len' => $content->getSize(),
|
|
|
|
|
'page_latest' => 'TBD', // None available
|
|
|
|
|
'page_touched' => wfTimestamp( TS_MW ),
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $titleInfo;
|
2016-09-08 03:26:48 +00:00
|
|
|
}
|
|
|
|
|
|
2016-09-14 21:14:31 +00:00
|
|
|
protected static function fetchTitleInfo( IDatabase $db, array $pages, $fname = __METHOD__ ) {
|
2016-09-08 03:26:48 +00:00
|
|
|
$titleInfo = [];
|
|
|
|
|
$batch = new LinkBatch;
|
|
|
|
|
foreach ( $pages as $titleText ) {
|
2016-09-27 18:03:48 +00:00
|
|
|
$title = Title::newFromText( $titleText );
|
|
|
|
|
if ( $title ) {
|
|
|
|
|
// Page name may be invalid if user-provided (e.g. gadgets)
|
|
|
|
|
$batch->addObj( $title );
|
|
|
|
|
}
|
2016-09-08 03:26:48 +00:00
|
|
|
}
|
|
|
|
|
if ( !$batch->isEmpty() ) {
|
|
|
|
|
$res = $db->select( 'page',
|
|
|
|
|
// Include page_touched to allow purging if cache is poisoned (T117587, T113916)
|
|
|
|
|
[ 'page_namespace', 'page_title', 'page_touched', 'page_len', 'page_latest' ],
|
|
|
|
|
$batch->constructSet( 'page', $db ),
|
|
|
|
|
$fname
|
|
|
|
|
);
|
|
|
|
|
foreach ( $res as $row ) {
|
|
|
|
|
// Avoid including ids or timestamps of revision/page tables so
|
|
|
|
|
// that versions are not wasted
|
resourceloader: Improve titleInfo docs and simplify title key
* Document the structure of the in-process $titleInfo cache.
Specifically, specify that it is not the value from getTitleInfo(),
but rather a container for zero or more versions of such values.
The reason this is fragmented is because ResourceLoaderContext
is a parameter to most methods and as such, makes everything
variable. Tracked as T99107.
* Make various bits easier to understand by consistently refering
to the container keys as "batchKey", and referring to the internal
keys as "titleKey".
* Centralise title key logic by moving to private method.
* Replace the internal creation of titleKey to be based on LinkTarget
with plain namespace IDs and db keys, instead of invoking the
expensive getPrefixedTitle function which involves quite a lot
of overhead (TitleCodec, GenderCache, Database, Language,
LocalisationCache, ..).
Change-Id: I701e5156ef7815a0e36caefae5871524eff3f688
2018-04-10 17:29:50 +00:00
|
|
|
$title = new TitleValue( (int)$row->page_namespace, $row->page_title );
|
|
|
|
|
$titleInfo[ self::makeTitleKey( $title ) ] = [
|
2016-09-08 03:26:48 +00:00
|
|
|
'page_len' => $row->page_len,
|
|
|
|
|
'page_latest' => $row->page_latest,
|
|
|
|
|
'page_touched' => $row->page_touched,
|
|
|
|
|
];
|
2015-06-04 01:52:42 +00:00
|
|
|
}
|
2016-09-08 03:26:48 +00:00
|
|
|
}
|
|
|
|
|
return $titleInfo;
|
|
|
|
|
}
|
2011-10-14 08:06:54 +00:00
|
|
|
|
2016-09-08 03:26:48 +00:00
|
|
|
/**
|
|
|
|
|
* @since 1.28
|
|
|
|
|
* @param ResourceLoaderContext $context
|
|
|
|
|
* @param IDatabase $db
|
2016-09-02 08:28:23 +00:00
|
|
|
* @param string[] $moduleNames
|
2016-09-08 03:26:48 +00:00
|
|
|
*/
|
|
|
|
|
public static function preloadTitleInfo(
|
|
|
|
|
ResourceLoaderContext $context, IDatabase $db, array $moduleNames
|
|
|
|
|
) {
|
|
|
|
|
$rl = $context->getResourceLoader();
|
|
|
|
|
// getDB() can be overridden to point to a foreign database.
|
|
|
|
|
// For now, only preload local. In the future, we could preload by wikiID.
|
|
|
|
|
$allPages = [];
|
2016-09-02 08:28:23 +00:00
|
|
|
/** @var ResourceLoaderWikiModule[] $wikiModules */
|
2016-09-08 03:26:48 +00:00
|
|
|
$wikiModules = [];
|
|
|
|
|
foreach ( $moduleNames as $name ) {
|
|
|
|
|
$module = $rl->getModule( $name );
|
|
|
|
|
if ( $module instanceof self ) {
|
|
|
|
|
$mDB = $module->getDB();
|
|
|
|
|
// Subclasses may disable getDB and implement getTitleInfo differently
|
2017-09-25 10:37:13 +00:00
|
|
|
if ( $mDB && $mDB->getDomainID() === $db->getDomainID() ) {
|
2016-09-08 03:26:48 +00:00
|
|
|
$wikiModules[] = $module;
|
|
|
|
|
$allPages += $module->getPages( $context );
|
2015-06-04 01:52:42 +00:00
|
|
|
}
|
2011-02-19 17:07:05 +00:00
|
|
|
}
|
|
|
|
|
}
|
2016-09-02 08:28:23 +00:00
|
|
|
|
2017-02-22 21:09:06 +00:00
|
|
|
if ( !$wikiModules ) {
|
|
|
|
|
// Nothing to preload
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-20 21:43:57 +00:00
|
|
|
$pageNames = array_keys( $allPages );
|
|
|
|
|
sort( $pageNames );
|
|
|
|
|
$hash = sha1( implode( '|', $pageNames ) );
|
2016-09-02 08:28:23 +00:00
|
|
|
|
|
|
|
|
// Avoid Zend bug where "static::" does not apply LSB in the closure
|
|
|
|
|
$func = [ static::class, 'fetchTitleInfo' ];
|
2016-10-20 21:43:57 +00:00
|
|
|
$fname = __METHOD__;
|
2016-09-02 08:28:23 +00:00
|
|
|
|
|
|
|
|
$cache = ObjectCache::getMainWANInstance();
|
|
|
|
|
$allInfo = $cache->getWithSetCallback(
|
2017-09-25 10:37:13 +00:00
|
|
|
$cache->makeGlobalKey( 'resourceloader', 'titleinfo', $db->getDomainID(), $hash ),
|
2016-09-02 08:28:23 +00:00
|
|
|
$cache::TTL_HOUR,
|
2016-10-20 21:43:57 +00:00
|
|
|
function ( $curVal, &$ttl, array &$setOpts ) use ( $func, $pageNames, $db, $fname ) {
|
2016-09-02 08:28:23 +00:00
|
|
|
$setOpts += Database::getCacheSetOptions( $db );
|
|
|
|
|
|
2016-10-20 21:43:57 +00:00
|
|
|
return call_user_func( $func, $db, $pageNames, $fname );
|
2016-09-02 08:28:23 +00:00
|
|
|
},
|
2017-09-25 10:37:13 +00:00
|
|
|
[
|
|
|
|
|
'checkKeys' => [
|
|
|
|
|
$cache->makeGlobalKey( 'resourceloader', 'titleinfo', $db->getDomainID() ) ]
|
|
|
|
|
]
|
2016-09-02 08:28:23 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
foreach ( $wikiModules as $wikiModule ) {
|
|
|
|
|
$pages = $wikiModule->getPages( $context );
|
2016-09-14 21:14:31 +00:00
|
|
|
// Before we intersect, map the names to canonical form (T145673).
|
|
|
|
|
$intersect = [];
|
resourceloader: Improve titleInfo docs and simplify title key
* Document the structure of the in-process $titleInfo cache.
Specifically, specify that it is not the value from getTitleInfo(),
but rather a container for zero or more versions of such values.
The reason this is fragmented is because ResourceLoaderContext
is a parameter to most methods and as such, makes everything
variable. Tracked as T99107.
* Make various bits easier to understand by consistently refering
to the container keys as "batchKey", and referring to the internal
keys as "titleKey".
* Centralise title key logic by moving to private method.
* Replace the internal creation of titleKey to be based on LinkTarget
with plain namespace IDs and db keys, instead of invoking the
expensive getPrefixedTitle function which involves quite a lot
of overhead (TitleCodec, GenderCache, Database, Language,
LocalisationCache, ..).
Change-Id: I701e5156ef7815a0e36caefae5871524eff3f688
2018-04-10 17:29:50 +00:00
|
|
|
foreach ( $pages as $pageName => $unused ) {
|
|
|
|
|
$title = Title::newFromText( $pageName );
|
2016-09-27 18:03:48 +00:00
|
|
|
if ( $title ) {
|
resourceloader: Improve titleInfo docs and simplify title key
* Document the structure of the in-process $titleInfo cache.
Specifically, specify that it is not the value from getTitleInfo(),
but rather a container for zero or more versions of such values.
The reason this is fragmented is because ResourceLoaderContext
is a parameter to most methods and as such, makes everything
variable. Tracked as T99107.
* Make various bits easier to understand by consistently refering
to the container keys as "batchKey", and referring to the internal
keys as "titleKey".
* Centralise title key logic by moving to private method.
* Replace the internal creation of titleKey to be based on LinkTarget
with plain namespace IDs and db keys, instead of invoking the
expensive getPrefixedTitle function which involves quite a lot
of overhead (TitleCodec, GenderCache, Database, Language,
LocalisationCache, ..).
Change-Id: I701e5156ef7815a0e36caefae5871524eff3f688
2018-04-10 17:29:50 +00:00
|
|
|
$intersect[ self::makeTitleKey( $title ) ] = 1;
|
2016-09-27 18:03:48 +00:00
|
|
|
} else {
|
|
|
|
|
// Page name may be invalid if user-provided (e.g. gadgets)
|
|
|
|
|
$rl->getLogger()->info(
|
|
|
|
|
'Invalid wiki page title "{title}" in ' . __METHOD__,
|
resourceloader: Improve titleInfo docs and simplify title key
* Document the structure of the in-process $titleInfo cache.
Specifically, specify that it is not the value from getTitleInfo(),
but rather a container for zero or more versions of such values.
The reason this is fragmented is because ResourceLoaderContext
is a parameter to most methods and as such, makes everything
variable. Tracked as T99107.
* Make various bits easier to understand by consistently refering
to the container keys as "batchKey", and referring to the internal
keys as "titleKey".
* Centralise title key logic by moving to private method.
* Replace the internal creation of titleKey to be based on LinkTarget
with plain namespace IDs and db keys, instead of invoking the
expensive getPrefixedTitle function which involves quite a lot
of overhead (TitleCodec, GenderCache, Database, Language,
LocalisationCache, ..).
Change-Id: I701e5156ef7815a0e36caefae5871524eff3f688
2018-04-10 17:29:50 +00:00
|
|
|
[ 'title' => $pageName ]
|
2016-09-27 18:03:48 +00:00
|
|
|
);
|
|
|
|
|
}
|
2016-09-14 21:14:31 +00:00
|
|
|
}
|
|
|
|
|
$info = array_intersect_key( $allInfo, $intersect );
|
2016-09-08 03:26:48 +00:00
|
|
|
$pageNames = array_keys( $pages );
|
|
|
|
|
sort( $pageNames );
|
resourceloader: Improve titleInfo docs and simplify title key
* Document the structure of the in-process $titleInfo cache.
Specifically, specify that it is not the value from getTitleInfo(),
but rather a container for zero or more versions of such values.
The reason this is fragmented is because ResourceLoaderContext
is a parameter to most methods and as such, makes everything
variable. Tracked as T99107.
* Make various bits easier to understand by consistently refering
to the container keys as "batchKey", and referring to the internal
keys as "titleKey".
* Centralise title key logic by moving to private method.
* Replace the internal creation of titleKey to be based on LinkTarget
with plain namespace IDs and db keys, instead of invoking the
expensive getPrefixedTitle function which involves quite a lot
of overhead (TitleCodec, GenderCache, Database, Language,
LocalisationCache, ..).
Change-Id: I701e5156ef7815a0e36caefae5871524eff3f688
2018-04-10 17:29:50 +00:00
|
|
|
$batchKey = implode( '|', $pageNames );
|
|
|
|
|
$wikiModule->setTitleInfo( $batchKey, $info );
|
2016-09-02 08:28:23 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Clear the preloadTitleInfo() cache for all wiki modules on this wiki on
|
|
|
|
|
* page change if it was a JS or CSS page
|
|
|
|
|
*
|
|
|
|
|
* @param Title $title
|
|
|
|
|
* @param Revision|null $old Prior page revision
|
|
|
|
|
* @param Revision|null $new New page revision
|
|
|
|
|
* @param string $wikiId
|
|
|
|
|
* @since 1.28
|
|
|
|
|
*/
|
|
|
|
|
public static function invalidateModuleCache(
|
|
|
|
|
Title $title, Revision $old = null, Revision $new = null, $wikiId
|
|
|
|
|
) {
|
|
|
|
|
static $formats = [ CONTENT_FORMAT_CSS, CONTENT_FORMAT_JAVASCRIPT ];
|
|
|
|
|
|
|
|
|
|
if ( $old && in_array( $old->getContentFormat(), $formats ) ) {
|
|
|
|
|
$purge = true;
|
|
|
|
|
} elseif ( $new && in_array( $new->getContentFormat(), $formats ) ) {
|
|
|
|
|
$purge = true;
|
|
|
|
|
} else {
|
2018-02-13 00:15:30 +00:00
|
|
|
$purge = ( $title->isSiteConfigPage() || $title->isUserConfigPage() );
|
2016-09-02 08:28:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $purge ) {
|
|
|
|
|
$cache = ObjectCache::getMainWANInstance();
|
|
|
|
|
$key = $cache->makeGlobalKey( 'resourceloader', 'titleinfo', $wikiId );
|
|
|
|
|
$cache->touchCheckKey( $key );
|
2016-09-08 03:26:48 +00:00
|
|
|
}
|
2011-02-19 17:07:05 +00:00
|
|
|
}
|
2015-05-29 20:00:17 +00:00
|
|
|
|
2016-05-10 19:00:44 +00:00
|
|
|
/**
|
|
|
|
|
* @since 1.28
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getType() {
|
|
|
|
|
// Check both because subclasses don't always pass pages via the constructor,
|
|
|
|
|
// they may also override getPages() instead, in which case we should keep
|
|
|
|
|
// defaulting to LOAD_GENERAL and allow them to override getType() separately.
|
|
|
|
|
return ( $this->styles && !$this->scripts ) ? self::LOAD_STYLES : self::LOAD_GENERAL;
|
|
|
|
|
}
|
2010-10-19 18:25:42 +00:00
|
|
|
}
|