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 ) {
48 protected function __construct() {
52 * Returns the default gender option in this wiki.
55 protected function getDefault() {
56 if ( $this->default === null ) {
57 $this->default = User
::getDefaultOption( 'gender' );
60 return $this->default;
64 * Returns the gender for given username.
65 * @param string|User $username Username
66 * @param string $caller The calling method
69 public function getGenderOf( $username, $caller = '' ) {
72 if ( $username instanceof User
) {
73 $username = $username->getName();
76 $username = self
::normalizeUsername( $username );
77 if ( !isset( $this->cache
[$username] ) ) {
78 if ( $this->misses
>= $this->missLimit
&& $wgUser->getName() !== $username ) {
79 if ( $this->misses
=== $this->missLimit
) {
81 wfDebug( __METHOD__
. ": too many misses, returning default onwards\n" );
84 return $this->getDefault();
87 $this->doQuery( $username, $caller );
91 /* Undefined if there is a valid username which for some reason doesn't
92 * exist in the database.
94 return isset( $this->cache
[$username] ) ?
$this->cache
[$username] : $this->getDefault();
98 * Wrapper for doQuery that processes raw LinkBatch data.
101 * @param string $caller
103 public function doLinkBatch( $data, $caller = '' ) {
105 foreach ( $data as $ns => $pagenames ) {
106 if ( !MWNamespace
::hasGenderDistinction( $ns ) ) {
109 foreach ( array_keys( $pagenames ) as $username ) {
110 $users[$username] = true;
114 $this->doQuery( array_keys( $users ), $caller );
118 * Wrapper for doQuery that processes a title or string array.
121 * @param array $titles Array of Title objects or strings
122 * @param string $caller The calling method
124 public function doTitlesArray( $titles, $caller = '' ) {
126 foreach ( $titles as $title ) {
127 $titleObj = is_string( $title ) ? Title
::newFromText( $title ) : $title;
131 if ( !MWNamespace
::hasGenderDistinction( $titleObj->getNamespace() ) ) {
134 $users[] = $titleObj->getText();
137 $this->doQuery( $users, $caller );
141 * Preloads genders for given list of users.
142 * @param array|string $users Usernames
143 * @param string $caller The calling method
145 public function doQuery( $users, $caller = '' ) {
146 $default = $this->getDefault();
148 $usersToCheck = array();
149 foreach ( (array)$users as $value ) {
150 $name = self
::normalizeUsername( $value );
151 // Skip users whose gender setting we already know
152 if ( !isset( $this->cache
[$name] ) ) {
153 // For existing users, this value will be overwritten by the correct value
154 $this->cache
[$name] = $default;
155 // query only for valid names, which can be in the database
156 if ( User
::isValidUserName( $name ) ) {
157 $usersToCheck[] = $name;
162 if ( count( $usersToCheck ) === 0 ) {
166 $dbr = wfGetDB( DB_SLAVE
);
167 $table = array( 'user', 'user_properties' );
168 $fields = array( 'user_name', 'up_value' );
169 $conds = array( 'user_name' => $usersToCheck );
170 $joins = array( 'user_properties' =>
171 array( 'LEFT JOIN', array( 'user_id = up_user', 'up_property' => 'gender' ) ) );
173 $comment = __METHOD__
;
174 if ( strval( $caller ) !== '' ) {
175 $comment .= "/$caller";
177 $res = $dbr->select( $table, $fields, $conds, $comment, array(), $joins );
179 foreach ( $res as $row ) {
180 $this->cache
[$row->user_name
] = $row->up_value ?
$row->up_value
: $default;
184 private static function normalizeUsername( $username ) {
185 // Strip off subpages
186 $indexSlash = strpos( $username, '/' );
187 if ( $indexSlash !== false ) {
188 $username = substr( $username, 0, $indexSlash );
191 // normalize underscore/spaces
192 return strtr( $username, '_', ' ' );