|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file contains only the TopEditsRepository class. |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace Xtools; |
|
7
|
|
|
|
|
8
|
|
|
use DateInterval; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* TopEditsRepository is responsible for retrieving data from the database |
|
12
|
|
|
* about the top-edited pages of a user. It doesn't do any post-processing |
|
13
|
|
|
* of that information. There's really only one query here, but for testing |
|
14
|
|
|
* purposes we want this in a separate file. |
|
15
|
|
|
*/ |
|
16
|
|
|
class TopEditsRepository extends Repository |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Get the top edits by a user in the given namespace. |
|
20
|
|
|
* @param Project $project |
|
21
|
|
|
* @param User $user |
|
22
|
|
|
* @param int $namespace Namespace ID. |
|
23
|
|
|
* @param int $limit Number of edits to fetch. |
|
24
|
|
|
* @return string[] page_namespace, page_title, page_is_redirect, |
|
25
|
|
|
* count (number of edits), assessment (page assessment). |
|
26
|
|
|
*/ |
|
27
|
|
|
public function getTopEdits(Project $project, User $user, $namespace = 0, $limit = 100) |
|
28
|
|
|
{ |
|
29
|
|
|
// Set up cache. |
|
30
|
|
|
$cacheKey = 'topedits.'.$project->getDatabaseName().'.'.$user->getCacheKey(). |
|
31
|
|
|
$namespace.$limit; |
|
32
|
|
|
if ($this->cache->hasItem($cacheKey)) { |
|
33
|
|
|
return $this->cache->getItem($cacheKey)->get(); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
$pageTable = $this->getTableName($project->getDatabaseName(), 'page'); |
|
37
|
|
|
$revisionTable = $this->getTableName($project->getDatabaseName(), 'revision'); |
|
38
|
|
|
|
|
39
|
|
|
$hasPageAssessments = $this->isLabs() && $project->hasPageAssessments() && $namespace === 0; |
|
40
|
|
|
$pageAssessmentsTable = $this->getTableName($project->getDatabaseName(), 'page_assessments'); |
|
41
|
|
|
$paJoin = $hasPageAssessments ? "LEFT JOIN $pageAssessmentsTable ON rev_page = pa_page_id" : ''; |
|
42
|
|
|
$paSelects = $hasPageAssessments ? ', pa_class' : ''; |
|
43
|
|
|
|
|
44
|
|
|
$sql = "SELECT page_namespace, page_title, page_is_redirect, COUNT(page_title) AS count |
|
45
|
|
|
$paSelects |
|
46
|
|
|
FROM $pageTable JOIN $revisionTable ON page_id = rev_page |
|
47
|
|
|
$paJoin |
|
48
|
|
|
WHERE rev_user_text = :username |
|
49
|
|
|
AND page_namespace = :namespace |
|
50
|
|
|
GROUP BY page_namespace, page_title |
|
51
|
|
|
ORDER BY count DESC |
|
52
|
|
|
LIMIT $limit"; |
|
53
|
|
|
$resultQuery = $this->getProjectsConnection()->prepare($sql); |
|
54
|
|
|
$username = $user->getUsername(); |
|
55
|
|
|
$resultQuery->bindParam('username', $username); |
|
56
|
|
|
$resultQuery->bindParam('namespace', $namespace); |
|
57
|
|
|
$resultQuery->execute(); |
|
58
|
|
|
$results = $resultQuery->fetchAll(); |
|
59
|
|
|
|
|
60
|
|
|
// Cache for 10 minutes, and return. |
|
61
|
|
|
$cacheItem = $this->cache->getItem($cacheKey) |
|
62
|
|
|
->set($results) |
|
63
|
|
|
->expiresAfter(new DateInterval('PT10M')); |
|
64
|
|
|
$this->cache->save($cacheItem); |
|
65
|
|
|
|
|
66
|
|
|
return $results; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Get the display titles of the given pages. |
|
71
|
|
|
* @param Project $project |
|
72
|
|
|
* @param string[] $titles List of page titles. |
|
73
|
|
|
* @return string[] Keys are the original supplied titles, and values are the display titles. |
|
74
|
|
|
*/ |
|
75
|
|
|
public function getDisplayTitles(Project $project, $titles) |
|
76
|
|
|
{ |
|
77
|
|
|
/** @var ApiHelper $apiHelper */ |
|
78
|
|
|
$apiHelper = $this->container->get('app.api_helper'); |
|
79
|
|
|
|
|
80
|
|
|
return $apiHelper->displayTitles($project->getDomain(), $titles); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|