|
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. |
|
14
|
|
|
* @codeCoverageIgnore |
|
15
|
|
|
*/ |
|
16
|
|
|
class TopEditsRepository extends Repository |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Get the top edits by a user in a single 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 getTopEditsNamespace(Project $project, User $user, $namespace = 0, $limit = 100) |
|
28
|
|
|
{ |
|
29
|
|
|
// Set up cache. |
|
30
|
|
|
$cacheKey = $this->getCacheKey(func_get_args(), 'topedits'); |
|
31
|
|
|
if ($this->cache->hasItem($cacheKey)) { |
|
32
|
|
|
return $this->cache->getItem($cacheKey)->get(); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
$pageTable = $this->getTableName($project->getDatabaseName(), 'page'); |
|
36
|
|
|
$revisionTable = $this->getTableName($project->getDatabaseName(), 'revision'); |
|
37
|
|
|
|
|
38
|
|
|
$hasPageAssessments = $this->isLabs() && $project->hasPageAssessments() && $namespace === 0; |
|
39
|
|
|
$paSelect = $hasPageAssessments |
|
40
|
|
|
? ", ( |
|
41
|
|
|
SELECT pa_class |
|
42
|
|
|
FROM page_assessments |
|
43
|
|
|
WHERE pa_page_id = page_id |
|
44
|
|
|
AND pa_class != 'Unknown' |
|
45
|
|
|
LIMIT 1 |
|
46
|
|
|
) AS pa_class" |
|
47
|
|
|
: ''; |
|
48
|
|
|
|
|
49
|
|
|
$sql = "SELECT page_namespace, page_title, page_is_redirect, COUNT(page_title) AS count |
|
50
|
|
|
$paSelect |
|
51
|
|
|
FROM $pageTable JOIN $revisionTable ON page_id = rev_page |
|
52
|
|
|
WHERE rev_user_text = :username |
|
53
|
|
|
AND page_namespace = :namespace |
|
54
|
|
|
GROUP BY page_namespace, page_title |
|
55
|
|
|
ORDER BY count DESC |
|
56
|
|
|
LIMIT $limit"; |
|
57
|
|
|
$resultQuery = $this->getProjectsConnection()->prepare($sql); |
|
58
|
|
|
$username = $user->getUsername(); |
|
59
|
|
|
$resultQuery->bindParam('username', $username); |
|
60
|
|
|
$resultQuery->bindParam('namespace', $namespace); |
|
61
|
|
|
$resultQuery->execute(); |
|
62
|
|
|
$results = $resultQuery->fetchAll(); |
|
63
|
|
|
|
|
64
|
|
|
// Cache for 10 minutes, and return. |
|
65
|
|
|
$cacheItem = $this->cache->getItem($cacheKey) |
|
66
|
|
|
->set($results) |
|
67
|
|
|
->expiresAfter(new DateInterval('PT10M')); |
|
68
|
|
|
$this->cache->save($cacheItem); |
|
69
|
|
|
|
|
70
|
|
|
return $results; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Get the top edits by a user across all namespaces. |
|
75
|
|
|
* @param Project $project |
|
76
|
|
|
* @param User $user |
|
77
|
|
|
* @param int $limit Number of edits to fetch. |
|
78
|
|
|
* @return string[] page_namespace, page_title, page_is_redirect, |
|
79
|
|
|
* count (number of edits), assessment (page assessment). |
|
80
|
|
|
*/ |
|
81
|
|
|
public function getTopEditsAllNamespaces(Project $project, User $user, $limit = 10) |
|
82
|
|
|
{ |
|
83
|
|
|
// Set up cache. |
|
84
|
|
|
$cacheKey = $this->getCacheKey(func_get_args(), 'topedits_all'); |
|
85
|
|
|
if ($this->cache->hasItem($cacheKey)) { |
|
86
|
|
|
return $this->cache->getItem($cacheKey)->get(); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$pageTable = $this->getTableName($project->getDatabaseName(), 'page'); |
|
90
|
|
|
$revisionTable = $this->getTableName($project->getDatabaseName(), 'revision'); |
|
91
|
|
|
$hasPageAssessments = $this->isLabs() && $project->hasPageAssessments(); |
|
92
|
|
|
$pageAssessmentsTable = $this->getTableName($project->getDatabaseName(), 'page_assessments'); |
|
93
|
|
|
$paSelect = $hasPageAssessments |
|
94
|
|
|
? ", ( |
|
95
|
|
|
SELECT pa_class |
|
96
|
|
|
FROM $pageAssessmentsTable |
|
97
|
|
|
WHERE pa_page_id = e.page_id |
|
98
|
|
|
LIMIT 1 |
|
99
|
|
|
) AS pa_class" |
|
100
|
|
|
: ', NULL as pa_class'; |
|
101
|
|
|
|
|
102
|
|
|
$sql = "SELECT c.page_namespace, e.page_title, c.count $paSelect |
|
103
|
|
|
FROM |
|
104
|
|
|
( |
|
105
|
|
|
SELECT b.page_namespace, b.rev_page, b.count |
|
106
|
|
|
,@rn := if(@ns = b.page_namespace, @rn + 1, 1) AS row_number |
|
107
|
|
|
,@ns := b.page_namespace AS dummy |
|
108
|
|
|
FROM |
|
109
|
|
|
( |
|
110
|
|
|
SELECT page_namespace, rev_page, count(rev_page) AS count |
|
111
|
|
|
FROM $revisionTable |
|
112
|
|
|
JOIN $pageTable ON page_id = rev_page |
|
113
|
|
|
WHERE rev_user_text = :username |
|
114
|
|
|
GROUP BY page_namespace, rev_page |
|
115
|
|
|
) AS b |
|
116
|
|
|
JOIN (SELECT @ns := NULL, @rn := 0) AS vars |
|
117
|
|
|
ORDER BY b.page_namespace ASC, b.count DESC |
|
118
|
|
|
) AS c |
|
119
|
|
|
JOIN $pageTable e ON e.page_id = c.rev_page |
|
120
|
|
|
WHERE c.row_number < $limit"; |
|
121
|
|
|
$resultQuery = $this->getProjectsConnection()->prepare($sql); |
|
122
|
|
|
$username = $user->getUsername(); |
|
123
|
|
|
$resultQuery->bindParam('username', $username); |
|
124
|
|
|
$resultQuery->execute(); |
|
125
|
|
|
$results = $resultQuery->fetchAll(); |
|
126
|
|
|
|
|
127
|
|
|
// Cache for 10 minutes, and return. |
|
128
|
|
|
$cacheItem = $this->cache->getItem($cacheKey) |
|
129
|
|
|
->set($results) |
|
130
|
|
|
->expiresAfter(new DateInterval('PT10M')); |
|
131
|
|
|
$this->cache->save($cacheItem); |
|
132
|
|
|
|
|
133
|
|
|
return $results; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Get the display titles of the given pages. |
|
138
|
|
|
* @param Project $project |
|
139
|
|
|
* @param string[] $titles List of page titles. |
|
140
|
|
|
* @return string[] Keys are the original supplied titles, and values are the display titles. |
|
141
|
|
|
*/ |
|
142
|
|
|
public function getDisplayTitles(Project $project, $titles) |
|
143
|
|
|
{ |
|
144
|
|
|
/** @var ApiHelper $apiHelper */ |
|
145
|
|
|
$apiHelper = $this->container->get('app.api_helper'); |
|
146
|
|
|
|
|
147
|
|
|
return $apiHelper->displayTitles($project->getDomain(), $titles); |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|