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
21 * @author Niklas Laxström
26 * Caches user genders when needed to use correct namespace aliases.
31 protected $cache = array();
33 protected $misses = 0;
34 protected $missLimit = 1000;
39 public static function singleton() {
41 if ( $that === null ) {
47 protected function __construct() {}
50 * Returns the default gender option in this wiki.
53 protected function getDefault() {
54 if ( $this->default === null ) {
55 $this->default = User
::getDefaultOption( 'gender' );
57 return $this->default;
61 * Returns the gender for given username.
62 * @param string $username or User: username
63 * @param string $caller the calling method
66 public function getGenderOf( $username, $caller = '' ) {
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
) {
78 wfDebug( __METHOD__
. ": too many misses, returning default onwards\n" );
80 return $this->getDefault();
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();
95 * Wrapper for doQuery that processes raw LinkBatch data.
100 public function doLinkBatch( $data, $caller = '' ) {
102 foreach ( $data as $ns => $pagenames ) {
103 if ( !MWNamespace
::hasGenderDistinction( $ns ) ) {
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.
118 * @param $titles List: array of Title objects or strings
119 * @param string $caller the calling method
121 public function doTitlesArray( $titles, $caller = '' ) {
123 foreach ( $titles as $title ) {
124 $titleObj = is_string( $title ) ? Title
::newFromText( $title ) : $title;
128 if ( !MWNamespace
::hasGenderDistinction( $titleObj->getNamespace() ) ) {
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 ) {
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, '_', ' ' );