44 private $loadBalancer;
47 private $userOptionsLookup;
54 $this->nsInfo = $nsInfo ?? MediaWikiServices::getInstance()->getNamespaceInfo();
55 $this->loadBalancer = $loadBalancer;
56 $this->userOptionsLookup = $userOptionsLookup ?? MediaWikiServices::getInstance()->getUserOptionsLookup();
64 $this->
default ??= $this->userOptionsLookup->getDefaultOption(
'gender' );
66 return $this->default;
77 $username = $username->getName();
80 $username = self::normalizeUsername( $username );
81 if ( !isset( $this->cache[$username] ) ) {
82 if ( $this->misses >= $this->missLimit &&
83 RequestContext::getMain()->
getUser()->getName() !== $username
85 if ( $this->misses === $this->missLimit ) {
87 wfDebug( __METHOD__ .
": too many misses, returning default onwards" );
93 $this->
doQuery( $username, $caller );
100 return $this->cache[$username] ?? $this->
getDefault();
111 foreach ( $data as $ns => $pagenames ) {
112 if ( !$this->nsInfo->hasGenderDistinction( $ns ) ) {
115 foreach ( array_keys( $pagenames ) as $username ) {
116 $users[$username] =
true;
120 $this->
doQuery( array_keys( $users ), $caller );
132 foreach ( $titles as $titleObj ) {
133 if ( !$this->nsInfo->hasGenderDistinction( $titleObj->getNamespace() ) ) {
136 $users[] = $titleObj->getText();
139 $this->
doQuery( $users, $caller );
147 public function doQuery( $users, $caller =
'' ) {
151 foreach ( (array)$users as $value ) {
152 $name = self::normalizeUsername( $value );
154 if ( !isset( $this->cache[$name] ) ) {
158 $usersToCheck[] = $name;
162 if ( count( $usersToCheck ) === 0 ) {
168 if ( $this->loadBalancer ===
null ) {
173 $table = [
'user',
'user_properties' ];
174 $fields = [
'user_name',
'up_value' ];
175 $conds = [
'user_name' => $usersToCheck ];
176 $joins = [
'user_properties' =>
177 [
'LEFT JOIN', [
'user_id = up_user',
'up_property' =>
'gender' ] ] ];
179 $comment = __METHOD__;
180 if ( strval( $caller ) !==
'' ) {
181 $comment .=
"/$caller";
183 $res =
$dbr->select( $table, $fields, $conds, $comment, [], $joins );
185 foreach (
$res as $row ) {
186 $this->cache[$row->user_name] = $row->up_value ?:
$default;
190 private static function normalizeUsername( $username ) {
192 $indexSlash = strpos( $username,
'/' );
193 if ( $indexSlash !==
false ) {
194 $username = substr( $username, 0, $indexSlash );
198 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.
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.
doLinkBatch( $data, $caller='')
Wrapper for doQuery that processes raw LinkBatch data.
getDefault()
Returns the default gender option in this wiki.
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...