SetClaimValue::getAllowedParams()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 9.424
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace Wikibase\Repo\Api;
6
7
use ApiBase;
8
use ApiMain;
9
use Wikibase\DataModel\Services\Statement\StatementGuidParser;
10
use Wikibase\Repo\ChangeOp\StatementChangeOpFactory;
11
use Wikibase\Repo\WikibaseRepo;
12
13
/**
14
 * API module for setting the DataValue contained by the main snak of a claim.
15
 *
16
 * @license GPL-2.0-or-later
17
 * @author Jeroen De Dauw < [email protected] >
18
 * @author Tobias Gritschacher < [email protected] >
19
 */
20
class SetClaimValue extends ApiBase {
21
22
	use FederatedPropertyApiValidatorTrait;
23
24
	/**
25
	 * @var StatementChangeOpFactory
26
	 */
27
	private $statementChangeOpFactory;
28
29
	/**
30
	 * @var ApiErrorReporter
31
	 */
32
	protected $errorReporter;
33
34
	/**
35
	 * @var StatementModificationHelper
36
	 */
37
	private $modificationHelper;
38
39
	/**
40
	 * @var StatementGuidParser
41
	 */
42
	private $guidParser;
43
44
	/**
45
	 * @var ResultBuilder
46
	 */
47
	private $resultBuilder;
48
49
	/**
50
	 * @var EntitySavingHelper
51
	 */
52
	private $entitySavingHelper;
53
54
	public function __construct(
55
		ApiMain $mainModule,
56
		string $moduleName,
57
		ApiErrorReporter $errorReporter,
58
		StatementChangeOpFactory $statementChangeOpFactory,
59
		StatementModificationHelper $modificationHelper,
60
		StatementGuidParser $guidParser,
61
		callable $resultBuilderInstantiator,
62
		callable $entitySavingHelperInstantiator,
63
		bool $federatedPropertiesEnabled
64
	) {
65
		parent::__construct( $mainModule, $moduleName );
66
67
		$this->errorReporter = $errorReporter;
68
		$this->statementChangeOpFactory = $statementChangeOpFactory;
69
		$this->modificationHelper = $modificationHelper;
70
		$this->guidParser = $guidParser;
71
		$this->resultBuilder = $resultBuilderInstantiator( $this );
72
		$this->entitySavingHelper = $entitySavingHelperInstantiator( $this );
73
		$this->federatedPropertiesEnabled = $federatedPropertiesEnabled;
74
	}
75
76
	public static function factory( ApiMain $mainModule, string $moduleName ): self {
77
		$wikibaseRepo = WikibaseRepo::getDefaultInstance();
78
		$apiHelperFactory = $wikibaseRepo->getApiHelperFactory( $mainModule->getContext() );
79
		$changeOpFactoryProvider = $wikibaseRepo->getChangeOpFactoryProvider();
80
81
		$modificationHelper = new StatementModificationHelper(
82
			$wikibaseRepo->getSnakFactory(),
83
			$wikibaseRepo->getEntityIdParser(),
84
			$wikibaseRepo->getStatementGuidValidator(),
85
			$apiHelperFactory->getErrorReporter( $mainModule )
86
		);
87
88
		return new self(
89
			$mainModule,
90
			$moduleName,
91
			$apiHelperFactory->getErrorReporter( $mainModule ),
92
			$changeOpFactoryProvider->getStatementChangeOpFactory(),
93
			$modificationHelper,
94
			$wikibaseRepo->getStatementGuidParser(),
95
			function ( $module ) use ( $apiHelperFactory ) {
96
				return $apiHelperFactory->getResultBuilder( $module );
97
			},
98
			function ( $module ) use ( $apiHelperFactory ) {
99
				return $apiHelperFactory->getEntitySavingHelper( $module );
100
			},
101
			$wikibaseRepo->inFederatedPropertyMode()
102
		);
103
	}
104
105
	/**
106
	 * @inheritDoc
107
	 */
108
	public function execute(): void {
109
		$params = $this->extractRequestParams();
110
		$this->validateParameters( $params );
111
112
		$this->logFeatureUsage( 'action=wbsetclaimvalue' );
113
114
		$guid = $params['claim'];
115
		$entityId = $this->guidParser->parse( $guid )->getEntityId();
116
		$this->validateAlteringEntityById( $entityId );
117
		$entity = $this->entitySavingHelper->loadEntity( $entityId );
118
119
		$claim = $this->modificationHelper->getStatementFromEntity( $guid, $entity );
120
121
		$snak = $this->modificationHelper->getSnakInstance( $params, $claim->getPropertyId() );
122
123
		$summary = $this->modificationHelper->createSummary( $params, $this );
124
125
		$changeOp = $this->statementChangeOpFactory->newSetMainSnakOp( $guid, $snak );
126
127
		$this->modificationHelper->applyChangeOp( $changeOp, $entity, $summary );
128
129
		$status = $this->entitySavingHelper->attemptSaveEntity( $entity, $summary );
130
		$this->resultBuilder->addRevisionIdFromStatusToResult( $status, 'pageinfo' );
131
		$this->resultBuilder->markSuccess();
132
		$this->resultBuilder->addStatement( $claim );
133
	}
134
135
	/**
136
	 * @param array $params
137
	 */
138
	private function validateParameters( array $params ): void {
139
		if ( !( $this->modificationHelper->validateStatementGuid( $params['claim'] ) ) ) {
140
			$this->errorReporter->dieError( 'Invalid claim guid', 'invalid-guid' );
0 ignored issues
show
Deprecated Code introduced by
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.

Loading history...
141
		}
142
	}
143
144
	/**
145
	 * @inheritDoc
146
	 */
147
	public function isWriteMode(): bool {
148
		return true;
149
	}
150
151
	/**
152
	 * @see ApiBase::needsToken
153
	 *
154
	 * @return string
155
	 */
156
	public function needsToken(): string {
157
		return 'csrf';
158
	}
159
160
	/**
161
	 * @inheritDoc
162
	 */
163
	protected function getAllowedParams(): array {
164
		return array_merge(
165
			[
166
				'claim' => [
167
					self::PARAM_TYPE => 'string',
168
					self::PARAM_REQUIRED => true,
169
				],
170
				'value' => [
171
					self::PARAM_TYPE => 'text',
172
					self::PARAM_REQUIRED => false,
173
				],
174
				'snaktype' => [
175
					self::PARAM_TYPE => [ 'value', 'novalue', 'somevalue' ],
176
					self::PARAM_REQUIRED => true,
177
				],
178
				'summary' => [
179
					self::PARAM_TYPE => 'string',
180
				],
181
				'tags' => [
182
					self::PARAM_TYPE => 'tags',
183
					self::PARAM_ISMULTI => true,
184
				],
185
				'token' => null,
186
				'baserevid' => [
187
					self::PARAM_TYPE => 'integer',
188
				],
189
				'bot' => false,
190
			],
191
			parent::getAllowedParams()
192
		);
193
	}
194
195
	/**
196
	 * @inheritDoc
197
	 */
198
	protected function getExamplesMessages(): array {
199
		return [
200
			'action=wbsetclaimvalue&claim=Q42$D8404CDA-25E4-4334-AF13-A3290BCD9C0F&snaktype=value'
201
				. '&value={"entity-type":"item","numeric-id":1}&token=foobar&baserevid=7201010'
202
				=> 'apihelp-wbsetclaimvalue-example-1',
203
		];
204
	}
205
206
}
207