| 1 | package Parse::MediaWikiDump::CategoryLinks; | ||
| 2 | |||
| 3 | our $VERSION = '1.0.3'; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | sub new { | ||
| 9 | my ($class, $source) = @_; | ||
| 10 | my $self = {}; | ||
| 11 | |||
| 12 | $$self{BUFFER} = []; | ||
| 13 | $$self{BYTE} = 0; | ||
| 14 | |||
| 15 | bless($self, $class); | ||
| 16 | |||
| 17 | $self->open($source); | ||
| 18 | $self->init; | ||
| 19 | |||
| 20 | return $self; | ||
| 21 | } | ||
| 22 | |||
| 23 | sub next { | ||
| 24 | my ($self) = @_; | ||
| 25 | my $buffer = $$self{BUFFER}; | ||
| 26 | my $link; | ||
| 27 | |||
| 28 | while(1) { | ||
| 29 | if (defined($link = pop(@$buffer))) { | ||
| 30 | last; | ||
| 31 | } | ||
| 32 | |||
| 33 | #signals end of input | ||
| 34 | return undef unless $self->parse_more; | ||
| 35 | } | ||
| 36 | |||
| 37 | return Parse::MediaWikiDump::category_link->new($link); | ||
| 38 | } | ||
| 39 | |||
| 40 | #private functions with OO interface | ||
| 41 | sub parse_more { | ||
| 42 | my ($self) = @_; | ||
| 43 | my $source = $$self{SOURCE}; | ||
| 44 | my $need_data = 1; | ||
| 45 | |||
| 46 | while($need_data) { | ||
| 47 | my $line = <$source>; | ||
| 48 | |||
| 49 | last unless defined($line); | ||
| 50 | |||
| 51 | $$self{BYTE} += length($line); | ||
| 52 | |||
| 53 | while($line =~ m/\((\d+),'(.*?)','(.*?)',(\d+)\)[;,]/g) { | ||
| 54 | push(@{$$self{BUFFER}}, [$1, $2, $3, $4]); | ||
| 55 | $need_data = 0; | ||
| 56 | } | ||
| 57 | } | ||
| 58 | |||
| 59 | #if we still need data and we are here it means we ran out of input | ||
| 60 | if ($need_data) { | ||
| 61 | return 0; | ||
| 62 | } | ||
| 63 | |||
| 64 | return 1; | ||
| 65 | } | ||
| 66 | |||
| 67 | sub open { | ||
| 68 | my ($self, $source) = @_; | ||
| 69 | |||
| 70 | if (ref($source) ne 'GLOB') { | ||
| 71 | die "could not open $source: $!" unless | ||
| 72 | open($$self{SOURCE}, $source); | ||
| 73 | |||
| 74 | $$self{SOURCE_FILE} = $source; | ||
| 75 | } else { | ||
| 76 | $$self{SOURCE} = $source; | ||
| 77 | } | ||
| 78 | |||
| 79 | binmode($$self{SOURCE}, ':utf8'); | ||
| 80 | |||
| 81 | return 1; | ||
| 82 | } | ||
| 83 | |||
| 84 | sub init { | ||
| 85 | my ($self) = @_; | ||
| 86 | my $source = $$self{SOURCE}; | ||
| 87 | my $found = 0; | ||
| 88 | |||
| 89 | while(<$source>) { | ||
| 90 | if (m/^LOCK TABLES `categorylinks` WRITE;/) { | ||
| 91 | $found = 1; | ||
| 92 | last; | ||
| 93 | } | ||
| 94 | } | ||
| 95 | |||
| 96 | die "not a MediaWiki link dump file" unless $found; | ||
| 97 | } | ||
| 98 | |||
| 99 | sub current_byte { | ||
| 100 | my ($self) = @_; | ||
| 101 | |||
| 102 | return $$self{BYTE}; | ||
| 103 | } | ||
| 104 | |||
| 105 | sub size { | ||
| 106 | my ($self) = @_; | ||
| 107 | |||
| 108 | return undef unless defined $$self{SOURCE_FILE}; | ||
| 109 | |||
| 110 | my @stat = stat($$self{SOURCE_FILE}); | ||
| 111 | |||
| 112 | return $stat[7]; | ||
| 113 | } | ||
| 114 | |||
| 115 | 1; |