| 1 | package Wikibase::Datatype::Struct::Item; | ||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||
| 3 | 8 | ▶ | use base qw(Exporter); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 4 | 8 | ▶ | use strict; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 5 | 8 | ▶ | use warnings; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||
| 7 | 8 | ▶ | use Error::Pure qw(err); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 8 | 8 | ▶ | use Readonly; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 9 | 8 | ▶ | use Wikibase::Datatype::Item 0.39; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 10 | 8 | ▶ | use Wikibase::Datatype::Struct::Language; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 11 | 8 | ▶ | use Wikibase::Datatype::Struct::Sitelink; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 12 | 8 | ▶ | use Wikibase::Datatype::Struct::Statement; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 13 | 8 | ▶ | use Wikibase::Datatype::Struct::Term; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||
| 15 | 8 | Readonly::Array our @EXPORT_OK => qw(obj2struct struct2obj); | |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||
| 17 | 8 | our $VERSION = 0.15; | |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||
| 19 | 8 | ▶ | sub obj2struct { | ||||||||||||||||||||||||||||
|
Subroutine
obj2struct: called CC=17 SCAR=28.3
Pod
documented
| |||||||||||||||||||||||||||||||
| 20 | 8 | my ($obj, $base_uri) = @_; | |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||
| 22 | 8 | ▶ | if (! defined $obj) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 23 | 1 | err "Object doesn't exist."; | |||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||||
| 25 | 7 | ▶ | if (! $obj->isa('Wikibase::Datatype::Item')) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 26 | 1 | err "Object isn't 'Wikibase::Datatype::Item'."; | |||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||||
| 28 | 6 | ▶ | if (! defined $base_uri) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 29 | 1 | err 'Base URI is required.'; | |||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||
| 32 | 5 | my $struct_hr = { | |||||||||||||||||||||||||||||
| 33 | 'type' => 'item', | ||||||||||||||||||||||||||||||
| 34 | }; | ||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||
| 36 | # Aliases. | ||||||||||||||||||||||||||||||
| 37 | 5 | foreach my $alias (@{$obj->aliases}) { | |||||||||||||||||||||||||||||
| 38 | 4 | ▶ | if (! exists $struct_hr->{'aliases'}->{$alias->language}) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 39 | 2 | $struct_hr->{'aliases'}->{$alias->language} = []; | |||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||
| 41 | 4 | push @{$struct_hr->{'aliases'}->{$alias->language}}, | |||||||||||||||||||||||||||||
| 42 | Wikibase::Datatype::Struct::Term::obj2struct($alias); | ||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||
| 45 | # Claims. | ||||||||||||||||||||||||||||||
| 46 | 5 | foreach my $statement (@{$obj->statements}) { | |||||||||||||||||||||||||||||
| 47 | 4 | ▶ | $struct_hr->{'claims'}->{$statement->snak->property} //= []; | ||||||||||||||||||||||||||||
|
Condition100%2/2
$struct_hr->{"claims"}{$statement->snak->property} //= []
MC/DC100%1/1
$struct_hr->{"claims"}{$statement->snak->property} //= []
$struct_hr->{"claims"}{$statement->snak->property}
Truth table100%2/2
A
A= $struct_hr->{"claims"}{$statement->snak->property}
| |||||||||||||||||||||||||||||||
| 48 | 4 | push @{$struct_hr->{'claims'}->{$statement->snak->property}}, | |||||||||||||||||||||||||||||
| 49 | Wikibase::Datatype::Struct::Statement::obj2struct($statement, $base_uri); | ||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||
| 52 | # Descriptions. | ||||||||||||||||||||||||||||||
| 53 | 5 | foreach my $desc (@{$obj->descriptions}) { | |||||||||||||||||||||||||||||
| 54 | 2 | $struct_hr->{'descriptions'}->{$desc->language} | |||||||||||||||||||||||||||||
| 55 | = Wikibase::Datatype::Struct::Term::obj2struct($desc); | ||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||
| 58 | # Id. | ||||||||||||||||||||||||||||||
| 59 | 5 | ▶ | if (defined $obj->id) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 60 | 1 | $struct_hr->{'id'} = $obj->id; | |||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||
| 63 | # Labels. | ||||||||||||||||||||||||||||||
| 64 | 5 | foreach my $label (@{$obj->labels}) { | |||||||||||||||||||||||||||||
| 65 | 2 | $struct_hr->{'labels'}->{$label->language} | |||||||||||||||||||||||||||||
| 66 | = Wikibase::Datatype::Struct::Term::obj2struct($label); | ||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||
| 69 | # Last revision id. | ||||||||||||||||||||||||||||||
| 70 | 5 | ▶ | if (defined $obj->lastrevid) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 71 | 1 | $struct_hr->{'lastrevid'} = $obj->lastrevid; | |||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||
| 74 | # Modified date. | ||||||||||||||||||||||||||||||
| 75 | 5 | ▶ | if (defined $obj->modified) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 76 | 1 | $struct_hr->{'modified'} = $obj->modified; | |||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||
| 79 | # Namespace. | ||||||||||||||||||||||||||||||
| 80 | 5 | ▶ | if (defined $obj->ns) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 81 | 4 | $struct_hr->{'ns'} = $obj->ns; | |||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||
| 84 | # Page ID. | ||||||||||||||||||||||||||||||
| 85 | 5 | ▶ | if (defined $obj->page_id) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 86 | 1 | $struct_hr->{'pageid'} = $obj->page_id; | |||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||
| 89 | # Sitelinks. | ||||||||||||||||||||||||||||||
| 90 | 5 | foreach my $sitelink (@{$obj->sitelinks}) { | |||||||||||||||||||||||||||||
| 91 | 2 | $struct_hr->{'sitelinks'}->{$sitelink->site} | |||||||||||||||||||||||||||||
| 92 | = Wikibase::Datatype::Struct::Sitelink::obj2struct($sitelink); | ||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||
| 95 | # Title. | ||||||||||||||||||||||||||||||
| 96 | 5 | ▶ | if (defined $obj->title) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 97 | 1 | $struct_hr->{'title'} = $obj->title; | |||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||
| 100 | 5 | return $struct_hr; | |||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||
| 103 | 6 | ▶ | sub struct2obj { | ||||||||||||||||||||||||||||
|
Subroutine
struct2obj: called CC=16 SCAR=27.7
Pod
documented
| |||||||||||||||||||||||||||||||
| 104 | 6 | my $struct_hr = shift; | |||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||
| 106 | 6 | ▶ | if (! exists $struct_hr->{'type'} || $struct_hr->{'type'} ne 'item') { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not exists $struct_hr->{"type"} or $struct_hr->{"type"} ne "item"
MC/DC100%2/2
not exists $struct_hr->{"type"} or $struct_hr->{"type"} ne "item"
not exists $struct_hr->{"type"}
$struct_hr->{"type"} ne "item"
Truth table100%3/3
not(A) or B
A= not exists $struct_hr->{"type"}B= $struct_hr->{"type"} ne "item"
| |||||||||||||||||||||||||||||||
| 107 | 2 | err "Structure isn't for 'item' type."; | |||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||
| 110 | # Aliases. | ||||||||||||||||||||||||||||||
| 111 | 4 | my $aliases_ar = []; | |||||||||||||||||||||||||||||
| 112 | 4 | foreach my $lang (keys %{$struct_hr->{'aliases'}}) { | |||||||||||||||||||||||||||||
| 113 | 2 | foreach my $alias_hr (@{$struct_hr->{'aliases'}->{$lang}}) { | |||||||||||||||||||||||||||||
| 114 | 4 | push @{$aliases_ar}, Wikibase::Datatype::Struct::Term::struct2obj( | |||||||||||||||||||||||||||||
| 115 | $alias_hr, | ||||||||||||||||||||||||||||||
| 116 | ); | ||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||
| 120 | # Descriptions. | ||||||||||||||||||||||||||||||
| 121 | 4 | my $descriptions_ar = []; | |||||||||||||||||||||||||||||
| 122 | 4 | foreach my $lang (keys %{$struct_hr->{'descriptions'}}) { | |||||||||||||||||||||||||||||
| 123 | 2 | push @{$descriptions_ar}, Wikibase::Datatype::Struct::Term::struct2obj( | |||||||||||||||||||||||||||||
| 124 | 2 | $struct_hr->{'descriptions'}->{$lang}, | |||||||||||||||||||||||||||||
| 125 | ); | ||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||
| 128 | # Labels. | ||||||||||||||||||||||||||||||
| 129 | 4 | my $labels_ar = []; | |||||||||||||||||||||||||||||
| 130 | 4 | foreach my $lang (keys %{$struct_hr->{'labels'}}) { | |||||||||||||||||||||||||||||
| 131 | 2 | push @{$labels_ar}, Wikibase::Datatype::Struct::Term::struct2obj( | |||||||||||||||||||||||||||||
| 132 | 2 | $struct_hr->{'labels'}->{$lang}, | |||||||||||||||||||||||||||||
| 133 | ); | ||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||
| 136 | # Sitelinks. | ||||||||||||||||||||||||||||||
| 137 | 4 | my $sitelinks_ar = []; | |||||||||||||||||||||||||||||
| 138 | 4 | foreach my $site (keys %{$struct_hr->{'sitelinks'}}) { | |||||||||||||||||||||||||||||
| 139 | 2 | push @{$sitelinks_ar}, Wikibase::Datatype::Struct::Sitelink::struct2obj( | |||||||||||||||||||||||||||||
| 140 | 2 | $struct_hr->{'sitelinks'}->{$site}, | |||||||||||||||||||||||||||||
| 141 | ); | ||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||
| 144 | # Statements. | ||||||||||||||||||||||||||||||
| 145 | 4 | my $statements_ar = []; | |||||||||||||||||||||||||||||
| 146 | 4 | foreach my $property (keys %{$struct_hr->{'claims'}}) { | |||||||||||||||||||||||||||||
| 147 | 2 | foreach my $claim_hr (@{$struct_hr->{'claims'}->{$property}}) { | |||||||||||||||||||||||||||||
| 148 | 2 | push @{$statements_ar}, Wikibase::Datatype::Struct::Statement::struct2obj( | |||||||||||||||||||||||||||||
| 149 | $claim_hr, | ||||||||||||||||||||||||||||||
| 150 | ); | ||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||
| 154 | my $obj = Wikibase::Datatype::Item->new( | ||||||||||||||||||||||||||||||
| 155 | 'aliases' => $aliases_ar, | ||||||||||||||||||||||||||||||
| 156 | 'descriptions' => $descriptions_ar, | ||||||||||||||||||||||||||||||
| 157 | defined $struct_hr->{'id'} ? ('id' => $struct_hr->{'id'}) : (), | ||||||||||||||||||||||||||||||
| 158 | 'labels' => $labels_ar, | ||||||||||||||||||||||||||||||
| 159 | defined $struct_hr->{'lastrevid'} ? ('lastrevid' => $struct_hr->{'lastrevid'}) : (), | ||||||||||||||||||||||||||||||
| 160 | defined $struct_hr->{'modified'} ? ('modified' => $struct_hr->{'modified'}) : (), | ||||||||||||||||||||||||||||||
| 161 | defined $struct_hr->{'ns'} ? ('ns' => $struct_hr->{'ns'}) : (), | ||||||||||||||||||||||||||||||
| 162 | defined $struct_hr->{'pageid'} ? ('page_id' => $struct_hr->{'pageid'}) : (), | ||||||||||||||||||||||||||||||
| 163 | 'sitelinks' => $sitelinks_ar, | ||||||||||||||||||||||||||||||
| 164 | 'statements' => $statements_ar, | ||||||||||||||||||||||||||||||
| 165 | 4 | ▶ | defined $struct_hr->{'title'} ? ('title' => $struct_hr->{'title'}) : (), | ||||||||||||||||||||||||||||
|
Branch: 6 branches
| |||||||||||||||||||||||||||||||
| 166 | ); | ||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||
| 168 | 4 | return $obj; | |||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||
| 171 | 8 | 1; | |||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||
| 173 | __END__ | ||||||||||||||||||||||||||||||