2011-04-03 11:09:49 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
2011-07-17 07:47:13 +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
|
|
|
|
|
*
|
2011-04-29 21:29:20 +00:00
|
|
|
* @since 1.18
|
|
|
|
|
*
|
2011-07-17 07:47:13 +00:00
|
|
|
* @author Alexandre Emsenhuber
|
2011-04-03 11:09:49 +00:00
|
|
|
* @author Daniel Friesen
|
|
|
|
|
* @file
|
|
|
|
|
*/
|
|
|
|
|
|
2015-12-10 22:58:11 +00:00
|
|
|
use MediaWiki\Logger\LoggerFactory;
|
2022-04-13 15:28:26 +00:00
|
|
|
use MediaWiki\MainConfigNames;
|
2016-04-11 18:28:17 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2021-01-08 01:01:28 +00:00
|
|
|
use MediaWiki\Permissions\Authority;
|
2021-06-02 22:13:53 +00:00
|
|
|
use MediaWiki\Session\CsrfTokenSet;
|
2020-01-10 00:00:51 +00:00
|
|
|
use Wikimedia\AtEase\AtEase;
|
2019-06-25 18:53:15 +00:00
|
|
|
use Wikimedia\IPUtils;
|
2020-09-07 09:30:43 +00:00
|
|
|
use Wikimedia\NonSerializable\NonSerializableTrait;
|
2020-01-10 00:00:51 +00:00
|
|
|
use Wikimedia\ScopedCallback;
|
2015-12-10 22:58:11 +00:00
|
|
|
|
2011-07-17 07:47:13 +00:00
|
|
|
/**
|
|
|
|
|
* Group all the pieces relevant to the context of a request into one instance
|
2020-06-30 10:13:50 +00:00
|
|
|
* @newable
|
|
|
|
|
* @note marked as newable in 1.35 for lack of a better alternative,
|
|
|
|
|
* but should use a factory in the future and should be narrowed
|
|
|
|
|
* down to not expose heavy weight objects.
|
2011-07-17 07:47:13 +00:00
|
|
|
*/
|
2014-07-21 09:13:23 +00:00
|
|
|
class RequestContext implements IContextSource, MutableContext {
|
2020-09-07 09:30:43 +00:00
|
|
|
use NonSerializableTrait;
|
|
|
|
|
|
2011-05-28 16:32:09 +00:00
|
|
|
/**
|
|
|
|
|
* @var WebRequest
|
|
|
|
|
*/
|
2011-06-27 19:23:54 +00:00
|
|
|
private $request;
|
2011-05-28 16:32:09 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var Title
|
|
|
|
|
*/
|
2011-06-27 19:23:54 +00:00
|
|
|
private $title;
|
2011-05-28 16:32:09 +00:00
|
|
|
|
2012-01-06 20:00:04 +00:00
|
|
|
/**
|
2022-02-26 07:54:16 +00:00
|
|
|
* @var WikiPage|null
|
2012-01-06 20:00:04 +00:00
|
|
|
*/
|
|
|
|
|
private $wikipage;
|
|
|
|
|
|
context: Add a cached RequestContext::getActionName method
This method is dependent on, and inherently must depend on, all of
Title, WikiPage, and WebRequest. And, like Title and WikiPage,
which also have getters in RequestContext, Action is also derived
from a query parameter that is widely recognised in almost all
web requests to index.php.
The status quo in core and extensions, is to obtain this value
via Action::getActionName(), which as a static method that bypasses
dependency injection and also has the problem of not being cached.
Caching it within ActionFactory seems hard and awkward, due to
varying by context.
In change I61d66211bd (22f9a32853f) a cached wrapper method was added
internally to the Skin class. In change I8cbc4bba4d248d9 (235820d631)
another cached wrapper was added in the Gadgets extension.
This change takes this approach further by making it a stable public
method on RequestContext.
To facilitate testing and to offer basic confidence in this working
correctly, this commit also adopts the new method in two place that
are considered "safe" (Skin, and OutputPage). Both of these are
called relatively late in the PHP proccess and well after any Setup
code and overrides (such as in MediaWiki.php), during which it is
more complex to call this. I'll audit and update those in a subsequent
change.
Change-Id: I1e259b54dca48a32be5a8c6cbb8eb69aec2da115
2021-11-06 00:28:23 +00:00
|
|
|
/**
|
|
|
|
|
* @var null|string
|
|
|
|
|
*/
|
|
|
|
|
private $action;
|
|
|
|
|
|
2011-05-28 16:32:09 +00:00
|
|
|
/**
|
|
|
|
|
* @var OutputPage
|
|
|
|
|
*/
|
2011-06-27 19:23:54 +00:00
|
|
|
private $output;
|
2011-05-28 16:32:09 +00:00
|
|
|
|
|
|
|
|
/**
|
2022-07-02 11:58:44 +00:00
|
|
|
* @var User|null
|
2011-05-28 16:32:09 +00:00
|
|
|
*/
|
2011-06-27 19:23:54 +00:00
|
|
|
private $user;
|
2011-05-28 16:32:09 +00:00
|
|
|
|
2021-01-08 01:01:28 +00:00
|
|
|
/**
|
|
|
|
|
* @var Authority
|
|
|
|
|
*/
|
|
|
|
|
private $authority;
|
|
|
|
|
|
2011-05-28 16:32:09 +00:00
|
|
|
/**
|
2021-09-22 20:16:51 +00:00
|
|
|
* @var Language|null
|
2011-05-28 16:32:09 +00:00
|
|
|
*/
|
2011-06-27 19:23:54 +00:00
|
|
|
private $lang;
|
2011-05-28 16:32:09 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var Skin
|
|
|
|
|
*/
|
2011-06-27 19:23:54 +00:00
|
|
|
private $skin;
|
2011-04-03 11:09:49 +00:00
|
|
|
|
2015-11-03 02:26:07 +00:00
|
|
|
/**
|
|
|
|
|
* @var Timing
|
|
|
|
|
*/
|
|
|
|
|
private $timing;
|
|
|
|
|
|
2013-10-25 21:17:24 +00:00
|
|
|
/**
|
2014-01-24 01:45:11 +00:00
|
|
|
* @var Config
|
2013-10-25 21:17:24 +00:00
|
|
|
*/
|
|
|
|
|
private $config;
|
|
|
|
|
|
2014-06-27 17:59:47 +00:00
|
|
|
/**
|
2021-09-22 20:16:51 +00:00
|
|
|
* @var RequestContext|null
|
2014-06-27 17:59:47 +00:00
|
|
|
*/
|
|
|
|
|
private static $instance = null;
|
|
|
|
|
|
2019-09-08 16:50:13 +00:00
|
|
|
/**
|
|
|
|
|
* Boolean flag to guard against recursion in getLanguage
|
|
|
|
|
* @var bool
|
|
|
|
|
*/
|
|
|
|
|
private $languageRecursion = false;
|
|
|
|
|
|
2013-10-25 21:17:24 +00:00
|
|
|
/**
|
2017-12-28 15:31:56 +00:00
|
|
|
* @param Config $config
|
2013-10-25 21:17:24 +00:00
|
|
|
*/
|
2017-12-28 15:31:56 +00:00
|
|
|
public function setConfig( Config $config ) {
|
|
|
|
|
$this->config = $config;
|
2013-10-25 21:17:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-01-24 01:45:11 +00:00
|
|
|
* @return Config
|
2013-10-25 21:17:24 +00:00
|
|
|
*/
|
|
|
|
|
public function getConfig() {
|
|
|
|
|
if ( $this->config === null ) {
|
Make abstract Config class truly implementation-agnostic
Follow up to I13baec0b6 ("Config: Add Config and GlobalConfig classes"):
Config:
* Rather than returning Status objects, Config::set will now throw an exception
if an error is encountered
* Config::factory was moved into it's own ConfigFactory class.
* Since there are no more functions in it, Config was turned into an interface.
GlobalConfig:
* Remove $prefix args from Config::set and ::get. The idea of having an
abstract Config class is to abstract some notion of configuration data from
the particular way in which it is currently implemented (global variables).
So the abstract base class has no business dealing with variable name
prefixes.
** Instead GlobalVarConfig's implementations of get and set call getWithPrefix
and setWithPrefix internally, which are now protected
* Rename GlobalConfig to GlobalVarConfig, which makes it clearer that it isn't
referring to the scope of the configuration value, but to the scope of the
variable name which provides it.
ConfigFactory:
* ConfigFactory is where Config objects are registered, and later constructed.
* Config objects are registered with a given name, and a callback factory function.
This allows for implementations to construct the object with the parameters they want,
and avoids the overhead of needing an entire class.
** The name 'main' is the default object returned by RequestContext::getConfig(),
and is intended to be used by core.
* This is a singleton class, the main instance can be obtained with:
ConfigFactory::getDefaultInstance()
In addition to the above:
* $wgConfigClass was removed, and $wgConfigRegistry was introduced, which
stores a name => callback. The name is to be what the Config instance is
registered with, and the callback should return an implementation of Config.
* Tests were written for the new ConfigFactory, and GlobalVarConfig's tests
were improved.
Co-Authored-By: Ori Livneh <ori@wikimedia.org>
Co-Authored-By: Chad Horohoe <chadh@wikimedia.org>
Co-Authored-By: Mattflaschen <mflaschen@wikimedia.org>
Co-Authored-By: Parent5446 <tylerromeo@gmail.com>
Co-Authored-By: Reedy <reedy@wikimedia.org>
Co-Authored-By: Daniel Kinzler <daniel.kinzler@wikimedia.de>
Change-Id: I5a5857fcfa07598ba4ce9ae5bbb4ce54a567d31e
2014-05-10 08:19:00 +00:00
|
|
|
// @todo In the future, we could move this to WebStart.php so
|
|
|
|
|
// the Config object is ready for when initialization happens
|
2016-11-22 23:39:22 +00:00
|
|
|
$this->config = MediaWikiServices::getInstance()->getMainConfig();
|
2013-10-25 21:17:24 +00:00
|
|
|
}
|
2014-02-05 11:02:29 +00:00
|
|
|
|
2013-10-25 21:17:24 +00:00
|
|
|
return $this->config;
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-03 11:09:49 +00:00
|
|
|
/**
|
2017-12-28 18:59:53 +00:00
|
|
|
* @param WebRequest $request
|
2011-04-03 11:09:49 +00:00
|
|
|
*/
|
2017-12-28 18:59:53 +00:00
|
|
|
public function setRequest( WebRequest $request ) {
|
|
|
|
|
$this->request = $request;
|
2011-04-03 11:09:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return WebRequest
|
|
|
|
|
*/
|
|
|
|
|
public function getRequest() {
|
2011-07-08 07:02:23 +00:00
|
|
|
if ( $this->request === null ) {
|
2015-11-17 18:43:47 +00:00
|
|
|
global $wgCommandLineMode;
|
|
|
|
|
// create the WebRequest object on the fly
|
|
|
|
|
if ( $wgCommandLineMode ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->request = new FauxRequest( [] );
|
2015-11-17 18:43:47 +00:00
|
|
|
} else {
|
|
|
|
|
$this->request = new WebRequest();
|
|
|
|
|
}
|
2011-04-03 11:09:49 +00:00
|
|
|
}
|
2013-11-20 05:21:57 +00:00
|
|
|
|
2011-06-27 19:23:54 +00:00
|
|
|
return $this->request;
|
2011-04-03 11:09:49 +00:00
|
|
|
}
|
|
|
|
|
|
2015-02-20 08:23:18 +00:00
|
|
|
/**
|
2016-04-11 18:28:17 +00:00
|
|
|
* @deprecated since 1.27 use a StatsdDataFactory from MediaWikiServices (preferably injected)
|
|
|
|
|
*
|
2017-07-08 00:19:39 +00:00
|
|
|
* @return IBufferingStatsdDataFactory
|
2015-02-20 08:23:18 +00:00
|
|
|
*/
|
|
|
|
|
public function getStats() {
|
2016-04-11 18:28:17 +00:00
|
|
|
return MediaWikiServices::getInstance()->getStatsdDataFactory();
|
2015-02-20 08:23:18 +00:00
|
|
|
}
|
|
|
|
|
|
2015-11-03 02:26:07 +00:00
|
|
|
/**
|
|
|
|
|
* @return Timing
|
|
|
|
|
*/
|
|
|
|
|
public function getTiming() {
|
|
|
|
|
if ( $this->timing === null ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->timing = new Timing( [
|
2015-12-10 22:58:11 +00:00
|
|
|
'logger' => LoggerFactory::getInstance( 'Timing' )
|
2016-02-17 09:09:32 +00:00
|
|
|
] );
|
2015-11-03 02:26:07 +00:00
|
|
|
}
|
|
|
|
|
return $this->timing;
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-03 11:09:49 +00:00
|
|
|
/**
|
2016-10-04 07:01:54 +00:00
|
|
|
* @param Title|null $title
|
2011-04-03 11:09:49 +00:00
|
|
|
*/
|
2016-03-07 23:29:20 +00:00
|
|
|
public function setTitle( Title $title = null ) {
|
2014-08-18 10:51:40 +00:00
|
|
|
$this->title = $title;
|
context: Add a cached RequestContext::getActionName method
This method is dependent on, and inherently must depend on, all of
Title, WikiPage, and WebRequest. And, like Title and WikiPage,
which also have getters in RequestContext, Action is also derived
from a query parameter that is widely recognised in almost all
web requests to index.php.
The status quo in core and extensions, is to obtain this value
via Action::getActionName(), which as a static method that bypasses
dependency injection and also has the problem of not being cached.
Caching it within ActionFactory seems hard and awkward, due to
varying by context.
In change I61d66211bd (22f9a32853f) a cached wrapper method was added
internally to the Skin class. In change I8cbc4bba4d248d9 (235820d631)
another cached wrapper was added in the Gadgets extension.
This change takes this approach further by making it a stable public
method on RequestContext.
To facilitate testing and to offer basic confidence in this working
correctly, this commit also adopts the new method in two place that
are considered "safe" (Skin, and OutputPage). Both of these are
called relatively late in the PHP proccess and well after any Setup
code and overrides (such as in MediaWiki.php), during which it is
more complex to call this. I'll audit and update those in a subsequent
change.
Change-Id: I1e259b54dca48a32be5a8c6cbb8eb69aec2da115
2021-11-06 00:28:23 +00:00
|
|
|
// Clear cache of derived getters
|
2012-10-24 14:32:46 +00:00
|
|
|
$this->wikipage = null;
|
context: Add a cached RequestContext::getActionName method
This method is dependent on, and inherently must depend on, all of
Title, WikiPage, and WebRequest. And, like Title and WikiPage,
which also have getters in RequestContext, Action is also derived
from a query parameter that is widely recognised in almost all
web requests to index.php.
The status quo in core and extensions, is to obtain this value
via Action::getActionName(), which as a static method that bypasses
dependency injection and also has the problem of not being cached.
Caching it within ActionFactory seems hard and awkward, due to
varying by context.
In change I61d66211bd (22f9a32853f) a cached wrapper method was added
internally to the Skin class. In change I8cbc4bba4d248d9 (235820d631)
another cached wrapper was added in the Gadgets extension.
This change takes this approach further by making it a stable public
method on RequestContext.
To facilitate testing and to offer basic confidence in this working
correctly, this commit also adopts the new method in two place that
are considered "safe" (Skin, and OutputPage). Both of these are
called relatively late in the PHP proccess and well after any Setup
code and overrides (such as in MediaWiki.php), during which it is
more complex to call this. I'll audit and update those in a subsequent
change.
Change-Id: I1e259b54dca48a32be5a8c6cbb8eb69aec2da115
2021-11-06 00:28:23 +00:00
|
|
|
$this->clearActionName();
|
2011-04-03 11:09:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-04-24 17:53:20 +00:00
|
|
|
* @return Title|null
|
2011-04-03 11:09:49 +00:00
|
|
|
*/
|
|
|
|
|
public function getTitle() {
|
2011-07-07 17:42:19 +00:00
|
|
|
if ( $this->title === null ) {
|
2011-04-03 11:09:49 +00:00
|
|
|
global $wgTitle; # fallback to $wg till we can improve this
|
2011-06-27 19:23:54 +00:00
|
|
|
$this->title = $wgTitle;
|
2019-12-20 13:12:59 +00:00
|
|
|
$logger = LoggerFactory::getInstance( 'GlobalTitleFail' );
|
|
|
|
|
$logger->info(
|
|
|
|
|
__METHOD__ . ' called with no title set.',
|
|
|
|
|
[ 'exception' => new Exception ]
|
2015-09-26 16:58:43 +00:00
|
|
|
);
|
2011-04-03 11:09:49 +00:00
|
|
|
}
|
2013-11-20 05:21:57 +00:00
|
|
|
|
2011-06-27 19:23:54 +00:00
|
|
|
return $this->title;
|
2011-04-03 11:09:49 +00:00
|
|
|
}
|
|
|
|
|
|
2014-09-26 17:18:43 +00:00
|
|
|
/**
|
|
|
|
|
* Check, if a Title object is set
|
|
|
|
|
*
|
|
|
|
|
* @since 1.25
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function hasTitle() {
|
|
|
|
|
return $this->title !== null;
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-14 14:27:46 +00:00
|
|
|
/**
|
|
|
|
|
* Check whether a WikiPage object can be get with getWikiPage().
|
|
|
|
|
* Callers should expect that an exception is thrown from getWikiPage()
|
|
|
|
|
* if this method returns false.
|
|
|
|
|
*
|
|
|
|
|
* @since 1.19
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function canUseWikiPage() {
|
2014-07-13 06:41:25 +00:00
|
|
|
if ( $this->wikipage ) {
|
|
|
|
|
// If there's a WikiPage object set, we can for sure get it
|
2012-01-14 14:27:46 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
2014-07-13 06:41:25 +00:00
|
|
|
// Only pages with legitimate titles can have WikiPages.
|
|
|
|
|
// That usually means pages in non-virtual namespaces.
|
2012-01-14 14:27:46 +00:00
|
|
|
$title = $this->getTitle();
|
2014-07-13 06:41:25 +00:00
|
|
|
return $title ? $title->canExist() : false;
|
2012-01-14 14:27:46 +00:00
|
|
|
}
|
|
|
|
|
|
2012-01-06 20:00:04 +00:00
|
|
|
/**
|
|
|
|
|
* @since 1.19
|
2017-12-28 18:59:53 +00:00
|
|
|
* @param WikiPage $wikiPage
|
2012-01-06 20:00:04 +00:00
|
|
|
*/
|
2017-12-28 18:59:53 +00:00
|
|
|
public function setWikiPage( WikiPage $wikiPage ) {
|
|
|
|
|
$pageTitle = $wikiPage->getTitle();
|
2014-12-12 19:17:55 +00:00
|
|
|
if ( !$this->hasTitle() || !$pageTitle->equals( $this->getTitle() ) ) {
|
2012-10-24 14:32:46 +00:00
|
|
|
$this->setTitle( $pageTitle );
|
|
|
|
|
}
|
|
|
|
|
// Defer this to the end since setTitle sets it to null.
|
2017-12-28 18:59:53 +00:00
|
|
|
$this->wikipage = $wikiPage;
|
context: Add a cached RequestContext::getActionName method
This method is dependent on, and inherently must depend on, all of
Title, WikiPage, and WebRequest. And, like Title and WikiPage,
which also have getters in RequestContext, Action is also derived
from a query parameter that is widely recognised in almost all
web requests to index.php.
The status quo in core and extensions, is to obtain this value
via Action::getActionName(), which as a static method that bypasses
dependency injection and also has the problem of not being cached.
Caching it within ActionFactory seems hard and awkward, due to
varying by context.
In change I61d66211bd (22f9a32853f) a cached wrapper method was added
internally to the Skin class. In change I8cbc4bba4d248d9 (235820d631)
another cached wrapper was added in the Gadgets extension.
This change takes this approach further by making it a stable public
method on RequestContext.
To facilitate testing and to offer basic confidence in this working
correctly, this commit also adopts the new method in two place that
are considered "safe" (Skin, and OutputPage). Both of these are
called relatively late in the PHP proccess and well after any Setup
code and overrides (such as in MediaWiki.php), during which it is
more complex to call this. I'll audit and update those in a subsequent
change.
Change-Id: I1e259b54dca48a32be5a8c6cbb8eb69aec2da115
2021-11-06 00:28:23 +00:00
|
|
|
// Clear cache of derived getter
|
|
|
|
|
$this->clearActionName();
|
2012-01-06 20:00:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2012-01-14 14:27:46 +00:00
|
|
|
* Get the WikiPage object.
|
|
|
|
|
* May throw an exception if there's no Title object set or the Title object
|
|
|
|
|
* belongs to a special namespace that doesn't have WikiPage, so use first
|
|
|
|
|
* canUseWikiPage() to check whether this method can be called safely.
|
2012-01-06 20:00:04 +00:00
|
|
|
*
|
|
|
|
|
* @since 1.19
|
2012-10-07 23:35:26 +00:00
|
|
|
* @throws MWException
|
2012-01-06 20:00:04 +00:00
|
|
|
* @return WikiPage
|
|
|
|
|
*/
|
|
|
|
|
public function getWikiPage() {
|
|
|
|
|
if ( $this->wikipage === null ) {
|
|
|
|
|
$title = $this->getTitle();
|
|
|
|
|
if ( $title === null ) {
|
|
|
|
|
throw new MWException( __METHOD__ . ' called without Title object set' );
|
|
|
|
|
}
|
2020-11-11 21:46:45 +00:00
|
|
|
$this->wikipage = MediaWikiServices::getInstance()->getWikiPageFactory()->newFromTitle( $title );
|
2012-01-06 20:00:04 +00:00
|
|
|
}
|
2013-11-20 05:21:57 +00:00
|
|
|
|
2012-01-06 20:00:04 +00:00
|
|
|
return $this->wikipage;
|
|
|
|
|
}
|
|
|
|
|
|
context: Add a cached RequestContext::getActionName method
This method is dependent on, and inherently must depend on, all of
Title, WikiPage, and WebRequest. And, like Title and WikiPage,
which also have getters in RequestContext, Action is also derived
from a query parameter that is widely recognised in almost all
web requests to index.php.
The status quo in core and extensions, is to obtain this value
via Action::getActionName(), which as a static method that bypasses
dependency injection and also has the problem of not being cached.
Caching it within ActionFactory seems hard and awkward, due to
varying by context.
In change I61d66211bd (22f9a32853f) a cached wrapper method was added
internally to the Skin class. In change I8cbc4bba4d248d9 (235820d631)
another cached wrapper was added in the Gadgets extension.
This change takes this approach further by making it a stable public
method on RequestContext.
To facilitate testing and to offer basic confidence in this working
correctly, this commit also adopts the new method in two place that
are considered "safe" (Skin, and OutputPage). Both of these are
called relatively late in the PHP proccess and well after any Setup
code and overrides (such as in MediaWiki.php), during which it is
more complex to call this. I'll audit and update those in a subsequent
change.
Change-Id: I1e259b54dca48a32be5a8c6cbb8eb69aec2da115
2021-11-06 00:28:23 +00:00
|
|
|
/**
|
|
|
|
|
* @since 1.38
|
|
|
|
|
* @param string $action
|
|
|
|
|
*/
|
|
|
|
|
public function setActionName( string $action ): void {
|
|
|
|
|
$this->action = $action;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the action name for the current web request.
|
|
|
|
|
*
|
|
|
|
|
* This generally returns "view" if the current request or process is
|
|
|
|
|
* not for a skinned index.php web request (e.g. load.php, thumb.php,
|
|
|
|
|
* job runner, CLI, API).
|
|
|
|
|
*
|
|
|
|
|
* @warning This must not be called before or during the Setup.php phase,
|
|
|
|
|
* and may cause an error or warning if called too early.
|
|
|
|
|
*
|
|
|
|
|
* @since 1.38
|
|
|
|
|
* @return string Action
|
|
|
|
|
*/
|
|
|
|
|
public function getActionName(): string {
|
|
|
|
|
// Optimisation: This is cached to avoid repeated running of the
|
|
|
|
|
// expensive operations to compute this. The computation involves creation
|
|
|
|
|
// of Article, WikiPage, and ContentHandler objects (and the various
|
|
|
|
|
// database queries these classes require to be instantiated), as well
|
|
|
|
|
// as potentially slow extension hooks at various level in these
|
|
|
|
|
// classes.
|
|
|
|
|
//
|
|
|
|
|
// This is value frequently needed in OutputPage and in various
|
|
|
|
|
// Skin-related methods and classes.
|
|
|
|
|
if ( $this->action === null ) {
|
|
|
|
|
$this->action = MediaWikiServices::getInstance()
|
|
|
|
|
->getActionFactory()
|
|
|
|
|
->getActionName( $this );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->action;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function clearActionName(): void {
|
|
|
|
|
if ( $this->action !== null ) {
|
|
|
|
|
// Log if cleared after something already computed it as that is
|
|
|
|
|
// likely to cause bugs (given the first caller may be using it for
|
|
|
|
|
// something), and also because it's an expensive thing to needlessly
|
|
|
|
|
// compute multiple times even when it produces the same value.
|
|
|
|
|
//
|
|
|
|
|
// TODO: Once confident we don't rely on this,
|
|
|
|
|
// change to E_USER_WARNING with trigger_error and silence error
|
|
|
|
|
// in relevant tests.
|
|
|
|
|
$logger = LoggerFactory::getInstance( 'Setup' );
|
|
|
|
|
$logger->warning( 'Changing action after getActionName was already called',
|
|
|
|
|
[ 'exception' => new Exception ]
|
|
|
|
|
);
|
|
|
|
|
$this->action = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-17 15:07:51 +00:00
|
|
|
/**
|
2017-12-28 18:59:53 +00:00
|
|
|
* @param OutputPage $output
|
2011-06-17 15:07:51 +00:00
|
|
|
*/
|
2017-12-28 18:59:53 +00:00
|
|
|
public function setOutput( OutputPage $output ) {
|
|
|
|
|
$this->output = $output;
|
2011-06-17 15:07:51 +00:00
|
|
|
}
|
|
|
|
|
|
2011-04-03 11:09:49 +00:00
|
|
|
/**
|
2013-01-11 15:56:04 +00:00
|
|
|
* @return OutputPage
|
2011-04-03 11:09:49 +00:00
|
|
|
*/
|
|
|
|
|
public function getOutput() {
|
2011-07-07 17:42:19 +00:00
|
|
|
if ( $this->output === null ) {
|
2011-06-27 19:23:54 +00:00
|
|
|
$this->output = new OutputPage( $this );
|
2011-04-03 11:09:49 +00:00
|
|
|
}
|
2013-11-20 05:21:57 +00:00
|
|
|
|
2011-06-27 19:23:54 +00:00
|
|
|
return $this->output;
|
2011-04-03 11:09:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2017-12-28 18:59:53 +00:00
|
|
|
* @param User $user
|
2011-04-03 11:09:49 +00:00
|
|
|
*/
|
2017-12-28 18:59:53 +00:00
|
|
|
public function setUser( User $user ) {
|
|
|
|
|
$this->user = $user;
|
2021-01-08 01:01:28 +00:00
|
|
|
// Keep authority consistent
|
|
|
|
|
$this->authority = $user;
|
2019-01-25 21:50:34 +00:00
|
|
|
// Invalidate cached user interface language
|
|
|
|
|
$this->lang = null;
|
2011-04-03 11:09:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return User
|
|
|
|
|
*/
|
|
|
|
|
public function getUser() {
|
2011-07-07 17:42:19 +00:00
|
|
|
if ( $this->user === null ) {
|
2022-07-02 11:58:44 +00:00
|
|
|
if ( $this->authority !== null ) {
|
|
|
|
|
// Keep user consistent by using a possible set authority
|
|
|
|
|
$this->user = MediaWikiServices::getInstance()
|
|
|
|
|
->getUserFactory()
|
|
|
|
|
->newFromAuthority( $this->authority );
|
|
|
|
|
} else {
|
|
|
|
|
$this->user = User::newFromSession( $this->getRequest() );
|
|
|
|
|
}
|
2011-04-03 11:09:49 +00:00
|
|
|
}
|
2013-11-20 05:21:57 +00:00
|
|
|
|
2011-06-27 19:23:54 +00:00
|
|
|
return $this->user;
|
2011-04-03 11:09:49 +00:00
|
|
|
}
|
|
|
|
|
|
2021-05-09 23:17:47 +00:00
|
|
|
/**
|
|
|
|
|
* @param Authority $authority
|
|
|
|
|
*/
|
2021-01-08 01:01:28 +00:00
|
|
|
public function setAuthority( Authority $authority ) {
|
|
|
|
|
$this->authority = $authority;
|
2022-07-02 11:58:44 +00:00
|
|
|
// If needed, a User object is constructed from this authority
|
|
|
|
|
$this->user = null;
|
2021-01-08 01:01:28 +00:00
|
|
|
// Invalidate cached user interface language
|
|
|
|
|
$this->lang = null;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-13 18:38:36 +00:00
|
|
|
/**
|
|
|
|
|
* @since 1.36
|
|
|
|
|
* @return Authority
|
|
|
|
|
*/
|
2021-01-08 01:01:28 +00:00
|
|
|
public function getAuthority(): Authority {
|
|
|
|
|
return $this->authority ?: $this->getUser();
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-05 15:25:20 +00:00
|
|
|
/**
|
2021-11-22 13:35:17 +00:00
|
|
|
* Accepts a language code and ensures it's sensible. Outputs a cleaned up language
|
|
|
|
|
* code and replaces with $wgLanguageCode if not sensible.
|
2013-01-11 15:56:04 +00:00
|
|
|
* @param string $code Language code
|
2011-12-19 01:07:30 +00:00
|
|
|
* @return string
|
2011-09-05 15:25:20 +00:00
|
|
|
*/
|
2011-12-19 19:07:18 +00:00
|
|
|
public static function sanitizeLangCode( $code ) {
|
2011-09-05 15:25:20 +00:00
|
|
|
global $wgLanguageCode;
|
|
|
|
|
|
|
|
|
|
// BCP 47 - letter case MUST NOT carry meaning
|
|
|
|
|
$code = strtolower( $code );
|
|
|
|
|
|
|
|
|
|
# Validate $code
|
2020-01-03 23:03:14 +00:00
|
|
|
if ( !$code
|
|
|
|
|
|| !MediaWikiServices::getInstance()->getLanguageNameUtils()
|
|
|
|
|
->isValidCode( $code )
|
|
|
|
|
|| $code === 'qqq'
|
|
|
|
|
) {
|
2011-09-05 15:25:20 +00:00
|
|
|
$code = $wgLanguageCode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $code;
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-23 10:28:21 +00:00
|
|
|
/**
|
2017-12-28 18:59:53 +00:00
|
|
|
* @param Language|string $language Language instance or language code
|
2012-10-07 23:35:26 +00:00
|
|
|
* @throws MWException
|
2011-12-05 18:56:09 +00:00
|
|
|
* @since 1.19
|
2011-11-23 10:28:21 +00:00
|
|
|
*/
|
2017-12-28 18:59:53 +00:00
|
|
|
public function setLanguage( $language ) {
|
|
|
|
|
if ( $language instanceof Language ) {
|
|
|
|
|
$this->lang = $language;
|
|
|
|
|
} elseif ( is_string( $language ) ) {
|
|
|
|
|
$language = self::sanitizeLangCode( $language );
|
2019-08-26 12:24:37 +00:00
|
|
|
$obj = MediaWikiServices::getInstance()->getLanguageFactory()->getLanguage( $language );
|
2011-09-05 15:25:20 +00:00
|
|
|
$this->lang = $obj;
|
|
|
|
|
} else {
|
|
|
|
|
throw new MWException( __METHOD__ . " was passed an invalid type of data." );
|
|
|
|
|
}
|
2011-07-09 19:38:58 +00:00
|
|
|
}
|
|
|
|
|
|
2011-04-03 11:09:49 +00:00
|
|
|
/**
|
2013-01-16 09:47:06 +00:00
|
|
|
* Get the Language object.
|
|
|
|
|
* Initialization of user or request objects can depend on this.
|
2011-04-03 11:09:49 +00:00
|
|
|
* @return Language
|
2020-07-23 22:08:40 +00:00
|
|
|
* @throws LogicException
|
2011-12-05 18:56:09 +00:00
|
|
|
* @since 1.19
|
2011-04-03 11:09:49 +00:00
|
|
|
*/
|
2011-11-21 16:13:21 +00:00
|
|
|
public function getLanguage() {
|
2019-09-08 16:50:13 +00:00
|
|
|
if ( $this->languageRecursion === true ) {
|
2020-07-23 22:08:40 +00:00
|
|
|
throw new LogicException( 'Recursion detected' );
|
|
|
|
|
}
|
2013-02-08 05:38:41 +00:00
|
|
|
|
2020-07-23 22:08:40 +00:00
|
|
|
if ( $this->lang === null ) {
|
2019-09-08 16:50:13 +00:00
|
|
|
$this->languageRecursion = true;
|
2013-01-16 09:47:06 +00:00
|
|
|
|
2014-06-24 13:55:39 +00:00
|
|
|
try {
|
|
|
|
|
$request = $this->getRequest();
|
|
|
|
|
$user = $this->getUser();
|
2013-01-16 09:47:06 +00:00
|
|
|
|
2019-07-29 22:57:43 +00:00
|
|
|
// Optimisation: Avoid slow getVal(), this isn't user-generated content.
|
|
|
|
|
$code = $request->getRawVal( 'uselang', 'user' );
|
2015-01-05 16:59:48 +00:00
|
|
|
if ( $code === 'user' ) {
|
2022-04-29 19:46:30 +00:00
|
|
|
$userOptionsLookup = MediaWikiServices::getInstance()
|
|
|
|
|
->getUserOptionsLookup();
|
|
|
|
|
$code = $userOptionsLookup->getOption( $user, 'language' );
|
2015-01-05 16:59:48 +00:00
|
|
|
}
|
2019-10-17 02:50:33 +00:00
|
|
|
|
|
|
|
|
// There are certain characters we don't allow in language code strings,
|
|
|
|
|
// but by and large almost any valid UTF-8 string will makes it past
|
|
|
|
|
// this check and the LanguageNameUtils::isValidCode method it uses.
|
|
|
|
|
// This is to support on-wiki interface message overrides for
|
|
|
|
|
// non-existent language codes. Also known as "Uselang hacks".
|
|
|
|
|
// See <https://www.mediawiki.org/wiki/Manual:Uselang_hack>
|
|
|
|
|
// For something like "en-whatever" or "de-whatever" it will end up
|
|
|
|
|
// with a mostly "en" or "de" interface, but with an extra layer of
|
|
|
|
|
// possible MessageCache overrides from `MediaWiki:*/<code>` titles.
|
|
|
|
|
// While non-ASCII works here, it is required that they are in
|
|
|
|
|
// NFC form given this will not convert to normalised form.
|
2014-06-24 13:55:39 +00:00
|
|
|
$code = self::sanitizeLangCode( $code );
|
2011-04-03 11:09:49 +00:00
|
|
|
|
Hooks::run() call site migration
Migrate all callers of Hooks::run() to use the new
HookContainer/HookRunner system.
General principles:
* Use DI if it is already used. We're not changing the way state is
managed in this patch.
* HookContainer is always injected, not HookRunner. HookContainer
is a service, it's a more generic interface, it is the only
thing that provides isRegistered() which is needed in some cases,
and a HookRunner can be efficiently constructed from it
(confirmed by benchmark). Because HookContainer is needed
for object construction, it is also needed by all factories.
* "Ask your friendly local base class". Big hierarchies like
SpecialPage and ApiBase have getHookContainer() and getHookRunner()
methods in the base class, and classes that extend that base class
are not expected to know or care where the base class gets its
HookContainer from.
* ProtectedHookAccessorTrait provides protected getHookContainer() and
getHookRunner() methods, getting them from the global service
container. The point of this is to ease migration to DI by ensuring
that call sites ask their local friendly base class rather than
getting a HookRunner from the service container directly.
* Private $this->hookRunner. In some smaller classes where accessor
methods did not seem warranted, there is a private HookRunner property
which is accessed directly. Very rarely (two cases), there is a
protected property, for consistency with code that conventionally
assumes protected=private, but in cases where the class might actually
be overridden, a protected accessor is preferred over a protected
property.
* The last resort: Hooks::runner(). Mostly for static, file-scope and
global code. In a few cases it was used for objects with broken
construction schemes, out of horror or laziness.
Constructors with new required arguments:
* AuthManager
* BadFileLookup
* BlockManager
* ClassicInterwikiLookup
* ContentHandlerFactory
* ContentSecurityPolicy
* DefaultOptionsManager
* DerivedPageDataUpdater
* FullSearchResultWidget
* HtmlCacheUpdater
* LanguageFactory
* LanguageNameUtils
* LinkRenderer
* LinkRendererFactory
* LocalisationCache
* MagicWordFactory
* MessageCache
* NamespaceInfo
* PageEditStash
* PageHandlerFactory
* PageUpdater
* ParserFactory
* PermissionManager
* RevisionStore
* RevisionStoreFactory
* SearchEngineConfig
* SearchEngineFactory
* SearchFormWidget
* SearchNearMatcher
* SessionBackend
* SpecialPageFactory
* UserNameUtils
* UserOptionsManager
* WatchedItemQueryService
* WatchedItemStore
Constructors with new optional arguments:
* DefaultPreferencesFactory
* Language
* LinkHolderArray
* MovePage
* Parser
* ParserCache
* PasswordReset
* Router
setHookContainer() now required after construction:
* AuthenticationProvider
* ResourceLoaderModule
* SearchEngine
Change-Id: Id442b0dbe43aba84bd5cf801d86dedc768b082c7
2020-03-19 02:42:09 +00:00
|
|
|
Hooks::runner()->onUserGetLanguageObject( $user, $code, $this );
|
2011-04-03 11:09:49 +00:00
|
|
|
|
2022-04-13 15:28:26 +00:00
|
|
|
if ( $code === $this->getConfig()->get( MainConfigNames::LanguageCode ) ) {
|
2018-07-29 12:24:54 +00:00
|
|
|
$this->lang = MediaWikiServices::getInstance()->getContentLanguage();
|
2014-06-24 13:55:39 +00:00
|
|
|
} else {
|
2019-08-26 12:24:37 +00:00
|
|
|
$obj = MediaWikiServices::getInstance()->getLanguageFactory()
|
|
|
|
|
->getLanguage( $code );
|
2014-06-24 13:55:39 +00:00
|
|
|
$this->lang = $obj;
|
|
|
|
|
}
|
2019-03-26 13:13:51 +00:00
|
|
|
} finally {
|
2019-09-08 16:50:13 +00:00
|
|
|
$this->languageRecursion = false;
|
2014-06-24 13:55:39 +00:00
|
|
|
}
|
2011-04-03 11:09:49 +00:00
|
|
|
}
|
2013-01-16 09:47:06 +00:00
|
|
|
|
2011-06-27 19:23:54 +00:00
|
|
|
return $this->lang;
|
2011-04-03 11:09:49 +00:00
|
|
|
}
|
|
|
|
|
|
2011-07-09 19:38:58 +00:00
|
|
|
/**
|
2017-12-28 18:59:53 +00:00
|
|
|
* @param Skin $skin
|
2011-07-09 19:38:58 +00:00
|
|
|
*/
|
2017-12-28 18:59:53 +00:00
|
|
|
public function setSkin( Skin $skin ) {
|
|
|
|
|
$this->skin = clone $skin;
|
2011-07-09 19:38:58 +00:00
|
|
|
$this->skin->setContext( $this );
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-03 11:09:49 +00:00
|
|
|
/**
|
|
|
|
|
* @return Skin
|
|
|
|
|
*/
|
|
|
|
|
public function getSkin() {
|
2011-07-07 17:42:19 +00:00
|
|
|
if ( $this->skin === null ) {
|
2012-01-03 01:58:27 +00:00
|
|
|
$skin = null;
|
Hooks::run() call site migration
Migrate all callers of Hooks::run() to use the new
HookContainer/HookRunner system.
General principles:
* Use DI if it is already used. We're not changing the way state is
managed in this patch.
* HookContainer is always injected, not HookRunner. HookContainer
is a service, it's a more generic interface, it is the only
thing that provides isRegistered() which is needed in some cases,
and a HookRunner can be efficiently constructed from it
(confirmed by benchmark). Because HookContainer is needed
for object construction, it is also needed by all factories.
* "Ask your friendly local base class". Big hierarchies like
SpecialPage and ApiBase have getHookContainer() and getHookRunner()
methods in the base class, and classes that extend that base class
are not expected to know or care where the base class gets its
HookContainer from.
* ProtectedHookAccessorTrait provides protected getHookContainer() and
getHookRunner() methods, getting them from the global service
container. The point of this is to ease migration to DI by ensuring
that call sites ask their local friendly base class rather than
getting a HookRunner from the service container directly.
* Private $this->hookRunner. In some smaller classes where accessor
methods did not seem warranted, there is a private HookRunner property
which is accessed directly. Very rarely (two cases), there is a
protected property, for consistency with code that conventionally
assumes protected=private, but in cases where the class might actually
be overridden, a protected accessor is preferred over a protected
property.
* The last resort: Hooks::runner(). Mostly for static, file-scope and
global code. In a few cases it was used for objects with broken
construction schemes, out of horror or laziness.
Constructors with new required arguments:
* AuthManager
* BadFileLookup
* BlockManager
* ClassicInterwikiLookup
* ContentHandlerFactory
* ContentSecurityPolicy
* DefaultOptionsManager
* DerivedPageDataUpdater
* FullSearchResultWidget
* HtmlCacheUpdater
* LanguageFactory
* LanguageNameUtils
* LinkRenderer
* LinkRendererFactory
* LocalisationCache
* MagicWordFactory
* MessageCache
* NamespaceInfo
* PageEditStash
* PageHandlerFactory
* PageUpdater
* ParserFactory
* PermissionManager
* RevisionStore
* RevisionStoreFactory
* SearchEngineConfig
* SearchEngineFactory
* SearchFormWidget
* SearchNearMatcher
* SessionBackend
* SpecialPageFactory
* UserNameUtils
* UserOptionsManager
* WatchedItemQueryService
* WatchedItemStore
Constructors with new optional arguments:
* DefaultPreferencesFactory
* Language
* LinkHolderArray
* MovePage
* Parser
* ParserCache
* PasswordReset
* Router
setHookContainer() now required after construction:
* AuthenticationProvider
* ResourceLoaderModule
* SearchEngine
Change-Id: Id442b0dbe43aba84bd5cf801d86dedc768b082c7
2020-03-19 02:42:09 +00:00
|
|
|
Hooks::runner()->onRequestContextCreateSkin( $this, $skin );
|
2019-04-15 19:21:21 +00:00
|
|
|
$factory = MediaWikiServices::getInstance()->getSkinFactory();
|
2012-01-03 01:58:27 +00:00
|
|
|
|
|
|
|
|
if ( $skin instanceof Skin ) {
|
2019-07-29 22:42:57 +00:00
|
|
|
// The hook provided a skin object
|
2012-01-03 01:58:27 +00:00
|
|
|
$this->skin = $skin;
|
2013-01-11 15:56:04 +00:00
|
|
|
} elseif ( is_string( $skin ) ) {
|
2019-07-29 22:42:57 +00:00
|
|
|
// The hook provided a skin name
|
2014-08-14 20:47:24 +00:00
|
|
|
// Normalize the key, just in case the hook did something weird.
|
|
|
|
|
$normalized = Skin::normalizeKey( $skin );
|
|
|
|
|
$this->skin = $factory->makeSkin( $normalized );
|
2019-07-29 22:42:57 +00:00
|
|
|
} else {
|
|
|
|
|
// No hook override, go through normal processing
|
2022-04-13 15:28:26 +00:00
|
|
|
if ( !in_array( 'skin',
|
|
|
|
|
$this->getConfig()->get( MainConfigNames::HiddenPrefs ) ) ) {
|
2022-04-29 19:46:30 +00:00
|
|
|
$userOptionsLookup = MediaWikiServices::getInstance()
|
|
|
|
|
->getUserOptionsLookup();
|
|
|
|
|
$userSkin = $userOptionsLookup->getOption( $this->getUser(), 'skin' );
|
2019-07-29 22:57:43 +00:00
|
|
|
// Optimisation: Avoid slow getVal(), this isn't user-generated content.
|
|
|
|
|
$userSkin = $this->getRequest()->getRawVal( 'useskin', $userSkin );
|
2012-01-03 01:58:27 +00:00
|
|
|
} else {
|
2022-04-13 15:28:26 +00:00
|
|
|
$userSkin = $this->getConfig()->get( MainConfigNames::DefaultSkin );
|
2012-01-03 01:58:27 +00:00
|
|
|
}
|
|
|
|
|
|
2019-07-29 22:42:57 +00:00
|
|
|
// Normalize the key in case the user is passing gibberish query params
|
|
|
|
|
// or has old user preferences (T71566).
|
|
|
|
|
// Skin::normalizeKey will also validate it, so makeSkin() won't throw.
|
2014-08-14 20:47:24 +00:00
|
|
|
$normalized = Skin::normalizeKey( $userSkin );
|
|
|
|
|
$this->skin = $factory->makeSkin( $normalized );
|
2011-04-03 12:46:36 +00:00
|
|
|
}
|
|
|
|
|
|
2012-01-03 01:58:27 +00:00
|
|
|
// After all that set a context on whatever skin got created
|
2011-06-27 19:23:54 +00:00
|
|
|
$this->skin->setContext( $this );
|
2011-04-03 12:46:36 +00:00
|
|
|
}
|
2013-11-20 05:21:57 +00:00
|
|
|
|
2011-06-27 19:23:54 +00:00
|
|
|
return $this->skin;
|
2011-04-03 11:09:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get a Message object with context set
|
|
|
|
|
* Parameters are the same as wfMessage()
|
|
|
|
|
*
|
2017-06-12 09:53:51 +00:00
|
|
|
* @param string|string[]|MessageSpecifier $key Message key, or array of keys,
|
|
|
|
|
* or a MessageSpecifier.
|
2018-06-08 20:35:15 +00:00
|
|
|
* @param mixed ...$params
|
2013-01-11 15:56:04 +00:00
|
|
|
* @return Message
|
2011-04-03 11:09:49 +00:00
|
|
|
*/
|
2018-06-08 20:35:15 +00:00
|
|
|
public function msg( $key, ...$params ) {
|
|
|
|
|
return wfMessage( $key, ...$params )->setContext( $this );
|
2011-04-03 11:09:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the RequestContext object associated with the main request
|
|
|
|
|
*
|
2013-01-11 15:56:04 +00:00
|
|
|
* @return RequestContext
|
2011-04-03 11:09:49 +00:00
|
|
|
*/
|
2021-07-22 03:11:47 +00:00
|
|
|
public static function getMain(): RequestContext {
|
2014-06-27 17:59:47 +00:00
|
|
|
if ( self::$instance === null ) {
|
|
|
|
|
self::$instance = new self;
|
2011-04-03 11:09:49 +00:00
|
|
|
}
|
2013-11-20 05:21:57 +00:00
|
|
|
|
2014-06-27 17:59:47 +00:00
|
|
|
return self::$instance;
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-22 20:36:59 +00:00
|
|
|
/**
|
|
|
|
|
* Get the RequestContext object associated with the main request
|
|
|
|
|
* and gives a warning to the log, to find places, where a context maybe is missing.
|
|
|
|
|
*
|
2014-08-25 16:50:35 +00:00
|
|
|
* @param string $func
|
2014-08-22 20:36:59 +00:00
|
|
|
* @return RequestContext
|
|
|
|
|
* @since 1.24
|
|
|
|
|
*/
|
|
|
|
|
public static function getMainAndWarn( $func = __METHOD__ ) {
|
|
|
|
|
wfDebug( $func . ' called without context. ' .
|
2021-11-19 23:19:42 +00:00
|
|
|
"Using RequestContext::getMain()" );
|
2014-08-22 20:36:59 +00:00
|
|
|
|
|
|
|
|
return self::getMain();
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-27 17:59:47 +00:00
|
|
|
/**
|
|
|
|
|
* Resets singleton returned by getMain(). Should be called only from unit tests.
|
|
|
|
|
*/
|
|
|
|
|
public static function resetMain() {
|
2016-04-11 20:37:32 +00:00
|
|
|
if ( !( defined( 'MW_PHPUNIT_TEST' ) || defined( 'MW_PARSER_TEST' ) ) ) {
|
|
|
|
|
throw new MWException( __METHOD__ . '() should be called only from unit tests!' );
|
|
|
|
|
}
|
2014-06-27 17:59:47 +00:00
|
|
|
self::$instance = null;
|
2011-04-03 11:09:49 +00:00
|
|
|
}
|
2011-09-05 15:25:20 +00:00
|
|
|
|
2013-02-13 21:25:37 +00:00
|
|
|
/**
|
2013-03-14 22:43:42 +00:00
|
|
|
* Export the resolved user IP, HTTP headers, user ID, and session ID.
|
|
|
|
|
* The result will be reasonably sized to allow for serialization.
|
|
|
|
|
*
|
2014-04-14 19:43:18 +00:00
|
|
|
* @return array
|
2013-03-14 22:43:42 +00:00
|
|
|
* @since 1.21
|
|
|
|
|
*/
|
|
|
|
|
public function exportSession() {
|
2016-02-01 20:44:03 +00:00
|
|
|
$session = MediaWiki\Session\SessionManager::getGlobalSession();
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
2013-04-20 21:11:46 +00:00
|
|
|
'ip' => $this->getRequest()->getIP(),
|
|
|
|
|
'headers' => $this->getRequest()->getAllHeaders(),
|
2016-02-01 20:44:03 +00:00
|
|
|
'sessionId' => $session->isPersistent() ? $session->getId() : '',
|
2013-04-20 21:11:46 +00:00
|
|
|
'userId' => $this->getUser()->getId()
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2013-03-14 22:43:42 +00:00
|
|
|
}
|
|
|
|
|
|
2021-07-22 03:11:47 +00:00
|
|
|
public function getCsrfTokenSet(): CsrfTokenSet {
|
2021-06-02 22:13:53 +00:00
|
|
|
return new CsrfTokenSet( $this->getRequest() );
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-14 22:43:42 +00:00
|
|
|
/**
|
2014-09-29 23:53:26 +00:00
|
|
|
* Import an client IP address, HTTP headers, user ID, and session ID
|
|
|
|
|
*
|
2015-07-09 21:32:04 +00:00
|
|
|
* This sets the current session, $wgUser, and $wgRequest from $params.
|
2013-02-13 21:25:37 +00:00
|
|
|
* Once the return value falls out of scope, the old context is restored.
|
2015-07-09 21:32:04 +00:00
|
|
|
* This method should only be called in contexts where there is no session
|
|
|
|
|
* ID or end user receiving the response (CLI or HTTP job runners). This
|
2014-09-29 23:53:26 +00:00
|
|
|
* is partly enforced, and is done so to avoid leaking cookies if certain
|
|
|
|
|
* error conditions arise.
|
2013-02-13 21:25:37 +00:00
|
|
|
*
|
2015-07-09 21:32:04 +00:00
|
|
|
* This is useful when background scripts inherit context when acting on
|
|
|
|
|
* behalf of a user. In general the 'sessionId' parameter should be set
|
|
|
|
|
* to an empty string unless session importing is *truly* needed. This
|
|
|
|
|
* feature is somewhat deprecated.
|
2013-02-13 21:25:37 +00:00
|
|
|
*
|
2013-07-08 01:37:02 +00:00
|
|
|
* @note suhosin.session.encrypt may interfere with this method.
|
|
|
|
|
*
|
|
|
|
|
* @param array $params Result of RequestContext::exportSession()
|
2013-02-13 21:25:37 +00:00
|
|
|
* @return ScopedCallback
|
|
|
|
|
* @throws MWException
|
|
|
|
|
* @since 1.21
|
|
|
|
|
*/
|
|
|
|
|
public static function importScopedSession( array $params ) {
|
2016-02-01 20:44:03 +00:00
|
|
|
if ( strlen( $params['sessionId'] ) &&
|
|
|
|
|
MediaWiki\Session\SessionManager::getGlobalSession()->isPersistent()
|
|
|
|
|
) {
|
2021-11-19 23:19:42 +00:00
|
|
|
// Check to avoid sending random cookies for the wrong users.
|
2014-09-29 23:53:26 +00:00
|
|
|
// This method should only called by CLI scripts or by HTTP job runners.
|
|
|
|
|
throw new MWException( "Sessions can only be imported when none is active." );
|
2019-06-25 18:53:15 +00:00
|
|
|
} elseif ( !IPUtils::isValid( $params['ip'] ) ) {
|
2014-09-29 23:53:26 +00:00
|
|
|
throw new MWException( "Invalid client IP address '{$params['ip']}'." );
|
2013-02-13 21:25:37 +00:00
|
|
|
}
|
|
|
|
|
|
2013-03-14 22:43:42 +00:00
|
|
|
if ( $params['userId'] ) { // logged-in user
|
|
|
|
|
$user = User::newFromId( $params['userId'] );
|
2014-07-14 05:03:00 +00:00
|
|
|
$user->load();
|
2022-04-29 18:32:20 +00:00
|
|
|
if ( !$user->isRegistered() ) {
|
2013-03-14 22:43:42 +00:00
|
|
|
throw new MWException( "No user with ID '{$params['userId']}'." );
|
|
|
|
|
}
|
|
|
|
|
} else { // anon user
|
|
|
|
|
$user = User::newFromName( $params['ip'], false );
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-10 22:31:02 +00:00
|
|
|
$importSessionFunc = static function ( User $user, array $params ) {
|
2021-09-04 19:19:47 +00:00
|
|
|
global $wgRequest;
|
2013-02-13 21:25:37 +00:00
|
|
|
|
2013-03-14 22:43:42 +00:00
|
|
|
$context = RequestContext::getMain();
|
2016-02-01 20:44:03 +00:00
|
|
|
|
2013-03-14 22:43:42 +00:00
|
|
|
// Commit and close any current session
|
2016-02-01 20:44:03 +00:00
|
|
|
if ( MediaWiki\Session\PHPSessionHandler::isEnabled() ) {
|
|
|
|
|
session_write_close(); // persist
|
|
|
|
|
session_id( '' ); // detach
|
2016-02-17 09:09:32 +00:00
|
|
|
$_SESSION = []; // clear in-memory array
|
2016-02-01 20:44:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get new session, if applicable
|
|
|
|
|
$session = null;
|
|
|
|
|
if ( strlen( $params['sessionId'] ) ) { // don't make a new random ID
|
|
|
|
|
$manager = MediaWiki\Session\SessionManager::singleton();
|
|
|
|
|
$session = $manager->getSessionById( $params['sessionId'], true )
|
|
|
|
|
?: $manager->getEmptySession();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Remove any user IP or agent information, and attach the request
|
|
|
|
|
// with the new session.
|
2016-02-17 09:09:32 +00:00
|
|
|
$context->setRequest( new FauxRequest( [], false, $session ) );
|
2013-03-14 22:43:42 +00:00
|
|
|
$wgRequest = $context->getRequest(); // b/c
|
2016-02-01 20:44:03 +00:00
|
|
|
|
2013-03-14 22:43:42 +00:00
|
|
|
// Now that all private information is detached from the user, it should
|
|
|
|
|
// be safe to load the new user. If errors occur or an exception is thrown
|
|
|
|
|
// and caught (leaving the main context in a mixed state), there is no risk
|
|
|
|
|
// of the User object being attached to the wrong IP, headers, or session.
|
|
|
|
|
$context->setUser( $user );
|
2021-09-04 19:19:47 +00:00
|
|
|
StubGlobalUser::setUser( $context->getUser() ); // b/c
|
2016-02-01 20:44:03 +00:00
|
|
|
if ( $session && MediaWiki\Session\PHPSessionHandler::isEnabled() ) {
|
|
|
|
|
session_id( $session->getId() );
|
2019-05-26 21:46:15 +00:00
|
|
|
AtEase::quietCall( 'session_start' );
|
2013-02-13 21:25:37 +00:00
|
|
|
}
|
2016-02-17 09:09:32 +00:00
|
|
|
$request = new FauxRequest( [], false, $session );
|
2013-02-13 21:25:37 +00:00
|
|
|
$request->setIP( $params['ip'] );
|
|
|
|
|
foreach ( $params['headers'] as $name => $value ) {
|
|
|
|
|
$request->setHeader( $name, $value );
|
|
|
|
|
}
|
|
|
|
|
// Set the current context to use the new WebRequest
|
|
|
|
|
$context->setRequest( $request );
|
|
|
|
|
$wgRequest = $context->getRequest(); // b/c
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Stash the old session and load in the new one
|
2013-03-14 22:43:42 +00:00
|
|
|
$oUser = self::getMain()->getUser();
|
|
|
|
|
$oParams = self::getMain()->exportSession();
|
2014-09-29 23:53:26 +00:00
|
|
|
$oRequest = self::getMain()->getRequest();
|
|
|
|
|
$importSessionFunc( $user, $params );
|
2013-02-13 21:25:37 +00:00
|
|
|
|
|
|
|
|
// Set callback to save and close the new session and reload the old one
|
2014-09-29 23:53:26 +00:00
|
|
|
return new ScopedCallback(
|
2021-02-10 22:31:02 +00:00
|
|
|
static function () use ( $importSessionFunc, $oUser, $oParams, $oRequest ) {
|
2014-09-29 23:53:26 +00:00
|
|
|
global $wgRequest;
|
|
|
|
|
$importSessionFunc( $oUser, $oParams );
|
|
|
|
|
// Restore the exact previous Request object (instead of leaving FauxRequest)
|
|
|
|
|
RequestContext::getMain()->setRequest( $oRequest );
|
|
|
|
|
$wgRequest = RequestContext::getMain()->getRequest(); // b/c
|
|
|
|
|
}
|
|
|
|
|
);
|
2013-02-13 21:25:37 +00:00
|
|
|
}
|
|
|
|
|
|
2011-09-05 15:25:20 +00:00
|
|
|
/**
|
|
|
|
|
* Create a new extraneous context. The context is filled with information
|
|
|
|
|
* external to the current session.
|
|
|
|
|
* - Title is specified by argument
|
|
|
|
|
* - Request is a FauxRequest, or a FauxRequest can be specified by argument
|
|
|
|
|
* - User is an anonymous user, for separation IPv4 localhost is used
|
|
|
|
|
* - Language will be based on the anonymous user and request, may be content
|
|
|
|
|
* language or a uselang param in the fauxrequest data may change the lang
|
|
|
|
|
* - Skin will be based on the anonymous user, should be the wiki's default skin
|
|
|
|
|
*
|
2013-01-11 15:56:04 +00:00
|
|
|
* @param Title $title Title to use for the extraneous request
|
2013-01-16 09:47:06 +00:00
|
|
|
* @param WebRequest|array $request A WebRequest or data to use for a FauxRequest
|
2011-09-05 15:25:20 +00:00
|
|
|
* @return RequestContext
|
|
|
|
|
*/
|
2016-02-17 09:09:32 +00:00
|
|
|
public static function newExtraneousContext( Title $title, $request = [] ) {
|
2011-09-05 15:25:20 +00:00
|
|
|
$context = new self;
|
|
|
|
|
$context->setTitle( $title );
|
|
|
|
|
if ( $request instanceof WebRequest ) {
|
|
|
|
|
$context->setRequest( $request );
|
|
|
|
|
} else {
|
|
|
|
|
$context->setRequest( new FauxRequest( $request ) );
|
|
|
|
|
}
|
|
|
|
|
$context->user = User::newFromName( '127.0.0.1', false );
|
2013-11-20 05:21:57 +00:00
|
|
|
|
2011-09-05 15:25:20 +00:00
|
|
|
return $context;
|
|
|
|
|
}
|
2011-04-03 11:09:49 +00:00
|
|
|
}
|