| 1 | package Parse::MediaWikiDump::Links; | ||
| 2 | |||
| 3 | #this needs to be fully replaced by MediaWiki::DumpFile::Compat | ||
| 4 | #because it uses a much more correct SQL parser | ||
| 5 | |||
| 6 | our $VERSION = '1.0.6'; | ||
| 7 | |||
| 8 | use strict; | ||
| 9 | use warnings; | ||
| 10 | |||
| 11 | sub new { | ||
| 12 | my ($class, $source) = @_; | ||
| 13 | my $self = {}; | ||
| 14 | $$self{BUFFER} = []; | ||
| 15 | |||
| 16 | bless($self, $class); | ||
| 17 | |||
| 18 | $self->open($source); | ||
| 19 | #fix for bug 58196 | ||
| 20 | #$self->init; | ||
| 21 | |||
| 22 | return $self; | ||
| 23 | } | ||
| 24 | |||
| 25 | sub next { | ||
| 26 | my ($self) = @_; | ||
| 27 | my $buffer = $$self{BUFFER}; | ||
| 28 | my $link; | ||
| 29 | |||
| 30 | while(1) { | ||
| 31 | if (defined($link = pop(@$buffer))) { | ||
| 32 | last; | ||
| 33 | } | ||
| 34 | |||
| 35 | #signals end of input | ||
| 36 | return undef unless $self->parse_more; | ||
| 37 | } | ||
| 38 | |||
| 39 | return Parse::MediaWikiDump::link->new($link); | ||
| 40 | } | ||
| 41 | |||
| 42 | #private functions with OO interface | ||
| 43 | sub parse_more { | ||
| 44 | my ($self) = @_; | ||
| 45 | my $source = $$self{SOURCE}; | ||
| 46 | my $need_data = 1; | ||
| 47 | |||
| 48 | while($need_data) { | ||
| 49 | my $line = <$source>; | ||
| 50 | |||
| 51 | last unless defined($line); | ||
| 52 | |||
| 53 | while($line =~ m/\((\d+),(-?\d+),'(.*?)'\)[;,]/g) { | ||
| 54 | push(@{$$self{BUFFER}}, [$1, $2, $3]); | ||
| 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 | } else { | ||
| 74 | $$self{SOURCE} = $source; | ||
| 75 | } | ||
| 76 | |||
| 77 | binmode($$self{SOURCE}, ':utf8'); | ||
| 78 | |||
| 79 | return 1; | ||
| 80 | } | ||
| 81 | |||
| 82 | sub init { | ||
| 83 | my ($self) = @_; | ||
| 84 | my $source = $$self{SOURCE}; | ||
| 85 | my $found = 0; | ||
| 86 | |||
| 87 | while(<$source>) { | ||
| 88 | if (m/^LOCK TABLES `pagelinks` WRITE;/) { | ||
| 89 | $found = 1; | ||
| 90 | last; | ||
| 91 | } | ||
| 92 | } | ||
| 93 | |||
| 94 | die "not a MediaWiki link dump file" unless $found; | ||
| 95 | } | ||
| 96 | |||
| 97 | #depreciated backwards compatibility methods | ||
| 98 | |||
| 99 | #replaced by next() | ||
| 100 | sub link { | ||
| 101 | my ($self) = @_; | ||
| 102 | $self->next(@_); | ||
| 103 | } | ||
| 104 | |||
| 105 | |||
| 106 | 1; | ||
| 107 | __END__ |