Merge "maintenance: Script to rename titles for Unicode uppercasing changes"
[lhc/web/wiklou.git] / includes / cache / GenderCache.php
1 <?php
2 /**
3 * Caches user genders when needed to use correct namespace aliases.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @author Niklas Laxström
22 * @ingroup Cache
23 */
24 use MediaWiki\MediaWikiServices;
25
26 /**
27 * Caches user genders when needed to use correct namespace aliases.
28 *
29 * @since 1.18
30 */
31 class GenderCache {
32 protected $cache = [];
33 protected $default;
34 protected $misses = 0;
35 protected $missLimit = 1000;
36
37 /** @var NamespaceInfo */
38 private $nsInfo;
39
40 public function __construct( NamespaceInfo $nsInfo = null ) {
41 $this->nsInfo = $nsInfo ?? MediaWikiServices::getInstance()->getNamespaceInfo();
42 }
43
44 /**
45 * @deprecated in 1.28 see MediaWikiServices::getInstance()->getGenderCache()
46 * @return GenderCache
47 */
48 public static function singleton() {
49 return MediaWikiServices::getInstance()->getGenderCache();
50 }
51
52 /**
53 * Returns the default gender option in this wiki.
54 * @return string
55 */
56 protected function getDefault() {
57 if ( $this->default === null ) {
58 $this->default = User::getDefaultOption( 'gender' );
59 }
60
61 return $this->default;
62 }
63
64 /**
65 * Returns the gender for given username.
66 * @param string|User $username
67 * @param string $caller The calling method
68 * @return string
69 */
70 public function getGenderOf( $username, $caller = '' ) {
71 global $wgUser;
72
73 if ( $username instanceof User ) {
74 $username = $username->getName();
75 }
76
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 ) {
81 $this->misses++;
82 wfDebug( __METHOD__ . ": too many misses, returning default onwards\n" );
83 }
84
85 return $this->getDefault();
86 } else {
87 $this->misses++;
88 $this->doQuery( $username, $caller );
89 }
90 }
91
92 /* Undefined if there is a valid username which for some reason doesn't
93 * exist in the database.
94 */
95 return $this->cache[$username] ?? $this->getDefault();
96 }
97
98 /**
99 * Wrapper for doQuery that processes raw LinkBatch data.
100 *
101 * @param array $data
102 * @param string $caller
103 */
104 public function doLinkBatch( $data, $caller = '' ) {
105 $users = [];
106 foreach ( $data as $ns => $pagenames ) {
107 if ( !$this->nsInfo->hasGenderDistinction( $ns ) ) {
108 continue;
109 }
110 foreach ( array_keys( $pagenames ) as $username ) {
111 $users[$username] = true;
112 }
113 }
114
115 $this->doQuery( array_keys( $users ), $caller );
116 }
117
118 /**
119 * Wrapper for doQuery that processes a title or string array.
120 *
121 * @since 1.20
122 * @param array $titles Array of Title objects or strings
123 * @param string $caller The calling method
124 */
125 public function doTitlesArray( $titles, $caller = '' ) {
126 $users = [];
127 foreach ( $titles as $title ) {
128 $titleObj = is_string( $title ) ? Title::newFromText( $title ) : $title;
129 if ( !$titleObj ) {
130 continue;
131 }
132 if ( !$this->nsInfo->hasGenderDistinction( $titleObj->getNamespace() ) ) {
133 continue;
134 }
135 $users[] = $titleObj->getText();
136 }
137
138 $this->doQuery( $users, $caller );
139 }
140
141 /**
142 * Preloads genders for given list of users.
143 * @param array|string $users Usernames
144 * @param string $caller The calling method
145 */
146 public function doQuery( $users, $caller = '' ) {
147 $default = $this->getDefault();
148
149 $usersToCheck = [];
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;
159 }
160 }
161 }
162
163 if ( count( $usersToCheck ) === 0 ) {
164 return;
165 }
166
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' ] ] ];
173
174 $comment = __METHOD__;
175 if ( strval( $caller ) !== '' ) {
176 $comment .= "/$caller";
177 }
178 $res = $dbr->select( $table, $fields, $conds, $comment, [], $joins );
179
180 foreach ( $res as $row ) {
181 $this->cache[$row->user_name] = $row->up_value ?: $default;
182 }
183 }
184
185 private static function normalizeUsername( $username ) {
186 // Strip off subpages
187 $indexSlash = strpos( $username, '/' );
188 if ( $indexSlash !== false ) {
189 $username = substr( $username, 0, $indexSlash );
190 }
191
192 // normalize underscore/spaces
193 return strtr( $username, '_', ' ' );
194 }
195 }