1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wikibase\Repo\Specials; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use Wikibase\DataModel\Entity\EntityDocument; |
7
|
|
|
use Wikibase\DataModel\Term\LabelsProvider; |
8
|
|
|
use Wikibase\Lib\Store\EntityTitleLookup; |
9
|
|
|
use Wikibase\Lib\Summary; |
10
|
|
|
use Wikibase\Repo\ChangeOp\ChangeOps; |
11
|
|
|
use Wikibase\Repo\CopyrightMessageBuilder; |
12
|
|
|
use Wikibase\Repo\EditEntity\MediawikiEditEntityFactory; |
13
|
|
|
use Wikibase\Repo\Store\EntityPermissionChecker; |
14
|
|
|
use Wikibase\Repo\SummaryFormatter; |
15
|
|
|
use Wikibase\Repo\WikibaseRepo; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Special page for setting the label of a Wikibase entity. |
19
|
|
|
* |
20
|
|
|
* @license GPL-2.0-or-later |
21
|
|
|
* @author Bene* < [email protected] > |
22
|
|
|
*/ |
23
|
|
|
class SpecialSetLabel extends SpecialModifyTerm { |
|
|
|
|
24
|
|
|
|
25
|
|
|
public function __construct( |
26
|
|
|
SpecialPageCopyrightView $copyrightView, |
27
|
|
|
SummaryFormatter $summaryFormatter, |
28
|
|
|
EntityTitleLookup $entityTitleLookup, |
29
|
|
|
MediawikiEditEntityFactory $editEntityFactory, |
30
|
|
|
EntityPermissionChecker $entityPermissionChecker |
31
|
|
|
) { |
32
|
|
|
parent::__construct( |
33
|
|
|
'SetLabel', |
34
|
|
|
$copyrightView, |
35
|
|
|
$summaryFormatter, |
36
|
|
|
$entityTitleLookup, |
37
|
|
|
$editEntityFactory, |
38
|
|
|
$entityPermissionChecker |
39
|
|
|
); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public static function factory(): self { |
43
|
|
|
$wikibaseRepo = WikibaseRepo::getDefaultInstance(); |
44
|
|
|
|
45
|
|
|
$settings = $wikibaseRepo->getSettings(); |
46
|
|
|
$copyrightView = new SpecialPageCopyrightView( |
47
|
|
|
new CopyrightMessageBuilder(), |
48
|
|
|
$settings->getSetting( 'dataRightsUrl' ), |
49
|
|
|
$settings->getSetting( 'dataRightsText' ) |
50
|
|
|
); |
51
|
|
|
|
52
|
|
|
return new self( |
53
|
|
|
$copyrightView, |
54
|
|
|
$wikibaseRepo->getSummaryFormatter(), |
55
|
|
|
$wikibaseRepo->getEntityTitleLookup(), |
56
|
|
|
$wikibaseRepo->newEditEntityFactory(), |
57
|
|
|
$wikibaseRepo->getEntityPermissionChecker() |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function doesWrites() { |
62
|
|
|
return true; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @see SpecialModifyTerm::validateInput |
67
|
|
|
* |
68
|
|
|
* @return bool |
69
|
|
|
*/ |
70
|
|
|
protected function validateInput() { |
71
|
|
|
if ( !parent::validateInput() ) { |
72
|
|
|
return false; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $this->getBaseRevision()->getEntity() instanceof LabelsProvider; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @see SpecialModifyTerm::getPostedValue() |
80
|
|
|
* |
81
|
|
|
* @return string|null |
82
|
|
|
*/ |
83
|
|
|
protected function getPostedValue() { |
84
|
|
|
return $this->getRequest()->getVal( 'label' ); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @see SpecialModifyTerm::getValue() |
89
|
|
|
* |
90
|
|
|
* @param EntityDocument $entity |
91
|
|
|
* @param string $languageCode |
92
|
|
|
* |
93
|
|
|
* @throws InvalidArgumentException |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
|
|
protected function getValue( EntityDocument $entity, $languageCode ) { |
97
|
|
|
if ( !( $entity instanceof LabelsProvider ) ) { |
98
|
|
|
throw new InvalidArgumentException( '$entity must be a LabelsProvider' ); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$labels = $entity->getLabels(); |
102
|
|
|
|
103
|
|
|
if ( $labels->hasTermForLanguage( $languageCode ) ) { |
104
|
|
|
return $labels->getByLanguage( $languageCode )->getText(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return ''; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @see SpecialModifyTerm::setValue() |
112
|
|
|
* |
113
|
|
|
* @param EntityDocument $entity |
114
|
|
|
* @param string $languageCode |
115
|
|
|
* @param string $value |
116
|
|
|
* |
117
|
|
|
* @return Summary |
118
|
|
|
*/ |
119
|
|
|
protected function setValue( EntityDocument $entity, $languageCode, $value ) { |
120
|
|
|
$value = $value === '' ? null : $value; |
121
|
|
|
$summary = new Summary( 'wbsetlabel' ); |
122
|
|
|
|
123
|
|
|
if ( $value === null ) { |
124
|
|
|
$changeOp = $this->termChangeOpFactory->newRemoveLabelOp( $languageCode ); |
125
|
|
|
} else { |
126
|
|
|
$changeOp = $this->termChangeOpFactory->newSetLabelOp( $languageCode, $value ); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$fingerprintChangeOp = $this->termChangeOpFactory->newFingerprintChangeOp( new ChangeOps( [ $changeOp ] ) ); |
130
|
|
|
|
131
|
|
|
$this->applyChangeOp( $fingerprintChangeOp, $entity, $summary ); |
132
|
|
|
|
133
|
|
|
return $summary; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
} |
137
|
|
|
|