Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
21.95% covered (danger)
21.95%
9 / 41
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
GenderCache
22.50% covered (danger)
22.50%
9 / 40
0.00% covered (danger)
0.00%
0 / 7
226.28
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getDefault
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getGenderOf
75.00% covered (warning)
75.00%
9 / 12
0.00% covered (danger)
0.00%
0 / 1
6.56
 doLinkBatch
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 doTitlesArray
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 doQuery
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
30
 normalizeUsername
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
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
22namespace MediaWiki\Cache;
23
24use MediaWiki\Context\RequestContext;
25use MediaWiki\Linker\LinkTarget;
26use MediaWiki\Title\NamespaceInfo;
27use MediaWiki\User\Options\UserOptionsLookup;
28use MediaWiki\User\UserIdentity;
29
30/**
31 * Look up "gender" user preference.
32 *
33 * This primarily used in MediaWiki\Title\TitleFormatter for title formatting
34 * of pages in gendered namespace aliases, and in CoreParserFunctions for the
35 * `{{gender:}}` parser function.
36 *
37 * @since 1.18
38 * @ingroup Cache
39 */
40class GenderCache {
41    /** @var string[] */
42    protected $cache = [];
43    /** @var string|null */
44    protected $default = null;
45    /** @var int */
46    protected $misses = 0;
47    /**
48     * @internal Exposed for MediaWiki core unit tests.
49     * @var int
50     */
51    protected $missLimit = 1000;
52
53    private NamespaceInfo $nsInfo;
54    private UserOptionsLookup $userOptionsLookup;
55
56    public function __construct(
57        NamespaceInfo $nsInfo,
58        UserOptionsLookup $userOptionsLookup
59    ) {
60        $this->nsInfo = $nsInfo;
61        $this->userOptionsLookup = $userOptionsLookup;
62    }
63
64    /**
65     * Get the default gender option on this wiki.
66     *
67     * @return string
68     */
69    protected function getDefault() {
70        $this->default ??= $this->userOptionsLookup->getDefaultOption( 'gender' );
71        return $this->default;
72    }
73
74    /**
75     * Get the gender option for given username.
76     *
77     * @param string|UserIdentity $username
78     * @param string|null $caller Calling method for database profiling
79     * @return string
80     */
81    public function getGenderOf( $username, $caller = '' ) {
82        if ( $username instanceof UserIdentity ) {
83            $username = $username->getName();
84        }
85
86        $username = self::normalizeUsername( $username );
87        if ( !isset( $this->cache[$username] ) ) {
88            if ( $this->misses < $this->missLimit ||
89                RequestContext::getMain()->getUser()->getName() === $username
90            ) {
91                $this->misses++;
92                $this->doQuery( $username, $caller );
93            }
94            if ( $this->misses === $this->missLimit ) {
95                // Log only once and don't bother incrementing beyond limit+1
96                $this->misses++;
97                wfDebug( __METHOD__ . ': too many misses, returning default onwards' );
98            }
99        }
100
101        return $this->cache[$username] ?? $this->getDefault();
102    }
103
104    /**
105     * Wrapper for doQuery that processes raw LinkBatch data.
106     *
107     * @param array<int,array<string,mixed>> $data
108     * @param string|null $caller
109     */
110    public function doLinkBatch( array $data, $caller = '' ) {
111        $users = [];
112        foreach ( $data as $ns => $pagenames ) {
113            if ( $this->nsInfo->hasGenderDistinction( $ns ) ) {
114                $users += $pagenames;
115            }
116        }
117        $this->doQuery( array_keys( $users ), $caller );
118    }
119
120    /**
121     * Wrapper for doQuery that processes a title array.
122     *
123     * @since 1.20
124     * @param LinkTarget[] $titles
125     * @param string|null $caller Calling method for database profiling
126     */
127    public function doTitlesArray( $titles, $caller = '' ) {
128        $users = [];
129        foreach ( $titles as $titleObj ) {
130            if ( $this->nsInfo->hasGenderDistinction( $titleObj->getNamespace() ) ) {
131                $users[] = $titleObj->getText();
132            }
133        }
134        $this->doQuery( $users, $caller );
135    }
136
137    /**
138     * Preload gender option for multiple user names.
139     *
140     * @param string[]|string $users Usernames
141     * @param string|null $caller Calling method for database profiling
142     */
143    public function doQuery( $users, $caller = '' ) {
144        $usersToFetch = [];
145        foreach ( (array)$users as $userName ) {
146            $userName = self::normalizeUsername( $userName );
147            if ( !isset( $this->cache[$userName] ) ) {
148                $usersToFetch[] = $userName;
149            }
150        }
151        if ( !$usersToFetch ) {
152            return;
153        }
154
155        $genders = $this->userOptionsLookup->getOptionBatchForUserNames( $usersToFetch, 'gender' );
156        foreach ( $genders as $userName => $gender ) {
157            $this->cache[$userName] = $gender;
158        }
159    }
160
161    private static function normalizeUsername( string $username ): string {
162        // Strip off subpages
163        $indexSlash = strpos( $username, '/' );
164        if ( $indexSlash !== false ) {
165            $username = substr( $username, 0, $indexSlash );
166        }
167
168        // normalize underscore/spaces
169        return strtr( $username, '_', ' ' );
170    }
171}
172
173/** @deprecated class alias since 1.42 */
174class_alias( GenderCache::class, 'GenderCache' );