Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow tightenco/collect 9, require php7.4+ #23

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
[![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).

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
Expand Down Expand Up @@ -208,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

Expand Down
19 changes: 13 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
{
"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": "5.0.0",
"authors": [
{
"name": "Arhey",
"role": "developer"
},
{
"name": "tacman",
"role": "developer"
}
],
"require": {
"tightenco/collect": "^8.0",
"guzzlehttp/guzzle": "^7.0"
"php": "^8.2",
"illuminate/collections": "^10.0|^11.0",
"symfony/http-client": "^6.4||^7.0"
},
"autoload": {
"psr-4": {
Expand All @@ -36,6 +41,8 @@
]
},
"require-dev": {
"phpunit/phpunit": "^9.0"
"phpunit/phpunit": "^9.0",
"phpstan/phpstan": "^1.10",
"rector/rector": "^1.1"
}
}
5 changes: 5 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
parameters:
level: 4
# inferPrivatePropertyTypeFromConstructor: true
paths:
- ./src/
23 changes: 23 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\TypeDeclaration\Rector\ClassMethod\AddVoidReturnTypeWhereNoReturnRector;
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromAssignsRector;
use Rector\Set\ValueObject\SetList;

return RectorConfig::configure()
->withPaths([
__DIR__ . '/src',
__DIR__ . '/tests',
])
// uncomment to reach your current PHP version
->withPhpSets(php82: true)
->withSets([
SetList::TYPE_DECLARATION,
])
->withRules([
TypedPropertyFromAssignsRector::class,
AddVoidReturnTypeWhereNoReturnRector::class,
]);
131 changes: 64 additions & 67 deletions src/ApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,86 +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';
public const API_ENDPOINT = 'https://www.wikidata.org/w/api.php';

/**
* @var string Value Id
*/
private $client;

/**
* @param array $data
*/
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 string $ids The IDs of the entities to get the data from (eg.: Q2)
* @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)
*
* @return \Illuminate\Support\Collection
*/
public function getEntities($ids, $lang = 'en', $props = [])
{
$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, string $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 = $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 string $limit Max count of returning items (default: 10)
*
* @return \Illuminate\Support\Collection
*/
public function searchEntities($query, $lang = 'en', $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 = $results['search'] ?? [];

return collect($data);
}
return collect($data);
}
}
29 changes: 11 additions & 18 deletions src/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Wikidata;

use Illuminate\Support\Collection;
use Wikidata\Property;

class Entity
Expand All @@ -11,11 +12,6 @@ class Entity
*/
public $id;

/**
* @var string Entity language
*/
public $lang;

/**
* @var string Entity label
*/
Expand All @@ -34,20 +30,17 @@ 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
* @param string $lang
*/
public function __construct($data, $lang)
public function __construct($data, public $lang)
{
$this->lang = $lang;
$this->properties = new Collection();
$this->parseData($data);
}

Expand All @@ -56,7 +49,7 @@ public function __construct($data, $lang)
*
* @param array $data
*/
private function parseData($data)
private function parseData(array $data): void
{
$lang = $this->lang;
$site = $lang . 'wiki';
Expand All @@ -71,19 +64,19 @@ 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): void
{
$collection = collect($data)->groupBy('prop');
$this->properties = $collection->mapWithKeys(function ($item) {
$collection = (new Collection($data))->groupBy('prop');
$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,
Expand Down
8 changes: 3 additions & 5 deletions src/Property.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,16 @@ public function __construct($data)

/**
* Parse input data
*
*
* @param array $data
*/
private function parseData($data)
private function parseData($data): void
{
$grouped = collect($data)->groupBy('statement');
$flatten = $grouped->flatten(1);

$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): \Wikidata\Value => new Value($v->toArray()));
}
}
2 changes: 1 addition & 1 deletion src/Qualifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function __construct($data)
*
* @param array $data
*/
private function parseData($data)
private function parseData(array $data): void
{
$this->id = get_id($data['qualifier']);
$this->label = $data['qualifierLabel'];
Expand Down
Loading