Merge "allow localization of elements via data-msg-text and data-msg-html"
[lhc/web/wiklou.git] / includes / cache / UserCache.php
1 <?php
2 /**
3 * Caches current user names and other info based on user IDs.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Cache
22 */
23
24 /**
25 * @since 1.20
26 */
27 class UserCache {
28 protected $cache = array(); // (uid => property => value)
29 protected $typesCached = array(); // (uid => cache type => 1)
30
31 /**
32 * @return UserCache
33 */
34 public static function singleton() {
35 static $instance = null;
36 if ( $instance === null ) {
37 $instance = new self();
38 }
39 return $instance;
40 }
41
42 protected function __construct() {}
43
44 /**
45 * Get a property of a user based on their user ID
46 *
47 * @param $userId integer User ID
48 * @param $prop string User property
49 * @return mixed The property or false if the user does not exist
50 */
51 public function getProp( $userId, $prop ) {
52 if ( !isset( $this->cache[$userId][$prop] ) ) {
53 wfDebug( __METHOD__ . ": querying DB for prop '$prop' for user ID '$userId'.\n" );
54 $this->doQuery( array( $userId ) ); // cache miss
55 }
56 return isset( $this->cache[$userId][$prop] )
57 ? $this->cache[$userId][$prop]
58 : false; // user does not exist?
59 }
60
61 /**
62 * Preloads user names for given list of users.
63 * @param $userIds Array List of user IDs
64 * @param $options Array Option flags; include 'userpage' and 'usertalk'
65 * @param $caller String: the calling method
66 */
67 public function doQuery( array $userIds, $options = array(), $caller = '' ) {
68 wfProfileIn( __METHOD__ );
69
70 $usersToCheck = array();
71 $usersToQuery = array();
72
73 foreach ( $userIds as $userId ) {
74 $userId = (int)$userId;
75 if ( $userId <= 0 ) {
76 continue; // skip anons
77 }
78 if ( isset( $this->cache[$userId]['name'] ) ) {
79 $usersToCheck[$userId] = $this->cache[$userId]['name']; // already have name
80 } else {
81 $usersToQuery[] = $userId; // we need to get the name
82 }
83 }
84
85 // Lookup basic info for users not yet loaded...
86 if ( count( $usersToQuery ) ) {
87 $dbr = wfGetDB( DB_SLAVE );
88 $table = array( 'user' );
89 $conds = array( 'user_id' => $usersToQuery );
90 $fields = array( 'user_name', 'user_real_name', 'user_registration', 'user_id' );
91
92 $comment = __METHOD__;
93 if ( strval( $caller ) !== '' ) {
94 $comment .= "/$caller";
95 }
96
97 $res = $dbr->select( $table, $fields, $conds, $comment );
98 foreach ( $res as $row ) { // load each user into cache
99 $userId = (int)$row->user_id;
100 $this->cache[$userId]['name'] = $row->user_name;
101 $this->cache[$userId]['real_name'] = $row->user_real_name;
102 $this->cache[$userId]['registration'] = $row->user_registration;
103 $usersToCheck[$userId] = $row->user_name;
104 }
105 }
106
107 $lb = new LinkBatch();
108 foreach ( $usersToCheck as $userId => $name ) {
109 if ( $this->queryNeeded( $userId, 'userpage', $options ) ) {
110 $lb->add( NS_USER, str_replace( ' ', '_', $row->user_name ) );
111 $this->typesCached[$userId]['userpage'] = 1;
112 }
113 if ( $this->queryNeeded( $userId, 'usertalk', $options ) ) {
114 $lb->add( NS_USER_TALK, str_replace( ' ', '_', $row->user_name ) );
115 $this->typesCached[$userId]['usertalk'] = 1;
116 }
117 }
118 $lb->execute();
119
120 wfProfileOut( __METHOD__ );
121 }
122
123 /**
124 * Check if a cache type is in $options and was not loaded for this user
125 *
126 * @param $uid integer user ID
127 * @param $type string Cache type
128 * @param $options Array Requested cache types
129 * @return bool
130 */
131 protected function queryNeeded( $uid, $type, array $options ) {
132 return ( in_array( $type, $options ) && !isset( $this->typesCached[$uid][$type] ) );
133 }
134 }