59 $this->dbProvider = $dbProvider;
69 $this->
default ??= $this->userOptionsLookup->getDefaultOption(
'gender' );
82 $username = $username->getName();
85 $username = self::normalizeUsername( $username );
86 if ( !isset( $this->cache[$username] ) ) {
87 if ( $this->misses < $this->missLimit ||
88 RequestContext::getMain()->
getUser()->getName() === $username
91 $this->
doQuery( $username, $caller );
93 if ( $this->misses === $this->missLimit ) {
96 wfDebug( __METHOD__ .
': too many misses, returning default onwards' );
100 return $this->cache[$username] ?? $this->
getDefault();
111 foreach ( $data as $ns => $pagenames ) {
112 if ( $this->nsInfo->hasGenderDistinction( $ns ) ) {
113 $users += $pagenames;
116 $this->
doQuery( array_keys( $users ), $caller );
128 foreach ( $titles as $titleObj ) {
129 if ( $this->nsInfo->hasGenderDistinction( $titleObj->getNamespace() ) ) {
130 $users[] = $titleObj->getText();
133 $this->
doQuery( $users, $caller );
142 public function doQuery( $users, $caller =
'' ) {
146 foreach ( (array)$users as $value ) {
147 $name = self::normalizeUsername( $value );
148 if ( !isset( $this->cache[$name] ) ) {
152 $usersToFetch[] = $name;
157 if ( !$usersToFetch || !$this->dbProvider ) {
161 $caller = __METHOD__ . ( $caller ?
"/$caller" :
'' );
163 $res = $queryBuilder = $this->dbProvider->getReplicaDatabase()->newSelectQueryBuilder()
164 ->select( [
'user_name',
'up_value' ] )
166 ->leftJoin(
'user_properties',
null, [
'user_id = up_user',
'up_property' =>
'gender' ] )
167 ->where( [
'user_name' => $usersToFetch ] )
171 foreach ( $res as $row ) {
172 $this->cache[$row->user_name] = $row->up_value ?:
$default;
176 private static function normalizeUsername( $username ) {
178 $indexSlash = strpos( $username,
'/' );
179 if ( $indexSlash !==
false ) {
180 $username = substr( $username, 0, $indexSlash );
184 return strtr( $username,
'_',
' ' );
189class_alias( GenderCache::class,
'GenderCache' );
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Group all the pieces relevant to the context of a request into one instance.