NAME

Wikibase::Datatype::Struct::Property - Wikibase property structure serialization.

SYNOPSIS

use Wikibase::Datatype::Struct::Property qw(obj2struct struct2obj);

my $struct_hr = obj2struct($obj, $base_uri);
my $obj = struct2obj($struct_hr);

DESCRIPTION

This conversion is between objects defined in Wikibase::Datatype and structures serialized via JSON to MediaWiki.

SUBROUTINES

obj2struct

my $struct_hr = obj2struct($obj, $base_uri);

Convert Wikibase::Datatype::Property instance to structure. $base_uri is base URI of Wikibase system (e.g. http://test.wikidata.org/entity/).

Returns reference to hash with structure.

struct2obj

my $obj = struct2obj($struct_hr);

Convert structure of property to object.

Returns Wikibase::Datatype::Property instance.

ERRORS

obj2struct():
        Base URI is required.
        Object doesn't exist.
        Object isn't 'Wikibase::Datatype::Property'.

struct2obj():
        Structure isn't for 'property' type.

EXAMPLE1

use strict;
use warnings;

use Data::Printer;
use Unicode::UTF8 qw(decode_utf8);
use Wikibase::Datatype::Property;
use Wikibase::Datatype::Reference;
use Wikibase::Datatype::Sitelink;
use Wikibase::Datatype::Snak;
use Wikibase::Datatype::Statement;
use Wikibase::Datatype::Struct::Property qw(obj2struct);
use Wikibase::Datatype::Value::Item;
use Wikibase::Datatype::Value::Monolingual;
use Wikibase::Datatype::Value::String;
use Wikibase::Datatype::Value::Time;

# Statement.
my $statement1 = Wikibase::Datatype::Statement->new(
        # instance of (P31) Wikidata property (Q18616576)
        'snak' => Wikibase::Datatype::Snak->new(
                'datatype' => 'wikibase-item',
                'datavalue' => Wikibase::Datatype::Value::Item->new(
                        'value' => 'Q18616576',
                ),
                'property' => 'P31',
        ),
);

# Main property.
my $obj = Wikibase::Datatype::Property->new(
        'aliases' => [
                Wikibase::Datatype::Value::Monolingual->new(
                        'language' => 'cs',
                        'value' => 'je',
                ),
                Wikibase::Datatype::Value::Monolingual->new(
                        'language' => 'en',
                        'value' => 'is a',
                ),
                Wikibase::Datatype::Value::Monolingual->new(
                        'language' => 'en',
                        'value' => 'is an',
                ),
        ],
        'datatype' => 'wikibase-item',
        'descriptions' => [
                Wikibase::Datatype::Value::Monolingual->new(
                        'language' => 'cs',
                        'value' => decode_utf8('tato položka je jedna konkrétní věc (exemplář, '.
                                'příklad) patřící do této třídy, kategorie nebo skupiny předmětů'),
                ),
                Wikibase::Datatype::Value::Monolingual->new(
                        'language' => 'en',
                        'value' => 'that class of which this subject is a particular example and member',
                ),
        ],
        'id' => 'P31',
        'labels' => [
                Wikibase::Datatype::Value::Monolingual->new(
                        'language' => 'cs',
                        'value' => decode_utf8('instance (čeho)'),
                ),
                Wikibase::Datatype::Value::Monolingual->new(
                        'language' => 'en',
                        'value' => 'instance of',
                ),
        ],
        'page_id' => 3918489,
        'statements' => [
                $statement1,
        ],
        'title' => 'Property:P31',
);

# Get structure.
my $struct_hr = obj2struct($obj, 'http://test.wikidata.org/entity/');

# Dump to output.
p $struct_hr;

# Output:
# {
#     aliases        {
#         cs   [
#             [0] {
#                     language   "cs",
#                     value      "je"
#                 }
#         ],
#         en   [
#             [0] {
#                     language   "en",
#                     value      "is a"
#                 },
#             [1] {
#                     language   "en",
#                     value      "is an"
#                 }
#         ]
#     },
#     claims         {
#         P31   [
#             [0] {
#                     mainsnak   {
#                         datatype    "wikibase-item",
#                         datavalue   {
#                             type    "wikibase-entityid",
#                             value   {
#                                 entity-type   "item",
#                                 id            "Q18616576",
#                                 numeric-id    18616576
#                             }
#                         },
#                         property    "P31",
#                         snaktype    "value"
#                     },
#                     rank       "normal",
#                     type       "statement"
#                 }
#         ]
#     },
#     datatype       "wikibase-item",
#     descriptions   {
#         cs   {
#             language   "cs",
#             value      "tato položka je jedna konkrétní věc (exemplář, příklad) patřící do této třídy, kategorie nebo skupiny předmětů"
#         },
#         en   {
#             language   "en",
#             value      "that class of which this subject is a particular example and member"
#         }
#     },
#     id             "P31",
#     labels         {
#         cs   {
#             language   "cs",
#             value      "instance (čeho)"
#         },
#         en   {
#             language   "en",
#             value      "instance of"
#         }
#     },
#     ns             120,
#     pageid         3918489,
#     title          "Property:P31",
#     type           "property"
# }

EXAMPLE2

use strict;
use warnings;

use Data::Printer;
use Unicode::UTF8 qw(decode_utf8);
use Wikibase::Datatype::Struct::Property qw(struct2obj);

# Item structure.
my $struct_hr = {
        'aliases' => {
                'cs' => [{
                        'language' => 'cs',
                        'value' => 'je',
                }],
                'en' => [{
                        'language' => 'en',
                        'value' => 'is a',
                }, {
                        'language' => 'en',
                        'value' => 'is an',
                }],
        },
        'claims' => {
                'P31' => [{
                        'mainsnak' => {
                                'datatype' => 'wikibase-item',
                                'datavalue' => {
                                        'type' => 'wikibase-entityid',
                                        'value' => {
                                                'entity-type' => 'item',
                                                'id' => 'Q18616576',
                                                'numeric-id' => 18616576,
                                        },
                                },
                                'property' => 'P31',
                                'snaktype' => 'value',
                        },
                        'rank' => 'normal',
                        'type' => 'statement',
                }],
        },
        'datatype' => 'wikibase-item',
        'descriptions' => {
                'cs' => {
                        'language' => 'cs',
                        'value' => decode_utf8('tato položka je jedna konkrétní věc (exemplář, příklad) patřící do této třídy, kategorie nebo skupiny předmětů'),
                },
                'en' => {
                        'language' => 'en',
                        'value' => 'that class of which this subject is a particular example and member',
                },
        },
        'id' => 'P31',
        'labels' => {
                'cs' => {
                        'language' => 'cs',
                        'value' => decode_utf8('instance (čeho)'),
                },
                'en' => {
                        'language' => 'en',
                        'value' => 'instance of',
                },
        },
        'ns' => 120,
        'pageid' => 3918489,
        'title' => 'Property:P31',
        'type' => 'property',
};

# Get object.
my $obj = struct2obj($struct_hr);

# Print out.
p $obj;

# Output:
# Wikibase::Datatype::Property  {
#     parents: Mo::Object
#     public methods (8):
#         BUILD
#         Error::Pure:
#             err
#         List::Util:
#             none
#         Mo::utils:
#             check_array_object, check_number, check_number_of_items, check_required
#         Readonly:
#             Readonly
#     private methods (0)
#     internals: {
#         aliases        [
#             [0] Wikibase::Datatype::Value::Monolingual,
#             [1] Wikibase::Datatype::Value::Monolingual,
#             [2] Wikibase::Datatype::Value::Monolingual
#         ],
#         datatype       "wikibase-item",
#         descriptions   [
#             [0] Wikibase::Datatype::Value::Monolingual,
#             [1] Wikibase::Datatype::Value::Monolingual
#         ],
#         id             "P31",
#         labels         [
#             [0] Wikibase::Datatype::Value::Monolingual,
#             [1] Wikibase::Datatype::Value::Monolingual
#         ],
#         ns             120,
#         page_id        3918489,
#         statements     [
#             [0] Wikibase::Datatype::Statement
#         ],
#         title          "Property:P31"
#     }
# }

DEPENDENCIES

Error::Pure, Exporter, Readonly, Wikibase::Datatype::Item, Wikibase::Datatype::Reference, Wikibase::Datatype::Struct::Language, Wikibase::Datatype::Struct::Statement, Wikibase::Datatype::Struct::Value::Monolingual.

SEE ALSO

Wikibase::Datatype::Struct

Wikibase structure serialization.

Wikibase::Datatype::Item

Wikibase item datatype.

REPOSITORY

https://github.com/michal-josef-spacek/Wikibase-Datatype-Struct

AUTHOR

Michal Josef Špaček mailto:skim@cpan.org

http://skim.cz

LICENSE AND COPYRIGHT

© 2020-2023 Michal Josef Špaček

BSD 2-Clause License

VERSION

0.12