Passed
Push — master ( d0265c...1b1a87 )
by MusikAnimal
07:34
created

EditRepository::getEditFromRevIdForPage()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 40
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 21
nc 4
nop 3
dl 0
loc 40
rs 9.584
c 1
b 0
f 0
1
<?php
2
declare(strict_types = 1);
3
4
namespace AppBundle\Repository;
5
6
use AppBundle\Model\Edit;
7
use AppBundle\Model\Page;
8
use AppBundle\Model\Project;
9
10
/**
11
 * An EditRepository fetches data about a single revision.
12
 * @codeCoverageIgnore
13
 */
14
class EditRepository extends Repository
15
{
16
    /**
17
     * Get an Edit instance given the revision ID. This does NOT set the associated User or Page.
18
     * @param Project $project
19
     * @param int $revId
20
     * @param Page|null $page Provide if you already know the Page, so as to point to the same instance.
21
     * @return Edit|null Null if not found.
22
     */
23
    public function getEditFromRevIdForPage(Project $project, int $revId, ?Page $page = null): ?Edit
24
    {
25
        $revisionTable = $project->getTableName('revision', '');
26
        $commentTable = $project->getTableName('comment', 'revision');
27
        $actorTable = $project->getTableName('actor', 'revision');
28
        $pageSelect = '';
29
        $pageJoin = '';
30
31
        if (null === $page) {
32
            $pageTable = $project->getTableName('page');
33
            $pageSelect = "page_title,";
34
            $pageJoin = "JOIN $pageTable ON revs.rev_page = page_id";
35
        }
36
37
        $sql = "SELECT $pageSelect
38
                    revs.rev_id AS id,
39
                    actor_name AS username,
40
                    revs.rev_timestamp AS timestamp,
41
                    revs.rev_minor_edit AS minor,
42
                    revs.rev_len AS length,
43
                    (CAST(revs.rev_len AS SIGNED) - IFNULL(parentrevs.rev_len, 0)) AS length_change,
44
                    comment_text AS comment
45
                FROM $revisionTable AS revs
46
                $pageJoin
47
                JOIN $actorTable ON actor_id = rev_actor
48
                LEFT JOIN $revisionTable AS parentrevs ON (revs.rev_parent_id = parentrevs.rev_id)
49
                LEFT OUTER JOIN $commentTable ON (revs.rev_comment_id = comment_id)
50
                WHERE revs.rev_id = :revId";
51
52
        $result = $this->executeProjectsQuery($sql, ['revId' => $revId])->fetch();
53
54
        // Create the Page instance.
55
        if (null === $page) {
56
            $page = new Page($project, $result['page_title']);
57
        }
58
59
        $edit = new Edit($page, $result);
60
        $edit->setRepository($this);
61
62
        return $edit;
63
    }
64
65
    /**
66
     * Use the Compare API to get HTML for the diff.
67
     * @param Edit $edit
68
     * @return string Raw HTML, must be wrapped in a <table> tag.
69
     */
70
    public function getDiffHtml(Edit $edit): string
71
    {
72
        $params = [
73
            'action' => 'compare',
74
            'fromrev' => $edit->getId(),
75
            'torelative' => 'prev',
76
        ];
77
78
        $res = $this->executeApiRequest($edit->getProject(), $params);
79
        return $res['compare']['*'];
80
    }
81
}
82