Module Wikidata.Entity

Represents Wikidata Entities.

Why Objects?

Unlike most other Wikidata things in this library, Entities are represented by objects rather than records. This is because many Entities share fields, like how Items (Item.t) and Properties (Property.t) both have ids, labels, entity_types, descriptions, aliases, and claims.

Records are not polymorphic, and so were this library to use record types, code that uses only these shared fields would have to be duplicated to work on both Items and Properties.

By using objects, through the magic of row polymorphism, we can write code that operates on any object with compatible methods. For example, the following function can be run on both Item.ts and Property.ts:

let label_and_description lang entity : string * string =
  (
    entity#label lang,
    entity#description lang
  )

Were we to use record types for Items and Properties, this function would require pattern matching and then duplicating the code to work on both Items and Properties.

Virtual Classes

These classes are not meant to be instantiated, but represent features that are implemented in the non-virtual classes Item.t and Property.t.

class virtual basic_entity : id:string -> entity_type:string -> object ... end

Implements basic Entity functionality.

class virtual label_description_aliases_mixin : labels:(lang * string) list -> descriptions:(lang * string) list -> aliases:(lang * string list) list -> object ... end

Mixin for labels, descriptions, and aliases

class virtual statements_mixin : statements:(lang * Statement.t list) list -> object ... end

Mixin for Statements.

Items

module Item : sig ... end

Represents Wikidata Items.

Properties

module Property : sig ... end

Represents Wikidata Properties.

Entities

In this section, entities are represented as a variant type between different types of entities (like Item.ts and Property.ts) rather than as an object that is a subtype of both. The reason is that OCaml's object system lacks narrowing. As such, were we to return a mutual subtype of all entity types, methods that are not shared between those entity types would become inaccessible.

type t =
| Item of Item.t

Represents a Wikidata Item.

| Property of Property.t

Represents a Wikidata Property.

A variant type of all supported Entity types.

Conversions

val of_string : string -> t

Turns a json string representing some Entity (as described in the Wikibase JSON specification) into a t.

val of_entities_string : string -> t

Returns the first entity in a JSON string representing a group of entities as a t. This is provided as a convenience function, as this is the format that requests to https://wikidata.org/wiki/Special:EntityData/{ID}.json (where ID is a Wikidata Entity ID) are returned in.