Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 55
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
UserCache
0.00% covered (danger)
0.00%
0 / 54
0.00% covered (danger)
0.00%
0 / 6
342
0.00% covered (danger)
0.00%
0 / 1
 singleton
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 getProp
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
6
 getUserName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
 doQuery
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 1
110
 queryNeeded
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
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
24namespace MediaWiki\Cache;
25
26use MediaWiki\MediaWikiServices;
27use Psr\Log\LoggerInterface;
28use Wikimedia\Rdbms\IConnectionProvider;
29
30/**
31 * @since 1.20
32 * @deprecated since 1.43, use ActorStore
33 */
34class UserCache {
35    protected $cache = []; // (uid => property => value)
36    protected $typesCached = []; // (uid => cache type => 1)
37
38    /** @var LoggerInterface */
39    private $logger;
40
41    /** @var LinkBatchFactory */
42    private $linkBatchFactory;
43
44    /** @var IConnectionProvider */
45    private $dbProvider;
46
47    /**
48     * @deprecated since 1.43, use MediaWikiServices::getInstance()->getUserCache()
49     * @return UserCache
50     */
51    public static function singleton() {
52        wfDeprecated( __METHOD__, '1.43' );
53        return MediaWikiServices::getInstance()->getUserCache();
54    }
55
56    /**
57     * Uses dependency injection since 1.36
58     *
59     * @param LoggerInterface $logger
60     * @param IConnectionProvider $dbProvider
61     * @param LinkBatchFactory $linkBatchFactory
62     */
63    public function __construct(
64        LoggerInterface $logger,
65        IConnectionProvider $dbProvider,
66        LinkBatchFactory $linkBatchFactory
67    ) {
68        $this->logger = $logger;
69        $this->dbProvider = $dbProvider;
70        $this->linkBatchFactory = $linkBatchFactory;
71    }
72
73    /**
74     * Get a property of a user based on their user ID
75     *
76     * @param int $userId
77     * @param string $prop User property
78     * @return mixed|false The property or false if the user does not exist
79     */
80    public function getProp( $userId, $prop ) {
81        if ( !isset( $this->cache[$userId][$prop] ) ) {
82            $this->logger->debug(
83                'Querying DB for prop {prop} for user ID {userId}',
84                [
85                    'prop' => $prop,
86                    'userId' => $userId,
87                ]
88            );
89            $this->doQuery( [ $userId ] ); // cache miss
90        }
91
92        return $this->cache[$userId][$prop] ?? false; // user does not exist?
93    }
94
95    /**
96     * Get the name of a user or return $ip if the user ID is 0
97     *
98     * @param int $userId
99     * @param string $ip
100     * @return string
101     * @since 1.22
102     */
103    public function getUserName( $userId, $ip ) {
104        return $userId > 0 ? $this->getProp( $userId, 'name' ) : $ip;
105    }
106
107    /**
108     * Preloads user names for given list of users.
109     * @param array $userIds List of user IDs
110     * @param array $options Option flags; include 'userpage' and 'usertalk'
111     * @param string $caller The calling method
112     */
113    public function doQuery( array $userIds, $options = [], $caller = '' ) {
114        $usersToCheck = [];
115        $usersToQuery = [];
116
117        $userIds = array_unique( $userIds );
118
119        foreach ( $userIds as $userId ) {
120            $userId = (int)$userId;
121            if ( $userId <= 0 ) {
122                continue; // skip anons
123            }
124            if ( isset( $this->cache[$userId]['name'] ) ) {
125                $usersToCheck[$userId] = $this->cache[$userId]['name']; // already have name
126            } else {
127                $usersToQuery[] = $userId; // we need to get the name
128            }
129        }
130
131        // Lookup basic info for users not yet loaded...
132        if ( count( $usersToQuery ) ) {
133            $dbr = $this->dbProvider->getReplicaDatabase();
134            $queryBuilder = $dbr->newSelectQueryBuilder()
135                ->select( [ 'user_name', 'user_real_name', 'user_registration', 'user_id', 'actor_id' ] )
136                ->from( 'user' )
137                ->join( 'actor', null, 'actor_user = user_id' )
138                ->where( [ 'user_id' => $usersToQuery ] );
139
140            $comment = __METHOD__;
141            if ( strval( $caller ) !== '' ) {
142                $comment .= "/$caller";
143            }
144
145            $res = $queryBuilder->caller( $comment )->fetchResultSet();
146            foreach ( $res as $row ) { // load each user into cache
147                $userId = (int)$row->user_id;
148                $this->cache[$userId]['name'] = $row->user_name;
149                $this->cache[$userId]['real_name'] = $row->user_real_name;
150                $this->cache[$userId]['registration'] = $row->user_registration;
151                $this->cache[$userId]['actor'] = $row->actor_id;
152                $usersToCheck[$userId] = $row->user_name;
153            }
154        }
155
156        $lb = $this->linkBatchFactory->newLinkBatch();
157        foreach ( $usersToCheck as $userId => $name ) {
158            if ( $this->queryNeeded( $userId, 'userpage', $options ) ) {
159                $lb->add( NS_USER, $name );
160                $this->typesCached[$userId]['userpage'] = 1;
161            }
162            if ( $this->queryNeeded( $userId, 'usertalk', $options ) ) {
163                $lb->add( NS_USER_TALK, $name );
164                $this->typesCached[$userId]['usertalk'] = 1;
165            }
166        }
167        $lb->execute();
168    }
169
170    /**
171     * Check if a cache type is in $options and was not loaded for this user
172     *
173     * @param int $uid User ID
174     * @param string $type Cache type
175     * @param array $options Requested cache types
176     * @return bool
177     */
178    protected function queryNeeded( $uid, $type, array $options ) {
179        return ( in_array( $type, $options ) && !isset( $this->typesCached[$uid][$type] ) );
180    }
181}
182
183/** @deprecated class alias since 1.42 */
184class_alias( UserCache::class, 'UserCache' );