Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
95.24% |
20 / 21 |
|
80.00% |
4 / 5 |
CRAP | |
0.00% |
0 / 1 |
ChangeOpRemoveFormRepresentation | |
95.24% |
20 / 21 |
|
80.00% |
4 / 5 |
7 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
validate | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
apply | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 | |||
updateSummary | |
90.00% |
9 / 10 |
|
0.00% |
0 / 1 |
2.00 | |||
getActions | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace Wikibase\Lexeme\DataAccess\ChangeOp; |
4 | |
5 | use ValueValidators\Result; |
6 | use Wikibase\DataModel\Entity\EntityDocument; |
7 | use Wikibase\Lexeme\Domain\Model\Form; |
8 | use Wikibase\Lib\Summary; |
9 | use Wikibase\Repo\ChangeOp\ChangeOp; |
10 | use Wikibase\Repo\ChangeOp\DummyChangeOpResult; |
11 | use Wikibase\Repo\Store\EntityPermissionChecker; |
12 | use Wikimedia\Assert\Assert; |
13 | |
14 | /** |
15 | * @license GPL-2.0-or-later |
16 | */ |
17 | class ChangeOpRemoveFormRepresentation implements ChangeOp { |
18 | |
19 | private const SUMMARY_ACTION_REMOVE = 'remove-form-representations'; |
20 | |
21 | /** |
22 | * TODO LanguageCode model? |
23 | * |
24 | * @var string |
25 | */ |
26 | private $language; |
27 | |
28 | /** |
29 | * @param string $language The language to remove |
30 | */ |
31 | public function __construct( $language ) { |
32 | $this->language = $language; |
33 | } |
34 | |
35 | public function validate( EntityDocument $entity ) { |
36 | Assert::parameterType( Form::class, $entity, '$entity' ); |
37 | |
38 | return Result::newSuccess(); |
39 | } |
40 | |
41 | public function apply( EntityDocument $entity, ?Summary $summary = null ) { |
42 | Assert::parameterType( Form::class, $entity, '$entity' ); |
43 | '@phan-var Form $entity'; |
44 | |
45 | /** @var Form $entity */ |
46 | |
47 | $representations = $entity->getRepresentations(); |
48 | |
49 | if ( !$representations->hasTermForLanguage( $this->language ) ) { |
50 | return new DummyChangeOpResult(); |
51 | } |
52 | |
53 | $this->updateSummary( $entity, $summary ); |
54 | |
55 | $representations->removeByLanguage( $this->language ); |
56 | |
57 | return new DummyChangeOpResult(); |
58 | } |
59 | |
60 | private function updateSummary( Form $form, ?Summary $summary = null ) { |
61 | if ( $summary === null ) { |
62 | return; |
63 | } |
64 | |
65 | $summary->setAction( self::SUMMARY_ACTION_REMOVE ); |
66 | |
67 | $summary->setLanguage( $this->language ); |
68 | $summary->addAutoCommentArgs( [ |
69 | $form->getId()->getSerialization(), // TODO: use FormId not string? |
70 | ] ); |
71 | $summary->addAutoSummaryArgs( [ |
72 | $this->language => $form->getRepresentations()->getByLanguage( $this->language )->getText(), |
73 | ] ); |
74 | } |
75 | |
76 | public function getActions() { |
77 | return [ EntityPermissionChecker::ACTION_EDIT ]; |
78 | } |
79 | |
80 | } |