Skip to content

Commit

Permalink
Merge branch 'v3.6.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
freearhey committed Dec 20, 2020
2 parents b8845ac + 8c96d40 commit 3760252
Show file tree
Hide file tree
Showing 16 changed files with 410 additions and 276 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ composer.lock
/examples
.idea
/demo
.phpunit.*
.phpunit.*
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: php
php:
- '7.2'
- '7.3'
before_script:
- composer install
script: phpunit
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
[![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 provides a API for searching and retrieving data from [wikidata.org](https://www.wikidata.org).
Expand Down Expand Up @@ -53,7 +52,7 @@ $results = $wikidata->search('car', 'fr', 5);
id: "Q1759802"
lang: "fr"
label: "autocar"
wikipedia_article: "https://fr.wikipedia.org/wiki/autocar"
wiki_url: "https://fr.wikipedia.org/wiki/autocar"
description: "transport routier pouvant accueillir plusieurs voyageurs pour de longues distances"
aliases: array:1 [
0 => "car"
Expand All @@ -63,7 +62,7 @@ $results = $wikidata->search('car', 'fr', 5);
id: "Q224743"
lang: "fr"
label: "Car"
wikipedia_article: "https://fr.wikipedia.org/wiki/Car"
wiki_url: "https://fr.wikipedia.org/wiki/Car"
description: "page d'homonymie d'un projet Wikimédia"
aliases: []
}
Expand Down Expand Up @@ -103,7 +102,7 @@ $results = $wikidata->searchBy('P19', 'Q486868');
id: "Q92638"
lang: "en"
label: "Robert Tarjan"
wikipedia_article: "https://en.wikipedia.org/wiki/Robert_Tarjan"
wiki_url: "https://en.wikipedia.org/wiki/Robert_Tarjan"
description: "American computer scientist"
aliases: array:2 [
0 => "Robert E. Tarjan"
Expand All @@ -114,7 +113,7 @@ $results = $wikidata->searchBy('P19', 'Q486868');
id: "Q184805"
lang: "en"
label: "Tom Waits"
wikipedia_article: "https://en.wikipedia.org/wiki/Tom_Waits"
wiki_url: "https://en.wikipedia.org/wiki/Tom_Waits"
description: "American singer-songwriter and actor"
aliases: []
}
Expand Down Expand Up @@ -150,7 +149,7 @@ $entity = $wikidata->get('Q19837');
id: "Q19837"
lang: "en"
label: "Steve Jobs"
wikipedia_article: "https://en.wikipedia.org/wiki/Steve_Jobs"
wiki_url: "https://en.wikipedia.org/wiki/Steve_Jobs"
aliases: array:2 [
0 => "Steven Jobs",
1 => "Steven Paul Jobs"
Expand Down Expand Up @@ -195,7 +194,7 @@ $properties = $entity->properties->toArray();

### Upgrade Guide

#### Upgrade to 3.2.* from 3.1.*
#### Upgrade to 3.2._ from 3.1._

The main changes in the new version have occurred in the way property values are stored. Now each property can have more than one value. In this regard, the attribute `value` in the `Property` object was replaced with the attribute `values`.

Expand All @@ -208,7 +207,9 @@ 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).

### License

Wikidata is licensed under the [MIT license](http://opensource.org/licenses/MIT).
74 changes: 37 additions & 37 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
{
"name": "freearhey/wikidata",
"description": "A PHP client for working with Wikidata API.",
"keywords": [
"wikidata",
"client",
"php"
],
"homepage": "https://github.com/freearhey/wikidata",
"license": "MIT",
"version": "3.5.1",
"authors": [
{
"name": "Arhey",
"role": "developer"
}
],
"require": {
"tightenco/collect": "^7.11",
"guzzlehttp/guzzle": "^6.3"
},
"autoload": {
"psr-4": {
"Wikidata\\": "src"
},
"files": [
"src/helpers.php"
]
"name": "freearhey/wikidata",
"description": "A PHP client for working with Wikidata API.",
"keywords": [
"wikidata",
"client",
"php"
],
"homepage": "https://github.com/freearhey/wikidata",
"license": "MIT",
"version": "3.6.0",
"authors": [
{
"name": "Arhey",
"role": "developer"
}
],
"require": {
"tightenco/collect": "^8.0",
"guzzlehttp/guzzle": "^7.0"
},
"autoload": {
"psr-4": {
"Wikidata\\": "src"
},
"autoload-dev": {
"psr-4": {
"Wikidata\\Tests\\": "tests"
},
"files": [
"src/helpers.php"
]
"files": [
"src/helpers.php"
]
},
"autoload-dev": {
"psr-4": {
"Wikidata\\Tests\\": "tests"
},
"require-dev": {
"phpunit/phpunit": "^6.5"
}
"files": [
"src/helpers.php"
]
},
"require-dev": {
"phpunit/phpunit": "^9.0"
}
}
30 changes: 11 additions & 19 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="tests/bootstrap.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./app</directory>
</whitelist>
</filter>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" backupStaticAttributes="false" bootstrap="tests/bootstrap.php" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./app</directory>
</include>
</coverage>
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
</testsuites>
</phpunit>
87 changes: 87 additions & 0 deletions src/ApiClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

namespace Wikidata;

use GuzzleHttp\Client;

class ApiClient
{
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();
}

/**
* 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;

$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,
],
]);

$results = json_decode($response->getBody(), true);

$data = isset($results['entities']) ? $results['entities'] : [];

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' => '',
],
]);

$results = json_decode($response->getBody(), true);

$data = isset($results['search']) ? $results['search'] : [];

return collect($data);
}
}
42 changes: 33 additions & 9 deletions src/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Entity
/**
* @var string A link to a Wikipedia article about this entity
*/
public $wikipedia_article;
public $wiki_url = null;

/**
* @var string[] List of entity aliases
Expand All @@ -39,16 +39,16 @@ class Entity
/**
* @var \Illuminate\Support\Collection Collection of entity properties
*/
public $properties;
public $properties = [];

/**
* @param array $data
* @param string $lang
*/
public function __construct($data, $lang)
{
$this->parseData($data);
$this->lang = $lang;
$this->parseData($data);
}

/**
Expand All @@ -58,17 +58,41 @@ public function __construct($data, $lang)
*/
private function parseData($data)
{
$this->id = get_id($data[0]['item']);
$this->label = $data[0]['itemLabel'];
$this->wikipedia_article = $data[0]['wikipediaArticle'];
$this->aliases = is_string($data[0]['itemAltLabel']) ? explode(', ', $data[0]['itemAltLabel']) : [];
$this->description = $data[0]['itemDescription'];
$lang = $this->lang;
$site = $lang . 'wiki';

$this->id = $data['id'];
$this->label = isset($data['labels'][$lang]) ? $data['labels'][$lang]['value'] : null;
$this->description = isset($data['descriptions'][$lang]) ? $data['descriptions'][$lang]['value'] : null;
$this->wiki_url = isset($data['sitelinks'][$site]) ? $data['sitelinks'][$site]['url'] : null;
$this->aliases = isset($data['aliases'][$lang]) ? collect($data['aliases'][$lang])->pluck('value')->toArray() : [];
}

/**
* Parse entity properties from sparql result
*
* @param array $data
*/
public function parseProperties($data)
{
$collection = collect($data)->groupBy('prop');
$this->properties = $collection->mapWithKeys(function($item) {
$this->properties = $collection->mapWithKeys(function ($item) {
$property = new Property($item);

return [$property->id => $property];
});
}

public function toArray()
{
return [
'id' => $this->id,
'lang' => $this->lang,
'label' => $this->label,
'description' => $this->description,
'wiki_url' => $this->wiki_url,
'aliases' => $this->aliases,
'properties' => $this->properties,
];
}
}
8 changes: 4 additions & 4 deletions src/SearchResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class SearchResult
/**
* @var string
*/
public $wikipedia_article;
public $wiki_url;

/**
* @var string
Expand All @@ -37,18 +37,18 @@ class SearchResult
/**
* @param array $data
*/
public function __construct($data, $lang = 'en')
public function __construct($data, $lang = 'en')
{
$this->parseData($data);
$this->lang = $lang;
}

private function parseData($data)
private function parseData($data)
{
$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->wikipedia_article = isset($data['wikipediaArticle']) ? $data['wikipediaArticle'] : null;
$this->wiki_url = isset($data['wiki_url']) ? $data['wiki_url'] : null;
}
}
Loading

0 comments on commit 3760252

Please sign in to comment.