MediaWiki master
GenderCache.php
Go to the documentation of this file.
1<?php
29
41 protected $cache = [];
42 protected $default = null;
43 protected $misses = 0;
44 /* @internal Exposed for MediaWiki core unit tests. */
45 protected $missLimit = 1000;
46
47 private NamespaceInfo $nsInfo;
48 private ?IConnectionProvider $dbProvider;
49 private UserOptionsLookup $userOptionsLookup;
50
51 public function __construct(
52 NamespaceInfo $nsInfo = null,
53 IConnectionProvider $dbProvider = null,
54 UserOptionsLookup $userOptionsLookup = null
55 ) {
56 $this->nsInfo = $nsInfo ?? MediaWikiServices::getInstance()->getNamespaceInfo();
57 $this->dbProvider = $dbProvider;
58 $this->userOptionsLookup = $userOptionsLookup ?? MediaWikiServices::getInstance()->getUserOptionsLookup();
59 }
60
66 protected function getDefault() {
67 $this->default ??= $this->userOptionsLookup->getDefaultOption( 'gender' );
68 return $this->default;
69 }
70
78 public function getGenderOf( $username, $caller = '' ) {
79 if ( $username instanceof UserIdentity ) {
80 $username = $username->getName();
81 }
82
83 $username = self::normalizeUsername( $username );
84 if ( !isset( $this->cache[$username] ) ) {
85 if ( $this->misses < $this->missLimit ||
86 RequestContext::getMain()->getUser()->getName() === $username
87 ) {
88 $this->misses++;
89 $this->doQuery( $username, $caller );
90 }
91 if ( $this->misses === $this->missLimit ) {
92 // Log only once and don't bother incrementing beyond limit+1
93 $this->misses++;
94 wfDebug( __METHOD__ . ': too many misses, returning default onwards' );
95 }
96 }
97
98 return $this->cache[$username] ?? $this->getDefault();
99 }
100
107 public function doLinkBatch( array $data, $caller = '' ) {
108 $users = [];
109 foreach ( $data as $ns => $pagenames ) {
110 if ( $this->nsInfo->hasGenderDistinction( $ns ) ) {
111 $users += $pagenames;
112 }
113 }
114 $this->doQuery( array_keys( $users ), $caller );
115 }
116
124 public function doTitlesArray( $titles, $caller = '' ) {
125 $users = [];
126 foreach ( $titles as $titleObj ) {
127 if ( $this->nsInfo->hasGenderDistinction( $titleObj->getNamespace() ) ) {
128 $users[] = $titleObj->getText();
129 }
130 }
131 $this->doQuery( $users, $caller );
132 }
133
140 public function doQuery( $users, $caller = '' ) {
141 $default = $this->getDefault();
142
143 $usersToFetch = [];
144 foreach ( (array)$users as $value ) {
145 $name = self::normalizeUsername( $value );
146 if ( !isset( $this->cache[$name] ) ) {
147 // This may be overwritten below by a fetched value
148 $this->cache[$name] = $default;
149 // T267054: We don't need to fetch data for invalid usernames, but filtering breaks DI
150 $usersToFetch[] = $name;
151 }
152 }
153
154 // Skip query when database is unavailable (e.g. via the installer)
155 if ( !$usersToFetch || !$this->dbProvider ) {
156 return;
157 }
158
159 $caller = __METHOD__ . ( $caller ? "/$caller" : '' );
160
161 $res = $queryBuilder = $this->dbProvider->getReplicaDatabase()->newSelectQueryBuilder()
162 ->select( [ 'user_name', 'up_value' ] )
163 ->from( 'user' )
164 ->leftJoin( 'user_properties', null, [ 'user_id = up_user', 'up_property' => 'gender' ] )
165 ->where( [ 'user_name' => $usersToFetch ] )
166 ->caller( $caller )
167 ->fetchResultSet();
168
169 foreach ( $res as $row ) {
170 $this->cache[$row->user_name] = $row->up_value ?: $default;
171 }
172 }
173
174 private static function normalizeUsername( $username ) {
175 // Strip off subpages
176 $indexSlash = strpos( $username, '/' );
177 if ( $indexSlash !== false ) {
178 $username = substr( $username, 0, $indexSlash );
179 }
180
181 // normalize underscore/spaces
182 return strtr( $username, '_', ' ' );
183 }
184}
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.
__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.
Group all the pieces relevant to the context of a request into one instance.
Service locator for MediaWiki core services.
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.