|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare( strict_types = 1 ); |
|
4
|
|
|
|
|
5
|
|
|
namespace Wikibase\Repo\Api; |
|
6
|
|
|
|
|
7
|
|
|
use ApiMain; |
|
8
|
|
|
use ApiUsageException; |
|
9
|
|
|
use IBufferingStatsdDataFactory; |
|
10
|
|
|
use Wikibase\DataModel\Entity\EntityDocument; |
|
11
|
|
|
use Wikibase\DataModel\Term\AliasesProvider; |
|
12
|
|
|
use Wikibase\Lib\Summary; |
|
13
|
|
|
use Wikibase\Repo\ChangeOp\ChangeOp; |
|
14
|
|
|
use Wikibase\Repo\ChangeOp\ChangeOps; |
|
15
|
|
|
use Wikibase\Repo\ChangeOp\FingerprintChangeOpFactory; |
|
16
|
|
|
use Wikibase\Repo\WikibaseRepo; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* API module to set the aliases for a Wikibase entity. |
|
20
|
|
|
* Requires API write mode to be enabled. |
|
21
|
|
|
* |
|
22
|
|
|
* @license GPL-2.0-or-later |
|
23
|
|
|
*/ |
|
24
|
|
|
class SetAliases extends ModifyEntity { |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var FingerprintChangeOpFactory |
|
28
|
|
|
*/ |
|
29
|
|
|
private $termChangeOpFactory; |
|
30
|
|
|
|
|
31
|
|
|
/** @var IBufferingStatsdDataFactory */ |
|
32
|
|
|
private $stats; |
|
33
|
|
|
|
|
34
|
|
|
public function __construct( |
|
35
|
|
|
ApiMain $mainModule, |
|
36
|
|
|
string $moduleName, |
|
37
|
|
|
FingerprintChangeOpFactory $termChangeOpFactory, |
|
38
|
|
|
IBufferingStatsdDataFactory $stats, |
|
39
|
|
|
bool $federatedPropertiesEnabled |
|
40
|
|
|
) { |
|
41
|
|
|
parent::__construct( $mainModule, $moduleName, $federatedPropertiesEnabled ); |
|
42
|
|
|
$this->termChangeOpFactory = $termChangeOpFactory; |
|
43
|
|
|
$this->stats = $stats; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public static function factory( ApiMain $mainModule, string $moduleName, IBufferingStatsdDataFactory $stats ): self { |
|
47
|
|
|
$wikibaseRepo = WikibaseRepo::getDefaultInstance(); |
|
48
|
|
|
return new self( |
|
49
|
|
|
$mainModule, |
|
50
|
|
|
$moduleName, |
|
51
|
|
|
$wikibaseRepo->getChangeOpFactoryProvider() |
|
52
|
|
|
->getFingerprintChangeOpFactory(), |
|
53
|
|
|
$stats, |
|
54
|
|
|
$wikibaseRepo->inFederatedPropertyMode() |
|
55
|
|
|
); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @see ApiBase::needsToken |
|
60
|
|
|
* |
|
61
|
|
|
* @return string |
|
62
|
|
|
*/ |
|
63
|
|
|
public function needsToken(): string { |
|
64
|
|
|
return 'csrf'; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @see ApiBase::isWriteMode() |
|
69
|
|
|
* |
|
70
|
|
|
* @return bool Always true. |
|
71
|
|
|
*/ |
|
72
|
|
|
public function isWriteMode(): bool { |
|
73
|
|
|
return true; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @see ModifyEntity::validateParameters |
|
78
|
|
|
* |
|
79
|
|
|
* @param array $params |
|
80
|
|
|
* |
|
81
|
|
|
* @throws ApiUsageException |
|
82
|
|
|
*/ |
|
83
|
|
|
protected function validateParameters( array $params ): void { |
|
84
|
|
|
parent::validateParameters( $params ); |
|
85
|
|
|
|
|
86
|
|
|
if ( !( ( !empty( $params['add'] ) || !empty( $params['remove'] ) ) |
|
87
|
|
|
xor isset( $params['set'] ) |
|
88
|
|
|
) ) { |
|
89
|
|
|
$this->errorReporter->dieError( |
|
|
|
|
|
|
90
|
|
|
"Parameters 'add' and 'remove' are not allowed to be set when parameter 'set' is provided", |
|
91
|
|
|
'invalid-list' |
|
92
|
|
|
); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
private function adjustSummary( Summary $summary, array $params, AliasesProvider $entity ): void { |
|
97
|
|
|
if ( !empty( $params['add'] ) && !empty( $params['remove'] ) ) { |
|
98
|
|
|
$language = $params['language']; |
|
99
|
|
|
|
|
100
|
|
|
$aliasGroups = $entity->getAliasGroups(); |
|
101
|
|
|
|
|
102
|
|
|
$summary->setAction( 'update' ); |
|
103
|
|
|
$summary->setLanguage( $language ); |
|
104
|
|
|
|
|
105
|
|
|
// Get the full list of current aliases |
|
106
|
|
|
if ( $aliasGroups->hasGroupForLanguage( $language ) ) { |
|
107
|
|
|
$aliases = $aliasGroups->getByLanguage( $language )->getAliases(); |
|
108
|
|
|
$summary->addAutoSummaryArgs( $aliases ); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
protected function modifyEntity( EntityDocument $entity, ChangeOp $changeOp, array $preparedParameters ): Summary { |
|
114
|
|
|
if ( !( $entity instanceof AliasesProvider ) ) { |
|
115
|
|
|
$this->errorReporter->dieError( 'The given entity cannot contain aliases', 'not-supported' ); |
|
|
|
|
|
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
$language = $preparedParameters['language']; |
|
119
|
|
|
|
|
120
|
|
|
// FIXME: if we have ADD and REMOVE operations in the same call, |
|
121
|
|
|
// we will also have two ChangeOps updating the same edit summary. |
|
122
|
|
|
// This will cause the edit summary to be overwritten by the last ChangeOp being applied. |
|
123
|
|
|
$this->stats->increment( 'wikibase.repo.api.wbsetaliases.total' ); |
|
124
|
|
|
if ( !empty( $preparedParameters['add'] ) && !empty( $preparedParameters['remove'] ) ) { |
|
125
|
|
|
$this->stats->increment( 'wikibase.repo.api.wbsetaliases.addremove' ); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
$summary = $this->createSummary( $preparedParameters ); |
|
129
|
|
|
|
|
130
|
|
|
$this->applyChangeOp( $changeOp, $entity, $summary ); |
|
131
|
|
|
|
|
132
|
|
|
$this->adjustSummary( $summary, $preparedParameters, $entity ); |
|
133
|
|
|
|
|
134
|
|
|
$aliasGroups = $entity->getAliasGroups(); |
|
|
|
|
|
|
135
|
|
|
|
|
136
|
|
|
if ( $aliasGroups->hasGroupForLanguage( $language ) ) { |
|
137
|
|
|
$aliasGroupList = $aliasGroups->getWithLanguages( [ $language ] ); |
|
138
|
|
|
$this->getResultBuilder()->addAliasGroupList( $aliasGroupList, 'entity' ); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
return $summary; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @param string[] $aliases |
|
146
|
|
|
* |
|
147
|
|
|
* @return string[] |
|
148
|
|
|
*/ |
|
149
|
|
|
private function normalizeAliases( array $aliases ): array { |
|
150
|
|
|
$stringNormalizer = $this->stringNormalizer; |
|
151
|
|
|
|
|
152
|
|
|
$aliases = array_map( |
|
153
|
|
|
function( $str ) use ( $stringNormalizer ) { |
|
154
|
|
|
return $stringNormalizer->trimToNFC( $str ); |
|
155
|
|
|
}, |
|
156
|
|
|
$aliases |
|
157
|
|
|
); |
|
158
|
|
|
|
|
159
|
|
|
$aliases = array_filter( |
|
160
|
|
|
$aliases, |
|
161
|
|
|
function( $str ) { |
|
162
|
|
|
return $str !== ''; |
|
163
|
|
|
} |
|
164
|
|
|
); |
|
165
|
|
|
|
|
166
|
|
|
return $aliases; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
protected function getChangeOp( array $preparedParameters, EntityDocument $entity ): ChangeOp { |
|
170
|
|
|
$changeOps = []; |
|
171
|
|
|
$language = $preparedParameters['language']; |
|
172
|
|
|
|
|
173
|
|
|
// Set the list of aliases to a user given one OR add/ remove certain entries |
|
174
|
|
|
if ( isset( $preparedParameters['set'] ) ) { |
|
175
|
|
|
$changeOps[] = |
|
176
|
|
|
$this->termChangeOpFactory->newSetAliasesOp( |
|
177
|
|
|
$language, |
|
178
|
|
|
$this->normalizeAliases( $preparedParameters['set'] ) |
|
179
|
|
|
); |
|
180
|
|
|
} else { |
|
181
|
|
|
// FIXME: if we have ADD and REMOVE operations in the same call, |
|
182
|
|
|
// we will also have two ChangeOps updating the same edit summary. |
|
183
|
|
|
// This will cause the edit summary to be overwritten by the last ChangeOp beeing applied. |
|
184
|
|
|
if ( !empty( $preparedParameters['add'] ) ) { |
|
185
|
|
|
$changeOps[] = |
|
186
|
|
|
$this->termChangeOpFactory->newAddAliasesOp( |
|
187
|
|
|
$language, |
|
188
|
|
|
$this->normalizeAliases( $preparedParameters['add'] ) |
|
189
|
|
|
); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
if ( !empty( $preparedParameters['remove'] ) ) { |
|
193
|
|
|
$changeOps[] = |
|
194
|
|
|
$this->termChangeOpFactory->newRemoveAliasesOp( |
|
195
|
|
|
$language, |
|
196
|
|
|
$this->normalizeAliases( $preparedParameters['remove'] ) |
|
197
|
|
|
); |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
return $this->termChangeOpFactory->newFingerprintChangeOp( new ChangeOps( $changeOps ) ); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* @inheritDoc |
|
206
|
|
|
*/ |
|
207
|
|
|
protected function getAllowedParams(): array { |
|
208
|
|
|
return array_merge( |
|
209
|
|
|
parent::getAllowedParams(), |
|
210
|
|
|
[ |
|
211
|
|
|
'add' => [ |
|
212
|
|
|
self::PARAM_TYPE => 'string', |
|
213
|
|
|
self::PARAM_ISMULTI => true, |
|
214
|
|
|
], |
|
215
|
|
|
'remove' => [ |
|
216
|
|
|
self::PARAM_TYPE => 'string', |
|
217
|
|
|
self::PARAM_ISMULTI => true, |
|
218
|
|
|
], |
|
219
|
|
|
'set' => [ |
|
220
|
|
|
self::PARAM_TYPE => 'string', |
|
221
|
|
|
self::PARAM_ISMULTI => true, |
|
222
|
|
|
], |
|
223
|
|
|
'language' => [ |
|
224
|
|
|
self::PARAM_TYPE => WikibaseRepo::getDefaultInstance()->getTermsLanguages()->getLanguages(), |
|
225
|
|
|
self::PARAM_REQUIRED => true, |
|
226
|
|
|
], |
|
227
|
|
|
'new' => [ |
|
228
|
|
|
self::PARAM_TYPE => $this->getEntityTypesWithAliases(), |
|
229
|
|
|
], |
|
230
|
|
|
] |
|
231
|
|
|
); |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
protected function getEntityTypesWithAliases(): array { |
|
235
|
|
|
// TODO inject me |
|
236
|
|
|
$entityFactory = WikibaseRepo::getDefaultInstance()->getEntityFactory(); |
|
237
|
|
|
$supportedEntityTypes = []; |
|
238
|
|
|
foreach ( $this->enabledEntityTypes as $entityType ) { |
|
239
|
|
|
$testEntity = $entityFactory->newEmpty( $entityType ); |
|
240
|
|
|
if ( $testEntity instanceof AliasesProvider ) { |
|
241
|
|
|
$supportedEntityTypes[] = $entityType; |
|
242
|
|
|
} |
|
243
|
|
|
} |
|
244
|
|
|
return $supportedEntityTypes; |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* @inheritDoc |
|
249
|
|
|
*/ |
|
250
|
|
|
protected function getExamplesMessages(): array { |
|
251
|
|
|
return [ |
|
252
|
|
|
'action=wbsetaliases&language=en&id=Q1&set=Foo|Bar' |
|
253
|
|
|
=> 'apihelp-wbsetaliases-example-1', |
|
254
|
|
|
|
|
255
|
|
|
'action=wbsetaliases&language=en&id=Q1&add=Foo|Bar' |
|
256
|
|
|
=> 'apihelp-wbsetaliases-example-2', |
|
257
|
|
|
|
|
258
|
|
|
'action=wbsetaliases&language=en&id=Q1&remove=Foo|Bar' |
|
259
|
|
|
=> 'apihelp-wbsetaliases-example-3', |
|
260
|
|
|
|
|
261
|
|
|
'action=wbsetaliases&language=en&id=Q1&remove=Foo&add=Bar' |
|
262
|
|
|
=> 'apihelp-wbsetaliases-example-4', |
|
263
|
|
|
]; |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
} |
|
267
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.