55 $this->nsInfo = $nsInfo ?? MediaWikiServices::getInstance()->getNamespaceInfo();
56 $this->dbProvider = $dbProvider;
57 $this->userOptionsLookup = $userOptionsLookup ?? MediaWikiServices::getInstance()->getUserOptionsLookup();
66 $this->
default ??= $this->userOptionsLookup->getDefaultOption(
'gender' );
67 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
88 $this->
doQuery( $username, $caller );
90 if ( $this->misses === $this->missLimit ) {
93 wfDebug( __METHOD__ .
': too many misses, returning default onwards' );
97 return $this->cache[$username] ?? $this->
getDefault();
108 foreach ( $data as $ns => $pagenames ) {
109 if ( $this->nsInfo->hasGenderDistinction( $ns ) ) {
110 $users += $pagenames;
113 $this->
doQuery( array_keys( $users ), $caller );
125 foreach ( $titles as $titleObj ) {
126 if ( $this->nsInfo->hasGenderDistinction( $titleObj->getNamespace() ) ) {
127 $users[] = $titleObj->getText();
130 $this->
doQuery( $users, $caller );
139 public function doQuery( $users, $caller =
'' ) {
143 foreach ( (array)$users as $value ) {
144 $name = self::normalizeUsername( $value );
145 if ( !isset( $this->cache[$name] ) ) {
149 $usersToFetch[] = $name;
154 if ( !$usersToFetch || !$this->dbProvider ) {
158 $caller = __METHOD__ . ( $caller ?
"/$caller" :
'' );
160 $res = $queryBuilder = $this->dbProvider->getReplicaDatabase()->newSelectQueryBuilder()
161 ->select( [
'user_name',
'up_value' ] )
163 ->leftJoin(
'user_properties',
null, [
'user_id = up_user',
'up_property' =>
'gender' ] )
164 ->where( [
'user_name' => $usersToFetch ] )
168 foreach ( $res as $row ) {
169 $this->cache[$row->user_name] = $row->up_value ?:
$default;
173 private static function normalizeUsername( $username ) {
175 $indexSlash = strpos( $username,
'/' );
176 if ( $indexSlash !==
false ) {
177 $username = substr( $username, 0, $indexSlash );
181 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.
Look up "gender" user preference.
__construct(NamespaceInfo $nsInfo=null, IConnectionProvider $dbProvider=null, UserOptionsLookup $userOptionsLookup=null)
getGenderOf( $username, $caller='')
Get the gender option for given username.
doLinkBatch(array $data, $caller='')
Wrapper for doQuery that processes raw LinkBatch data.
getDefault()
Get the default gender option on this wiki.
doTitlesArray( $titles, $caller='')
Wrapper for doQuery that processes a title array.
doQuery( $users, $caller='')
Preload gender option for multiple user names.