@@ -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 | } |