ItemHandler::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 30
rs 9.44
c 0
b 0
f 0
cc 1
nc 1
nop 11

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace Wikibase\Repo\Content;
4
5
use Content;
6
use IContextSource;
7
use MediaWiki\Revision\SlotRenderingProvider;
8
use Page;
9
use Title;
10
use Wikibase\DataModel\Entity\EntityDocument;
11
use Wikibase\DataModel\Entity\EntityId;
12
use Wikibase\DataModel\Entity\EntityIdParser;
13
use Wikibase\DataModel\Entity\EntityRedirect;
14
use Wikibase\DataModel\Entity\Item;
15
use Wikibase\DataModel\Entity\ItemId;
16
use Wikibase\DataModel\Services\Lookup\PropertyDataTypeLookup;
17
use Wikibase\DataModel\Services\Lookup\PropertyDataTypeLookupException;
18
use Wikibase\DataModel\Statement\StatementList;
19
use Wikibase\Lib\Store\EntityContentDataCodec;
20
use Wikibase\Lib\Store\EntityIdLookup;
21
use Wikibase\Lib\Store\EntityTermStoreWriter;
22
use Wikibase\Lib\Store\LanguageFallbackLabelDescriptionLookupFactory;
23
use Wikibase\Lib\Store\SiteLinkStore;
24
use Wikibase\Repo\Actions\EditEntityAction;
25
use Wikibase\Repo\Actions\HistoryEntityAction;
26
use Wikibase\Repo\Actions\SubmitEntityAction;
27
use Wikibase\Repo\Actions\ViewEntityAction;
28
use Wikibase\Repo\Search\Fields\FieldDefinitions;
29
use Wikibase\Repo\Validators\EntityConstraintProvider;
30
use Wikibase\Repo\Validators\ValidatorErrorLocalizer;
31
32
/**
33
 * Content handler for Wikibase items.
34
 *
35
 * @license GPL-2.0-or-later
36
 * @author Jeroen De Dauw < [email protected] >
37
 * @author Daniel Kinzler
38
 * @author Adrian Heine <[email protected]>
39
 */
40
class ItemHandler extends EntityHandler {
41
42
	/**
43
	 * @var SiteLinkStore
44
	 */
45
	private $siteLinkStore;
46
47
	/**
48
	 * @var EntityIdLookup
49
	 */
50
	private $entityIdLookup;
51
52
	/**
53
	 * @var LanguageFallbackLabelDescriptionLookupFactory
54
	 */
55
	private $labelLookupFactory;
56
57
	/**
58
	 * @var PropertyDataTypeLookup
59
	 */
60
	private $dataTypeLookup;
61
62
	/**
63
	 * @var EntityTermStoreWriter
64
	 */
65
	private $entityTermStoreWriter;
66
67
	/**
68
	 * @param EntityTermStoreWriter $entityTermStoreWriter
69
	 * @param EntityContentDataCodec $contentCodec
70
	 * @param EntityConstraintProvider $constraintProvider
71
	 * @param ValidatorErrorLocalizer $errorLocalizer
72
	 * @param EntityIdParser $entityIdParser
73
	 * @param SiteLinkStore $siteLinkStore
74
	 * @param EntityIdLookup $entityIdLookup
75
	 * @param LanguageFallbackLabelDescriptionLookupFactory $labelLookupFactory
76
	 * @param FieldDefinitions $itemFieldDefinitions
77
	 * @param PropertyDataTypeLookup $dataTypeLookup
78
	 * @param callable|null $legacyExportFormatDetector
79
	 */
80
	public function __construct(
81
		EntityTermStoreWriter $entityTermStoreWriter,
82
		EntityContentDataCodec $contentCodec,
83
		EntityConstraintProvider $constraintProvider,
84
		ValidatorErrorLocalizer $errorLocalizer,
85
		EntityIdParser $entityIdParser,
86
		SiteLinkStore $siteLinkStore,
87
		EntityIdLookup $entityIdLookup,
88
		LanguageFallbackLabelDescriptionLookupFactory $labelLookupFactory,
89
		FieldDefinitions $itemFieldDefinitions,
90
		PropertyDataTypeLookup $dataTypeLookup,
91
		$legacyExportFormatDetector = null
92
	) {
93
		parent::__construct(
94
			ItemContent::CONTENT_MODEL_ID,
95
			null,
96
			$contentCodec,
97
			$constraintProvider,
98
			$errorLocalizer,
99
			$entityIdParser,
100
			$itemFieldDefinitions,
101
			$legacyExportFormatDetector
102
		);
103
104
		$this->entityIdLookup = $entityIdLookup;
105
		$this->labelLookupFactory = $labelLookupFactory;
106
		$this->siteLinkStore = $siteLinkStore;
107
		$this->dataTypeLookup = $dataTypeLookup;
108
		$this->entityTermStoreWriter = $entityTermStoreWriter;
109
	}
110
111
	/**
112
	 * @return (\Closure|class-string)[]
0 ignored issues
show
Documentation introduced by
The doc-type (\Closure|class-string)[] could not be parsed: Unknown type name "class-string" at position 10. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
113
	 */
114
	public function getActionOverrides() {
115
		return [
116
			'history' => function( Page $page, IContextSource $context ) {
117
				return new HistoryEntityAction(
118
					$page,
119
					$context,
120
					$this->entityIdLookup,
121
					$this->labelLookupFactory->newLabelDescriptionLookup( $context->getLanguage() )
122
				);
123
			},
124
			'view' => ViewEntityAction::class,
125
			'edit' => EditEntityAction::class,
126
			'submit' => SubmitEntityAction::class,
127
		];
128
	}
129
130
	/**
131
	 * @see EntityHandler::getSpecialPageForCreation
132
	 *
133
	 * @return string
134
	 */
135
	public function getSpecialPageForCreation() {
136
		return 'NewItem';
137
	}
138
139
	/**
140
	 * Returns Item::ENTITY_TYPE
141
	 *
142
	 * @return string
143
	 */
144
	public function getEntityType() {
145
		return Item::ENTITY_TYPE;
146
	}
147
148
	public function getSecondaryDataUpdates(
149
		Title $title,
150
		Content $content,
151
		$role,
152
		SlotRenderingProvider $slotOutput
153
	) {
154
		$updates = parent::getSecondaryDataUpdates( $title, $content, $role, $slotOutput );
155
156
		/** @var ItemContent $content */
157
		'@phan-var ItemContent $content';
158
		$id = $content->getEntityId();
159
160
		if ( $content->isRedirect() ) {
161
			$updates[] = new DataUpdateAdapter(
162
				[ $this->siteLinkStore, 'deleteLinksOfItem' ],
163
				$id
164
			);
165
			$updates[] = new DataUpdateAdapter(
166
				[ $this->entityTermStoreWriter, 'deleteTermsOfEntity' ],
167
				$id
168
			);
169
		} else {
170
			/** @var ItemContent $content */
171
			'@phan-var ItemContent $content';
172
			$item = $content->getItem();
173
174
			$updates[] = new DataUpdateAdapter(
175
				[ $this->entityTermStoreWriter, 'saveTermsOfEntity' ],
176
				$item
177
			);
178
179
			$updates[] = new DataUpdateAdapter(
180
				[ $this->siteLinkStore, 'saveLinksOfItem' ],
181
				$item
182
			);
183
		}
184
185
		return $updates;
186
	}
187
188
	public function getDeletionUpdates( Title $title, $role ) {
189
		$updates = parent::getDeletionUpdates( $title, $role );
190
191
		$id = $this->getIdForTitle( $title );
192
193
		// Unregister the entity from the term store.
194
		$updates[] = new DataUpdateAdapter(
195
			[ $this->entityTermStoreWriter, 'deleteTermsOfEntity' ],
196
			$id
197
		);
198
199
		$updates[] = new DataUpdateAdapter(
200
			[ $this->siteLinkStore, 'deleteLinksOfItem' ],
201
			$id
202
		);
203
204
		return $updates;
205
	}
206
207
	/**
208
	 * @see EntityHandler::makeEmptyEntity()
209
	 *
210
	 * @return EntityDocument
211
	 */
212
	public function makeEmptyEntity() {
213
		return new Item();
214
	}
215
216
	/**
217
	 * @see EntityHandler::makeEntityRedirectContent
218
	 *
219
	 * @param EntityRedirect $redirect
220
	 *
221
	 * @return ItemContent
222
	 */
223
	public function makeEntityRedirectContent( EntityRedirect $redirect ) {
224
		$title = $this->getTitleForId( $redirect->getTargetId() );
225
		return ItemContent::newFromRedirect( $redirect, $title );
226
	}
227
228
	/**
229
	 * @see EntityHandler::supportsRedirects
230
	 *
231
	 * @return bool Always true.
232
	 */
233
	public function supportsRedirects() {
234
		return true;
235
	}
236
237
	/**
238
	 * @see EntityHandler::newEntityContent
239
	 *
240
	 * @param EntityHolder|null $entityHolder
241
	 *
242
	 * @return ItemContent
243
	 */
244
	protected function newEntityContent( EntityHolder $entityHolder = null ) {
245
		return new ItemContent( $entityHolder );
246
	}
247
248
	/**
249
	 * @see EntityContent::makeEntityId
250
	 *
251
	 * @param string $id
252
	 *
253
	 * @return EntityId
254
	 */
255
	public function makeEntityId( $id ) {
256
		return new ItemId( $id );
257
	}
258
259
	/**
260
	 * @param StatementList $statementList
261
	 * @return int
262
	 */
263
	public function getIdentifiersCount( StatementList $statementList ) {
264
		$identifiers = 0;
265
		foreach ( $statementList->getPropertyIds() as $propertyIdSerialization => $propertyId ) {
266
			try {
267
				$dataType = $this->dataTypeLookup->getDataTypeIdForProperty( $propertyId );
268
			} catch ( PropertyDataTypeLookupException $e ) {
269
				continue;
270
			}
271
272
			if ( $dataType === 'external-id' ) {
273
				$identifiers += $statementList->getByPropertyId( $propertyId )->count();
274
			}
275
		}
276
277
		return $identifiers;
278
	}
279
280
}
281