User:AnomieBOT/source/AnomieBOT/API/Iterator.pm: Difference between revisions

Content deleted Content added
AnomieBOT (talk | contribs)
Updating published sources: General: * D'oh, of course the node set can be empty in the iterator. * Minor doc fixes. WikiProjectTagger: * Completed.
AnomieBOT (talk | contribs)
Updating published sources: General: * Remove pointless copyright notices with dates I never remember to update. * Add LICENSE.txt file instead.
 
(11 intermediate revisions by the same user not shown)
Line 1:
{{ombox|text=See [[/doc]] for formatted documentation}}
<sourcesyntaxhighlight lang="perl">
package AnomieBOT::API::Iterator;
 
Line 17:
croak "Invalid query: values must be scalars or arrayrefs\n" unless ref($v) eq 'ARRAY';
croak "Invalid query: only one arrayref is allowed\n" if defined($ik);
($ik,$iv)=($k,$v);
$iv=[@$v]; # copy array ref so we don't shift the passed parameter
$query{$ik}=shift(@$iv);
}
Line 25 ⟶ 26:
iterkey => $ik,
iterval => $iv,
_ok_ cur_iterval => 0undef,
query => \%query,
cont => {},
Line 31 ⟶ 33:
bless $self, $class;
return $self;
}
 
sub iterval {
return $_[0]->{'cur_iterval'};
}
 
Line 48 ⟶ 54:
return undef unless exists($self->{'query'});
 
$self->{'cur_iterval'}=$self->{'query'}{$self->{'iterkey'}} if defined($self->{'iterkey'});
my $res=$api->query(%{$self->{'query'}}, %{$self->{'cont'}});
if($res->{'code'} ne 'success'){
Line 54 ⟶ 61:
}
 
my @%res=values exists($res->{'query'})?%{$res->{'query'}}:();
if(@delete $res < 1){'normalized'};
delete return $res{'redirects'};
delete _ok_ => 0,$res{'interwiki'};
my code @res=>values 'notiterable',%res;
error => 'The result set contained no nodes under the query node',
};
}
if(@res > 1){
return {
_ok_ => 0,
code => 'notiterable',
error => 'The result set contained too many nodes under the query node: '.join(', ', keys %{$res->{'query'}}),
};
}
my $ret=$res[0];
$ret=[ values %$ret ] if ref($ret) eq 'HASH';
if(ref($ret) ne 'ARRAY'){
return {
_ok_ => 0,
code => 'wtferror',
error => 'The result node list is not an array or hash reference. WTF?',
};
} elsif(@res > 0){
my $ret=$res[0];
$ret=[ values %$ret ] if ref($ret) eq 'HASH';
if(ref($ret) ne 'ARRAY'){
}; return {
_ok_ => 0,
code => 'wtferror',
error => 'The result setnode containedlist nois nodesnot underan thearray queryor nodehash reference. WTF?',
};
};
return $self->{'res'}=$ret;
}
$self->{'res'}=$ret;
 
if(exists($res->{'query-continue'})){
Line 135 ⟶ 140:
 
Note that no API query is done on creation, and thus no result object is
current. C<< $iter->next >> must be called at least once before this function is
useful.
 
Line 145 ⟶ 150:
The return object is normally a hashref representing one page object, with an
additional property C<_ok_> set to a true value. If C<_ok_> is false, the
returned hashref is instead the error object as returned by C<< $api->query() >>.
C<< $api->query() >>. Calling C<< $iter->next >> again after an error will
retry the API query, which may or may not succeed.
may not succeed.
 
When no more results are available, undef is returned.
 
=back
 
=head1 COPYRIGHT
 
Copyright 2008 Anomie
 
This library is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.
 
=cut
 
</syntaxhighlight>
</source>