| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
namespace MediaWiki\Cache; |
| 25 |
|
| 26 |
use MediaWiki\MediaWikiServices; |
| 27 |
use Psr\Log\LoggerInterface; |
| 28 |
use Wikimedia\Rdbms\IConnectionProvider; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
class UserCache { |
| 35 |
|
| 36 |
protected $cache = []; |
| 37 |
|
| 38 |
protected $typesCached = []; |
| 39 |
|
| 40 |
|
| 41 |
private $logger; |
| 42 |
|
| 43 |
|
| 44 |
private $linkBatchFactory; |
| 45 |
|
| 46 |
|
| 47 |
private $dbProvider; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public static function singleton() { |
| 54 |
wfDeprecated( __METHOD__, '1.43' ); |
| 55 |
return MediaWikiServices::getInstance()->getUserCache(); |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
public function __construct( |
| 66 |
LoggerInterface $logger, |
| 67 |
IConnectionProvider $dbProvider, |
| 68 |
LinkBatchFactory $linkBatchFactory |
| 69 |
) { |
| 70 |
$this->logger = $logger; |
| 71 |
$this->dbProvider = $dbProvider; |
| 72 |
$this->linkBatchFactory = $linkBatchFactory; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public function getProp( $userId, $prop ) { |
| 83 |
if ( !isset( $this->cache[$userId][$prop] ) ) { |
| 84 |
$this->logger->debug( |
| 85 |
'Querying DB for prop {prop} for user ID {userId}', |
| 86 |
[ |
| 87 |
'prop' => $prop, |
| 88 |
'userId' => $userId, |
| 89 |
] |
| 90 |
); |
| 91 |
$this->doQuery( [ $userId ] ); |
| 92 |
} |
| 93 |
|
| 94 |
return $this->cache[$userId][$prop] ?? false; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
public function getUserName( $userId, $ip ) { |
| 106 |
return $userId > 0 ? $this->getProp( $userId, 'name' ) : $ip; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
public function doQuery( array $userIds, $options = [], $caller = '' ) { |
| 116 |
$usersToCheck = []; |
| 117 |
$usersToQuery = []; |
| 118 |
|
| 119 |
$userIds = array_unique( $userIds ); |
| 120 |
|
| 121 |
foreach ( $userIds as $userId ) { |
| 122 |
$userId = (int)$userId; |
| 123 |
if ( $userId <= 0 ) { |
| 124 |
continue; |
| 125 |
} |
| 126 |
if ( isset( $this->cache[$userId]['name'] ) ) { |
| 127 |
$usersToCheck[$userId] = $this->cache[$userId]['name']; |
| 128 |
} else { |
| 129 |
$usersToQuery[] = $userId; |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
if ( count( $usersToQuery ) ) { |
| 135 |
$dbr = $this->dbProvider->getReplicaDatabase(); |
| 136 |
$queryBuilder = $dbr->newSelectQueryBuilder() |
| 137 |
->select( [ 'user_name', 'user_real_name', 'user_registration', 'user_id', 'actor_id' ] ) |
| 138 |
->from( 'user' ) |
| 139 |
->join( 'actor', null, 'actor_user = user_id' ) |
| 140 |
->where( [ 'user_id' => $usersToQuery ] ); |
| 141 |
|
| 142 |
$comment = __METHOD__; |
| 143 |
if ( strval( $caller ) !== '' ) { |
| 144 |
$comment .= "/$caller"; |
| 145 |
} |
| 146 |
|
| 147 |
$res = $queryBuilder->caller( $comment )->fetchResultSet(); |
| 148 |
foreach ( $res as $row ) { |
| 149 |
$userId = (int)$row->user_id; |
| 150 |
$this->cache[$userId]['name'] = $row->user_name; |
| 151 |
$this->cache[$userId]['real_name'] = $row->user_real_name; |
| 152 |
$this->cache[$userId]['registration'] = $row->user_registration; |
| 153 |
$this->cache[$userId]['actor'] = $row->actor_id; |
| 154 |
$usersToCheck[$userId] = $row->user_name; |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
$lb = $this->linkBatchFactory->newLinkBatch(); |
| 159 |
foreach ( $usersToCheck as $userId => $name ) { |
| 160 |
if ( $this->queryNeeded( $userId, 'userpage', $options ) ) { |
| 161 |
$lb->add( NS_USER, $name ); |
| 162 |
$this->typesCached[$userId]['userpage'] = 1; |
| 163 |
} |
| 164 |
if ( $this->queryNeeded( $userId, 'usertalk', $options ) ) { |
| 165 |
$lb->add( NS_USER_TALK, $name ); |
| 166 |
$this->typesCached[$userId]['usertalk'] = 1; |
| 167 |
} |
| 168 |
} |
| 169 |
$lb->execute(); |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
protected function queryNeeded( $uid, $type, array $options ) { |
| 181 |
return ( in_array( $type, $options ) && !isset( $this->typesCached[$uid][$type] ) ); |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
class_alias( UserCache::class, 'UserCache' ); |