Passed
Push — deepCloning ( 4428ab...80fdb4 )
by no
05:26
created

Entity::getAllAliases()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 16
rs 9.2
cc 4
eloc 7
nc 3
nop 1
1
<?php
2
3
namespace Wikibase\DataModel\Entity;
4
5
use Wikibase\DataModel\Term\AliasGroup;
6
use Wikibase\DataModel\Term\AliasGroupList;
7
use Wikibase\DataModel\Term\FingerprintHolder;
8
use Wikibase\DataModel\Term\TermList;
9
10
/**
11
 * Represents a single Wikibase entity.
12
 * See https://www.mediawiki.org/wiki/Wikibase/DataModel#Values
13
 *
14
 * @deprecated since 1.0 - do not type hint against Entity. See
15
 * https://lists.wikimedia.org/pipermail/wikidata-tech/2014-June/000489.html
16
 *
17
 * @licence GNU GPL v2+
18
 * @author Jeroen De Dauw < [email protected] >
19
 */
20
abstract class Entity implements FingerprintHolder, EntityDocument {
21
22
	/**
23
	 * Sets the value for the label in a certain value.
24
	 *
25
	 * @deprecated since 0.7.3 - use getFingerprint and setFingerprint
26
	 *
27
	 * @param string $languageCode
28
	 * @param string $value
29
	 */
30
	public function setLabel( $languageCode, $value ) {
31
		$this->getFingerprint()->setLabel( $languageCode, $value );
32
	}
33
34
	/**
35
	 * Sets the value for the description in a certain value.
36
	 *
37
	 * @deprecated since 0.7.3 - use getFingerprint and setFingerprint
38
	 *
39
	 * @param string $languageCode
40
	 * @param string $value
41
	 */
42
	public function setDescription( $languageCode, $value ) {
43
		$this->getFingerprint()->setDescription( $languageCode, $value );
44
	}
45
46
	/**
47
	 * Removes the labels in the specified languages.
48
	 *
49
	 * @deprecated since 0.7.3 - use getFingerprint and setFingerprint
50
	 *
51
	 * @param string $languageCode
52
	 */
53
	public function removeLabel( $languageCode ) {
54
		$this->getFingerprint()->removeLabel( $languageCode );
55
	}
56
57
	/**
58
	 * Removes the descriptions in the specified languages.
59
	 *
60
	 * @deprecated since 0.7.3 - use getFingerprint and setFingerprint
61
	 *
62
	 * @param string $languageCode
63
	 */
64
	public function removeDescription( $languageCode ) {
65
		$this->getFingerprint()->removeDescription( $languageCode );
66
	}
67
68
	/**
69
	 * Returns the aliases for the item in the language with the specified code.
70
	 *
71
	 * @deprecated since 0.7.3 - use getFingerprint and setFingerprint
72
	 *
73
	 * @param string $languageCode
74
	 *
75
	 * @return string[]
76
	 */
77
	public function getAliases( $languageCode ) {
78
		$aliases = $this->getFingerprint()->getAliasGroups();
79
80
		if ( $aliases->hasGroupForLanguage( $languageCode ) ) {
81
			return $aliases->getByLanguage( $languageCode )->getAliases();
82
		}
83
84
		return array();
85
	}
86
87
	/**
88
	 * Returns all the aliases for the item.
89
	 * The result is an array with language codes pointing to an array of aliases in the language they specify.
90
	 *
91
	 * @deprecated since 0.7.3 - use getFingerprint and setFingerprint
92
	 *
93
	 * @param string[]|null $languageCodes
94
	 *
95
	 * @return array[]
96
	 */
97
	public function getAllAliases( array $languageCodes = null ) {
98
		$aliases = $this->getFingerprint()->getAliasGroups();
99
100
		$textLists = array();
101
102
		/**
103
		 * @var AliasGroup $aliasGroup
104
		 */
105
		foreach ( $aliases as $languageCode => $aliasGroup ) {
106
			if ( $languageCodes === null || in_array( $languageCode, $languageCodes ) ) {
107
				$textLists[$languageCode] = $aliasGroup->getAliases();
108
			}
109
		}
110
111
		return $textLists;
112
	}
113
114
	/**
115
	 * Sets the aliases for the item in the language with the specified code.
116
	 *
117
	 * @deprecated since 0.7.3 - use getFingerprint and setFingerprint
118
	 *
119
	 * @param string $languageCode
120
	 * @param string[] $aliases
121
	 */
122
	public function setAliases( $languageCode, array $aliases ) {
123
		$this->getFingerprint()->setAliasGroup( $languageCode, $aliases );
124
	}
125
126
	/**
127
	 * Add the provided aliases to the aliases list of the item in the language with the specified code.
128
	 *
129
	 * @deprecated since 0.7.3 - use getFingerprint and setFingerprint
130
	 *
131
	 * @param string $languageCode
132
	 * @param string[] $aliases
133
	 */
134
	public function addAliases( $languageCode, array $aliases ) {
135
		$this->setAliases(
0 ignored issues
show
Deprecated Code introduced by
The method Wikibase\DataModel\Entity\Entity::setAliases() has been deprecated with message: since 0.7.3 - use getFingerprint and setFingerprint

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...
136
			$languageCode,
137
			array_merge(
138
				$this->getAliases( $languageCode ),
0 ignored issues
show
Deprecated Code introduced by
The method Wikibase\DataModel\Entity\Entity::getAliases() has been deprecated with message: since 0.7.3 - use getFingerprint and setFingerprint

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...
139
				$aliases
140
			)
141
		);
142
	}
143
144
	/**
145
	 * Removed the provided aliases from the aliases list of the item in the language with the specified code.
146
	 *
147
	 * @deprecated since 0.7.3 - use getFingerprint and setFingerprint
148
	 *
149
	 * @param string $languageCode
150
	 * @param string[] $aliases
151
	 */
152
	public function removeAliases( $languageCode, array $aliases ) {
153
		$this->setAliases(
0 ignored issues
show
Deprecated Code introduced by
The method Wikibase\DataModel\Entity\Entity::setAliases() has been deprecated with message: since 0.7.3 - use getFingerprint and setFingerprint

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...
154
			$languageCode,
155
			array_diff(
156
				$this->getAliases( $languageCode ),
0 ignored issues
show
Deprecated Code introduced by
The method Wikibase\DataModel\Entity\Entity::getAliases() has been deprecated with message: since 0.7.3 - use getFingerprint and setFingerprint

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...
157
				$aliases
158
			)
159
		);
160
	}
161
162
	/**
163
	 * Returns the descriptions of the entity in the provided languages.
164
	 *
165
	 * @deprecated since 0.7.3 - use getFingerprint and setFingerprint
166
	 *
167
	 * @param string[]|null $languageCodes Note that an empty array gives
168
	 * descriptions for no languages while a null pointer gives all
169
	 *
170
	 * @return string[] Found descriptions in given languages
171
	 */
172
	public function getDescriptions( array $languageCodes = null ) {
173
		return $this->getMultilangTexts( 'description', $languageCodes );
174
	}
175
176
	/**
177
	 * Returns the labels of the entity in the provided languages.
178
	 *
179
	 * @deprecated since 0.7.3 - use getFingerprint and setFingerprint
180
	 *
181
	 * @param string[]|null $languageCodes Note that an empty array gives
182
	 * labels for no languages while a null pointer gives all
183
	 *
184
	 * @return string[] Found labels in given languages
185
	 */
186
	public function getLabels( array $languageCodes = null ) {
187
		return $this->getMultilangTexts( 'label', $languageCodes );
188
	}
189
190
	/**
191
	 * Returns the description of the entity in the language with the provided code,
192
	 * or false in cases there is none in this language.
193
	 *
194
	 * @deprecated since 0.7.3 - use getFingerprint and setFingerprint
195
	 *
196
	 * @param string $languageCode
197
	 *
198
	 * @return string|bool
199
	 */
200
	public function getDescription( $languageCode ) {
201
		if ( !$this->getFingerprint()->hasDescription( $languageCode ) ) {
202
			return false;
203
		}
204
205
		return $this->getFingerprint()->getDescription( $languageCode )->getText();
206
	}
207
208
	/**
209
	 * Returns the label of the entity in the language with the provided code,
210
	 * or false in cases there is none in this language.
211
	 *
212
	 * @deprecated since 0.7.3 - use getFingerprint and setFingerprint
213
	 *
214
	 * @param string $languageCode
215
	 *
216
	 * @return string|bool
217
	 */
218
	public function getLabel( $languageCode ) {
219
		if ( !$this->getFingerprint()->hasLabel( $languageCode ) ) {
220
			return false;
221
		}
222
223
		return $this->getFingerprint()->getLabel( $languageCode )->getText();
224
	}
225
226
	/**
227
	 * Get texts from an item with a field specifier.
228
	 *
229
	 * @param string $fieldKey
230
	 * @param string[]|null $languageCodes
231
	 *
232
	 * @return string[]
233
	 */
234
	private function getMultilangTexts( $fieldKey, array $languageCodes = null ) {
235
		if ( $fieldKey === 'label' ) {
236
			$textList = $this->getFingerprint()->getLabels()->toTextArray();
237
		} else {
238
			$textList = $this->getFingerprint()->getDescriptions()->toTextArray();
239
		}
240
241
		if ( $languageCodes !== null ) {
242
			$textList = array_intersect_key( $textList, array_flip( $languageCodes ) );
243
		}
244
245
		return $textList;
246
	}
247
248
	/**
249
	 * Replaces the currently set labels with the provided ones.
250
	 * The labels are provided as an associative array where the keys are
251
	 * language codes pointing to the label in that language.
252
	 *
253
	 * @since 0.4
254
	 * @deprecated since 0.7.3 - use getFingerprint and setFingerprint
255
	 *
256
	 * @param string[] $labels
257
	 */
258
	public function setLabels( array $labels ) {
259
		$this->getFingerprint()->setLabels( new TermList() );
260
261
		foreach ( $labels as $languageCode => $labelText ) {
262
			$this->setLabel( $languageCode, $labelText );
0 ignored issues
show
Deprecated Code introduced by
The method Wikibase\DataModel\Entity\Entity::setLabel() has been deprecated with message: since 0.7.3 - use getFingerprint and setFingerprint

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...
263
		}
264
	}
265
266
	/**
267
	 * Replaces the currently set descriptions with the provided ones.
268
	 * The descriptions are provided as an associative array where the keys are
269
	 * language codes pointing to the description in that language.
270
	 *
271
	 * @since 0.4
272
	 * @deprecated since 0.7.3 - use getFingerprint and setFingerprint
273
	 *
274
	 * @param string[] $descriptions
275
	 */
276
	public function setDescriptions( array $descriptions ) {
277
		$this->getFingerprint()->setDescriptions( new TermList() );
278
279
		foreach ( $descriptions as $languageCode => $descriptionText ) {
280
			$this->setDescription( $languageCode, $descriptionText );
0 ignored issues
show
Deprecated Code introduced by
The method Wikibase\DataModel\Entity\Entity::setDescription() has been deprecated with message: since 0.7.3 - use getFingerprint and setFingerprint

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...
281
		}
282
	}
283
284
	/**
285
	 * Replaces the currently set aliases with the provided ones.
286
	 * The aliases are provided as an associative array where the keys are
287
	 * language codes pointing to an array value that holds the aliases
288
	 * in that language.
289
	 *
290
	 * @since 0.4
291
	 * @deprecated since 0.7.3 - use getFingerprint and setFingerprint
292
	 *
293
	 * @param array[] $aliasLists
294
	 */
295
	public function setAllAliases( array $aliasLists ) {
296
		$this->getFingerprint()->setAliasGroups( new AliasGroupList() );
297
298
		foreach ( $aliasLists as $languageCode => $aliasList ) {
299
			$this->setAliases( $languageCode, $aliasList );
0 ignored issues
show
Deprecated Code introduced by
The method Wikibase\DataModel\Entity\Entity::setAliases() has been deprecated with message: since 0.7.3 - use getFingerprint and setFingerprint

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...
300
		}
301
	}
302
303
	/**
304
	 * @see EntityDocument::copy
305
	 *
306
	 * @since 0.1
307
	 *
308
	 * @return self
309
	 */
310
	public function copy() {
311
		return unserialize( serialize( $this ) );
312
	}
313
314
	/**
315
	 * Removes all content from the Entity.
316
	 * The id is not part of the content.
317
	 *
318
	 * @since 0.1
319
	 * @deprecated since 1.0
320
	 */
321
	public abstract function clear();
322
323
}
324