Fixed spacing in actions/cache/filebackend/filerepo/job folder
[mediawiki.git] / includes / cache / GenderCache.php
blobb7d1b26116fe63597b10a50b3c033cea60ac3997
1 <?php
2 /**
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
20 * @file
21 * @author Niklas Laxström
22 * @ingroup Cache
25 /**
26 * Caches user genders when needed to use correct namespace aliases.
28 * @since 1.18
30 class GenderCache {
31 protected $cache = array();
32 protected $default;
33 protected $misses = 0;
34 protected $missLimit = 1000;
36 /**
37 * @return GenderCache
39 public static function singleton() {
40 static $that = null;
41 if ( $that === null ) {
42 $that = new self();
44 return $that;
47 protected function __construct() {}
49 /**
50 * Returns the default gender option in this wiki.
51 * @return String
53 protected function getDefault() {
54 if ( $this->default === null ) {
55 $this->default = User::getDefaultOption( 'gender' );
57 return $this->default;
60 /**
61 * Returns the gender for given username.
62 * @param string $username or User: username
63 * @param string $caller the calling method
64 * @return String
66 public function getGenderOf( $username, $caller = '' ) {
67 global $wgUser;
69 if ( $username instanceof User ) {
70 $username = $username->getName();
73 $username = self::normalizeUsername( $username );
74 if ( !isset( $this->cache[$username] ) ) {
75 if ( $this->misses >= $this->missLimit && $wgUser->getName() !== $username ) {
76 if ( $this->misses === $this->missLimit ) {
77 $this->misses++;
78 wfDebug( __METHOD__ . ": too many misses, returning default onwards\n" );
80 return $this->getDefault();
82 } else {
83 $this->misses++;
84 $this->doQuery( $username, $caller );
88 /* Undefined if there is a valid username which for some reason doesn't
89 * exist in the database.
91 return isset( $this->cache[$username] ) ? $this->cache[$username] : $this->getDefault();
94 /**
95 * Wrapper for doQuery that processes raw LinkBatch data.
97 * @param $data
98 * @param $caller
100 public function doLinkBatch( $data, $caller = '' ) {
101 $users = array();
102 foreach ( $data as $ns => $pagenames ) {
103 if ( !MWNamespace::hasGenderDistinction( $ns ) ) {
104 continue;
106 foreach ( array_keys( $pagenames ) as $username ) {
107 $users[$username] = true;
111 $this->doQuery( array_keys( $users ), $caller );
115 * Wrapper for doQuery that processes a title or string array.
117 * @since 1.20
118 * @param $titles List: array of Title objects or strings
119 * @param string $caller the calling method
121 public function doTitlesArray( $titles, $caller = '' ) {
122 $users = array();
123 foreach ( $titles as $title ) {
124 $titleObj = is_string( $title ) ? Title::newFromText( $title ) : $title;
125 if ( !$titleObj ) {
126 continue;
128 if ( !MWNamespace::hasGenderDistinction( $titleObj->getNamespace() ) ) {
129 continue;
131 $users[] = $titleObj->getText();
134 $this->doQuery( $users, $caller );
138 * Preloads genders for given list of users.
139 * @param $users List|String: usernames
140 * @param string $caller the calling method
142 public function doQuery( $users, $caller = '' ) {
143 $default = $this->getDefault();
145 $usersToCheck = array();
146 foreach ( (array) $users as $value ) {
147 $name = self::normalizeUsername( $value );
148 // Skip users whose gender setting we already know
149 if ( !isset( $this->cache[$name] ) ) {
150 // For existing users, this value will be overwritten by the correct value
151 $this->cache[$name] = $default;
152 // query only for valid names, which can be in the database
153 if ( User::isValidUserName( $name ) ) {
154 $usersToCheck[] = $name;
159 if ( count( $usersToCheck ) === 0 ) {
160 return;
163 $dbr = wfGetDB( DB_SLAVE );
164 $table = array( 'user', 'user_properties' );
165 $fields = array( 'user_name', 'up_value' );
166 $conds = array( 'user_name' => $usersToCheck );
167 $joins = array( 'user_properties' =>
168 array( 'LEFT JOIN', array( 'user_id = up_user', 'up_property' => 'gender' ) ) );
170 $comment = __METHOD__;
171 if ( strval( $caller ) !== '' ) {
172 $comment .= "/$caller";
174 $res = $dbr->select( $table, $fields, $conds, $comment, array(), $joins );
176 foreach ( $res as $row ) {
177 $this->cache[$row->user_name] = $row->up_value ? $row->up_value : $default;
181 private static function normalizeUsername( $username ) {
182 // Strip off subpages
183 $indexSlash = strpos( $username, '/' );
184 if ( $indexSlash !== false ) {
185 $username = substr( $username, 0, $indexSlash );
187 // normalize underscore/spaces
188 return strtr( $username, '_', ' ' );