Apparently we can commit code that doesn't compile but I am not allowed to have commi...
[mediawiki.git] / includes / cache / GenderCache.php
blob342f8dba837adb4db36a61963beba1fde37add4e
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 /**
15 * @return GenderCache
17 public static function singleton() {
18 static $that = null;
19 if ( $that === null ) {
20 $that = new self();
22 return $that;
25 protected function __construct() {}
27 /**
28 * Returns the default gender option in this wiki.
29 * @return String
31 protected function getDefault() {
32 if ( $this->default === null ) {
33 $this->default = User::getDefaultOption( 'gender' );
35 return $this->default;
38 /**
39 * Returns the gender for given username.
40 * @param $username String: username
41 * @param $caller String: the calling method
42 * @return String
44 public function getGenderOf( $username, $caller = '' ) {
45 global $wgUser;
47 $username = strtr( $username, '_', ' ' );
48 if ( !isset( $this->cache[$username] ) ) {
50 if ( $this->misses >= $this->missLimit && $wgUser->getName() !== $username ) {
51 if( $this->misses === $this->missLimit ) {
52 $this->misses++;
53 wfDebug( __METHOD__ . ": too many misses, returning default onwards\n" );
55 return $this->getDefault();
57 } else {
58 $this->misses++;
59 if ( !User::isValidUserName( $username ) ) {
60 $this->cache[$username] = $this->getDefault();
61 } else {
62 $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();
74 /**
75 * Wrapper for doQuery that processes raw LinkBatch data.
77 * @param $data
78 * @param $caller
80 public function doLinkBatch( $data, $caller = '' ) {
81 $users = array();
82 foreach ( $data as $ns => $pagenames ) {
83 if ( !MWNamespace::hasGenderDistinction( $ns ) ) continue;
84 foreach ( array_keys( $pagenames ) as $username ) {
85 if ( isset( $this->cache[$username] ) ) continue;
86 $users[$username] = true;
90 $this->doQuery( array_keys( $users ), $caller );
93 /**
94 * Preloads genders for given list of users.
95 * @param $users List|String: usernames
96 * @param $caller String: the calling method
98 public function doQuery( $users, $caller = '' ) {
99 $default = $this->getDefault();
101 foreach ( (array) $users as $index => $value ) {
102 $name = strtr( $value, '_', ' ' );
103 if ( isset( $this->cache[$name] ) ) {
104 // Skip users whose gender setting we already know
105 unset( $users[$index] );
106 } else {
107 $users[$index] = $name;
108 // For existing users, this value will be overwritten by the correct value
109 $this->cache[$name] = $default;
113 if ( count( $users ) === 0 ) {
114 return;
117 $dbr = wfGetDB( DB_SLAVE );
118 $table = array( 'user', 'user_properties' );
119 $fields = array( 'user_name', 'up_value' );
120 $conds = array( 'user_name' => $users );
121 $joins = array( 'user_properties' =>
122 array( 'LEFT JOIN', array( 'user_id = up_user', 'up_property' => 'gender' ) ) );
124 $comment = __METHOD__;
125 if ( strval( $caller ) !== '' ) {
126 $comment .= "/$caller";
128 $res = $dbr->select( $table, $fields, $conds, $comment, $joins, $joins );
130 foreach ( $res as $row ) {
131 $this->cache[$row->user_name] = $row->up_value ? $row->up_value : $default;