Skip to content
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
44 changes: 44 additions & 0 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
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: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.2

- 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
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
[![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

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
composer require freearhey/wikidata
composer require survos/wikidata
```

## Usage
Expand Down Expand Up @@ -208,7 +213,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
22 changes: 16 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
{
"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",
"authors": [
{
"name": "Arhey",
"role": "developer"
},
{
"name": "tacman",
"role": "developer"
}
],
"scripts": {
"test": "vendor/bin/phpunit"
},
"require": {
"tightenco/collect": "^8.0",
"guzzlehttp/guzzle": "^7.0"
"php": "^8.4",
"illuminate/collections": "^11.0||^12.0",
"symfony/http-client": "^7.4||^8.0"
},
"autoload": {
"psr-4": {
Expand All @@ -36,6 +43,9 @@
]
},
"require-dev": {
"phpunit/phpunit": "^9.0"
"roave/security-advisories": "dev-latest",
"phpunit/phpunit": "^9.0",
"phpstan/phpstan": "^2.0",
"rector/rector": "^2.0"
}
}
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/
22 changes: 22 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?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(php83: true)
->withSets([
SetList::TYPE_DECLARATION,
])
->withRules([
TypedPropertyFromAssignsRector::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);
}
}
39 changes: 16 additions & 23 deletions src/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,45 @@

namespace Wikidata;

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

class Entity
{
/**
* @var string Entity Id
*/
public $id;

/**
* @var string Entity language
*/
public $lang;
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 Entity description
* @var string|null Entity description
*/
public $description;
public ?string $description=null;

/**
* @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(array $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
Loading