This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | declare( strict_types = 1 ); |
||
4 | |||
5 | namespace Wikibase\Repo\Api; |
||
6 | |||
7 | use ApiBase; |
||
8 | use ApiMain; |
||
9 | use ApiUsageException; |
||
10 | use Wikibase\DataModel\Services\Statement\StatementGuidParser; |
||
11 | use Wikibase\DataModel\Statement\Statement; |
||
12 | use Wikibase\Repo\ChangeOp\ChangeOp; |
||
13 | use Wikibase\Repo\ChangeOp\ChangeOpException; |
||
14 | use Wikibase\Repo\ChangeOp\ChangeOps; |
||
15 | use Wikibase\Repo\ChangeOp\StatementChangeOpFactory; |
||
16 | use Wikibase\Repo\WikibaseRepo; |
||
17 | |||
18 | /** |
||
19 | * API module for removing qualifiers from a statement. |
||
20 | * |
||
21 | * @license GPL-2.0-or-later |
||
22 | * @author Jeroen De Dauw < [email protected] > |
||
23 | * @author Tobias Gritschacher < [email protected] > |
||
24 | */ |
||
25 | class RemoveQualifiers extends ApiBase { |
||
26 | |||
27 | use FederatedPropertyApiValidatorTrait; |
||
28 | |||
29 | /** |
||
30 | * @var StatementChangeOpFactory |
||
31 | */ |
||
32 | private $statementChangeOpFactory; |
||
33 | |||
34 | /** |
||
35 | * @var ApiErrorReporter |
||
36 | */ |
||
37 | protected $errorReporter; |
||
38 | |||
39 | /** |
||
40 | * @var StatementModificationHelper |
||
41 | */ |
||
42 | private $modificationHelper; |
||
43 | |||
44 | /** |
||
45 | * @var StatementGuidParser |
||
46 | */ |
||
47 | private $guidParser; |
||
48 | |||
49 | /** |
||
50 | * @var ResultBuilder |
||
51 | */ |
||
52 | private $resultBuilder; |
||
53 | |||
54 | /** |
||
55 | * @var EntitySavingHelper |
||
56 | */ |
||
57 | private $entitySavingHelper; |
||
58 | |||
59 | public function __construct( |
||
60 | ApiMain $mainModule, |
||
61 | string $moduleName, |
||
62 | ApiErrorReporter $errorReporter, |
||
63 | StatementChangeOpFactory $statementChangeOpFactory, |
||
64 | StatementModificationHelper $modificationHelper, |
||
65 | StatementGuidParser $guidParser, |
||
66 | callable $resultBuilderInstantiator, |
||
67 | callable $entitySavingHelperInstantiator, |
||
68 | bool $federatedPropertiesEnabled |
||
69 | ) { |
||
70 | parent::__construct( $mainModule, $moduleName ); |
||
71 | |||
72 | $this->errorReporter = $errorReporter; |
||
73 | $this->statementChangeOpFactory = $statementChangeOpFactory; |
||
74 | $this->modificationHelper = $modificationHelper; |
||
75 | $this->guidParser = $guidParser; |
||
76 | $this->resultBuilder = $resultBuilderInstantiator( $this ); |
||
77 | $this->entitySavingHelper = $entitySavingHelperInstantiator( $this ); |
||
78 | $this->federatedPropertiesEnabled = $federatedPropertiesEnabled; |
||
79 | } |
||
80 | |||
81 | public static function factory( ApiMain $mainModule, string $moduleName ): self { |
||
82 | $wikibaseRepo = WikibaseRepo::getDefaultInstance(); |
||
83 | $apiHelperFactory = $wikibaseRepo->getApiHelperFactory( $mainModule->getContext() ); |
||
84 | $changeOpFactoryProvider = $wikibaseRepo->getChangeOpFactoryProvider(); |
||
85 | |||
86 | $modificationHelper = new StatementModificationHelper( |
||
87 | $wikibaseRepo->getSnakFactory(), |
||
88 | $wikibaseRepo->getEntityIdParser(), |
||
89 | $wikibaseRepo->getStatementGuidValidator(), |
||
90 | $apiHelperFactory->getErrorReporter( $mainModule ) |
||
91 | ); |
||
92 | |||
93 | return new self( |
||
94 | $mainModule, |
||
95 | $moduleName, |
||
96 | $apiHelperFactory->getErrorReporter( $mainModule ), |
||
97 | $changeOpFactoryProvider->getStatementChangeOpFactory(), |
||
98 | $modificationHelper, |
||
99 | $wikibaseRepo->getStatementGuidParser(), |
||
100 | function ( $module ) use ( $apiHelperFactory ) { |
||
101 | return $apiHelperFactory->getResultBuilder( $module ); |
||
102 | }, |
||
103 | function ( $module ) use ( $apiHelperFactory ) { |
||
104 | return $apiHelperFactory->getEntitySavingHelper( $module ); |
||
105 | }, |
||
106 | $wikibaseRepo->inFederatedPropertyMode() |
||
107 | ); |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * @inheritDoc |
||
112 | */ |
||
113 | public function execute(): void { |
||
114 | $params = $this->extractRequestParams(); |
||
115 | $this->validateParameters( $params ); |
||
116 | |||
117 | $guid = $params['claim']; |
||
118 | $entityId = $this->guidParser->parse( $guid )->getEntityId(); |
||
119 | |||
120 | $this->validateAlteringEntityById( $entityId ); |
||
121 | |||
122 | $entity = $this->entitySavingHelper->loadEntity( $entityId ); |
||
123 | |||
124 | $summary = $this->modificationHelper->createSummary( $params, $this ); |
||
125 | |||
126 | $statement = $this->modificationHelper->getStatementFromEntity( $guid, $entity ); |
||
127 | $qualifierHashes = $this->getQualifierHashesFromParams( $params, $statement ); |
||
128 | |||
129 | $changeOps = new ChangeOps(); |
||
130 | $changeOps->add( $this->getChangeOps( $guid, $qualifierHashes ) ); |
||
131 | |||
132 | try { |
||
133 | $changeOps->apply( $entity, $summary ); |
||
134 | } catch ( ChangeOpException $e ) { |
||
135 | $this->errorReporter->dieException( $e, 'failed-save' ); |
||
136 | } |
||
137 | |||
138 | $status = $this->entitySavingHelper->attemptSaveEntity( $entity, $summary ); |
||
139 | $this->resultBuilder->addRevisionIdFromStatusToResult( $status, 'pageinfo' ); |
||
140 | $this->resultBuilder->markSuccess(); |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * @param array $params |
||
145 | * |
||
146 | * @throws ApiUsageException |
||
147 | */ |
||
148 | private function validateParameters( array $params ): void { |
||
149 | if ( !( $this->modificationHelper->validateStatementGuid( $params['claim'] ) ) ) { |
||
150 | $this->errorReporter->dieError( 'Invalid claim guid', 'invalid-guid' ); |
||
0 ignored issues
–
show
|
|||
151 | } |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * @param string $statementGuid |
||
156 | * @param string[] $qualifierHashes |
||
157 | * |
||
158 | * @return ChangeOp[] |
||
159 | */ |
||
160 | private function getChangeOps( string $statementGuid, array $qualifierHashes ): array { |
||
161 | $changeOps = []; |
||
162 | |||
163 | foreach ( $qualifierHashes as $hash ) { |
||
164 | $changeOps[] = $this->statementChangeOpFactory->newRemoveQualifierOp( |
||
165 | $statementGuid, |
||
166 | $hash |
||
167 | ); |
||
168 | } |
||
169 | |||
170 | return $changeOps; |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * @param array $params |
||
175 | * @param Statement $statement |
||
176 | * |
||
177 | * @return string[] |
||
178 | */ |
||
179 | private function getQualifierHashesFromParams( array $params, Statement $statement ): array { |
||
180 | $qualifiers = $statement->getQualifiers(); |
||
181 | $hashes = []; |
||
182 | |||
183 | foreach ( array_unique( $params['qualifiers'] ) as $qualifierHash ) { |
||
184 | if ( !$qualifiers->hasSnakHash( $qualifierHash ) ) { |
||
185 | $this->errorReporter->dieError( 'Invalid snak hash', 'no-such-qualifier' ); |
||
0 ignored issues
–
show
The method
Wikibase\Repo\Api\ApiErrorReporter::dieError() has been deprecated with message: Use dieWithError() instead.
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. ![]() |
|||
186 | } |
||
187 | |||
188 | $hashes[] = $qualifierHash; |
||
189 | } |
||
190 | |||
191 | return $hashes; |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * @inheritDoc |
||
196 | */ |
||
197 | public function isWriteMode(): bool { |
||
198 | return true; |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * @see ApiBase::needsToken |
||
203 | * |
||
204 | * @return string |
||
205 | */ |
||
206 | public function needsToken(): string { |
||
207 | return 'csrf'; |
||
208 | } |
||
209 | |||
210 | /** |
||
211 | * @inheritDoc |
||
212 | */ |
||
213 | protected function getAllowedParams(): array { |
||
214 | return array_merge( |
||
215 | [ |
||
216 | 'claim' => [ |
||
217 | self::PARAM_TYPE => 'string', |
||
218 | self::PARAM_REQUIRED => true, |
||
219 | ], |
||
220 | 'qualifiers' => [ |
||
221 | self::PARAM_TYPE => 'string', |
||
222 | self::PARAM_REQUIRED => true, |
||
223 | self::PARAM_ISMULTI => true, |
||
224 | ], |
||
225 | 'summary' => [ |
||
226 | self::PARAM_TYPE => 'string', |
||
227 | ], |
||
228 | 'tags' => [ |
||
229 | self::PARAM_TYPE => 'tags', |
||
230 | self::PARAM_ISMULTI => true, |
||
231 | ], |
||
232 | 'token' => null, |
||
233 | 'baserevid' => [ |
||
234 | self::PARAM_TYPE => 'integer', |
||
235 | ], |
||
236 | 'bot' => false, |
||
237 | ], |
||
238 | parent::getAllowedParams() |
||
239 | ); |
||
240 | } |
||
241 | |||
242 | /** |
||
243 | * @inheritDoc |
||
244 | */ |
||
245 | protected function getExamplesMessages(): array { |
||
246 | return [ |
||
247 | 'action=wbremovequalifiers&claim=Q42$D8404CDA-25E4-4334-AF13-A3290BCD9C0F' |
||
248 | . '&references=1eb8793c002b1d9820c833d234a1b54c8e94187e&token=foobar' |
||
249 | . '&baserevid=7201010' |
||
250 | => 'apihelp-wbremovequalifiers-example-1', |
||
251 | ]; |
||
252 | } |
||
253 | |||
254 | } |
||
255 |
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.