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