Giter Club home page Giter Club logo

wikidata's Introduction

wikidata

Wikidata Build Status

Wikidata provides a API for searching and retrieving data from wikidata.org.

Installation

composer require freearhey/wikidata

Usage

First we need to create an instance of Wikidata class and save it to some variable, like this:

require_once('vendor/autoload.php');

use Wikidata\Wikidata;

$wikidata = new Wikidata();

after that we can use one of the available methods to access the Wikidata database.

Available Methods

search()

The search() method give you a way to find Wikidata entity by it label.

$results = $wikidata->search($query, $lang, $limit);

Arguments:

  • $query: term to search (required)
  • $lang: specify the results language (default: 'en')
  • $limit: set a custom limit (default: 10)

Example:

$results = $wikidata->search('car', 'fr', 5);

/*
  Collection {
    #items: array:5 [
      0 => SearchResult {
        id: "Q1759802"
        lang: "fr"
        label: "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"
        ]
      }
      1 => SearchResult {
        id: "Q224743"
        lang: "fr"
        label: "Car"
        wiki_url: "https://fr.wikipedia.org/wiki/Car"
        description: "page d'homonymie d'un projet Wikimédia"
        aliases: []
      }
      ...
    ]
  }
*/

The search() method always returns Illuminate\Support\Collection class with results. This means you can use all the methods available in Laravel's Collections.

searchBy()

The searchBy help you to find Wikidata entities by it properties value.

$results = $wikidata->searchBy($propId, $entityId, $lang, $limit);

Arguments:

  • $propId: id of the property by which to search (required)
  • $entityId: id of the entity (required)
  • $lang: specify the results language (default: 'en')
  • $limit: set a custom limit (default: 10)

Example:

// List of people who born in city Pomona, US
$results = $wikidata->searchBy('P19', 'Q486868');

/*
  Collection {
    #items: array:10 [
      0 => SearchResult {
        id: "Q92638"
        lang: "en"
        label: "Robert Tarjan"
        wiki_url: "https://en.wikipedia.org/wiki/Robert_Tarjan"
        description: "American computer scientist"
        aliases: array:2 [
          0 => "Robert E. Tarjan"
          1 => "Robert Endre Tarjan"
        ]
      }
      1 => SearchResult {
        id: "Q184805"
        lang: "en"
        label: "Tom Waits"
        wiki_url: "https://en.wikipedia.org/wiki/Tom_Waits"
        description: "American singer-songwriter and actor"
        aliases: []
      }
      ...
    ]
  }
*/

The searchBy() method always returns Illuminate\Support\Collection class with results. This means you can use all the methods available in Laravel's Collections.

get()

The get() returns Wikidata entity by specified ID.

$entity = $wikidata->get($entityId, $lang);

Arguments:

  • $entityId: id of the entity (required)
  • $lang: specify the results language (default: 'en')

Example:

// Get all data about Steve Jobs
$entity = $wikidata->get('Q19837');

/*
  Entity {
    id: "Q19837"
    lang: "en"
    label: "Steve Jobs"
    wiki_url: "https://en.wikipedia.org/wiki/Steve_Jobs"
    aliases: array:2 [
      0 => "Steven Jobs",
      1 => "Steven Paul Jobs"
    ]
    description: "American entrepreneur and co-founder of Apple Inc."
    properties: Collection { ... }
  }
*/


// List of all properties as an array
$properties = $entity->properties->toArray();

/*
  [
    "P1006" => Property {
      id: "P1006"
      label: "NTA ID"
      values: Collection {
        #items: array:6 [
          0 => Value {
            id: "Q5593916"
            label: "Grammy Trustees Award"
            qualifiers: Collection {
              #items: array:1 [
                0 => Qualifier {
                  id: "P585"
                  label: "point in time"
                  value: "2012-01-01T00:00:00Z"
                }
              ]
            }
          },
          ...
        ]
      }
    },
    ...
  ]
 */

Upgrade Guide

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.

Also, values are now presented not as a string, but as an Value object. This allowed us to save along with each value not only its string representation but also a list of its qualifiers. Detailed information on what qualifiers is can be found here.

Testing

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 or a pull request.

License

Wikidata is licensed under the MIT license.

wikidata's People

Contributors

andrei-i-gavrila avatar freearhey avatar genietim avatar jamesfrost avatar jeremytubbs avatar mpipet avatar mrkriskrisu avatar pat-o avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

wikidata's Issues

Query Property Labels & Wikipedia URL

Hi Aleksandr,

thank you so much for this. It's very useful and fun to use!
However I'm wondering if it's possible to get the label of a property as well. E.g. if I use $entity->getPropertyValues('P138') which returns the "Named After" property, I would love to get that label as well. Maybe something like $entity->getPropertyValues('P138')->getLabel()` would be perfect.

Another questions which was already asked on StackOverflow is, whether it's possible to get the related Wikipedia URL as well. E.g. searching at Wikidata for "Taj Mahal" provides a list of the wikipedia articles in different languages. It would be perfect to just get the URL of the queried WikiData entry. E.g. $entity->getWikipediaURL('en').

Looking forward to your answer an thank you, again!

Philipp

Exclude some properties

Is there a way to exclude some properties for instance let's say I want to get humans who are still alive but exclude those who have passed?

wikipedia_article is completely bogus

In the search function, a piece of code create an url 'wikipedia_article'.

$collection = $wikidata->search($keyword);

This url is the simple concatenation of the domain name and the label.

        $output = $collection->map(function ($item) use ($lang) {
            $item['wikipediaArticle'] = 'https://' . $lang . '.wikipedia.org/wiki/' . rawurlencode($item['label']);
            return new SearchResult($item, $lang);
        });

But the label does not contain the differentiation information between the different results. So, sometimes they all have the same URL...

image

Error in Quantity value

ErrorException in QuantityValue.php line 14: Undefined property: stdClass::$upperBound

Reproduce:

$result = $wikidata->search('Philipp Lahm')->first()->getEntityId();
$wikidata->entities($result);

Fix:

$this->upperBound = isset($value->upperBound) ? $value->upperBound : null;
$this->lowerBound = isset($value->lowerBound) ? $value->lowerBound : null;

Caching?

Would be it possible to add save/load response feature? That could serve as caching system -for testing projects that needs a few (or way more) fetches, eventually to manually change data to test edge cases ("what my script will do if this field won't be filled on WD"). And even better for public scripts, with a lot of hits where author could easily cache data for certain time.

I imagine it something like:

if (file_exists('cache.txt') $wikidata->loadString(file_get_contents('cache.txt')); // and loadFile('cache.txt')? That would be rad
else { $wikidata->get('Q12345'); file_put_contents('cache.txt', $wikidata->rawResponse); }

So an additional property, which is filled upon get() method and can be populated with loadString() method (and then processed, as it would be response from curl). Optionally (because one can keep track of it by oneself, not relying on class), a property source to display curl, string or file (in case f proposed loadFile())

Unkown value

Hi, thanks for providing this helpfull module for Wikidata.
I did see you worked on no value issues, but I still run into one. I tried to get entity of Q347 (State of Liechtenstein) and got errors (if I did correctly debug that) on the 'end date' of certain presidencies when set to "unknown value". So on qualifiers, if I'm correct?
They are set as in API response as: ["snaktype"]=> string(9) "somevalue"

Hope this helps you to make the module better. Thanks.
Best,
Roeneman

Allow for other http clients

I'd rather use Symfony's HttpClient than guzzle. I believe there's a way to simply say "This needs a psr-7 http client, but it doesn't matter which one".

Alas, I'm not sure of the details. But since my project already uses the Symfony client, I don't want to introduce guzzle just for this one call.

Great library, BTW. Thanks for releasing it.

Not able to install it with Laravel 5.8 and tightenco/collect 5.8

Error I get is:

Your requirements could not be resolved to an installable set of packages.

Problem 1
- Installation request for freearhey/wikidata ^3.0 -> satisfiable by freearhey/wikidata[3.0.0].
- freearhey/wikidata 3.0.0 requires tightenco/collect 5.6.35 -> satisfiable by tightenco/collect[v5.6.35] but these conflict with your requirements or minimum-stability.

Installation failed, reverting ./composer.json to its original content.

How to access child properties?

Thank you very much for providing this package! I have the following problem: I would like to access e.g. property P1033, which is a child of P4952. How can I access this using this library? For example, entity Q49546: when accessing P4952 on Q59546, I only get

"P4952" => Wikidata\Property^ {#576
        +id: "P4952"
        +label: "safety classification and labelling"
        +value: "Regulation (EC) No. 1272/2008, NFPA 704: Standard System for the Identification of the Hazards of Materials for Emergency Response"
      }

without any children (no GHS properties). Trying to access the child properties (P1033) directly on the root entity (Q59546) does not help, as they are not set there. How can I achive this? If this is not yet possible, do you accept PRs?

Bump to PHP 8, tighten/collect:^9

Now that php 7.4 is officially EOL, what do you think of dropping support for the older versions?

That would allow for upgrading the library to return typehints, etc.

Or, at a minimum what do you think of requiring ^7.4|^8.0 in composer.json, and tighten/collect:^8.0|^9.0

I can make a PR if you're okay with that. My preference would be for PHP 8.0+, but maybe you'd want that to be a new release.

how to get property id of property

how to get property id like instance of => P31

it is great if we have function $entity->getPropertyArray() and return like this:

[
 'P31' => [
   'property' => 'instance of',
   'value' => 'film',
 ],
 ...
];

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.