MediaWiki master
GenderCache.php
Go to the documentation of this file.
1<?php
22namespace MediaWiki\Cache;
23
31
43 protected $cache = [];
44 protected $default = null;
45 protected $misses = 0;
46 /* @internal Exposed for MediaWiki core unit tests. */
47 protected $missLimit = 1000;
48
49 private NamespaceInfo $nsInfo;
50 private ?IConnectionProvider $dbProvider;
51 private UserOptionsLookup $userOptionsLookup;
52
53 public function __construct(
54 NamespaceInfo $nsInfo = null,
55 IConnectionProvider $dbProvider = null,
56 UserOptionsLookup $userOptionsLookup = null
57 ) {
58 $this->nsInfo = $nsInfo ?? MediaWikiServices::getInstance()->getNamespaceInfo();
59 $this->dbProvider = $dbProvider;
60 $this->userOptionsLookup = $userOptionsLookup ?? MediaWikiServices::getInstance()->getUserOptionsLookup();
61 }
62
68 protected function getDefault() {
69 $this->default ??= $this->userOptionsLookup->getDefaultOption( 'gender' );
70 return $this->default;
71 }
72
80 public function getGenderOf( $username, $caller = '' ) {
81 if ( $username instanceof UserIdentity ) {
82 $username = $username->getName();
83 }
84
85 $username = self::normalizeUsername( $username );
86 if ( !isset( $this->cache[$username] ) ) {
87 if ( $this->misses < $this->missLimit ||
88 RequestContext::getMain()->getUser()->getName() === $username
89 ) {
90 $this->misses++;
91 $this->doQuery( $username, $caller );
92 }
93 if ( $this->misses === $this->missLimit ) {
94 // Log only once and don't bother incrementing beyond limit+1
95 $this->misses++;
96 wfDebug( __METHOD__ . ': too many misses, returning default onwards' );
97 }
98 }
99
100 return $this->cache[$username] ?? $this->getDefault();
101 }
102
109 public function doLinkBatch( array $data, $caller = '' ) {
110 $users = [];
111 foreach ( $data as $ns => $pagenames ) {
112 if ( $this->nsInfo->hasGenderDistinction( $ns ) ) {
113 $users += $pagenames;
114 }
115 }
116 $this->doQuery( array_keys( $users ), $caller );
117 }
118
126 public function doTitlesArray( $titles, $caller = '' ) {
127 $users = [];
128 foreach ( $titles as $titleObj ) {
129 if ( $this->nsInfo->hasGenderDistinction( $titleObj->getNamespace() ) ) {
130 $users[] = $titleObj->getText();
131 }
132 }
133 $this->doQuery( $users, $caller );
134 }
135
142 public function doQuery( $users, $caller = '' ) {
143 $default = $this->getDefault();
144
145 $usersToFetch = [];
146 foreach ( (array)$users as $value ) {
147 $name = self::normalizeUsername( $value );
148 if ( !isset( $this->cache[$name] ) ) {
149 // This may be overwritten below by a fetched value
150 $this->cache[$name] = $default;
151 // T267054: We don't need to fetch data for invalid usernames, but filtering breaks DI
152 $usersToFetch[] = $name;
153 }
154 }
155
156 // Skip query when database is unavailable (e.g. via the installer)
157 if ( !$usersToFetch || !$this->dbProvider ) {
158 return;
159 }
160
161 $caller = __METHOD__ . ( $caller ? "/$caller" : '' );
162
163 $res = $queryBuilder = $this->dbProvider->getReplicaDatabase()->newSelectQueryBuilder()
164 ->select( [ 'user_name', 'up_value' ] )
165 ->from( 'user' )
166 ->leftJoin( 'user_properties', null, [ 'user_id = up_user', 'up_property' => 'gender' ] )
167 ->where( [ 'user_name' => $usersToFetch ] )
168 ->caller( $caller )
169 ->fetchResultSet();
170
171 foreach ( $res as $row ) {
172 $this->cache[$row->user_name] = $row->up_value ?: $default;
173 }
174 }
175
176 private static function normalizeUsername( $username ) {
177 // Strip off subpages
178 $indexSlash = strpos( $username, '/' );
179 if ( $indexSlash !== false ) {
180 $username = substr( $username, 0, $indexSlash );
181 }
182
183 // normalize underscore/spaces
184 return strtr( $username, '_', ' ' );
185 }
186}
187
189class_alias( GenderCache::class, 'GenderCache' );
getUser()
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.
doQuery( $users, $caller='')
Preload gender option for multiple user names.
__construct(NamespaceInfo $nsInfo=null, IConnectionProvider $dbProvider=null, UserOptionsLookup $userOptionsLookup=null)
getDefault()
Get the default gender option on this wiki.
getGenderOf( $username, $caller='')
Get the gender option for given username.
doTitlesArray( $titles, $caller='')
Wrapper for doQuery that processes a title array.
doLinkBatch(array $data, $caller='')
Wrapper for doQuery that processes raw LinkBatch data.
Group all the pieces relevant to the context of a request into one instance.
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
This is a utility class for dealing with namespaces that encodes all the "magic" behaviors of them ba...
Provides access to user options.
Represents the target of a wiki link.
Interface for objects representing user identity.
Provide primary and replica IDatabase connections.