@@ -12,180 +12,180 @@ discard block |
||
12 | 12 | |
13 | 13 | class Item { |
14 | 14 | |
15 | - /** @var string|bool */ |
|
16 | - const INSTANCE_OF = false; |
|
17 | - |
|
18 | - const PROP_INSTANCE_OF = 'P31'; |
|
19 | - const PROP_TITLE = 'P1476'; |
|
20 | - const PROP_IMAGE = 'P18'; |
|
21 | - const PROP_AUTHOR = 'P50'; |
|
22 | - |
|
23 | - /** @var string[] List of Q-numbers of registered classes. */ |
|
24 | - protected static $registeredClasses = []; |
|
25 | - |
|
26 | - /** @var string */ |
|
27 | - protected $id; |
|
28 | - |
|
29 | - /** @var MediawikiApi */ |
|
30 | - protected $wdApi; |
|
31 | - |
|
32 | - /** @var string */ |
|
33 | - protected $lang; |
|
34 | - |
|
35 | - /** @var CacheItemPoolInterface */ |
|
36 | - protected $cache; |
|
37 | - |
|
38 | - /** @var string The base URL of Wikidata, with trailing slash. */ |
|
39 | - protected $wikidataUrlBase = 'https://www.wikidata.org/wiki/'; |
|
40 | - |
|
41 | - private function __construct( $id, $lang, CacheItemPoolInterface $cache ) { |
|
42 | - if ( !is_string( $id ) || preg_match( '/[QP][0-9]*/i', $id ) !== 1 ) { |
|
43 | - throw new Exception( "Not a valid ID: " . var_export( $id, true ) ); |
|
44 | - } |
|
45 | - $this->id = $id; |
|
46 | - $this->wdApi = new MediawikiApi( 'https://www.wikidata.org/w/api.php' ); |
|
47 | - $this->entities = []; |
|
48 | - $this->lang = $lang; |
|
49 | - $this->cache = $cache; |
|
50 | - } |
|
51 | - |
|
52 | - /** |
|
53 | - * Create a new Item object with class based on the item's 'instance of' statement. |
|
54 | - * |
|
55 | - * @param string $id The item ID (Q-number). |
|
56 | - * @param string $lang The language code. |
|
57 | - * @param CacheItemPoolInterface $cache The cache to use. |
|
58 | - * @return Item |
|
59 | - */ |
|
60 | - public static function factory( $id, $lang, CacheItemPoolInterface $cache ) { |
|
61 | - $item = new Item( $id, $lang, $cache ); |
|
62 | - foreach ( $item->getPropertyOfTypeItem( self::PROP_INSTANCE_OF ) as $instanceOf ) { |
|
63 | - // Try to find a class matching the registered 'instance of'. |
|
64 | - foreach ( static::$registeredClasses as $classId => $className ) { |
|
65 | - // If this 'instance of' is registered, use it. |
|
66 | - if ( $classId === $instanceOf->getItem()->getId() ) { |
|
67 | - // This won't re-request the metadata, because that's cached. |
|
68 | - return new $className( $id, $lang, $cache ); |
|
69 | - } |
|
70 | - } |
|
71 | - } |
|
72 | - |
|
73 | - // If we're here, just leave it as a basic Item. |
|
74 | - $item->setCache( $cache ); |
|
75 | - return $item; |
|
76 | - } |
|
77 | - |
|
78 | - /** |
|
79 | - * Register this class as a candidate for being created by Item::factory. |
|
80 | - * Should only be called on subclasses of Item. |
|
81 | - * @throws Exception if called on Item or the registering class does not have INSTANCE_OF set. |
|
82 | - */ |
|
83 | - public static function register() { |
|
84 | - if ( static::class === self::class ) { |
|
85 | - throw new Exception( __METHOD__ . ' should only be called on subclasses of Item' ); |
|
86 | - } |
|
87 | - if ( !static::INSTANCE_OF ) { |
|
88 | - throw new Exception( 'Please set INSTANCE_OF for ' . static::class ); |
|
89 | - } |
|
90 | - static::$registeredClasses[ static::INSTANCE_OF ] = static::class; |
|
91 | - } |
|
92 | - |
|
93 | - /** |
|
94 | - * @param CacheItemPoolInterface $cache The cache to use. |
|
95 | - */ |
|
96 | - public function setCache( CacheItemPoolInterface $cache ) { |
|
97 | - $this->cache = $cache; |
|
98 | - } |
|
99 | - |
|
100 | - /** |
|
101 | - * Get the ID (Q-number) of this item. |
|
102 | - * @return string|bool The ID or false if it couldn't be determined. |
|
103 | - */ |
|
104 | - public function getId() { |
|
105 | - $entity = $this->getEntity( $this->id ); |
|
106 | - return isset( $entity['id'] ) ? $entity['id'] : false; |
|
107 | - } |
|
108 | - |
|
109 | - /** |
|
110 | - * Get this item's label. |
|
111 | - * @return string |
|
112 | - */ |
|
113 | - public function getLabel() { |
|
114 | - $entity = $this->getEntity( $this->id ); |
|
115 | - if ( !empty( $entity['labels'][ $this->lang ]['value'] ) ) { |
|
116 | - // Use the label if there is one. |
|
117 | - return $entity['labels'][ $this->lang ]['value']; |
|
118 | - } |
|
119 | - // Or just use the ID. |
|
120 | - return $entity['id']; |
|
121 | - } |
|
122 | - |
|
123 | - /** |
|
124 | - * @return string The Wikidata.org URL for this item. |
|
125 | - */ |
|
126 | - public function getWikidataUrl() { |
|
127 | - return $this->wikidataUrlBase . $this->id; |
|
128 | - } |
|
129 | - |
|
130 | - /** |
|
131 | - * Wikiprojects list their properties like this: |
|
132 | - * |
|
133 | - * {{List of properties/Header}} |
|
134 | - * {{List of properties/Row|id=31|example-subject=Q923767|example-object=Q3331189}} |
|
135 | - * </table> |
|
136 | - * |
|
137 | - * @param string $wikiProject The name of the WikiProject (must exist as a Wikidata page e.g. |
|
138 | - * [[Wikidata:$wikiProject]]). |
|
139 | - * @param string $type |
|
140 | - * @return array |
|
141 | - */ |
|
142 | - public function getStandardProperties( $wikiProject = 'WikiProject_Books', $type = 'work' ) { |
|
143 | - if ( $type !== 'work' ) { |
|
144 | - $type = 'edition'; |
|
145 | - } |
|
146 | - $cacheKey = $type . '_item_property_IDs'; |
|
147 | - if ( $this->cache->hasItem( $cacheKey ) ) { |
|
148 | - $propIds = $this->cache->getItem( $cacheKey )->get(); |
|
149 | - } else { |
|
150 | - $domCrawler = new Crawler(); |
|
151 | - $wikiProjectUrl = 'https://www.wikidata.org/wiki/Wikidata:' . $wikiProject; |
|
152 | - $domCrawler->addHtmlContent( file_get_contents( $wikiProjectUrl ) ); |
|
153 | - $propAncors = "//h3/span[@id='" . ucfirst( $type ) . "_item_properties']" |
|
154 | - . "/../following-sibling::table[1]//td[2]/a"; |
|
155 | - $propCells = $domCrawler->filterXPath( $propAncors ); |
|
156 | - $propIds = []; |
|
157 | - $propCells->each( function ( Crawler $node, $i ) use ( &$propIds ) { |
|
158 | - $propId = $node->text(); |
|
159 | - $propIds[] = $propId; |
|
160 | - } ); |
|
161 | - $cacheItem = $this->cache->getItem( $cacheKey ) |
|
162 | - ->expiresAfter( new DateInterval( 'PT1H' ) ) |
|
163 | - ->set( $propIds ); |
|
164 | - $this->cache->save( $cacheItem ); |
|
165 | - } |
|
166 | - $workProperties = []; |
|
167 | - foreach ( $propIds as $propId ) { |
|
168 | - $workProperties[] = self::factory( $propId, $this->lang, $this->cache ); |
|
169 | - } |
|
170 | - |
|
171 | - return $workProperties; |
|
172 | - } |
|
173 | - |
|
174 | - /** |
|
175 | - * @param string $propertyId |
|
176 | - * @return bool|Time[] |
|
177 | - */ |
|
178 | - public function getPropertyOfTypeTime( $propertyId ) { |
|
179 | - $times = []; |
|
180 | - $entity = $this->getEntity(); |
|
181 | - if ( !isset( $entity['claims'][$propertyId] ) ) { |
|
182 | - // No statements for this property. |
|
183 | - return $times; |
|
184 | - } |
|
185 | - // print_r($entity['claims'][$propertyId]);exit(); |
|
186 | - foreach ( $entity['claims'][$propertyId] as $claim ) { |
|
187 | - // print_r($claim); |
|
188 | - $times[] = new Time( $claim, $this->lang, $this->cache ); |
|
15 | + /** @var string|bool */ |
|
16 | + const INSTANCE_OF = false; |
|
17 | + |
|
18 | + const PROP_INSTANCE_OF = 'P31'; |
|
19 | + const PROP_TITLE = 'P1476'; |
|
20 | + const PROP_IMAGE = 'P18'; |
|
21 | + const PROP_AUTHOR = 'P50'; |
|
22 | + |
|
23 | + /** @var string[] List of Q-numbers of registered classes. */ |
|
24 | + protected static $registeredClasses = []; |
|
25 | + |
|
26 | + /** @var string */ |
|
27 | + protected $id; |
|
28 | + |
|
29 | + /** @var MediawikiApi */ |
|
30 | + protected $wdApi; |
|
31 | + |
|
32 | + /** @var string */ |
|
33 | + protected $lang; |
|
34 | + |
|
35 | + /** @var CacheItemPoolInterface */ |
|
36 | + protected $cache; |
|
37 | + |
|
38 | + /** @var string The base URL of Wikidata, with trailing slash. */ |
|
39 | + protected $wikidataUrlBase = 'https://www.wikidata.org/wiki/'; |
|
40 | + |
|
41 | + private function __construct( $id, $lang, CacheItemPoolInterface $cache ) { |
|
42 | + if ( !is_string( $id ) || preg_match( '/[QP][0-9]*/i', $id ) !== 1 ) { |
|
43 | + throw new Exception( "Not a valid ID: " . var_export( $id, true ) ); |
|
44 | + } |
|
45 | + $this->id = $id; |
|
46 | + $this->wdApi = new MediawikiApi( 'https://www.wikidata.org/w/api.php' ); |
|
47 | + $this->entities = []; |
|
48 | + $this->lang = $lang; |
|
49 | + $this->cache = $cache; |
|
50 | + } |
|
51 | + |
|
52 | + /** |
|
53 | + * Create a new Item object with class based on the item's 'instance of' statement. |
|
54 | + * |
|
55 | + * @param string $id The item ID (Q-number). |
|
56 | + * @param string $lang The language code. |
|
57 | + * @param CacheItemPoolInterface $cache The cache to use. |
|
58 | + * @return Item |
|
59 | + */ |
|
60 | + public static function factory( $id, $lang, CacheItemPoolInterface $cache ) { |
|
61 | + $item = new Item( $id, $lang, $cache ); |
|
62 | + foreach ( $item->getPropertyOfTypeItem( self::PROP_INSTANCE_OF ) as $instanceOf ) { |
|
63 | + // Try to find a class matching the registered 'instance of'. |
|
64 | + foreach ( static::$registeredClasses as $classId => $className ) { |
|
65 | + // If this 'instance of' is registered, use it. |
|
66 | + if ( $classId === $instanceOf->getItem()->getId() ) { |
|
67 | + // This won't re-request the metadata, because that's cached. |
|
68 | + return new $className( $id, $lang, $cache ); |
|
69 | + } |
|
70 | + } |
|
71 | + } |
|
72 | + |
|
73 | + // If we're here, just leave it as a basic Item. |
|
74 | + $item->setCache( $cache ); |
|
75 | + return $item; |
|
76 | + } |
|
77 | + |
|
78 | + /** |
|
79 | + * Register this class as a candidate for being created by Item::factory. |
|
80 | + * Should only be called on subclasses of Item. |
|
81 | + * @throws Exception if called on Item or the registering class does not have INSTANCE_OF set. |
|
82 | + */ |
|
83 | + public static function register() { |
|
84 | + if ( static::class === self::class ) { |
|
85 | + throw new Exception( __METHOD__ . ' should only be called on subclasses of Item' ); |
|
86 | + } |
|
87 | + if ( !static::INSTANCE_OF ) { |
|
88 | + throw new Exception( 'Please set INSTANCE_OF for ' . static::class ); |
|
89 | + } |
|
90 | + static::$registeredClasses[ static::INSTANCE_OF ] = static::class; |
|
91 | + } |
|
92 | + |
|
93 | + /** |
|
94 | + * @param CacheItemPoolInterface $cache The cache to use. |
|
95 | + */ |
|
96 | + public function setCache( CacheItemPoolInterface $cache ) { |
|
97 | + $this->cache = $cache; |
|
98 | + } |
|
99 | + |
|
100 | + /** |
|
101 | + * Get the ID (Q-number) of this item. |
|
102 | + * @return string|bool The ID or false if it couldn't be determined. |
|
103 | + */ |
|
104 | + public function getId() { |
|
105 | + $entity = $this->getEntity( $this->id ); |
|
106 | + return isset( $entity['id'] ) ? $entity['id'] : false; |
|
107 | + } |
|
108 | + |
|
109 | + /** |
|
110 | + * Get this item's label. |
|
111 | + * @return string |
|
112 | + */ |
|
113 | + public function getLabel() { |
|
114 | + $entity = $this->getEntity( $this->id ); |
|
115 | + if ( !empty( $entity['labels'][ $this->lang ]['value'] ) ) { |
|
116 | + // Use the label if there is one. |
|
117 | + return $entity['labels'][ $this->lang ]['value']; |
|
118 | + } |
|
119 | + // Or just use the ID. |
|
120 | + return $entity['id']; |
|
121 | + } |
|
122 | + |
|
123 | + /** |
|
124 | + * @return string The Wikidata.org URL for this item. |
|
125 | + */ |
|
126 | + public function getWikidataUrl() { |
|
127 | + return $this->wikidataUrlBase . $this->id; |
|
128 | + } |
|
129 | + |
|
130 | + /** |
|
131 | + * Wikiprojects list their properties like this: |
|
132 | + * |
|
133 | + * {{List of properties/Header}} |
|
134 | + * {{List of properties/Row|id=31|example-subject=Q923767|example-object=Q3331189}} |
|
135 | + * </table> |
|
136 | + * |
|
137 | + * @param string $wikiProject The name of the WikiProject (must exist as a Wikidata page e.g. |
|
138 | + * [[Wikidata:$wikiProject]]). |
|
139 | + * @param string $type |
|
140 | + * @return array |
|
141 | + */ |
|
142 | + public function getStandardProperties( $wikiProject = 'WikiProject_Books', $type = 'work' ) { |
|
143 | + if ( $type !== 'work' ) { |
|
144 | + $type = 'edition'; |
|
145 | + } |
|
146 | + $cacheKey = $type . '_item_property_IDs'; |
|
147 | + if ( $this->cache->hasItem( $cacheKey ) ) { |
|
148 | + $propIds = $this->cache->getItem( $cacheKey )->get(); |
|
149 | + } else { |
|
150 | + $domCrawler = new Crawler(); |
|
151 | + $wikiProjectUrl = 'https://www.wikidata.org/wiki/Wikidata:' . $wikiProject; |
|
152 | + $domCrawler->addHtmlContent( file_get_contents( $wikiProjectUrl ) ); |
|
153 | + $propAncors = "//h3/span[@id='" . ucfirst( $type ) . "_item_properties']" |
|
154 | + . "/../following-sibling::table[1]//td[2]/a"; |
|
155 | + $propCells = $domCrawler->filterXPath( $propAncors ); |
|
156 | + $propIds = []; |
|
157 | + $propCells->each( function ( Crawler $node, $i ) use ( &$propIds ) { |
|
158 | + $propId = $node->text(); |
|
159 | + $propIds[] = $propId; |
|
160 | + } ); |
|
161 | + $cacheItem = $this->cache->getItem( $cacheKey ) |
|
162 | + ->expiresAfter( new DateInterval( 'PT1H' ) ) |
|
163 | + ->set( $propIds ); |
|
164 | + $this->cache->save( $cacheItem ); |
|
165 | + } |
|
166 | + $workProperties = []; |
|
167 | + foreach ( $propIds as $propId ) { |
|
168 | + $workProperties[] = self::factory( $propId, $this->lang, $this->cache ); |
|
169 | + } |
|
170 | + |
|
171 | + return $workProperties; |
|
172 | + } |
|
173 | + |
|
174 | + /** |
|
175 | + * @param string $propertyId |
|
176 | + * @return bool|Time[] |
|
177 | + */ |
|
178 | + public function getPropertyOfTypeTime( $propertyId ) { |
|
179 | + $times = []; |
|
180 | + $entity = $this->getEntity(); |
|
181 | + if ( !isset( $entity['claims'][$propertyId] ) ) { |
|
182 | + // No statements for this property. |
|
183 | + return $times; |
|
184 | + } |
|
185 | + // print_r($entity['claims'][$propertyId]);exit(); |
|
186 | + foreach ( $entity['claims'][$propertyId] as $claim ) { |
|
187 | + // print_r($claim); |
|
188 | + $times[] = new Time( $claim, $this->lang, $this->cache ); |
|
189 | 189 | // |
190 | 190 | // $timeValue = $claim['datavalue']['value']['time']; |
191 | 191 | // // Ugly workaround for imprecise dates. :-( |
@@ -195,296 +195,296 @@ discard block |
||
195 | 195 | // } |
196 | 196 | // $time = strtotime($timeValue); |
197 | 197 | // return date($dateFormat, $time); |
198 | - // } |
|
199 | - } |
|
200 | - return $times; |
|
201 | - } |
|
202 | - |
|
203 | - /** |
|
204 | - * Get the Item that is referred to by the specified item's property. |
|
205 | - * |
|
206 | - * @param string $propertyId |
|
207 | - * |
|
208 | - * @return \Samwilson\SimpleWikidata\Properties\Item[] |
|
209 | - */ |
|
210 | - public function getPropertyOfTypeItem( $propertyId ) { |
|
211 | - $entity = $this->getEntity( $this->id ); |
|
212 | - if ( !isset( $entity['claims'][$propertyId] ) ) { |
|
213 | - return []; |
|
214 | - } |
|
215 | - $items = []; |
|
216 | - foreach ( $entity['claims'][$propertyId] as $claim ) { |
|
217 | - $items[] = new Properties\Item( $claim, $this->lang, $this->cache ); |
|
218 | - } |
|
219 | - |
|
220 | - return $items; |
|
221 | - } |
|
222 | - |
|
223 | - /** |
|
224 | - * Set a claim to the given Item. |
|
225 | - * @param string $property Property ID, with 'P'. |
|
226 | - * @param string $itemId Item ID with 'Q'. |
|
227 | - */ |
|
228 | - public function setPropertyOfTypeItem( $property, $itemId ) { |
|
229 | - $itemIdNumeric = substr( $itemId, 1 ); |
|
230 | - |
|
231 | - // First see if this property already exists, and that it is different from what's being set. |
|
232 | - $entity = $this->getEntity( $this->id ); |
|
233 | - if ( !empty( $entity['claims'][$property] ) ) { |
|
234 | - // Get the first claim, and update it if necessary. |
|
235 | - $claim = array_shift( $entity['claims'][$property] ); |
|
236 | - if ( $claim['mainsnak']['datavalue']['value']['id'] == $itemId ) { |
|
237 | - // Already is the required value, no need to change. |
|
238 | - return; |
|
239 | - } |
|
240 | - $claim['mainsnak']['datavalue']['value']['id'] = $itemId; |
|
241 | - $claim['mainsnak']['datavalue']['value']['numeric-id'] = $itemIdNumeric; |
|
242 | - $apiParams = [ |
|
243 | - 'action' => 'wbsetclaim', |
|
244 | - 'claim' => json_encode( $claim ), |
|
245 | - ]; |
|
246 | - } |
|
247 | - |
|
248 | - // If no claim was found (and modified) above, create a new claim. |
|
249 | - if ( !isset( $apiParams ) ) { |
|
250 | - $apiParams = [ |
|
251 | - 'action' => 'wbcreateclaim', |
|
252 | - 'entity' => $this->getId(), |
|
253 | - 'property' => $property, |
|
254 | - 'snaktype' => 'value', |
|
255 | - 'value' => json_encode( [ 'entity-type' => 'item', 'numeric-id' => $itemIdNumeric ] ), |
|
256 | - ]; |
|
257 | - } |
|
258 | - |
|
259 | - // @TODO Save the property. |
|
260 | - |
|
261 | - // Clear the cache. |
|
262 | - $this->cache->deleteItem( $this->getEntityCacheKey( $this->id ) ); |
|
263 | - } |
|
264 | - |
|
265 | - /** |
|
266 | - * @param string $entityId |
|
267 | - * @param string $propertyId |
|
268 | - * @return array|bool |
|
269 | - */ |
|
270 | - public function getPropertyOfTypeUrl( $entityId, $propertyId ) { |
|
271 | - $entity = $this->getEntity( $entityId ); |
|
272 | - if ( !isset( $entity['claims'][$propertyId] ) ) { |
|
273 | - return false; |
|
274 | - } |
|
275 | - $urls = []; |
|
276 | - foreach ( $entity['claims'][$propertyId] as $claim ) { |
|
277 | - $urls[] = $claim['mainsnak']['datavalue']['value']; |
|
278 | - } |
|
279 | - |
|
280 | - return $urls; |
|
281 | - } |
|
282 | - |
|
283 | - /** |
|
284 | - * @param string $entityId |
|
285 | - * @param string $propertyId |
|
286 | - * @return array|bool |
|
287 | - */ |
|
288 | - public function getPropertyOfTypeExternalIdentifier( $entityId, $propertyId ) { |
|
289 | - $entity = $this->getEntity( $entityId ); |
|
290 | - if ( !isset( $entity['claims'][$propertyId] ) ) { |
|
291 | - return false; |
|
292 | - } |
|
293 | - $idents = []; |
|
294 | - foreach ( $entity['claims'][$propertyId] as $claim ) { |
|
295 | - $qualifiers = []; |
|
296 | - if ( !isset( $claim['qualifiers'] ) ) { |
|
297 | - continue; |
|
298 | - } |
|
299 | - foreach ( $claim['qualifiers'] as $qualsInfo ) { |
|
300 | - foreach ( $qualsInfo as $qualInfo ) { |
|
301 | - $qualProp = self::factory( $qualInfo['property'], $this->lang, $this->cache ); |
|
302 | - $propLabel = $qualProp->getLabel(); |
|
303 | - if ( !isset( $qualifiers[$propLabel] ) ) { |
|
304 | - $qualifiers[$propLabel] = []; |
|
305 | - } |
|
306 | - $qualifiers[$propLabel][] = $qualInfo['datavalue']['value']; |
|
307 | - } |
|
308 | - } |
|
309 | - $idents[] = [ |
|
310 | - 'qualifiers' => $qualifiers, |
|
311 | - 'value' => $claim['mainsnak']['datavalue']['value'], |
|
312 | - ]; |
|
313 | - } |
|
314 | - |
|
315 | - return $idents; |
|
316 | - } |
|
317 | - |
|
318 | - /** |
|
319 | - * Get a single-valued text property. |
|
320 | - * @param string $property One of the PROP_* constants. |
|
321 | - * @return string|bool The value, or false if it can't be found. |
|
322 | - */ |
|
323 | - public function getPropertyOfTypeText( $property ) { |
|
324 | - $entity = $this->getEntity( $this->id ); |
|
325 | - if ( isset( $entity['claims'][$property] ) ) { |
|
326 | - // Use the first title. |
|
327 | - foreach ( $entity['claims'][$property] as $t ) { |
|
328 | - if ( !isset( $t['mainsnak']['datavalue']['value']['language'] ) ) { |
|
329 | - var_dump( $t['mainsnak']['datavalue']['value'] ); |
|
330 | - exit(); |
|
331 | - } |
|
332 | - if ( $t['mainsnak']['datavalue']['value']['language'] == $this->lang |
|
333 | - && !empty( $t['mainsnak']['datavalue']['value']['text'] ) |
|
334 | - ) { |
|
335 | - return $t['mainsnak']['datavalue']['value']['text']; |
|
336 | - } |
|
337 | - } |
|
338 | - } |
|
339 | - return false; |
|
340 | - } |
|
341 | - |
|
342 | - /** |
|
343 | - * Literal data field for a quantity that relates to some kind of well-defined unit. |
|
344 | - * The actual unit goes in the data values that is entered. |
|
345 | - * - amount – implicit part of the string (mapping of unit prefix is unclear) |
|
346 | - * - unit – implicit part of the string that defaults to "1" (mapping to standardizing |
|
347 | - * body is unclear) |
|
348 | - * - upperbound - quantity's upper bound |
|
349 | - * - lowerbound - quantity's lower bound |
|
350 | - * @param string $property |
|
351 | - * @return mixed[]|bool If it's not false it's an array with 'amount', 'unit', etc. |
|
352 | - */ |
|
353 | - public function getPropertyOfTypeQuantity( $property ) { |
|
354 | - $quantities = []; |
|
355 | - $entity = $this->getEntity( $this->id ); |
|
356 | - if ( !isset( $entity['claims'][$property] ) ) { |
|
357 | - return false; |
|
358 | - } |
|
359 | - foreach ( $entity['claims'][$property] as $t ) { |
|
360 | - $quantity = $t['mainsnak']['datavalue']['value']; |
|
361 | - $unitId = substr( $quantity['unit'], strlen( $this->wikidataUrlBase ) + 1 ); |
|
362 | - $quantity['unit'] = self::factory( $unitId, $this->lang, $this->cache ); |
|
363 | - $quantities[] = $quantity; |
|
364 | - } |
|
365 | - return $quantities; |
|
366 | - } |
|
367 | - |
|
368 | - /** |
|
369 | - * Set a single-valued text property. |
|
370 | - * @param string $property One of the PROP_* constants. |
|
371 | - * @param string $value The value. |
|
372 | - */ |
|
373 | - public function setPropertyOfTypeText( $property, $value ) { |
|
374 | - // First see if this property already exists, and that it is different from what's being set. |
|
375 | - $entity = $this->getEntity( $this->id ); |
|
376 | - if ( !empty( $entity['claims'][$property] ) ) { |
|
377 | - // Find this language's claim (if there is one). |
|
378 | - foreach ( $entity['claims'][$property] as $claim ) { |
|
379 | - if ( $claim['mainsnak']['datavalue']['value']['language'] == $this->lang ) { |
|
380 | - // Modify this claim's text value. |
|
381 | - $titleClaim = $claim; |
|
382 | - $titleClaim['mainsnak']['datavalue']['value']['text'] = $value; |
|
383 | - $setTitleParams = [ |
|
384 | - 'action' => 'wbsetclaim', |
|
385 | - 'claim' => \GuzzleHttp\json_encode( $titleClaim ), |
|
386 | - ]; |
|
387 | - continue; |
|
388 | - } |
|
389 | - } |
|
390 | - } |
|
391 | - |
|
392 | - // If no claim was found (and modified) above, create a new claim. |
|
393 | - if ( !isset( $setTitleParams ) ) { |
|
394 | - $setTitleParams = [ |
|
395 | - 'action' => 'wbcreateclaim', |
|
396 | - 'entity' => $this->getId(), |
|
397 | - 'property' => $property, |
|
398 | - 'snaktype' => 'value', |
|
399 | - 'value' => \GuzzleHttp\json_encode( [ 'text' => $value, 'language' => $this->lang ] ), |
|
400 | - ]; |
|
401 | - } |
|
402 | - |
|
403 | - // Save the property. |
|
404 | - $wdWpOauth = new WdWpOauth(); |
|
405 | - $wdWpOauth->makeCall( $setTitleParams, true ); |
|
406 | - |
|
407 | - // Clear the cache. |
|
408 | - $this->cache->deleteItem( $this->getEntityCacheKey( $this->id ) ); |
|
409 | - } |
|
410 | - |
|
411 | - /** |
|
412 | - * Does this item exist? |
|
413 | - * @return bool |
|
414 | - */ |
|
415 | - public function exists() { |
|
416 | - return $this->getId() !== false; |
|
417 | - } |
|
418 | - |
|
419 | - /** |
|
420 | - * @return array With 'html' and 'title' keys. |
|
421 | - */ |
|
422 | - public function getWikipediaIntro() { |
|
423 | - $cacheKey = 'wikipedia-intro-' . $this->id . $this->lang; |
|
424 | - if ( $this->cache->hasItem( $cacheKey ) ) { |
|
425 | - return $this->cache->getItem( $cacheKey )->get(); |
|
426 | - } |
|
427 | - $entity = $this->getEntity( $this->id ); |
|
428 | - if ( !isset( $entity['sitelinks'] ) ) { |
|
429 | - return []; |
|
430 | - } |
|
431 | - foreach ( $entity['sitelinks'] as $sitelink ) { |
|
432 | - if ( $sitelink['site'] == $this->lang . 'wiki' ) { |
|
433 | - $api = new MediawikiApi( 'https://' . $this->lang . '.wikipedia.org/w/api.php' ); |
|
434 | - $req = new SimpleRequest( 'query', [ |
|
435 | - 'prop' => 'extracts', |
|
436 | - 'exintro' => true, |
|
437 | - 'titles' => $sitelink['title'], |
|
438 | - ] ); |
|
439 | - $response = $api->getRequest( $req ); |
|
440 | - $page = array_shift( $response['query']['pages'] ); |
|
441 | - $out = [ |
|
442 | - 'title' => $page['title'], |
|
443 | - 'html' => $page['extract'], |
|
444 | - ]; |
|
445 | - $cacheItem = $this->cache->getItem( $cacheKey ) |
|
446 | - ->expiresAfter( new DateInterval( 'P1D' ) ) |
|
447 | - ->set( $out ); |
|
448 | - $this->cache->save( $cacheItem ); |
|
449 | - |
|
450 | - return $out; |
|
451 | - } |
|
452 | - } |
|
453 | - |
|
454 | - return []; |
|
455 | - } |
|
456 | - |
|
457 | - /** |
|
458 | - * Get the raw entity data from the 'wbgetentities' API call. |
|
459 | - * @param string|null $id The Q-number. |
|
460 | - * @param bool $ignoreCache |
|
461 | - * @return array|bool |
|
462 | - */ |
|
463 | - public function getEntity( $id = null, $ignoreCache = false ) { |
|
464 | - $idActual = $id ?: $this->id; |
|
465 | - $cacheKey = $this->getEntityCacheKey( $idActual ); |
|
466 | - if ( !$ignoreCache && $this->cache->hasItem( $cacheKey ) ) { |
|
467 | - return $this->cache->getItem( $cacheKey )->get(); |
|
468 | - } |
|
469 | - $metadataRequest = new SimpleRequest( 'wbgetentities', [ 'ids' => $idActual ] ); |
|
470 | - $itemResult = $this->wdApi->getRequest( $metadataRequest ); |
|
471 | - if ( !isset( $itemResult['success'] ) || !isset( $itemResult['entities'][$id] ) ) { |
|
472 | - return false; |
|
473 | - } |
|
474 | - $metadata = $itemResult['entities'][$idActual]; |
|
475 | - $cacheItem = $this->cache->getItem( $cacheKey ) |
|
476 | - ->expiresAfter( new DateInterval( 'PT10M' ) ) |
|
477 | - ->set( $metadata ); |
|
478 | - $this->cache->save( $cacheItem ); |
|
479 | - return $metadata; |
|
480 | - } |
|
481 | - |
|
482 | - /** |
|
483 | - * @param string $id |
|
484 | - * |
|
485 | - * @return string |
|
486 | - */ |
|
487 | - protected function getEntityCacheKey( $id ) { |
|
488 | - return 'entities' . $id; |
|
489 | - } |
|
198 | + // } |
|
199 | + } |
|
200 | + return $times; |
|
201 | + } |
|
202 | + |
|
203 | + /** |
|
204 | + * Get the Item that is referred to by the specified item's property. |
|
205 | + * |
|
206 | + * @param string $propertyId |
|
207 | + * |
|
208 | + * @return \Samwilson\SimpleWikidata\Properties\Item[] |
|
209 | + */ |
|
210 | + public function getPropertyOfTypeItem( $propertyId ) { |
|
211 | + $entity = $this->getEntity( $this->id ); |
|
212 | + if ( !isset( $entity['claims'][$propertyId] ) ) { |
|
213 | + return []; |
|
214 | + } |
|
215 | + $items = []; |
|
216 | + foreach ( $entity['claims'][$propertyId] as $claim ) { |
|
217 | + $items[] = new Properties\Item( $claim, $this->lang, $this->cache ); |
|
218 | + } |
|
219 | + |
|
220 | + return $items; |
|
221 | + } |
|
222 | + |
|
223 | + /** |
|
224 | + * Set a claim to the given Item. |
|
225 | + * @param string $property Property ID, with 'P'. |
|
226 | + * @param string $itemId Item ID with 'Q'. |
|
227 | + */ |
|
228 | + public function setPropertyOfTypeItem( $property, $itemId ) { |
|
229 | + $itemIdNumeric = substr( $itemId, 1 ); |
|
230 | + |
|
231 | + // First see if this property already exists, and that it is different from what's being set. |
|
232 | + $entity = $this->getEntity( $this->id ); |
|
233 | + if ( !empty( $entity['claims'][$property] ) ) { |
|
234 | + // Get the first claim, and update it if necessary. |
|
235 | + $claim = array_shift( $entity['claims'][$property] ); |
|
236 | + if ( $claim['mainsnak']['datavalue']['value']['id'] == $itemId ) { |
|
237 | + // Already is the required value, no need to change. |
|
238 | + return; |
|
239 | + } |
|
240 | + $claim['mainsnak']['datavalue']['value']['id'] = $itemId; |
|
241 | + $claim['mainsnak']['datavalue']['value']['numeric-id'] = $itemIdNumeric; |
|
242 | + $apiParams = [ |
|
243 | + 'action' => 'wbsetclaim', |
|
244 | + 'claim' => json_encode( $claim ), |
|
245 | + ]; |
|
246 | + } |
|
247 | + |
|
248 | + // If no claim was found (and modified) above, create a new claim. |
|
249 | + if ( !isset( $apiParams ) ) { |
|
250 | + $apiParams = [ |
|
251 | + 'action' => 'wbcreateclaim', |
|
252 | + 'entity' => $this->getId(), |
|
253 | + 'property' => $property, |
|
254 | + 'snaktype' => 'value', |
|
255 | + 'value' => json_encode( [ 'entity-type' => 'item', 'numeric-id' => $itemIdNumeric ] ), |
|
256 | + ]; |
|
257 | + } |
|
258 | + |
|
259 | + // @TODO Save the property. |
|
260 | + |
|
261 | + // Clear the cache. |
|
262 | + $this->cache->deleteItem( $this->getEntityCacheKey( $this->id ) ); |
|
263 | + } |
|
264 | + |
|
265 | + /** |
|
266 | + * @param string $entityId |
|
267 | + * @param string $propertyId |
|
268 | + * @return array|bool |
|
269 | + */ |
|
270 | + public function getPropertyOfTypeUrl( $entityId, $propertyId ) { |
|
271 | + $entity = $this->getEntity( $entityId ); |
|
272 | + if ( !isset( $entity['claims'][$propertyId] ) ) { |
|
273 | + return false; |
|
274 | + } |
|
275 | + $urls = []; |
|
276 | + foreach ( $entity['claims'][$propertyId] as $claim ) { |
|
277 | + $urls[] = $claim['mainsnak']['datavalue']['value']; |
|
278 | + } |
|
279 | + |
|
280 | + return $urls; |
|
281 | + } |
|
282 | + |
|
283 | + /** |
|
284 | + * @param string $entityId |
|
285 | + * @param string $propertyId |
|
286 | + * @return array|bool |
|
287 | + */ |
|
288 | + public function getPropertyOfTypeExternalIdentifier( $entityId, $propertyId ) { |
|
289 | + $entity = $this->getEntity( $entityId ); |
|
290 | + if ( !isset( $entity['claims'][$propertyId] ) ) { |
|
291 | + return false; |
|
292 | + } |
|
293 | + $idents = []; |
|
294 | + foreach ( $entity['claims'][$propertyId] as $claim ) { |
|
295 | + $qualifiers = []; |
|
296 | + if ( !isset( $claim['qualifiers'] ) ) { |
|
297 | + continue; |
|
298 | + } |
|
299 | + foreach ( $claim['qualifiers'] as $qualsInfo ) { |
|
300 | + foreach ( $qualsInfo as $qualInfo ) { |
|
301 | + $qualProp = self::factory( $qualInfo['property'], $this->lang, $this->cache ); |
|
302 | + $propLabel = $qualProp->getLabel(); |
|
303 | + if ( !isset( $qualifiers[$propLabel] ) ) { |
|
304 | + $qualifiers[$propLabel] = []; |
|
305 | + } |
|
306 | + $qualifiers[$propLabel][] = $qualInfo['datavalue']['value']; |
|
307 | + } |
|
308 | + } |
|
309 | + $idents[] = [ |
|
310 | + 'qualifiers' => $qualifiers, |
|
311 | + 'value' => $claim['mainsnak']['datavalue']['value'], |
|
312 | + ]; |
|
313 | + } |
|
314 | + |
|
315 | + return $idents; |
|
316 | + } |
|
317 | + |
|
318 | + /** |
|
319 | + * Get a single-valued text property. |
|
320 | + * @param string $property One of the PROP_* constants. |
|
321 | + * @return string|bool The value, or false if it can't be found. |
|
322 | + */ |
|
323 | + public function getPropertyOfTypeText( $property ) { |
|
324 | + $entity = $this->getEntity( $this->id ); |
|
325 | + if ( isset( $entity['claims'][$property] ) ) { |
|
326 | + // Use the first title. |
|
327 | + foreach ( $entity['claims'][$property] as $t ) { |
|
328 | + if ( !isset( $t['mainsnak']['datavalue']['value']['language'] ) ) { |
|
329 | + var_dump( $t['mainsnak']['datavalue']['value'] ); |
|
330 | + exit(); |
|
331 | + } |
|
332 | + if ( $t['mainsnak']['datavalue']['value']['language'] == $this->lang |
|
333 | + && !empty( $t['mainsnak']['datavalue']['value']['text'] ) |
|
334 | + ) { |
|
335 | + return $t['mainsnak']['datavalue']['value']['text']; |
|
336 | + } |
|
337 | + } |
|
338 | + } |
|
339 | + return false; |
|
340 | + } |
|
341 | + |
|
342 | + /** |
|
343 | + * Literal data field for a quantity that relates to some kind of well-defined unit. |
|
344 | + * The actual unit goes in the data values that is entered. |
|
345 | + * - amount – implicit part of the string (mapping of unit prefix is unclear) |
|
346 | + * - unit – implicit part of the string that defaults to "1" (mapping to standardizing |
|
347 | + * body is unclear) |
|
348 | + * - upperbound - quantity's upper bound |
|
349 | + * - lowerbound - quantity's lower bound |
|
350 | + * @param string $property |
|
351 | + * @return mixed[]|bool If it's not false it's an array with 'amount', 'unit', etc. |
|
352 | + */ |
|
353 | + public function getPropertyOfTypeQuantity( $property ) { |
|
354 | + $quantities = []; |
|
355 | + $entity = $this->getEntity( $this->id ); |
|
356 | + if ( !isset( $entity['claims'][$property] ) ) { |
|
357 | + return false; |
|
358 | + } |
|
359 | + foreach ( $entity['claims'][$property] as $t ) { |
|
360 | + $quantity = $t['mainsnak']['datavalue']['value']; |
|
361 | + $unitId = substr( $quantity['unit'], strlen( $this->wikidataUrlBase ) + 1 ); |
|
362 | + $quantity['unit'] = self::factory( $unitId, $this->lang, $this->cache ); |
|
363 | + $quantities[] = $quantity; |
|
364 | + } |
|
365 | + return $quantities; |
|
366 | + } |
|
367 | + |
|
368 | + /** |
|
369 | + * Set a single-valued text property. |
|
370 | + * @param string $property One of the PROP_* constants. |
|
371 | + * @param string $value The value. |
|
372 | + */ |
|
373 | + public function setPropertyOfTypeText( $property, $value ) { |
|
374 | + // First see if this property already exists, and that it is different from what's being set. |
|
375 | + $entity = $this->getEntity( $this->id ); |
|
376 | + if ( !empty( $entity['claims'][$property] ) ) { |
|
377 | + // Find this language's claim (if there is one). |
|
378 | + foreach ( $entity['claims'][$property] as $claim ) { |
|
379 | + if ( $claim['mainsnak']['datavalue']['value']['language'] == $this->lang ) { |
|
380 | + // Modify this claim's text value. |
|
381 | + $titleClaim = $claim; |
|
382 | + $titleClaim['mainsnak']['datavalue']['value']['text'] = $value; |
|
383 | + $setTitleParams = [ |
|
384 | + 'action' => 'wbsetclaim', |
|
385 | + 'claim' => \GuzzleHttp\json_encode( $titleClaim ), |
|
386 | + ]; |
|
387 | + continue; |
|
388 | + } |
|
389 | + } |
|
390 | + } |
|
391 | + |
|
392 | + // If no claim was found (and modified) above, create a new claim. |
|
393 | + if ( !isset( $setTitleParams ) ) { |
|
394 | + $setTitleParams = [ |
|
395 | + 'action' => 'wbcreateclaim', |
|
396 | + 'entity' => $this->getId(), |
|
397 | + 'property' => $property, |
|
398 | + 'snaktype' => 'value', |
|
399 | + 'value' => \GuzzleHttp\json_encode( [ 'text' => $value, 'language' => $this->lang ] ), |
|
400 | + ]; |
|
401 | + } |
|
402 | + |
|
403 | + // Save the property. |
|
404 | + $wdWpOauth = new WdWpOauth(); |
|
405 | + $wdWpOauth->makeCall( $setTitleParams, true ); |
|
406 | + |
|
407 | + // Clear the cache. |
|
408 | + $this->cache->deleteItem( $this->getEntityCacheKey( $this->id ) ); |
|
409 | + } |
|
410 | + |
|
411 | + /** |
|
412 | + * Does this item exist? |
|
413 | + * @return bool |
|
414 | + */ |
|
415 | + public function exists() { |
|
416 | + return $this->getId() !== false; |
|
417 | + } |
|
418 | + |
|
419 | + /** |
|
420 | + * @return array With 'html' and 'title' keys. |
|
421 | + */ |
|
422 | + public function getWikipediaIntro() { |
|
423 | + $cacheKey = 'wikipedia-intro-' . $this->id . $this->lang; |
|
424 | + if ( $this->cache->hasItem( $cacheKey ) ) { |
|
425 | + return $this->cache->getItem( $cacheKey )->get(); |
|
426 | + } |
|
427 | + $entity = $this->getEntity( $this->id ); |
|
428 | + if ( !isset( $entity['sitelinks'] ) ) { |
|
429 | + return []; |
|
430 | + } |
|
431 | + foreach ( $entity['sitelinks'] as $sitelink ) { |
|
432 | + if ( $sitelink['site'] == $this->lang . 'wiki' ) { |
|
433 | + $api = new MediawikiApi( 'https://' . $this->lang . '.wikipedia.org/w/api.php' ); |
|
434 | + $req = new SimpleRequest( 'query', [ |
|
435 | + 'prop' => 'extracts', |
|
436 | + 'exintro' => true, |
|
437 | + 'titles' => $sitelink['title'], |
|
438 | + ] ); |
|
439 | + $response = $api->getRequest( $req ); |
|
440 | + $page = array_shift( $response['query']['pages'] ); |
|
441 | + $out = [ |
|
442 | + 'title' => $page['title'], |
|
443 | + 'html' => $page['extract'], |
|
444 | + ]; |
|
445 | + $cacheItem = $this->cache->getItem( $cacheKey ) |
|
446 | + ->expiresAfter( new DateInterval( 'P1D' ) ) |
|
447 | + ->set( $out ); |
|
448 | + $this->cache->save( $cacheItem ); |
|
449 | + |
|
450 | + return $out; |
|
451 | + } |
|
452 | + } |
|
453 | + |
|
454 | + return []; |
|
455 | + } |
|
456 | + |
|
457 | + /** |
|
458 | + * Get the raw entity data from the 'wbgetentities' API call. |
|
459 | + * @param string|null $id The Q-number. |
|
460 | + * @param bool $ignoreCache |
|
461 | + * @return array|bool |
|
462 | + */ |
|
463 | + public function getEntity( $id = null, $ignoreCache = false ) { |
|
464 | + $idActual = $id ?: $this->id; |
|
465 | + $cacheKey = $this->getEntityCacheKey( $idActual ); |
|
466 | + if ( !$ignoreCache && $this->cache->hasItem( $cacheKey ) ) { |
|
467 | + return $this->cache->getItem( $cacheKey )->get(); |
|
468 | + } |
|
469 | + $metadataRequest = new SimpleRequest( 'wbgetentities', [ 'ids' => $idActual ] ); |
|
470 | + $itemResult = $this->wdApi->getRequest( $metadataRequest ); |
|
471 | + if ( !isset( $itemResult['success'] ) || !isset( $itemResult['entities'][$id] ) ) { |
|
472 | + return false; |
|
473 | + } |
|
474 | + $metadata = $itemResult['entities'][$idActual]; |
|
475 | + $cacheItem = $this->cache->getItem( $cacheKey ) |
|
476 | + ->expiresAfter( new DateInterval( 'PT10M' ) ) |
|
477 | + ->set( $metadata ); |
|
478 | + $this->cache->save( $cacheItem ); |
|
479 | + return $metadata; |
|
480 | + } |
|
481 | + |
|
482 | + /** |
|
483 | + * @param string $id |
|
484 | + * |
|
485 | + * @return string |
|
486 | + */ |
|
487 | + protected function getEntityCacheKey( $id ) { |
|
488 | + return 'entities' . $id; |
|
489 | + } |
|
490 | 490 | } |
@@ -38,12 +38,12 @@ discard block |
||
38 | 38 | /** @var string The base URL of Wikidata, with trailing slash. */ |
39 | 39 | protected $wikidataUrlBase = 'https://www.wikidata.org/wiki/'; |
40 | 40 | |
41 | - private function __construct( $id, $lang, CacheItemPoolInterface $cache ) { |
|
42 | - if ( !is_string( $id ) || preg_match( '/[QP][0-9]*/i', $id ) !== 1 ) { |
|
43 | - throw new Exception( "Not a valid ID: " . var_export( $id, true ) ); |
|
41 | + private function __construct($id, $lang, CacheItemPoolInterface $cache) { |
|
42 | + if (!is_string($id) || preg_match('/[QP][0-9]*/i', $id) !== 1) { |
|
43 | + throw new Exception("Not a valid ID: " . var_export($id, true)); |
|
44 | 44 | } |
45 | 45 | $this->id = $id; |
46 | - $this->wdApi = new MediawikiApi( 'https://www.wikidata.org/w/api.php' ); |
|
46 | + $this->wdApi = new MediawikiApi('https://www.wikidata.org/w/api.php'); |
|
47 | 47 | $this->entities = []; |
48 | 48 | $this->lang = $lang; |
49 | 49 | $this->cache = $cache; |
@@ -57,21 +57,21 @@ discard block |
||
57 | 57 | * @param CacheItemPoolInterface $cache The cache to use. |
58 | 58 | * @return Item |
59 | 59 | */ |
60 | - public static function factory( $id, $lang, CacheItemPoolInterface $cache ) { |
|
61 | - $item = new Item( $id, $lang, $cache ); |
|
62 | - foreach ( $item->getPropertyOfTypeItem( self::PROP_INSTANCE_OF ) as $instanceOf ) { |
|
60 | + public static function factory($id, $lang, CacheItemPoolInterface $cache) { |
|
61 | + $item = new Item($id, $lang, $cache); |
|
62 | + foreach ($item->getPropertyOfTypeItem(self::PROP_INSTANCE_OF) as $instanceOf) { |
|
63 | 63 | // Try to find a class matching the registered 'instance of'. |
64 | - foreach ( static::$registeredClasses as $classId => $className ) { |
|
64 | + foreach (static::$registeredClasses as $classId => $className) { |
|
65 | 65 | // If this 'instance of' is registered, use it. |
66 | - if ( $classId === $instanceOf->getItem()->getId() ) { |
|
66 | + if ($classId === $instanceOf->getItem()->getId()) { |
|
67 | 67 | // This won't re-request the metadata, because that's cached. |
68 | - return new $className( $id, $lang, $cache ); |
|
68 | + return new $className($id, $lang, $cache); |
|
69 | 69 | } |
70 | 70 | } |
71 | 71 | } |
72 | 72 | |
73 | 73 | // If we're here, just leave it as a basic Item. |
74 | - $item->setCache( $cache ); |
|
74 | + $item->setCache($cache); |
|
75 | 75 | return $item; |
76 | 76 | } |
77 | 77 | |
@@ -81,19 +81,19 @@ discard block |
||
81 | 81 | * @throws Exception if called on Item or the registering class does not have INSTANCE_OF set. |
82 | 82 | */ |
83 | 83 | public static function register() { |
84 | - if ( static::class === self::class ) { |
|
85 | - throw new Exception( __METHOD__ . ' should only be called on subclasses of Item' ); |
|
84 | + if (static::class === self::class) { |
|
85 | + throw new Exception(__METHOD__ . ' should only be called on subclasses of Item'); |
|
86 | 86 | } |
87 | - if ( !static::INSTANCE_OF ) { |
|
88 | - throw new Exception( 'Please set INSTANCE_OF for ' . static::class ); |
|
87 | + if (!static::INSTANCE_OF) { |
|
88 | + throw new Exception('Please set INSTANCE_OF for ' . static::class); |
|
89 | 89 | } |
90 | - static::$registeredClasses[ static::INSTANCE_OF ] = static::class; |
|
90 | + static::$registeredClasses[static::INSTANCE_OF] = static::class; |
|
91 | 91 | } |
92 | 92 | |
93 | 93 | /** |
94 | 94 | * @param CacheItemPoolInterface $cache The cache to use. |
95 | 95 | */ |
96 | - public function setCache( CacheItemPoolInterface $cache ) { |
|
96 | + public function setCache(CacheItemPoolInterface $cache) { |
|
97 | 97 | $this->cache = $cache; |
98 | 98 | } |
99 | 99 | |
@@ -102,8 +102,8 @@ discard block |
||
102 | 102 | * @return string|bool The ID or false if it couldn't be determined. |
103 | 103 | */ |
104 | 104 | public function getId() { |
105 | - $entity = $this->getEntity( $this->id ); |
|
106 | - return isset( $entity['id'] ) ? $entity['id'] : false; |
|
105 | + $entity = $this->getEntity($this->id); |
|
106 | + return isset($entity['id']) ? $entity['id'] : false; |
|
107 | 107 | } |
108 | 108 | |
109 | 109 | /** |
@@ -111,10 +111,10 @@ discard block |
||
111 | 111 | * @return string |
112 | 112 | */ |
113 | 113 | public function getLabel() { |
114 | - $entity = $this->getEntity( $this->id ); |
|
115 | - if ( !empty( $entity['labels'][ $this->lang ]['value'] ) ) { |
|
114 | + $entity = $this->getEntity($this->id); |
|
115 | + if (!empty($entity['labels'][$this->lang]['value'])) { |
|
116 | 116 | // Use the label if there is one. |
117 | - return $entity['labels'][ $this->lang ]['value']; |
|
117 | + return $entity['labels'][$this->lang]['value']; |
|
118 | 118 | } |
119 | 119 | // Or just use the ID. |
120 | 120 | return $entity['id']; |
@@ -139,33 +139,33 @@ discard block |
||
139 | 139 | * @param string $type |
140 | 140 | * @return array |
141 | 141 | */ |
142 | - public function getStandardProperties( $wikiProject = 'WikiProject_Books', $type = 'work' ) { |
|
143 | - if ( $type !== 'work' ) { |
|
142 | + public function getStandardProperties($wikiProject = 'WikiProject_Books', $type = 'work') { |
|
143 | + if ($type !== 'work') { |
|
144 | 144 | $type = 'edition'; |
145 | 145 | } |
146 | 146 | $cacheKey = $type . '_item_property_IDs'; |
147 | - if ( $this->cache->hasItem( $cacheKey ) ) { |
|
148 | - $propIds = $this->cache->getItem( $cacheKey )->get(); |
|
147 | + if ($this->cache->hasItem($cacheKey)) { |
|
148 | + $propIds = $this->cache->getItem($cacheKey)->get(); |
|
149 | 149 | } else { |
150 | 150 | $domCrawler = new Crawler(); |
151 | 151 | $wikiProjectUrl = 'https://www.wikidata.org/wiki/Wikidata:' . $wikiProject; |
152 | - $domCrawler->addHtmlContent( file_get_contents( $wikiProjectUrl ) ); |
|
153 | - $propAncors = "//h3/span[@id='" . ucfirst( $type ) . "_item_properties']" |
|
152 | + $domCrawler->addHtmlContent(file_get_contents($wikiProjectUrl)); |
|
153 | + $propAncors = "//h3/span[@id='" . ucfirst($type) . "_item_properties']" |
|
154 | 154 | . "/../following-sibling::table[1]//td[2]/a"; |
155 | - $propCells = $domCrawler->filterXPath( $propAncors ); |
|
155 | + $propCells = $domCrawler->filterXPath($propAncors); |
|
156 | 156 | $propIds = []; |
157 | - $propCells->each( function ( Crawler $node, $i ) use ( &$propIds ) { |
|
157 | + $propCells->each(function(Crawler $node, $i) use (&$propIds) { |
|
158 | 158 | $propId = $node->text(); |
159 | 159 | $propIds[] = $propId; |
160 | 160 | } ); |
161 | - $cacheItem = $this->cache->getItem( $cacheKey ) |
|
162 | - ->expiresAfter( new DateInterval( 'PT1H' ) ) |
|
163 | - ->set( $propIds ); |
|
164 | - $this->cache->save( $cacheItem ); |
|
161 | + $cacheItem = $this->cache->getItem($cacheKey) |
|
162 | + ->expiresAfter(new DateInterval('PT1H')) |
|
163 | + ->set($propIds); |
|
164 | + $this->cache->save($cacheItem); |
|
165 | 165 | } |
166 | 166 | $workProperties = []; |
167 | - foreach ( $propIds as $propId ) { |
|
168 | - $workProperties[] = self::factory( $propId, $this->lang, $this->cache ); |
|
167 | + foreach ($propIds as $propId) { |
|
168 | + $workProperties[] = self::factory($propId, $this->lang, $this->cache); |
|
169 | 169 | } |
170 | 170 | |
171 | 171 | return $workProperties; |
@@ -175,17 +175,17 @@ discard block |
||
175 | 175 | * @param string $propertyId |
176 | 176 | * @return bool|Time[] |
177 | 177 | */ |
178 | - public function getPropertyOfTypeTime( $propertyId ) { |
|
178 | + public function getPropertyOfTypeTime($propertyId) { |
|
179 | 179 | $times = []; |
180 | 180 | $entity = $this->getEntity(); |
181 | - if ( !isset( $entity['claims'][$propertyId] ) ) { |
|
181 | + if (!isset($entity['claims'][$propertyId])) { |
|
182 | 182 | // No statements for this property. |
183 | 183 | return $times; |
184 | 184 | } |
185 | 185 | // print_r($entity['claims'][$propertyId]);exit(); |
186 | - foreach ( $entity['claims'][$propertyId] as $claim ) { |
|
186 | + foreach ($entity['claims'][$propertyId] as $claim) { |
|
187 | 187 | // print_r($claim); |
188 | - $times[] = new Time( $claim, $this->lang, $this->cache ); |
|
188 | + $times[] = new Time($claim, $this->lang, $this->cache); |
|
189 | 189 | // |
190 | 190 | // $timeValue = $claim['datavalue']['value']['time']; |
191 | 191 | // // Ugly workaround for imprecise dates. :-( |
@@ -207,14 +207,14 @@ discard block |
||
207 | 207 | * |
208 | 208 | * @return \Samwilson\SimpleWikidata\Properties\Item[] |
209 | 209 | */ |
210 | - public function getPropertyOfTypeItem( $propertyId ) { |
|
211 | - $entity = $this->getEntity( $this->id ); |
|
212 | - if ( !isset( $entity['claims'][$propertyId] ) ) { |
|
210 | + public function getPropertyOfTypeItem($propertyId) { |
|
211 | + $entity = $this->getEntity($this->id); |
|
212 | + if (!isset($entity['claims'][$propertyId])) { |
|
213 | 213 | return []; |
214 | 214 | } |
215 | 215 | $items = []; |
216 | - foreach ( $entity['claims'][$propertyId] as $claim ) { |
|
217 | - $items[] = new Properties\Item( $claim, $this->lang, $this->cache ); |
|
216 | + foreach ($entity['claims'][$propertyId] as $claim) { |
|
217 | + $items[] = new Properties\Item($claim, $this->lang, $this->cache); |
|
218 | 218 | } |
219 | 219 | |
220 | 220 | return $items; |
@@ -225,15 +225,15 @@ discard block |
||
225 | 225 | * @param string $property Property ID, with 'P'. |
226 | 226 | * @param string $itemId Item ID with 'Q'. |
227 | 227 | */ |
228 | - public function setPropertyOfTypeItem( $property, $itemId ) { |
|
229 | - $itemIdNumeric = substr( $itemId, 1 ); |
|
228 | + public function setPropertyOfTypeItem($property, $itemId) { |
|
229 | + $itemIdNumeric = substr($itemId, 1); |
|
230 | 230 | |
231 | 231 | // First see if this property already exists, and that it is different from what's being set. |
232 | - $entity = $this->getEntity( $this->id ); |
|
233 | - if ( !empty( $entity['claims'][$property] ) ) { |
|
232 | + $entity = $this->getEntity($this->id); |
|
233 | + if (!empty($entity['claims'][$property])) { |
|
234 | 234 | // Get the first claim, and update it if necessary. |
235 | - $claim = array_shift( $entity['claims'][$property] ); |
|
236 | - if ( $claim['mainsnak']['datavalue']['value']['id'] == $itemId ) { |
|
235 | + $claim = array_shift($entity['claims'][$property]); |
|
236 | + if ($claim['mainsnak']['datavalue']['value']['id'] == $itemId) { |
|
237 | 237 | // Already is the required value, no need to change. |
238 | 238 | return; |
239 | 239 | } |
@@ -241,25 +241,25 @@ discard block |
||
241 | 241 | $claim['mainsnak']['datavalue']['value']['numeric-id'] = $itemIdNumeric; |
242 | 242 | $apiParams = [ |
243 | 243 | 'action' => 'wbsetclaim', |
244 | - 'claim' => json_encode( $claim ), |
|
244 | + 'claim' => json_encode($claim), |
|
245 | 245 | ]; |
246 | 246 | } |
247 | 247 | |
248 | 248 | // If no claim was found (and modified) above, create a new claim. |
249 | - if ( !isset( $apiParams ) ) { |
|
249 | + if (!isset($apiParams)) { |
|
250 | 250 | $apiParams = [ |
251 | 251 | 'action' => 'wbcreateclaim', |
252 | 252 | 'entity' => $this->getId(), |
253 | 253 | 'property' => $property, |
254 | 254 | 'snaktype' => 'value', |
255 | - 'value' => json_encode( [ 'entity-type' => 'item', 'numeric-id' => $itemIdNumeric ] ), |
|
255 | + 'value' => json_encode(['entity-type' => 'item', 'numeric-id' => $itemIdNumeric]), |
|
256 | 256 | ]; |
257 | 257 | } |
258 | 258 | |
259 | 259 | // @TODO Save the property. |
260 | 260 | |
261 | 261 | // Clear the cache. |
262 | - $this->cache->deleteItem( $this->getEntityCacheKey( $this->id ) ); |
|
262 | + $this->cache->deleteItem($this->getEntityCacheKey($this->id)); |
|
263 | 263 | } |
264 | 264 | |
265 | 265 | /** |
@@ -267,13 +267,13 @@ discard block |
||
267 | 267 | * @param string $propertyId |
268 | 268 | * @return array|bool |
269 | 269 | */ |
270 | - public function getPropertyOfTypeUrl( $entityId, $propertyId ) { |
|
271 | - $entity = $this->getEntity( $entityId ); |
|
272 | - if ( !isset( $entity['claims'][$propertyId] ) ) { |
|
270 | + public function getPropertyOfTypeUrl($entityId, $propertyId) { |
|
271 | + $entity = $this->getEntity($entityId); |
|
272 | + if (!isset($entity['claims'][$propertyId])) { |
|
273 | 273 | return false; |
274 | 274 | } |
275 | 275 | $urls = []; |
276 | - foreach ( $entity['claims'][$propertyId] as $claim ) { |
|
276 | + foreach ($entity['claims'][$propertyId] as $claim) { |
|
277 | 277 | $urls[] = $claim['mainsnak']['datavalue']['value']; |
278 | 278 | } |
279 | 279 | |
@@ -285,22 +285,22 @@ discard block |
||
285 | 285 | * @param string $propertyId |
286 | 286 | * @return array|bool |
287 | 287 | */ |
288 | - public function getPropertyOfTypeExternalIdentifier( $entityId, $propertyId ) { |
|
289 | - $entity = $this->getEntity( $entityId ); |
|
290 | - if ( !isset( $entity['claims'][$propertyId] ) ) { |
|
288 | + public function getPropertyOfTypeExternalIdentifier($entityId, $propertyId) { |
|
289 | + $entity = $this->getEntity($entityId); |
|
290 | + if (!isset($entity['claims'][$propertyId])) { |
|
291 | 291 | return false; |
292 | 292 | } |
293 | 293 | $idents = []; |
294 | - foreach ( $entity['claims'][$propertyId] as $claim ) { |
|
294 | + foreach ($entity['claims'][$propertyId] as $claim) { |
|
295 | 295 | $qualifiers = []; |
296 | - if ( !isset( $claim['qualifiers'] ) ) { |
|
296 | + if (!isset($claim['qualifiers'])) { |
|
297 | 297 | continue; |
298 | 298 | } |
299 | - foreach ( $claim['qualifiers'] as $qualsInfo ) { |
|
300 | - foreach ( $qualsInfo as $qualInfo ) { |
|
301 | - $qualProp = self::factory( $qualInfo['property'], $this->lang, $this->cache ); |
|
299 | + foreach ($claim['qualifiers'] as $qualsInfo) { |
|
300 | + foreach ($qualsInfo as $qualInfo) { |
|
301 | + $qualProp = self::factory($qualInfo['property'], $this->lang, $this->cache); |
|
302 | 302 | $propLabel = $qualProp->getLabel(); |
303 | - if ( !isset( $qualifiers[$propLabel] ) ) { |
|
303 | + if (!isset($qualifiers[$propLabel])) { |
|
304 | 304 | $qualifiers[$propLabel] = []; |
305 | 305 | } |
306 | 306 | $qualifiers[$propLabel][] = $qualInfo['datavalue']['value']; |
@@ -320,17 +320,17 @@ discard block |
||
320 | 320 | * @param string $property One of the PROP_* constants. |
321 | 321 | * @return string|bool The value, or false if it can't be found. |
322 | 322 | */ |
323 | - public function getPropertyOfTypeText( $property ) { |
|
324 | - $entity = $this->getEntity( $this->id ); |
|
325 | - if ( isset( $entity['claims'][$property] ) ) { |
|
323 | + public function getPropertyOfTypeText($property) { |
|
324 | + $entity = $this->getEntity($this->id); |
|
325 | + if (isset($entity['claims'][$property])) { |
|
326 | 326 | // Use the first title. |
327 | - foreach ( $entity['claims'][$property] as $t ) { |
|
328 | - if ( !isset( $t['mainsnak']['datavalue']['value']['language'] ) ) { |
|
329 | - var_dump( $t['mainsnak']['datavalue']['value'] ); |
|
327 | + foreach ($entity['claims'][$property] as $t) { |
|
328 | + if (!isset($t['mainsnak']['datavalue']['value']['language'])) { |
|
329 | + var_dump($t['mainsnak']['datavalue']['value']); |
|
330 | 330 | exit(); |
331 | 331 | } |
332 | - if ( $t['mainsnak']['datavalue']['value']['language'] == $this->lang |
|
333 | - && !empty( $t['mainsnak']['datavalue']['value']['text'] ) |
|
332 | + if ($t['mainsnak']['datavalue']['value']['language'] == $this->lang |
|
333 | + && !empty($t['mainsnak']['datavalue']['value']['text']) |
|
334 | 334 | ) { |
335 | 335 | return $t['mainsnak']['datavalue']['value']['text']; |
336 | 336 | } |
@@ -350,16 +350,16 @@ discard block |
||
350 | 350 | * @param string $property |
351 | 351 | * @return mixed[]|bool If it's not false it's an array with 'amount', 'unit', etc. |
352 | 352 | */ |
353 | - public function getPropertyOfTypeQuantity( $property ) { |
|
353 | + public function getPropertyOfTypeQuantity($property) { |
|
354 | 354 | $quantities = []; |
355 | - $entity = $this->getEntity( $this->id ); |
|
356 | - if ( !isset( $entity['claims'][$property] ) ) { |
|
355 | + $entity = $this->getEntity($this->id); |
|
356 | + if (!isset($entity['claims'][$property])) { |
|
357 | 357 | return false; |
358 | 358 | } |
359 | - foreach ( $entity['claims'][$property] as $t ) { |
|
359 | + foreach ($entity['claims'][$property] as $t) { |
|
360 | 360 | $quantity = $t['mainsnak']['datavalue']['value']; |
361 | - $unitId = substr( $quantity['unit'], strlen( $this->wikidataUrlBase ) + 1 ); |
|
362 | - $quantity['unit'] = self::factory( $unitId, $this->lang, $this->cache ); |
|
361 | + $unitId = substr($quantity['unit'], strlen($this->wikidataUrlBase) + 1); |
|
362 | + $quantity['unit'] = self::factory($unitId, $this->lang, $this->cache); |
|
363 | 363 | $quantities[] = $quantity; |
364 | 364 | } |
365 | 365 | return $quantities; |
@@ -370,19 +370,19 @@ discard block |
||
370 | 370 | * @param string $property One of the PROP_* constants. |
371 | 371 | * @param string $value The value. |
372 | 372 | */ |
373 | - public function setPropertyOfTypeText( $property, $value ) { |
|
373 | + public function setPropertyOfTypeText($property, $value) { |
|
374 | 374 | // First see if this property already exists, and that it is different from what's being set. |
375 | - $entity = $this->getEntity( $this->id ); |
|
376 | - if ( !empty( $entity['claims'][$property] ) ) { |
|
375 | + $entity = $this->getEntity($this->id); |
|
376 | + if (!empty($entity['claims'][$property])) { |
|
377 | 377 | // Find this language's claim (if there is one). |
378 | - foreach ( $entity['claims'][$property] as $claim ) { |
|
379 | - if ( $claim['mainsnak']['datavalue']['value']['language'] == $this->lang ) { |
|
378 | + foreach ($entity['claims'][$property] as $claim) { |
|
379 | + if ($claim['mainsnak']['datavalue']['value']['language'] == $this->lang) { |
|
380 | 380 | // Modify this claim's text value. |
381 | 381 | $titleClaim = $claim; |
382 | 382 | $titleClaim['mainsnak']['datavalue']['value']['text'] = $value; |
383 | 383 | $setTitleParams = [ |
384 | 384 | 'action' => 'wbsetclaim', |
385 | - 'claim' => \GuzzleHttp\json_encode( $titleClaim ), |
|
385 | + 'claim' => \GuzzleHttp\json_encode($titleClaim), |
|
386 | 386 | ]; |
387 | 387 | continue; |
388 | 388 | } |
@@ -390,22 +390,22 @@ discard block |
||
390 | 390 | } |
391 | 391 | |
392 | 392 | // If no claim was found (and modified) above, create a new claim. |
393 | - if ( !isset( $setTitleParams ) ) { |
|
393 | + if (!isset($setTitleParams)) { |
|
394 | 394 | $setTitleParams = [ |
395 | 395 | 'action' => 'wbcreateclaim', |
396 | 396 | 'entity' => $this->getId(), |
397 | 397 | 'property' => $property, |
398 | 398 | 'snaktype' => 'value', |
399 | - 'value' => \GuzzleHttp\json_encode( [ 'text' => $value, 'language' => $this->lang ] ), |
|
399 | + 'value' => \GuzzleHttp\json_encode(['text' => $value, 'language' => $this->lang]), |
|
400 | 400 | ]; |
401 | 401 | } |
402 | 402 | |
403 | 403 | // Save the property. |
404 | 404 | $wdWpOauth = new WdWpOauth(); |
405 | - $wdWpOauth->makeCall( $setTitleParams, true ); |
|
405 | + $wdWpOauth->makeCall($setTitleParams, true); |
|
406 | 406 | |
407 | 407 | // Clear the cache. |
408 | - $this->cache->deleteItem( $this->getEntityCacheKey( $this->id ) ); |
|
408 | + $this->cache->deleteItem($this->getEntityCacheKey($this->id)); |
|
409 | 409 | } |
410 | 410 | |
411 | 411 | /** |
@@ -421,31 +421,31 @@ discard block |
||
421 | 421 | */ |
422 | 422 | public function getWikipediaIntro() { |
423 | 423 | $cacheKey = 'wikipedia-intro-' . $this->id . $this->lang; |
424 | - if ( $this->cache->hasItem( $cacheKey ) ) { |
|
425 | - return $this->cache->getItem( $cacheKey )->get(); |
|
424 | + if ($this->cache->hasItem($cacheKey)) { |
|
425 | + return $this->cache->getItem($cacheKey)->get(); |
|
426 | 426 | } |
427 | - $entity = $this->getEntity( $this->id ); |
|
428 | - if ( !isset( $entity['sitelinks'] ) ) { |
|
427 | + $entity = $this->getEntity($this->id); |
|
428 | + if (!isset($entity['sitelinks'])) { |
|
429 | 429 | return []; |
430 | 430 | } |
431 | - foreach ( $entity['sitelinks'] as $sitelink ) { |
|
432 | - if ( $sitelink['site'] == $this->lang . 'wiki' ) { |
|
433 | - $api = new MediawikiApi( 'https://' . $this->lang . '.wikipedia.org/w/api.php' ); |
|
434 | - $req = new SimpleRequest( 'query', [ |
|
431 | + foreach ($entity['sitelinks'] as $sitelink) { |
|
432 | + if ($sitelink['site'] == $this->lang . 'wiki') { |
|
433 | + $api = new MediawikiApi('https://' . $this->lang . '.wikipedia.org/w/api.php'); |
|
434 | + $req = new SimpleRequest('query', [ |
|
435 | 435 | 'prop' => 'extracts', |
436 | 436 | 'exintro' => true, |
437 | 437 | 'titles' => $sitelink['title'], |
438 | - ] ); |
|
439 | - $response = $api->getRequest( $req ); |
|
440 | - $page = array_shift( $response['query']['pages'] ); |
|
438 | + ]); |
|
439 | + $response = $api->getRequest($req); |
|
440 | + $page = array_shift($response['query']['pages']); |
|
441 | 441 | $out = [ |
442 | 442 | 'title' => $page['title'], |
443 | 443 | 'html' => $page['extract'], |
444 | 444 | ]; |
445 | - $cacheItem = $this->cache->getItem( $cacheKey ) |
|
446 | - ->expiresAfter( new DateInterval( 'P1D' ) ) |
|
447 | - ->set( $out ); |
|
448 | - $this->cache->save( $cacheItem ); |
|
445 | + $cacheItem = $this->cache->getItem($cacheKey) |
|
446 | + ->expiresAfter(new DateInterval('P1D')) |
|
447 | + ->set($out); |
|
448 | + $this->cache->save($cacheItem); |
|
449 | 449 | |
450 | 450 | return $out; |
451 | 451 | } |
@@ -460,22 +460,22 @@ discard block |
||
460 | 460 | * @param bool $ignoreCache |
461 | 461 | * @return array|bool |
462 | 462 | */ |
463 | - public function getEntity( $id = null, $ignoreCache = false ) { |
|
463 | + public function getEntity($id = null, $ignoreCache = false) { |
|
464 | 464 | $idActual = $id ?: $this->id; |
465 | - $cacheKey = $this->getEntityCacheKey( $idActual ); |
|
466 | - if ( !$ignoreCache && $this->cache->hasItem( $cacheKey ) ) { |
|
467 | - return $this->cache->getItem( $cacheKey )->get(); |
|
465 | + $cacheKey = $this->getEntityCacheKey($idActual); |
|
466 | + if (!$ignoreCache && $this->cache->hasItem($cacheKey)) { |
|
467 | + return $this->cache->getItem($cacheKey)->get(); |
|
468 | 468 | } |
469 | - $metadataRequest = new SimpleRequest( 'wbgetentities', [ 'ids' => $idActual ] ); |
|
470 | - $itemResult = $this->wdApi->getRequest( $metadataRequest ); |
|
471 | - if ( !isset( $itemResult['success'] ) || !isset( $itemResult['entities'][$id] ) ) { |
|
469 | + $metadataRequest = new SimpleRequest('wbgetentities', ['ids' => $idActual]); |
|
470 | + $itemResult = $this->wdApi->getRequest($metadataRequest); |
|
471 | + if (!isset($itemResult['success']) || !isset($itemResult['entities'][$id])) { |
|
472 | 472 | return false; |
473 | 473 | } |
474 | 474 | $metadata = $itemResult['entities'][$idActual]; |
475 | - $cacheItem = $this->cache->getItem( $cacheKey ) |
|
476 | - ->expiresAfter( new DateInterval( 'PT10M' ) ) |
|
477 | - ->set( $metadata ); |
|
478 | - $this->cache->save( $cacheItem ); |
|
475 | + $cacheItem = $this->cache->getItem($cacheKey) |
|
476 | + ->expiresAfter(new DateInterval('PT10M')) |
|
477 | + ->set($metadata); |
|
478 | + $this->cache->save($cacheItem); |
|
479 | 479 | return $metadata; |
480 | 480 | } |
481 | 481 | |
@@ -484,7 +484,7 @@ discard block |
||
484 | 484 | * |
485 | 485 | * @return string |
486 | 486 | */ |
487 | - protected function getEntityCacheKey( $id ) { |
|
487 | + protected function getEntityCacheKey($id) { |
|
488 | 488 | return 'entities' . $id; |
489 | 489 | } |
490 | 490 | } |
@@ -6,37 +6,37 @@ |
||
6 | 6 | |
7 | 7 | abstract class Property { |
8 | 8 | |
9 | - /** @var string[] */ |
|
10 | - protected $claim; |
|
11 | - |
|
12 | - /** @var string */ |
|
13 | - protected $lang; |
|
14 | - |
|
15 | - /** @var CacheItemPoolInterface */ |
|
16 | - protected $cache; |
|
17 | - |
|
18 | - /** |
|
19 | - * @param string $claim The claim data array. |
|
20 | - * @param string $lang The language code. |
|
21 | - * @param CacheItemPoolInterface $cache |
|
22 | - */ |
|
23 | - public function __construct( array $claim, $lang, CacheItemPoolInterface $cache ) { |
|
24 | - $this->claim = $claim; |
|
25 | - $this->lang = $lang; |
|
26 | - $this->cache = $cache; |
|
27 | - } |
|
28 | - |
|
29 | - /** |
|
30 | - * @return Reference[] |
|
31 | - */ |
|
32 | - public function getReferences() { |
|
33 | - $references = []; |
|
34 | - if ( !isset( $this->claim['references'] ) ) { |
|
35 | - return $references; |
|
36 | - } |
|
37 | - foreach ( $this->claim['references'] as $ref ) { |
|
38 | - $references[] = new Reference( $ref, $this->lang, $this->cache ); |
|
39 | - } |
|
40 | - return $references; |
|
41 | - } |
|
9 | + /** @var string[] */ |
|
10 | + protected $claim; |
|
11 | + |
|
12 | + /** @var string */ |
|
13 | + protected $lang; |
|
14 | + |
|
15 | + /** @var CacheItemPoolInterface */ |
|
16 | + protected $cache; |
|
17 | + |
|
18 | + /** |
|
19 | + * @param string $claim The claim data array. |
|
20 | + * @param string $lang The language code. |
|
21 | + * @param CacheItemPoolInterface $cache |
|
22 | + */ |
|
23 | + public function __construct( array $claim, $lang, CacheItemPoolInterface $cache ) { |
|
24 | + $this->claim = $claim; |
|
25 | + $this->lang = $lang; |
|
26 | + $this->cache = $cache; |
|
27 | + } |
|
28 | + |
|
29 | + /** |
|
30 | + * @return Reference[] |
|
31 | + */ |
|
32 | + public function getReferences() { |
|
33 | + $references = []; |
|
34 | + if ( !isset( $this->claim['references'] ) ) { |
|
35 | + return $references; |
|
36 | + } |
|
37 | + foreach ( $this->claim['references'] as $ref ) { |
|
38 | + $references[] = new Reference( $ref, $this->lang, $this->cache ); |
|
39 | + } |
|
40 | + return $references; |
|
41 | + } |
|
42 | 42 | } |
@@ -20,7 +20,7 @@ discard block |
||
20 | 20 | * @param string $lang The language code. |
21 | 21 | * @param CacheItemPoolInterface $cache |
22 | 22 | */ |
23 | - public function __construct( array $claim, $lang, CacheItemPoolInterface $cache ) { |
|
23 | + public function __construct(array $claim, $lang, CacheItemPoolInterface $cache) { |
|
24 | 24 | $this->claim = $claim; |
25 | 25 | $this->lang = $lang; |
26 | 26 | $this->cache = $cache; |
@@ -31,11 +31,11 @@ discard block |
||
31 | 31 | */ |
32 | 32 | public function getReferences() { |
33 | 33 | $references = []; |
34 | - if ( !isset( $this->claim['references'] ) ) { |
|
34 | + if (!isset($this->claim['references'])) { |
|
35 | 35 | return $references; |
36 | 36 | } |
37 | - foreach ( $this->claim['references'] as $ref ) { |
|
38 | - $references[] = new Reference( $ref, $this->lang, $this->cache ); |
|
37 | + foreach ($this->claim['references'] as $ref) { |
|
38 | + $references[] = new Reference($ref, $this->lang, $this->cache); |
|
39 | 39 | } |
40 | 40 | return $references; |
41 | 41 | } |
@@ -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 Edition && $b instanceof Edition ) { |
|
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 Edition && $b instanceof Edition ) { |
|
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 Edition && $b instanceof Edition ) { |
|
91 | + usort($editions, function(Item $a, Item $b) { |
|
92 | + if ($a instanceof Edition && $b instanceof Edition) { |
|
93 | 93 | return $a->getPublicationYear() - $b->getPublicationYear(); |
94 | 94 | } |
95 | 95 | return 0; |
@@ -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 | } |
@@ -17,22 +17,22 @@ discard block |
||
17 | 17 | * @return string |
18 | 18 | */ |
19 | 19 | public function getPublicationYear() { |
20 | - $publicationYears = $this->getPropertyOfTypeTime( static::PROP_PUBLICATION_DATE ); |
|
21 | - return $publicationYears[0]->getDateTime()->format( 'Y' ); |
|
20 | + $publicationYears = $this->getPropertyOfTypeTime(static::PROP_PUBLICATION_DATE); |
|
21 | + return $publicationYears[0]->getDateTime()->format('Y'); |
|
22 | 22 | } |
23 | 23 | |
24 | 24 | /** |
25 | 25 | * @return \Samwilson\SimpleWikidata\Properties\Item[] |
26 | 26 | */ |
27 | 27 | public function getPublishers() { |
28 | - return $this->getPropertyOfTypeItem( static::PROP_PUBLISHER ); |
|
28 | + return $this->getPropertyOfTypeItem(static::PROP_PUBLISHER); |
|
29 | 29 | } |
30 | 30 | |
31 | 31 | /** |
32 | 32 | * @return array|bool |
33 | 33 | */ |
34 | 34 | public function getWikisourceIndexPages() { |
35 | - return $this->getPropertyOfTypeUrl( $this->getId(), static::PROP_WIKISOURCE_INDEX_PAGE ); |
|
35 | + return $this->getPropertyOfTypeUrl($this->getId(), static::PROP_WIKISOURCE_INDEX_PAGE); |
|
36 | 36 | } |
37 | 37 | |
38 | 38 | /** |
@@ -51,13 +51,13 @@ discard block |
||
51 | 51 | * @return string[] |
52 | 52 | */ |
53 | 53 | public function getWikisourceLink() { |
54 | - $entity = $this->getEntity( $this->id ); |
|
55 | - if ( !isset( $entity['sitelinks'] ) ) { |
|
54 | + $entity = $this->getEntity($this->id); |
|
55 | + if (!isset($entity['sitelinks'])) { |
|
56 | 56 | return []; |
57 | 57 | } |
58 | - foreach ( $entity['sitelinks'] as $sitelink ) { |
|
59 | - if ( strpos( $sitelink['site'], 'wikisource' ) !== false ) { |
|
60 | - $lang = substr( $sitelink['site'], 0, strpos( $sitelink['site'], 'wikisource' ) ); |
|
58 | + foreach ($entity['sitelinks'] as $sitelink) { |
|
59 | + if (strpos($sitelink['site'], 'wikisource') !== false) { |
|
60 | + $lang = substr($sitelink['site'], 0, strpos($sitelink['site'], 'wikisource')); |
|
61 | 61 | return [ |
62 | 62 | 'title' => $sitelink['title'], |
63 | 63 | 'url' => "https://$lang.wikisource.org/wiki/" . $sitelink['title'], |
@@ -13,31 +13,31 @@ discard block |
||
13 | 13 | * @link https://www.wikidata.org/wiki/Wikidata:WikiProject_Railways |
14 | 14 | */ |
15 | 15 | class RailwayStation extends \Samwilson\SimpleWikidata\Item { |
16 | - // @codingStandardsIgnoreEnd |
|
17 | - /** |
|
18 | - * Every subclass of Item should define its 'instance of' ID. |
|
19 | - */ |
|
20 | - const INSTANCE_OF = 'Q55488'; |
|
16 | + // @codingStandardsIgnoreEnd |
|
17 | + /** |
|
18 | + * Every subclass of Item should define its 'instance of' ID. |
|
19 | + */ |
|
20 | + const INSTANCE_OF = 'Q55488'; |
|
21 | 21 | |
22 | - /** |
|
23 | - * It can then contain any type-specific methods that are required. |
|
24 | - * @return RailwayStation[] |
|
25 | - */ |
|
26 | - public function hasAdjacentStations() { |
|
27 | - return $this->getPropertyOfTypeItem( 'P197' ); |
|
28 | - } |
|
22 | + /** |
|
23 | + * It can then contain any type-specific methods that are required. |
|
24 | + * @return RailwayStation[] |
|
25 | + */ |
|
26 | + public function hasAdjacentStations() { |
|
27 | + return $this->getPropertyOfTypeItem( 'P197' ); |
|
28 | + } |
|
29 | 29 | |
30 | - /** |
|
31 | - * Including queries. |
|
32 | - * @return RailwayStation[] |
|
33 | - */ |
|
34 | - public function isAdjacentTo() { |
|
35 | - $sparql = "SELECT ?item WHERE { ?item wdt:P197 wd:" . $this->getId() . " }"; |
|
36 | - $query = new \Samwilson\SimpleWikidata\Query( $sparql, $this->lang, $this->cache ); |
|
37 | - // Each query result will also be a RailwayStation |
|
38 | - // (if they're correctly recorded as such on Wikidata). |
|
39 | - return $query->getItems(); |
|
40 | - } |
|
30 | + /** |
|
31 | + * Including queries. |
|
32 | + * @return RailwayStation[] |
|
33 | + */ |
|
34 | + public function isAdjacentTo() { |
|
35 | + $sparql = "SELECT ?item WHERE { ?item wdt:P197 wd:" . $this->getId() . " }"; |
|
36 | + $query = new \Samwilson\SimpleWikidata\Query( $sparql, $this->lang, $this->cache ); |
|
37 | + // Each query result will also be a RailwayStation |
|
38 | + // (if they're correctly recorded as such on Wikidata). |
|
39 | + return $query->getItems(); |
|
40 | + } |
|
41 | 41 | } |
42 | 42 | |
43 | 43 | // Then the class must register itself. |
@@ -47,5 +47,5 @@ discard block |
||
47 | 47 | /** @var RailwayStation $eustonStation */ |
48 | 48 | $eustonStation = \Samwilson\SimpleWikidata\Item::factory( 'Q800751', 'en', $cache ); |
49 | 49 | echo $eustonStation->getLabel() |
50 | - . " has " . count( $eustonStation->hasAdjacentStations() ) . " adjacent stations" |
|
51 | - . " and is adjacent to " . count( $eustonStation->isAdjacentTo() ) . " stations.\n"; |
|
50 | + . " has " . count( $eustonStation->hasAdjacentStations() ) . " adjacent stations" |
|
51 | + . " and is adjacent to " . count( $eustonStation->isAdjacentTo() ) . " stations.\n"; |
@@ -3,7 +3,7 @@ discard block |
||
3 | 3 | require_once __DIR__ . '/../vendor/autoload.php'; |
4 | 4 | |
5 | 5 | // Any PSR6 cache can be used. |
6 | -$cache = new Stash\Pool( new \Stash\Driver\FileSystem() ); |
|
6 | +$cache = new Stash\Pool(new \Stash\Driver\FileSystem()); |
|
7 | 7 | |
8 | 8 | // @codingStandardsIgnoreStart |
9 | 9 | /** |
@@ -24,7 +24,7 @@ discard block |
||
24 | 24 | * @return RailwayStation[] |
25 | 25 | */ |
26 | 26 | public function hasAdjacentStations() { |
27 | - return $this->getPropertyOfTypeItem( 'P197' ); |
|
27 | + return $this->getPropertyOfTypeItem('P197'); |
|
28 | 28 | } |
29 | 29 | |
30 | 30 | /** |
@@ -33,7 +33,7 @@ discard block |
||
33 | 33 | */ |
34 | 34 | public function isAdjacentTo() { |
35 | 35 | $sparql = "SELECT ?item WHERE { ?item wdt:P197 wd:" . $this->getId() . " }"; |
36 | - $query = new \Samwilson\SimpleWikidata\Query( $sparql, $this->lang, $this->cache ); |
|
36 | + $query = new \Samwilson\SimpleWikidata\Query($sparql, $this->lang, $this->cache); |
|
37 | 37 | // Each query result will also be a RailwayStation |
38 | 38 | // (if they're correctly recorded as such on Wikidata). |
39 | 39 | return $query->getItems(); |
@@ -45,7 +45,7 @@ discard block |
||
45 | 45 | |
46 | 46 | // Then we can create items with the factory and use them. |
47 | 47 | /** @var RailwayStation $eustonStation */ |
48 | -$eustonStation = \Samwilson\SimpleWikidata\Item::factory( 'Q800751', 'en', $cache ); |
|
48 | +$eustonStation = \Samwilson\SimpleWikidata\Item::factory('Q800751', 'en', $cache); |
|
49 | 49 | echo $eustonStation->getLabel() |
50 | - . " has " . count( $eustonStation->hasAdjacentStations() ) . " adjacent stations" |
|
51 | - . " and is adjacent to " . count( $eustonStation->isAdjacentTo() ) . " stations.\n"; |
|
50 | + . " has " . count($eustonStation->hasAdjacentStations()) . " adjacent stations" |
|
51 | + . " and is adjacent to " . count($eustonStation->isAdjacentTo()) . " stations.\n"; |
@@ -11,12 +11,12 @@ |
||
11 | 11 | $query = new \Samwilson\SimpleWikidata\Query( $sparql, 'en', $cache ); |
12 | 12 | $hills = $query->getItems(); |
13 | 13 | foreach ( $hills as $hill ) { |
14 | - $heights = $hill->getPropertyOfTypeQuantity( 'P2044' ); |
|
15 | - if ( !$heights ) { |
|
16 | - echo "No heights found for " . $hill->getLabel() . "\n"; |
|
17 | - continue; |
|
18 | - } |
|
19 | - $height = array_shift( $heights ); |
|
20 | - echo $hill->getLabel() . " is " |
|
21 | - . $height['amount'] . " " . $height['unit']->getLabel() . " high.\n"; |
|
14 | + $heights = $hill->getPropertyOfTypeQuantity( 'P2044' ); |
|
15 | + if ( !$heights ) { |
|
16 | + echo "No heights found for " . $hill->getLabel() . "\n"; |
|
17 | + continue; |
|
18 | + } |
|
19 | + $height = array_shift( $heights ); |
|
20 | + echo $hill->getLabel() . " is " |
|
21 | + . $height['amount'] . " " . $height['unit']->getLabel() . " high.\n"; |
|
22 | 22 | } |
@@ -3,20 +3,20 @@ |
||
3 | 3 | require_once __DIR__ . '/../vendor/autoload.php'; |
4 | 4 | |
5 | 5 | // Any PSR6 cache can be used. |
6 | -$cache = new Stash\Pool( new \Stash\Driver\FileSystem() ); |
|
6 | +$cache = new Stash\Pool(new \Stash\Driver\FileSystem()); |
|
7 | 7 | |
8 | 8 | $sparql = 'SELECT ?item WHERE { |
9 | 9 | ?item wdt:P31 wd:Q54050 |
10 | 10 | } LIMIT 5'; |
11 | -$query = new \Samwilson\SimpleWikidata\Query( $sparql, 'en', $cache ); |
|
11 | +$query = new \Samwilson\SimpleWikidata\Query($sparql, 'en', $cache); |
|
12 | 12 | $hills = $query->getItems(); |
13 | -foreach ( $hills as $hill ) { |
|
14 | - $heights = $hill->getPropertyOfTypeQuantity( 'P2044' ); |
|
15 | - if ( !$heights ) { |
|
13 | +foreach ($hills as $hill) { |
|
14 | + $heights = $hill->getPropertyOfTypeQuantity('P2044'); |
|
15 | + if (!$heights) { |
|
16 | 16 | echo "No heights found for " . $hill->getLabel() . "\n"; |
17 | 17 | continue; |
18 | 18 | } |
19 | - $height = array_shift( $heights ); |
|
19 | + $height = array_shift($heights); |
|
20 | 20 | echo $hill->getLabel() . " is " |
21 | 21 | . $height['amount'] . " " . $height['unit']->getLabel() . " high.\n"; |
22 | 22 | } |
@@ -9,9 +9,9 @@ |
||
9 | 9 | $search = new \Samwilson\SimpleWikidata\Search( 'pride and prejudice', 'en', $cache ); |
10 | 10 | $items = $search->getItems( 3 ); |
11 | 11 | foreach ( $items as $item ) { |
12 | - $instanceOf = $item->getPropertyOfTypeItem( |
|
13 | - \Samwilson\SimpleWikidata\Item::PROP_INSTANCE_OF |
|
14 | - ); |
|
15 | - $instanceOfLabel = isset( $instanceOf[0] ) ? $instanceOf[0]->getItem()->getLabel() : 'UNKNOWN'; |
|
16 | - echo $item->getLabel() . ' (' . $instanceOfLabel . ')' . "\n"; |
|
12 | + $instanceOf = $item->getPropertyOfTypeItem( |
|
13 | + \Samwilson\SimpleWikidata\Item::PROP_INSTANCE_OF |
|
14 | + ); |
|
15 | + $instanceOfLabel = isset( $instanceOf[0] ) ? $instanceOf[0]->getItem()->getLabel() : 'UNKNOWN'; |
|
16 | + echo $item->getLabel() . ' (' . $instanceOfLabel . ')' . "\n"; |
|
17 | 17 | } |
@@ -3,15 +3,15 @@ |
||
3 | 3 | require_once __DIR__ . '/../vendor/autoload.php'; |
4 | 4 | |
5 | 5 | // Any PSR6 cache can be used. |
6 | -$cache = new Stash\Pool( new \Stash\Driver\FileSystem() ); |
|
6 | +$cache = new Stash\Pool(new \Stash\Driver\FileSystem()); |
|
7 | 7 | |
8 | 8 | // Search for anything. |
9 | -$search = new \Samwilson\SimpleWikidata\Search( 'pride and prejudice', 'en', $cache ); |
|
10 | -$items = $search->getItems( 3 ); |
|
11 | -foreach ( $items as $item ) { |
|
9 | +$search = new \Samwilson\SimpleWikidata\Search('pride and prejudice', 'en', $cache); |
|
10 | +$items = $search->getItems(3); |
|
11 | +foreach ($items as $item) { |
|
12 | 12 | $instanceOf = $item->getPropertyOfTypeItem( |
13 | 13 | \Samwilson\SimpleWikidata\Item::PROP_INSTANCE_OF |
14 | 14 | ); |
15 | - $instanceOfLabel = isset( $instanceOf[0] ) ? $instanceOf[0]->getItem()->getLabel() : 'UNKNOWN'; |
|
15 | + $instanceOfLabel = isset($instanceOf[0]) ? $instanceOf[0]->getItem()->getLabel() : 'UNKNOWN'; |
|
16 | 16 | echo $item->getLabel() . ' (' . $instanceOfLabel . ')' . "\n"; |
17 | 17 | } |
@@ -17,10 +17,10 @@ discard block |
||
17 | 17 | $datesOfBirth = $princeCharles->getDatesOfBirth(); |
18 | 18 | echo " Date of birth: " . $datesOfBirth[0]->getDateTime()->format( 'j F, Y' ) . " "; |
19 | 19 | foreach ( $datesOfBirth[0]->getReferences() as $ref ) { |
20 | - if ( $ref->statedIn() ) { |
|
21 | - echo "[$refNum]"; |
|
22 | - $references[$refNum] = $ref; |
|
23 | - } |
|
20 | + if ( $ref->statedIn() ) { |
|
21 | + echo "[$refNum]"; |
|
22 | + $references[$refNum] = $ref; |
|
23 | + } |
|
24 | 24 | } |
25 | 25 | echo "\n"; |
26 | 26 | |
@@ -28,13 +28,13 @@ discard block |
||
28 | 28 | $fathers = $princeCharles->fathers(); |
29 | 29 | echo " Father: " . $fathers[0]->getItem()->getLabel() . " "; |
30 | 30 | foreach ( $fathers[0]->getReferences() as $ref ) { |
31 | - if ( $ref->statedIn() ) { |
|
32 | - echo "[$refNum]"; |
|
33 | - $references[$refNum] = $ref; |
|
34 | - } |
|
31 | + if ( $ref->statedIn() ) { |
|
32 | + echo "[$refNum]"; |
|
33 | + $references[$refNum] = $ref; |
|
34 | + } |
|
35 | 35 | } |
36 | 36 | echo "\n"; |
37 | 37 | |
38 | 38 | foreach ( $references as $refNum => $ref ) { |
39 | - echo " [$refNum] - " . $ref->statedIn()->getLabel() . "\n"; |
|
39 | + echo " [$refNum] - " . $ref->statedIn()->getLabel() . "\n"; |
|
40 | 40 | } |
@@ -3,10 +3,10 @@ discard block |
||
3 | 3 | require_once __DIR__ . '/../vendor/autoload.php'; |
4 | 4 | |
5 | 5 | // Any PSR6 cache can be used. |
6 | -$cache = new Stash\Pool( new \Stash\Driver\FileSystem() ); |
|
6 | +$cache = new Stash\Pool(new \Stash\Driver\FileSystem()); |
|
7 | 7 | |
8 | 8 | /** @var \Samwilson\SimpleWikidata\Items\Human $princeCharles */ |
9 | -$princeCharles = Samwilson\SimpleWikidata\Item::factory( 'Q43274', 'en', $cache ); |
|
9 | +$princeCharles = Samwilson\SimpleWikidata\Item::factory('Q43274', 'en', $cache); |
|
10 | 10 | |
11 | 11 | echo $princeCharles->getLabel() . ":\n"; |
12 | 12 | |
@@ -15,9 +15,9 @@ discard block |
||
15 | 15 | |
16 | 16 | /** @var \Samwilson\SimpleWikidata\Properties\Time[] $datesOfBirth */ |
17 | 17 | $datesOfBirth = $princeCharles->getDatesOfBirth(); |
18 | -echo " Date of birth: " . $datesOfBirth[0]->getDateTime()->format( 'j F, Y' ) . " "; |
|
19 | -foreach ( $datesOfBirth[0]->getReferences() as $ref ) { |
|
20 | - if ( $ref->statedIn() ) { |
|
18 | +echo " Date of birth: " . $datesOfBirth[0]->getDateTime()->format('j F, Y') . " "; |
|
19 | +foreach ($datesOfBirth[0]->getReferences() as $ref) { |
|
20 | + if ($ref->statedIn()) { |
|
21 | 21 | echo "[$refNum]"; |
22 | 22 | $references[$refNum] = $ref; |
23 | 23 | } |
@@ -27,14 +27,14 @@ discard block |
||
27 | 27 | /** @var \Samwilson\SimpleWikidata\Properties\Item[] $fathers */ |
28 | 28 | $fathers = $princeCharles->fathers(); |
29 | 29 | echo " Father: " . $fathers[0]->getItem()->getLabel() . " "; |
30 | -foreach ( $fathers[0]->getReferences() as $ref ) { |
|
31 | - if ( $ref->statedIn() ) { |
|
30 | +foreach ($fathers[0]->getReferences() as $ref) { |
|
31 | + if ($ref->statedIn()) { |
|
32 | 32 | echo "[$refNum]"; |
33 | 33 | $references[$refNum] = $ref; |
34 | 34 | } |
35 | 35 | } |
36 | 36 | echo "\n"; |
37 | 37 | |
38 | -foreach ( $references as $refNum => $ref ) { |
|
38 | +foreach ($references as $refNum => $ref) { |
|
39 | 39 | echo " [$refNum] - " . $ref->statedIn()->getLabel() . "\n"; |
40 | 40 | } |