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