@@ -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 | } |
@@ -9,84 +9,84 @@ |
||
| 9 | 9 | |
| 10 | 10 | class Query { |
| 11 | 11 | |
| 12 | - /** @var string */ |
|
| 13 | - protected $query; |
|
| 12 | + /** @var string */ |
|
| 13 | + protected $query; |
|
| 14 | 14 | |
| 15 | - /** @var string */ |
|
| 16 | - protected $lang; |
|
| 15 | + /** @var string */ |
|
| 16 | + protected $lang; |
|
| 17 | 17 | |
| 18 | - /** @var CacheItemPoolInterface */ |
|
| 19 | - protected $cache; |
|
| 18 | + /** @var CacheItemPoolInterface */ |
|
| 19 | + protected $cache; |
|
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * Query constructor. |
|
| 23 | - * @param string $query The Sparql query. |
|
| 24 | - * @param string $lang The language. |
|
| 25 | - * @param CacheItemPoolInterface $cache The cache. |
|
| 26 | - */ |
|
| 27 | - public function __construct( $query, $lang, CacheItemPoolInterface $cache ) { |
|
| 28 | - $this->query = $query; |
|
| 29 | - $this->lang = $lang; |
|
| 30 | - $this->cache = $cache; |
|
| 31 | - } |
|
| 21 | + /** |
|
| 22 | + * Query constructor. |
|
| 23 | + * @param string $query The Sparql query. |
|
| 24 | + * @param string $lang The language. |
|
| 25 | + * @param CacheItemPoolInterface $cache The cache. |
|
| 26 | + */ |
|
| 27 | + public function __construct( $query, $lang, CacheItemPoolInterface $cache ) { |
|
| 28 | + $this->query = $query; |
|
| 29 | + $this->lang = $lang; |
|
| 30 | + $this->cache = $cache; |
|
| 31 | + } |
|
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * Get the items. |
|
| 35 | - * @return Item[] The results. |
|
| 36 | - */ |
|
| 37 | - public function getItems() { |
|
| 38 | - $xml = $this->getXml( $this->query ); |
|
| 39 | - $results = []; |
|
| 40 | - foreach ( $xml->results->result as $res ) { |
|
| 41 | - $result = $this->getBindings( $res ); |
|
| 42 | - $id = substr( $result['item'], strrpos( $result['item'], '/' ) + 1 ); |
|
| 43 | - $item = Item::factory( $id, $this->lang, $this->cache ); |
|
| 44 | - $results[] = $item; |
|
| 45 | - } |
|
| 46 | - return $results; |
|
| 47 | - } |
|
| 33 | + /** |
|
| 34 | + * Get the items. |
|
| 35 | + * @return Item[] The results. |
|
| 36 | + */ |
|
| 37 | + public function getItems() { |
|
| 38 | + $xml = $this->getXml( $this->query ); |
|
| 39 | + $results = []; |
|
| 40 | + foreach ( $xml->results->result as $res ) { |
|
| 41 | + $result = $this->getBindings( $res ); |
|
| 42 | + $id = substr( $result['item'], strrpos( $result['item'], '/' ) + 1 ); |
|
| 43 | + $item = Item::factory( $id, $this->lang, $this->cache ); |
|
| 44 | + $results[] = $item; |
|
| 45 | + } |
|
| 46 | + return $results; |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * @param string $query The Sparql query. |
|
| 51 | - * @return SimpleXmlElement |
|
| 52 | - * @throws Exception |
|
| 53 | - */ |
|
| 54 | - protected function getXml( $query ) { |
|
| 55 | - $url = "https://query.wikidata.org/bigdata/namespace/wdq/sparql"; |
|
| 56 | - $client = new Client(); |
|
| 57 | - try { |
|
| 58 | - $response = $client->request( 'GET', $url, [ |
|
| 59 | - // https://meta.wikimedia.org/wiki/User-Agent_policy |
|
| 60 | - 'User-Agent' => 'samwilson/simple-wikidata (User:Samwilson) PHP', |
|
| 61 | - 'query' => [ 'query' => $query ], |
|
| 62 | - ] ); |
|
| 63 | - } catch ( Exception $e ) { |
|
| 64 | - throw new Exception( "Unable to run query: <pre>" . htmlspecialchars( $query ) . "</pre>", 500 ); |
|
| 65 | - } |
|
| 66 | - $result = $response->getBody()->getContents(); |
|
| 67 | - if ( empty( $result ) ) { |
|
| 68 | - $msg = "No result from query: <pre>" . htmlspecialchars( $query ) . "</pre>"; |
|
| 69 | - throw new Exception( $msg, 500 ); |
|
| 70 | - } |
|
| 71 | - $xml = new SimpleXmlElement( $result ); |
|
| 72 | - return $xml; |
|
| 73 | - } |
|
| 49 | + /** |
|
| 50 | + * @param string $query The Sparql query. |
|
| 51 | + * @return SimpleXmlElement |
|
| 52 | + * @throws Exception |
|
| 53 | + */ |
|
| 54 | + protected function getXml( $query ) { |
|
| 55 | + $url = "https://query.wikidata.org/bigdata/namespace/wdq/sparql"; |
|
| 56 | + $client = new Client(); |
|
| 57 | + try { |
|
| 58 | + $response = $client->request( 'GET', $url, [ |
|
| 59 | + // https://meta.wikimedia.org/wiki/User-Agent_policy |
|
| 60 | + 'User-Agent' => 'samwilson/simple-wikidata (User:Samwilson) PHP', |
|
| 61 | + 'query' => [ 'query' => $query ], |
|
| 62 | + ] ); |
|
| 63 | + } catch ( Exception $e ) { |
|
| 64 | + throw new Exception( "Unable to run query: <pre>" . htmlspecialchars( $query ) . "</pre>", 500 ); |
|
| 65 | + } |
|
| 66 | + $result = $response->getBody()->getContents(); |
|
| 67 | + if ( empty( $result ) ) { |
|
| 68 | + $msg = "No result from query: <pre>" . htmlspecialchars( $query ) . "</pre>"; |
|
| 69 | + throw new Exception( $msg, 500 ); |
|
| 70 | + } |
|
| 71 | + $xml = new SimpleXmlElement( $result ); |
|
| 72 | + return $xml; |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - /** |
|
| 76 | - * @param SimpleXmlElement $xml The query result XML. |
|
| 77 | - * @return array |
|
| 78 | - */ |
|
| 79 | - protected function getBindings( $xml ) { |
|
| 80 | - $out = []; |
|
| 81 | - foreach ( $xml->binding as $binding ) { |
|
| 82 | - if ( isset( $binding->literal ) ) { |
|
| 83 | - $out[(string)$binding['name']] = (string)$binding->literal; |
|
| 84 | - } |
|
| 85 | - if ( isset( $binding->uri ) ) { |
|
| 86 | - $out[(string)$binding['name']] = (string)$binding->uri; |
|
| 87 | - } |
|
| 88 | - } |
|
| 89 | - return $out; |
|
| 90 | - } |
|
| 75 | + /** |
|
| 76 | + * @param SimpleXmlElement $xml The query result XML. |
|
| 77 | + * @return array |
|
| 78 | + */ |
|
| 79 | + protected function getBindings( $xml ) { |
|
| 80 | + $out = []; |
|
| 81 | + foreach ( $xml->binding as $binding ) { |
|
| 82 | + if ( isset( $binding->literal ) ) { |
|
| 83 | + $out[(string)$binding['name']] = (string)$binding->literal; |
|
| 84 | + } |
|
| 85 | + if ( isset( $binding->uri ) ) { |
|
| 86 | + $out[(string)$binding['name']] = (string)$binding->uri; |
|
| 87 | + } |
|
| 88 | + } |
|
| 89 | + return $out; |
|
| 90 | + } |
|
| 91 | 91 | |
| 92 | 92 | } |
@@ -24,7 +24,7 @@ discard block |
||
| 24 | 24 | * @param string $lang The language. |
| 25 | 25 | * @param CacheItemPoolInterface $cache The cache. |
| 26 | 26 | */ |
| 27 | - public function __construct( $query, $lang, CacheItemPoolInterface $cache ) { |
|
| 27 | + public function __construct($query, $lang, CacheItemPoolInterface $cache) { |
|
| 28 | 28 | $this->query = $query; |
| 29 | 29 | $this->lang = $lang; |
| 30 | 30 | $this->cache = $cache; |
@@ -35,12 +35,12 @@ discard block |
||
| 35 | 35 | * @return Item[] The results. |
| 36 | 36 | */ |
| 37 | 37 | public function getItems() { |
| 38 | - $xml = $this->getXml( $this->query ); |
|
| 38 | + $xml = $this->getXml($this->query); |
|
| 39 | 39 | $results = []; |
| 40 | - foreach ( $xml->results->result as $res ) { |
|
| 41 | - $result = $this->getBindings( $res ); |
|
| 42 | - $id = substr( $result['item'], strrpos( $result['item'], '/' ) + 1 ); |
|
| 43 | - $item = Item::factory( $id, $this->lang, $this->cache ); |
|
| 40 | + foreach ($xml->results->result as $res) { |
|
| 41 | + $result = $this->getBindings($res); |
|
| 42 | + $id = substr($result['item'], strrpos($result['item'], '/') + 1); |
|
| 43 | + $item = Item::factory($id, $this->lang, $this->cache); |
|
| 44 | 44 | $results[] = $item; |
| 45 | 45 | } |
| 46 | 46 | return $results; |
@@ -51,24 +51,24 @@ discard block |
||
| 51 | 51 | * @return SimpleXmlElement |
| 52 | 52 | * @throws Exception |
| 53 | 53 | */ |
| 54 | - protected function getXml( $query ) { |
|
| 54 | + protected function getXml($query) { |
|
| 55 | 55 | $url = "https://query.wikidata.org/bigdata/namespace/wdq/sparql"; |
| 56 | 56 | $client = new Client(); |
| 57 | 57 | try { |
| 58 | - $response = $client->request( 'GET', $url, [ |
|
| 58 | + $response = $client->request('GET', $url, [ |
|
| 59 | 59 | // https://meta.wikimedia.org/wiki/User-Agent_policy |
| 60 | 60 | 'User-Agent' => 'samwilson/simple-wikidata (User:Samwilson) PHP', |
| 61 | - 'query' => [ 'query' => $query ], |
|
| 62 | - ] ); |
|
| 63 | - } catch ( Exception $e ) { |
|
| 64 | - throw new Exception( "Unable to run query: <pre>" . htmlspecialchars( $query ) . "</pre>", 500 ); |
|
| 61 | + 'query' => ['query' => $query], |
|
| 62 | + ]); |
|
| 63 | + } catch (Exception $e) { |
|
| 64 | + throw new Exception("Unable to run query: <pre>" . htmlspecialchars($query) . "</pre>", 500); |
|
| 65 | 65 | } |
| 66 | 66 | $result = $response->getBody()->getContents(); |
| 67 | - if ( empty( $result ) ) { |
|
| 68 | - $msg = "No result from query: <pre>" . htmlspecialchars( $query ) . "</pre>"; |
|
| 69 | - throw new Exception( $msg, 500 ); |
|
| 67 | + if (empty($result)) { |
|
| 68 | + $msg = "No result from query: <pre>" . htmlspecialchars($query) . "</pre>"; |
|
| 69 | + throw new Exception($msg, 500); |
|
| 70 | 70 | } |
| 71 | - $xml = new SimpleXmlElement( $result ); |
|
| 71 | + $xml = new SimpleXmlElement($result); |
|
| 72 | 72 | return $xml; |
| 73 | 73 | } |
| 74 | 74 | |
@@ -76,13 +76,13 @@ discard block |
||
| 76 | 76 | * @param SimpleXmlElement $xml The query result XML. |
| 77 | 77 | * @return array |
| 78 | 78 | */ |
| 79 | - protected function getBindings( $xml ) { |
|
| 79 | + protected function getBindings($xml) { |
|
| 80 | 80 | $out = []; |
| 81 | - foreach ( $xml->binding as $binding ) { |
|
| 82 | - if ( isset( $binding->literal ) ) { |
|
| 81 | + foreach ($xml->binding as $binding) { |
|
| 82 | + if (isset($binding->literal)) { |
|
| 83 | 83 | $out[(string)$binding['name']] = (string)$binding->literal; |
| 84 | 84 | } |
| 85 | - if ( isset( $binding->uri ) ) { |
|
| 85 | + if (isset($binding->uri)) { |
|
| 86 | 86 | $out[(string)$binding['name']] = (string)$binding->uri; |
| 87 | 87 | } |
| 88 | 88 | } |