| 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\ResourceLoader; |
| 23 |
|
| 24 |
use MediaWiki\MediaWikiServices; |
| 25 |
use MediaWiki\Request\WebRequest; |
| 26 |
use MediaWiki\User\User; |
| 27 |
use MediaWiki\User\UserIdentity; |
| 28 |
use MediaWiki\User\UserRigorOptions; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
class DerivativeContext extends Context { |
| 40 |
private const INHERIT_VALUE = -1; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
private $context; |
| 46 |
|
| 47 |
|
| 48 |
protected $modules = self::INHERIT_VALUE; |
| 49 |
|
| 50 |
protected $language = self::INHERIT_VALUE; |
| 51 |
|
| 52 |
protected $direction = self::INHERIT_VALUE; |
| 53 |
|
| 54 |
protected $skin = self::INHERIT_VALUE; |
| 55 |
|
| 56 |
protected $user = self::INHERIT_VALUE; |
| 57 |
|
| 58 |
protected $userIdentity = self::INHERIT_VALUE; |
| 59 |
|
| 60 |
protected $userObj = self::INHERIT_VALUE; |
| 61 |
|
| 62 |
protected $debug = self::INHERIT_VALUE; |
| 63 |
|
| 64 |
protected $only = self::INHERIT_VALUE; |
| 65 |
|
| 66 |
protected $version = self::INHERIT_VALUE; |
| 67 |
|
| 68 |
protected $raw = self::INHERIT_VALUE; |
| 69 |
|
| 70 |
protected $sourcemap = self::INHERIT_VALUE; |
| 71 |
|
| 72 |
protected $contentOverrideCallback = self::INHERIT_VALUE; |
| 73 |
|
| 74 |
public function __construct( Context $context ) { |
| 75 |
$this->context = $context; |
| 76 |
} |
| 77 |
|
| 78 |
public function getModules(): array { |
| 79 |
if ( $this->modules === self::INHERIT_VALUE ) { |
| 80 |
return $this->context->getModules(); |
| 81 |
} |
| 82 |
|
| 83 |
return $this->modules; |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
public function setModules( array $modules ) { |
| 90 |
$this->modules = $modules; |
| 91 |
} |
| 92 |
|
| 93 |
public function getLanguage(): string { |
| 94 |
if ( $this->language === self::INHERIT_VALUE ) { |
| 95 |
return $this->context->getLanguage(); |
| 96 |
} |
| 97 |
return $this->language; |
| 98 |
} |
| 99 |
|
| 100 |
public function setLanguage( string $language ) { |
| 101 |
$this->language = $language; |
| 102 |
|
| 103 |
$this->direction = null; |
| 104 |
$this->hash = null; |
| 105 |
} |
| 106 |
|
| 107 |
public function getDirection(): string { |
| 108 |
if ( $this->direction === self::INHERIT_VALUE ) { |
| 109 |
return $this->context->getDirection(); |
| 110 |
} |
| 111 |
if ( $this->direction === null ) { |
| 112 |
$this->direction = MediaWikiServices::getInstance()->getLanguageFactory() |
| 113 |
->getLanguage( $this->getLanguage() )->getDir(); |
| 114 |
} |
| 115 |
return $this->direction; |
| 116 |
} |
| 117 |
|
| 118 |
public function setDirection( string $direction ) { |
| 119 |
$this->direction = $direction; |
| 120 |
$this->hash = null; |
| 121 |
} |
| 122 |
|
| 123 |
public function getSkin(): string { |
| 124 |
if ( $this->skin === self::INHERIT_VALUE ) { |
| 125 |
return $this->context->getSkin(); |
| 126 |
} |
| 127 |
return $this->skin; |
| 128 |
} |
| 129 |
|
| 130 |
public function setSkin( string $skin ) { |
| 131 |
$this->skin = $skin; |
| 132 |
$this->hash = null; |
| 133 |
} |
| 134 |
|
| 135 |
public function getUser(): ?string { |
| 136 |
if ( $this->user === self::INHERIT_VALUE ) { |
| 137 |
return $this->context->getUser(); |
| 138 |
} |
| 139 |
return $this->user; |
| 140 |
} |
| 141 |
|
| 142 |
public function getUserIdentity(): ?UserIdentity { |
| 143 |
if ( $this->userIdentity === self::INHERIT_VALUE ) { |
| 144 |
return $this->context->getUserIdentity(); |
| 145 |
} |
| 146 |
if ( $this->userIdentity === false ) { |
| 147 |
$username = $this->getUser(); |
| 148 |
if ( $username === null ) { |
| 149 |
|
| 150 |
$this->userIdentity = null; |
| 151 |
} else { |
| 152 |
|
| 153 |
$this->userIdentity = MediaWikiServices::getInstance() |
| 154 |
->getUserFactory() |
| 155 |
->newFromName( $username, UserRigorOptions::RIGOR_VALID ); |
| 156 |
} |
| 157 |
} |
| 158 |
return $this->userIdentity; |
| 159 |
} |
| 160 |
|
| 161 |
public function getUserObj(): User { |
| 162 |
if ( $this->userObj === self::INHERIT_VALUE ) { |
| 163 |
return $this->context->getUserObj(); |
| 164 |
} |
| 165 |
if ( $this->userObj === null ) { |
| 166 |
$username = $this->getUser(); |
| 167 |
$userFactory = MediaWikiServices::getInstance()->getUserFactory(); |
| 168 |
if ( $username ) { |
| 169 |
$this->userObj = $userFactory->newFromName( $username, UserRigorOptions::RIGOR_VALID ); |
| 170 |
} |
| 171 |
$this->userObj ??= $userFactory->newAnonymous(); |
| 172 |
} |
| 173 |
return $this->userObj; |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
public function setUser( ?string $user ) { |
| 180 |
$this->user = $user; |
| 181 |
$this->hash = null; |
| 182 |
|
| 183 |
$this->userObj = null; |
| 184 |
$this->userIdentity = false; |
| 185 |
} |
| 186 |
|
| 187 |
public function getDebug(): int { |
| 188 |
if ( $this->debug === self::INHERIT_VALUE ) { |
| 189 |
return $this->context->getDebug(); |
| 190 |
} |
| 191 |
return $this->debug; |
| 192 |
} |
| 193 |
|
| 194 |
public function setDebug( int $debug ) { |
| 195 |
$this->debug = $debug; |
| 196 |
$this->hash = null; |
| 197 |
} |
| 198 |
|
| 199 |
public function getOnly(): ?string { |
| 200 |
if ( $this->only === self::INHERIT_VALUE ) { |
| 201 |
return $this->context->getOnly(); |
| 202 |
} |
| 203 |
return $this->only; |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
public function setOnly( ?string $only ) { |
| 210 |
$this->only = $only; |
| 211 |
$this->hash = null; |
| 212 |
} |
| 213 |
|
| 214 |
public function getVersion(): ?string { |
| 215 |
if ( $this->version === self::INHERIT_VALUE ) { |
| 216 |
return $this->context->getVersion(); |
| 217 |
} |
| 218 |
return $this->version; |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
public function setVersion( ?string $version ) { |
| 225 |
$this->version = $version; |
| 226 |
$this->hash = null; |
| 227 |
} |
| 228 |
|
| 229 |
public function getRaw(): bool { |
| 230 |
if ( $this->raw === self::INHERIT_VALUE ) { |
| 231 |
return $this->context->getRaw(); |
| 232 |
} |
| 233 |
return $this->raw; |
| 234 |
} |
| 235 |
|
| 236 |
public function setRaw( bool $raw ) { |
| 237 |
$this->raw = $raw; |
| 238 |
} |
| 239 |
|
| 240 |
public function isSourceMap(): bool { |
| 241 |
if ( $this->sourcemap === self::INHERIT_VALUE ) { |
| 242 |
return $this->context->isSourceMap(); |
| 243 |
} |
| 244 |
return $this->sourcemap; |
| 245 |
} |
| 246 |
|
| 247 |
public function setIsSourceMap( bool $sourcemap ) { |
| 248 |
$this->sourcemap = $sourcemap; |
| 249 |
} |
| 250 |
|
| 251 |
public function getRequest(): WebRequest { |
| 252 |
return $this->context->getRequest(); |
| 253 |
} |
| 254 |
|
| 255 |
public function getResourceLoader(): ResourceLoader { |
| 256 |
return $this->context->getResourceLoader(); |
| 257 |
} |
| 258 |
|
| 259 |
public function getContentOverrideCallback() { |
| 260 |
if ( $this->contentOverrideCallback === self::INHERIT_VALUE ) { |
| 261 |
return $this->context->getContentOverrideCallback(); |
| 262 |
} |
| 263 |
return $this->contentOverrideCallback; |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
public function setContentOverrideCallback( $callback ) { |
| 273 |
$this->contentOverrideCallback = $callback; |
| 274 |
} |
| 275 |
|
| 276 |
} |