46 $this->nsInfo =
$nsInfo ?? MediaWikiServices::getInstance()->getNamespaceInfo();
55 return MediaWikiServices::getInstance()->getGenderCache();
63 if ( $this->
default ===
null ) {
67 return $this->default;
79 if ( $username instanceof
User ) {
80 $username = $username->getName();
83 $username = self::normalizeUsername( $username );
84 if ( !isset( $this->cache[$username] ) ) {
85 if ( $this->misses >= $this->missLimit && $wgUser->getName() !== $username ) {
86 if ( $this->misses === $this->missLimit ) {
88 wfDebug( __METHOD__ .
": too many misses, returning default onwards\n" );
94 $this->
doQuery( $username, $caller );
101 return $this->cache[$username] ?? $this->
getDefault();
112 foreach ( $data as $ns => $pagenames ) {
113 if ( !$this->nsInfo->hasGenderDistinction( $ns ) ) {
116 foreach ( array_keys( $pagenames ) as $username ) {
117 $users[$username] =
true;
121 $this->
doQuery( array_keys( $users ), $caller );
133 foreach ( $titles as
$title ) {
138 if ( !$this->nsInfo->hasGenderDistinction( $titleObj->getNamespace() ) ) {
141 $users[] = $titleObj->getText();
144 $this->
doQuery( $users, $caller );
152 public function doQuery( $users, $caller =
'' ) {
156 foreach ( (array)$users as $value ) {
157 $name = self::normalizeUsername( $value );
159 if ( !isset( $this->cache[$name] ) ) {
164 $usersToCheck[] = $name;
169 if ( count( $usersToCheck ) === 0 ) {
175 if ( $this->loadBalancer ===
null ) {
180 $table = [
'user',
'user_properties' ];
181 $fields = [
'user_name',
'up_value' ];
182 $conds = [
'user_name' => $usersToCheck ];
183 $joins = [
'user_properties' =>
184 [
'LEFT JOIN', [
'user_id = up_user',
'up_property' =>
'gender' ] ] ];
186 $comment = __METHOD__;
187 if ( strval( $caller ) !==
'' ) {
188 $comment .=
"/$caller";
190 $res =
$dbr->select( $table, $fields, $conds, $comment, [], $joins );
192 foreach (
$res as $row ) {
193 $this->cache[$row->user_name] = $row->up_value ?:
$default;
199 $indexSlash = strpos( $username,
'/' );
200 if ( $indexSlash !==
false ) {
201 $username = substr( $username, 0, $indexSlash );
205 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.
getGenderOf( $username, $caller='')
Returns the gender for given username.
static normalizeUsername( $username)
doLinkBatch( $data, $caller='')
Wrapper for doQuery that processes raw LinkBatch data.
getDefault()
Returns the default gender option in this wiki.
__construct(NamespaceInfo $nsInfo=null, ILoadBalancer $loadBalancer=null)
ILoadBalancer null $loadBalancer
doTitlesArray( $titles, $caller='')
Wrapper for doQuery that processes a title or string 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...
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
static getDefaultOption( $opt)
Get a given default option value.
static isValidUserName( $name)
Is the input a valid username?