|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Wikibase\Repo\Specials; |
|
4
|
|
|
|
|
5
|
|
|
use OutputPage; |
|
6
|
|
|
use Status; |
|
7
|
|
|
use Wikibase\DataModel\Entity\EntityDocument; |
|
8
|
|
|
use Wikibase\DataModel\Entity\Property; |
|
9
|
|
|
use Wikibase\DataModel\Term\Term; |
|
10
|
|
|
use Wikibase\Lib\Store\EntityNamespaceLookup; |
|
11
|
|
|
use Wikibase\Lib\Store\EntityTitleLookup; |
|
12
|
|
|
use Wikibase\Lib\Summary; |
|
13
|
|
|
use Wikibase\Repo\CopyrightMessageBuilder; |
|
14
|
|
|
use Wikibase\Repo\DataTypeSelector; |
|
15
|
|
|
use Wikibase\Repo\EditEntity\MediawikiEditEntityFactory; |
|
16
|
|
|
use Wikibase\Repo\Specials\HTMLForm\HTMLAliasesField; |
|
17
|
|
|
use Wikibase\Repo\Specials\HTMLForm\HTMLContentLanguageField; |
|
18
|
|
|
use Wikibase\Repo\Specials\HTMLForm\HTMLTrimmedTextField; |
|
19
|
|
|
use Wikibase\Repo\Store\TermsCollisionDetector; |
|
20
|
|
|
use Wikibase\Repo\SummaryFormatter; |
|
21
|
|
|
use Wikibase\Repo\WikibaseRepo; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Page for creating new Wikibase properties. |
|
25
|
|
|
* |
|
26
|
|
|
* @license GPL-2.0-or-later |
|
27
|
|
|
* @author John Erling Blad < [email protected] > |
|
28
|
|
|
*/ |
|
29
|
|
|
class SpecialNewProperty extends SpecialNewEntity { |
|
30
|
|
|
const FIELD_LANG = 'lang'; |
|
31
|
|
|
const FIELD_DATATYPE = 'datatype'; |
|
32
|
|
|
const FIELD_LABEL = 'label'; |
|
33
|
|
|
const FIELD_DESCRIPTION = 'description'; |
|
34
|
|
|
const FIELD_ALIASES = 'aliases'; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var TermsCollisionDetector |
|
38
|
|
|
*/ |
|
39
|
|
|
private $termsCollisionDetector; |
|
40
|
|
|
|
|
41
|
|
|
public function __construct( |
|
42
|
|
|
SpecialPageCopyrightView $specialPageCopyrightView, |
|
43
|
|
|
EntityNamespaceLookup $entityNamespaceLookup, |
|
44
|
|
|
SummaryFormatter $summaryFormatter, |
|
45
|
|
|
EntityTitleLookup $entityTitleLookup, |
|
46
|
|
|
MediawikiEditEntityFactory $editEntityFactory, |
|
47
|
|
|
TermsCollisionDetector $termsCollisionDetector |
|
48
|
|
|
) { |
|
49
|
|
|
parent::__construct( |
|
50
|
|
|
'NewProperty', |
|
51
|
|
|
'property-create', |
|
52
|
|
|
$specialPageCopyrightView, |
|
53
|
|
|
$entityNamespaceLookup, |
|
54
|
|
|
$summaryFormatter, |
|
55
|
|
|
$entityTitleLookup, |
|
56
|
|
|
$editEntityFactory |
|
57
|
|
|
); |
|
58
|
|
|
|
|
59
|
|
|
$this->termsCollisionDetector = $termsCollisionDetector; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public static function factory(): self { |
|
63
|
|
|
$wikibaseRepo = WikibaseRepo::getDefaultInstance(); |
|
64
|
|
|
|
|
65
|
|
|
$settings = $wikibaseRepo->getSettings(); |
|
66
|
|
|
$copyrightView = new SpecialPageCopyrightView( |
|
67
|
|
|
new CopyrightMessageBuilder(), |
|
68
|
|
|
$settings->getSetting( 'dataRightsUrl' ), |
|
69
|
|
|
$settings->getSetting( 'dataRightsText' ) |
|
70
|
|
|
); |
|
71
|
|
|
|
|
72
|
|
|
return new self( |
|
73
|
|
|
$copyrightView, |
|
74
|
|
|
$wikibaseRepo->getEntityNamespaceLookup(), |
|
75
|
|
|
$wikibaseRepo->getSummaryFormatter(), |
|
76
|
|
|
$wikibaseRepo->getEntityTitleLookup(), |
|
77
|
|
|
$wikibaseRepo->newEditEntityFactory(), |
|
78
|
|
|
$wikibaseRepo->getPropertyTermsCollisionDetector() |
|
79
|
|
|
); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @see SpecialNewEntity::doesWrites |
|
84
|
|
|
* |
|
85
|
|
|
* @return bool |
|
86
|
|
|
*/ |
|
87
|
|
|
public function doesWrites() { |
|
88
|
|
|
return true; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @see SpecialNewEntity::createEntityFromFormData |
|
93
|
|
|
* |
|
94
|
|
|
* @param array $formData |
|
95
|
|
|
* |
|
96
|
|
|
* @return Property |
|
97
|
|
|
*/ |
|
98
|
|
|
protected function createEntityFromFormData( array $formData ) { |
|
99
|
|
|
$languageCode = $formData[ self::FIELD_LANG ]; |
|
100
|
|
|
|
|
101
|
|
|
$property = Property::newFromType( $formData[ self::FIELD_DATATYPE ] ); |
|
102
|
|
|
|
|
103
|
|
|
$property->setLabel( $languageCode, $formData[ self::FIELD_LABEL ] ); |
|
104
|
|
|
$property->setDescription( $languageCode, $formData[ self::FIELD_DESCRIPTION ] ); |
|
105
|
|
|
|
|
106
|
|
|
$property->setAliases( $languageCode, $formData[ self::FIELD_ALIASES ] ); |
|
107
|
|
|
|
|
108
|
|
|
return $property; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @param string $dataType |
|
113
|
|
|
* |
|
114
|
|
|
* @return bool |
|
115
|
|
|
*/ |
|
116
|
|
|
private function dataTypeExists( $dataType ) { |
|
117
|
|
|
$dataTypeFactory = WikibaseRepo::getDefaultInstance()->getDataTypeFactory(); |
|
118
|
|
|
|
|
119
|
|
|
return in_array( $dataType, $dataTypeFactory->getTypeIds() ); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @see SpecialNewEntity::getFormFields() |
|
124
|
|
|
* |
|
125
|
|
|
* @return array[] |
|
126
|
|
|
*/ |
|
127
|
|
|
protected function getFormFields() { |
|
128
|
|
|
$formFields = [ |
|
129
|
|
|
self::FIELD_LANG => [ |
|
130
|
|
|
'name' => self::FIELD_LANG, |
|
131
|
|
|
'class' => HTMLContentLanguageField::class, |
|
132
|
|
|
'id' => 'wb-newentity-language', |
|
133
|
|
|
], |
|
134
|
|
|
self::FIELD_LABEL => [ |
|
135
|
|
|
'name' => self::FIELD_LABEL, |
|
136
|
|
|
'default' => $this->parts[0] ?? '', |
|
137
|
|
|
'class' => HTMLTrimmedTextField::class, |
|
138
|
|
|
'id' => 'wb-newentity-label', |
|
139
|
|
|
'placeholder-message' => 'wikibase-label-edit-placeholder', |
|
140
|
|
|
'label-message' => 'wikibase-newentity-label' |
|
141
|
|
|
], |
|
142
|
|
|
self::FIELD_DESCRIPTION => [ |
|
143
|
|
|
'name' => self::FIELD_DESCRIPTION, |
|
144
|
|
|
'default' => $this->parts[1] ?? '', |
|
145
|
|
|
'class' => HTMLTrimmedTextField::class, |
|
146
|
|
|
'id' => 'wb-newentity-description', |
|
147
|
|
|
'placeholder-message' => 'wikibase-description-edit-placeholder', |
|
148
|
|
|
'label-message' => 'wikibase-newentity-description' |
|
149
|
|
|
], |
|
150
|
|
|
self::FIELD_ALIASES => [ |
|
151
|
|
|
'name' => self::FIELD_ALIASES, |
|
152
|
|
|
'class' => HTMLAliasesField::class, |
|
153
|
|
|
'id' => 'wb-newentity-aliases', |
|
154
|
|
|
] |
|
155
|
|
|
]; |
|
156
|
|
|
|
|
157
|
|
|
$dataTypeFactory = WikibaseRepo::getDefaultInstance()->getDataTypeFactory(); |
|
158
|
|
|
$selector = new DataTypeSelector( |
|
159
|
|
|
$dataTypeFactory->getTypes(), |
|
160
|
|
|
$this->getLanguage()->getCode() |
|
161
|
|
|
); |
|
162
|
|
|
|
|
163
|
|
|
$options = [ |
|
164
|
|
|
$this->msg( 'wikibase-newproperty-pick-data-type' )->text() => '' |
|
165
|
|
|
]; |
|
166
|
|
|
$formFields[ self::FIELD_DATATYPE ] = [ |
|
167
|
|
|
'name' => self::FIELD_DATATYPE, |
|
168
|
|
|
'type' => 'select', |
|
169
|
|
|
'default' => $this->parts[2] ?? '', |
|
170
|
|
|
'options' => array_merge( $options, $selector->getOptionsArray() ), |
|
171
|
|
|
'id' => 'wb-newproperty-datatype', |
|
172
|
|
|
'validation-callback' => function ( $dataType, $formData, $form ) { |
|
|
|
|
|
|
173
|
|
|
if ( !$this->dataTypeExists( $dataType ) ) { |
|
174
|
|
|
return [ $this->msg( 'wikibase-newproperty-invalid-datatype' )->text() ]; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
return true; |
|
178
|
|
|
}, |
|
179
|
|
|
'label-message' => 'wikibase-newproperty-datatype' |
|
180
|
|
|
]; |
|
181
|
|
|
|
|
182
|
|
|
return $formFields; |
|
|
|
|
|
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* @inheritDoc |
|
187
|
|
|
*/ |
|
188
|
|
|
protected function getLegend() { |
|
189
|
|
|
return $this->msg( 'wikibase-newproperty-fieldset' ); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* @see SpecialNewEntity::getWarnings |
|
194
|
|
|
* |
|
195
|
|
|
* @return string[] |
|
196
|
|
|
*/ |
|
197
|
|
|
protected function getWarnings() { |
|
198
|
|
|
if ( $this->getUser()->isAnon() ) { |
|
199
|
|
|
return [ |
|
200
|
|
|
$this->msg( |
|
201
|
|
|
'wikibase-anonymouseditwarning', |
|
202
|
|
|
$this->msg( 'wikibase-entity-property' ) |
|
203
|
|
|
)->parse(), |
|
204
|
|
|
]; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
return []; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* @param array $formData |
|
212
|
|
|
* |
|
213
|
|
|
* @return Status |
|
214
|
|
|
*/ |
|
215
|
|
|
protected function validateFormData( array $formData ) { |
|
216
|
|
|
if ( $formData[ self::FIELD_LABEL ] == '' |
|
217
|
|
|
&& $formData[ self::FIELD_DESCRIPTION ] == '' |
|
218
|
|
|
&& $formData[ self::FIELD_ALIASES ] === [] |
|
219
|
|
|
) { |
|
220
|
|
|
return Status::newFatal( 'wikibase-newproperty-insufficient-data' ); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
if ( $formData[ self::FIELD_LABEL ] !== '' && |
|
224
|
|
|
$formData[ self::FIELD_LABEL ] === $formData[ self::FIELD_DESCRIPTION ] |
|
225
|
|
|
) { |
|
226
|
|
|
return Status::newFatal( 'wikibase-newproperty-same-label-and-description' ); |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
$collidingPropertyId = $this->termsCollisionDetector->detectLabelCollision( |
|
230
|
|
|
$formData[ self::FIELD_LANG ], |
|
231
|
|
|
$formData[ self::FIELD_LABEL ] |
|
232
|
|
|
); |
|
233
|
|
|
if ( $collidingPropertyId !== null ) { |
|
234
|
|
|
return Status::newFatal( |
|
235
|
|
|
'wikibase-validator-label-conflict', |
|
236
|
|
|
$formData[ self::FIELD_LABEL ], |
|
237
|
|
|
$formData[ self::FIELD_LANG ], |
|
238
|
|
|
$collidingPropertyId |
|
239
|
|
|
); |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
return Status::newGood(); |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* @param Property $property |
|
247
|
|
|
* |
|
248
|
|
|
* @return Summary |
|
249
|
|
|
* @suppress PhanParamSignatureMismatch Uses intersection types |
|
250
|
|
|
*/ |
|
251
|
|
|
protected function createSummary( EntityDocument $property ) { |
|
252
|
|
|
$uiLanguageCode = $this->getLanguage()->getCode(); |
|
253
|
|
|
|
|
254
|
|
|
$summary = new Summary( 'wbeditentity', 'create' ); |
|
255
|
|
|
$summary->setLanguage( $uiLanguageCode ); |
|
256
|
|
|
/** @var Term|null $labelTerm */ |
|
257
|
|
|
$labelTerm = $property->getLabels()->getIterator()->current(); |
|
|
|
|
|
|
258
|
|
|
/** @var Term|null $descriptionTerm */ |
|
259
|
|
|
$descriptionTerm = $property->getDescriptions()->getIterator()->current(); |
|
|
|
|
|
|
260
|
|
|
$summary->addAutoSummaryArgs( |
|
261
|
|
|
$labelTerm ? $labelTerm->getText() : '', |
|
262
|
|
|
$descriptionTerm ? $descriptionTerm->getText() : '' |
|
263
|
|
|
); |
|
264
|
|
|
|
|
265
|
|
|
return $summary; |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
protected function displayBeforeForm( OutputPage $output ) { |
|
269
|
|
|
parent::displayBeforeForm( $output ); |
|
270
|
|
|
$output->addModules( 'wikibase.special.languageLabelDescriptionAliases' ); |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
/** |
|
274
|
|
|
* @inheritDoc |
|
275
|
|
|
*/ |
|
276
|
|
|
protected function getEntityType() { |
|
277
|
|
|
return Property::ENTITY_TYPE; |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
} |
|
281
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.