@@ -13,150 +13,150 @@ discard block |
||
13 | 13 | |
14 | 14 | class Item { |
15 | 15 | |
16 | - const PROP_INSTANCE_OF = 'P31'; |
|
17 | - const PROP_TITLE = 'P1476'; |
|
18 | - const PROP_IMAGE = 'P18'; |
|
19 | - const PROP_AUTHOR = 'P50'; |
|
20 | - |
|
21 | - /** @var string */ |
|
22 | - protected $id; |
|
23 | - |
|
24 | - /** @var MediawikiApi */ |
|
25 | - protected $wdApi; |
|
26 | - |
|
27 | - /** @var string */ |
|
28 | - protected $lang; |
|
29 | - |
|
30 | - /** @var CacheItemPoolInterface */ |
|
31 | - protected $cache; |
|
32 | - |
|
33 | - /** @var string The base URL of Wikidata, with trailing slash. */ |
|
34 | - protected $wikidataUrlBase = 'https://www.wikidata.org/wiki/'; |
|
35 | - |
|
36 | - private function __construct( $id, $lang, CacheItemPoolInterface $cache ) { |
|
37 | - if ( !is_string( $id ) || preg_match( '/[QP][0-9]*/i', $id ) !== 1 ) { |
|
38 | - throw new Exception( "Not a valid ID: " . var_export( $id, true ) ); |
|
39 | - } |
|
40 | - $this->id = $id; |
|
41 | - $this->wdApi = new MediawikiApi( 'https://www.wikidata.org/w/api.php' ); |
|
42 | - $this->entities = []; |
|
43 | - $this->lang = $lang; |
|
44 | - $this->cache = $cache; |
|
45 | - } |
|
46 | - |
|
47 | - /** |
|
48 | - * Create a new Item object with class based on the item's 'instance of' statement. |
|
49 | - * |
|
50 | - * @param string $id |
|
51 | - * @param string $lang |
|
52 | - * |
|
53 | - * @return Item |
|
54 | - */ |
|
55 | - public static function factory( $id, $lang, CacheItemPoolInterface $cache ) { |
|
56 | - $item = new Item( $id, $lang, $cache ); |
|
57 | - foreach ( $item->getPropertyOfTypeItem( self::PROP_INSTANCE_OF ) as $instanceOf ) { |
|
58 | - // Try to find a class mating the 'instance of' name. |
|
59 | - $possibleClassName = __NAMESPACE__ . '\\Items\\' . Str::toCamelCase( $instanceOf->getItem()->getLabel() ); |
|
60 | - if ( class_exists( $possibleClassName ) ) { |
|
61 | - // This won't re-request the metadata, because that's cached. |
|
62 | - $specificItem = new $possibleClassName( $id, $lang, $cache ); |
|
63 | - return $specificItem; |
|
64 | - } |
|
65 | - } |
|
66 | - |
|
67 | - // If we're here, just leave it as a basic Item. |
|
68 | - $item->setCache( $cache ); |
|
69 | - return $item; |
|
70 | - } |
|
71 | - |
|
72 | - public function setCache( CacheItemPoolInterface $cache_item_pool ) { |
|
73 | - $this->cache = $cache_item_pool; |
|
74 | - } |
|
75 | - |
|
76 | - /** |
|
77 | - * Get the ID (Q-number) of this item. |
|
78 | - * @return string|bool The ID or false if it couldn't be determined. |
|
79 | - */ |
|
80 | - public function getId() { |
|
81 | - $entity = $this->getEntity( $this->id ); |
|
82 | - return isset( $entity['id'] ) ? $entity['id'] : false; |
|
83 | - } |
|
84 | - |
|
85 | - /** |
|
86 | - * Get this item's label. |
|
87 | - * @return string |
|
88 | - */ |
|
89 | - public function getLabel() { |
|
90 | - $entity = $this->getEntity( $this->id ); |
|
91 | - if ( ! empty( $entity['labels'][ $this->lang ]['value'] ) ) { |
|
92 | - // Use the label if there is one. |
|
93 | - return $entity['labels'][ $this->lang ]['value']; |
|
94 | - } |
|
95 | - // Or just use the ID. |
|
96 | - return $entity['id']; |
|
97 | - } |
|
98 | - |
|
99 | - public function getWikidataUrl() { |
|
100 | - return $this->wikidataUrlBase.$this->id; |
|
101 | - } |
|
102 | - |
|
103 | - /** |
|
104 | - * Wikiprojects list their properties like this: |
|
105 | - * |
|
106 | - * {{List of properties/Header}} |
|
107 | - * {{List of properties/Row|id=31|example-subject=Q923767|example-object=Q3331189}} |
|
108 | - * </table> |
|
109 | - * |
|
110 | - * @param string $wikiProject |
|
111 | - * @param string $type |
|
112 | - * @return array |
|
113 | - */ |
|
114 | - public function getStandardProperties( $wikiProject = 'WikiProject_Books', $type = 'work' ) { |
|
115 | - if ( $type !== 'work' ) { |
|
116 | - $type = 'edition'; |
|
117 | - } |
|
118 | - $cacheKey = $type . '_item_property_IDs'; |
|
119 | - if ( $this->cache->hasItem( $cacheKey ) ) { |
|
120 | - $propIds = $this->cache->getItem( $cacheKey )->get(); |
|
121 | - } else { |
|
122 | - $domCrawler = new Crawler(); |
|
123 | - $wikiProjectUrl = 'https://www.wikidata.org/wiki/Wikidata:' . $wikiProject; |
|
124 | - $domCrawler->addHtmlContent( file_get_contents( $wikiProjectUrl ) ); |
|
125 | - $propAncors = "//h3/span[@id='" . ucfirst( $type ) . "_item_properties']/../following-sibling::table[1]//td[2]/a"; |
|
126 | - $propCells = $domCrawler->filterXPath( $propAncors ); |
|
127 | - $propIds = []; |
|
128 | - $propCells->each( function ( Crawler $node, $i ) use ( &$propIds ) { |
|
129 | - $propId = $node->text(); |
|
130 | - $propIds[] = $propId; |
|
131 | - } ); |
|
132 | - $cacheItem = $this->cache->getItem( $cacheKey ) |
|
133 | - ->expiresAfter( new DateInterval( 'PT1H' ) ) |
|
134 | - ->set( $propIds ); |
|
135 | - $this->cache->save( $cacheItem ); |
|
136 | - } |
|
137 | - $workProperties = []; |
|
138 | - foreach ( $propIds as $propId ) { |
|
139 | - $workProperties[] = self::factory( $propId, $this->lang, $this->cache ); |
|
140 | - } |
|
141 | - |
|
142 | - return $workProperties; |
|
143 | - } |
|
144 | - |
|
145 | - /** |
|
146 | - * @param string $propertyId |
|
147 | - * @return bool|Time[] |
|
148 | - */ |
|
149 | - protected function getPropertyOfTypeTime( $propertyId ) { |
|
150 | - $times = []; |
|
151 | - $entity = $this->getEntity(); |
|
152 | - if ( !isset( $entity['claims'][$propertyId] ) ) { |
|
153 | - // No statements for this property. |
|
154 | - return $times; |
|
155 | - } |
|
156 | - // print_r($entity['claims'][$propertyId]);exit(); |
|
157 | - foreach ( $entity['claims'][$propertyId] as $claim ) { |
|
158 | - // print_r($claim); |
|
159 | - $times[] = new Time( $claim, $this->lang, $this->cache ); |
|
16 | + const PROP_INSTANCE_OF = 'P31'; |
|
17 | + const PROP_TITLE = 'P1476'; |
|
18 | + const PROP_IMAGE = 'P18'; |
|
19 | + const PROP_AUTHOR = 'P50'; |
|
20 | + |
|
21 | + /** @var string */ |
|
22 | + protected $id; |
|
23 | + |
|
24 | + /** @var MediawikiApi */ |
|
25 | + protected $wdApi; |
|
26 | + |
|
27 | + /** @var string */ |
|
28 | + protected $lang; |
|
29 | + |
|
30 | + /** @var CacheItemPoolInterface */ |
|
31 | + protected $cache; |
|
32 | + |
|
33 | + /** @var string The base URL of Wikidata, with trailing slash. */ |
|
34 | + protected $wikidataUrlBase = 'https://www.wikidata.org/wiki/'; |
|
35 | + |
|
36 | + private function __construct( $id, $lang, CacheItemPoolInterface $cache ) { |
|
37 | + if ( !is_string( $id ) || preg_match( '/[QP][0-9]*/i', $id ) !== 1 ) { |
|
38 | + throw new Exception( "Not a valid ID: " . var_export( $id, true ) ); |
|
39 | + } |
|
40 | + $this->id = $id; |
|
41 | + $this->wdApi = new MediawikiApi( 'https://www.wikidata.org/w/api.php' ); |
|
42 | + $this->entities = []; |
|
43 | + $this->lang = $lang; |
|
44 | + $this->cache = $cache; |
|
45 | + } |
|
46 | + |
|
47 | + /** |
|
48 | + * Create a new Item object with class based on the item's 'instance of' statement. |
|
49 | + * |
|
50 | + * @param string $id |
|
51 | + * @param string $lang |
|
52 | + * |
|
53 | + * @return Item |
|
54 | + */ |
|
55 | + public static function factory( $id, $lang, CacheItemPoolInterface $cache ) { |
|
56 | + $item = new Item( $id, $lang, $cache ); |
|
57 | + foreach ( $item->getPropertyOfTypeItem( self::PROP_INSTANCE_OF ) as $instanceOf ) { |
|
58 | + // Try to find a class mating the 'instance of' name. |
|
59 | + $possibleClassName = __NAMESPACE__ . '\\Items\\' . Str::toCamelCase( $instanceOf->getItem()->getLabel() ); |
|
60 | + if ( class_exists( $possibleClassName ) ) { |
|
61 | + // This won't re-request the metadata, because that's cached. |
|
62 | + $specificItem = new $possibleClassName( $id, $lang, $cache ); |
|
63 | + return $specificItem; |
|
64 | + } |
|
65 | + } |
|
66 | + |
|
67 | + // If we're here, just leave it as a basic Item. |
|
68 | + $item->setCache( $cache ); |
|
69 | + return $item; |
|
70 | + } |
|
71 | + |
|
72 | + public function setCache( CacheItemPoolInterface $cache_item_pool ) { |
|
73 | + $this->cache = $cache_item_pool; |
|
74 | + } |
|
75 | + |
|
76 | + /** |
|
77 | + * Get the ID (Q-number) of this item. |
|
78 | + * @return string|bool The ID or false if it couldn't be determined. |
|
79 | + */ |
|
80 | + public function getId() { |
|
81 | + $entity = $this->getEntity( $this->id ); |
|
82 | + return isset( $entity['id'] ) ? $entity['id'] : false; |
|
83 | + } |
|
84 | + |
|
85 | + /** |
|
86 | + * Get this item's label. |
|
87 | + * @return string |
|
88 | + */ |
|
89 | + public function getLabel() { |
|
90 | + $entity = $this->getEntity( $this->id ); |
|
91 | + if ( ! empty( $entity['labels'][ $this->lang ]['value'] ) ) { |
|
92 | + // Use the label if there is one. |
|
93 | + return $entity['labels'][ $this->lang ]['value']; |
|
94 | + } |
|
95 | + // Or just use the ID. |
|
96 | + return $entity['id']; |
|
97 | + } |
|
98 | + |
|
99 | + public function getWikidataUrl() { |
|
100 | + return $this->wikidataUrlBase.$this->id; |
|
101 | + } |
|
102 | + |
|
103 | + /** |
|
104 | + * Wikiprojects list their properties like this: |
|
105 | + * |
|
106 | + * {{List of properties/Header}} |
|
107 | + * {{List of properties/Row|id=31|example-subject=Q923767|example-object=Q3331189}} |
|
108 | + * </table> |
|
109 | + * |
|
110 | + * @param string $wikiProject |
|
111 | + * @param string $type |
|
112 | + * @return array |
|
113 | + */ |
|
114 | + public function getStandardProperties( $wikiProject = 'WikiProject_Books', $type = 'work' ) { |
|
115 | + if ( $type !== 'work' ) { |
|
116 | + $type = 'edition'; |
|
117 | + } |
|
118 | + $cacheKey = $type . '_item_property_IDs'; |
|
119 | + if ( $this->cache->hasItem( $cacheKey ) ) { |
|
120 | + $propIds = $this->cache->getItem( $cacheKey )->get(); |
|
121 | + } else { |
|
122 | + $domCrawler = new Crawler(); |
|
123 | + $wikiProjectUrl = 'https://www.wikidata.org/wiki/Wikidata:' . $wikiProject; |
|
124 | + $domCrawler->addHtmlContent( file_get_contents( $wikiProjectUrl ) ); |
|
125 | + $propAncors = "//h3/span[@id='" . ucfirst( $type ) . "_item_properties']/../following-sibling::table[1]//td[2]/a"; |
|
126 | + $propCells = $domCrawler->filterXPath( $propAncors ); |
|
127 | + $propIds = []; |
|
128 | + $propCells->each( function ( Crawler $node, $i ) use ( &$propIds ) { |
|
129 | + $propId = $node->text(); |
|
130 | + $propIds[] = $propId; |
|
131 | + } ); |
|
132 | + $cacheItem = $this->cache->getItem( $cacheKey ) |
|
133 | + ->expiresAfter( new DateInterval( 'PT1H' ) ) |
|
134 | + ->set( $propIds ); |
|
135 | + $this->cache->save( $cacheItem ); |
|
136 | + } |
|
137 | + $workProperties = []; |
|
138 | + foreach ( $propIds as $propId ) { |
|
139 | + $workProperties[] = self::factory( $propId, $this->lang, $this->cache ); |
|
140 | + } |
|
141 | + |
|
142 | + return $workProperties; |
|
143 | + } |
|
144 | + |
|
145 | + /** |
|
146 | + * @param string $propertyId |
|
147 | + * @return bool|Time[] |
|
148 | + */ |
|
149 | + protected function getPropertyOfTypeTime( $propertyId ) { |
|
150 | + $times = []; |
|
151 | + $entity = $this->getEntity(); |
|
152 | + if ( !isset( $entity['claims'][$propertyId] ) ) { |
|
153 | + // No statements for this property. |
|
154 | + return $times; |
|
155 | + } |
|
156 | + // print_r($entity['claims'][$propertyId]);exit(); |
|
157 | + foreach ( $entity['claims'][$propertyId] as $claim ) { |
|
158 | + // print_r($claim); |
|
159 | + $times[] = new Time( $claim, $this->lang, $this->cache ); |
|
160 | 160 | // |
161 | 161 | // $timeValue = $claim['datavalue']['value']['time']; |
162 | 162 | // // Ugly workaround for imprecise dates. :-( |
@@ -166,283 +166,283 @@ discard block |
||
166 | 166 | // } |
167 | 167 | // $time = strtotime($timeValue); |
168 | 168 | // return date($dateFormat, $time); |
169 | - // } |
|
170 | - } |
|
171 | - return $times; |
|
172 | - } |
|
173 | - |
|
174 | - /** |
|
175 | - * Get the Item that is referred to by the specified item's property. |
|
176 | - * |
|
177 | - * @param string $propertyId |
|
178 | - * |
|
179 | - * @return \Samwilson\SimpleWikidata\Properties\Item[] |
|
180 | - */ |
|
181 | - protected function getPropertyOfTypeItem( $propertyId ) { |
|
182 | - $entity = $this->getEntity( $this->id ); |
|
183 | - if ( !isset( $entity['claims'][$propertyId] ) ) { |
|
184 | - return []; |
|
185 | - } |
|
186 | - $items = []; |
|
187 | - foreach ( $entity['claims'][$propertyId] as $claim ) { |
|
188 | - $items[] = new Properties\Item( $claim, $this->lang, $this->cache ); |
|
189 | - } |
|
190 | - |
|
191 | - return $items; |
|
192 | - } |
|
193 | - |
|
194 | - public function setPropertyOfTypeItem( $property, $itemId ) { |
|
195 | - $itemIdNumeric = substr( $itemId, 1 ); |
|
196 | - |
|
197 | - // First see if this property already exists, and that it is different from what's being set. |
|
198 | - $entity = $this->getEntity( $this->id ); |
|
199 | - if ( !empty( $entity['claims'][$property] ) ) { |
|
200 | - // Get the first claim, and update it if necessary. |
|
201 | - $claim = array_shift( $entity['claims'][$property] ); |
|
202 | - if ( $claim['mainsnak']['datavalue']['value']['id'] == $itemId ) { |
|
203 | - // Already is the required value, no need to change. |
|
204 | - return; |
|
205 | - } |
|
206 | - $claim['mainsnak']['datavalue']['value']['id'] = $itemId; |
|
207 | - $claim['mainsnak']['datavalue']['value']['numeric-id'] = $itemIdNumeric; |
|
208 | - $apiParams = [ |
|
209 | - 'action' => 'wbsetclaim', |
|
210 | - 'claim' => json_encode( $claim ), |
|
211 | - ]; |
|
212 | - } |
|
213 | - |
|
214 | - // If no claim was found (and modified) above, create a new claim. |
|
215 | - if ( !isset( $apiParams ) ) { |
|
216 | - $apiParams = [ |
|
217 | - 'action' => 'wbcreateclaim', |
|
218 | - 'entity' => $this->getId(), |
|
219 | - 'property' => $property, |
|
220 | - 'snaktype' => 'value', |
|
221 | - 'value' => json_encode( [ 'entity-type' => 'item', 'numeric-id' => $itemIdNumeric ] ), |
|
222 | - ]; |
|
223 | - } |
|
224 | - |
|
225 | - // Save the property. |
|
226 | - $wdWpOauth = new WdOauth(); |
|
227 | - $wdWpOauth->makeCall( $apiParams, true ); |
|
228 | - |
|
229 | - // Clear the cache. |
|
230 | - $this->cache->deleteItem( $this->getEntityCacheKey( $this->id ) ); |
|
231 | - } |
|
232 | - |
|
233 | - public function getPropertyOfTypeUrl( $entityId, $propertyId ) { |
|
234 | - $entity = $this->getEntity( $entityId ); |
|
235 | - if ( !isset( $entity['claims'][$propertyId] ) ) { |
|
236 | - return false; |
|
237 | - } |
|
238 | - $urls = []; |
|
239 | - foreach ( $entity['claims'][$propertyId] as $claim ) { |
|
240 | - $urls[] = $claim['mainsnak']['datavalue']['value']; |
|
241 | - } |
|
242 | - |
|
243 | - return $urls; |
|
244 | - } |
|
245 | - |
|
246 | - public function getPropertyOfTypeExternalIdentifier( $entityId, $propertyId ) { |
|
247 | - $entity = $this->getEntity( $entityId ); |
|
248 | - if ( !isset( $entity['claims'][$propertyId] ) ) { |
|
249 | - return false; |
|
250 | - } |
|
251 | - $idents = []; |
|
252 | - foreach ( $entity['claims'][$propertyId] as $claim ) { |
|
253 | - $qualifiers = []; |
|
254 | - if ( !isset( $claim['qualifiers'] ) ) { |
|
255 | - continue; |
|
256 | - } |
|
257 | - foreach ( $claim['qualifiers'] as $qualsInfo ) { |
|
258 | - foreach ( $qualsInfo as $qualInfo ) { |
|
259 | - $qualProp = self::factory( $qualInfo['property'], $this->lang, $this->cache ); |
|
260 | - $propLabel = $qualProp->getLabel(); |
|
261 | - if ( !isset( $qualifiers[$propLabel] ) ) { |
|
262 | - $qualifiers[$propLabel] = []; |
|
263 | - } |
|
264 | - $qualifiers[$propLabel][] = $qualInfo['datavalue']['value']; |
|
265 | - } |
|
266 | - } |
|
267 | - $idents[] = [ |
|
268 | - 'qualifiers' => $qualifiers, |
|
269 | - 'value' => $claim['mainsnak']['datavalue']['value'], |
|
270 | - ]; |
|
271 | - } |
|
272 | - |
|
273 | - return $idents; |
|
274 | - } |
|
275 | - |
|
276 | - /** |
|
277 | - * Get a single-valued text property. |
|
278 | - * @param string $property One of the PROP_* constants. |
|
279 | - * @return string|bool The value, or false if it can't be found. |
|
280 | - */ |
|
281 | - public function getPropertyOfTypeText( $property ) { |
|
282 | - $entity = $this->getEntity( $this->id ); |
|
283 | - if ( isset( $entity['claims'][$property] ) ) { |
|
284 | - // Use the first title. |
|
285 | - foreach ( $entity['claims'][$property] as $t ) { |
|
286 | - if ( !isset( $t['mainsnak']['datavalue']['value']['language'] ) ) { |
|
287 | - var_dump( $t['mainsnak']['datavalue']['value'] ); |
|
288 | - exit(); |
|
289 | - } |
|
290 | - if ( $t['mainsnak']['datavalue']['value']['language'] == $this->lang |
|
291 | - && !empty( $t['mainsnak']['datavalue']['value']['text'] ) |
|
292 | - ) { |
|
293 | - return $t['mainsnak']['datavalue']['value']['text']; |
|
294 | - } |
|
295 | - } |
|
296 | - } |
|
297 | - return false; |
|
298 | - } |
|
299 | - |
|
300 | - /** |
|
301 | - * Literal data field for a quantity that relates to some kind of well-defined unit. The actual unit goes in the data values that is entered. |
|
302 | - * - amount – implicit part of the string (mapping of unit prefix is unclear) |
|
303 | - * - unit – implicit part of the string that defaults to "1" (mapping to standardizing body is unclear) |
|
304 | - * - upperbound - quantity's upper bound |
|
305 | - * - lowerbound - quantity's lower bound |
|
306 | - * @param $property |
|
307 | - * @return mixed[]|bool If it's not false it's an array with 'amount', 'unit', etc. |
|
308 | - */ |
|
309 | - public function getPropertyOfTypeQuantity( $property ) { |
|
310 | - $quantities = []; |
|
311 | - $entity = $this->getEntity( $this->id ); |
|
312 | - if ( !isset( $entity['claims'][$property] ) ) { |
|
313 | - return false; |
|
314 | - } |
|
315 | - foreach ( $entity['claims'][$property] as $t ) { |
|
316 | - $quantity = $t['mainsnak']['datavalue']['value']; |
|
317 | - $unitId = substr( $quantity['unit'], strlen( $this->wikidataUrlBase ) + 1 ); |
|
318 | - $quantity['unit'] = self::factory( $unitId, $this->lang, $this->cache ); |
|
319 | - $quantities[] = $quantity; |
|
320 | - } |
|
321 | - return $quantities; |
|
322 | - } |
|
323 | - |
|
324 | - /** |
|
325 | - * Set a single-valued text property. |
|
326 | - * @param string $property One of the PROP_* constants. |
|
327 | - * @param string $value The value. |
|
328 | - */ |
|
329 | - public function setPropertyOfTypeText( $property, $value ) { |
|
330 | - // First see if this property already exists, and that it is different from what's being set. |
|
331 | - $entity = $this->getEntity( $this->id ); |
|
332 | - if ( !empty( $entity['claims'][$property] ) ) { |
|
333 | - // Find this language's claim (if there is one). |
|
334 | - foreach ( $entity['claims'][$property] as $claim ) { |
|
335 | - if ( $claim['mainsnak']['datavalue']['value']['language'] == $this->lang ) { |
|
336 | - // Modify this claim's text value. |
|
337 | - $titleClaim = $claim; |
|
338 | - $titleClaim['mainsnak']['datavalue']['value']['text'] = $value; |
|
339 | - $setTitleParams = [ |
|
340 | - 'action' => 'wbsetclaim', |
|
341 | - 'claim' => \GuzzleHttp\json_encode( $titleClaim ), |
|
342 | - ]; |
|
343 | - continue; |
|
344 | - } |
|
345 | - } |
|
346 | - } |
|
347 | - |
|
348 | - // If no claim was found (and modified) above, create a new claim. |
|
349 | - if ( !isset( $setTitleParams ) ) { |
|
350 | - $setTitleParams = [ |
|
351 | - 'action' => 'wbcreateclaim', |
|
352 | - 'entity' => $this->getId(), |
|
353 | - 'property' => $property, |
|
354 | - 'snaktype' => 'value', |
|
355 | - 'value' => \GuzzleHttp\json_encode( [ 'text' => $value, 'language' => $this->lang ] ), |
|
356 | - ]; |
|
357 | - } |
|
358 | - |
|
359 | - // Save the property. |
|
360 | - $wdWpOauth = new WdWpOauth(); |
|
361 | - $wdWpOauth->makeCall( $setTitleParams, true ); |
|
362 | - |
|
363 | - // Clear the cache. |
|
364 | - $this->cache->deleteItem( $this->getEntityCacheKey( $this->id ) ); |
|
365 | - } |
|
366 | - |
|
367 | - public function getInstanceOf() { |
|
368 | - $instancesOf = $this->getPropertyOfTypeItem( $this->getId(), self::PROP_INSTANCE_OF ); |
|
369 | - return array_shift( $instancesOf ); |
|
370 | - } |
|
371 | - |
|
372 | - /** |
|
373 | - * Does this item exist? |
|
374 | - * @return bool |
|
375 | - */ |
|
376 | - public function exists() { |
|
377 | - return $this->getId() !== false; |
|
378 | - } |
|
379 | - |
|
380 | - public function getWikipediaIntro() { |
|
381 | - $cacheKey = 'wikipedia-intro-' . $this->id . $this->lang; |
|
382 | - if ( $this->cache->hasItem( $cacheKey ) ) { |
|
383 | - return $this->cache->getItem( $cacheKey )->get(); |
|
384 | - } |
|
385 | - $entity = $this->getEntity( $this->id ); |
|
386 | - if ( !isset( $entity['sitelinks'] ) ) { |
|
387 | - return []; |
|
388 | - } |
|
389 | - foreach ( $entity['sitelinks'] as $sitelink ) { |
|
390 | - if ( $sitelink['site'] == $this->lang . 'wiki' ) { |
|
391 | - $api = new MediawikiApi( 'https://' . $this->lang . '.wikipedia.org/w/api.php' ); |
|
392 | - $req = new SimpleRequest( 'query', [ |
|
393 | - 'prop' => 'extracts', |
|
394 | - 'exintro' => true, |
|
395 | - 'titles' => $sitelink['title'], |
|
396 | - ] ); |
|
397 | - $response = $api->getRequest( $req ); |
|
398 | - $page = array_shift( $response['query']['pages'] ); |
|
399 | - $out = [ |
|
400 | - 'title' => $page['title'], |
|
401 | - 'html' => $page['extract'], |
|
402 | - ]; |
|
403 | - $cacheItem = $this->cache->getItem( $cacheKey ) |
|
404 | - ->expiresAfter( new DateInterval( 'P1D' ) ) |
|
405 | - ->set( $out ); |
|
406 | - $this->cache->save( $cacheItem ); |
|
407 | - |
|
408 | - return $out; |
|
409 | - } |
|
410 | - } |
|
411 | - |
|
412 | - return []; |
|
413 | - } |
|
414 | - |
|
415 | - /** |
|
416 | - * Get the raw entity data from the 'wbgetentities' API call. |
|
417 | - * @param string $id |
|
418 | - * @param bool $ignoreCache |
|
419 | - * @return bool |
|
420 | - */ |
|
421 | - public function getEntity( $id = null, $ignoreCache = false ) { |
|
422 | - $idActual = $id ?: $this->id; |
|
423 | - $cacheKey = $this->getEntityCacheKey( $idActual ); |
|
424 | - if ( !$ignoreCache && $this->cache->hasItem( $cacheKey ) ) { |
|
425 | - return $this->cache->getItem( $cacheKey )->get(); |
|
426 | - } |
|
427 | - $metadataRequest = new SimpleRequest( 'wbgetentities', [ 'ids' => $idActual ] ); |
|
428 | - $itemResult = $this->wdApi->getRequest( $metadataRequest ); |
|
429 | - if ( !isset( $itemResult['success'] ) || !isset( $itemResult['entities'][$id] ) ) { |
|
430 | - return false; |
|
431 | - } |
|
432 | - $metadata = $itemResult['entities'][$idActual]; |
|
433 | - $cacheItem = $this->cache->getItem( $cacheKey ) |
|
434 | - ->expiresAfter( new DateInterval( 'PT10M' ) ) |
|
435 | - ->set( $metadata ); |
|
436 | - $this->cache->save( $cacheItem ); |
|
437 | - return $metadata; |
|
438 | - } |
|
439 | - |
|
440 | - /** |
|
441 | - * @param $id |
|
442 | - * |
|
443 | - * @return string |
|
444 | - */ |
|
445 | - protected function getEntityCacheKey( $id ) { |
|
446 | - return 'entities' . $id; |
|
447 | - } |
|
169 | + // } |
|
170 | + } |
|
171 | + return $times; |
|
172 | + } |
|
173 | + |
|
174 | + /** |
|
175 | + * Get the Item that is referred to by the specified item's property. |
|
176 | + * |
|
177 | + * @param string $propertyId |
|
178 | + * |
|
179 | + * @return \Samwilson\SimpleWikidata\Properties\Item[] |
|
180 | + */ |
|
181 | + protected function getPropertyOfTypeItem( $propertyId ) { |
|
182 | + $entity = $this->getEntity( $this->id ); |
|
183 | + if ( !isset( $entity['claims'][$propertyId] ) ) { |
|
184 | + return []; |
|
185 | + } |
|
186 | + $items = []; |
|
187 | + foreach ( $entity['claims'][$propertyId] as $claim ) { |
|
188 | + $items[] = new Properties\Item( $claim, $this->lang, $this->cache ); |
|
189 | + } |
|
190 | + |
|
191 | + return $items; |
|
192 | + } |
|
193 | + |
|
194 | + public function setPropertyOfTypeItem( $property, $itemId ) { |
|
195 | + $itemIdNumeric = substr( $itemId, 1 ); |
|
196 | + |
|
197 | + // First see if this property already exists, and that it is different from what's being set. |
|
198 | + $entity = $this->getEntity( $this->id ); |
|
199 | + if ( !empty( $entity['claims'][$property] ) ) { |
|
200 | + // Get the first claim, and update it if necessary. |
|
201 | + $claim = array_shift( $entity['claims'][$property] ); |
|
202 | + if ( $claim['mainsnak']['datavalue']['value']['id'] == $itemId ) { |
|
203 | + // Already is the required value, no need to change. |
|
204 | + return; |
|
205 | + } |
|
206 | + $claim['mainsnak']['datavalue']['value']['id'] = $itemId; |
|
207 | + $claim['mainsnak']['datavalue']['value']['numeric-id'] = $itemIdNumeric; |
|
208 | + $apiParams = [ |
|
209 | + 'action' => 'wbsetclaim', |
|
210 | + 'claim' => json_encode( $claim ), |
|
211 | + ]; |
|
212 | + } |
|
213 | + |
|
214 | + // If no claim was found (and modified) above, create a new claim. |
|
215 | + if ( !isset( $apiParams ) ) { |
|
216 | + $apiParams = [ |
|
217 | + 'action' => 'wbcreateclaim', |
|
218 | + 'entity' => $this->getId(), |
|
219 | + 'property' => $property, |
|
220 | + 'snaktype' => 'value', |
|
221 | + 'value' => json_encode( [ 'entity-type' => 'item', 'numeric-id' => $itemIdNumeric ] ), |
|
222 | + ]; |
|
223 | + } |
|
224 | + |
|
225 | + // Save the property. |
|
226 | + $wdWpOauth = new WdOauth(); |
|
227 | + $wdWpOauth->makeCall( $apiParams, true ); |
|
228 | + |
|
229 | + // Clear the cache. |
|
230 | + $this->cache->deleteItem( $this->getEntityCacheKey( $this->id ) ); |
|
231 | + } |
|
232 | + |
|
233 | + public function getPropertyOfTypeUrl( $entityId, $propertyId ) { |
|
234 | + $entity = $this->getEntity( $entityId ); |
|
235 | + if ( !isset( $entity['claims'][$propertyId] ) ) { |
|
236 | + return false; |
|
237 | + } |
|
238 | + $urls = []; |
|
239 | + foreach ( $entity['claims'][$propertyId] as $claim ) { |
|
240 | + $urls[] = $claim['mainsnak']['datavalue']['value']; |
|
241 | + } |
|
242 | + |
|
243 | + return $urls; |
|
244 | + } |
|
245 | + |
|
246 | + public function getPropertyOfTypeExternalIdentifier( $entityId, $propertyId ) { |
|
247 | + $entity = $this->getEntity( $entityId ); |
|
248 | + if ( !isset( $entity['claims'][$propertyId] ) ) { |
|
249 | + return false; |
|
250 | + } |
|
251 | + $idents = []; |
|
252 | + foreach ( $entity['claims'][$propertyId] as $claim ) { |
|
253 | + $qualifiers = []; |
|
254 | + if ( !isset( $claim['qualifiers'] ) ) { |
|
255 | + continue; |
|
256 | + } |
|
257 | + foreach ( $claim['qualifiers'] as $qualsInfo ) { |
|
258 | + foreach ( $qualsInfo as $qualInfo ) { |
|
259 | + $qualProp = self::factory( $qualInfo['property'], $this->lang, $this->cache ); |
|
260 | + $propLabel = $qualProp->getLabel(); |
|
261 | + if ( !isset( $qualifiers[$propLabel] ) ) { |
|
262 | + $qualifiers[$propLabel] = []; |
|
263 | + } |
|
264 | + $qualifiers[$propLabel][] = $qualInfo['datavalue']['value']; |
|
265 | + } |
|
266 | + } |
|
267 | + $idents[] = [ |
|
268 | + 'qualifiers' => $qualifiers, |
|
269 | + 'value' => $claim['mainsnak']['datavalue']['value'], |
|
270 | + ]; |
|
271 | + } |
|
272 | + |
|
273 | + return $idents; |
|
274 | + } |
|
275 | + |
|
276 | + /** |
|
277 | + * Get a single-valued text property. |
|
278 | + * @param string $property One of the PROP_* constants. |
|
279 | + * @return string|bool The value, or false if it can't be found. |
|
280 | + */ |
|
281 | + public function getPropertyOfTypeText( $property ) { |
|
282 | + $entity = $this->getEntity( $this->id ); |
|
283 | + if ( isset( $entity['claims'][$property] ) ) { |
|
284 | + // Use the first title. |
|
285 | + foreach ( $entity['claims'][$property] as $t ) { |
|
286 | + if ( !isset( $t['mainsnak']['datavalue']['value']['language'] ) ) { |
|
287 | + var_dump( $t['mainsnak']['datavalue']['value'] ); |
|
288 | + exit(); |
|
289 | + } |
|
290 | + if ( $t['mainsnak']['datavalue']['value']['language'] == $this->lang |
|
291 | + && !empty( $t['mainsnak']['datavalue']['value']['text'] ) |
|
292 | + ) { |
|
293 | + return $t['mainsnak']['datavalue']['value']['text']; |
|
294 | + } |
|
295 | + } |
|
296 | + } |
|
297 | + return false; |
|
298 | + } |
|
299 | + |
|
300 | + /** |
|
301 | + * Literal data field for a quantity that relates to some kind of well-defined unit. The actual unit goes in the data values that is entered. |
|
302 | + * - amount – implicit part of the string (mapping of unit prefix is unclear) |
|
303 | + * - unit – implicit part of the string that defaults to "1" (mapping to standardizing body is unclear) |
|
304 | + * - upperbound - quantity's upper bound |
|
305 | + * - lowerbound - quantity's lower bound |
|
306 | + * @param $property |
|
307 | + * @return mixed[]|bool If it's not false it's an array with 'amount', 'unit', etc. |
|
308 | + */ |
|
309 | + public function getPropertyOfTypeQuantity( $property ) { |
|
310 | + $quantities = []; |
|
311 | + $entity = $this->getEntity( $this->id ); |
|
312 | + if ( !isset( $entity['claims'][$property] ) ) { |
|
313 | + return false; |
|
314 | + } |
|
315 | + foreach ( $entity['claims'][$property] as $t ) { |
|
316 | + $quantity = $t['mainsnak']['datavalue']['value']; |
|
317 | + $unitId = substr( $quantity['unit'], strlen( $this->wikidataUrlBase ) + 1 ); |
|
318 | + $quantity['unit'] = self::factory( $unitId, $this->lang, $this->cache ); |
|
319 | + $quantities[] = $quantity; |
|
320 | + } |
|
321 | + return $quantities; |
|
322 | + } |
|
323 | + |
|
324 | + /** |
|
325 | + * Set a single-valued text property. |
|
326 | + * @param string $property One of the PROP_* constants. |
|
327 | + * @param string $value The value. |
|
328 | + */ |
|
329 | + public function setPropertyOfTypeText( $property, $value ) { |
|
330 | + // First see if this property already exists, and that it is different from what's being set. |
|
331 | + $entity = $this->getEntity( $this->id ); |
|
332 | + if ( !empty( $entity['claims'][$property] ) ) { |
|
333 | + // Find this language's claim (if there is one). |
|
334 | + foreach ( $entity['claims'][$property] as $claim ) { |
|
335 | + if ( $claim['mainsnak']['datavalue']['value']['language'] == $this->lang ) { |
|
336 | + // Modify this claim's text value. |
|
337 | + $titleClaim = $claim; |
|
338 | + $titleClaim['mainsnak']['datavalue']['value']['text'] = $value; |
|
339 | + $setTitleParams = [ |
|
340 | + 'action' => 'wbsetclaim', |
|
341 | + 'claim' => \GuzzleHttp\json_encode( $titleClaim ), |
|
342 | + ]; |
|
343 | + continue; |
|
344 | + } |
|
345 | + } |
|
346 | + } |
|
347 | + |
|
348 | + // If no claim was found (and modified) above, create a new claim. |
|
349 | + if ( !isset( $setTitleParams ) ) { |
|
350 | + $setTitleParams = [ |
|
351 | + 'action' => 'wbcreateclaim', |
|
352 | + 'entity' => $this->getId(), |
|
353 | + 'property' => $property, |
|
354 | + 'snaktype' => 'value', |
|
355 | + 'value' => \GuzzleHttp\json_encode( [ 'text' => $value, 'language' => $this->lang ] ), |
|
356 | + ]; |
|
357 | + } |
|
358 | + |
|
359 | + // Save the property. |
|
360 | + $wdWpOauth = new WdWpOauth(); |
|
361 | + $wdWpOauth->makeCall( $setTitleParams, true ); |
|
362 | + |
|
363 | + // Clear the cache. |
|
364 | + $this->cache->deleteItem( $this->getEntityCacheKey( $this->id ) ); |
|
365 | + } |
|
366 | + |
|
367 | + public function getInstanceOf() { |
|
368 | + $instancesOf = $this->getPropertyOfTypeItem( $this->getId(), self::PROP_INSTANCE_OF ); |
|
369 | + return array_shift( $instancesOf ); |
|
370 | + } |
|
371 | + |
|
372 | + /** |
|
373 | + * Does this item exist? |
|
374 | + * @return bool |
|
375 | + */ |
|
376 | + public function exists() { |
|
377 | + return $this->getId() !== false; |
|
378 | + } |
|
379 | + |
|
380 | + public function getWikipediaIntro() { |
|
381 | + $cacheKey = 'wikipedia-intro-' . $this->id . $this->lang; |
|
382 | + if ( $this->cache->hasItem( $cacheKey ) ) { |
|
383 | + return $this->cache->getItem( $cacheKey )->get(); |
|
384 | + } |
|
385 | + $entity = $this->getEntity( $this->id ); |
|
386 | + if ( !isset( $entity['sitelinks'] ) ) { |
|
387 | + return []; |
|
388 | + } |
|
389 | + foreach ( $entity['sitelinks'] as $sitelink ) { |
|
390 | + if ( $sitelink['site'] == $this->lang . 'wiki' ) { |
|
391 | + $api = new MediawikiApi( 'https://' . $this->lang . '.wikipedia.org/w/api.php' ); |
|
392 | + $req = new SimpleRequest( 'query', [ |
|
393 | + 'prop' => 'extracts', |
|
394 | + 'exintro' => true, |
|
395 | + 'titles' => $sitelink['title'], |
|
396 | + ] ); |
|
397 | + $response = $api->getRequest( $req ); |
|
398 | + $page = array_shift( $response['query']['pages'] ); |
|
399 | + $out = [ |
|
400 | + 'title' => $page['title'], |
|
401 | + 'html' => $page['extract'], |
|
402 | + ]; |
|
403 | + $cacheItem = $this->cache->getItem( $cacheKey ) |
|
404 | + ->expiresAfter( new DateInterval( 'P1D' ) ) |
|
405 | + ->set( $out ); |
|
406 | + $this->cache->save( $cacheItem ); |
|
407 | + |
|
408 | + return $out; |
|
409 | + } |
|
410 | + } |
|
411 | + |
|
412 | + return []; |
|
413 | + } |
|
414 | + |
|
415 | + /** |
|
416 | + * Get the raw entity data from the 'wbgetentities' API call. |
|
417 | + * @param string $id |
|
418 | + * @param bool $ignoreCache |
|
419 | + * @return bool |
|
420 | + */ |
|
421 | + public function getEntity( $id = null, $ignoreCache = false ) { |
|
422 | + $idActual = $id ?: $this->id; |
|
423 | + $cacheKey = $this->getEntityCacheKey( $idActual ); |
|
424 | + if ( !$ignoreCache && $this->cache->hasItem( $cacheKey ) ) { |
|
425 | + return $this->cache->getItem( $cacheKey )->get(); |
|
426 | + } |
|
427 | + $metadataRequest = new SimpleRequest( 'wbgetentities', [ 'ids' => $idActual ] ); |
|
428 | + $itemResult = $this->wdApi->getRequest( $metadataRequest ); |
|
429 | + if ( !isset( $itemResult['success'] ) || !isset( $itemResult['entities'][$id] ) ) { |
|
430 | + return false; |
|
431 | + } |
|
432 | + $metadata = $itemResult['entities'][$idActual]; |
|
433 | + $cacheItem = $this->cache->getItem( $cacheKey ) |
|
434 | + ->expiresAfter( new DateInterval( 'PT10M' ) ) |
|
435 | + ->set( $metadata ); |
|
436 | + $this->cache->save( $cacheItem ); |
|
437 | + return $metadata; |
|
438 | + } |
|
439 | + |
|
440 | + /** |
|
441 | + * @param $id |
|
442 | + * |
|
443 | + * @return string |
|
444 | + */ |
|
445 | + protected function getEntityCacheKey( $id ) { |
|
446 | + return 'entities' . $id; |
|
447 | + } |
|
448 | 448 | } |
@@ -33,12 +33,12 @@ discard block |
||
33 | 33 | /** @var string The base URL of Wikidata, with trailing slash. */ |
34 | 34 | protected $wikidataUrlBase = 'https://www.wikidata.org/wiki/'; |
35 | 35 | |
36 | - private function __construct( $id, $lang, CacheItemPoolInterface $cache ) { |
|
37 | - if ( !is_string( $id ) || preg_match( '/[QP][0-9]*/i', $id ) !== 1 ) { |
|
38 | - throw new Exception( "Not a valid ID: " . var_export( $id, true ) ); |
|
36 | + private function __construct($id, $lang, CacheItemPoolInterface $cache) { |
|
37 | + if (!is_string($id) || preg_match('/[QP][0-9]*/i', $id) !== 1) { |
|
38 | + throw new Exception("Not a valid ID: " . var_export($id, true)); |
|
39 | 39 | } |
40 | 40 | $this->id = $id; |
41 | - $this->wdApi = new MediawikiApi( 'https://www.wikidata.org/w/api.php' ); |
|
41 | + $this->wdApi = new MediawikiApi('https://www.wikidata.org/w/api.php'); |
|
42 | 42 | $this->entities = []; |
43 | 43 | $this->lang = $lang; |
44 | 44 | $this->cache = $cache; |
@@ -52,24 +52,24 @@ discard block |
||
52 | 52 | * |
53 | 53 | * @return Item |
54 | 54 | */ |
55 | - public static function factory( $id, $lang, CacheItemPoolInterface $cache ) { |
|
56 | - $item = new Item( $id, $lang, $cache ); |
|
57 | - foreach ( $item->getPropertyOfTypeItem( self::PROP_INSTANCE_OF ) as $instanceOf ) { |
|
55 | + public static function factory($id, $lang, CacheItemPoolInterface $cache) { |
|
56 | + $item = new Item($id, $lang, $cache); |
|
57 | + foreach ($item->getPropertyOfTypeItem(self::PROP_INSTANCE_OF) as $instanceOf) { |
|
58 | 58 | // Try to find a class mating the 'instance of' name. |
59 | - $possibleClassName = __NAMESPACE__ . '\\Items\\' . Str::toCamelCase( $instanceOf->getItem()->getLabel() ); |
|
60 | - if ( class_exists( $possibleClassName ) ) { |
|
59 | + $possibleClassName = __NAMESPACE__ . '\\Items\\' . Str::toCamelCase($instanceOf->getItem()->getLabel()); |
|
60 | + if (class_exists($possibleClassName)) { |
|
61 | 61 | // This won't re-request the metadata, because that's cached. |
62 | - $specificItem = new $possibleClassName( $id, $lang, $cache ); |
|
62 | + $specificItem = new $possibleClassName($id, $lang, $cache); |
|
63 | 63 | return $specificItem; |
64 | 64 | } |
65 | 65 | } |
66 | 66 | |
67 | 67 | // If we're here, just leave it as a basic Item. |
68 | - $item->setCache( $cache ); |
|
68 | + $item->setCache($cache); |
|
69 | 69 | return $item; |
70 | 70 | } |
71 | 71 | |
72 | - public function setCache( CacheItemPoolInterface $cache_item_pool ) { |
|
72 | + public function setCache(CacheItemPoolInterface $cache_item_pool) { |
|
73 | 73 | $this->cache = $cache_item_pool; |
74 | 74 | } |
75 | 75 | |
@@ -78,8 +78,8 @@ discard block |
||
78 | 78 | * @return string|bool The ID or false if it couldn't be determined. |
79 | 79 | */ |
80 | 80 | public function getId() { |
81 | - $entity = $this->getEntity( $this->id ); |
|
82 | - return isset( $entity['id'] ) ? $entity['id'] : false; |
|
81 | + $entity = $this->getEntity($this->id); |
|
82 | + return isset($entity['id']) ? $entity['id'] : false; |
|
83 | 83 | } |
84 | 84 | |
85 | 85 | /** |
@@ -87,17 +87,17 @@ discard block |
||
87 | 87 | * @return string |
88 | 88 | */ |
89 | 89 | public function getLabel() { |
90 | - $entity = $this->getEntity( $this->id ); |
|
91 | - if ( ! empty( $entity['labels'][ $this->lang ]['value'] ) ) { |
|
90 | + $entity = $this->getEntity($this->id); |
|
91 | + if (!empty($entity['labels'][$this->lang]['value'])) { |
|
92 | 92 | // Use the label if there is one. |
93 | - return $entity['labels'][ $this->lang ]['value']; |
|
93 | + return $entity['labels'][$this->lang]['value']; |
|
94 | 94 | } |
95 | 95 | // Or just use the ID. |
96 | 96 | return $entity['id']; |
97 | 97 | } |
98 | 98 | |
99 | 99 | public function getWikidataUrl() { |
100 | - return $this->wikidataUrlBase.$this->id; |
|
100 | + return $this->wikidataUrlBase . $this->id; |
|
101 | 101 | } |
102 | 102 | |
103 | 103 | /** |
@@ -111,32 +111,32 @@ discard block |
||
111 | 111 | * @param string $type |
112 | 112 | * @return array |
113 | 113 | */ |
114 | - public function getStandardProperties( $wikiProject = 'WikiProject_Books', $type = 'work' ) { |
|
115 | - if ( $type !== 'work' ) { |
|
114 | + public function getStandardProperties($wikiProject = 'WikiProject_Books', $type = 'work') { |
|
115 | + if ($type !== 'work') { |
|
116 | 116 | $type = 'edition'; |
117 | 117 | } |
118 | 118 | $cacheKey = $type . '_item_property_IDs'; |
119 | - if ( $this->cache->hasItem( $cacheKey ) ) { |
|
120 | - $propIds = $this->cache->getItem( $cacheKey )->get(); |
|
119 | + if ($this->cache->hasItem($cacheKey)) { |
|
120 | + $propIds = $this->cache->getItem($cacheKey)->get(); |
|
121 | 121 | } else { |
122 | 122 | $domCrawler = new Crawler(); |
123 | 123 | $wikiProjectUrl = 'https://www.wikidata.org/wiki/Wikidata:' . $wikiProject; |
124 | - $domCrawler->addHtmlContent( file_get_contents( $wikiProjectUrl ) ); |
|
125 | - $propAncors = "//h3/span[@id='" . ucfirst( $type ) . "_item_properties']/../following-sibling::table[1]//td[2]/a"; |
|
126 | - $propCells = $domCrawler->filterXPath( $propAncors ); |
|
124 | + $domCrawler->addHtmlContent(file_get_contents($wikiProjectUrl)); |
|
125 | + $propAncors = "//h3/span[@id='" . ucfirst($type) . "_item_properties']/../following-sibling::table[1]//td[2]/a"; |
|
126 | + $propCells = $domCrawler->filterXPath($propAncors); |
|
127 | 127 | $propIds = []; |
128 | - $propCells->each( function ( Crawler $node, $i ) use ( &$propIds ) { |
|
128 | + $propCells->each(function(Crawler $node, $i) use (&$propIds) { |
|
129 | 129 | $propId = $node->text(); |
130 | 130 | $propIds[] = $propId; |
131 | 131 | } ); |
132 | - $cacheItem = $this->cache->getItem( $cacheKey ) |
|
133 | - ->expiresAfter( new DateInterval( 'PT1H' ) ) |
|
134 | - ->set( $propIds ); |
|
135 | - $this->cache->save( $cacheItem ); |
|
132 | + $cacheItem = $this->cache->getItem($cacheKey) |
|
133 | + ->expiresAfter(new DateInterval('PT1H')) |
|
134 | + ->set($propIds); |
|
135 | + $this->cache->save($cacheItem); |
|
136 | 136 | } |
137 | 137 | $workProperties = []; |
138 | - foreach ( $propIds as $propId ) { |
|
139 | - $workProperties[] = self::factory( $propId, $this->lang, $this->cache ); |
|
138 | + foreach ($propIds as $propId) { |
|
139 | + $workProperties[] = self::factory($propId, $this->lang, $this->cache); |
|
140 | 140 | } |
141 | 141 | |
142 | 142 | return $workProperties; |
@@ -146,17 +146,17 @@ discard block |
||
146 | 146 | * @param string $propertyId |
147 | 147 | * @return bool|Time[] |
148 | 148 | */ |
149 | - protected function getPropertyOfTypeTime( $propertyId ) { |
|
149 | + protected function getPropertyOfTypeTime($propertyId) { |
|
150 | 150 | $times = []; |
151 | 151 | $entity = $this->getEntity(); |
152 | - if ( !isset( $entity['claims'][$propertyId] ) ) { |
|
152 | + if (!isset($entity['claims'][$propertyId])) { |
|
153 | 153 | // No statements for this property. |
154 | 154 | return $times; |
155 | 155 | } |
156 | 156 | // print_r($entity['claims'][$propertyId]);exit(); |
157 | - foreach ( $entity['claims'][$propertyId] as $claim ) { |
|
157 | + foreach ($entity['claims'][$propertyId] as $claim) { |
|
158 | 158 | // print_r($claim); |
159 | - $times[] = new Time( $claim, $this->lang, $this->cache ); |
|
159 | + $times[] = new Time($claim, $this->lang, $this->cache); |
|
160 | 160 | // |
161 | 161 | // $timeValue = $claim['datavalue']['value']['time']; |
162 | 162 | // // Ugly workaround for imprecise dates. :-( |
@@ -178,28 +178,28 @@ discard block |
||
178 | 178 | * |
179 | 179 | * @return \Samwilson\SimpleWikidata\Properties\Item[] |
180 | 180 | */ |
181 | - protected function getPropertyOfTypeItem( $propertyId ) { |
|
182 | - $entity = $this->getEntity( $this->id ); |
|
183 | - if ( !isset( $entity['claims'][$propertyId] ) ) { |
|
181 | + protected function getPropertyOfTypeItem($propertyId) { |
|
182 | + $entity = $this->getEntity($this->id); |
|
183 | + if (!isset($entity['claims'][$propertyId])) { |
|
184 | 184 | return []; |
185 | 185 | } |
186 | 186 | $items = []; |
187 | - foreach ( $entity['claims'][$propertyId] as $claim ) { |
|
188 | - $items[] = new Properties\Item( $claim, $this->lang, $this->cache ); |
|
187 | + foreach ($entity['claims'][$propertyId] as $claim) { |
|
188 | + $items[] = new Properties\Item($claim, $this->lang, $this->cache); |
|
189 | 189 | } |
190 | 190 | |
191 | 191 | return $items; |
192 | 192 | } |
193 | 193 | |
194 | - public function setPropertyOfTypeItem( $property, $itemId ) { |
|
195 | - $itemIdNumeric = substr( $itemId, 1 ); |
|
194 | + public function setPropertyOfTypeItem($property, $itemId) { |
|
195 | + $itemIdNumeric = substr($itemId, 1); |
|
196 | 196 | |
197 | 197 | // First see if this property already exists, and that it is different from what's being set. |
198 | - $entity = $this->getEntity( $this->id ); |
|
199 | - if ( !empty( $entity['claims'][$property] ) ) { |
|
198 | + $entity = $this->getEntity($this->id); |
|
199 | + if (!empty($entity['claims'][$property])) { |
|
200 | 200 | // Get the first claim, and update it if necessary. |
201 | - $claim = array_shift( $entity['claims'][$property] ); |
|
202 | - if ( $claim['mainsnak']['datavalue']['value']['id'] == $itemId ) { |
|
201 | + $claim = array_shift($entity['claims'][$property]); |
|
202 | + if ($claim['mainsnak']['datavalue']['value']['id'] == $itemId) { |
|
203 | 203 | // Already is the required value, no need to change. |
204 | 204 | return; |
205 | 205 | } |
@@ -207,58 +207,58 @@ discard block |
||
207 | 207 | $claim['mainsnak']['datavalue']['value']['numeric-id'] = $itemIdNumeric; |
208 | 208 | $apiParams = [ |
209 | 209 | 'action' => 'wbsetclaim', |
210 | - 'claim' => json_encode( $claim ), |
|
210 | + 'claim' => json_encode($claim), |
|
211 | 211 | ]; |
212 | 212 | } |
213 | 213 | |
214 | 214 | // If no claim was found (and modified) above, create a new claim. |
215 | - if ( !isset( $apiParams ) ) { |
|
215 | + if (!isset($apiParams)) { |
|
216 | 216 | $apiParams = [ |
217 | 217 | 'action' => 'wbcreateclaim', |
218 | 218 | 'entity' => $this->getId(), |
219 | 219 | 'property' => $property, |
220 | 220 | 'snaktype' => 'value', |
221 | - 'value' => json_encode( [ 'entity-type' => 'item', 'numeric-id' => $itemIdNumeric ] ), |
|
221 | + 'value' => json_encode(['entity-type' => 'item', 'numeric-id' => $itemIdNumeric]), |
|
222 | 222 | ]; |
223 | 223 | } |
224 | 224 | |
225 | 225 | // Save the property. |
226 | 226 | $wdWpOauth = new WdOauth(); |
227 | - $wdWpOauth->makeCall( $apiParams, true ); |
|
227 | + $wdWpOauth->makeCall($apiParams, true); |
|
228 | 228 | |
229 | 229 | // Clear the cache. |
230 | - $this->cache->deleteItem( $this->getEntityCacheKey( $this->id ) ); |
|
230 | + $this->cache->deleteItem($this->getEntityCacheKey($this->id)); |
|
231 | 231 | } |
232 | 232 | |
233 | - public function getPropertyOfTypeUrl( $entityId, $propertyId ) { |
|
234 | - $entity = $this->getEntity( $entityId ); |
|
235 | - if ( !isset( $entity['claims'][$propertyId] ) ) { |
|
233 | + public function getPropertyOfTypeUrl($entityId, $propertyId) { |
|
234 | + $entity = $this->getEntity($entityId); |
|
235 | + if (!isset($entity['claims'][$propertyId])) { |
|
236 | 236 | return false; |
237 | 237 | } |
238 | 238 | $urls = []; |
239 | - foreach ( $entity['claims'][$propertyId] as $claim ) { |
|
239 | + foreach ($entity['claims'][$propertyId] as $claim) { |
|
240 | 240 | $urls[] = $claim['mainsnak']['datavalue']['value']; |
241 | 241 | } |
242 | 242 | |
243 | 243 | return $urls; |
244 | 244 | } |
245 | 245 | |
246 | - public function getPropertyOfTypeExternalIdentifier( $entityId, $propertyId ) { |
|
247 | - $entity = $this->getEntity( $entityId ); |
|
248 | - if ( !isset( $entity['claims'][$propertyId] ) ) { |
|
246 | + public function getPropertyOfTypeExternalIdentifier($entityId, $propertyId) { |
|
247 | + $entity = $this->getEntity($entityId); |
|
248 | + if (!isset($entity['claims'][$propertyId])) { |
|
249 | 249 | return false; |
250 | 250 | } |
251 | 251 | $idents = []; |
252 | - foreach ( $entity['claims'][$propertyId] as $claim ) { |
|
252 | + foreach ($entity['claims'][$propertyId] as $claim) { |
|
253 | 253 | $qualifiers = []; |
254 | - if ( !isset( $claim['qualifiers'] ) ) { |
|
254 | + if (!isset($claim['qualifiers'])) { |
|
255 | 255 | continue; |
256 | 256 | } |
257 | - foreach ( $claim['qualifiers'] as $qualsInfo ) { |
|
258 | - foreach ( $qualsInfo as $qualInfo ) { |
|
259 | - $qualProp = self::factory( $qualInfo['property'], $this->lang, $this->cache ); |
|
257 | + foreach ($claim['qualifiers'] as $qualsInfo) { |
|
258 | + foreach ($qualsInfo as $qualInfo) { |
|
259 | + $qualProp = self::factory($qualInfo['property'], $this->lang, $this->cache); |
|
260 | 260 | $propLabel = $qualProp->getLabel(); |
261 | - if ( !isset( $qualifiers[$propLabel] ) ) { |
|
261 | + if (!isset($qualifiers[$propLabel])) { |
|
262 | 262 | $qualifiers[$propLabel] = []; |
263 | 263 | } |
264 | 264 | $qualifiers[$propLabel][] = $qualInfo['datavalue']['value']; |
@@ -278,17 +278,17 @@ discard block |
||
278 | 278 | * @param string $property One of the PROP_* constants. |
279 | 279 | * @return string|bool The value, or false if it can't be found. |
280 | 280 | */ |
281 | - public function getPropertyOfTypeText( $property ) { |
|
282 | - $entity = $this->getEntity( $this->id ); |
|
283 | - if ( isset( $entity['claims'][$property] ) ) { |
|
281 | + public function getPropertyOfTypeText($property) { |
|
282 | + $entity = $this->getEntity($this->id); |
|
283 | + if (isset($entity['claims'][$property])) { |
|
284 | 284 | // Use the first title. |
285 | - foreach ( $entity['claims'][$property] as $t ) { |
|
286 | - if ( !isset( $t['mainsnak']['datavalue']['value']['language'] ) ) { |
|
287 | - var_dump( $t['mainsnak']['datavalue']['value'] ); |
|
285 | + foreach ($entity['claims'][$property] as $t) { |
|
286 | + if (!isset($t['mainsnak']['datavalue']['value']['language'])) { |
|
287 | + var_dump($t['mainsnak']['datavalue']['value']); |
|
288 | 288 | exit(); |
289 | 289 | } |
290 | - if ( $t['mainsnak']['datavalue']['value']['language'] == $this->lang |
|
291 | - && !empty( $t['mainsnak']['datavalue']['value']['text'] ) |
|
290 | + if ($t['mainsnak']['datavalue']['value']['language'] == $this->lang |
|
291 | + && !empty($t['mainsnak']['datavalue']['value']['text']) |
|
292 | 292 | ) { |
293 | 293 | return $t['mainsnak']['datavalue']['value']['text']; |
294 | 294 | } |
@@ -306,16 +306,16 @@ discard block |
||
306 | 306 | * @param $property |
307 | 307 | * @return mixed[]|bool If it's not false it's an array with 'amount', 'unit', etc. |
308 | 308 | */ |
309 | - public function getPropertyOfTypeQuantity( $property ) { |
|
309 | + public function getPropertyOfTypeQuantity($property) { |
|
310 | 310 | $quantities = []; |
311 | - $entity = $this->getEntity( $this->id ); |
|
312 | - if ( !isset( $entity['claims'][$property] ) ) { |
|
311 | + $entity = $this->getEntity($this->id); |
|
312 | + if (!isset($entity['claims'][$property])) { |
|
313 | 313 | return false; |
314 | 314 | } |
315 | - foreach ( $entity['claims'][$property] as $t ) { |
|
315 | + foreach ($entity['claims'][$property] as $t) { |
|
316 | 316 | $quantity = $t['mainsnak']['datavalue']['value']; |
317 | - $unitId = substr( $quantity['unit'], strlen( $this->wikidataUrlBase ) + 1 ); |
|
318 | - $quantity['unit'] = self::factory( $unitId, $this->lang, $this->cache ); |
|
317 | + $unitId = substr($quantity['unit'], strlen($this->wikidataUrlBase) + 1); |
|
318 | + $quantity['unit'] = self::factory($unitId, $this->lang, $this->cache); |
|
319 | 319 | $quantities[] = $quantity; |
320 | 320 | } |
321 | 321 | return $quantities; |
@@ -326,19 +326,19 @@ discard block |
||
326 | 326 | * @param string $property One of the PROP_* constants. |
327 | 327 | * @param string $value The value. |
328 | 328 | */ |
329 | - public function setPropertyOfTypeText( $property, $value ) { |
|
329 | + public function setPropertyOfTypeText($property, $value) { |
|
330 | 330 | // First see if this property already exists, and that it is different from what's being set. |
331 | - $entity = $this->getEntity( $this->id ); |
|
332 | - if ( !empty( $entity['claims'][$property] ) ) { |
|
331 | + $entity = $this->getEntity($this->id); |
|
332 | + if (!empty($entity['claims'][$property])) { |
|
333 | 333 | // Find this language's claim (if there is one). |
334 | - foreach ( $entity['claims'][$property] as $claim ) { |
|
335 | - if ( $claim['mainsnak']['datavalue']['value']['language'] == $this->lang ) { |
|
334 | + foreach ($entity['claims'][$property] as $claim) { |
|
335 | + if ($claim['mainsnak']['datavalue']['value']['language'] == $this->lang) { |
|
336 | 336 | // Modify this claim's text value. |
337 | 337 | $titleClaim = $claim; |
338 | 338 | $titleClaim['mainsnak']['datavalue']['value']['text'] = $value; |
339 | 339 | $setTitleParams = [ |
340 | 340 | 'action' => 'wbsetclaim', |
341 | - 'claim' => \GuzzleHttp\json_encode( $titleClaim ), |
|
341 | + 'claim' => \GuzzleHttp\json_encode($titleClaim), |
|
342 | 342 | ]; |
343 | 343 | continue; |
344 | 344 | } |
@@ -346,27 +346,27 @@ discard block |
||
346 | 346 | } |
347 | 347 | |
348 | 348 | // If no claim was found (and modified) above, create a new claim. |
349 | - if ( !isset( $setTitleParams ) ) { |
|
349 | + if (!isset($setTitleParams)) { |
|
350 | 350 | $setTitleParams = [ |
351 | 351 | 'action' => 'wbcreateclaim', |
352 | 352 | 'entity' => $this->getId(), |
353 | 353 | 'property' => $property, |
354 | 354 | 'snaktype' => 'value', |
355 | - 'value' => \GuzzleHttp\json_encode( [ 'text' => $value, 'language' => $this->lang ] ), |
|
355 | + 'value' => \GuzzleHttp\json_encode(['text' => $value, 'language' => $this->lang]), |
|
356 | 356 | ]; |
357 | 357 | } |
358 | 358 | |
359 | 359 | // Save the property. |
360 | 360 | $wdWpOauth = new WdWpOauth(); |
361 | - $wdWpOauth->makeCall( $setTitleParams, true ); |
|
361 | + $wdWpOauth->makeCall($setTitleParams, true); |
|
362 | 362 | |
363 | 363 | // Clear the cache. |
364 | - $this->cache->deleteItem( $this->getEntityCacheKey( $this->id ) ); |
|
364 | + $this->cache->deleteItem($this->getEntityCacheKey($this->id)); |
|
365 | 365 | } |
366 | 366 | |
367 | 367 | public function getInstanceOf() { |
368 | - $instancesOf = $this->getPropertyOfTypeItem( $this->getId(), self::PROP_INSTANCE_OF ); |
|
369 | - return array_shift( $instancesOf ); |
|
368 | + $instancesOf = $this->getPropertyOfTypeItem($this->getId(), self::PROP_INSTANCE_OF); |
|
369 | + return array_shift($instancesOf); |
|
370 | 370 | } |
371 | 371 | |
372 | 372 | /** |
@@ -379,31 +379,31 @@ discard block |
||
379 | 379 | |
380 | 380 | public function getWikipediaIntro() { |
381 | 381 | $cacheKey = 'wikipedia-intro-' . $this->id . $this->lang; |
382 | - if ( $this->cache->hasItem( $cacheKey ) ) { |
|
383 | - return $this->cache->getItem( $cacheKey )->get(); |
|
382 | + if ($this->cache->hasItem($cacheKey)) { |
|
383 | + return $this->cache->getItem($cacheKey)->get(); |
|
384 | 384 | } |
385 | - $entity = $this->getEntity( $this->id ); |
|
386 | - if ( !isset( $entity['sitelinks'] ) ) { |
|
385 | + $entity = $this->getEntity($this->id); |
|
386 | + if (!isset($entity['sitelinks'])) { |
|
387 | 387 | return []; |
388 | 388 | } |
389 | - foreach ( $entity['sitelinks'] as $sitelink ) { |
|
390 | - if ( $sitelink['site'] == $this->lang . 'wiki' ) { |
|
391 | - $api = new MediawikiApi( 'https://' . $this->lang . '.wikipedia.org/w/api.php' ); |
|
392 | - $req = new SimpleRequest( 'query', [ |
|
389 | + foreach ($entity['sitelinks'] as $sitelink) { |
|
390 | + if ($sitelink['site'] == $this->lang . 'wiki') { |
|
391 | + $api = new MediawikiApi('https://' . $this->lang . '.wikipedia.org/w/api.php'); |
|
392 | + $req = new SimpleRequest('query', [ |
|
393 | 393 | 'prop' => 'extracts', |
394 | 394 | 'exintro' => true, |
395 | 395 | 'titles' => $sitelink['title'], |
396 | - ] ); |
|
397 | - $response = $api->getRequest( $req ); |
|
398 | - $page = array_shift( $response['query']['pages'] ); |
|
396 | + ]); |
|
397 | + $response = $api->getRequest($req); |
|
398 | + $page = array_shift($response['query']['pages']); |
|
399 | 399 | $out = [ |
400 | 400 | 'title' => $page['title'], |
401 | 401 | 'html' => $page['extract'], |
402 | 402 | ]; |
403 | - $cacheItem = $this->cache->getItem( $cacheKey ) |
|
404 | - ->expiresAfter( new DateInterval( 'P1D' ) ) |
|
405 | - ->set( $out ); |
|
406 | - $this->cache->save( $cacheItem ); |
|
403 | + $cacheItem = $this->cache->getItem($cacheKey) |
|
404 | + ->expiresAfter(new DateInterval('P1D')) |
|
405 | + ->set($out); |
|
406 | + $this->cache->save($cacheItem); |
|
407 | 407 | |
408 | 408 | return $out; |
409 | 409 | } |
@@ -418,22 +418,22 @@ discard block |
||
418 | 418 | * @param bool $ignoreCache |
419 | 419 | * @return bool |
420 | 420 | */ |
421 | - public function getEntity( $id = null, $ignoreCache = false ) { |
|
421 | + public function getEntity($id = null, $ignoreCache = false) { |
|
422 | 422 | $idActual = $id ?: $this->id; |
423 | - $cacheKey = $this->getEntityCacheKey( $idActual ); |
|
424 | - if ( !$ignoreCache && $this->cache->hasItem( $cacheKey ) ) { |
|
425 | - return $this->cache->getItem( $cacheKey )->get(); |
|
423 | + $cacheKey = $this->getEntityCacheKey($idActual); |
|
424 | + if (!$ignoreCache && $this->cache->hasItem($cacheKey)) { |
|
425 | + return $this->cache->getItem($cacheKey)->get(); |
|
426 | 426 | } |
427 | - $metadataRequest = new SimpleRequest( 'wbgetentities', [ 'ids' => $idActual ] ); |
|
428 | - $itemResult = $this->wdApi->getRequest( $metadataRequest ); |
|
429 | - if ( !isset( $itemResult['success'] ) || !isset( $itemResult['entities'][$id] ) ) { |
|
427 | + $metadataRequest = new SimpleRequest('wbgetentities', ['ids' => $idActual]); |
|
428 | + $itemResult = $this->wdApi->getRequest($metadataRequest); |
|
429 | + if (!isset($itemResult['success']) || !isset($itemResult['entities'][$id])) { |
|
430 | 430 | return false; |
431 | 431 | } |
432 | 432 | $metadata = $itemResult['entities'][$idActual]; |
433 | - $cacheItem = $this->cache->getItem( $cacheKey ) |
|
434 | - ->expiresAfter( new DateInterval( 'PT10M' ) ) |
|
435 | - ->set( $metadata ); |
|
436 | - $this->cache->save( $cacheItem ); |
|
433 | + $cacheItem = $this->cache->getItem($cacheKey) |
|
434 | + ->expiresAfter(new DateInterval('PT10M')) |
|
435 | + ->set($metadata); |
|
436 | + $this->cache->save($cacheItem); |
|
437 | 437 | return $metadata; |
438 | 438 | } |
439 | 439 | |
@@ -442,7 +442,7 @@ discard block |
||
442 | 442 | * |
443 | 443 | * @return string |
444 | 444 | */ |
445 | - protected function getEntityCacheKey( $id ) { |
|
445 | + protected function getEntityCacheKey($id) { |
|
446 | 446 | return 'entities' . $id; |
447 | 447 | } |
448 | 448 | } |
@@ -6,66 +6,66 @@ |
||
6 | 6 | |
7 | 7 | class Edition extends Item { |
8 | 8 | |
9 | - const PROP_EDITION_OR_TRANSLATION_OF = 'P629'; |
|
10 | - const PROP_WIKISOURCE_INDEX_PAGE = 'P1957'; |
|
11 | - const PROP_SCANNED_FILE_ON_COMMONS = 'P996'; |
|
12 | - const PROP_INTERNET_ARCHIVE_ID = 'P724'; |
|
13 | - const PROP_PUBLICATION_DATE = 'P577'; |
|
14 | - const PROP_PUBLISHER = 'P123'; |
|
9 | + const PROP_EDITION_OR_TRANSLATION_OF = 'P629'; |
|
10 | + const PROP_WIKISOURCE_INDEX_PAGE = 'P1957'; |
|
11 | + const PROP_SCANNED_FILE_ON_COMMONS = 'P996'; |
|
12 | + const PROP_INTERNET_ARCHIVE_ID = 'P724'; |
|
13 | + const PROP_PUBLICATION_DATE = 'P577'; |
|
14 | + const PROP_PUBLISHER = 'P123'; |
|
15 | 15 | |
16 | - /** |
|
17 | - * @return string |
|
18 | - */ |
|
19 | - public function getPublicationYear() { |
|
20 | - $publicationYears = $this->getPropertyOfTypeTime( static::PROP_PUBLICATION_DATE ); |
|
21 | - return $publicationYears[0]->getDateTime()->format( 'Y' ); |
|
22 | - } |
|
16 | + /** |
|
17 | + * @return string |
|
18 | + */ |
|
19 | + public function getPublicationYear() { |
|
20 | + $publicationYears = $this->getPropertyOfTypeTime( static::PROP_PUBLICATION_DATE ); |
|
21 | + return $publicationYears[0]->getDateTime()->format( 'Y' ); |
|
22 | + } |
|
23 | 23 | |
24 | - /** |
|
25 | - * @return \Samwilson\SimpleWikidata\Properties\Item[] |
|
26 | - */ |
|
27 | - public function getPublishers() { |
|
28 | - return $this->getPropertyOfTypeItem( static::PROP_PUBLISHER ); |
|
29 | - } |
|
24 | + /** |
|
25 | + * @return \Samwilson\SimpleWikidata\Properties\Item[] |
|
26 | + */ |
|
27 | + public function getPublishers() { |
|
28 | + return $this->getPropertyOfTypeItem( static::PROP_PUBLISHER ); |
|
29 | + } |
|
30 | 30 | |
31 | - /** |
|
32 | - * @return array|bool |
|
33 | - */ |
|
34 | - public function getWikisourceIndexPages() { |
|
35 | - return $this->getPropertyOfTypeUrl( $this->getId(), static::PROP_WIKISOURCE_INDEX_PAGE ); |
|
36 | - } |
|
31 | + /** |
|
32 | + * @return array|bool |
|
33 | + */ |
|
34 | + public function getWikisourceIndexPages() { |
|
35 | + return $this->getPropertyOfTypeUrl( $this->getId(), static::PROP_WIKISOURCE_INDEX_PAGE ); |
|
36 | + } |
|
37 | 37 | |
38 | - /** |
|
39 | - * @return array |
|
40 | - */ |
|
41 | - public function internetArchiveIds() { |
|
42 | - return $this->getPropertyOfTypeExternalIdentifier( |
|
43 | - $this->getId(), |
|
44 | - self::PROP_INTERNET_ARCHIVE_ID |
|
45 | - ); |
|
46 | - } |
|
38 | + /** |
|
39 | + * @return array |
|
40 | + */ |
|
41 | + public function internetArchiveIds() { |
|
42 | + return $this->getPropertyOfTypeExternalIdentifier( |
|
43 | + $this->getId(), |
|
44 | + self::PROP_INTERNET_ARCHIVE_ID |
|
45 | + ); |
|
46 | + } |
|
47 | 47 | |
48 | - /** |
|
49 | - * Get information about the Wikisource sitelink. |
|
50 | - * An edition should only ever be present on one Wikisource. |
|
51 | - * @return string[] |
|
52 | - */ |
|
53 | - public function getWikisourceLink() { |
|
54 | - $entity = $this->getEntity( $this->id ); |
|
55 | - if ( !isset( $entity['sitelinks'] ) ) { |
|
56 | - return []; |
|
57 | - } |
|
58 | - foreach ( $entity['sitelinks'] as $sitelink ) { |
|
59 | - if ( strpos( $sitelink['site'], 'wikisource' ) !== false ) { |
|
60 | - $lang = substr( $sitelink['site'], 0, strpos( $sitelink['site'], 'wikisource' ) ); |
|
61 | - return [ |
|
62 | - 'title' => $sitelink['title'], |
|
63 | - 'url' => "https://$lang.wikisource.org/wiki/".$sitelink['title'], |
|
64 | - 'lang' => $lang, |
|
65 | - ]; |
|
66 | - } |
|
67 | - } |
|
68 | - return []; |
|
69 | - } |
|
48 | + /** |
|
49 | + * Get information about the Wikisource sitelink. |
|
50 | + * An edition should only ever be present on one Wikisource. |
|
51 | + * @return string[] |
|
52 | + */ |
|
53 | + public function getWikisourceLink() { |
|
54 | + $entity = $this->getEntity( $this->id ); |
|
55 | + if ( !isset( $entity['sitelinks'] ) ) { |
|
56 | + return []; |
|
57 | + } |
|
58 | + foreach ( $entity['sitelinks'] as $sitelink ) { |
|
59 | + if ( strpos( $sitelink['site'], 'wikisource' ) !== false ) { |
|
60 | + $lang = substr( $sitelink['site'], 0, strpos( $sitelink['site'], 'wikisource' ) ); |
|
61 | + return [ |
|
62 | + 'title' => $sitelink['title'], |
|
63 | + 'url' => "https://$lang.wikisource.org/wiki/".$sitelink['title'], |
|
64 | + 'lang' => $lang, |
|
65 | + ]; |
|
66 | + } |
|
67 | + } |
|
68 | + return []; |
|
69 | + } |
|
70 | 70 | |
71 | 71 | } |
@@ -11,94 +11,94 @@ |
||
11 | 11 | */ |
12 | 12 | class Work extends Item { |
13 | 13 | |
14 | - const ITEM_WORK = 'Q386724'; |
|
15 | - const PROP_SUBTITLE = 'P1680'; |
|
16 | - const PROP_GENRE = 'P136'; |
|
17 | - const PROP_SUBJECT = 'P921'; |
|
14 | + const ITEM_WORK = 'Q386724'; |
|
15 | + const PROP_SUBTITLE = 'P1680'; |
|
16 | + const PROP_GENRE = 'P136'; |
|
17 | + const PROP_SUBJECT = 'P921'; |
|
18 | 18 | |
19 | - /** |
|
20 | - * @param string $lang ISO639 language code. |
|
21 | - * @param CacheItemPoolInterface $cache The cache. |
|
22 | - * @return array|Item[] |
|
23 | - */ |
|
24 | - public static function getBookTypes( $lang = 'en', $cache ) { |
|
25 | - $sparql = "SELECT ?item WHERE { |
|
19 | + /** |
|
20 | + * @param string $lang ISO639 language code. |
|
21 | + * @param CacheItemPoolInterface $cache The cache. |
|
22 | + * @return array|Item[] |
|
23 | + */ |
|
24 | + public static function getBookTypes( $lang = 'en', $cache ) { |
|
25 | + $sparql = "SELECT ?item WHERE { |
|
26 | 26 | ?item wdt:P279 wd:Q571 . |
27 | 27 | ?item rdfs:label ?label . |
28 | 28 | FILTER(LANG(?label) = '$lang') . |
29 | 29 | } ORDER BY ?label "; |
30 | - $query = new Query( $sparql, $lang ); |
|
31 | - $query->setCache( $cache ); |
|
32 | - $bookType = Item::factory( self::ITEM_WORK, $lang, $cache ); |
|
33 | - return [ $bookType ] + $query->getItems(); |
|
34 | - } |
|
30 | + $query = new Query( $sparql, $lang ); |
|
31 | + $query->setCache( $cache ); |
|
32 | + $bookType = Item::factory( self::ITEM_WORK, $lang, $cache ); |
|
33 | + return [ $bookType ] + $query->getItems(); |
|
34 | + } |
|
35 | 35 | |
36 | - /** |
|
37 | - * @return bool|string |
|
38 | - */ |
|
39 | - public function getSubtitle() { |
|
40 | - return $this->getPropertyOfTypeText( self::PROP_SUBTITLE ); |
|
41 | - } |
|
36 | + /** |
|
37 | + * @return bool|string |
|
38 | + */ |
|
39 | + public function getSubtitle() { |
|
40 | + return $this->getPropertyOfTypeText( self::PROP_SUBTITLE ); |
|
41 | + } |
|
42 | 42 | |
43 | - /** |
|
44 | - * @param string $subtitle The new subtitle. |
|
45 | - */ |
|
46 | - public function setSubtitle( $subtitle ) { |
|
47 | - $this->setPropertyOfTypeText( self::PROP_SUBTITLE, $subtitle ); |
|
48 | - } |
|
43 | + /** |
|
44 | + * @param string $subtitle The new subtitle. |
|
45 | + */ |
|
46 | + public function setSubtitle( $subtitle ) { |
|
47 | + $this->setPropertyOfTypeText( self::PROP_SUBTITLE, $subtitle ); |
|
48 | + } |
|
49 | 49 | |
50 | - /** |
|
51 | - * @param string $property A property identifier. |
|
52 | - * @return array |
|
53 | - */ |
|
54 | - public function getPropertyOfTypeItems( $property ) { |
|
55 | - $entity = $this->getEntity( $this->id ); |
|
56 | - if ( ! isset( $entity['claims'][ $property ] ) ) { |
|
57 | - return []; |
|
58 | - } |
|
59 | - $items = []; |
|
60 | - foreach ( $entity['claims'][ $property ] as $authorClaim ) { |
|
61 | - $item_id = $authorClaim['mainsnak']['datavalue']['value']['id']; |
|
62 | - $items[] = Item::factory( $item_id, $this->lang, $this->cache ); |
|
63 | - } |
|
50 | + /** |
|
51 | + * @param string $property A property identifier. |
|
52 | + * @return array |
|
53 | + */ |
|
54 | + public function getPropertyOfTypeItems( $property ) { |
|
55 | + $entity = $this->getEntity( $this->id ); |
|
56 | + if ( ! isset( $entity['claims'][ $property ] ) ) { |
|
57 | + return []; |
|
58 | + } |
|
59 | + $items = []; |
|
60 | + foreach ( $entity['claims'][ $property ] as $authorClaim ) { |
|
61 | + $item_id = $authorClaim['mainsnak']['datavalue']['value']['id']; |
|
62 | + $items[] = Item::factory( $item_id, $this->lang, $this->cache ); |
|
63 | + } |
|
64 | 64 | |
65 | - return $items; |
|
66 | - } |
|
65 | + return $items; |
|
66 | + } |
|
67 | 67 | |
68 | - /** |
|
69 | - * @return array |
|
70 | - */ |
|
71 | - public function getAuthors() { |
|
72 | - return $this->getPropertyOfTypeItems( self::PROP_AUTHOR ); |
|
73 | - } |
|
68 | + /** |
|
69 | + * @return array |
|
70 | + */ |
|
71 | + public function getAuthors() { |
|
72 | + return $this->getPropertyOfTypeItems( self::PROP_AUTHOR ); |
|
73 | + } |
|
74 | 74 | |
75 | - /** |
|
76 | - * @return Item[] |
|
77 | - */ |
|
78 | - public function getSubjects() { |
|
79 | - return $this->getPropertyOfTypeItems( self::PROP_SUBJECT ); |
|
80 | - } |
|
75 | + /** |
|
76 | + * @return Item[] |
|
77 | + */ |
|
78 | + public function getSubjects() { |
|
79 | + return $this->getPropertyOfTypeItems( self::PROP_SUBJECT ); |
|
80 | + } |
|
81 | 81 | |
82 | - /** |
|
83 | - * @return Item[] |
|
84 | - */ |
|
85 | - public function getEditions() { |
|
86 | - $sparql = "SELECT ?item WHERE {" |
|
87 | - . " ?item wdt:" . Edition::PROP_EDITION_OR_TRANSLATION_OF . " wd:" . $this->getId() |
|
88 | - . "}"; |
|
89 | - $query = new Query( $sparql, $this->lang, $this->cache ); |
|
90 | - $editions = $query->getItems(); |
|
91 | - usort( $editions, function ( Item $a, Item $b ) { |
|
92 | - if ( $a instanceof EditionItem and $b instanceof EditionItem ) { |
|
93 | - return $a->getPublicationYear() - $b->getPublicationYear(); |
|
94 | - } |
|
95 | - return 0; |
|
96 | - } ); |
|
82 | + /** |
|
83 | + * @return Item[] |
|
84 | + */ |
|
85 | + public function getEditions() { |
|
86 | + $sparql = "SELECT ?item WHERE {" |
|
87 | + . " ?item wdt:" . Edition::PROP_EDITION_OR_TRANSLATION_OF . " wd:" . $this->getId() |
|
88 | + . "}"; |
|
89 | + $query = new Query( $sparql, $this->lang, $this->cache ); |
|
90 | + $editions = $query->getItems(); |
|
91 | + usort( $editions, function ( Item $a, Item $b ) { |
|
92 | + if ( $a instanceof EditionItem and $b instanceof EditionItem ) { |
|
93 | + return $a->getPublicationYear() - $b->getPublicationYear(); |
|
94 | + } |
|
95 | + return 0; |
|
96 | + } ); |
|
97 | 97 | |
98 | - return $editions; |
|
99 | - } |
|
98 | + return $editions; |
|
99 | + } |
|
100 | 100 | |
101 | - public function newEdition() { |
|
102 | - } |
|
101 | + public function newEdition() { |
|
102 | + } |
|
103 | 103 | |
104 | 104 | } |
@@ -21,45 +21,45 @@ discard block |
||
21 | 21 | * @param CacheItemPoolInterface $cache The cache. |
22 | 22 | * @return array|Item[] |
23 | 23 | */ |
24 | - public static function getBookTypes( $lang = 'en', $cache ) { |
|
24 | + public static function getBookTypes($lang = 'en', $cache) { |
|
25 | 25 | $sparql = "SELECT ?item WHERE { |
26 | 26 | ?item wdt:P279 wd:Q571 . |
27 | 27 | ?item rdfs:label ?label . |
28 | 28 | FILTER(LANG(?label) = '$lang') . |
29 | 29 | } ORDER BY ?label "; |
30 | - $query = new Query( $sparql, $lang ); |
|
31 | - $query->setCache( $cache ); |
|
32 | - $bookType = Item::factory( self::ITEM_WORK, $lang, $cache ); |
|
33 | - return [ $bookType ] + $query->getItems(); |
|
30 | + $query = new Query($sparql, $lang); |
|
31 | + $query->setCache($cache); |
|
32 | + $bookType = Item::factory(self::ITEM_WORK, $lang, $cache); |
|
33 | + return [$bookType] + $query->getItems(); |
|
34 | 34 | } |
35 | 35 | |
36 | 36 | /** |
37 | 37 | * @return bool|string |
38 | 38 | */ |
39 | 39 | public function getSubtitle() { |
40 | - return $this->getPropertyOfTypeText( self::PROP_SUBTITLE ); |
|
40 | + return $this->getPropertyOfTypeText(self::PROP_SUBTITLE); |
|
41 | 41 | } |
42 | 42 | |
43 | 43 | /** |
44 | 44 | * @param string $subtitle The new subtitle. |
45 | 45 | */ |
46 | - public function setSubtitle( $subtitle ) { |
|
47 | - $this->setPropertyOfTypeText( self::PROP_SUBTITLE, $subtitle ); |
|
46 | + public function setSubtitle($subtitle) { |
|
47 | + $this->setPropertyOfTypeText(self::PROP_SUBTITLE, $subtitle); |
|
48 | 48 | } |
49 | 49 | |
50 | 50 | /** |
51 | 51 | * @param string $property A property identifier. |
52 | 52 | * @return array |
53 | 53 | */ |
54 | - public function getPropertyOfTypeItems( $property ) { |
|
55 | - $entity = $this->getEntity( $this->id ); |
|
56 | - if ( ! isset( $entity['claims'][ $property ] ) ) { |
|
54 | + public function getPropertyOfTypeItems($property) { |
|
55 | + $entity = $this->getEntity($this->id); |
|
56 | + if (!isset($entity['claims'][$property])) { |
|
57 | 57 | return []; |
58 | 58 | } |
59 | 59 | $items = []; |
60 | - foreach ( $entity['claims'][ $property ] as $authorClaim ) { |
|
61 | - $item_id = $authorClaim['mainsnak']['datavalue']['value']['id']; |
|
62 | - $items[] = Item::factory( $item_id, $this->lang, $this->cache ); |
|
60 | + foreach ($entity['claims'][$property] as $authorClaim) { |
|
61 | + $item_id = $authorClaim['mainsnak']['datavalue']['value']['id']; |
|
62 | + $items[] = Item::factory($item_id, $this->lang, $this->cache); |
|
63 | 63 | } |
64 | 64 | |
65 | 65 | return $items; |
@@ -69,14 +69,14 @@ discard block |
||
69 | 69 | * @return array |
70 | 70 | */ |
71 | 71 | public function getAuthors() { |
72 | - return $this->getPropertyOfTypeItems( self::PROP_AUTHOR ); |
|
72 | + return $this->getPropertyOfTypeItems(self::PROP_AUTHOR); |
|
73 | 73 | } |
74 | 74 | |
75 | 75 | /** |
76 | 76 | * @return Item[] |
77 | 77 | */ |
78 | 78 | public function getSubjects() { |
79 | - return $this->getPropertyOfTypeItems( self::PROP_SUBJECT ); |
|
79 | + return $this->getPropertyOfTypeItems(self::PROP_SUBJECT); |
|
80 | 80 | } |
81 | 81 | |
82 | 82 | /** |
@@ -86,10 +86,10 @@ discard block |
||
86 | 86 | $sparql = "SELECT ?item WHERE {" |
87 | 87 | . " ?item wdt:" . Edition::PROP_EDITION_OR_TRANSLATION_OF . " wd:" . $this->getId() |
88 | 88 | . "}"; |
89 | - $query = new Query( $sparql, $this->lang, $this->cache ); |
|
89 | + $query = new Query($sparql, $this->lang, $this->cache); |
|
90 | 90 | $editions = $query->getItems(); |
91 | - usort( $editions, function ( Item $a, Item $b ) { |
|
92 | - if ( $a instanceof EditionItem and $b instanceof EditionItem ) { |
|
91 | + usort($editions, function(Item $a, Item $b) { |
|
92 | + if ($a instanceof EditionItem and $b instanceof EditionItem) { |
|
93 | 93 | return $a->getPublicationYear() - $b->getPublicationYear(); |
94 | 94 | } |
95 | 95 | return 0; |