Merge "(bug 32537) Pre-register default-loaded RL modules in the client-side loader."
[lhc/web/wiklou.git] / includes / cache / 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 /**
15 * @return GenderCache
16 */
17 public static function singleton() {
18 static $that = null;
19 if ( $that === null ) {
20 $that = new self();
21 }
22 return $that;
23 }
24
25 protected function __construct() {}
26
27 /**
28 * Returns the default gender option in this wiki.
29 * @return String
30 */
31 protected function getDefault() {
32 if ( $this->default === null ) {
33 $this->default = User::getDefaultOption( 'gender' );
34 }
35 return $this->default;
36 }
37
38 /**
39 * Returns the gender for given username.
40 * @param $username String or User: username
41 * @param $caller String: the calling method
42 * @return String
43 */
44 public function getGenderOf( $username, $caller = '' ) {
45 global $wgUser;
46
47 if( $username instanceof User ) {
48 $username = $username->getName();
49 }
50
51 $username = strtr( $username, '_', ' ' );
52 if ( !isset( $this->cache[$username] ) ) {
53
54 if ( $this->misses >= $this->missLimit && $wgUser->getName() !== $username ) {
55 if( $this->misses === $this->missLimit ) {
56 $this->misses++;
57 wfDebug( __METHOD__ . ": too many misses, returning default onwards\n" );
58 }
59 return $this->getDefault();
60
61 } else {
62 $this->misses++;
63 if ( !User::isValidUserName( $username ) ) {
64 $this->cache[$username] = $this->getDefault();
65 } else {
66 $this->doQuery( $username, $caller );
67 }
68 }
69
70 }
71
72 /* Undefined if there is a valid username which for some reason doesn't
73 * exist in the database.
74 */
75 return isset( $this->cache[$username] ) ? $this->cache[$username] : $this->getDefault();
76 }
77
78 /**
79 * Wrapper for doQuery that processes raw LinkBatch data.
80 *
81 * @param $data
82 * @param $caller
83 */
84 public function doLinkBatch( $data, $caller = '' ) {
85 $users = array();
86 foreach ( $data as $ns => $pagenames ) {
87 if ( !MWNamespace::hasGenderDistinction( $ns ) ) continue;
88 foreach ( array_keys( $pagenames ) as $username ) {
89 if ( isset( $this->cache[$username] ) ) continue;
90 $users[$username] = true;
91 }
92 }
93
94 $this->doQuery( array_keys( $users ), $caller );
95 }
96
97 /**
98 * Preloads genders for given list of users.
99 * @param $users List|String: usernames
100 * @param $caller String: the calling method
101 */
102 public function doQuery( $users, $caller = '' ) {
103 $default = $this->getDefault();
104
105 foreach ( (array) $users as $index => $value ) {
106 $name = strtr( $value, '_', ' ' );
107 if ( isset( $this->cache[$name] ) ) {
108 // Skip users whose gender setting we already know
109 unset( $users[$index] );
110 } else {
111 $users[$index] = $name;
112 // For existing users, this value will be overwritten by the correct value
113 $this->cache[$name] = $default;
114 }
115 }
116
117 if ( count( $users ) === 0 ) {
118 return;
119 }
120
121 $dbr = wfGetDB( DB_SLAVE );
122 $table = array( 'user', 'user_properties' );
123 $fields = array( 'user_name', 'up_value' );
124 $conds = array( 'user_name' => $users );
125 $joins = array( 'user_properties' =>
126 array( 'LEFT JOIN', array( 'user_id = up_user', 'up_property' => 'gender' ) ) );
127
128 $comment = __METHOD__;
129 if ( strval( $caller ) !== '' ) {
130 $comment .= "/$caller";
131 }
132 $res = $dbr->select( $table, $fields, $conds, $comment, $joins, $joins );
133
134 foreach ( $res as $row ) {
135 $this->cache[$row->user_name] = $row->up_value ? $row->up_value : $default;
136 }
137 }
138
139 }