From 98cd2b68f4d5b5adabe2db8c373cc9d5e1e6f5d8 Mon Sep 17 00:00:00 2001 From: Tac Tacelosky Date: Tue, 13 Dec 2022 08:28:04 -0600 Subject: [PATCH 01/19] allow tightenco/collect 9, require php7.4+ --- composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index bea5296..50248d9 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,8 @@ } ], "require": { - "tightenco/collect": "^8.0", + "php": "^7.4|^8.0", + "tightenco/collect": "^8.0|^9.0", "guzzlehttp/guzzle": "^7.0" }, "autoload": { From 6ea3a0520947cb2f59938ad6bcd95a84d525ce18 Mon Sep 17 00:00:00 2001 From: Tac Tacelosky Date: Wed, 17 Jan 2024 08:13:59 -0500 Subject: [PATCH 02/19] rename package to survos --- README.md | 5 ++++- composer.json | 8 ++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index d6a5a1b..0b87793 100644 --- a/README.md +++ b/README.md @@ -4,10 +4,13 @@ Wikidata provides a API for searching and retrieving data from [wikidata.org](https://www.wikidata.org). +Fork of https://github.com/freearhey/wikidata +@todo: consider https://github.com/freearhey/wikidata/compare/master...JamesFrost:wikidata:master + ## Installation ```sh -composer require freearhey/wikidata +composer require survos/wikidata ``` ## Usage diff --git a/composer.json b/composer.json index bea5296..edb4404 100644 --- a/composer.json +++ b/composer.json @@ -1,14 +1,14 @@ { - "name": "freearhey/wikidata", + "name": "survos/wikidata", "description": "A PHP client for working with Wikidata API.", "keywords": [ "wikidata", "client", "php" ], - "homepage": "https://github.com/freearhey/wikidata", + "homepage": "https://github.com/survos/wikidata", "license": "MIT", - "version": "3.6.0", + "version": "4.0.0", "authors": [ { "name": "Arhey", @@ -16,7 +16,7 @@ } ], "require": { - "tightenco/collect": "^8.0", + "tightenco/collect": "^9.0", "guzzlehttp/guzzle": "^7.0" }, "autoload": { From 9981a7a6c6f1c2036847920218671bc339a9861b Mon Sep 17 00:00:00 2001 From: Tac Tacelosky Date: Wed, 17 Jan 2024 09:19:44 -0500 Subject: [PATCH 03/19] add typehint --- tests/Unit/WikidataTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Unit/WikidataTest.php b/tests/Unit/WikidataTest.php index 515b981..e396cca 100644 --- a/tests/Unit/WikidataTest.php +++ b/tests/Unit/WikidataTest.php @@ -7,7 +7,7 @@ class WikidataTest extends TestCase { - protected $wikidata; + protected Wikidata $wikidata; public function setUp(): void { From cbdd90b6ce2ca1e09dc8ce623a12909688df0296 Mon Sep 17 00:00:00 2001 From: Tac Tacelosky Date: Wed, 17 Jan 2024 09:20:01 -0500 Subject: [PATCH 04/19] php 8.2 --- composer.json | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index edb4404..1de5fb1 100644 --- a/composer.json +++ b/composer.json @@ -13,11 +13,17 @@ { "name": "Arhey", "role": "developer" + }, + { + "name": "tacman", + "role": "developer" } ], "require": { - "tightenco/collect": "^9.0", - "guzzlehttp/guzzle": "^7.0" + "php":"^8.2", + "illuminate/collections": "^10.0", + "guzzlehttp/guzzle": "^7.0", + "symfony/http-client": "^6.4||^7.0" }, "autoload": { "psr-4": { From 53815d0e9479c9ce8641146d749c765b77ed9510 Mon Sep 17 00:00:00 2001 From: Tac Tacelosky Date: Wed, 17 Jan 2024 10:13:19 -0500 Subject: [PATCH 05/19] cleanup per phpstan feedback --- composer.json | 3 ++- phpstan.neon | 5 +++++ src/ApiClient.php | 19 +++++++------------ src/Entity.php | 15 +++++++-------- src/Property.php | 6 ++---- src/SparqlClient.php | 5 ++--- src/Wikidata.php | 12 +++++------- 7 files changed, 30 insertions(+), 35 deletions(-) create mode 100644 phpstan.neon diff --git a/composer.json b/composer.json index 1de5fb1..0c7c112 100644 --- a/composer.json +++ b/composer.json @@ -42,6 +42,7 @@ ] }, "require-dev": { - "phpunit/phpunit": "^9.0" + "phpunit/phpunit": "^9.0", + "phpstan/phpstan": "^1.10" } } diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..562fa66 --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,5 @@ +parameters: + level: 4 +# inferPrivatePropertyTypeFromConstructor: true + paths: + - ./src/ diff --git a/src/ApiClient.php b/src/ApiClient.php index 08c38ff..06e6c3e 100644 --- a/src/ApiClient.php +++ b/src/ApiClient.php @@ -3,19 +3,14 @@ namespace Wikidata; use GuzzleHttp\Client; +use Illuminate\Support\Collection; class ApiClient { const API_ENDPOINT = 'https://www.wikidata.org/w/api.php'; - /** - * @var string Value Id - */ - private $client; + private Client $client; - /** - * @param array $data - */ public function __construct() { $this->client = new Client(); @@ -24,13 +19,13 @@ public function __construct() /** * Get all entities by their ids from wikidata api * - * @param string $ids The IDs of the entities to get the data from (eg.: Q2) + * @param array|string $ids The IDs of the entities to get the data from (eg.: Q2, Q2|Q3) * @param string $lang Language (default: en) - * @param string $props Array of the properties to get back from each entity (supported: aliases, claims, datatype, descriptions, info, labels, sitelinks, sitelinks/urls) + * @param array|string $props Array of the properties to get back from each entity (supported: aliases, claims, datatype, descriptions, info, labels, sitelinks, sitelinks/urls) * * @return \Illuminate\Support\Collection */ - public function getEntities($ids, $lang = 'en', $props = []) + public function getEntities(array|string $ids, $lang = 'en', array|string $props=[]): Collection { $ids = is_array($ids) ? implode('|', $ids) : $ids; @@ -59,11 +54,11 @@ public function getEntities($ids, $lang = 'en', $props = []) * * @param string $query * @param string $lang Language (default: en) - * @param string $limit Max count of returning items (default: 10) + * @param int $limit Max count of returning items (default: 10) * * @return \Illuminate\Support\Collection */ - public function searchEntities($query, $lang = 'en', $limit = 10) + public function searchEntities($query, $lang = 'en', int $limit = 10) { $response = $this->client->get(self::API_ENDPOINT, [ 'query' => [ diff --git a/src/Entity.php b/src/Entity.php index dc5aae4..1f155da 100644 --- a/src/Entity.php +++ b/src/Entity.php @@ -2,6 +2,7 @@ namespace Wikidata; +use Illuminate\Support\Collection; use Wikidata\Property; class Entity @@ -34,12 +35,9 @@ class Entity /** * @var string Entity description */ - public $description; + public string $description; - /** - * @var \Illuminate\Support\Collection Collection of entity properties - */ - public $properties = []; + public Collection $properties; /** * @param array $data @@ -48,6 +46,7 @@ class Entity public function __construct($data, $lang) { $this->lang = $lang; + $this->properties = new Collection(); $this->parseData($data); } @@ -71,11 +70,11 @@ private function parseData($data) /** * Parse entity properties from sparql result * - * @param array $data + * @param Collection $data */ - public function parseProperties($data) + public function parseProperties(array|Collection $data) { - $collection = collect($data)->groupBy('prop'); + $collection = (new Collection($data))->groupBy('prop'); $this->properties = $collection->mapWithKeys(function ($item) { $property = new Property($item); diff --git a/src/Property.php b/src/Property.php index b15d3e9..8ce2b15 100644 --- a/src/Property.php +++ b/src/Property.php @@ -31,7 +31,7 @@ public function __construct($data) /** * Parse input data - * + * * @param array $data */ private function parseData($data) @@ -41,8 +41,6 @@ private function parseData($data) $this->id = get_id($flatten[0]['prop']); $this->label = $flatten[0]['propertyLabel']; - $this->values = $grouped->values()->map(function($v) { - return new Value($v->toArray()); - }); + $this->values = $grouped->values()->map(fn($v) => new Value($v->toArray())); } } diff --git a/src/SparqlClient.php b/src/SparqlClient.php index c170d03..caa96b9 100644 --- a/src/SparqlClient.php +++ b/src/SparqlClient.php @@ -26,9 +26,9 @@ class SparqlClient { /** * Request method - * @var array + * @var string */ - private $method = 'GET'; + private string $method = 'GET'; /** * Query SPARQL endpoint @@ -38,7 +38,6 @@ class SparqlClient { * * @return array List of results, one row per array element * Each row will contain fields indexed by variable name. - * @throws SparqlException */ public function execute( $query, $rawData = false ) { diff --git a/src/Wikidata.php b/src/Wikidata.php index d14abfb..f76092b 100644 --- a/src/Wikidata.php +++ b/src/Wikidata.php @@ -15,11 +15,11 @@ class Wikidata * * @param string $query * @param string $lang Language (default: en) - * @param string $limit Max count of returning items (default: 10) + * @param int $limit Max count of returning items (default: 10) * * @return \Illuminate\Support\Collection Return collection of \Wikidata\SearchResult */ - public function search($query, $lang = 'en', $limit = 10) + public function search($query, $lang = 'en', int $limit = 10) { $client = new ApiClient(); @@ -43,11 +43,11 @@ public function search($query, $lang = 'en', $limit = 10) * @param string $property Wikidata ID of property (e.g.: P646) * @param string $value String value of property or Wikidata entity ID (e.g.: Q11696) * @param string $lang Language (default: en) - * @param string $limit Max count of returning items (default: 10) + * @param int $limit Max count of returning items (default: 10) * * @return \Illuminate\Support\Collection Return collection of \Wikidata\SearchResult */ - public function searchBy($property, $value = null, $lang = 'en', $limit = 10) + public function searchBy($property, $value = null, $lang = 'en', int $limit = 10) { if (!is_pid($property)) { throw new Exception("First argument in searchBy() must be a valid Wikidata property ID (e.g.: P646).", 1); @@ -120,9 +120,7 @@ public function get($entityId, $lang = 'en') $data = $client->execute($query); - if (isset($data)) { - $entity->parseProperties($data); - } + $entity->parseProperties($data); return $entity; } From 7a132de10901a1aba74858dca16e452641db6280 Mon Sep 17 00:00:00 2001 From: Tac Tacelosky Date: Wed, 21 Feb 2024 19:33:00 -0500 Subject: [PATCH 06/19] selectively add functions --- README.md | 4 ++-- src/helpers.php | 28 ++++++++++++++++++---------- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 0b87793..156b305 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ [![wikidata](https://raw.githubusercontent.com/maxlath/wikidata-cli/master/assets/wikidata_logo_alone.jpg)](https://wikidata.org) -# Wikidata [![Build Status](https://travis-ci.org/freearhey/wikidata.svg?branch=master)](https://travis-ci.org/freearhey/wikidata) +# Wikidata Wikidata provides a API for searching and retrieving data from [wikidata.org](https://www.wikidata.org). @@ -211,7 +211,7 @@ vendor/bin/phpunit ### Contribution -If you find a bug or want to contribute to the code or documentation, you can help by submitting an [issue](https://github.com/freearhey/wikidata/issues) or a [pull request](https://github.com/freearhey/wikidata/pulls). +If you find a bug or want to contribute to the code or documentation, you can help by submitting an [issue](https://github.com/survos/wikidata/issues) or a [pull request](https://github.com/freearhey/wikidata/pulls). ### License diff --git a/src/helpers.php b/src/helpers.php index bf12d43..ae618c7 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -1,4 +1,5 @@ Date: Tue, 25 Jun 2024 14:50:23 -0400 Subject: [PATCH 07/19] remove guzzle dependency --- src/ApiClient.php | 124 ++++++++++++++++++------------------ src/SparqlClient.php | 18 ++++-- tests/Unit/WikidataTest.php | 1 + 3 files changed, 77 insertions(+), 66 deletions(-) diff --git a/src/ApiClient.php b/src/ApiClient.php index 06e6c3e..2f43edc 100644 --- a/src/ApiClient.php +++ b/src/ApiClient.php @@ -2,81 +2,83 @@ namespace Wikidata; -use GuzzleHttp\Client; use Illuminate\Support\Collection; +use Symfony\Component\HttpClient\HttpClient; +use Symfony\Contracts\HttpClient\HttpClientInterface; class ApiClient { - const API_ENDPOINT = 'https://www.wikidata.org/w/api.php'; + const API_ENDPOINT = 'https://www.wikidata.org/w/api.php'; - private Client $client; - public function __construct() - { - $this->client = new Client(); - } + public function __construct(private ?HttpClientInterface $client=null) + { + if (!$this->client) { + $this->client = HttpClient::create(); + } + } - /** - * Get all entities by their ids from wikidata api - * - * @param array|string $ids The IDs of the entities to get the data from (eg.: Q2, Q2|Q3) - * @param string $lang Language (default: en) - * @param array|string $props Array of the properties to get back from each entity (supported: aliases, claims, datatype, descriptions, info, labels, sitelinks, sitelinks/urls) - * - * @return \Illuminate\Support\Collection - */ - public function getEntities(array|string $ids, $lang = 'en', array|string $props=[]): Collection - { - $ids = is_array($ids) ? implode('|', $ids) : $ids; + /** + * Get all entities by their ids from wikidata api + * + * @param array|string $ids The IDs of the entities to get the data from (eg.: Q2, Q2|Q3) + * @param string $lang Language (default: en) + * @param array|string $props Array of the properties to get back from each entity (supported: aliases, claims, datatype, descriptions, info, labels, sitelinks, sitelinks/urls) + * + * @return \Illuminate\Support\Collection + */ + public function getEntities(array|string $ids, $lang = 'en', array|string $props = []): Collection + { + $ids = is_array($ids) ? implode('|', $ids) : $ids; - $props = $props ? implode('|', $props) : null; + $props = $props ? implode('|', $props) : null; - $response = $this->client->get(self::API_ENDPOINT, [ - 'query' => [ - 'action' => 'wbgetentities', - 'format' => 'json', - 'languages' => $lang, - 'ids' => $ids, - 'sitefilter' => $lang . 'wiki', - 'props' => $props, - ], - ]); + $response = $this->client->request('GET', self::API_ENDPOINT, [ + 'query' => [ + 'action' => 'wbgetentities', + 'format' => 'json', + 'languages' => $lang, + 'ids' => $ids, + 'sitefilter' => $lang . 'wiki', + 'props' => $props, + ], + ]); - $results = json_decode($response->getBody(), true); + $results = json_decode($response->getContent(), true); - $data = isset($results['entities']) ? $results['entities'] : []; + $data = isset($results['entities']) ? $results['entities'] : []; - return collect($data); - } + return collect($data); + } - /** - * Searches for entities using labels and aliases - * - * @param string $query - * @param string $lang Language (default: en) - * @param int $limit Max count of returning items (default: 10) - * - * @return \Illuminate\Support\Collection - */ - public function searchEntities($query, $lang = 'en', int $limit = 10) - { - $response = $this->client->get(self::API_ENDPOINT, [ - 'query' => [ - 'action' => 'wbsearchentities', - 'format' => 'json', - 'strictlanguage' => true, - 'language' => $lang, - 'uselang' => $lang, - 'search' => $query, - 'limit' => $limit, - 'props' => '', - ], - ]); + /** + * Searches for entities using labels and aliases + * + * @param string $query + * @param string $lang Language (default: en) + * @param int $limit Max count of returning items (default: 10) + * + * @return \Illuminate\Support\Collection + */ + public function searchEntities($query, $lang = 'en', int $limit = 10) + { + $response = $this->client->request('GET', self::API_ENDPOINT, [ + 'query' => [ + 'action' => 'wbsearchentities', + 'format' => 'json', + 'strictlanguage' => true, + 'language' => $lang, + 'uselang' => $lang, + 'search' => $query, + 'limit' => $limit, + 'props' => '', + ], + ]); - $results = json_decode($response->getBody(), true); + $results = json_decode($response->getContent(), true); - $data = isset($results['search']) ? $results['search'] : []; + $data = isset($results['search']) ? $results['search'] : []; - return collect($data); - } + return collect($data); + } } diff --git a/src/SparqlClient.php b/src/SparqlClient.php index caa96b9..16228a1 100644 --- a/src/SparqlClient.php +++ b/src/SparqlClient.php @@ -3,10 +3,20 @@ namespace Wikidata; use Exception; -use GuzzleHttp\Client; +use Symfony\Component\HttpClient\HttpClient; +use Symfony\Contracts\HttpClient\HttpClientInterface; class SparqlClient { + public function __construct( + private ?HttpClientInterface $client=null + ) + { + if (!$this->client) { + $this->client = HttpClient::create(); + } + } + /** * Limit on how long can be the query to be sent by GET. */ @@ -46,9 +56,7 @@ public function execute( $query, $rawData = false ) { $this->method = 'POST'; } - $client = new Client(); - - $response = $client->request( $this->method, self::SPARQL_ENDPOINT, [ + $response = $this->client->request( $this->method, self::SPARQL_ENDPOINT, [ 'query' => [ "query" => $query, "format" => "json", @@ -62,7 +70,7 @@ public function execute( $query, $rawData = false ) { throw new Exception( 'HTTP Error' ); } - $result = $response->getBody(); + $result = $response->getContent(); $data = json_decode( $result, true ); diff --git a/tests/Unit/WikidataTest.php b/tests/Unit/WikidataTest.php index e396cca..48dd9ea 100644 --- a/tests/Unit/WikidataTest.php +++ b/tests/Unit/WikidataTest.php @@ -50,6 +50,7 @@ public function testSearchResultsCouldBeEmpty() { $results = $this->wikidata->search('asdfgh'); + $this->assertInstanceOf('Illuminate\Support\Collection', $results); $this->assertEquals(true, $results->isEmpty()); From 67e2054b4a4d9fa22a78829b5d0f7d749faa98ba Mon Sep 17 00:00:00 2001 From: Tac Tacelosky Date: Tue, 25 Jun 2024 14:52:37 -0400 Subject: [PATCH 08/19] add rector --- composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index c2f0793..11377a0 100644 --- a/composer.json +++ b/composer.json @@ -42,6 +42,7 @@ }, "require-dev": { "phpunit/phpunit": "^9.0", - "phpstan/phpstan": "^1.10" + "phpstan/phpstan": "^1.10", + "rector/rector": "^1.1" } } From 8f07928c40d97c0107a0b48fc94dbe2f27186315 Mon Sep 17 00:00:00 2001 From: Tac Tacelosky Date: Tue, 25 Jun 2024 14:57:44 -0400 Subject: [PATCH 09/19] add voids, use class constants instead of strings --- src/ApiClient.php | 6 +++--- src/Entity.php | 12 +++-------- src/Property.php | 2 +- src/Qualifier.php | 2 +- src/SearchResult.php | 21 +++++++----------- src/SparqlClient.php | 4 ++-- src/Value.php | 2 +- src/Wikidata.php | 4 +--- src/helpers.php | 6 +++--- tests/Unit/EntityTest.php | 20 ++++++++--------- tests/Unit/PropertyTest.php | 10 ++++----- tests/Unit/QualifierTest.php | 6 +++--- tests/Unit/SearchResultTest.php | 12 +++++------ tests/Unit/ValueTest.php | 10 ++++----- tests/Unit/WikidataTest.php | 38 ++++++++++++++++----------------- 15 files changed, 71 insertions(+), 84 deletions(-) diff --git a/src/ApiClient.php b/src/ApiClient.php index 2f43edc..2d308d6 100644 --- a/src/ApiClient.php +++ b/src/ApiClient.php @@ -8,7 +8,7 @@ class ApiClient { - const API_ENDPOINT = 'https://www.wikidata.org/w/api.php'; + public const API_ENDPOINT = 'https://www.wikidata.org/w/api.php'; public function __construct(private ?HttpClientInterface $client=null) @@ -46,7 +46,7 @@ public function getEntities(array|string $ids, $lang = 'en', array|string $props $results = json_decode($response->getContent(), true); - $data = isset($results['entities']) ? $results['entities'] : []; + $data = $results['entities'] ?? []; return collect($data); } @@ -77,7 +77,7 @@ public function searchEntities($query, $lang = 'en', int $limit = 10) $results = json_decode($response->getContent(), true); - $data = isset($results['search']) ? $results['search'] : []; + $data = $results['search'] ?? []; return collect($data); } diff --git a/src/Entity.php b/src/Entity.php index 1f155da..8263936 100644 --- a/src/Entity.php +++ b/src/Entity.php @@ -12,11 +12,6 @@ class Entity */ public $id; - /** - * @var string Entity language - */ - public $lang; - /** * @var string Entity label */ @@ -43,9 +38,8 @@ class Entity * @param array $data * @param string $lang */ - public function __construct($data, $lang) + public function __construct($data, public $lang) { - $this->lang = $lang; $this->properties = new Collection(); $this->parseData($data); } @@ -55,7 +49,7 @@ public function __construct($data, $lang) * * @param array $data */ - private function parseData($data) + private function parseData($data): void { $lang = $this->lang; $site = $lang . 'wiki'; @@ -72,7 +66,7 @@ private function parseData($data) * * @param Collection $data */ - public function parseProperties(array|Collection $data) + public function parseProperties(array|Collection $data): void { $collection = (new Collection($data))->groupBy('prop'); $this->properties = $collection->mapWithKeys(function ($item) { diff --git a/src/Property.php b/src/Property.php index 8ce2b15..887ace2 100644 --- a/src/Property.php +++ b/src/Property.php @@ -34,7 +34,7 @@ public function __construct($data) * * @param array $data */ - private function parseData($data) + private function parseData($data): void { $grouped = collect($data)->groupBy('statement'); $flatten = $grouped->flatten(1); diff --git a/src/Qualifier.php b/src/Qualifier.php index ceeb59b..f2364d3 100644 --- a/src/Qualifier.php +++ b/src/Qualifier.php @@ -32,7 +32,7 @@ public function __construct($data) * * @param array $data */ - private function parseData($data) + private function parseData($data): void { $this->id = get_id($data['qualifier']); $this->label = $data['qualifierLabel']; diff --git a/src/SearchResult.php b/src/SearchResult.php index 56d3d81..f0f0a59 100644 --- a/src/SearchResult.php +++ b/src/SearchResult.php @@ -9,11 +9,6 @@ class SearchResult */ public $id; - /** - * @var string - */ - public $lang; - /** * @var string */ @@ -36,19 +31,19 @@ class SearchResult /** * @param array $data + * @param string $lang */ - public function __construct($data, $lang = 'en') + public function __construct($data, public $lang = 'en') { $this->parseData($data); - $this->lang = $lang; } - private function parseData($data) + private function parseData($data): void { - $this->id = isset($data['id']) ? $data['id'] : null; - $this->label = isset($data['label']) ? $data['label'] : null; - $this->aliases = isset($data['aliases']) ? $data['aliases'] : []; - $this->description = isset($data['description']) ? $data['description'] : null; - $this->wiki_url = isset($data['wiki_url']) ? $data['wiki_url'] : null; + $this->id = $data['id'] ?? null; + $this->label = $data['label'] ?? null; + $this->aliases = $data['aliases'] ?? []; + $this->description = $data['description'] ?? null; + $this->wiki_url = $data['wiki_url'] ?? null; } } diff --git a/src/SparqlClient.php b/src/SparqlClient.php index 16228a1..8481704 100644 --- a/src/SparqlClient.php +++ b/src/SparqlClient.php @@ -20,13 +20,13 @@ public function __construct( /** * Limit on how long can be the query to be sent by GET. */ - const MAX_GET_SIZE = 2048; + public const MAX_GET_SIZE = 2048; /** * SPARQL endpoint URL * @var string */ - const SPARQL_ENDPOINT = 'https://query.wikidata.org/sparql'; + public const SPARQL_ENDPOINT = 'https://query.wikidata.org/sparql'; /** * Query timeout (seconds) diff --git a/src/Value.php b/src/Value.php index 2215dd0..511e400 100644 --- a/src/Value.php +++ b/src/Value.php @@ -34,7 +34,7 @@ public function __construct($data) * * @param array $data */ - private function parseData($data) + private function parseData($data): void { $this->id = get_id($data[0]['propertyValue']); $this->label = $data[0]['propertyValueLabel']; diff --git a/src/Wikidata.php b/src/Wikidata.php index f76092b..611569b 100644 --- a/src/Wikidata.php +++ b/src/Wikidata.php @@ -69,9 +69,7 @@ public function searchBy($property, $value = null, $lang = 'en', int $limit = 10 $data = $client->execute($query); - $ids = collect($data)->map(function ($data) { - return str_replace("http://www.wikidata.org/entity/", "", $data['item']); - })->toArray(); + $ids = collect($data)->map(fn($data) => str_replace("http://www.wikidata.org/entity/", "", $data['item']))->toArray(); $client = new ApiClient(); diff --git a/src/helpers.php b/src/helpers.php index ae618c7..f8a4527 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -11,7 +11,7 @@ if (!function_exists('is_qid')) { function is_qid($value) { - return preg_match("/^Q[0-9]+/", $value); + return preg_match("/^Q[0-9]+/", (string) $value); } } @@ -25,7 +25,7 @@ function is_qid($value) if (!function_exists('is_pid')) { function is_pid($value) { - return preg_match("/^P[0-9]+/", $value); + return preg_match("/^P[0-9]+/", (string) $value); } } @@ -40,7 +40,7 @@ function is_pid($value) function get_id($string) { - preg_match('/(Q|P)\d+/i', $string, $matches); + preg_match('/(Q|P)\d+/i', (string) $string, $matches); return !empty($matches) ? $matches[0] : $string; } diff --git a/tests/Unit/EntityTest.php b/tests/Unit/EntityTest.php index f5a6910..081d34b 100644 --- a/tests/Unit/EntityTest.php +++ b/tests/Unit/EntityTest.php @@ -15,53 +15,53 @@ public function setUp(): void $this->entity = new Entity($this->dummy[0], $this->lang); } - public function testGetEntityId() + public function testGetEntityId(): void { $this->assertEquals($this->dummy[0]['id'], $this->entity->id); } - public function testGetEntityLang() + public function testGetEntityLang(): void { $this->assertEquals($this->lang, $this->entity->lang); } - public function testGetEntityLabel() + public function testGetEntityLabel(): void { $this->assertEquals($this->dummy[0]['labels'][$this->lang]['value'], $this->entity->label); } - public function testGetEntityDescription() + public function testGetEntityDescription(): void { $this->assertEquals($this->dummy[0]['descriptions'][$this->lang]['value'], $this->entity->description); } - public function testGetWikiUrl() + public function testGetWikiUrl(): void { $this->assertEquals($this->dummy[0]['sitelinks'][$this->lang . 'wiki']['url'], $this->entity->wiki_url); } - public function testGetEntityAliases() + public function testGetEntityAliases(): void { $aliases = collect($this->dummy[0]['aliases'][$this->lang])->pluck('value')->toArray(); $this->assertEquals($aliases, $this->entity->aliases); } - public function testGetEntityWithoutAliases() + public function testGetEntityWithoutAliases(): void { $entity = new Entity($this->dummy[1], 'en'); $this->assertEquals([], $entity->aliases); } - public function testGetEntityProperties() + public function testGetEntityProperties(): void { $this->entity->parseProperties($this->dummyProperties); $properties = $this->entity->properties; - $this->assertInstanceOf('Illuminate\Support\Collection', $properties); + $this->assertInstanceOf(\Illuminate\Support\Collection::class, $properties); - $this->assertInstanceOf('Wikidata\Property', $properties->first()); + $this->assertInstanceOf(\Wikidata\Property::class, $properties->first()); } } diff --git a/tests/Unit/PropertyTest.php b/tests/Unit/PropertyTest.php index 0df05e7..e93655e 100644 --- a/tests/Unit/PropertyTest.php +++ b/tests/Unit/PropertyTest.php @@ -13,24 +13,24 @@ public function setUp(): void $this->property = new Property($this->dummyProperties); } - public function testGetPropertyId() + public function testGetPropertyId(): void { $id = str_replace('http://www.wikidata.org/prop/', '', $this->dummyProperties[0]['prop']); $this->assertEquals($id, $this->property->id); } - public function testGetPropertyLabel() + public function testGetPropertyLabel(): void { $this->assertEquals($this->dummyProperties[0]['propertyLabel'], $this->property->label); } - public function testGetPropertyValues() + public function testGetPropertyValues(): void { $values = $this->property->values; - $this->assertInstanceOf('Illuminate\Support\Collection', $values); + $this->assertInstanceOf(\Illuminate\Support\Collection::class, $values); - $this->assertInstanceOf('Wikidata\Value', $values->first()); + $this->assertInstanceOf(\Wikidata\Value::class, $values->first()); } } diff --git a/tests/Unit/QualifierTest.php b/tests/Unit/QualifierTest.php index e638ad4..2e0b6d6 100644 --- a/tests/Unit/QualifierTest.php +++ b/tests/Unit/QualifierTest.php @@ -13,19 +13,19 @@ public function setUp(): void $this->qualifier = new Qualifier($this->dummyProperties[0]); } - public function testGetQualifierId() + public function testGetQualifierId(): void { $id = str_replace('http://www.wikidata.org/entity/', '', $this->dummyProperties[0]['qualifier']); $this->assertEquals($id, $this->qualifier->id); } - public function testGetQualifierLabel() + public function testGetQualifierLabel(): void { $this->assertEquals($this->dummyProperties[0]['qualifierLabel'], $this->qualifier->label); } - public function testGetQualifierValue() + public function testGetQualifierValue(): void { $this->assertEquals($this->dummyProperties[0]['qualifierValueLabel'], $this->qualifier->value); } diff --git a/tests/Unit/SearchResultTest.php b/tests/Unit/SearchResultTest.php index 1c78f63..2e22b73 100644 --- a/tests/Unit/SearchResultTest.php +++ b/tests/Unit/SearchResultTest.php @@ -28,32 +28,32 @@ public function setUp(): void $this->result = new SearchResult($collection); } - public function testGetResultId() + public function testGetResultId(): void { $this->assertEquals($this->dummy['id'], $this->result->id); } - public function testGetResultLang() + public function testGetResultLang(): void { $this->assertEquals($this->dummy['lang'], $this->result->lang); } - public function testGetResultLabel() + public function testGetResultLabel(): void { $this->assertEquals($this->dummy['label'], $this->result->label); } - public function testGetResultWikiUrl() + public function testGetResultWikiUrl(): void { $this->assertEquals($this->dummy['wiki_url'], $this->result->wiki_url); } - public function testGetResultAliases() + public function testGetResultAliases(): void { $this->assertEquals($this->dummy['aliases'], $this->result->aliases); } - public function testGetResultDescription() + public function testGetResultDescription(): void { $this->assertEquals($this->dummy['description'], $this->result->description); } diff --git a/tests/Unit/ValueTest.php b/tests/Unit/ValueTest.php index 1deaaa3..4e2528a 100644 --- a/tests/Unit/ValueTest.php +++ b/tests/Unit/ValueTest.php @@ -13,24 +13,24 @@ public function setUp(): void $this->value = new Value($this->dummyProperties); } - public function testGetValueId() + public function testGetValueId(): void { $id = str_replace('http://www.wikidata.org/entity/', '', $this->dummyProperties[0]['propertyValue']); $this->assertEquals($id, $this->value->id); } - public function testGetValueLabel() + public function testGetValueLabel(): void { $this->assertEquals($this->dummyProperties[0]['propertyValueLabel'], $this->value->label); } - public function testGetValueQualifiers() + public function testGetValueQualifiers(): void { $qualifiers = $this->value->qualifiers; - $this->assertInstanceOf('Illuminate\Support\Collection', $qualifiers); + $this->assertInstanceOf(\Illuminate\Support\Collection::class, $qualifiers); - $this->assertInstanceOf('Wikidata\Qualifier', $qualifiers->first()); + $this->assertInstanceOf(\Wikidata\Qualifier::class, $qualifiers->first()); } } diff --git a/tests/Unit/WikidataTest.php b/tests/Unit/WikidataTest.php index 48dd9ea..cd5875f 100644 --- a/tests/Unit/WikidataTest.php +++ b/tests/Unit/WikidataTest.php @@ -14,15 +14,15 @@ public function setUp(): void $this->wikidata = new Wikidata(); } - public function testSearchByTerm() + public function testSearchByTerm(): void { $results = $this->wikidata->search('London'); - $this->assertInstanceOf('Illuminate\Support\Collection', $results); + $this->assertInstanceOf(\Illuminate\Support\Collection::class, $results); $result = $results->first(); - $this->assertInstanceOf('Wikidata\SearchResult', $result); + $this->assertInstanceOf(\Wikidata\SearchResult::class, $result); $this->assertEquals(true, property_exists($result, 'id')); $this->assertEquals(true, property_exists($result, 'lang')); @@ -32,39 +32,39 @@ public function testSearchByTerm() $this->assertEquals(true, property_exists($result, 'wiki_url')); } - public function testSearchOnAnotherLanguage() + public function testSearchOnAnotherLanguage(): void { $results = $this->wikidata->search('London', 'fr'); $this->assertEquals('fr', $results->first()->lang); } - public function testSearchWithLimit() + public function testSearchWithLimit(): void { $results = $this->wikidata->search('car', 'en', 10); $this->assertEquals(10, $results->count()); } - public function testSearchResultsCouldBeEmpty() + public function testSearchResultsCouldBeEmpty(): void { $results = $this->wikidata->search('asdfgh'); - $this->assertInstanceOf('Illuminate\Support\Collection', $results); + $this->assertInstanceOf(\Illuminate\Support\Collection::class, $results); $this->assertEquals(true, $results->isEmpty()); } - public function testSearchByPropertyIdAndValue() + public function testSearchByPropertyIdAndValue(): void { $results = $this->wikidata->searchBy('P646', '/m/02mjmr'); - $this->assertInstanceOf('Illuminate\Support\Collection', $results); + $this->assertInstanceOf(\Illuminate\Support\Collection::class, $results); $result = $results->first(); - $this->assertInstanceOf('Wikidata\SearchResult', $result); + $this->assertInstanceOf(\Wikidata\SearchResult::class, $result); $this->assertEquals(true, property_exists($result, 'id')); $this->assertEquals(true, property_exists($result, 'lang')); @@ -74,36 +74,36 @@ public function testSearchByPropertyIdAndValue() $this->assertEquals(true, property_exists($result, 'wiki_url')); } - public function testSearchByThrowExceptionIfSecondPropertyMissing() + public function testSearchByThrowExceptionIfSecondPropertyMissing(): void { $this->expectException(Exception::class); $this->wikidata->searchBy('P646'); } - public function testSearchByThrowExceptionIfPropertyIdInvalid() + public function testSearchByThrowExceptionIfPropertyIdInvalid(): void { $this->expectException(Exception::class); $this->wikidata->searchBy('Pasd', '/m/02mjmr'); } - public function testSearchByPropertyIdAndEntityId() + public function testSearchByPropertyIdAndEntityId(): void { $results = $this->wikidata->searchBy('P39', 'Q11696'); - $this->assertInstanceOf('Illuminate\Support\Collection', $results); + $this->assertInstanceOf(\Illuminate\Support\Collection::class, $results); $result = $results->first(); - $this->assertInstanceOf('Wikidata\SearchResult', $result); + $this->assertInstanceOf(\Wikidata\SearchResult::class, $result); } - public function testGetEntityById() + public function testGetEntityById(): void { $entity = $this->wikidata->get('Q44077'); - $this->assertInstanceOf('Wikidata\Entity', $entity); + $this->assertInstanceOf(\Wikidata\Entity::class, $entity); $this->assertEquals(true, property_exists($entity, 'id')); $this->assertEquals(true, property_exists($entity, 'lang')); @@ -113,14 +113,14 @@ public function testGetEntityById() $this->assertEquals(true, property_exists($entity, 'wiki_url')); } - public function testGetEntityOnAnotherLanguage() + public function testGetEntityOnAnotherLanguage(): void { $entity = $this->wikidata->get('Q44077', 'es'); $this->assertEquals('es', $entity->lang); } - public function testGetEntityThrowExceptionIfEntityIdInvalid() + public function testGetEntityThrowExceptionIfEntityIdInvalid(): void { $this->expectException(Exception::class); From 3c0910ce79f000c20db215e57d1be39d21e9f3d2 Mon Sep 17 00:00:00 2001 From: Tac Tacelosky Date: Tue, 25 Jun 2024 15:06:56 -0400 Subject: [PATCH 10/19] add typed properties and returns --- rector.php | 23 +++++++++++++++++++++++ src/ApiClient.php | 2 +- src/Entity.php | 6 +++--- src/Property.php | 2 +- src/Qualifier.php | 2 +- src/SparqlClient.php | 5 ++--- src/Value.php | 4 ++-- src/Wikidata.php | 10 +++++----- src/helpers.php | 4 ++-- 9 files changed, 40 insertions(+), 18 deletions(-) create mode 100644 rector.php diff --git a/rector.php b/rector.php new file mode 100644 index 0000000..0174aa4 --- /dev/null +++ b/rector.php @@ -0,0 +1,23 @@ +withPaths([ + __DIR__ . '/src', + __DIR__ . '/tests', + ]) + // uncomment to reach your current PHP version + ->withPhpSets(php82: true) + ->withSets([ + SetList::TYPE_DECLARATION, + ]) + ->withRules([ + TypedPropertyFromAssignsRector::class, + AddVoidReturnTypeWhereNoReturnRector::class, + ]); diff --git a/src/ApiClient.php b/src/ApiClient.php index 2d308d6..175947f 100644 --- a/src/ApiClient.php +++ b/src/ApiClient.php @@ -27,7 +27,7 @@ public function __construct(private ?HttpClientInterface $client=null) * * @return \Illuminate\Support\Collection */ - public function getEntities(array|string $ids, $lang = 'en', array|string $props = []): Collection + public function getEntities(array|string $ids, string $lang = 'en', array|string $props = []): Collection { $ids = is_array($ids) ? implode('|', $ids) : $ids; diff --git a/src/Entity.php b/src/Entity.php index 8263936..a240e09 100644 --- a/src/Entity.php +++ b/src/Entity.php @@ -49,7 +49,7 @@ public function __construct($data, public $lang) * * @param array $data */ - private function parseData($data): void + private function parseData(array $data): void { $lang = $this->lang; $site = $lang . 'wiki'; @@ -69,14 +69,14 @@ private function parseData($data): void public function parseProperties(array|Collection $data): void { $collection = (new Collection($data))->groupBy('prop'); - $this->properties = $collection->mapWithKeys(function ($item) { + $this->properties = $collection->mapWithKeys(function ($item): array { $property = new Property($item); return [$property->id => $property]; }); } - public function toArray() + public function toArray(): array { return [ 'id' => $this->id, diff --git a/src/Property.php b/src/Property.php index 887ace2..f3a69fb 100644 --- a/src/Property.php +++ b/src/Property.php @@ -41,6 +41,6 @@ private function parseData($data): void $this->id = get_id($flatten[0]['prop']); $this->label = $flatten[0]['propertyLabel']; - $this->values = $grouped->values()->map(fn($v) => new Value($v->toArray())); + $this->values = $grouped->values()->map(fn($v): \Wikidata\Value => new Value($v->toArray())); } } diff --git a/src/Qualifier.php b/src/Qualifier.php index f2364d3..bbafe5c 100644 --- a/src/Qualifier.php +++ b/src/Qualifier.php @@ -32,7 +32,7 @@ public function __construct($data) * * @param array $data */ - private function parseData($data): void + private function parseData(array $data): void { $this->id = get_id($data['qualifier']); $this->label = $data['qualifierLabel']; diff --git a/src/SparqlClient.php b/src/SparqlClient.php index 8481704..8443706 100644 --- a/src/SparqlClient.php +++ b/src/SparqlClient.php @@ -30,9 +30,8 @@ public function __construct( /** * Query timeout (seconds) - * @var int */ - private $timeout = 30; + private int $timeout = 30; /** * Request method @@ -92,7 +91,7 @@ public function execute( $query, $rawData = false ) { * * @return array List of results, one row per element. */ - private function extractData( $data, $rawData = false ) { + private function extractData( array $data, $rawData = false ): array { $result = []; if ( $data && !empty( $data['results'] ) ) { $vars = $data['head']['vars']; diff --git a/src/Value.php b/src/Value.php index 511e400..31aebd9 100644 --- a/src/Value.php +++ b/src/Value.php @@ -34,11 +34,11 @@ public function __construct($data) * * @param array $data */ - private function parseData($data): void + private function parseData(array $data): void { $this->id = get_id($data[0]['propertyValue']); $this->label = $data[0]['propertyValueLabel']; - $this->qualifiers = collect($data)->map(function($item) { + $this->qualifiers = collect($data)->map(function(array $item): ?\Wikidata\Qualifier { if($item['qualifier']) { return new Qualifier($item); } diff --git a/src/Wikidata.php b/src/Wikidata.php index 611569b..08da2ef 100644 --- a/src/Wikidata.php +++ b/src/Wikidata.php @@ -29,7 +29,7 @@ public function search($query, $lang = 'en', int $limit = 10) $entities = $client->getEntities($ids, $lang, ['sitelinks/urls', 'aliases', 'descriptions', 'labels']); - $output = $entities->map(function ($item) use ($lang) { + $output = $entities->map(function ($item) use ($lang): \Wikidata\SearchResult { $entity = new Entity($item, $lang); return new SearchResult($entity->toArray(), $lang); }); @@ -47,7 +47,7 @@ public function search($query, $lang = 'en', int $limit = 10) * * @return \Illuminate\Support\Collection Return collection of \Wikidata\SearchResult */ - public function searchBy($property, $value = null, $lang = 'en', int $limit = 10) + public function searchBy(string $property, $value = null, $lang = 'en', int $limit = 10) { if (!is_pid($property)) { throw new Exception("First argument in searchBy() must be a valid Wikidata property ID (e.g.: P646).", 1); @@ -69,13 +69,13 @@ public function searchBy($property, $value = null, $lang = 'en', int $limit = 10 $data = $client->execute($query); - $ids = collect($data)->map(fn($data) => str_replace("http://www.wikidata.org/entity/", "", $data['item']))->toArray(); + $ids = collect($data)->map(fn($data): string|array => str_replace("http://www.wikidata.org/entity/", "", $data['item']))->toArray(); $client = new ApiClient(); $entities = $client->getEntities($ids, $lang, ['sitelinks/urls', 'aliases', 'descriptions', 'labels']); - $output = $entities->map(function ($data) use ($lang) { + $output = $entities->map(function ($data) use ($lang): \Wikidata\SearchResult { $entity = new Entity($data, $lang); return new SearchResult($entity->toArray(), $lang); }); @@ -91,7 +91,7 @@ public function searchBy($property, $value = null, $lang = 'en', int $limit = 10 * * @return \Wikidata\Entity Return entity */ - public function get($entityId, $lang = 'en') + public function get(string $entityId, string $lang = 'en'): \Wikidata\Entity { if (!is_qid($entityId)) { throw new Exception("First argument in get() must by a valid Wikidata entity ID (e.g.: Q646).", 1); diff --git a/src/helpers.php b/src/helpers.php index f8a4527..305d1ca 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -9,7 +9,7 @@ * @return bool Return true if string is valid or false */ if (!function_exists('is_qid')) { - function is_qid($value) + function is_qid($value): int|false { return preg_match("/^Q[0-9]+/", (string) $value); } @@ -23,7 +23,7 @@ function is_qid($value) * @return bool Return true if string is valid or false */ if (!function_exists('is_pid')) { - function is_pid($value) + function is_pid($value): int|false { return preg_match("/^P[0-9]+/", (string) $value); } From 215fe6afd41c3396d6d10853862ef71de6f97d64 Mon Sep 17 00:00:00 2001 From: Tac Tacelosky Date: Tue, 25 Jun 2024 15:34:37 -0400 Subject: [PATCH 11/19] add composer test --- composer.json | 3 +++ 1 file changed, 3 insertions(+) diff --git a/composer.json b/composer.json index 11377a0..fd85f61 100644 --- a/composer.json +++ b/composer.json @@ -19,6 +19,9 @@ "role": "developer" } ], + "scripts": { + "test": "vendor/bin/phpunit" + }, "require": { "php": "^8.2", "illuminate/collections": "^10.0|^11.0", From 2f187ef93b3aebddefc7ad4c2ab13ceed226a9ad Mon Sep 17 00:00:00 2001 From: Tac Tacelosky Date: Tue, 25 Jun 2024 13:35:25 -0600 Subject: [PATCH 12/19] Create php.yml --- .github/workflows/php.yml | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 .github/workflows/php.yml diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml new file mode 100644 index 0000000..7145fe0 --- /dev/null +++ b/.github/workflows/php.yml @@ -0,0 +1,39 @@ +name: PHP Composer + +on: + push: + branches: [ "master" ] + pull_request: + branches: [ "master" ] + +permissions: + contents: read + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + + - name: Validate composer.json and composer.lock + run: composer validate --strict + + - name: Cache Composer packages + id: composer-cache + uses: actions/cache@v3 + with: + path: vendor + key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }} + restore-keys: | + ${{ runner.os }}-php- + + - name: Install dependencies + run: composer install --prefer-dist --no-progress + + # Add a test script to composer.json, for instance: "test": "vendor/bin/phpunit" + # Docs: https://getcomposer.org/doc/articles/scripts.md + + - name: Run test suite + run: composer run-script test From cc00f0e1a3b32f16325894eb2b856650868bd25e Mon Sep 17 00:00:00 2001 From: Tac Tacelosky Date: Tue, 25 Jun 2024 15:39:56 -0400 Subject: [PATCH 13/19] remove version so composer.json validates --- composer.json | 1 - 1 file changed, 1 deletion(-) diff --git a/composer.json b/composer.json index fd85f61..f389ba1 100644 --- a/composer.json +++ b/composer.json @@ -8,7 +8,6 @@ ], "homepage": "https://github.com/survos/wikidata", "license": "MIT", - "version": "5.0.0", "authors": [ { "name": "Arhey", From 020e3806df2379e194aee7d06e41976f3c2c2ab2 Mon Sep 17 00:00:00 2001 From: Tac Tacelosky Date: Tue, 25 Jun 2024 15:54:49 -0400 Subject: [PATCH 14/19] try to limit to php 8.2 only --- .github/workflows/php.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index 7145fe0..1fe2cbb 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -17,6 +17,11 @@ jobs: steps: - uses: actions/checkout@v4 + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: 8.2 + - name: Validate composer.json and composer.lock run: composer validate --strict From 10b7840dadf052db8ffb18008dbd27287dd8acce Mon Sep 17 00:00:00 2001 From: Thomas Berends Date: Sun, 25 Aug 2024 11:18:50 +0200 Subject: [PATCH 15/19] Make description nullable --- src/Entity.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Entity.php b/src/Entity.php index a240e09..0fc4069 100644 --- a/src/Entity.php +++ b/src/Entity.php @@ -28,9 +28,9 @@ class Entity public $aliases = []; /** - * @var string Entity description + * @var string|null Entity description */ - public string $description; + public ?string $description; public Collection $properties; From 0a326949df691f06a13964e84e962ce5b2b88342 Mon Sep 17 00:00:00 2001 From: Tac Tacelosky Date: Sun, 25 May 2025 19:55:10 -0400 Subject: [PATCH 16/19] update to php 8.3, rector, allow latest collections --- composer.json | 10 +++++----- rector.php | 3 +-- src/Entity.php | 2 +- src/Qualifier.php | 2 +- src/SparqlClient.php | 2 +- src/Value.php | 2 +- src/Wikidata.php | 2 +- 7 files changed, 11 insertions(+), 12 deletions(-) diff --git a/composer.json b/composer.json index f389ba1..2d6c3b6 100644 --- a/composer.json +++ b/composer.json @@ -22,9 +22,9 @@ "test": "vendor/bin/phpunit" }, "require": { - "php": "^8.2", - "illuminate/collections": "^10.0|^11.0", - "symfony/http-client": "^6.4||^7.0" + "php": "^8.3", + "illuminate/collections": "^11.0||^12.0", + "symfony/http-client": "^6.4||^7.2" }, "autoload": { "psr-4": { @@ -44,7 +44,7 @@ }, "require-dev": { "phpunit/phpunit": "^9.0", - "phpstan/phpstan": "^1.10", - "rector/rector": "^1.1" + "phpstan/phpstan": "^2.0", + "rector/rector": "^2.0" } } diff --git a/rector.php b/rector.php index 0174aa4..f6d0f6d 100644 --- a/rector.php +++ b/rector.php @@ -13,11 +13,10 @@ __DIR__ . '/tests', ]) // uncomment to reach your current PHP version - ->withPhpSets(php82: true) + ->withPhpSets(php83: true) ->withSets([ SetList::TYPE_DECLARATION, ]) ->withRules([ TypedPropertyFromAssignsRector::class, - AddVoidReturnTypeWhereNoReturnRector::class, ]); diff --git a/src/Entity.php b/src/Entity.php index 0fc4069..e07c97e 100644 --- a/src/Entity.php +++ b/src/Entity.php @@ -38,7 +38,7 @@ class Entity * @param array $data * @param string $lang */ - public function __construct($data, public $lang) + public function __construct(array $data, public $lang) { $this->properties = new Collection(); $this->parseData($data); diff --git a/src/Qualifier.php b/src/Qualifier.php index bbafe5c..abeaf17 100644 --- a/src/Qualifier.php +++ b/src/Qualifier.php @@ -22,7 +22,7 @@ class Qualifier /** * @param array $data */ - public function __construct($data) + public function __construct(array $data) { $this->parseData($data); } diff --git a/src/SparqlClient.php b/src/SparqlClient.php index 8443706..7811ab9 100644 --- a/src/SparqlClient.php +++ b/src/SparqlClient.php @@ -48,7 +48,7 @@ public function __construct( * @return array List of results, one row per array element * Each row will contain fields indexed by variable name. */ - public function execute( $query, $rawData = false ) { + public function execute( $query, $rawData = false ): array { if ( strlen( $query ) > self::MAX_GET_SIZE ) { // big requests go to POST diff --git a/src/Value.php b/src/Value.php index 31aebd9..f63783d 100644 --- a/src/Value.php +++ b/src/Value.php @@ -24,7 +24,7 @@ class Value /** * @param array $data */ - public function __construct($data) + public function __construct(array $data) { $this->parseData($data); } diff --git a/src/Wikidata.php b/src/Wikidata.php index 08da2ef..63eee15 100644 --- a/src/Wikidata.php +++ b/src/Wikidata.php @@ -47,7 +47,7 @@ public function search($query, $lang = 'en', int $limit = 10) * * @return \Illuminate\Support\Collection Return collection of \Wikidata\SearchResult */ - public function searchBy(string $property, $value = null, $lang = 'en', int $limit = 10) + public function searchBy(string $property, $value = null, string $lang = 'en', int $limit = 10) { if (!is_pid($property)) { throw new Exception("First argument in searchBy() must be a valid Wikidata property ID (e.g.: P646).", 1); From acffecefa22d685e77c8e81e401a7fbd9db59ecc Mon Sep 17 00:00:00 2001 From: Tac Tacelosky Date: Sun, 7 Sep 2025 08:20:37 -0400 Subject: [PATCH 17/19] note to use survos/wiki-bundle instead --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 156b305..a79069b 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,8 @@ Wikidata provides a API for searching and retrieving data from [wikidata.org](ht Fork of https://github.com/freearhey/wikidata @todo: consider https://github.com/freearhey/wikidata/compare/master...JamesFrost:wikidata:master +This library is no longer used by survos/wiki-bundle, Symfony developers may want to use that directly since it adds caching and more options, and does not relay on illuminate/collections. + ## Installation ```sh From 575a9e0651eb4029a7f85b503a5c806e4530e75e Mon Sep 17 00:00:00 2001 From: Tac Tacelosky Date: Tue, 9 Dec 2025 07:18:07 -0500 Subject: [PATCH 18/19] update versions --- composer.json | 4 ++-- src/Entity.php | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/composer.json b/composer.json index 2d6c3b6..8c6cd4c 100644 --- a/composer.json +++ b/composer.json @@ -22,9 +22,9 @@ "test": "vendor/bin/phpunit" }, "require": { - "php": "^8.3", + "php": "^8.4", "illuminate/collections": "^11.0||^12.0", - "symfony/http-client": "^6.4||^7.2" + "symfony/http-client": "^7.3" }, "autoload": { "psr-4": { diff --git a/src/Entity.php b/src/Entity.php index e07c97e..929f343 100644 --- a/src/Entity.php +++ b/src/Entity.php @@ -10,27 +10,27 @@ class Entity /** * @var string Entity Id */ - public $id; + public string $id; /** * @var string Entity label */ - public $label; + public string $label; /** * @var string A link to a Wikipedia article about this entity */ - public $wiki_url = null; + public ?string $wiki_url = null; /** * @var string[] List of entity aliases */ - public $aliases = []; + public array $aliases = []; /** * @var string|null Entity description */ - public ?string $description; + public ?string $description=null; public Collection $properties; @@ -68,7 +68,7 @@ private function parseData(array $data): void */ public function parseProperties(array|Collection $data): void { - $collection = (new Collection($data))->groupBy('prop'); + $collection = new Collection($data)->groupBy('prop'); $this->properties = $collection->mapWithKeys(function ($item): array { $property = new Property($item); From 5f2422ae985f7d0d962dd90a0f2e2ca024cdfabb Mon Sep 17 00:00:00 2001 From: Tac Tacelosky Date: Tue, 9 Dec 2025 07:20:57 -0500 Subject: [PATCH 19/19] update libraries --- composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 8c6cd4c..7ca566c 100644 --- a/composer.json +++ b/composer.json @@ -24,7 +24,7 @@ "require": { "php": "^8.4", "illuminate/collections": "^11.0||^12.0", - "symfony/http-client": "^7.3" + "symfony/http-client": "^7.4||^8.0" }, "autoload": { "psr-4": { @@ -43,6 +43,7 @@ ] }, "require-dev": { + "roave/security-advisories": "dev-latest", "phpunit/phpunit": "^9.0", "phpstan/phpstan": "^2.0", "rector/rector": "^2.0"