@@ -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 | } |
@@ -11,11 +11,11 @@ |
||
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 ".$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 ".$height['amount']." ".$height['unit']->getLabel()." high.\n"; |
|
21 | 21 | } |
@@ -8,95 +8,95 @@ |
||
8 | 8 | |
9 | 9 | class Work extends Item { |
10 | 10 | |
11 | - const ITEM_WORK = 'Q386724'; |
|
12 | - const PROP_SUBTITLE = 'P1680'; |
|
13 | - const PROP_GENRE = 'P136'; |
|
14 | - const PROP_SUBJECT = 'P921'; |
|
11 | + const ITEM_WORK = 'Q386724'; |
|
12 | + const PROP_SUBTITLE = 'P1680'; |
|
13 | + const PROP_GENRE = 'P136'; |
|
14 | + const PROP_SUBJECT = 'P921'; |
|
15 | 15 | |
16 | - /** |
|
17 | - * @param string $lang ISO639 language code. |
|
18 | - * @param CacheItemPoolInterface $cache The cache. |
|
19 | - * @return array|Item[] |
|
20 | - */ |
|
21 | - public static function getBookTypes( $lang = 'en', $cache ) { |
|
22 | - $sparql = "SELECT ?item WHERE { |
|
16 | + /** |
|
17 | + * @param string $lang ISO639 language code. |
|
18 | + * @param CacheItemPoolInterface $cache The cache. |
|
19 | + * @return array|Item[] |
|
20 | + */ |
|
21 | + public static function getBookTypes( $lang = 'en', $cache ) { |
|
22 | + $sparql = "SELECT ?item WHERE { |
|
23 | 23 | ?item wdt:P279 wd:Q571 . |
24 | 24 | ?item rdfs:label ?label . |
25 | 25 | FILTER(LANG(?label) = '$lang') . |
26 | 26 | } ORDER BY ?label "; |
27 | - $query = new Query( $sparql, $lang ); |
|
28 | - $query->setCache( $cache ); |
|
29 | - $bookType = Item::factory( self::ITEM_WORK, $lang, $cache ); |
|
30 | - return [ $bookType ] + $query->getItems(); |
|
31 | - } |
|
27 | + $query = new Query( $sparql, $lang ); |
|
28 | + $query->setCache( $cache ); |
|
29 | + $bookType = Item::factory( self::ITEM_WORK, $lang, $cache ); |
|
30 | + return [ $bookType ] + $query->getItems(); |
|
31 | + } |
|
32 | 32 | |
33 | - /** |
|
34 | - * @return bool|string |
|
35 | - */ |
|
36 | - public function getSubtitle() { |
|
37 | - return $this->getPropertyOfTypeText( self::PROP_SUBTITLE ); |
|
38 | - } |
|
33 | + /** |
|
34 | + * @return bool|string |
|
35 | + */ |
|
36 | + public function getSubtitle() { |
|
37 | + return $this->getPropertyOfTypeText( self::PROP_SUBTITLE ); |
|
38 | + } |
|
39 | 39 | |
40 | - /** |
|
41 | - * @param string $subtitle The new subtitle. |
|
42 | - */ |
|
43 | - public function setSubtitle( $subtitle ) { |
|
44 | - $this->setPropertyOfTypeText( self::PROP_SUBTITLE, $subtitle ); |
|
45 | - } |
|
40 | + /** |
|
41 | + * @param string $subtitle The new subtitle. |
|
42 | + */ |
|
43 | + public function setSubtitle( $subtitle ) { |
|
44 | + $this->setPropertyOfTypeText( self::PROP_SUBTITLE, $subtitle ); |
|
45 | + } |
|
46 | 46 | |
47 | - /** |
|
48 | - * @param string $property A property identifier. |
|
49 | - * @return array |
|
50 | - */ |
|
51 | - public function getPropertyOfTypeItems( $property ) { |
|
52 | - $entity = $this->getEntity( $this->id ); |
|
53 | - if ( ! isset( $entity['claims'][ $property ] ) ) { |
|
54 | - return []; |
|
55 | - } |
|
56 | - $items = []; |
|
57 | - foreach ( $entity['claims'][ $property ] as $authorClaim ) { |
|
58 | - $item_id = $authorClaim['mainsnak']['datavalue']['value']['id']; |
|
59 | - $items[] = Item::factory( $item_id, $this->lang, $this->cache ); |
|
60 | - } |
|
47 | + /** |
|
48 | + * @param string $property A property identifier. |
|
49 | + * @return array |
|
50 | + */ |
|
51 | + public function getPropertyOfTypeItems( $property ) { |
|
52 | + $entity = $this->getEntity( $this->id ); |
|
53 | + if ( ! isset( $entity['claims'][ $property ] ) ) { |
|
54 | + return []; |
|
55 | + } |
|
56 | + $items = []; |
|
57 | + foreach ( $entity['claims'][ $property ] as $authorClaim ) { |
|
58 | + $item_id = $authorClaim['mainsnak']['datavalue']['value']['id']; |
|
59 | + $items[] = Item::factory( $item_id, $this->lang, $this->cache ); |
|
60 | + } |
|
61 | 61 | |
62 | - return $items; |
|
63 | - } |
|
62 | + return $items; |
|
63 | + } |
|
64 | 64 | |
65 | - /** |
|
66 | - * @return array |
|
67 | - */ |
|
68 | - public function getAuthors() { |
|
69 | - return $this->getPropertyOfTypeItems( self::PROP_AUTHOR ); |
|
70 | - } |
|
65 | + /** |
|
66 | + * @return array |
|
67 | + */ |
|
68 | + public function getAuthors() { |
|
69 | + return $this->getPropertyOfTypeItems( self::PROP_AUTHOR ); |
|
70 | + } |
|
71 | 71 | |
72 | - /** |
|
73 | - * @return Item[] |
|
74 | - */ |
|
75 | - public function getSubjects() { |
|
76 | - return $this->getPropertyOfTypeItems( self::PROP_SUBJECT ); |
|
77 | - } |
|
72 | + /** |
|
73 | + * @return Item[] |
|
74 | + */ |
|
75 | + public function getSubjects() { |
|
76 | + return $this->getPropertyOfTypeItems( self::PROP_SUBJECT ); |
|
77 | + } |
|
78 | 78 | |
79 | - /** |
|
80 | - * @return Item[] |
|
81 | - */ |
|
82 | - public function getEditions() { |
|
83 | - $sparql = "SELECT ?item WHERE {" |
|
84 | - . " ?item wdt:" . self::PROP_EDITION_OR_TRANSLATION_OF . " wd:" . $this->getId() |
|
85 | - . "}"; |
|
86 | - $query = new Query( $sparql, $this->lang ); |
|
87 | - $query->setCache( $this->cache ); |
|
88 | - $editions = $query->getItems(); |
|
89 | - usort( $editions, function ( Item $a, Item $b ) { |
|
90 | - if ( $a instanceof EditionItem and $b instanceof EditionItem ) { |
|
91 | - return $a->getPublicationYear() - $b->getPublicationYear(); |
|
92 | - } |
|
93 | - return 0; |
|
94 | - } ); |
|
79 | + /** |
|
80 | + * @return Item[] |
|
81 | + */ |
|
82 | + public function getEditions() { |
|
83 | + $sparql = "SELECT ?item WHERE {" |
|
84 | + . " ?item wdt:" . self::PROP_EDITION_OR_TRANSLATION_OF . " wd:" . $this->getId() |
|
85 | + . "}"; |
|
86 | + $query = new Query( $sparql, $this->lang ); |
|
87 | + $query->setCache( $this->cache ); |
|
88 | + $editions = $query->getItems(); |
|
89 | + usort( $editions, function ( Item $a, Item $b ) { |
|
90 | + if ( $a instanceof EditionItem and $b instanceof EditionItem ) { |
|
91 | + return $a->getPublicationYear() - $b->getPublicationYear(); |
|
92 | + } |
|
93 | + return 0; |
|
94 | + } ); |
|
95 | 95 | |
96 | - return $editions; |
|
97 | - } |
|
96 | + return $editions; |
|
97 | + } |
|
98 | 98 | |
99 | - public function newEdition() { |
|
100 | - } |
|
99 | + public function newEdition() { |
|
100 | + } |
|
101 | 101 | |
102 | 102 | } |
@@ -6,65 +6,65 @@ |
||
6 | 6 | |
7 | 7 | class Edition extends Item { |
8 | 8 | |
9 | - const PROP_WIKISOURCE_INDEX_PAGE = 'P1957'; |
|
10 | - const PROP_SCANNED_FILE_ON_COMMONS = 'P996'; |
|
11 | - const PROP_INTERNET_ARCHIVE_ID = 'P724'; |
|
12 | - const PROP_PUBLICATION_DATE = ''; |
|
13 | - const PROP_PUBLISHER = ''; |
|
9 | + const PROP_WIKISOURCE_INDEX_PAGE = 'P1957'; |
|
10 | + const PROP_SCANNED_FILE_ON_COMMONS = 'P996'; |
|
11 | + const PROP_INTERNET_ARCHIVE_ID = 'P724'; |
|
12 | + const PROP_PUBLICATION_DATE = ''; |
|
13 | + const PROP_PUBLISHER = ''; |
|
14 | 14 | |
15 | - /** |
|
16 | - * @return string |
|
17 | - */ |
|
18 | - public function getPublicationYear() { |
|
19 | - $publicationYears = $this->getPropertyOfTypeTime( static::PROP_PUBLICATION_DATE ); |
|
20 | - return $publicationYears[0]->getDateTime()->format( 'Y' ); |
|
21 | - } |
|
15 | + /** |
|
16 | + * @return string |
|
17 | + */ |
|
18 | + public function getPublicationYear() { |
|
19 | + $publicationYears = $this->getPropertyOfTypeTime( static::PROP_PUBLICATION_DATE ); |
|
20 | + return $publicationYears[0]->getDateTime()->format( 'Y' ); |
|
21 | + } |
|
22 | 22 | |
23 | - /** |
|
24 | - * @return \Samwilson\SimpleWikidata\Properties\Item[] |
|
25 | - */ |
|
26 | - public function getPublishers() { |
|
27 | - return $this->getPropertyOfTypeItem( static::PROP_PUBLISHER ); |
|
28 | - } |
|
23 | + /** |
|
24 | + * @return \Samwilson\SimpleWikidata\Properties\Item[] |
|
25 | + */ |
|
26 | + public function getPublishers() { |
|
27 | + return $this->getPropertyOfTypeItem( static::PROP_PUBLISHER ); |
|
28 | + } |
|
29 | 29 | |
30 | - /** |
|
31 | - * @return array|bool |
|
32 | - */ |
|
33 | - public function getWikisourceIndexPages() { |
|
34 | - return $this->getPropertyOfTypeUrl( $this->getId(), static::PROP_WIKISOURCE_INDEX_PAGE ); |
|
35 | - } |
|
30 | + /** |
|
31 | + * @return array|bool |
|
32 | + */ |
|
33 | + public function getWikisourceIndexPages() { |
|
34 | + return $this->getPropertyOfTypeUrl( $this->getId(), static::PROP_WIKISOURCE_INDEX_PAGE ); |
|
35 | + } |
|
36 | 36 | |
37 | - /** |
|
38 | - * @return array |
|
39 | - */ |
|
40 | - public function internetArchiveIds() { |
|
41 | - return $this->getPropertyOfTypeExternalIdentifier( |
|
42 | - $this->getId(), |
|
43 | - self::PROP_INTERNET_ARCHIVE_ID |
|
44 | - ); |
|
45 | - } |
|
37 | + /** |
|
38 | + * @return array |
|
39 | + */ |
|
40 | + public function internetArchiveIds() { |
|
41 | + return $this->getPropertyOfTypeExternalIdentifier( |
|
42 | + $this->getId(), |
|
43 | + self::PROP_INTERNET_ARCHIVE_ID |
|
44 | + ); |
|
45 | + } |
|
46 | 46 | |
47 | - /** |
|
48 | - * Get information about the Wikisource sitelink. |
|
49 | - * An edition should only ever be present on one Wikisource. |
|
50 | - * @return string[] |
|
51 | - */ |
|
52 | - public function getWikisourceLink() { |
|
53 | - $entity = $this->getEntity( $this->id ); |
|
54 | - if ( !isset( $entity['sitelinks'] ) ) { |
|
55 | - return []; |
|
56 | - } |
|
57 | - foreach ( $entity['sitelinks'] as $sitelink ) { |
|
58 | - if ( strpos( $sitelink['site'], 'wikisource' ) !== false ) { |
|
59 | - $lang = substr( $sitelink['site'], 0, strpos( $sitelink['site'], 'wikisource' ) ); |
|
60 | - return [ |
|
61 | - 'title' => $sitelink['title'], |
|
62 | - 'url' => "https://$lang.wikisource.org/wiki/".$sitelink['title'], |
|
63 | - 'lang' => $lang, |
|
64 | - ]; |
|
65 | - } |
|
66 | - } |
|
67 | - return []; |
|
68 | - } |
|
47 | + /** |
|
48 | + * Get information about the Wikisource sitelink. |
|
49 | + * An edition should only ever be present on one Wikisource. |
|
50 | + * @return string[] |
|
51 | + */ |
|
52 | + public function getWikisourceLink() { |
|
53 | + $entity = $this->getEntity( $this->id ); |
|
54 | + if ( !isset( $entity['sitelinks'] ) ) { |
|
55 | + return []; |
|
56 | + } |
|
57 | + foreach ( $entity['sitelinks'] as $sitelink ) { |
|
58 | + if ( strpos( $sitelink['site'], 'wikisource' ) !== false ) { |
|
59 | + $lang = substr( $sitelink['site'], 0, strpos( $sitelink['site'], 'wikisource' ) ); |
|
60 | + return [ |
|
61 | + 'title' => $sitelink['title'], |
|
62 | + 'url' => "https://$lang.wikisource.org/wiki/".$sitelink['title'], |
|
63 | + 'lang' => $lang, |
|
64 | + ]; |
|
65 | + } |
|
66 | + } |
|
67 | + return []; |
|
68 | + } |
|
69 | 69 | |
70 | 70 | } |
@@ -6,18 +6,18 @@ |
||
6 | 6 | |
7 | 7 | class Human extends Item { |
8 | 8 | |
9 | - const PROP_DATE_OF_BIRTH = 'P569'; |
|
10 | - const PROP_FATHER = 'P22'; |
|
9 | + const PROP_DATE_OF_BIRTH = 'P569'; |
|
10 | + const PROP_FATHER = 'P22'; |
|
11 | 11 | |
12 | - public function getDatesOfBirth() { |
|
13 | - return $this->getPropertyOfTypeTime( self::PROP_DATE_OF_BIRTH ); |
|
14 | - } |
|
12 | + public function getDatesOfBirth() { |
|
13 | + return $this->getPropertyOfTypeTime( self::PROP_DATE_OF_BIRTH ); |
|
14 | + } |
|
15 | 15 | |
16 | - /** |
|
17 | - * @return Item[] |
|
18 | - */ |
|
19 | - public function fathers() { |
|
20 | - return $this->getPropertyOfTypeItem( self::PROP_FATHER ); |
|
21 | - } |
|
16 | + /** |
|
17 | + * @return Item[] |
|
18 | + */ |
|
19 | + public function fathers() { |
|
20 | + return $this->getPropertyOfTypeItem( self::PROP_FATHER ); |
|
21 | + } |
|
22 | 22 | |
23 | 23 | } |
@@ -27,48 +27,48 @@ |
||
27 | 27 | */ |
28 | 28 | class Time extends Property { |
29 | 29 | |
30 | - /** |
|
31 | - * @return DateTime |
|
32 | - */ |
|
33 | - public function getDateTime() { |
|
34 | - return new DateTime( $this->claim['mainsnak']['datavalue']['value']['time'] ); |
|
35 | - } |
|
30 | + /** |
|
31 | + * @return DateTime |
|
32 | + */ |
|
33 | + public function getDateTime() { |
|
34 | + return new DateTime( $this->claim['mainsnak']['datavalue']['value']['time'] ); |
|
35 | + } |
|
36 | 36 | |
37 | - /** |
|
38 | - * @return DateTimeZone |
|
39 | - */ |
|
40 | - public function getTimezone() { |
|
41 | - return new DateTimeZone( $this->claim['mainsnak']['datavalue']['value']['timezone'] ); |
|
42 | - } |
|
37 | + /** |
|
38 | + * @return DateTimeZone |
|
39 | + */ |
|
40 | + public function getTimezone() { |
|
41 | + return new DateTimeZone( $this->claim['mainsnak']['datavalue']['value']['timezone'] ); |
|
42 | + } |
|
43 | 43 | |
44 | - /** |
|
45 | - * @return mixed |
|
46 | - */ |
|
47 | - public function getBefore() { |
|
48 | - return $this->claim['mainsnak']['datavalue']['value']['before']; |
|
49 | - } |
|
44 | + /** |
|
45 | + * @return mixed |
|
46 | + */ |
|
47 | + public function getBefore() { |
|
48 | + return $this->claim['mainsnak']['datavalue']['value']['before']; |
|
49 | + } |
|
50 | 50 | |
51 | - /** |
|
52 | - * @return mixed |
|
53 | - */ |
|
54 | - public function getAfter() { |
|
55 | - return $this->claim['mainsnak']['datavalue']['value']['time']; |
|
56 | - } |
|
51 | + /** |
|
52 | + * @return mixed |
|
53 | + */ |
|
54 | + public function getAfter() { |
|
55 | + return $this->claim['mainsnak']['datavalue']['value']['time']; |
|
56 | + } |
|
57 | 57 | |
58 | - /** |
|
59 | - * @return int The numbers have the following meanings: 0 - billion years, |
|
60 | - * 1 - hundred million years, ..., |
|
61 | - * 6 - millennium, 7 - century, 8 - decade, 9 - year, 10 - month, 11 - day, 12 - hour, |
|
62 | - * 13 - minute, 14 - second. |
|
63 | - */ |
|
64 | - public function getPrecision() { |
|
65 | - return $this->claim['mainsnak']['datavalue']['value']['precision']; |
|
66 | - } |
|
58 | + /** |
|
59 | + * @return int The numbers have the following meanings: 0 - billion years, |
|
60 | + * 1 - hundred million years, ..., |
|
61 | + * 6 - millennium, 7 - century, 8 - decade, 9 - year, 10 - month, 11 - day, 12 - hour, |
|
62 | + * 13 - minute, 14 - second. |
|
63 | + */ |
|
64 | + public function getPrecision() { |
|
65 | + return $this->claim['mainsnak']['datavalue']['value']['precision']; |
|
66 | + } |
|
67 | 67 | |
68 | - /** |
|
69 | - * @return mixed |
|
70 | - */ |
|
71 | - public function getCalendarModel() { |
|
72 | - return $this->claim['mainsnak']['datavalue']['value']['calendarmodel']; |
|
73 | - } |
|
68 | + /** |
|
69 | + * @return mixed |
|
70 | + */ |
|
71 | + public function getCalendarModel() { |
|
72 | + return $this->claim['mainsnak']['datavalue']['value']['calendarmodel']; |
|
73 | + } |
|
74 | 74 | } |
@@ -6,11 +6,11 @@ |
||
6 | 6 | |
7 | 7 | class Item extends Property { |
8 | 8 | |
9 | - /** |
|
10 | - * @return \Samwilson\SimpleWikidata\Item |
|
11 | - */ |
|
12 | - public function getItem() { |
|
13 | - $itemId = $this->claim['mainsnak']['datavalue']['value']['id']; |
|
14 | - return \Samwilson\SimpleWikidata\Item::factory( $itemId, $this->lang, $this->cache ); |
|
15 | - } |
|
9 | + /** |
|
10 | + * @return \Samwilson\SimpleWikidata\Item |
|
11 | + */ |
|
12 | + public function getItem() { |
|
13 | + $itemId = $this->claim['mainsnak']['datavalue']['value']['id']; |
|
14 | + return \Samwilson\SimpleWikidata\Item::factory( $itemId, $this->lang, $this->cache ); |
|
15 | + } |
|
16 | 16 | } |
@@ -6,32 +6,32 @@ |
||
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 | - public function __construct( $claim, $lang, $cache ) { |
|
19 | - $this->claim = $claim; |
|
20 | - $this->lang = $lang; |
|
21 | - $this->cache = $cache; |
|
22 | - } |
|
23 | - |
|
24 | - /** |
|
25 | - * @return Reference[] |
|
26 | - */ |
|
27 | - public function getReferences() { |
|
28 | - $references = []; |
|
29 | - if ( !isset( $this->claim['references'] ) ) { |
|
30 | - return $references; |
|
31 | - } |
|
32 | - foreach ( $this->claim['references'] as $ref ) { |
|
33 | - $references[] = new Reference( $ref, $this->lang, $this->cache ); |
|
34 | - } |
|
35 | - return $references; |
|
36 | - } |
|
9 | + /** @var string[] */ |
|
10 | + protected $claim; |
|
11 | + |
|
12 | + /** @var string */ |
|
13 | + protected $lang; |
|
14 | + |
|
15 | + /** @var CacheItemPoolInterface */ |
|
16 | + protected $cache; |
|
17 | + |
|
18 | + public function __construct( $claim, $lang, $cache ) { |
|
19 | + $this->claim = $claim; |
|
20 | + $this->lang = $lang; |
|
21 | + $this->cache = $cache; |
|
22 | + } |
|
23 | + |
|
24 | + /** |
|
25 | + * @return Reference[] |
|
26 | + */ |
|
27 | + public function getReferences() { |
|
28 | + $references = []; |
|
29 | + if ( !isset( $this->claim['references'] ) ) { |
|
30 | + return $references; |
|
31 | + } |
|
32 | + foreach ( $this->claim['references'] as $ref ) { |
|
33 | + $references[] = new Reference( $ref, $this->lang, $this->cache ); |
|
34 | + } |
|
35 | + return $references; |
|
36 | + } |
|
37 | 37 | } |
@@ -8,78 +8,78 @@ |
||
8 | 8 | |
9 | 9 | class Query { |
10 | 10 | |
11 | - /** @var string */ |
|
12 | - protected $query; |
|
11 | + /** @var string */ |
|
12 | + protected $query; |
|
13 | 13 | |
14 | - /** @var string */ |
|
15 | - protected $lang; |
|
14 | + /** @var string */ |
|
15 | + protected $lang; |
|
16 | 16 | |
17 | - /** @var CacheItemPoolInterface */ |
|
18 | - protected $cache; |
|
17 | + /** @var CacheItemPoolInterface */ |
|
18 | + protected $cache; |
|
19 | 19 | |
20 | - /** |
|
21 | - * Query constructor. |
|
22 | - * @param string $query The Sparql query. |
|
23 | - * @param string $lang The language. |
|
24 | - * @param CacheItemPoolInterface $cache The cache. |
|
25 | - */ |
|
26 | - public function __construct( $query, $lang, CacheItemPoolInterface $cache ) { |
|
27 | - $this->query = $query; |
|
28 | - $this->lang = $lang; |
|
29 | - $this->cache = $cache; |
|
30 | - } |
|
20 | + /** |
|
21 | + * Query constructor. |
|
22 | + * @param string $query The Sparql query. |
|
23 | + * @param string $lang The language. |
|
24 | + * @param CacheItemPoolInterface $cache The cache. |
|
25 | + */ |
|
26 | + public function __construct( $query, $lang, CacheItemPoolInterface $cache ) { |
|
27 | + $this->query = $query; |
|
28 | + $this->lang = $lang; |
|
29 | + $this->cache = $cache; |
|
30 | + } |
|
31 | 31 | |
32 | - /** |
|
33 | - * Get the items. |
|
34 | - * @return Item[] The results. |
|
35 | - */ |
|
36 | - public function getItems() { |
|
37 | - $xml = $this->getXml( $this->query ); |
|
38 | - $results = []; |
|
39 | - foreach ( $xml->results->result as $res ) { |
|
40 | - $result = $this->getBindings( $res ); |
|
41 | - $id = substr( $result['item'], strrpos( $result['item'], '/' ) + 1 ); |
|
42 | - $item = Item::factory( $id, $this->lang, $this->cache ); |
|
43 | - $results[] = $item; |
|
44 | - } |
|
45 | - return $results; |
|
46 | - } |
|
32 | + /** |
|
33 | + * Get the items. |
|
34 | + * @return Item[] The results. |
|
35 | + */ |
|
36 | + public function getItems() { |
|
37 | + $xml = $this->getXml( $this->query ); |
|
38 | + $results = []; |
|
39 | + foreach ( $xml->results->result as $res ) { |
|
40 | + $result = $this->getBindings( $res ); |
|
41 | + $id = substr( $result['item'], strrpos( $result['item'], '/' ) + 1 ); |
|
42 | + $item = Item::factory( $id, $this->lang, $this->cache ); |
|
43 | + $results[] = $item; |
|
44 | + } |
|
45 | + return $results; |
|
46 | + } |
|
47 | 47 | |
48 | - /** |
|
49 | - * @param string $query The Sparql query. |
|
50 | - * @return SimpleXmlElement |
|
51 | - * @throws Exception |
|
52 | - */ |
|
53 | - protected function getXml( $query ) { |
|
54 | - $url = "https://query.wikidata.org/bigdata/namespace/wdq/sparql?query=" . urlencode( $query ); |
|
55 | - try { |
|
56 | - $result = file_get_contents( $url ); |
|
57 | - } catch ( Exception $e ) { |
|
58 | - throw new Exception( "Unable to run query: <pre>" . htmlspecialchars( $query ) . "</pre>", 500 ); |
|
59 | - } |
|
60 | - if ( empty( $result ) ) { |
|
61 | - $msg = "No result from query: <pre>" . htmlspecialchars( $query ) . "</pre>"; |
|
62 | - throw new Exception( $msg, 500 ); |
|
63 | - } |
|
64 | - $xml = new SimpleXmlElement( $result ); |
|
65 | - return $xml; |
|
66 | - } |
|
48 | + /** |
|
49 | + * @param string $query The Sparql query. |
|
50 | + * @return SimpleXmlElement |
|
51 | + * @throws Exception |
|
52 | + */ |
|
53 | + protected function getXml( $query ) { |
|
54 | + $url = "https://query.wikidata.org/bigdata/namespace/wdq/sparql?query=" . urlencode( $query ); |
|
55 | + try { |
|
56 | + $result = file_get_contents( $url ); |
|
57 | + } catch ( Exception $e ) { |
|
58 | + throw new Exception( "Unable to run query: <pre>" . htmlspecialchars( $query ) . "</pre>", 500 ); |
|
59 | + } |
|
60 | + if ( empty( $result ) ) { |
|
61 | + $msg = "No result from query: <pre>" . htmlspecialchars( $query ) . "</pre>"; |
|
62 | + throw new Exception( $msg, 500 ); |
|
63 | + } |
|
64 | + $xml = new SimpleXmlElement( $result ); |
|
65 | + return $xml; |
|
66 | + } |
|
67 | 67 | |
68 | - /** |
|
69 | - * @param SimpleXmlElement $xml The query result XML. |
|
70 | - * @return array |
|
71 | - */ |
|
72 | - protected function getBindings( $xml ) { |
|
73 | - $out = []; |
|
74 | - foreach ( $xml->binding as $binding ) { |
|
75 | - if ( isset( $binding->literal ) ) { |
|
76 | - $out[(string)$binding['name']] = (string)$binding->literal; |
|
77 | - } |
|
78 | - if ( isset( $binding->uri ) ) { |
|
79 | - $out[(string)$binding['name']] = (string)$binding->uri; |
|
80 | - } |
|
81 | - } |
|
82 | - return $out; |
|
83 | - } |
|
68 | + /** |
|
69 | + * @param SimpleXmlElement $xml The query result XML. |
|
70 | + * @return array |
|
71 | + */ |
|
72 | + protected function getBindings( $xml ) { |
|
73 | + $out = []; |
|
74 | + foreach ( $xml->binding as $binding ) { |
|
75 | + if ( isset( $binding->literal ) ) { |
|
76 | + $out[(string)$binding['name']] = (string)$binding->literal; |
|
77 | + } |
|
78 | + if ( isset( $binding->uri ) ) { |
|
79 | + $out[(string)$binding['name']] = (string)$binding->uri; |
|
80 | + } |
|
81 | + } |
|
82 | + return $out; |
|
83 | + } |
|
84 | 84 | |
85 | 85 | } |