-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
410 additions
and
276 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,4 @@ composer.lock | |
/examples | ||
.idea | ||
/demo | ||
.phpunit.* | ||
.phpunit.* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.