2014-06-28 02:57:40 +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 Kunal Mehta
|
|
|
|
|
*/
|
|
|
|
|
|
2019-08-26 12:24:37 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
|
|
|
|
|
2014-06-28 02:57:40 +00:00
|
|
|
/**
|
2019-09-14 04:32:54 +00:00
|
|
|
* A mutable version of ResourceLoaderContext.
|
|
|
|
|
*
|
2014-06-28 02:57:40 +00:00
|
|
|
* Allows changing specific properties of a context object,
|
2019-09-14 04:32:54 +00:00
|
|
|
* without changing the main one. Inspired by MediaWiki's DerivativeContext.
|
2014-06-28 02:57:40 +00:00
|
|
|
*
|
2019-09-14 04:32:54 +00:00
|
|
|
* @ingroup ResourceLoader
|
2014-06-28 02:57:40 +00:00
|
|
|
* @since 1.24
|
|
|
|
|
*/
|
|
|
|
|
class DerivativeResourceLoaderContext extends ResourceLoaderContext {
|
2020-05-15 21:36:51 +00:00
|
|
|
private const INHERIT_VALUE = -1;
|
2014-06-28 02:57:40 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var ResourceLoaderContext
|
|
|
|
|
*/
|
|
|
|
|
private $context;
|
2015-06-10 17:43:32 +00:00
|
|
|
|
2019-08-30 16:01:28 +00:00
|
|
|
/** @var int|array */
|
2015-06-10 17:43:32 +00:00
|
|
|
protected $modules = self::INHERIT_VALUE;
|
|
|
|
|
protected $language = self::INHERIT_VALUE;
|
|
|
|
|
protected $direction = self::INHERIT_VALUE;
|
|
|
|
|
protected $skin = self::INHERIT_VALUE;
|
|
|
|
|
protected $user = self::INHERIT_VALUE;
|
2020-04-14 02:59:52 +00:00
|
|
|
protected $userObj = self::INHERIT_VALUE;
|
2015-06-10 17:43:32 +00:00
|
|
|
protected $debug = self::INHERIT_VALUE;
|
|
|
|
|
protected $only = self::INHERIT_VALUE;
|
|
|
|
|
protected $version = self::INHERIT_VALUE;
|
|
|
|
|
protected $raw = self::INHERIT_VALUE;
|
2017-02-28 20:52:17 +00:00
|
|
|
protected $contentOverrideCallback = self::INHERIT_VALUE;
|
2014-06-28 02:57:40 +00:00
|
|
|
|
|
|
|
|
public function __construct( ResourceLoaderContext $context ) {
|
|
|
|
|
$this->context = $context;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-08 21:10:04 +00:00
|
|
|
public function getModules() : array {
|
2015-06-10 17:43:32 +00:00
|
|
|
if ( $this->modules === self::INHERIT_VALUE ) {
|
2014-06-28 02:57:40 +00:00
|
|
|
return $this->context->getModules();
|
|
|
|
|
}
|
2019-08-30 16:01:28 +00:00
|
|
|
|
2015-06-10 17:43:32 +00:00
|
|
|
return $this->modules;
|
2014-06-28 02:57:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string[] $modules
|
|
|
|
|
*/
|
|
|
|
|
public function setModules( array $modules ) {
|
|
|
|
|
$this->modules = $modules;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-08 21:10:04 +00:00
|
|
|
public function getLanguage() : string {
|
2015-06-10 17:43:32 +00:00
|
|
|
if ( $this->language === self::INHERIT_VALUE ) {
|
2014-06-28 02:57:40 +00:00
|
|
|
return $this->context->getLanguage();
|
|
|
|
|
}
|
2015-06-10 17:43:32 +00:00
|
|
|
return $this->language;
|
2014-06-28 02:57:40 +00:00
|
|
|
}
|
|
|
|
|
|
2019-10-08 21:10:04 +00:00
|
|
|
public function setLanguage( string $language ) {
|
2014-06-28 02:57:40 +00:00
|
|
|
$this->language = $language;
|
2015-06-10 17:43:32 +00:00
|
|
|
// Invalidate direction since it is based on language
|
2015-06-13 05:00:33 +00:00
|
|
|
$this->direction = null;
|
2014-06-28 02:57:40 +00:00
|
|
|
$this->hash = null;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-08 21:10:04 +00:00
|
|
|
public function getDirection() : string {
|
2015-06-10 17:43:32 +00:00
|
|
|
if ( $this->direction === self::INHERIT_VALUE ) {
|
2014-06-28 02:57:40 +00:00
|
|
|
return $this->context->getDirection();
|
|
|
|
|
}
|
2015-06-13 05:00:33 +00:00
|
|
|
if ( $this->direction === null ) {
|
2019-08-26 12:24:37 +00:00
|
|
|
$this->direction = MediaWikiServices::getInstance()->getLanguageFactory()
|
|
|
|
|
->getLanguage( $this->getLanguage() )->getDir();
|
2015-06-13 05:00:33 +00:00
|
|
|
}
|
2015-06-10 17:43:32 +00:00
|
|
|
return $this->direction;
|
2014-06-28 02:57:40 +00:00
|
|
|
}
|
|
|
|
|
|
2019-10-08 21:10:04 +00:00
|
|
|
public function setDirection( string $direction ) {
|
2014-06-28 02:57:40 +00:00
|
|
|
$this->direction = $direction;
|
|
|
|
|
$this->hash = null;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-08 21:10:04 +00:00
|
|
|
public function getSkin() : string {
|
2015-06-10 17:43:32 +00:00
|
|
|
if ( $this->skin === self::INHERIT_VALUE ) {
|
2014-06-28 02:57:40 +00:00
|
|
|
return $this->context->getSkin();
|
|
|
|
|
}
|
2015-06-10 17:43:32 +00:00
|
|
|
return $this->skin;
|
2014-06-28 02:57:40 +00:00
|
|
|
}
|
|
|
|
|
|
2019-10-08 21:10:04 +00:00
|
|
|
public function setSkin( string $skin ) {
|
2014-06-28 02:57:40 +00:00
|
|
|
$this->skin = $skin;
|
|
|
|
|
$this->hash = null;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-08 21:10:04 +00:00
|
|
|
public function getUser() : ?string {
|
2015-06-10 17:43:32 +00:00
|
|
|
if ( $this->user === self::INHERIT_VALUE ) {
|
2014-06-28 02:57:40 +00:00
|
|
|
return $this->context->getUser();
|
|
|
|
|
}
|
2015-06-10 17:43:32 +00:00
|
|
|
return $this->user;
|
2014-06-28 02:57:40 +00:00
|
|
|
}
|
|
|
|
|
|
2020-04-14 02:59:52 +00:00
|
|
|
public function getUserObj() : User {
|
|
|
|
|
if ( $this->userObj === self::INHERIT_VALUE ) {
|
|
|
|
|
return $this->context->getUserObj();
|
|
|
|
|
}
|
|
|
|
|
if ( $this->userObj === null ) {
|
|
|
|
|
$username = $this->getUser();
|
|
|
|
|
if ( $username ) {
|
|
|
|
|
$this->userObj = User::newFromName( $username ) ?: new User;
|
|
|
|
|
} else {
|
|
|
|
|
$this->userObj = new User;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $this->userObj;
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-28 02:57:40 +00:00
|
|
|
/**
|
resourceloader: Move queue formatting out of OutputPage
HTML formatting of the queue was distributed over several OutputPage methods.
Each method demanding a snippet of HTML by calling makeResourceLoaderLink()
with a limited amount of information. As such, makeResourceLoaderLink() was
unable to provide the client with the proper state information.
Centralising it also allows it to better reduce duplication in HTML output
and maintain a more accurate state.
Problems fixed by centralising:
1. The 'user' module is special (due to per-user 'version' and 'user' params).
It is manually requested via script-src. To avoid a separate (and wrong)
request from something that requires it, we set state=loading directly.
However, because the module is in the bottom, the old HTML formatter could
only put state=loading in the bottom also. This sometimes caused a wrong
request to be fired for modules=user if something in the top queue
triggered a requirement for it.
2. Since a464d1d4 (T87871) we track states of page-style modules, with purpose
of allowing dependencies on style modules without risking duplicate loading
on pages where the styles are loaded already. This didn't work, because the
state information about page-style modules is output near the stylesheet,
which is after the script tag with mw.loader.load(). That runs first, and
mw.loader would still make a duplicate request before it learns the state.
Changes:
* Document reasons for style/script tag order in getHeadHtml (per 09537e83).
* Pass $type from getModuleStyles() to getAllowedModules(). This wasn't needed
before since a duplicate check in makeResourceLoaderLink() verified the
origin a second time.
* Declare explicit position 'top' on 'user.options' and 'user.tokens' module.
Previously, OutputPage hardcoded them in the top. The new formatter doesn't.
* Remove getHeadScripts().
* Remove getInlineHeadScripts().
* Remove getExternalHeadScripts().
* Remove buildCssLinks().
* Remove getScriptsForBottomQueue().
* Change where Skin::setupSkinUserCss() is called. This methods lets the skin
add modules to the queue. Previously it was called from buildCssLinks(),
via headElement(), via prepareQuickTemplate(), via OutputPage::output().
It's now in OutputPage::output() directly (slightly earlier). This is needed
because prepareQuickTemplate() calls bottomScripts() before headElement().
And bottomScript() would lazy-initialise the queue and lock it before
setupSkinUserCss() is called from headElement().
This makes execution order more predictable instead of being dependent on
the arbitrary order of data extraction in prepareQuickTemplate (which varies
from one skin to another).
* Compute isUserModulePreview() and isKnownEmpty() for the 'user' module early
on so. This avoids wrongful loading and fixes problem 1.
Effective changes in output:
* mw.loader.state() is now before mw.loader.load(). This fixes problem 2.
* mw.loader.state() now sets 'user.options' and 'user.tokens' to "loading".
* mw.loader.state() now sets 'user' (as "loading" or "ready"). Fixes problem 1.
* The <script async src> tag for 'startup' changed position (slightly).
Previously it was after all inline scripts and stylesheets. It's still after
all inline scripts and after most stylesheets, but before any user styles.
Since the queue is now formatted outside OutputPage, it can't inject the
meta-ResourceLoaderDynamicStyles tag and user-stylesheet hack in the middle
of existing output. This shouldn't have any noticable impact.
Bug: T87871
Change-Id: I605b8cd1e1fc009b4662a0edbc54d09dd65ee1df
2016-07-15 14:13:09 +00:00
|
|
|
* @param string|null $user
|
2014-06-28 02:57:40 +00:00
|
|
|
*/
|
2019-10-08 21:10:04 +00:00
|
|
|
public function setUser( ?string $user ) {
|
2014-06-28 02:57:40 +00:00
|
|
|
$this->user = $user;
|
|
|
|
|
$this->hash = null;
|
2019-10-08 21:10:04 +00:00
|
|
|
// Clear getUserObj cache
|
2014-10-04 10:22:07 +00:00
|
|
|
$this->userObj = null;
|
2014-06-28 02:57:40 +00:00
|
|
|
}
|
|
|
|
|
|
2020-05-22 02:01:42 +00:00
|
|
|
public function getDebug() : int {
|
2015-06-10 17:43:32 +00:00
|
|
|
if ( $this->debug === self::INHERIT_VALUE ) {
|
2014-06-28 02:57:40 +00:00
|
|
|
return $this->context->getDebug();
|
|
|
|
|
}
|
2015-06-10 17:43:32 +00:00
|
|
|
return $this->debug;
|
2014-06-28 02:57:40 +00:00
|
|
|
}
|
|
|
|
|
|
2020-05-22 02:01:42 +00:00
|
|
|
public function setDebug( int $debug ) {
|
2014-06-28 02:57:40 +00:00
|
|
|
$this->debug = $debug;
|
|
|
|
|
$this->hash = null;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-08 21:10:04 +00:00
|
|
|
public function getOnly() : ?string {
|
2015-06-10 17:43:32 +00:00
|
|
|
if ( $this->only === self::INHERIT_VALUE ) {
|
2014-06-28 02:57:40 +00:00
|
|
|
return $this->context->getOnly();
|
|
|
|
|
}
|
2015-06-10 17:43:32 +00:00
|
|
|
return $this->only;
|
2014-06-28 02:57:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-06-10 17:43:32 +00:00
|
|
|
* @param string|null $only
|
2014-06-28 02:57:40 +00:00
|
|
|
*/
|
2019-10-08 21:10:04 +00:00
|
|
|
public function setOnly( ?string $only ) {
|
2014-06-28 02:57:40 +00:00
|
|
|
$this->only = $only;
|
|
|
|
|
$this->hash = null;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-08 21:10:04 +00:00
|
|
|
public function getVersion() : ?string {
|
2015-06-10 17:43:32 +00:00
|
|
|
if ( $this->version === self::INHERIT_VALUE ) {
|
2014-06-28 02:57:40 +00:00
|
|
|
return $this->context->getVersion();
|
|
|
|
|
}
|
2015-06-10 17:43:32 +00:00
|
|
|
return $this->version;
|
2014-06-28 02:57:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-06-10 17:43:32 +00:00
|
|
|
* @param string|null $version
|
2014-06-28 02:57:40 +00:00
|
|
|
*/
|
2019-10-08 21:10:04 +00:00
|
|
|
public function setVersion( ?string $version ) {
|
2014-06-28 02:57:40 +00:00
|
|
|
$this->version = $version;
|
|
|
|
|
$this->hash = null;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-08 21:10:04 +00:00
|
|
|
public function getRaw() : bool {
|
2015-06-10 17:43:32 +00:00
|
|
|
if ( $this->raw === self::INHERIT_VALUE ) {
|
2014-06-28 02:57:40 +00:00
|
|
|
return $this->context->getRaw();
|
|
|
|
|
}
|
2015-06-10 17:43:32 +00:00
|
|
|
return $this->raw;
|
2014-06-28 02:57:40 +00:00
|
|
|
}
|
|
|
|
|
|
2019-10-08 21:10:04 +00:00
|
|
|
public function setRaw( bool $raw ) {
|
2014-06-28 02:57:40 +00:00
|
|
|
$this->raw = $raw;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-08 21:10:04 +00:00
|
|
|
public function getRequest() : WebRequest {
|
2014-06-28 02:57:40 +00:00
|
|
|
return $this->context->getRequest();
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-08 21:10:04 +00:00
|
|
|
public function getResourceLoader() : ResourceLoader {
|
2014-06-28 02:57:40 +00:00
|
|
|
return $this->context->getResourceLoader();
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-28 20:52:17 +00:00
|
|
|
public function getContentOverrideCallback() {
|
|
|
|
|
if ( $this->contentOverrideCallback === self::INHERIT_VALUE ) {
|
|
|
|
|
return $this->context->getContentOverrideCallback();
|
|
|
|
|
}
|
|
|
|
|
return $this->contentOverrideCallback;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @see self::getContentOverrideCallback
|
|
|
|
|
* @since 1.32
|
|
|
|
|
* @param callable|null|int $callback As per self::getContentOverrideCallback,
|
|
|
|
|
* or self::INHERIT_VALUE
|
|
|
|
|
*/
|
|
|
|
|
public function setContentOverrideCallback( $callback ) {
|
|
|
|
|
$this->contentOverrideCallback = $callback;
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-28 02:57:40 +00:00
|
|
|
}
|