Merge "Added new MWTimestamp::getRelativeTimestamp for pure relative."
[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
25 /**
26 * Caches user genders when needed to use correct namespace aliases.
27 *
28 * @since 1.18
29 */
30 class GenderCache {
31 protected $cache = array();
32 protected $default;
33 protected $misses = 0;
34 protected $missLimit = 1000;
35
36 /**
37 * @return GenderCache
38 */
39 public static function singleton() {
40 static $that = null;
41 if ( $that === null ) {
42 $that = new self();
43 }
44 return $that;
45 }
46
47 protected function __construct() {}
48
49 /**
50 * Returns the default gender option in this wiki.
51 * @return String
52 */
53 protected function getDefault() {
54 if ( $this->default === null ) {
55 $this->default = User::getDefaultOption( 'gender' );
56 }
57 return $this->default;
58 }
59
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
65 */
66 public function getGenderOf( $username, $caller = '' ) {
67 global $wgUser;
68
69 if ( $username instanceof User ) {
70 $username = $username->getName();
71 }
72
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" );
79 }
80 return $this->getDefault();
81
82 } else {
83 $this->misses++;
84 $this->doQuery( $username, $caller );
85 }
86 }
87
88 /* Undefined if there is a valid username which for some reason doesn't
89 * exist in the database.
90 */
91 return isset( $this->cache[$username] ) ? $this->cache[$username] : $this->getDefault();
92 }
93
94 /**
95 * Wrapper for doQuery that processes raw LinkBatch data.
96 *
97 * @param $data
98 * @param $caller
99 */
100 public function doLinkBatch( $data, $caller = '' ) {
101 $users = array();
102 foreach ( $data as $ns => $pagenames ) {
103 if ( !MWNamespace::hasGenderDistinction( $ns ) ) {
104 continue;
105 }
106 foreach ( array_keys( $pagenames ) as $username ) {
107 $users[$username] = true;
108 }
109 }
110
111 $this->doQuery( array_keys( $users ), $caller );
112 }
113
114 /**
115 * Wrapper for doQuery that processes a title or string array.
116 *
117 * @since 1.20
118 * @param $titles List: array of Title objects or strings
119 * @param string $caller the calling method
120 */
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;
127 }
128 if ( !MWNamespace::hasGenderDistinction( $titleObj->getNamespace() ) ) {
129 continue;
130 }
131 $users[] = $titleObj->getText();
132 }
133
134 $this->doQuery( $users, $caller );
135 }
136
137 /**
138 * Preloads genders for given list of users.
139 * @param $users List|String: usernames
140 * @param string $caller the calling method
141 */
142 public function doQuery( $users, $caller = '' ) {
143 $default = $this->getDefault();
144
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;
155 }
156 }
157 }
158
159 if ( count( $usersToCheck ) === 0 ) {
160 return;
161 }
162
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' ) ) );
169
170 $comment = __METHOD__;
171 if ( strval( $caller ) !== '' ) {
172 $comment .= "/$caller";
173 }
174 $res = $dbr->select( $table, $fields, $conds, $comment, array(), $joins );
175
176 foreach ( $res as $row ) {
177 $this->cache[$row->user_name] = $row->up_value ? $row->up_value : $default;
178 }
179 }
180
181 private static function normalizeUsername( $username ) {
182 // Strip off subpages
183 $indexSlash = strpos( $username, '/' );
184 if ( $indexSlash !== false ) {
185 $username = substr( $username, 0, $indexSlash );
186 }
187 // normalize underscore/spaces
188 return strtr( $username, '_', ' ' );
189 }
190 }