MediaWiki REL1_40
GenderCache.php
Go to the documentation of this file.
1<?php
27
35 protected $cache = [];
36 protected $default;
37 protected $misses = 0;
38 protected $missLimit = 1000;
39
41 private $nsInfo;
42
44 private $loadBalancer;
45
47 private $userOptionsLookup;
48
49 public function __construct(
50 NamespaceInfo $nsInfo = null,
51 ILoadBalancer $loadBalancer = null,
52 UserOptionsLookup $userOptionsLookup = null
53 ) {
54 $this->nsInfo = $nsInfo ?? MediaWikiServices::getInstance()->getNamespaceInfo();
55 $this->loadBalancer = $loadBalancer;
56 $this->userOptionsLookup = $userOptionsLookup ?? MediaWikiServices::getInstance()->getUserOptionsLookup();
57 }
58
63 protected function getDefault() {
64 $this->default ??= $this->userOptionsLookup->getDefaultOption( 'gender' );
65
66 return $this->default;
67 }
68
75 public function getGenderOf( $username, $caller = '' ) {
76 if ( $username instanceof UserIdentity ) {
77 $username = $username->getName();
78 }
79
80 $username = self::normalizeUsername( $username );
81 if ( !isset( $this->cache[$username] ) ) {
82 if ( $this->misses >= $this->missLimit &&
83 RequestContext::getMain()->getUser()->getName() !== $username
84 ) {
85 if ( $this->misses === $this->missLimit ) {
86 $this->misses++;
87 wfDebug( __METHOD__ . ": too many misses, returning default onwards" );
88 }
89
90 return $this->getDefault();
91 } else {
92 $this->misses++;
93 $this->doQuery( $username, $caller );
94 }
95 }
96
97 /* Undefined if there is a valid username which for some reason doesn't
98 * exist in the database.
99 */
100 return $this->cache[$username] ?? $this->getDefault();
101 }
102
109 public function doLinkBatch( $data, $caller = '' ) {
110 $users = [];
111 foreach ( $data as $ns => $pagenames ) {
112 if ( !$this->nsInfo->hasGenderDistinction( $ns ) ) {
113 continue;
114 }
115 foreach ( array_keys( $pagenames ) as $username ) {
116 $users[$username] = true;
117 }
118 }
119
120 $this->doQuery( array_keys( $users ), $caller );
121 }
122
130 public function doTitlesArray( $titles, $caller = '' ) {
131 $users = [];
132 foreach ( $titles as $titleObj ) {
133 if ( !$this->nsInfo->hasGenderDistinction( $titleObj->getNamespace() ) ) {
134 continue;
135 }
136 $users[] = $titleObj->getText();
137 }
138
139 $this->doQuery( $users, $caller );
140 }
141
147 public function doQuery( $users, $caller = '' ) {
148 $default = $this->getDefault();
149
150 $usersToCheck = [];
151 foreach ( (array)$users as $value ) {
152 $name = self::normalizeUsername( $value );
153 // Skip users whose gender setting we already know
154 if ( !isset( $this->cache[$name] ) ) {
155 // For existing users, this value will be overwritten by the correct value
156 $this->cache[$name] = $default;
157 // We no longer verify that only valid names are checked for, T267054
158 $usersToCheck[] = $name;
159 }
160 }
161
162 if ( count( $usersToCheck ) === 0 ) {
163 return;
164 }
165
166 // Only query database, when load balancer is provided by service wiring
167 // This maybe not happen when running as part of the installer
168 if ( $this->loadBalancer === null ) {
169 return;
170 }
171
172 $dbr = $this->loadBalancer->getConnectionRef( DB_REPLICA );
173 $table = [ 'user', 'user_properties' ];
174 $fields = [ 'user_name', 'up_value' ];
175 $conds = [ 'user_name' => $usersToCheck ];
176 $joins = [ 'user_properties' =>
177 [ 'LEFT JOIN', [ 'user_id = up_user', 'up_property' => 'gender' ] ] ];
178
179 $comment = __METHOD__;
180 if ( strval( $caller ) !== '' ) {
181 $comment .= "/$caller";
182 }
183 $res = $dbr->select( $table, $fields, $conds, $comment, [], $joins );
184
185 foreach ( $res as $row ) {
186 $this->cache[$row->user_name] = $row->up_value ?: $default;
187 }
188 }
189
190 private static function normalizeUsername( $username ) {
191 // Strip off subpages
192 $indexSlash = strpos( $username, '/' );
193 if ( $indexSlash !== false ) {
194 $username = substr( $username, 0, $indexSlash );
195 }
196
197 // normalize underscore/spaces
198 return strtr( $username, '_', ' ' );
199 }
200}
getUser()
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Caches user genders when needed to use correct namespace aliases.
__construct(NamespaceInfo $nsInfo=null, ILoadBalancer $loadBalancer=null, UserOptionsLookup $userOptionsLookup=null)
getGenderOf( $username, $caller='')
Returns the gender for given username.
doLinkBatch( $data, $caller='')
Wrapper for doQuery that processes raw LinkBatch data.
getDefault()
Returns the default gender option in this wiki.
doTitlesArray( $titles, $caller='')
Wrapper for doQuery that processes a title array.
doQuery( $users, $caller='')
Preloads genders for given list of users.
Service locator for MediaWiki core services.
Provides access to user options.
This is a utility class for dealing with namespaces that encodes all the "magic" behaviors of them ba...
Interface for objects representing user identity.
This class is a delegate to ILBFactory for a given database cluster.
const DB_REPLICA
Definition defines.php:26