|
1 | | -Wikidata [](https://travis-ci.org/freearhey/wikidata) |
2 | | -======== |
| 1 | +[](https://wikidata.org) |
| 2 | + |
| 3 | + |
| 4 | +# Wikidata [](https://travis-ci.org/freearhey/wikidata) |
3 | 5 |
|
4 | 6 | Wikidata provides a API for searching and retrieving data from [wikidata.org](https://www.wikidata.org). |
5 | 7 |
|
6 | | -### Installation |
| 8 | +## Installation |
| 9 | + |
7 | 10 | ```sh |
8 | 11 | composer require freearhey/wikidata |
9 | 12 | ``` |
10 | 13 |
|
11 | | -### Usage |
| 14 | +## Usage |
| 15 | + |
| 16 | +First we need to create an instance of `Wikidata` class and save it to some variable, like this: |
12 | 17 |
|
13 | 18 | ```php |
14 | 19 | $wikidata = new Wikidata(); |
15 | 20 | ``` |
16 | 21 |
|
17 | | -#### Search |
| 22 | +After that we can use one of the available methods to access the Wikidata database: |
18 | 23 |
|
19 | | -Search by entity label: |
20 | 24 | ```php |
21 | | -$results = $wikidata->search('London'); |
| 25 | +$wikidata->search('London'); |
22 | 26 | ``` |
23 | 27 |
|
24 | | -Search by Wikidata property ID and string value: |
25 | | -```php |
26 | | -$results = $wikidata->searchBy('P238', 'LON'); |
27 | | -``` |
| 28 | +## Available Methods |
28 | 29 |
|
29 | | -Search by Wikidata property ID and entity ID: |
30 | | -```php |
31 | | -$results = $wikidata->searchBy('P17', 'Q146'); |
32 | | -``` |
| 30 | +### `search()` |
| 31 | + |
| 32 | +The `search()` method give you a way to find Wikidata entity by it label. |
33 | 33 |
|
34 | | -Check if no search results |
35 | 34 | ```php |
36 | | -if($results->isEmpty()) { |
37 | | - echo 'no results'; |
38 | | - die(); |
39 | | -} |
| 35 | +$results = $wikidata->search($query, $lang, $limit); |
40 | 36 | ``` |
41 | 37 |
|
42 | | -#### Result |
| 38 | +Arguments: |
43 | 39 |
|
44 | | -Retrieve first result data |
45 | | -```php |
46 | | -$singleResult = $results->first(); |
47 | | -``` |
| 40 | +- `$query`: term to search (required) |
| 41 | +- `$lang`: specify the results language (default: 'en') |
| 42 | +- `$limit`: set a custom limit (default: 10) |
48 | 43 |
|
49 | | -Get result ID |
50 | | -```php |
51 | | -$resultId = $singleResult->id; // Q84 |
52 | | -``` |
| 44 | +Example: |
53 | 45 |
|
54 | | -Get result label |
55 | 46 | ```php |
56 | | -$resultLabel = $singleResult->label; // London |
| 47 | +$results = $wikidata->search('car', 'fr', 5); |
57 | 48 | ``` |
58 | 49 |
|
59 | | -Get result aliases |
60 | | -```php |
61 | | -$resultAliases = $singleResult->aliases; // [ 'London, UK', 'London, United Kingdom', 'London, England' ] |
62 | | -``` |
| 50 | +The `search()` method always returns `Illuminate\Support\Collection` class with results. This means you can use all the [methods available](https://laravel.com/docs/5.6/collections#available-methods) in Laravel's Collections. |
63 | 51 |
|
64 | | -Get result description |
65 | | -```php |
66 | | -$resultDescription = $singleResult->description; // capital of England and the United Kingdom |
67 | | -``` |
| 52 | +### `searchBy()` |
68 | 53 |
|
69 | | -#### Entity |
| 54 | +The `searchBy` help you to find Wikidata entities by it properties value. |
70 | 55 |
|
71 | | -Get single entity by ID: |
72 | 56 | ```php |
73 | | -$entity = $wikidata->get('Q26'); |
| 57 | +$results = $wikidata->searchBy($propId, $entityId, $lang, $limit); |
74 | 58 | ``` |
75 | 59 |
|
76 | | -Get entity ID |
77 | | -```php |
78 | | -$entityId = $entity->id; // Q26 |
79 | | -``` |
| 60 | +Arguments: |
80 | 61 |
|
81 | | -Get entity label |
82 | | -```php |
83 | | -$entityLabel = $entity->label; // Northern Ireland |
84 | | -``` |
| 62 | +- `$propId`: id of the property by which to search (required) |
| 63 | +- `$entityId`: id of the entity (required) |
| 64 | +- `$lang`: specify the results language (default: 'en') |
| 65 | +- `$limit`: set a custom limit (default: 10) |
85 | 66 |
|
86 | | -Get entity aliases |
87 | | -```php |
88 | | -$entityAliases = $entity->aliases; // [ 'NIR', 'UKN', 'North Ireland' ] |
89 | | -``` |
| 67 | +Example: |
90 | 68 |
|
91 | | -Get entity description |
92 | 69 | ```php |
93 | | -$entityDescription = $entity->description; // region in north-west Europe, part of the United Kingdom |
| 70 | +// List of people who born in city Pomona, US |
| 71 | +$results = $wikidata->searchBy('P19', 'Q486868'); |
| 72 | + |
| 73 | +/* |
| 74 | + Collection { |
| 75 | + #items: array:10 [ |
| 76 | + 0 => SearchResult { |
| 77 | + id: "Q22254338" |
| 78 | + lang: "en" |
| 79 | + label: "Coco Velvett" |
| 80 | + description: "American pornographic actress" |
| 81 | + aliases: array:2 [] |
| 82 | + } |
| 83 | + 1 => SearchResult { |
| 84 | + id: "Q24176246" |
| 85 | + lang: "en" |
| 86 | + label: "Donald D. Engen" |
| 87 | + description: null |
| 88 | + aliases: [] |
| 89 | + } |
| 90 | + ... |
| 91 | + ] |
| 92 | + } |
| 93 | +*/ |
94 | 94 | ``` |
95 | 95 |
|
96 | | -Get list of all available properties for specific entity |
| 96 | +The `searchBy()` method always returns `Illuminate\Support\Collection` class with results. This means you can use all the [methods available](https://laravel.com/docs/5.6/collections#available-methods) in Laravel's Collections. |
| 97 | + |
| 98 | +### `get()` |
| 99 | + |
| 100 | +The `get()` returns Wikidata entity by specified ID. |
| 101 | + |
97 | 102 | ```php |
98 | | -$properties = $entity->properties; // array(1) { [0]=> string(11) "instance_of", ... } |
| 103 | +$entity = $wikidata->get($entityId, $lang); |
99 | 104 | ``` |
100 | 105 |
|
101 | | -Get value specific property |
| 106 | +Arguments: |
| 107 | + |
| 108 | +- `$entityId`: id of the entity (required) |
| 109 | +- `$lang`: specify the results language (default: 'en') |
| 110 | + |
| 111 | +Example: |
| 112 | + |
102 | 113 | ```php |
103 | | -$official_language = $entity->get('official_language'); // array(1) { [0]=> string(7) "English" } |
| 114 | +// Get all data about Steve Jobs |
| 115 | +$entity = $wikidata->get('Q19837'); |
| 116 | + |
| 117 | +/* |
| 118 | + Entity { |
| 119 | + id: "Q19837" |
| 120 | + lang: "en" |
| 121 | + label: "Steve Jobs" |
| 122 | + aliases: array:2 [ |
| 123 | + 0 => "Steven Jobs" |
| 124 | + 1 => "Steven Paul Jobs" |
| 125 | + ] |
| 126 | + description: "American entrepreneur and co-founder of Apple Inc." |
| 127 | + properties: Collection { |
| 128 | + #items: array:98 [ |
| 129 | + "P18" => Property { |
| 130 | + id: "P18" |
| 131 | + label: "image" |
| 132 | + value: "http://commons.wikimedia.org/wiki/Special:FilePath/Steve%20Jobs%20Headshot%202010-CROP2.jpg" |
| 133 | + } |
| 134 | + ... |
| 135 | + ] |
| 136 | + } |
| 137 | + } |
| 138 | +*/ |
| 139 | + |
| 140 | + |
| 141 | +// List of all properties as array |
| 142 | +$properties = $entity->properties->toArray(); |
| 143 | + |
| 144 | +/* |
| 145 | + [ |
| 146 | + "P18" => Property { |
| 147 | + id: "P18" |
| 148 | + label: "image" |
| 149 | + value: "http://commons.wikimedia.org/wiki/Special:FilePath/Steve%20Jobs%20Headshot%202010-CROP2.jpg" |
| 150 | + }, |
| 151 | + "P19" => Property { |
| 152 | + id: "P19" |
| 153 | + label: "place of birth" |
| 154 | + value: "San Francisco" |
| 155 | + }, |
| 156 | + ... |
| 157 | + ] |
| 158 | + */ |
| 159 | +``` |
| 160 | + |
| 161 | +### Testing |
| 162 | + |
| 163 | +```sh |
| 164 | +vendor/bin/phpunit |
104 | 165 | ``` |
105 | 166 |
|
106 | | -That's all. |
| 167 | +### Contribution |
| 168 | +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). |
107 | 169 |
|
108 | 170 | ### License |
109 | 171 | Wikidata is licensed under the [MIT license](http://opensource.org/licenses/MIT). |
0 commit comments