MediaWiki  master
DerivativeContext.php
Go to the documentation of this file.
1 <?php
23 
36  private $request;
37 
41  private $title;
42 
46  private $wikipage;
47 
51  private $output;
52 
56  private $user;
57 
61  private $authority;
62 
66  private $lang;
67 
71  private $skin;
72 
76  private $config;
77 
81  private $timing;
82 
87  public function __construct( IContextSource $context ) {
88  $this->setContext( $context );
89  }
90 
94  public function setConfig( Config $config ) {
95  $this->config = $config;
96  }
97 
101  public function getConfig() {
102  return $this->config ?: $this->getContext()->getConfig();
103  }
104 
110  public function getStats() {
111  return MediaWikiServices::getInstance()->getStatsdDataFactory();
112  }
113 
117  public function getTiming() {
118  return $this->timing ?: $this->getContext()->getTiming();
119  }
120 
124  public function setRequest( WebRequest $request ) {
125  $this->request = $request;
126  }
127 
131  public function getRequest() {
132  return $this->request ?: $this->getContext()->getRequest();
133  }
134 
138  public function setTitle( Title $title ) {
139  $this->title = $title;
140  }
141 
145  public function getTitle() {
146  return $this->title ?: $this->getContext()->getTitle();
147  }
148 
157  public function canUseWikiPage() {
158  if ( $this->wikipage !== null ) {
159  return true;
160  }
161 
162  if ( $this->title !== null ) {
163  return $this->title->canExist();
164  }
165 
166  return $this->getContext()->canUseWikiPage();
167  }
168 
173  public function setWikiPage( WikiPage $wikiPage ) {
174  $this->wikipage = $wikiPage;
175  }
176 
186  public function getWikiPage() {
187  return $this->wikipage ?: $this->getContext()->getWikiPage();
188  }
189 
193  public function setOutput( OutputPage $output ) {
194  $this->output = $output;
195  }
196 
200  public function getOutput() {
201  return $this->output ?: $this->getContext()->getOutput();
202  }
203 
207  public function setUser( User $user ) {
208  $this->authority = $user;
209  $this->user = $user;
210  }
211 
215  public function getUser() {
216  return $this->user ?: $this->getContext()->getUser();
217  }
218 
219  public function setAuthority( Authority $authority ) {
220  $this->authority = $authority;
221  $this->user = MediaWikiServices::getInstance()
222  ->getUserFactory()
223  ->newFromAuthority( $authority );
224  }
225 
230  public function getAuthority(): Authority {
231  return $this->authority ?: $this->getContext()->getAuthority();
232  }
233 
239  public function setLanguage( $language ) {
240  if ( $language instanceof Language ) {
241  $this->lang = $language;
242  } elseif ( is_string( $language ) ) {
243  $language = RequestContext::sanitizeLangCode( $language );
244  $obj = MediaWikiServices::getInstance()->getLanguageFactory()->getLanguage( $language );
245  $this->lang = $obj;
246  } else {
247  throw new MWException( __METHOD__ . " was passed an invalid type of data." );
248  }
249  }
250 
255  public function getLanguage() {
256  return $this->lang ?: $this->getContext()->getLanguage();
257  }
258 
262  public function setSkin( Skin $skin ) {
263  $this->skin = clone $skin;
264  $this->skin->setContext( $this );
265  }
266 
270  public function getSkin() {
271  return $this->skin ?: $this->getContext()->getSkin();
272  }
273 
286  public function msg( $key, ...$params ) {
287  // phpcs:ignore MediaWiki.Usage.ExtendClassUsage.FunctionVarUsage
288  return wfMessage( $key, ...$params )->setContext( $this );
289  }
290 }
DerivativeContext\__construct
__construct(IContextSource $context)
Definition: DerivativeContext.php:87
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:76
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:199
DerivativeContext\$wikipage
WikiPage $wikipage
Definition: DerivativeContext.php:46
DerivativeContext\setWikiPage
setWikiPage(WikiPage $wikiPage)
Definition: DerivativeContext.php:173
WikiPage
Class representing a MediaWiki article and history.
Definition: WikiPage.php:60
DerivativeContext\setConfig
setConfig(Config $config)
Definition: DerivativeContext.php:94
wfMessage
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Definition: GlobalFunctions.php:1183
DerivativeContext\setSkin
setSkin(Skin $skin)
Definition: DerivativeContext.php:262
DerivativeContext\setAuthority
setAuthority(Authority $authority)
Definition: DerivativeContext.php:219
DerivativeContext\getWikiPage
getWikiPage()
Get the WikiPage object.
Definition: DerivativeContext.php:186
DerivativeContext\getStats
getStats()
Definition: DerivativeContext.php:110
DerivativeContext\getUser
getUser()
Definition: DerivativeContext.php:215
DerivativeContext\getOutput
getOutput()
Definition: DerivativeContext.php:200
DerivativeContext\getTitle
getTitle()
Definition: DerivativeContext.php:145
DerivativeContext\$output
OutputPage $output
Definition: DerivativeContext.php:51
DerivativeContext\setRequest
setRequest(WebRequest $request)
Definition: DerivativeContext.php:124
Config
Interface for configuration instances.
Definition: Config.php:30
DerivativeContext\$authority
Authority $authority
Definition: DerivativeContext.php:61
DerivativeContext
An IContextSource implementation which will inherit context from another source but allow individual ...
Definition: DerivativeContext.php:32
MWException
MediaWiki exception.
Definition: MWException.php:29
DerivativeContext\$title
Title $title
Definition: DerivativeContext.php:41
DerivativeContext\$skin
Skin $skin
Definition: DerivativeContext.php:71
DerivativeContext\getTiming
getTiming()
Definition: DerivativeContext.php:117
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:207
DerivativeContext\msg
msg( $key,... $params)
Get a message using the current context.
Definition: DerivativeContext.php:286
DerivativeContext\getRequest
getRequest()
Definition: DerivativeContext.php:131
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:138
DerivativeContext\canUseWikiPage
canUseWikiPage()
Check whether a WikiPage object can be get with getWikiPage().
Definition: DerivativeContext.php:157
DerivativeContext\getConfig
getConfig()
Definition: DerivativeContext.php:101
DerivativeContext\getSkin
getSkin()
Definition: DerivativeContext.php:270
DerivativeContext\$timing
Timing $timing
Definition: DerivativeContext.php:81
DerivativeContext\$user
User $user
Definition: DerivativeContext.php:56
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:43
Title
Represents a title within MediaWiki.
Definition: Title.php:47
DerivativeContext\getAuthority
getAuthority()
Definition: DerivativeContext.php:230
DerivativeContext\setLanguage
setLanguage( $language)
Definition: DerivativeContext.php:239
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:193
Language
Internationalisation code See https://www.mediawiki.org/wiki/Special:MyLanguage/Localisation for more...
Definition: Language.php:42
DerivativeContext\getLanguage
getLanguage()
Definition: DerivativeContext.php:255
DerivativeContext\$request
WebRequest $request
Definition: DerivativeContext.php:36
DerivativeContext\$lang
Language $lang
Definition: DerivativeContext.php:66