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 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
BasicMediaInfoDiffVisualizer
0.00% covered (danger)
0.00%
0 / 35
0.00% covered (danger)
0.00%
0 / 4
90
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 visualizeEntityContentDiff
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 visualizeEntityDiff
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
12
 getClaimDiffHtml
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2
3namespace Wikibase\MediaInfo\Diff;
4
5use Diff\DiffOp\Diff\Diff;
6use Diff\DiffOp\DiffOp;
7use Diff\DiffOp\DiffOpAdd;
8use Diff\DiffOp\DiffOpChange;
9use Diff\DiffOp\DiffOpRemove;
10use InvalidArgumentException;
11use MessageLocalizer;
12use Wikibase\DataModel\Services\Diff\EntityDiff;
13use Wikibase\Repo\Content\EntityContentDiff;
14use Wikibase\Repo\Diff\BasicDiffView;
15use Wikibase\Repo\Diff\ClaimDiffer;
16use Wikibase\Repo\Diff\ClaimDifferenceVisualizer;
17use Wikibase\Repo\Diff\EntityDiffVisualizer;
18
19/**
20 * Class for generating basic diff views of mediainfo entities
21 *
22 * @license GPL-2.0-or-later
23 * @author Addshore
24 */
25class BasicMediaInfoDiffVisualizer implements EntityDiffVisualizer {
26
27    /**
28     * @var MessageLocalizer
29     */
30    private $messageLocalizer;
31
32    /**
33     * @var ClaimDiffer|null
34     */
35    private $claimDiffer;
36
37    /**
38     * @var ClaimDifferenceVisualizer|null
39     */
40    private $claimDiffVisualizer;
41
42    public function __construct(
43        MessageLocalizer $messageLocalizer,
44        ClaimDiffer $claimDiffer,
45        ClaimDifferenceVisualizer $claimDiffView
46    ) {
47        $this->messageLocalizer = $messageLocalizer;
48        $this->claimDiffer = $claimDiffer;
49        $this->claimDiffVisualizer = $claimDiffView;
50    }
51
52    /**
53     * Generates and returns an HTML visualization of the provided EntityContentDiff.
54     *
55     * @param EntityContentDiff $diff
56     *
57     * @return string
58     */
59    public function visualizeEntityContentDiff( EntityContentDiff $diff ) {
60        return $this->visualizeEntityDiff( $diff->getEntityDiff() );
61    }
62
63    /**
64     * Generates and returns an HTML visualization of the provided EntityDiff.
65     *
66     * @param EntityDiff $diff
67     *
68     * @return string
69     */
70    protected function visualizeEntityDiff( EntityDiff $diff ) {
71        if ( $diff->isEmpty() ) {
72            return '';
73        }
74
75        $html = '';
76
77        $termDiffVisualizer = new BasicDiffView(
78            [],
79            new Diff(
80                [
81                    $this->messageLocalizer->msg( 'wikibasemediainfo-diffview-label' )
82                        ->text() => $diff->getLabelsDiff(),
83                ],
84                true
85            )
86        );
87
88        $html .= $termDiffVisualizer->getHtml();
89
90        foreach ( $diff->getClaimsDiff() as $claimDiffOp ) {
91            $html .= $this->getClaimDiffHtml( $claimDiffOp );
92        }
93
94        return $html;
95    }
96
97    /**
98     * @param DiffOp $claimDiffOp
99     *
100     * @return string HTML
101     */
102    protected function getClaimDiffHtml( DiffOp $claimDiffOp ) {
103        if ( $claimDiffOp instanceof DiffOpChange ) {
104            $claimDifference = $this->claimDiffer->diffClaims(
105                $claimDiffOp->getOldValue(),
106                $claimDiffOp->getNewValue()
107            );
108            return $this->claimDiffVisualizer->visualizeClaimChange(
109                $claimDifference,
110                $claimDiffOp->getNewValue()
111            );
112        }
113
114        if ( $claimDiffOp instanceof DiffOpAdd ) {
115            return $this->claimDiffVisualizer->visualizeNewClaim( $claimDiffOp->getNewValue() );
116        } elseif ( $claimDiffOp instanceof DiffOpRemove ) {
117            return $this->claimDiffVisualizer->visualizeRemovedClaim( $claimDiffOp->getOldValue() );
118        } else {
119            throw new InvalidArgumentException( 'Encountered an unexpected diff operation type for a claim' );
120        }
121    }
122
123}