56 $this->nsInfo =
$nsInfo ?? MediaWikiServices::getInstance()->getNamespaceInfo();
58 $this->userOptionsLookup =
$userOptionsLookup ?? MediaWikiServices::getInstance()->getUserOptionsLookup();
68 return MediaWikiServices::getInstance()->getGenderCache();
76 if ( $this->
default ===
null ) {
77 $this->
default = $this->userOptionsLookup->getDefaultOption(
'gender' );
80 return $this->default;
93 $username = $username->getName();
96 $username = self::normalizeUsername( $username );
97 if ( !isset( $this->cache[$username] ) ) {
98 if ( $this->misses >= $this->missLimit && $wgUser->getName() !== $username ) {
99 if ( $this->misses === $this->missLimit ) {
101 wfDebug( __METHOD__ .
": too many misses, returning default onwards" );
107 $this->
doQuery( $username, $caller );
114 return $this->cache[$username] ?? $this->
getDefault();
125 foreach ( $data as $ns => $pagenames ) {
126 if ( !$this->nsInfo->hasGenderDistinction( $ns ) ) {
129 foreach ( array_keys( $pagenames ) as $username ) {
130 $users[$username] =
true;
134 $this->
doQuery( array_keys( $users ), $caller );
146 foreach ( $titles as $titleObj ) {
147 if ( !$this->nsInfo->hasGenderDistinction( $titleObj->getNamespace() ) ) {
150 $users[] = $titleObj->getText();
153 $this->
doQuery( $users, $caller );
161 public function doQuery( $users, $caller =
'' ) {
165 foreach ( (array)$users as $value ) {
166 $name = self::normalizeUsername( $value );
168 if ( !isset( $this->cache[$name] ) ) {
173 $usersToCheck[] = $name;
178 if ( count( $usersToCheck ) === 0 ) {
184 if ( $this->loadBalancer ===
null ) {
189 $table = [
'user',
'user_properties' ];
190 $fields = [
'user_name',
'up_value' ];
191 $conds = [
'user_name' => $usersToCheck ];
192 $joins = [
'user_properties' =>
193 [
'LEFT JOIN', [
'user_id = up_user',
'up_property' =>
'gender' ] ] ];
195 $comment = __METHOD__;
196 if ( strval( $caller ) !==
'' ) {
197 $comment .=
"/$caller";
199 $res =
$dbr->select( $table, $fields, $conds, $comment, [], $joins );
201 foreach (
$res as $row ) {
202 $this->cache[$row->user_name] = $row->up_value ?:
$default;
208 $indexSlash = strpos( $username,
'/' );
209 if ( $indexSlash !==
false ) {
210 $username = substr( $username, 0, $indexSlash );
214 return strtr( $username,
'_',
' ' );
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that $function is deprecated.
Caches user genders when needed to use correct namespace aliases.
__construct(NamespaceInfo $nsInfo=null, ILoadBalancer $loadBalancer=null, UserOptionsLookup $userOptionsLookup=null)
getGenderOf( $username, $caller='')
Returns the gender for given username.
static normalizeUsername( $username)
UserOptionsLookup $userOptionsLookup
doLinkBatch( $data, $caller='')
Wrapper for doQuery that processes raw LinkBatch data.
getDefault()
Returns the default gender option in this wiki.
ILoadBalancer null $loadBalancer
doTitlesArray( $titles, $caller='')
Wrapper for doQuery that processes a title array.
doQuery( $users, $caller='')
Preloads genders for given list of users.
This is a utility class for dealing with namespaces that encodes all the "magic" behaviors of them ba...
static isValidUserName( $name)
Is the input a valid username?