Follow-up r84814: revert redundant summary message addition.
[lhc/web/wiklou.git] / includes / GenderCache.php
1 <?php
2
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;
13
14 public static function singleton() {
15 static $that = null;
16 if ( $that === null ) {
17 $that = new self();
18 }
19 return $that;
20 }
21
22 protected function __construct() {}
23
24 /**
25 * Returns the default gender option in this wiki.
26 * @return String
27 */
28 protected function getDefault() {
29 if ( $this->default === null ) {
30 $this->default = User::getDefaultOption( 'gender' );
31 }
32 return $this->default;
33 }
34
35 /**
36 * Returns the gender for given username.
37 * @param $users String: username
38 * @param $caller String: the calling method
39 * @return String
40 */
41 public function getGenderOf( $username, $caller = '' ) {
42 global $wgUser;
43
44 $username = strtr( $username, '_', ' ' );
45 if ( !isset( $this->cache[$username] ) ) {
46
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" );
51 }
52 return $this->getDefault();
53
54 } else {
55 $this->misses++;
56 if ( !User::isValidUserName( $username ) ) {
57 $this->cache[$username] = $this->getDefault();
58 } else {
59 $this->doQuery( $username, $caller );
60 }
61 }
62
63 }
64
65 /* Undefined if there is a valid username which for some reason doesn't
66 * exist in the database.
67 */
68 return isset( $this->cache[$username] ) ? $this->cache[$username] : $this->getDefault();
69 }
70
71 /**
72 * Wrapper for doQuery that processes raw LinkBatch data.
73 */
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;
81 }
82 }
83
84 $this->doQuery( array_keys( $users ), $caller );
85
86 }
87
88 /**
89 * Preloads genders for given list of users.
90 * @param $users List|String: usernames
91 * @param $caller String: the calling method
92 */
93 public function doQuery( $users, $caller = '' ) {
94 if ( count( $users ) === 0 ) return false;
95
96 foreach ( (array) $users as $index => $value ) {
97 $users[$index] = strtr( $value, '_', ' ' );
98 }
99
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' ) ) );
106
107 $comment = __METHOD__;
108 if ( strval( $caller ) !== '' ) {
109 $comment .= "/$caller";
110 }
111 $res = $dbr->select( $table, $fields, $conds, $comment, $joins, $joins );
112
113 $default = $this->getDefault();
114 foreach ( $res as $row ) {
115 $this->cache[$row->user_name] = $row->up_value ? $row->up_value : $default;
116 }
117 }
118
119 }