2011-02-12 20:40:40 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
2011-02-12 21:05:27 +00:00
|
|
|
* Caches user genders when needed to use correct namespace aliases.
|
2012-05-08 12:51:21 +00:00
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
2011-02-12 20:40:40 +00:00
|
|
|
* @author Niklas Laxström
|
2012-05-08 12:51:21 +00:00
|
|
|
* @ingroup Cache
|
|
|
|
|
*/
|
2016-05-07 12:15:44 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2012-05-08 12:51:21 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Caches user genders when needed to use correct namespace aliases.
|
|
|
|
|
*
|
2011-02-12 20:40:40 +00:00
|
|
|
* @since 1.18
|
|
|
|
|
*/
|
|
|
|
|
class GenderCache {
|
2016-02-17 09:09:32 +00:00
|
|
|
protected $cache = [];
|
2011-02-12 20:40:40 +00:00
|
|
|
protected $default;
|
|
|
|
|
protected $misses = 0;
|
|
|
|
|
protected $missLimit = 1000;
|
2011-05-26 20:26:51 +00:00
|
|
|
|
|
|
|
|
/**
|
2016-05-07 12:15:44 +00:00
|
|
|
* @deprecated in 1.28 see MediaWikiServices::getInstance()->getGenderCache()
|
2011-05-26 20:26:51 +00:00
|
|
|
* @return GenderCache
|
|
|
|
|
*/
|
2011-02-12 20:40:40 +00:00
|
|
|
public static function singleton() {
|
2016-05-07 12:15:44 +00:00
|
|
|
return MediaWikiServices::getInstance()->getGenderCache();
|
2013-11-17 20:36:27 +00:00
|
|
|
}
|
2011-02-12 20:40:40 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the default gender option in this wiki.
|
2014-04-18 23:19:46 +00:00
|
|
|
* @return string
|
2011-02-12 20:40:40 +00:00
|
|
|
*/
|
|
|
|
|
protected function getDefault() {
|
|
|
|
|
if ( $this->default === null ) {
|
|
|
|
|
$this->default = User::getDefaultOption( 'gender' );
|
|
|
|
|
}
|
2013-11-17 20:36:27 +00:00
|
|
|
|
2011-02-12 20:40:40 +00:00
|
|
|
return $this->default;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the gender for given username.
|
2014-04-18 23:19:46 +00:00
|
|
|
* @param string|User $username Username
|
|
|
|
|
* @param string $caller The calling method
|
|
|
|
|
* @return string
|
2011-02-12 20:40:40 +00:00
|
|
|
*/
|
|
|
|
|
public function getGenderOf( $username, $caller = '' ) {
|
|
|
|
|
global $wgUser;
|
|
|
|
|
|
2013-04-20 17:18:13 +00:00
|
|
|
if ( $username instanceof User ) {
|
2012-04-07 20:45:31 +00:00
|
|
|
$username = $username->getName();
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-21 08:32:09 +00:00
|
|
|
$username = self::normalizeUsername( $username );
|
2011-02-12 20:40:40 +00:00
|
|
|
if ( !isset( $this->cache[$username] ) ) {
|
|
|
|
|
if ( $this->misses >= $this->missLimit && $wgUser->getName() !== $username ) {
|
2013-04-20 17:18:13 +00:00
|
|
|
if ( $this->misses === $this->missLimit ) {
|
2011-02-12 20:40:40 +00:00
|
|
|
$this->misses++;
|
|
|
|
|
wfDebug( __METHOD__ . ": too many misses, returning default onwards\n" );
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-17 20:36:27 +00:00
|
|
|
return $this->getDefault();
|
2011-02-12 20:40:40 +00:00
|
|
|
} else {
|
|
|
|
|
$this->misses++;
|
2012-04-21 08:32:09 +00:00
|
|
|
$this->doQuery( $username, $caller );
|
2011-02-12 20:40:40 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Undefined if there is a valid username which for some reason doesn't
|
|
|
|
|
* exist in the database.
|
|
|
|
|
*/
|
|
|
|
|
return isset( $this->cache[$username] ) ? $this->cache[$username] : $this->getDefault();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Wrapper for doQuery that processes raw LinkBatch data.
|
2011-05-26 20:26:51 +00:00
|
|
|
*
|
2014-04-18 23:19:46 +00:00
|
|
|
* @param array $data
|
|
|
|
|
* @param string $caller
|
2011-02-12 20:40:40 +00:00
|
|
|
*/
|
|
|
|
|
public function doLinkBatch( $data, $caller = '' ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$users = [];
|
2011-02-12 20:40:40 +00:00
|
|
|
foreach ( $data as $ns => $pagenames ) {
|
2013-04-20 17:18:13 +00:00
|
|
|
if ( !MWNamespace::hasGenderDistinction( $ns ) ) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2011-02-12 20:40:40 +00:00
|
|
|
foreach ( array_keys( $pagenames ) as $username ) {
|
|
|
|
|
$users[$username] = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->doQuery( array_keys( $users ), $caller );
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-16 14:46:22 +00:00
|
|
|
/**
|
|
|
|
|
* Wrapper for doQuery that processes a title or string array.
|
|
|
|
|
*
|
|
|
|
|
* @since 1.20
|
2014-04-18 23:19:46 +00:00
|
|
|
* @param array $titles Array of Title objects or strings
|
|
|
|
|
* @param string $caller The calling method
|
2012-05-16 14:46:22 +00:00
|
|
|
*/
|
|
|
|
|
public function doTitlesArray( $titles, $caller = '' ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$users = [];
|
2012-05-16 14:46:22 +00:00
|
|
|
foreach ( $titles as $title ) {
|
|
|
|
|
$titleObj = is_string( $title ) ? Title::newFromText( $title ) : $title;
|
|
|
|
|
if ( !$titleObj ) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if ( !MWNamespace::hasGenderDistinction( $titleObj->getNamespace() ) ) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$users[] = $titleObj->getText();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->doQuery( $users, $caller );
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-12 20:40:40 +00:00
|
|
|
/**
|
|
|
|
|
* Preloads genders for given list of users.
|
2014-04-18 23:19:46 +00:00
|
|
|
* @param array|string $users Usernames
|
|
|
|
|
* @param string $caller The calling method
|
2011-02-12 20:40:40 +00:00
|
|
|
*/
|
|
|
|
|
public function doQuery( $users, $caller = '' ) {
|
2011-09-19 09:04:39 +00:00
|
|
|
$default = $this->getDefault();
|
2011-02-12 20:40:40 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$usersToCheck = [];
|
2013-08-31 16:36:02 +00:00
|
|
|
foreach ( (array)$users as $value ) {
|
2012-04-21 08:32:09 +00:00
|
|
|
$name = self::normalizeUsername( $value );
|
|
|
|
|
// Skip users whose gender setting we already know
|
|
|
|
|
if ( !isset( $this->cache[$name] ) ) {
|
2011-09-19 09:04:39 +00:00
|
|
|
// For existing users, this value will be overwritten by the correct value
|
|
|
|
|
$this->cache[$name] = $default;
|
2012-04-21 08:32:09 +00:00
|
|
|
// query only for valid names, which can be in the database
|
2013-04-20 17:18:13 +00:00
|
|
|
if ( User::isValidUserName( $name ) ) {
|
2012-04-21 08:32:09 +00:00
|
|
|
$usersToCheck[] = $name;
|
|
|
|
|
}
|
2011-09-19 09:04:39 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-21 08:32:09 +00:00
|
|
|
if ( count( $usersToCheck ) === 0 ) {
|
2011-10-26 03:44:47 +00:00
|
|
|
return;
|
2011-02-12 20:40:40 +00:00
|
|
|
}
|
|
|
|
|
|
2016-09-05 19:55:19 +00:00
|
|
|
$dbr = wfGetDB( DB_REPLICA );
|
2016-02-17 09:09:32 +00:00
|
|
|
$table = [ 'user', 'user_properties' ];
|
|
|
|
|
$fields = [ 'user_name', 'up_value' ];
|
|
|
|
|
$conds = [ 'user_name' => $usersToCheck ];
|
|
|
|
|
$joins = [ 'user_properties' =>
|
|
|
|
|
[ 'LEFT JOIN', [ 'user_id = up_user', 'up_property' => 'gender' ] ] ];
|
2011-02-12 20:40:40 +00:00
|
|
|
|
|
|
|
|
$comment = __METHOD__;
|
|
|
|
|
if ( strval( $caller ) !== '' ) {
|
|
|
|
|
$comment .= "/$caller";
|
|
|
|
|
}
|
2016-02-17 09:09:32 +00:00
|
|
|
$res = $dbr->select( $table, $fields, $conds, $comment, [], $joins );
|
2011-02-12 20:40:40 +00:00
|
|
|
|
|
|
|
|
foreach ( $res as $row ) {
|
|
|
|
|
$this->cache[$row->user_name] = $row->up_value ? $row->up_value : $default;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-21 08:32:09 +00:00
|
|
|
private static function normalizeUsername( $username ) {
|
|
|
|
|
// Strip off subpages
|
|
|
|
|
$indexSlash = strpos( $username, '/' );
|
|
|
|
|
if ( $indexSlash !== false ) {
|
|
|
|
|
$username = substr( $username, 0, $indexSlash );
|
|
|
|
|
}
|
2013-11-17 20:36:27 +00:00
|
|
|
|
2012-04-21 08:32:09 +00:00
|
|
|
// normalize underscore/spaces
|
|
|
|
|
return strtr( $username, '_', ' ' );
|
|
|
|
|
}
|
2011-02-12 20:40:40 +00:00
|
|
|
}
|