| 1 | <?php |
||
| 16 | class EditCounterRepository extends Repository |
||
| 17 | { |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Get data about revisions, pages, etc. |
||
| 21 | * @param Project $project The project. |
||
| 22 | * @param User $user The user. |
||
| 23 | * @returns string[] With keys: 'deleted', 'live', 'total', 'first', 'last', '24h', '7d', '30d', |
||
| 24 | * '365d', 'small', 'large', 'with_comments', and 'minor_edits', ... |
||
| 25 | */ |
||
| 26 | public function getPairData(Project $project, User $user) |
||
| 27 | { |
||
| 28 | // Set up cache. |
||
| 29 | $cacheKey = 'pairdata.'.$project->getDatabaseName().'.'.$user->getCacheKey(); |
||
| 30 | if ($this->cache->hasItem($cacheKey)) { |
||
| 31 | return $this->cache->getItem($cacheKey)->get(); |
||
| 32 | } |
||
| 33 | |||
| 34 | // Prepare the queries and execute them. |
||
| 35 | $archiveTable = $this->getTableName($project->getDatabaseName(), 'archive'); |
||
| 36 | $revisionTable = $this->getTableName($project->getDatabaseName(), 'revision'); |
||
| 37 | $queries = " |
||
| 38 | |||
| 39 | -- Revision counts. |
||
| 40 | (SELECT 'deleted' AS `key`, COUNT(ar_id) AS val FROM $archiveTable |
||
| 41 | WHERE ar_user = :userId |
||
| 42 | ) UNION ( |
||
| 43 | SELECT 'live' AS `key`, COUNT(rev_id) AS val FROM $revisionTable |
||
| 44 | WHERE rev_user = :userId |
||
| 45 | ) UNION ( |
||
| 46 | SELECT 'day' AS `key`, COUNT(rev_id) AS val FROM $revisionTable |
||
| 47 | WHERE rev_user = :userId AND rev_timestamp >= DATE_SUB(NOW(), INTERVAL 1 DAY) |
||
| 48 | ) UNION ( |
||
| 49 | SELECT 'week' AS `key`, COUNT(rev_id) AS val FROM $revisionTable |
||
| 50 | WHERE rev_user = :userId AND rev_timestamp >= DATE_SUB(NOW(), INTERVAL 1 WEEK) |
||
| 51 | ) UNION ( |
||
| 52 | SELECT 'month' AS `key`, COUNT(rev_id) AS val FROM $revisionTable |
||
| 53 | WHERE rev_user = :userId AND rev_timestamp >= DATE_SUB(NOW(), INTERVAL 1 MONTH) |
||
| 54 | ) UNION ( |
||
| 55 | SELECT 'year' AS `key`, COUNT(rev_id) AS val FROM $revisionTable |
||
| 56 | WHERE rev_user = :userId AND rev_timestamp >= DATE_SUB(NOW(), INTERVAL 1 YEAR) |
||
| 57 | ) UNION ( |
||
| 58 | SELECT 'with_comments' AS `key`, COUNT(rev_id) AS val FROM $revisionTable |
||
| 59 | WHERE rev_user = :userId AND rev_comment != '' |
||
| 60 | ) UNION ( |
||
| 61 | SELECT 'minor' AS `key`, COUNT(rev_id) AS val FROM $revisionTable |
||
| 62 | WHERE rev_user = :userId AND rev_minor_edit = 1 |
||
| 63 | |||
| 64 | -- Dates. |
||
| 65 | ) UNION ( |
||
| 66 | SELECT 'first' AS `key`, rev_timestamp AS `val` FROM $revisionTable |
||
| 67 | WHERE rev_user = :userId ORDER BY rev_timestamp ASC LIMIT 1 |
||
| 68 | ) UNION ( |
||
| 69 | SELECT 'last' AS `key`, rev_timestamp AS `date` FROM $revisionTable |
||
| 70 | WHERE rev_user = :userId ORDER BY rev_timestamp DESC LIMIT 1 |
||
| 71 | |||
| 72 | -- Page counts. |
||
| 73 | ) UNION ( |
||
| 74 | SELECT 'edited-live' AS `key`, COUNT(DISTINCT rev_page) AS `val` |
||
| 75 | FROM $revisionTable |
||
| 76 | WHERE rev_user = :userId |
||
| 77 | ) UNION ( |
||
| 78 | SELECT 'edited-deleted' AS `key`, COUNT(DISTINCT ar_page_id) AS `val` |
||
| 79 | FROM $archiveTable |
||
| 80 | WHERE ar_user = :userId |
||
| 81 | ) UNION ( |
||
| 82 | SELECT 'created-live' AS `key`, COUNT(DISTINCT rev_page) AS `val` |
||
| 83 | FROM $revisionTable |
||
| 84 | WHERE rev_user = :userId AND rev_parent_id = 0 |
||
| 85 | ) UNION ( |
||
| 86 | SELECT 'created-deleted' AS `key`, COUNT(DISTINCT ar_page_id) AS `val` |
||
| 87 | FROM $archiveTable |
||
| 88 | WHERE ar_user = :userId AND ar_parent_id = 0 |
||
| 89 | ) |
||
| 90 | "; |
||
| 91 | $resultQuery = $this->getProjectsConnection()->prepare($queries); |
||
| 92 | $userId = $user->getId($project); |
||
| 93 | $resultQuery->bindParam("userId", $userId); |
||
| 94 | $resultQuery->execute(); |
||
| 95 | $revisionCounts = []; |
||
| 96 | while ($result = $resultQuery->fetch()) { |
||
| 97 | $revisionCounts[$result['key']] = $result['val']; |
||
| 98 | } |
||
| 99 | |||
| 100 | // Cache for 10 minutes, and return. |
||
|
1 ignored issue
–
show
|
|||
| 101 | $cacheItem = $this->cache->getItem($cacheKey) |
||
| 102 | ->set($revisionCounts) |
||
| 103 | ->expiresAfter(new DateInterval('PT10M')); |
||
| 104 | $this->cache->save($cacheItem); |
||
| 105 | |||
| 106 | return $revisionCounts; |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Get log totals for a user. |
||
| 111 | * @param Project $project The project. |
||
| 112 | * @param User $user The user. |
||
| 113 | * @return integer[] Keys are "<log>-<action>" strings, values are counts. |
||
| 114 | */ |
||
| 115 | public function getLogCounts(Project $project, User $user) |
||
| 116 | { |
||
| 117 | // Set up cache. |
||
| 118 | $cacheKey = 'logcounts.'.$project->getDatabaseName().'.'.$user->getCacheKey(); |
||
| 119 | if ($this->cache->hasItem($cacheKey)) { |
||
| 120 | return $this->cache->getItem($cacheKey)->get(); |
||
| 121 | } |
||
| 122 | $this->stopwatch->start($cacheKey, 'XTools'); |
||
| 123 | |||
| 124 | // Query. |
||
| 125 | $loggingTable = $this->getTableName($project->getDatabaseName(), 'logging'); |
||
| 126 | $sql = " |
||
| 127 | (SELECT CONCAT(log_type, '-', log_action) AS source, COUNT(log_id) AS value |
||
| 128 | FROM $loggingTable |
||
| 129 | WHERE log_user = :userId |
||
| 130 | GROUP BY log_type, log_action |
||
| 131 | )"; |
||
| 132 | $resultQuery = $this->getProjectsConnection()->prepare($sql); |
||
| 133 | $userId = $user->getId($project); |
||
| 134 | $resultQuery->bindParam('userId', $userId); |
||
| 135 | $resultQuery->execute(); |
||
| 136 | $results = $resultQuery->fetchAll(); |
||
| 137 | $logCounts = array_combine( |
||
| 138 | array_map(function ($e) { |
||
| 139 | return $e['source']; |
||
| 140 | }, $results), |
||
| 141 | array_map(function ($e) { |
||
| 142 | return $e['value']; |
||
| 143 | }, $results) |
||
| 144 | ); |
||
| 145 | |||
| 146 | // Make sure there is some value for each of the wanted counts. |
||
| 147 | $requiredCounts = [ |
||
| 148 | 'thanks-thank', |
||
| 149 | 'review-approve', |
||
| 150 | 'newusers-create2', |
||
| 151 | 'newusers-byemail', |
||
| 152 | 'patrol-patrol', |
||
| 153 | 'block-block', |
||
| 154 | 'block-reblock', |
||
| 155 | 'block-unblock', |
||
| 156 | 'protect-protect', |
||
| 157 | 'protect-modify', |
||
| 158 | 'protect-unprotect', |
||
| 159 | 'rights-rights', |
||
| 160 | 'move-move', |
||
| 161 | 'delete-delete', |
||
| 162 | 'delete-revision', |
||
| 163 | 'delete-restore', |
||
| 164 | 'import-import', |
||
| 165 | 'import-interwiki', |
||
| 166 | 'import-upload', |
||
| 167 | 'upload-upload', |
||
| 168 | 'upload-overwrite', |
||
| 169 | ]; |
||
| 170 | foreach ($requiredCounts as $req) { |
||
| 171 | if (!isset($logCounts[$req])) { |
||
| 172 | $logCounts[$req] = 0; |
||
| 173 | } |
||
| 174 | } |
||
| 175 | |||
| 176 | // Add Commons upload count, if applicable. |
||
| 177 | $logCounts['files_uploaded_commons'] = 0; |
||
| 178 | if ($this->isLabs()) { |
||
| 179 | $commons = ProjectRepository::getProject('commonswiki', $this->container); |
||
| 180 | $userId = $user->getId($commons); |
||
| 181 | if ($userId) { |
||
| 182 | $sql = "SELECT COUNT(log_id) FROM commonswiki_p.logging_userindex |
||
| 183 | WHERE log_type = 'upload' AND log_action = 'upload' AND log_user = :userId"; |
||
| 184 | $resultQuery = $this->getProjectsConnection()->prepare($sql); |
||
| 185 | $resultQuery->bindParam('userId', $userId); |
||
| 186 | $resultQuery->execute(); |
||
| 187 | $logCounts['files_uploaded_commons'] = $resultQuery->fetchColumn(); |
||
| 188 | } |
||
| 189 | } |
||
| 190 | |||
| 191 | // Cache for 10 minutes, and return. |
||
|
1 ignored issue
–
show
|
|||
| 192 | $cacheItem = $this->cache->getItem($cacheKey) |
||
| 193 | ->set($logCounts) |
||
| 194 | ->expiresAfter(new DateInterval('PT10M')); |
||
| 195 | $this->cache->save($cacheItem); |
||
| 196 | $this->stopwatch->stop($cacheKey); |
||
| 197 | |||
| 198 | return $logCounts; |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Get data for all blocks set on the given user. |
||
| 203 | * @param Project $project |
||
| 204 | * @param User $user |
||
| 205 | * @return array |
||
| 206 | */ |
||
| 207 | public function getBlocksReceived(Project $project, User $user) |
||
| 208 | { |
||
| 209 | $loggingTable = $this->getTableName($project->getDatabaseName(), 'logging', 'logindex'); |
||
| 210 | $sql = "SELECT log_timestamp, log_params FROM $loggingTable |
||
| 211 | WHERE log_type = 'block' |
||
| 212 | AND log_action = 'block' |
||
| 213 | AND log_timestamp > 0 |
||
| 214 | AND log_title = :username"; |
||
| 215 | $resultQuery = $this->getProjectsConnection()->prepare($sql); |
||
| 216 | $username = str_replace(' ', '_', $user->getUsername()); |
||
| 217 | $resultQuery->bindParam('username', $username); |
||
| 218 | $resultQuery->execute(); |
||
| 219 | return $resultQuery->fetchAll(); |
||
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Get a user's total edit count on all projects. |
||
| 224 | * @see EditCounterRepository::globalEditCountsFromCentralAuth() |
||
| 225 | * @see EditCounterRepository::globalEditCountsFromDatabases() |
||
| 226 | * @param User $user The user. |
||
| 227 | * @param Project $project The project to start from. |
||
| 228 | * @return mixed[] Elements are arrays with 'project' (Project), and 'total' (int). |
||
| 229 | */ |
||
| 230 | public function globalEditCounts(User $user, Project $project) |
||
| 231 | { |
||
| 232 | // Get the edit counts from CentralAuth or database. |
||
| 233 | $editCounts = $this->globalEditCountsFromCentralAuth($user, $project); |
||
| 234 | if ($editCounts === false) { |
||
| 235 | $editCounts = $this->globalEditCountsFromDatabases($user, $project); |
||
| 236 | } |
||
| 237 | |||
| 238 | // Pre-populate all projects' metadata, to prevent each project call from fetching it. |
||
| 239 | $project->getRepository()->getAll(); |
||
|
1 ignored issue
–
show
|
|||
| 240 | |||
| 241 | // Compile the output. |
||
| 242 | $out = []; |
||
| 243 | foreach ($editCounts as $editCount) { |
||
| 244 | $out[] = [ |
||
| 245 | 'project' => ProjectRepository::getProject($editCount['dbName'], $this->container), |
||
| 246 | 'total' => $editCount['total'], |
||
| 247 | ]; |
||
| 248 | } |
||
| 249 | return $out; |
||
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Get a user's total edit count on one or more project. |
||
| 254 | * Requires the CentralAuth extension to be installed on the project. |
||
| 255 | * |
||
| 256 | * @param User $user The user. |
||
| 257 | * @param Project $project The project to start from. |
||
| 258 | * @return mixed[] Elements are arrays with 'dbName' (string), and 'total' (int). |
||
| 259 | */ |
||
| 260 | protected function globalEditCountsFromCentralAuth(User $user, Project $project) |
||
| 261 | { |
||
| 262 | $this->log->debug(__METHOD__." Getting global edit counts for ".$user->getUsername()); |
||
| 263 | // Set up cache and stopwatch. |
||
| 264 | $cacheKey = 'globalRevisionCounts.'.$user->getCacheKey(); |
||
| 265 | if ($this->cache->hasItem($cacheKey)) { |
||
| 266 | return $this->cache->getItem($cacheKey)->get(); |
||
| 267 | } |
||
| 268 | $this->stopwatch->start($cacheKey, 'XTools'); |
||
| 269 | |||
| 270 | // Load all projects, so it doesn't have to request metadata about each one as it goes. |
||
| 271 | $project->getRepository()->getAll(); |
||
|
1 ignored issue
–
show
|
|||
| 272 | |||
| 273 | $api = $this->getMediawikiApi($project); |
||
| 274 | $params = [ |
||
| 275 | 'meta' => 'globaluserinfo', |
||
| 276 | 'guiprop' => 'editcount|merged', |
||
| 277 | 'guiuser' => $user->getUsername(), |
||
| 278 | ]; |
||
| 279 | $query = new SimpleRequest('query', $params); |
||
| 280 | $result = $api->getRequest($query); |
||
| 281 | if (!isset($result['query']['globaluserinfo']['merged'])) { |
||
| 282 | return []; |
||
| 283 | } |
||
| 284 | $out = []; |
||
| 285 | foreach ($result['query']['globaluserinfo']['merged'] as $result) { |
||
| 286 | $out[] = [ |
||
| 287 | 'dbName' => $result['wiki'], |
||
| 288 | 'total' => $result['editcount'], |
||
| 289 | ]; |
||
| 290 | } |
||
| 291 | |||
| 292 | // Cache for 10 minutes, and return. |
||
|
1 ignored issue
–
show
|
|||
| 293 | $cacheItem = $this->cache->getItem($cacheKey) |
||
| 294 | ->set($out) |
||
| 295 | ->expiresAfter(new DateInterval('PT10M')); |
||
| 296 | $this->cache->save($cacheItem); |
||
| 297 | $this->stopwatch->stop($cacheKey); |
||
| 298 | |||
| 299 | return $out; |
||
| 300 | } |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Get total edit counts from all projects for this user. |
||
| 304 | * @see EditCounterRepository::globalEditCountsFromCentralAuth() |
||
| 305 | * @param User $user The user. |
||
| 306 | * @param Project $project The project to start from. |
||
| 307 | * @return mixed[] Elements are arrays with 'dbName' (string), and 'total' (int). |
||
| 308 | */ |
||
| 309 | protected function globalEditCountsFromDatabases(User $user, Project $project) |
||
| 310 | { |
||
| 311 | $stopwatchName = 'globalRevisionCounts.'.$user->getUsername(); |
||
| 312 | $allProjects = $project->getRepository()->getAll(); |
||
|
1 ignored issue
–
show
|
|||
| 313 | $topEditCounts = []; |
||
| 314 | $username = $user->getUsername(); |
||
| 315 | foreach ($allProjects as $projectMeta) { |
||
| 316 | $revisionTableName = $this->getTableName($projectMeta['dbName'], 'revision'); |
||
| 317 | $sql = "SELECT COUNT(rev_id) FROM $revisionTableName WHERE rev_user_text=:username"; |
||
| 318 | $stmt = $this->getProjectsConnection()->prepare($sql); |
||
| 319 | $stmt->bindParam('username', $username); |
||
| 320 | $stmt->execute(); |
||
| 321 | $total = (int)$stmt->fetchColumn(); |
||
| 322 | $topEditCounts[] = [ |
||
| 323 | 'dbName' => $projectMeta['dbName'], |
||
| 324 | 'total' => $total, |
||
| 325 | ]; |
||
| 326 | $this->stopwatch->lap($stopwatchName); |
||
| 327 | } |
||
| 328 | return $topEditCounts; |
||
| 329 | } |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Get the given user's total edit counts per namespace on the given project. |
||
| 333 | * @param Project $project The project. |
||
| 334 | * @param User $user The user. |
||
| 335 | * @return integer[] Array keys are namespace IDs, values are the edit counts. |
||
| 336 | */ |
||
| 337 | public function getNamespaceTotals(Project $project, User $user) |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Get revisions by this user. |
||
| 375 | * @param Project[] $projects The projects. |
||
| 376 | * @param User $user The user. |
||
| 377 | * @param int $lim The maximum number of revisions to fetch from each project. |
||
| 378 | * @return array|mixed |
||
| 379 | */ |
||
| 380 | public function getRevisions($projects, User $user, $lim = 40) |
||
| 434 | |||
| 435 | /** |
||
| 436 | * Get data for a bar chart of monthly edit totals per namespace. |
||
| 437 | * @param Project $project The project. |
||
| 438 | * @param User $user The user. |
||
| 439 | * @return string[] [ |
||
| 440 | * [ |
||
| 441 | * 'year' => <year>, |
||
| 442 | * 'month' => <month>, |
||
| 443 | * 'page_namespace' => <namespace>, |
||
| 444 | * 'count' => <count>, |
||
| 445 | * ], |
||
| 446 | * ... |
||
| 447 | * ] |
||
| 448 | */ |
||
| 449 | public function getMonthCounts(Project $project, User $user) |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Get data for the timecard chart, with totals grouped by day and to the nearest two-hours. |
||
| 486 | * @param Project $project |
||
| 487 | * @param User $user |
||
| 488 | * @return string[] |
||
| 489 | */ |
||
| 490 | public function getTimeCard(Project $project, User $user) |
||
| 530 | |||
| 531 | /** |
||
| 532 | * Get various data about edit sizes of the past 5,000 edits. |
||
| 533 | * Will cache the result for 10 minutes. |
||
| 534 | * @param Project $project The project. |
||
| 535 | * @param User $user The user. |
||
| 536 | * @return string[] Values with for keys 'average_size', |
||
| 537 | * 'small_edits' and 'large_edits' |
||
| 538 | */ |
||
| 539 | public function getEditSizeData(Project $project, User $user) |
||
| 576 | } |
||
| 577 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.