Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 35
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
RevisionDiffViewFormatter
0.00% covered (danger)
0.00%
0 / 35
0.00% covered (danger)
0.00%
0 / 2
20
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 formatApi
0.00% covered (danger)
0.00%
0 / 33
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3namespace Flow\Formatter;
4
5use Flow\Model\UUID;
6use Flow\UrlGenerator;
7use IContextSource;
8
9class RevisionDiffViewFormatter {
10
11    /**
12     * @var RevisionViewFormatter
13     */
14    protected $revisionViewFormatter;
15
16    /**
17     * @var UrlGenerator
18     */
19    protected $urlGenerator;
20
21    public function __construct(
22        RevisionViewFormatter $revisionViewFormatter,
23        UrlGenerator $urlGenerator
24    ) {
25        $this->revisionViewFormatter = $revisionViewFormatter;
26        $this->urlGenerator = $urlGenerator;
27    }
28
29    /**
30     * Diff would format against two revisions
31     * @param FormatterRow $newRow
32     * @param FormatterRow $oldRow
33     * @param IContextSource $ctx
34     * @return array
35     */
36    public function formatApi( FormatterRow $newRow, FormatterRow $oldRow, IContextSource $ctx ) {
37        $oldRes = $this->revisionViewFormatter->formatApi( $oldRow, $ctx );
38        $newRes = $this->revisionViewFormatter->formatApi( $newRow, $ctx );
39
40        $oldContent = $oldRow->revision->getContentInWikitext();
41        $newContent = $newRow->revision->getContentInWikitext();
42
43        $differenceEngine = new \DifferenceEngine();
44
45        $differenceEngine->setContent(
46            new \TextContent( $oldContent ),
47            new \TextContent( $newContent )
48        );
49
50        if ( $oldRow->revision->isFirstRevision() ) {
51            $prevLink = null;
52        } else {
53            $prevLink = $this->urlGenerator->diffLink(
54                $oldRow->revision,
55                $ctx->getTitle(),
56                UUID::create( $oldRes['workflowId'] )
57            )->getLocalURL();
58        }
59
60        // this is probably a network request which typically goes in the query
61        // half, but we don't have to worry about batching because we only show
62        // one diff at a time so just do it.
63        $nextRevision = $newRow->revision->getCollection()->getNextRevision( $newRow->revision );
64        if ( $nextRevision === null ) {
65            $nextLink = null;
66        } else {
67            $nextLink = $this->urlGenerator->diffLink(
68                $nextRevision,
69                $ctx->getTitle(),
70                UUID::create( $newRes['workflowId'] )
71            )->getLocalURL();
72        }
73
74        return [
75            'new' => $newRes,
76            'old' => $oldRes,
77            'diff_content' => $differenceEngine->getDiffBody(),
78            'links' => [
79                'previous' => $prevLink,
80                'next' => $nextLink,
81            ],
82        ];
83    }
84}