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
24 use MediaWiki\MediaWikiServices
;
27 * Caches user genders when needed to use correct namespace aliases.
32 protected $cache = [];
34 protected $misses = 0;
35 protected $missLimit = 1000;
37 /** @var NamespaceInfo */
40 public function __construct( NamespaceInfo
$nsInfo = null ) {
41 $this->nsInfo
= $nsInfo ?? MediaWikiServices
::getInstance()->getNamespaceInfo();
45 * @deprecated in 1.28 see MediaWikiServices::getInstance()->getGenderCache()
48 public static function singleton() {
49 return MediaWikiServices
::getInstance()->getGenderCache();
53 * Returns the default gender option in this wiki.
56 protected function getDefault() {
57 if ( $this->default === null ) {
58 $this->default = User
::getDefaultOption( 'gender' );
61 return $this->default;
65 * Returns the gender for given username.
66 * @param string|User $username
67 * @param string $caller The calling method
70 public function getGenderOf( $username, $caller = '' ) {
73 if ( $username instanceof User
) {
74 $username = $username->getName();
77 $username = self
::normalizeUsername( $username );
78 if ( !isset( $this->cache
[$username] ) ) {
79 if ( $this->misses
>= $this->missLimit
&& $wgUser->getName() !== $username ) {
80 if ( $this->misses
=== $this->missLimit
) {
82 wfDebug( __METHOD__
. ": too many misses, returning default onwards\n" );
85 return $this->getDefault();
88 $this->doQuery( $username, $caller );
92 /* Undefined if there is a valid username which for some reason doesn't
93 * exist in the database.
95 return $this->cache
[$username] ??
$this->getDefault();
99 * Wrapper for doQuery that processes raw LinkBatch data.
102 * @param string $caller
104 public function doLinkBatch( $data, $caller = '' ) {
106 foreach ( $data as $ns => $pagenames ) {
107 if ( !$this->nsInfo
->hasGenderDistinction( $ns ) ) {
110 foreach ( array_keys( $pagenames ) as $username ) {
111 $users[$username] = true;
115 $this->doQuery( array_keys( $users ), $caller );
119 * Wrapper for doQuery that processes a title or string array.
122 * @param array $titles Array of Title objects or strings
123 * @param string $caller The calling method
125 public function doTitlesArray( $titles, $caller = '' ) {
127 foreach ( $titles as $title ) {
128 $titleObj = is_string( $title ) ? Title
::newFromText( $title ) : $title;
132 if ( !$this->nsInfo
->hasGenderDistinction( $titleObj->getNamespace() ) ) {
135 $users[] = $titleObj->getText();
138 $this->doQuery( $users, $caller );
142 * Preloads genders for given list of users.
143 * @param array|string $users Usernames
144 * @param string $caller The calling method
146 public function doQuery( $users, $caller = '' ) {
147 $default = $this->getDefault();
150 foreach ( (array)$users as $value ) {
151 $name = self
::normalizeUsername( $value );
152 // Skip users whose gender setting we already know
153 if ( !isset( $this->cache
[$name] ) ) {
154 // For existing users, this value will be overwritten by the correct value
155 $this->cache
[$name] = $default;
156 // query only for valid names, which can be in the database
157 if ( User
::isValidUserName( $name ) ) {
158 $usersToCheck[] = $name;
163 if ( count( $usersToCheck ) === 0 ) {
167 $dbr = wfGetDB( DB_REPLICA
);
168 $table = [ 'user', 'user_properties' ];
169 $fields = [ 'user_name', 'up_value' ];
170 $conds = [ 'user_name' => $usersToCheck ];
171 $joins = [ 'user_properties' =>
172 [ 'LEFT JOIN', [ 'user_id = up_user', 'up_property' => 'gender' ] ] ];
174 $comment = __METHOD__
;
175 if ( strval( $caller ) !== '' ) {
176 $comment .= "/$caller";
178 $res = $dbr->select( $table, $fields, $conds, $comment, [], $joins );
180 foreach ( $res as $row ) {
181 $this->cache
[$row->user_name
] = $row->up_value ?
: $default;
185 private static function normalizeUsername( $username ) {
186 // Strip off subpages
187 $indexSlash = strpos( $username, '/' );
188 if ( $indexSlash !== false ) {
189 $username = substr( $username, 0, $indexSlash );
192 // normalize underscore/spaces
193 return strtr( $username, '_', ' ' );