NAME
Wikibase::Datatype::Struct::Sense - Wikibase sense structure serialization.
SYNOPSIS
use Wikibase::Datatype::Struct::Sense 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::Sense 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 sense to object.
Returns Wikibase::Datatype::Sense instance.
ERRORS
obj2struct():
Base URI is required.
Object doesn't exist.
Object isn't 'Wikibase::Datatype::Sense'.
EXAMPLE1
use strict;
use warnings;
use Data::Printer;
use Wikibase::Datatype::Sense;
use Wikibase::Datatype::Snak;
use Wikibase::Datatype::Statement;
use Wikibase::Datatype::Struct::Sense qw(obj2struct);
use Wikibase::Datatype::Value::Item;
use Wikibase::Datatype::Value::Monolingual;
# Statement.
my $statement = Wikibase::Datatype::Statement->new(
# instance of (P31) human (Q5)
'snak' => Wikibase::Datatype::Snak->new(
'datatype' => 'wikibase-item',
'datavalue' => Wikibase::Datatype::Value::Item->new(
'value' => 'Q5',
),
'property' => 'P31',
),
);
# Object.
my $obj = Wikibase::Datatype::Sense->new(
'glosses' => [
Wikibase::Datatype::Value::Monolingual->new(
'language' => 'en',
'value' => 'Glosse en',
),
Wikibase::Datatype::Value::Monolingual->new(
'language' => 'cs',
'value' => 'Glosse cs',
),
],
'id' => 'ID',
'statements' => [
$statement,
],
);
# Get structure.
my $struct_hr = obj2struct($obj, 'http://test.wikidata.org/entity/');
# Dump to output.
p $struct_hr;
# Output:
# \ {
# glosses {
# cs {
# language "cs",
# value "Glosse cs"
# },
# en {
# language "en",
# value "Glosse en"
# }
# },
# id "ID",
# claims {
# P31 [
# [0] {
# mainsnak {
# datatype "wikibase-item",
# datavalue {
# type "wikibase-entityid",
# value {
# entity-type "item",
# id "Q5",
# numeric-id 5
# }
# },
# property "P31",
# snaktype "value"
# },
# rank "normal",
# type "statement"
# }
# ]
# }
# }
EXAMPLE2
use strict;
use warnings;
use Data::Printer;
use Wikibase::Datatype::Struct::Sense qw(struct2obj);
# Item structure.
my $struct_hr = {
'glosses' => {
'cs' => {
'language' => 'cs',
'value' => 'Glosse cs',
},
'en' => {
'language' => 'en',
'value' => 'Glosse en',
},
},
'id' => 'ID',
'claims' => {
'P31' => [{
'mainsnak' => {
'datatype' => 'wikibase-item',
'datavalue' => {
'type' => 'wikibase-entityid',
'value' => {
'entity-type' => 'item',
'id' => 'Q5',
'numeric-id' => 5,
},
},
'property' => 'P31',
'snaktype' => 'value',
},
'rank' => 'normal',
'type' => 'statement',
}],
},
};
# Get object.
my $obj = struct2obj($struct_hr);
# Dump object.
p $obj;
# Output:
# Wikibase::Datatype::Sense {
# Parents Mo::Object
# public methods (7) : BUILD, can (UNIVERSAL), DOES (UNIVERSAL), check_array_object (Mo::utils), check_number_of_items (Mo::utils), isa (UNIVERSAL), VERSION (UNIVERSAL)
# private methods (1) : __ANON__ (Mo)
# internals: {
# glosses [
# [0] Wikibase::Datatype::Value::Monolingual,
# [1] Wikibase::Datatype::Value::Monolingual
# ],
# id "ID",
# statements [
# [0] Wikibase::Datatype::Statement
# ]
# }
# }
DEPENDENCIES
Error::Pure, Exporter, Readonly, Wikibase::Datatype::Sense, Wikibase::Datatype::Struct::Language, Wikibase::Datatype::Struct::Statement.
SEE ALSO
- Wikibase::Datatype::Struct
-
Wikibase structure serialization.
- Wikibase::Datatype::Sense
-
Wikibase sense datatype.
REPOSITORY
https://github.com/michal-josef-spacek/Wikibase-Datatype-Struct
AUTHOR
Michal Josef Špaček mailto:skim@cpan.org
LICENSE AND COPYRIGHT
© 2020-2024 Michal Josef Špaček
BSD 2-Clause License
VERSION
0.13