1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace App\Repository; |
6
|
|
|
|
7
|
|
|
use App\Helper\AutomatedEditsHelper; |
8
|
|
|
use App\Model\Edit; |
9
|
|
|
use App\Model\Page; |
10
|
|
|
use App\Model\Project; |
11
|
|
|
use GuzzleHttp\Client; |
12
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
13
|
|
|
use Psr\Container\ContainerInterface; |
14
|
|
|
use Psr\Log\LoggerInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* An EditRepository fetches data about a single revision. |
18
|
|
|
* @codeCoverageIgnore |
19
|
|
|
*/ |
20
|
|
|
class EditRepository extends Repository |
21
|
|
|
{ |
22
|
|
|
protected AutomatedEditsHelper $autoEditsHelper; |
23
|
|
|
|
24
|
|
|
public function __construct( |
25
|
|
|
ContainerInterface $container, |
26
|
|
|
CacheItemPoolInterface $cache, |
27
|
|
|
Client $guzzle, |
28
|
|
|
LoggerInterface $logger, |
29
|
|
|
bool $isWMF, |
30
|
|
|
int $queryTimeout, |
31
|
|
|
AutomatedEditsHelper $autoEditsHelper |
32
|
|
|
) { |
33
|
|
|
$this->autoEditsHelper = $autoEditsHelper; |
34
|
|
|
parent::__construct($container, $cache, $guzzle, $logger, $isWMF, $queryTimeout); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @return AutomatedEditsHelper |
39
|
|
|
*/ |
40
|
|
|
public function getAutoEditsHelper(): AutomatedEditsHelper |
41
|
|
|
{ |
42
|
|
|
return $this->autoEditsHelper; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Get an Edit instance given the revision ID. This does NOT set the associated User or Page. |
47
|
|
|
* @param UserRepository $userRepo |
48
|
|
|
* @param Project $project |
49
|
|
|
* @param int $revId |
50
|
|
|
* @param Page|null $page Provide if you already know the Page, so as to point to the same instance. |
51
|
|
|
* This should already have the PageRepository set. |
52
|
|
|
* @return Edit|null Null if not found. |
53
|
|
|
*/ |
54
|
|
|
public function getEditFromRevIdForPage( |
55
|
|
|
UserRepository $userRepo, |
56
|
|
|
Project $project, |
57
|
|
|
int $revId, |
58
|
|
|
?Page $page = null |
59
|
|
|
): ?Edit { |
60
|
|
|
$revisionTable = $project->getTableName('revision', ''); |
61
|
|
|
$commentTable = $project->getTableName('comment', 'revision'); |
62
|
|
|
$actorTable = $project->getTableName('actor', 'revision'); |
63
|
|
|
$pageSelect = ''; |
64
|
|
|
$pageJoin = ''; |
65
|
|
|
|
66
|
|
|
if (null === $page) { |
67
|
|
|
$pageTable = $project->getTableName('page'); |
68
|
|
|
$pageSelect = "page_title,"; |
69
|
|
|
$pageJoin = "JOIN $pageTable ON revs.rev_page = page_id"; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$sql = "SELECT $pageSelect |
73
|
|
|
revs.rev_id AS id, |
74
|
|
|
actor_name AS username, |
75
|
|
|
revs.rev_timestamp AS timestamp, |
76
|
|
|
revs.rev_minor_edit AS minor, |
77
|
|
|
revs.rev_len AS length, |
78
|
|
|
(CAST(revs.rev_len AS SIGNED) - IFNULL(parentrevs.rev_len, 0)) AS length_change, |
79
|
|
|
comment_text AS comment |
80
|
|
|
FROM $revisionTable AS revs |
81
|
|
|
$pageJoin |
82
|
|
|
JOIN $actorTable ON actor_id = rev_actor |
83
|
|
|
LEFT JOIN $revisionTable AS parentrevs ON (revs.rev_parent_id = parentrevs.rev_id) |
84
|
|
|
LEFT OUTER JOIN $commentTable ON (revs.rev_comment_id = comment_id) |
85
|
|
|
WHERE revs.rev_id = :revId"; |
86
|
|
|
|
87
|
|
|
$result = $this->executeProjectsQuery($project, $sql, ['revId' => $revId]) |
88
|
|
|
->fetchAssociative(); |
89
|
|
|
|
90
|
|
|
if (!$result) { |
91
|
|
|
return null; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
// Create the Page instance. |
95
|
|
|
if (null === $page) { |
96
|
|
|
$page = new Page(null, $project, $result['page_title']); |
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$edit = new Edit($this, $userRepo, $page, $result); |
100
|
|
|
$edit->setRepository($this); |
101
|
|
|
|
102
|
|
|
return $edit; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Use the Compare API to get HTML for the diff. |
107
|
|
|
* @param Edit $edit |
108
|
|
|
* @return string|null Raw HTML, must be wrapped in a <table> tag. Null if no comparison found. |
109
|
|
|
*/ |
110
|
|
|
public function getDiffHtml(Edit $edit): ?string |
111
|
|
|
{ |
112
|
|
|
$params = [ |
113
|
|
|
'action' => 'compare', |
114
|
|
|
'fromrev' => $edit->getId(), |
115
|
|
|
'torelative' => 'prev', |
116
|
|
|
]; |
117
|
|
|
|
118
|
|
|
$res = $this->executeApiRequest($edit->getProject(), $params); |
119
|
|
|
return $res['compare']['*'] ?? null; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|