| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
namespace MediaWiki\Context; |
| 23 |
|
| 24 |
use MediaWiki\Config\Config; |
| 25 |
use MediaWiki\Language\Language; |
| 26 |
use MediaWiki\MediaWikiServices; |
| 27 |
use MediaWiki\Message\Message; |
| 28 |
use MediaWiki\Output\OutputPage; |
| 29 |
use MediaWiki\Permissions\Authority; |
| 30 |
use MediaWiki\Request\WebRequest; |
| 31 |
use MediaWiki\Title\Title; |
| 32 |
use MediaWiki\User\User; |
| 33 |
use Skin; |
| 34 |
use Timing; |
| 35 |
use Wikimedia\Assert\Assert; |
| 36 |
use Wikimedia\Message\MessageSpecifier; |
| 37 |
use WikiPage; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
class DerivativeContext extends ContextSource implements MutableContext { |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
private $request; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
private $title; |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
private $wikipage; |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
private $action = false; |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
private $output; |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
private $user; |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
private $authority; |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
private $lang; |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
private $skin; |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
private $config; |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
private $timing; |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
public function __construct( IContextSource $context ) { |
| 108 |
$this->setContext( $context ); |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
public function setConfig( Config $config ) { |
| 115 |
$this->config = $config; |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
public function getConfig() { |
| 122 |
return $this->config ?: $this->getContext()->getConfig(); |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
public function getTiming() { |
| 129 |
return $this->timing ?: $this->getContext()->getTiming(); |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
public function setRequest( WebRequest $request ) { |
| 136 |
$this->request = $request; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
public function getRequest() { |
| 143 |
return $this->request ?: $this->getContext()->getRequest(); |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
public function setTitle( Title $title ) { |
| 150 |
$this->title = $title; |
| 151 |
$this->action = null; |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
public function getTitle() { |
| 158 |
return $this->title ?: $this->getContext()->getTitle(); |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
public function canUseWikiPage() { |
| 170 |
if ( $this->wikipage !== null ) { |
| 171 |
return true; |
| 172 |
} |
| 173 |
|
| 174 |
if ( $this->title !== null ) { |
| 175 |
return $this->title->canExist(); |
| 176 |
} |
| 177 |
|
| 178 |
return $this->getContext()->canUseWikiPage(); |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
public function setWikiPage( WikiPage $wikiPage ) { |
| 186 |
$pageTitle = $wikiPage->getTitle(); |
| 187 |
if ( !$this->title || !$pageTitle->equals( $this->title ) ) { |
| 188 |
$this->setTitle( $pageTitle ); |
| 189 |
} |
| 190 |
$this->wikipage = $wikiPage; |
| 191 |
$this->action = null; |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
public function getWikiPage() { |
| 204 |
if ( !$this->wikipage && $this->title ) { |
| 205 |
$this->wikipage = MediaWikiServices::getInstance()->getWikiPageFactory()->newFromTitle( $this->title ); |
| 206 |
} |
| 207 |
|
| 208 |
return $this->wikipage ?: $this->getContext()->getWikiPage(); |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
public function setActionName( string $action ): void { |
| 216 |
$this->action = $action; |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
public function getActionName(): string { |
| 226 |
if ( $this->action === false ) { |
| 227 |
return $this->getContext()->getActionName(); |
| 228 |
} |
| 229 |
|
| 230 |
$this->action ??= MediaWikiServices::getInstance() |
| 231 |
->getActionFactory() |
| 232 |
->getActionName( $this ); |
| 233 |
|
| 234 |
return $this->action; |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
public function setOutput( OutputPage $output ) { |
| 241 |
$this->output = $output; |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
public function getOutput() { |
| 248 |
return $this->output ?: $this->getContext()->getOutput(); |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
public function setUser( User $user ) { |
| 255 |
$this->authority = $user; |
| 256 |
$this->user = $user; |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
public function getUser() { |
| 263 |
if ( !$this->user && $this->authority ) { |
| 264 |
|
| 265 |
$this->user = MediaWikiServices::getInstance() |
| 266 |
->getUserFactory() |
| 267 |
->newFromAuthority( $this->authority ); |
| 268 |
} |
| 269 |
return $this->user ?: $this->getContext()->getUser(); |
| 270 |
} |
| 271 |
|
| 272 |
public function setAuthority( Authority $authority ) { |
| 273 |
$this->authority = $authority; |
| 274 |
|
| 275 |
$this->user = null; |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
public function getAuthority(): Authority { |
| 283 |
return $this->authority ?: $this->getContext()->getAuthority(); |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
public function setLanguage( $language ) { |
| 291 |
Assert::parameterType( [ Language::class, 'string' ], $language, '$language' ); |
| 292 |
if ( $language instanceof Language ) { |
| 293 |
$this->lang = $language; |
| 294 |
} else { |
| 295 |
$language = RequestContext::sanitizeLangCode( $language ); |
| 296 |
$obj = MediaWikiServices::getInstance()->getLanguageFactory()->getLanguage( $language ); |
| 297 |
$this->lang = $obj; |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
public function getLanguage() { |
| 306 |
return $this->lang ?: $this->getContext()->getLanguage(); |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
public function setSkin( Skin $skin ) { |
| 313 |
$this->skin = clone $skin; |
| 314 |
$this->skin->setContext( $this ); |
| 315 |
} |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
public function getSkin() { |
| 321 |
return $this->skin ?: $this->getContext()->getSkin(); |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
public function msg( $key, ...$params ) { |
| 337 |
|
| 338 |
return wfMessage( $key, ...$params )->setContext( $this ); |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
class_alias( DerivativeContext::class, 'DerivativeContext' ); |