56 $this->nsInfo =
$nsInfo ?? MediaWikiServices::getInstance()->getNamespaceInfo();
58 $this->userOptionsLookup =
$userOptionsLookup ?? MediaWikiServices::getInstance()->getUserOptionsLookup();
66 if ( $this->
default ===
null ) {
67 $this->
default = $this->userOptionsLookup->getDefaultOption(
'gender' );
70 return $this->default;
81 $username = $username->getName();
84 $username = self::normalizeUsername( $username );
85 if ( !isset( $this->cache[$username] ) ) {
86 if ( $this->misses >= $this->missLimit &&
87 RequestContext::getMain()->getUser()->getName() !== $username
89 if ( $this->misses === $this->missLimit ) {
91 wfDebug( __METHOD__ .
": too many misses, returning default onwards" );
97 $this->
doQuery( $username, $caller );
104 return $this->cache[$username] ?? $this->
getDefault();
115 foreach ( $data as $ns => $pagenames ) {
116 if ( !$this->nsInfo->hasGenderDistinction( $ns ) ) {
119 foreach ( array_keys( $pagenames ) as $username ) {
120 $users[$username] =
true;
124 $this->
doQuery( array_keys( $users ), $caller );
136 foreach ( $titles as $titleObj ) {
137 if ( !$this->nsInfo->hasGenderDistinction( $titleObj->getNamespace() ) ) {
140 $users[] = $titleObj->getText();
143 $this->
doQuery( $users, $caller );
151 public function doQuery( $users, $caller =
'' ) {
155 foreach ( (array)$users as $value ) {
156 $name = self::normalizeUsername( $value );
158 if ( !isset( $this->cache[$name] ) ) {
162 $usersToCheck[] = $name;
166 if ( count( $usersToCheck ) === 0 ) {
172 if ( $this->loadBalancer ===
null ) {
177 $table = [
'user',
'user_properties' ];
178 $fields = [
'user_name',
'up_value' ];
179 $conds = [
'user_name' => $usersToCheck ];
180 $joins = [
'user_properties' =>
181 [
'LEFT JOIN', [
'user_id = up_user',
'up_property' =>
'gender' ] ] ];
183 $comment = __METHOD__;
184 if ( strval( $caller ) !==
'' ) {
185 $comment .=
"/$caller";
187 $res =
$dbr->select( $table, $fields, $conds, $comment, [], $joins );
189 foreach (
$res as $row ) {
190 $this->cache[$row->user_name] = $row->up_value ?:
$default;
196 $indexSlash = strpos( $username,
'/' );
197 if ( $indexSlash !==
false ) {
198 $username = substr( $username, 0, $indexSlash );
202 return strtr( $username,
'_',
' ' );
UserOptionsLookup $userOptionsLookup
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
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...