wiki.techinc.nl/includes/resourceloader/DerivativeResourceLoaderContext.php
Timo Tijhof 80e5b160e0 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-08-08 12:23:09 -07:00

199 lines
4.4 KiB
PHP

<?php
/**
* Derivative context for ResourceLoader modules.
*
* 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
*/
/**
* Allows changing specific properties of a context object,
* without changing the main one. Inspired by DerivativeContext.
*
* @since 1.24
*/
class DerivativeResourceLoaderContext extends ResourceLoaderContext {
const INHERIT_VALUE = -1;
/**
* @var ResourceLoaderContext
*/
private $context;
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;
protected $debug = self::INHERIT_VALUE;
protected $only = self::INHERIT_VALUE;
protected $version = self::INHERIT_VALUE;
protected $raw = self::INHERIT_VALUE;
public function __construct( ResourceLoaderContext $context ) {
$this->context = $context;
}
public function getModules() {
if ( $this->modules === self::INHERIT_VALUE ) {
return $this->context->getModules();
}
return $this->modules;
}
/**
* @param string[] $modules
*/
public function setModules( array $modules ) {
$this->modules = $modules;
}
public function getLanguage() {
if ( $this->language === self::INHERIT_VALUE ) {
return $this->context->getLanguage();
}
return $this->language;
}
/**
* @param string $language
*/
public function setLanguage( $language ) {
$this->language = $language;
// Invalidate direction since it is based on language
$this->direction = null;
$this->hash = null;
}
public function getDirection() {
if ( $this->direction === self::INHERIT_VALUE ) {
return $this->context->getDirection();
}
if ( $this->direction === null ) {
$this->direction = Language::factory( $this->getLanguage() )->getDir();
}
return $this->direction;
}
/**
* @param string $direction
*/
public function setDirection( $direction ) {
$this->direction = $direction;
$this->hash = null;
}
public function getSkin() {
if ( $this->skin === self::INHERIT_VALUE ) {
return $this->context->getSkin();
}
return $this->skin;
}
/**
* @param string $skin
*/
public function setSkin( $skin ) {
$this->skin = $skin;
$this->hash = null;
}
public function getUser() {
if ( $this->user === self::INHERIT_VALUE ) {
return $this->context->getUser();
}
return $this->user;
}
/**
* @param string|null $user
*/
public function setUser( $user ) {
$this->user = $user;
$this->hash = null;
$this->userObj = null;
}
public function getDebug() {
if ( $this->debug === self::INHERIT_VALUE ) {
return $this->context->getDebug();
}
return $this->debug;
}
/**
* @param bool $debug
*/
public function setDebug( $debug ) {
$this->debug = $debug;
$this->hash = null;
}
public function getOnly() {
if ( $this->only === self::INHERIT_VALUE ) {
return $this->context->getOnly();
}
return $this->only;
}
/**
* @param string|null $only
*/
public function setOnly( $only ) {
$this->only = $only;
$this->hash = null;
}
public function getVersion() {
if ( $this->version === self::INHERIT_VALUE ) {
return $this->context->getVersion();
}
return $this->version;
}
/**
* @param string|null $version
*/
public function setVersion( $version ) {
$this->version = $version;
$this->hash = null;
}
public function getRaw() {
if ( $this->raw === self::INHERIT_VALUE ) {
return $this->context->getRaw();
}
return $this->raw;
}
/**
* @param bool $raw
*/
public function setRaw( $raw ) {
$this->raw = $raw;
}
public function getRequest() {
return $this->context->getRequest();
}
public function getResourceLoader() {
return $this->context->getResourceLoader();
}
}