| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\User\Options; |
| 22 |
|
| 23 |
use MediaWiki\Config\ServiceOptions; |
| 24 |
use MediaWiki\HookContainer\HookContainer; |
| 25 |
use MediaWiki\HookContainer\HookRunner; |
| 26 |
use MediaWiki\Language\LanguageCode; |
| 27 |
use MediaWiki\Language\LanguageConverter; |
| 28 |
use MediaWiki\MainConfigNames; |
| 29 |
use MediaWiki\Title\NamespaceInfo; |
| 30 |
use MediaWiki\User\UserIdentity; |
| 31 |
use Skin; |
| 32 |
use Wikimedia\Assert\Assert; |
| 33 |
use Wikimedia\Rdbms\IDBAccessObject; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
class DefaultOptionsLookup extends UserOptionsLookup { |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public const CONSTRUCTOR_OPTIONS = [ |
| 45 |
MainConfigNames::DefaultSkin, |
| 46 |
MainConfigNames::DefaultUserOptions, |
| 47 |
MainConfigNames::NamespacesToBeSearchedDefault |
| 48 |
]; |
| 49 |
|
| 50 |
private ServiceOptions $serviceOptions; |
| 51 |
private LanguageCode $contentLang; |
| 52 |
private NamespaceInfo $nsInfo; |
| 53 |
private ConditionalDefaultsLookup $conditionalDefaultsLookup; |
| 54 |
|
| 55 |
|
| 56 |
private $defaultOptions = null; |
| 57 |
|
| 58 |
private HookRunner $hookRunner; |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public function __construct( |
| 68 |
ServiceOptions $options, |
| 69 |
LanguageCode $contentLang, |
| 70 |
HookContainer $hookContainer, |
| 71 |
NamespaceInfo $nsInfo, |
| 72 |
ConditionalDefaultsLookup $conditionalUserOptionsDefaultsLookup |
| 73 |
) { |
| 74 |
$options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS ); |
| 75 |
$this->serviceOptions = $options; |
| 76 |
$this->contentLang = $contentLang; |
| 77 |
$this->hookRunner = new HookRunner( $hookContainer ); |
| 78 |
$this->nsInfo = $nsInfo; |
| 79 |
$this->conditionalDefaultsLookup = $conditionalUserOptionsDefaultsLookup; |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
private function getGenericDefaultOptions(): array { |
| 88 |
if ( $this->defaultOptions !== null ) { |
| 89 |
return $this->defaultOptions; |
| 90 |
} |
| 91 |
|
| 92 |
$this->defaultOptions = $this->serviceOptions->get( MainConfigNames::DefaultUserOptions ); |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
$contentLangCode = $this->contentLang->toString(); |
| 98 |
$LangsWithStaticDefaultVariant = LanguageConverter::$languagesWithStaticDefaultVariant; |
| 99 |
$staticDefaultVariant = $LangsWithStaticDefaultVariant[$contentLangCode] ?? $contentLangCode; |
| 100 |
$this->defaultOptions['language'] = $contentLangCode; |
| 101 |
$this->defaultOptions['variant'] = $staticDefaultVariant; |
| 102 |
foreach ( LanguageConverter::$languagesWithVariants as $langCode ) { |
| 103 |
$staticDefaultVariant = $LangsWithStaticDefaultVariant[$langCode] ?? $langCode; |
| 104 |
$this->defaultOptions["variant-$langCode"] = $staticDefaultVariant; |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
$nsSearchDefault = $this->serviceOptions->get( MainConfigNames::NamespacesToBeSearchedDefault ); |
| 111 |
foreach ( $this->nsInfo->getValidNamespaces() as $n ) { |
| 112 |
$this->defaultOptions['searchNs' . $n] = ( $nsSearchDefault[$n] ?? false ) ? 1 : 0; |
| 113 |
} |
| 114 |
$this->defaultOptions['skin'] = Skin::normalizeKey( |
| 115 |
$this->serviceOptions->get( MainConfigNames::DefaultSkin ) ); |
| 116 |
|
| 117 |
$this->hookRunner->onUserGetDefaultOptions( $this->defaultOptions ); |
| 118 |
|
| 119 |
return $this->defaultOptions; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
public function getDefaultOptions( ?UserIdentity $userIdentity = null ): array { |
| 126 |
$defaultOptions = $this->getGenericDefaultOptions(); |
| 127 |
|
| 128 |
|
| 129 |
if ( $userIdentity ) { |
| 130 |
$conditionallyDefaultOptions = $this->conditionalDefaultsLookup->getConditionallyDefaultOptions(); |
| 131 |
foreach ( $conditionallyDefaultOptions as $optionName ) { |
| 132 |
$conditionalDefault = $this->conditionalDefaultsLookup->getOptionDefaultForUser( |
| 133 |
$optionName, $userIdentity |
| 134 |
); |
| 135 |
if ( $conditionalDefault !== null ) { |
| 136 |
$defaultOptions[$optionName] = $conditionalDefault; |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
return $defaultOptions; |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
public function getOption( |
| 148 |
UserIdentity $user, |
| 149 |
string $oname, |
| 150 |
$defaultOverride = null, |
| 151 |
bool $ignoreHidden = false, |
| 152 |
int $queryFlags = IDBAccessObject::READ_NORMAL |
| 153 |
) { |
| 154 |
$this->verifyUsable( $user, __METHOD__ ); |
| 155 |
return $this->getDefaultOption( $oname ) ?? $defaultOverride; |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
public function getOptions( |
| 162 |
UserIdentity $user, |
| 163 |
int $flags = 0, |
| 164 |
int $queryFlags = IDBAccessObject::READ_NORMAL |
| 165 |
): array { |
| 166 |
$this->verifyUsable( $user, __METHOD__ ); |
| 167 |
if ( $flags & self::EXCLUDE_DEFAULTS ) { |
| 168 |
return []; |
| 169 |
} |
| 170 |
return $this->getDefaultOptions(); |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
private function verifyUsable( UserIdentity $user, string $fname ) { |
| 188 |
if ( defined( 'MEDIAWIKI_INSTALL' ) ) { |
| 189 |
return; |
| 190 |
} |
| 191 |
Assert::precondition( !$user->isRegistered(), "$fname called on a registered user" ); |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
class_alias( DefaultOptionsLookup::class, 'MediaWiki\\User\\DefaultOptionsLookup' ); |