65 $this->dbProvider = $dbProvider;
75 $this->
default ??= $this->userOptionsLookup->getDefaultOption(
'gender' );
88 $username = $username->getName();
91 $username = self::normalizeUsername( $username );
92 if ( !isset( $this->cache[$username] ) ) {
93 if ( $this->misses < $this->missLimit ||
94 RequestContext::getMain()->getUser()->getName() === $username
97 $this->
doQuery( $username, $caller );
99 if ( $this->misses === $this->missLimit ) {
102 wfDebug( __METHOD__ .
': too many misses, returning default onwards' );
106 return $this->cache[$username] ?? $this->
getDefault();
117 foreach ( $data as $ns => $pagenames ) {
118 if ( $this->nsInfo->hasGenderDistinction( $ns ) ) {
119 $users += $pagenames;
122 $this->
doQuery( array_keys( $users ), $caller );
134 foreach ( $titles as $titleObj ) {
135 if ( $this->nsInfo->hasGenderDistinction( $titleObj->getNamespace() ) ) {
136 $users[] = $titleObj->getText();
139 $this->
doQuery( $users, $caller );
148 public function doQuery( $users, $caller =
'' ) {
152 foreach ( (array)$users as $value ) {
153 $name = self::normalizeUsername( $value );
154 if ( !isset( $this->cache[$name] ) ) {
158 $usersToFetch[] = $name;
163 if ( !$usersToFetch || !$this->dbProvider ) {
167 $caller = __METHOD__ . ( $caller ?
"/$caller" :
'' );
169 $res = $queryBuilder = $this->dbProvider->getReplicaDatabase()->newSelectQueryBuilder()
170 ->select( [
'user_name',
'up_value' ] )
172 ->leftJoin(
'user_properties',
null, [
'user_id = up_user',
'up_property' =>
'gender' ] )
173 ->where( [
'user_name' => $usersToFetch ] )
177 foreach ( $res as $row ) {
178 $this->cache[$row->user_name] = $row->up_value ?:
$default;
182 private static function normalizeUsername( $username ) {
184 $indexSlash = strpos( $username,
'/' );
185 if ( $indexSlash !==
false ) {
186 $username = substr( $username, 0, $indexSlash );
190 return strtr( $username,
'_',
' ' );
195class_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.