4 * Caches user genders when needed to use correct namespace aliases.
5 * @author Niklas Laxström
9 protected $cache = array();
11 protected $misses = 0;
12 protected $missLimit = 1000;
17 public static function singleton() {
19 if ( $that === null ) {
25 protected function __construct() {}
28 * Returns the default gender option in this wiki.
31 protected function getDefault() {
32 if ( $this->default === null ) {
33 $this->default = User
::getDefaultOption( 'gender' );
35 return $this->default;
39 * Returns the gender for given username.
40 * @param $username String or User: username
41 * @param $caller String: the calling method
44 public function getGenderOf( $username, $caller = '' ) {
47 if( $username instanceof User
) {
48 $username = $username->getName();
51 $username = self
::normalizeUsername( $username );
52 if ( !isset( $this->cache
[$username] ) ) {
54 if ( $this->misses
>= $this->missLimit
&& $wgUser->getName() !== $username ) {
55 if( $this->misses
=== $this->missLimit
) {
57 wfDebug( __METHOD__
. ": too many misses, returning default onwards\n" );
59 return $this->getDefault();
63 $this->doQuery( $username, $caller );
68 /* Undefined if there is a valid username which for some reason doesn't
69 * exist in the database.
71 return isset( $this->cache
[$username] ) ?
$this->cache
[$username] : $this->getDefault();
75 * Wrapper for doQuery that processes raw LinkBatch data.
80 public function doLinkBatch( $data, $caller = '' ) {
82 foreach ( $data as $ns => $pagenames ) {
83 if ( !MWNamespace
::hasGenderDistinction( $ns ) ) continue;
84 foreach ( array_keys( $pagenames ) as $username ) {
85 $users[$username] = true;
89 $this->doQuery( array_keys( $users ), $caller );
93 * Preloads genders for given list of users.
94 * @param $users List|String: usernames
95 * @param $caller String: the calling method
97 public function doQuery( $users, $caller = '' ) {
98 $default = $this->getDefault();
100 $usersToCheck = array();
101 foreach ( (array) $users as $value ) {
102 $name = self
::normalizeUsername( $value );
103 // Skip users whose gender setting we already know
104 if ( !isset( $this->cache
[$name] ) ) {
105 // For existing users, this value will be overwritten by the correct value
106 $this->cache
[$name] = $default;
107 // query only for valid names, which can be in the database
108 if( User
::isValidUserName( $name ) ) {
109 $usersToCheck[] = $name;
114 if ( count( $usersToCheck ) === 0 ) {
118 $dbr = wfGetDB( DB_SLAVE
);
119 $table = array( 'user', 'user_properties' );
120 $fields = array( 'user_name', 'up_value' );
121 $conds = array( 'user_name' => $usersToCheck );
122 $joins = array( 'user_properties' =>
123 array( 'LEFT JOIN', array( 'user_id = up_user', 'up_property' => 'gender' ) ) );
125 $comment = __METHOD__
;
126 if ( strval( $caller ) !== '' ) {
127 $comment .= "/$caller";
129 $res = $dbr->select( $table, $fields, $conds, $comment, array(), $joins );
131 foreach ( $res as $row ) {
132 $this->cache
[$row->user_name
] = $row->up_value ?
$row->up_value
: $default;
136 private static function normalizeUsername( $username ) {
137 // Strip off subpages
138 $indexSlash = strpos( $username, '/' );
139 if ( $indexSlash !== false ) {
140 $username = substr( $username, 0, $indexSlash );
142 // normalize underscore/spaces
143 return strtr( $username, '_', ' ' );