NAME

Wikibase::Datatype::Struct::Mediainfo - Wikibase mediainfo structure serialization.

SYNOPSIS

use Wikibase::Datatype::Struct::Mediainfo 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::Mediainfo 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 mediainfo to object.

Returns Wikibase::Datatype::Mediainfo instance.

ERRORS

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

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

EXAMPLE1

use strict;
use warnings;

use Data::Printer;
use Wikibase::Datatype::Mediainfo;
use Wikibase::Datatype::MediainfoSnak;
use Wikibase::Datatype::MediainfoStatement;
use Wikibase::Datatype::Struct::Mediainfo qw(obj2struct);
use Wikibase::Datatype::Value::Item;
use Wikibase::Datatype::Value::Monolingual;

# Object.
my $statement1 = Wikibase::Datatype::MediainfoStatement->new(
        # instance of (P31) human (Q5)
        'snak' => Wikibase::Datatype::MediainfoSnak->new(
                'datavalue' => Wikibase::Datatype::Value::Item->new(
                        'value' => 'Q5',
                ),
                'property' => 'P31',
        ),
        'property_snaks' => [
                # of (P642) alien (Q474741)
                Wikibase::Datatype::MediainfoSnak->new(
                        'datavalue' => Wikibase::Datatype::Value::Item->new(
                                'value' => 'Q474741',
                        ),
                        'property' => 'P642',
                ),
        ],
);
my $statement2 = Wikibase::Datatype::MediainfoStatement->new(
        # sex or gender (P21) male (Q6581097)
        'snak' => Wikibase::Datatype::MediainfoSnak->new(
                'datavalue' => Wikibase::Datatype::Value::Item->new(
                        'value' => 'Q6581097',
                ),
                'property' => 'P21',
        ),
);

# Main item.
my $obj = Wikibase::Datatype::Mediainfo->new(
        'id' => 'Q42',
        'labels' => [
                Wikibase::Datatype::Value::Monolingual->new(
                        'language' => 'cs',
                        'value' => 'Douglas Adams',
                ),
                Wikibase::Datatype::Value::Monolingual->new(
                        'language' => 'en',
                        'value' => 'Douglas Adams',
                ),
        ],
        'page_id' => 123,
        'statements' => [
                $statement1,
                $statement2,
        ],
        'title' => 'Q42',
);

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

# Dump to output.
p $struct_hr;

# Output:
# {
#     descriptions   {},
#     id             "Q42",
#     labels         {
#         cs   {
#             language   "cs",
#             value      "Douglas Adams"
#         },
#         en   {
#             language   "en",
#             value      "Douglas Adams"
#         }
#     },
#     ns             6,
#     pageid         123,
#     statements     {
#         P21   [
#             [0] {
#                     mainsnak   {
#                         datavalue   {
#                             type    "wikibase-entityid",
#                             value   {
#                                 entity-type   "item",
#                                 id            "Q6581097",
#                                 numeric-id    6581097
#                             }
#                         },
#                         property    "P21",
#                         snaktype    "value"
#                     },
#                     rank       "normal",
#                     type       "statement"
#                 }
#         ],
#         P31   [
#             [0] {
#                     mainsnak           {
#                         datavalue   {
#                             type    "wikibase-entityid",
#                             value   {
#                                 entity-type   "item",
#                                 id            "Q5",
#                                 numeric-id    5
#                             }
#                         },
#                         property    "P31",
#                         snaktype    "value"
#                     },
#                     qualifiers         {
#                         P642   [
#                             [0] {
#                                     datavalue   {
#                                         type    "wikibase-entityid",
#                                         value   {
#                                             entity-type   "item",
#                                             id            "Q474741",
#                                             numeric-id    474741
#                                         }
#                                     },
#                                     property    "P642",
#                                     snaktype    "value"
#                                 }
#                         ]
#                     },
#                     qualifiers-order   [
#                         [0] "P642"
#                     ],
#                     rank               "normal",
#                     type               "statement"
#                 }
#         ]
#     },
#     title          "Q42",
#     type           "mediainfo"
# }

EXAMPLE2

use strict;
use warnings;

use Data::Printer;
use Wikibase::Datatype::Struct::Mediainfo qw(struct2obj);

# Item structure.
my $struct_hr = {
        'descriptions' => {},
        'id' => 'Q42',
        'labels' => {
                'cs' => {
                        'language' => 'cs',
                        'value' => 'Douglas Adams',
                },
                'en' => {
                        'language' => 'en',
                        'value' => 'Douglas Adams',
                },
        },
        'ns' => 6,
        'pageid' => 123,
        'statements' => {
                'P21' => [{
                        'mainsnak' => {
                                'datavalue' => {
                                        'type' => 'wikibase-entityid',
                                        'value' => {
                                                'entity-type' => 'item',
                                                'id' => 'Q6581097',
                                                'numeric-id' => 6581097,
                                        },
                                },
                                'property' => 'P21',
                                'snaktype' => 'value',
                        },
                        'rank' => 'normal',
                        'type' => 'statement',
                }],
                'P31' => [{
                        'mainsnak' => {
                                'datavalue' => {
                                        'type' => 'wikibase-entityid',
                                        'value' => {
                                                'entity-type' => 'item',
                                                'id' => 'Q5',
                                                'numeric-id' => 5,
                                        },
                                },
                                'property' => 'P31',
                                'snaktype' => 'value',
                        },
                        'qualifiers' => {
                                'P642' => [{
                                        'datavalue' => {
                                                'type' => 'wikibase-entityid',
                                                'value' => {
                                                        'entity-type' => 'item',
                                                        'id' => 'Q474741',
                                                        'numeric-id' => 474741,
                                                }
                                        },
                                        'property' => 'P642',
                                        'snaktype' => 'value',
                                }],
                        },
                        'qualifiers-order' => [
                                'P642',
                        ],
                        'rank' => 'normal',
                        'type' => 'statement',
                }],
        },
        'title' => 'Q42',
        'type' => 'mediainfo',
};

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

# Print out.
p $obj;

# Output:
# Wikibase::Datatype::Mediainfo  {
#     parents: Mo::Object
#     public methods (5):
#         BUILD
#         Error::Pure:
#             err
#         Mo::utils:
#             check_array_object, check_number, check_number_of_items
#     private methods (0)
#     internals: {
#         descriptions   [],
#         id             "Q42",
#         labels         [
#             [0] Wikibase::Datatype::Value::Monolingual,
#             [1] Wikibase::Datatype::Value::Monolingual
#         ],
#         ns             6,
#         page_id        123,
#         statements     [
#             [0] Wikibase::Datatype::MediainfoStatement,
#             [1] Wikibase::Datatype::MediainfoStatement
#         ],
#         title          "Q42"
#     }
# }

DEPENDENCIES

Error::Pure, Exporter, Readonly, Wikibase::Datatype::Mediainfo, Wikibase::Datatype::Struct::Language, Wikibase::Datatype::Struct::MediainfoStatement.

SEE ALSO

Wikibase::Datatype::Struct

Wikibase structure serialization.

Wikibase::Datatype::Mediainfo

Wikibase mediainfo 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