* (bug 28444) Fix regression: edit-on-doubleclick retains revision id again
[mediawiki.git] / includes / GenderCache.php
blobf7537a2e843dadaa2abc7140319bfeeb625e7c01
1 <?php
3 /**
4 * Caches user genders when needed to use correct namespace aliases.
5 * @author Niklas Laxström
6 * @since 1.18
7 */
8 class GenderCache {
9 protected $cache = array();
10 protected $default;
11 protected $misses = 0;
12 protected $missLimit = 1000;
14 public static function singleton() {
15 static $that = null;
16 if ( $that === null ) {
17 $that = new self();
19 return $that;
22 protected function __construct() {}
24 /**
25 * Returns the default gender option in this wiki.
26 * @return String
28 protected function getDefault() {
29 if ( $this->default === null ) {
30 $this->default = User::getDefaultOption( 'gender' );
32 return $this->default;
35 /**
36 * Returns the gender for given username.
37 * @param $users String: username
38 * @param $caller String: the calling method
39 * @return String
41 public function getGenderOf( $username, $caller = '' ) {
42 global $wgUser;
44 $username = strtr( $username, '_', ' ' );
45 if ( !isset( $this->cache[$username] ) ) {
47 if ( $this->misses >= $this->missLimit && $wgUser->getName() !== $username ) {
48 if( $this->misses === $this->missLimit ) {
49 $this->misses++;
50 wfDebug( __METHOD__ . ": too many misses, returning default onwards\n" );
52 return $this->getDefault();
54 } else {
55 $this->misses++;
56 if ( !User::isValidUserName( $username ) ) {
57 $this->cache[$username] = $this->getDefault();
58 } else {
59 $this->doQuery( $username, $caller );
65 /* Undefined if there is a valid username which for some reason doesn't
66 * exist in the database.
68 return isset( $this->cache[$username] ) ? $this->cache[$username] : $this->getDefault();
71 /**
72 * Wrapper for doQuery that processes raw LinkBatch data.
74 public function doLinkBatch( $data, $caller = '' ) {
75 $users = array();
76 foreach ( $data as $ns => $pagenames ) {
77 if ( !MWNamespace::hasGenderDistinction( $ns ) ) continue;
78 foreach ( array_keys( $pagenames ) as $username ) {
79 if ( isset( $this->cache[$username] ) ) continue;
80 $users[$username] = true;
84 $this->doQuery( array_keys( $users ), $caller );
88 /**
89 * Preloads genders for given list of users.
90 * @param $users List|String: usernames
91 * @param $caller String: the calling method
93 public function doQuery( $users, $caller = '' ) {
94 if ( count( $users ) === 0 ) return false;
96 foreach ( (array) $users as $index => $value ) {
97 $users[$index] = strtr( $value, '_', ' ' );
100 $dbr = wfGetDB( DB_SLAVE );
101 $table = array( 'user', 'user_properties' );
102 $fields = array( 'user_name', 'up_value' );
103 $conds = array( 'user_name' => $users );
104 $joins = array( 'user_properties' =>
105 array( 'LEFT JOIN', array( 'user_id = up_user', 'up_property' => 'gender' ) ) );
107 $comment = __METHOD__;
108 if ( strval( $caller ) !== '' ) {
109 $comment .= "/$caller";
111 $res = $dbr->select( $table, $fields, $conds, $comment, $joins, $joins );
113 $default = $this->getDefault();
114 foreach ( $res as $row ) {
115 $this->cache[$row->user_name] = $row->up_value ? $row->up_value : $default;