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 if ( $this->
default ===
null ) {
65 $this->
default = $this->userOptionsLookup->getDefaultOption(
'gender' );
68 return $this->default;
79 $username = $username->getName();
82 $username = self::normalizeUsername( $username );
83 if ( !isset( $this->cache[$username] ) ) {
84 if ( $this->misses >= $this->missLimit &&
85 RequestContext::getMain()->
getUser()->getName() !== $username
87 if ( $this->misses === $this->missLimit ) {
89 wfDebug( __METHOD__ .
": too many misses, returning default onwards" );
95 $this->
doQuery( $username, $caller );
102 return $this->cache[$username] ?? $this->
getDefault();
113 foreach ( $data as $ns => $pagenames ) {
114 if ( !$this->nsInfo->hasGenderDistinction( $ns ) ) {
117 foreach ( array_keys( $pagenames ) as $username ) {
118 $users[$username] =
true;
122 $this->
doQuery( array_keys( $users ), $caller );
134 foreach ( $titles as $titleObj ) {
135 if ( !$this->nsInfo->hasGenderDistinction( $titleObj->getNamespace() ) ) {
138 $users[] = $titleObj->getText();
141 $this->
doQuery( $users, $caller );
149 public function doQuery( $users, $caller =
'' ) {
153 foreach ( (array)$users as $value ) {
154 $name = self::normalizeUsername( $value );
156 if ( !isset( $this->cache[$name] ) ) {
160 $usersToCheck[] = $name;
164 if ( count( $usersToCheck ) === 0 ) {
170 if ( $this->loadBalancer ===
null ) {
175 $table = [
'user',
'user_properties' ];
176 $fields = [
'user_name',
'up_value' ];
177 $conds = [
'user_name' => $usersToCheck ];
178 $joins = [
'user_properties' =>
179 [
'LEFT JOIN', [
'user_id = up_user',
'up_property' =>
'gender' ] ] ];
181 $comment = __METHOD__;
182 if ( strval( $caller ) !==
'' ) {
183 $comment .=
"/$caller";
185 $res =
$dbr->select( $table, $fields, $conds, $comment, [], $joins );
187 foreach (
$res as $row ) {
188 $this->cache[$row->user_name] = $row->up_value ?:
$default;
192 private static function normalizeUsername( $username ) {
194 $indexSlash = strpos( $username,
'/' );
195 if ( $indexSlash !==
false ) {
196 $username = substr( $username, 0, $indexSlash );
200 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...