3 * Caches user genders when needed to use correct namespace aliases.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @author Niklas Laxström
25 use MediaWiki\MediaWikiServices
;
26 use Wikimedia\Rdbms\ILoadBalancer
;
29 * Caches user genders when needed to use correct namespace aliases.
34 protected $cache = [];
36 protected $misses = 0;
37 protected $missLimit = 1000;
39 /** @var NamespaceInfo */
42 /** @var ILoadBalancer|null */
43 private $loadBalancer;
45 public function __construct( NamespaceInfo
$nsInfo = null, ILoadBalancer
$loadBalancer = null ) {
46 $this->nsInfo
= $nsInfo ?? MediaWikiServices
::getInstance()->getNamespaceInfo();
47 $this->loadBalancer
= $loadBalancer;
51 * @deprecated in 1.28 see MediaWikiServices::getInstance()->getGenderCache()
54 public static function singleton() {
55 return MediaWikiServices
::getInstance()->getGenderCache();
59 * Returns the default gender option in this wiki.
62 protected function getDefault() {
63 if ( $this->default === null ) {
64 $this->default = User
::getDefaultOption( 'gender' );
67 return $this->default;
71 * Returns the gender for given username.
72 * @param string|User $username
73 * @param string $caller The calling method
76 public function getGenderOf( $username, $caller = '' ) {
79 if ( $username instanceof User
) {
80 $username = $username->getName();
83 $username = self
::normalizeUsername( $username );
84 if ( !isset( $this->cache
[$username] ) ) {
85 if ( $this->misses
>= $this->missLimit
&& $wgUser->getName() !== $username ) {
86 if ( $this->misses
=== $this->missLimit
) {
88 wfDebug( __METHOD__
. ": too many misses, returning default onwards\n" );
91 return $this->getDefault();
94 $this->doQuery( $username, $caller );
98 /* Undefined if there is a valid username which for some reason doesn't
99 * exist in the database.
101 return $this->cache
[$username] ??
$this->getDefault();
105 * Wrapper for doQuery that processes raw LinkBatch data.
108 * @param string $caller
110 public function doLinkBatch( $data, $caller = '' ) {
112 foreach ( $data as $ns => $pagenames ) {
113 if ( !$this->nsInfo
->hasGenderDistinction( $ns ) ) {
116 foreach ( array_keys( $pagenames ) as $username ) {
117 $users[$username] = true;
121 $this->doQuery( array_keys( $users ), $caller );
125 * Wrapper for doQuery that processes a title or string array.
128 * @param array $titles Array of Title objects or strings
129 * @param string $caller The calling method
131 public function doTitlesArray( $titles, $caller = '' ) {
133 foreach ( $titles as $title ) {
134 $titleObj = is_string( $title ) ? Title
::newFromText( $title ) : $title;
138 if ( !$this->nsInfo
->hasGenderDistinction( $titleObj->getNamespace() ) ) {
141 $users[] = $titleObj->getText();
144 $this->doQuery( $users, $caller );
148 * Preloads genders for given list of users.
149 * @param array|string $users Usernames
150 * @param string $caller The calling method
152 public function doQuery( $users, $caller = '' ) {
153 $default = $this->getDefault();
156 foreach ( (array)$users as $value ) {
157 $name = self
::normalizeUsername( $value );
158 // Skip users whose gender setting we already know
159 if ( !isset( $this->cache
[$name] ) ) {
160 // For existing users, this value will be overwritten by the correct value
161 $this->cache
[$name] = $default;
162 // query only for valid names, which can be in the database
163 if ( User
::isValidUserName( $name ) ) {
164 $usersToCheck[] = $name;
169 if ( count( $usersToCheck ) === 0 ) {
173 // Only query database, when load balancer is provided by service wiring
174 // This maybe not happen when running as part of the installer
175 if ( $this->loadBalancer
=== null ) {
179 $dbr = $this->loadBalancer
->getConnectionRef( DB_REPLICA
);
180 $table = [ 'user', 'user_properties' ];
181 $fields = [ 'user_name', 'up_value' ];
182 $conds = [ 'user_name' => $usersToCheck ];
183 $joins = [ 'user_properties' =>
184 [ 'LEFT JOIN', [ 'user_id = up_user', 'up_property' => 'gender' ] ] ];
186 $comment = __METHOD__
;
187 if ( strval( $caller ) !== '' ) {
188 $comment .= "/$caller";
190 $res = $dbr->select( $table, $fields, $conds, $comment, [], $joins );
192 foreach ( $res as $row ) {
193 $this->cache
[$row->user_name
] = $row->up_value ?
: $default;
197 private static function normalizeUsername( $username ) {
198 // Strip off subpages
199 $indexSlash = strpos( $username, '/' );
200 if ( $indexSlash !== false ) {
201 $username = substr( $username, 0, $indexSlash );
204 // normalize underscore/spaces
205 return strtr( $username, '_', ' ' );