Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
17.65% |
9 / 51 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
GenderCache | |
18.00% |
9 / 50 |
|
0.00% |
0 / 7 |
341.59 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
getDefault | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
getGenderOf | |
75.00% |
9 / 12 |
|
0.00% |
0 / 1 |
6.56 | |||
doLinkBatch | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
doTitlesArray | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
doQuery | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
72 | |||
normalizeUsername | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | /** |
3 | * This program is free software; you can redistribute it and/or modify |
4 | * it under the terms of the GNU General Public License as published by |
5 | * the Free Software Foundation; either version 2 of the License, or |
6 | * (at your option) any later version. |
7 | * |
8 | * This program is distributed in the hope that it will be useful, |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | * GNU General Public License for more details. |
12 | * |
13 | * You should have received a copy of the GNU General Public License along |
14 | * with this program; if not, write to the Free Software Foundation, Inc., |
15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
16 | * http://www.gnu.org/copyleft/gpl.html |
17 | * |
18 | * @file |
19 | * @author Niklas Laxström |
20 | */ |
21 | |
22 | namespace MediaWiki\Cache; |
23 | |
24 | use MediaWiki\Context\RequestContext; |
25 | use MediaWiki\Linker\LinkTarget; |
26 | use MediaWiki\MediaWikiServices; |
27 | use MediaWiki\Title\NamespaceInfo; |
28 | use MediaWiki\User\Options\UserOptionsLookup; |
29 | use MediaWiki\User\UserIdentity; |
30 | use Wikimedia\Rdbms\IConnectionProvider; |
31 | |
32 | /** |
33 | * Look up "gender" user preference. |
34 | * |
35 | * This primarily used in MediaWiki\Title\MediaWikiTitleCodec for title formatting |
36 | * of pages in gendered namespace aliases, and in CoreParserFunctions for the |
37 | * `{{gender:}}` parser function. |
38 | * |
39 | * @since 1.18 |
40 | * @ingroup Cache |
41 | */ |
42 | class GenderCache { |
43 | protected $cache = []; |
44 | protected $default = null; |
45 | protected $misses = 0; |
46 | /* @internal Exposed for MediaWiki core unit tests. */ |
47 | protected $missLimit = 1000; |
48 | |
49 | private NamespaceInfo $nsInfo; |
50 | private ?IConnectionProvider $dbProvider; |
51 | private UserOptionsLookup $userOptionsLookup; |
52 | |
53 | public function __construct( |
54 | NamespaceInfo $nsInfo = null, |
55 | IConnectionProvider $dbProvider = null, |
56 | UserOptionsLookup $userOptionsLookup = null |
57 | ) { |
58 | $this->nsInfo = $nsInfo ?? MediaWikiServices::getInstance()->getNamespaceInfo(); |
59 | $this->dbProvider = $dbProvider; |
60 | $this->userOptionsLookup = $userOptionsLookup ?? MediaWikiServices::getInstance()->getUserOptionsLookup(); |
61 | } |
62 | |
63 | /** |
64 | * Get the default gender option on this wiki. |
65 | * |
66 | * @return string |
67 | */ |
68 | protected function getDefault() { |
69 | $this->default ??= $this->userOptionsLookup->getDefaultOption( 'gender' ); |
70 | return $this->default; |
71 | } |
72 | |
73 | /** |
74 | * Get the gender option for given username. |
75 | * |
76 | * @param string|UserIdentity $username |
77 | * @param string|null $caller Calling method for database profiling |
78 | * @return string |
79 | */ |
80 | public function getGenderOf( $username, $caller = '' ) { |
81 | if ( $username instanceof UserIdentity ) { |
82 | $username = $username->getName(); |
83 | } |
84 | |
85 | $username = self::normalizeUsername( $username ); |
86 | if ( !isset( $this->cache[$username] ) ) { |
87 | if ( $this->misses < $this->missLimit || |
88 | RequestContext::getMain()->getUser()->getName() === $username |
89 | ) { |
90 | $this->misses++; |
91 | $this->doQuery( $username, $caller ); |
92 | } |
93 | if ( $this->misses === $this->missLimit ) { |
94 | // Log only once and don't bother incrementing beyond limit+1 |
95 | $this->misses++; |
96 | wfDebug( __METHOD__ . ': too many misses, returning default onwards' ); |
97 | } |
98 | } |
99 | |
100 | return $this->cache[$username] ?? $this->getDefault(); |
101 | } |
102 | |
103 | /** |
104 | * Wrapper for doQuery that processes raw LinkBatch data. |
105 | * |
106 | * @param array<int,array<string,mixed>> $data |
107 | * @param string|null $caller |
108 | */ |
109 | public function doLinkBatch( array $data, $caller = '' ) { |
110 | $users = []; |
111 | foreach ( $data as $ns => $pagenames ) { |
112 | if ( $this->nsInfo->hasGenderDistinction( $ns ) ) { |
113 | $users += $pagenames; |
114 | } |
115 | } |
116 | $this->doQuery( array_keys( $users ), $caller ); |
117 | } |
118 | |
119 | /** |
120 | * Wrapper for doQuery that processes a title array. |
121 | * |
122 | * @since 1.20 |
123 | * @param LinkTarget[] $titles |
124 | * @param string|null $caller Calling method for database profiling |
125 | */ |
126 | public function doTitlesArray( $titles, $caller = '' ) { |
127 | $users = []; |
128 | foreach ( $titles as $titleObj ) { |
129 | if ( $this->nsInfo->hasGenderDistinction( $titleObj->getNamespace() ) ) { |
130 | $users[] = $titleObj->getText(); |
131 | } |
132 | } |
133 | $this->doQuery( $users, $caller ); |
134 | } |
135 | |
136 | /** |
137 | * Preload gender option for multiple user names. |
138 | * |
139 | * @param string[]|string $users Usernames |
140 | * @param string|null $caller Calling method for database profiling |
141 | */ |
142 | public function doQuery( $users, $caller = '' ) { |
143 | $default = $this->getDefault(); |
144 | |
145 | $usersToFetch = []; |
146 | foreach ( (array)$users as $value ) { |
147 | $name = self::normalizeUsername( $value ); |
148 | if ( !isset( $this->cache[$name] ) ) { |
149 | // This may be overwritten below by a fetched value |
150 | $this->cache[$name] = $default; |
151 | // T267054: We don't need to fetch data for invalid usernames, but filtering breaks DI |
152 | $usersToFetch[] = $name; |
153 | } |
154 | } |
155 | |
156 | // Skip query when database is unavailable (e.g. via the installer) |
157 | if ( !$usersToFetch || !$this->dbProvider ) { |
158 | return; |
159 | } |
160 | |
161 | $caller = __METHOD__ . ( $caller ? "/$caller" : '' ); |
162 | |
163 | $res = $queryBuilder = $this->dbProvider->getReplicaDatabase()->newSelectQueryBuilder() |
164 | ->select( [ 'user_name', 'up_value' ] ) |
165 | ->from( 'user' ) |
166 | ->leftJoin( 'user_properties', null, [ 'user_id = up_user', 'up_property' => 'gender' ] ) |
167 | ->where( [ 'user_name' => $usersToFetch ] ) |
168 | ->caller( $caller ) |
169 | ->fetchResultSet(); |
170 | |
171 | foreach ( $res as $row ) { |
172 | $this->cache[$row->user_name] = $row->up_value ?: $default; |
173 | } |
174 | } |
175 | |
176 | private static function normalizeUsername( $username ) { |
177 | // Strip off subpages |
178 | $indexSlash = strpos( $username, '/' ); |
179 | if ( $indexSlash !== false ) { |
180 | $username = substr( $username, 0, $indexSlash ); |
181 | } |
182 | |
183 | // normalize underscore/spaces |
184 | return strtr( $username, '_', ' ' ); |
185 | } |
186 | } |
187 | |
188 | /** @deprecated class alias since 1.42 */ |
189 | class_alias( GenderCache::class, 'GenderCache' ); |