Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
RevisionUndoViewFormatter
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 3
30
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 formatApi
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 1
2
 getUndoContent
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3namespace Flow\Formatter;
4
5use DifferenceEngine;
6use MediaWiki\Content\TextContent;
7use MediaWiki\Context\IContextSource;
8
9class RevisionUndoViewFormatter {
10    /** @var RevisionViewFormatter */
11    protected $revisionViewFormatter;
12
13    public function __construct( RevisionViewFormatter $revisionViewFormatter ) {
14        $this->revisionViewFormatter = $revisionViewFormatter;
15    }
16
17    /**
18     * Undoes the change that occurred between $start and $stop
19     * @param FormatterRow $start
20     * @param FormatterRow $stop
21     * @param FormatterRow $current
22     * @param IContextSource $context
23     * @return array
24     */
25    public function formatApi(
26        FormatterRow $start,
27        FormatterRow $stop,
28        FormatterRow $current,
29        IContextSource $context
30    ) {
31        $currentWikitext = $current->revision->getContentInWikitext();
32
33        $undoContent = $this->getUndoContent(
34            $start->revision->getContentInWikitext(),
35            $stop->revision->getContentInWikitext(),
36            $currentWikitext
37        );
38
39        $differenceEngine = new DifferenceEngine();
40        $differenceEngine->setContent(
41            new TextContent( $currentWikitext ),
42            new TextContent( $undoContent )
43        );
44
45        $this->revisionViewFormatter->setContentFormat( 'wikitext' );
46
47        // @todo if stop === current we could do a little less processing
48        return [
49            'start' => $this->revisionViewFormatter->formatApi( $start, $context ),
50            'stop' => $this->revisionViewFormatter->formatApi( $stop, $context ),
51            'current' => $this->revisionViewFormatter->formatApi( $current, $context ),
52            'undo' => [
53                'possible' => $undoContent !== false,
54                'content' => $undoContent,
55                'diff_content' => $differenceEngine->getDiffBody(),
56            ],
57            'articleTitle' => $start->workflow->getArticleTitle(),
58        ];
59    }
60
61    protected function getUndoContent( $startContent, $stopContent, $currentContent ) {
62        if ( $currentContent === $stopContent ) {
63            return $startContent;
64        } else {
65            // 3-way merge
66            $ok = wfMerge( $stopContent, $startContent, $currentContent, $result );
67            if ( $ok ) {
68                return $result;
69            } else {
70                return false;
71            }
72        }
73    }
74}