MediaWiki  master
DerivativeContext.php
Go to the documentation of this file.
1 <?php
24 
37  private $request;
38 
42  private $title;
43 
47  private $wikipage;
48 
52  private $output;
53 
57  private $user;
58 
62  private $authority;
63 
67  private $lang;
68 
72  private $skin;
73 
77  private $config;
78 
82  private $timing;
83 
88  public function __construct( IContextSource $context ) {
89  $this->setContext( $context );
90  }
91 
95  public function setConfig( Config $config ) {
96  $this->config = $config;
97  }
98 
102  public function getConfig() {
103  return $this->config ?: $this->getContext()->getConfig();
104  }
105 
111  public function getStats() {
112  return MediaWikiServices::getInstance()->getStatsdDataFactory();
113  }
114 
118  public function getTiming() {
119  return $this->timing ?: $this->getContext()->getTiming();
120  }
121 
125  public function setRequest( WebRequest $request ) {
126  $this->request = $request;
127  }
128 
132  public function getRequest() {
133  return $this->request ?: $this->getContext()->getRequest();
134  }
135 
139  public function setTitle( Title $title ) {
140  $this->title = $title;
141  }
142 
146  public function getTitle() {
147  return $this->title ?: $this->getContext()->getTitle();
148  }
149 
158  public function canUseWikiPage() {
159  if ( $this->wikipage !== null ) {
160  return true;
161  }
162 
163  if ( $this->title !== null ) {
164  return $this->title->canExist();
165  }
166 
167  return $this->getContext()->canUseWikiPage();
168  }
169 
174  public function setWikiPage( WikiPage $wikiPage ) {
175  $this->wikipage = $wikiPage;
176  }
177 
187  public function getWikiPage() {
188  return $this->wikipage ?: $this->getContext()->getWikiPage();
189  }
190 
194  public function setOutput( OutputPage $output ) {
195  $this->output = $output;
196  }
197 
201  public function getOutput() {
202  return $this->output ?: $this->getContext()->getOutput();
203  }
204 
208  public function setUser( User $user ) {
209  $this->authority = $user;
210  $this->user = $user;
211  }
212 
216  public function getUser() {
217  return $this->user ?: $this->getContext()->getUser();
218  }
219 
220  public function setAuthority( Authority $authority ) {
221  $this->authority = $authority;
222  $this->user = MediaWikiServices::getInstance()
223  ->getUserFactory()
224  ->newFromAuthority( $authority );
225  }
226 
231  public function getAuthority(): Authority {
232  return $this->authority ?: $this->getContext()->getAuthority();
233  }
234 
240  public function setLanguage( $language ) {
241  if ( $language instanceof Language ) {
242  $this->lang = $language;
243  } elseif ( is_string( $language ) ) {
244  $language = RequestContext::sanitizeLangCode( $language );
245  $obj = MediaWikiServices::getInstance()->getLanguageFactory()->getLanguage( $language );
246  $this->lang = $obj;
247  } else {
248  throw new MWException( __METHOD__ . " was passed an invalid type of data." );
249  }
250  }
251 
256  public function getLanguage() {
257  return $this->lang ?: $this->getContext()->getLanguage();
258  }
259 
263  public function setSkin( Skin $skin ) {
264  $this->skin = clone $skin;
265  $this->skin->setContext( $this );
266  }
267 
271  public function getSkin() {
272  return $this->skin ?: $this->getContext()->getSkin();
273  }
274 
287  public function msg( $key, ...$params ) {
288  // phpcs:ignore MediaWiki.Usage.ExtendClassUsage.FunctionVarUsage
289  return wfMessage( $key, ...$params )->setContext( $this );
290  }
291 
298  public function getCsrfTokenSet(): CsrfTokenSet {
299  return new CsrfTokenSet( $this->getRequest() );
300  }
301 }
DerivativeContext\__construct
__construct(IContextSource $context)
Definition: DerivativeContext.php:88
ContextSource\$context
IContextSource $context
Definition: ContextSource.php:39
ContextSource\getContext
getContext()
Get the base IContextSource object.
Definition: ContextSource.php:47
RequestContext\sanitizeLangCode
static sanitizeLangCode( $code)
Accepts a language code and ensures it's sane.
Definition: RequestContext.php:327
DerivativeContext\$config
Config $config
Definition: DerivativeContext.php:77
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:186
DerivativeContext\$wikipage
WikiPage $wikipage
Definition: DerivativeContext.php:47
DerivativeContext\setWikiPage
setWikiPage(WikiPage $wikiPage)
Definition: DerivativeContext.php:174
WikiPage
Class representing a MediaWiki article and history.
Definition: WikiPage.php:60
DerivativeContext\getCsrfTokenSet
getCsrfTokenSet()
Get a repository to obtain and match CSRF tokens.
Definition: DerivativeContext.php:298
DerivativeContext\setConfig
setConfig(Config $config)
Definition: DerivativeContext.php:95
wfMessage
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Definition: GlobalFunctions.php:1182
DerivativeContext\setSkin
setSkin(Skin $skin)
Definition: DerivativeContext.php:263
DerivativeContext\setAuthority
setAuthority(Authority $authority)
Definition: DerivativeContext.php:220
DerivativeContext\getWikiPage
getWikiPage()
Get the WikiPage object.
Definition: DerivativeContext.php:187
DerivativeContext\getStats
getStats()
Definition: DerivativeContext.php:111
DerivativeContext\getUser
getUser()
Definition: DerivativeContext.php:216
DerivativeContext\getOutput
getOutput()
Definition: DerivativeContext.php:201
DerivativeContext\getTitle
getTitle()
Definition: DerivativeContext.php:146
DerivativeContext\$output
OutputPage $output
Definition: DerivativeContext.php:52
DerivativeContext\setRequest
setRequest(WebRequest $request)
Definition: DerivativeContext.php:125
Config
Interface for configuration instances.
Definition: Config.php:30
DerivativeContext\$authority
Authority $authority
Definition: DerivativeContext.php:62
DerivativeContext
An IContextSource implementation which will inherit context from another source but allow individual ...
Definition: DerivativeContext.php:33
MWException
MediaWiki exception.
Definition: MWException.php:29
DerivativeContext\$title
Title $title
Definition: DerivativeContext.php:42
DerivativeContext\$skin
Skin $skin
Definition: DerivativeContext.php:72
DerivativeContext\getTiming
getTiming()
Definition: DerivativeContext.php:118
Timing
An interface to help developers measure the performance of their applications.
Definition: Timing.php:45
ContextSource
The simplest way of implementing IContextSource is to hold a RequestContext as a member variable and ...
Definition: ContextSource.php:33
DerivativeContext\setUser
setUser(User $user)
Definition: DerivativeContext.php:208
DerivativeContext\msg
msg( $key,... $params)
Get a message using the current context.
Definition: DerivativeContext.php:287
DerivativeContext\getRequest
getRequest()
Definition: DerivativeContext.php:132
ContextSource\setContext
setContext(IContextSource $context)
Definition: ContextSource.php:63
OutputPage
This is one of the Core classes and should be read at least once by any new developers.
Definition: OutputPage.php:50
MediaWiki\Permissions\Authority
This interface represents the authority associated the current execution context, such as a web reque...
Definition: Authority.php:37
MutableContext
Request-dependent objects containers.
Definition: MutableContext.php:28
DerivativeContext\setTitle
setTitle(Title $title)
Definition: DerivativeContext.php:139
DerivativeContext\canUseWikiPage
canUseWikiPage()
Check whether a WikiPage object can be get with getWikiPage().
Definition: DerivativeContext.php:158
MediaWiki\Session\CsrfTokenSet
Definition: CsrfTokenSet.php:31
DerivativeContext\getConfig
getConfig()
Definition: DerivativeContext.php:102
DerivativeContext\getSkin
getSkin()
Definition: DerivativeContext.php:271
DerivativeContext\$timing
Timing $timing
Definition: DerivativeContext.php:82
DerivativeContext\$user
User $user
Definition: DerivativeContext.php:57
IContextSource
Interface for objects which can provide a MediaWiki context on request.
Definition: IContextSource.php:58
WebRequest
The WebRequest class encapsulates getting at data passed in the URL or via a POSTed form stripping il...
Definition: WebRequest.php:42
Title
Represents a title within MediaWiki.
Definition: Title.php:49
DerivativeContext\getAuthority
getAuthority()
Definition: DerivativeContext.php:231
DerivativeContext\setLanguage
setLanguage( $language)
Definition: DerivativeContext.php:240
Skin
The main skin class which provides methods and properties for all other skins.
Definition: Skin.php:44
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:68
DerivativeContext\setOutput
setOutput(OutputPage $output)
Definition: DerivativeContext.php:194
Language
Internationalisation code See https://www.mediawiki.org/wiki/Special:MyLanguage/Localisation for more...
Definition: Language.php:42
DerivativeContext\getLanguage
getLanguage()
Definition: DerivativeContext.php:256
DerivativeContext\$request
WebRequest $request
Definition: DerivativeContext.php:37
DerivativeContext\$lang
Language $lang
Definition: DerivativeContext.php:67