User:Firefly/code

Main 'master control' program:

RenameChecker:

LogMonitor package HBCPlugins::LogMonitor; use    strict; use   XML::Simple; use    Data::Dumper; use    URI::Escape; use   LWP::UserAgent; our   $self; sub new { shift; $self = shift; bless($self); $self->{params}{jobs} = {}; $self->{UA} = my $ua = LWP::UserAgent->new('agent' => 'LogMonitor .0001b'); warn "LogMonitor active.\n"; return $self; } sub add_job { my $self = shift; my %params = @_; my $type = $params{type}; $self->{params}{jobs}{$type} = \%params; $self->{params}{jobs}{$type}{offset} = ((lc($params{start_point}) eq 'all') ? (0) : ($params{start_point})); $self->{params}{jobs}{$type}{log} = []; $self->{params}{jobs}{$type}{step_size} ||= 250; $self->{params}{jobs}{$type}{current} = 0; warn "Set initial offset for $type to ".$self->{params}{jobs}{$type}{offset}."\n\n"; &{$self->{shared}{add_job}}([\&handle_jobs,$type],0); } sub update_now { my $self = shift; my $type = shift; warn "Forcing manual update for '$type' log.\n"; &{$self->{shared}{add_job}}([\&handle_jobs,$type],0); } sub handle_jobs { my $type = shift; my $url_template = 'http://en.wikipedia.org/w/api.php?action=query&format=xml&list=logevents&letype=&lelimit=& ledir=newer'; my $url = $url_template; $self->{params}{jobs}{$type}{offset} ||= 0; warn "Reading up to ".$self->{params}{jobs}{$type}{step_size}." log entries from $type starting at: ".$self->{params}{jobs}{$type} {offset}."\n"; $url =~ s||$type|; $url =~ s||$self->{params}{jobs}{$type}{step_size}|; my $offset_line = ('&lestart='.$self->{params}{jobs}{$type}{offset}); if ($self->{params}{jobs}{$type}{offset}){$url =~ s||$offset_line|} else {$url =~ s|||} my $rh_xml = XMLin($self->{UA}->get($url)->content); my $ra_renames = ${$rh_xml}{query}{logevents}{item}; ($ra_renames = [$ra_renames]) if (ref($ra_renames) eq 'HASH'); shift(@{$ra_renames}) if ($self->{params}{jobs}{$type}{offset} > 0); push(@{$self->{params}{jobs}{$type}{log}}, @{$ra_renames}); @{$self->{params}{jobs}{$type}{log}} = sort {return ${$a}{timestamp} <=> ${$b}{timestamp}} (@{$self->{params}{jobs}{$type}{log}}); $self->{params}{jobs}{$type}{offset} = ${${$self->{params}{jobs}{$type}{log}}[scalar(@{$self->{params}{jobs}{$type}{log}}) - 1]}{'timestamp'}; unless (scalar(@{$ra_renames}) < ($self->{params}{jobs}{$type}{step_size}-1)) # Unless we got less than what we asked for, ask again using the last timestamp as an offset {   &{$self->{shared}{add_job}}([\&handle_jobs,$type],$self->{params}{jobs}{$type}{catch_up_frequency}); $self->{params}{jobs}{$type}{current} = 0; } else {   &{$self->{shared}{add_job}}([\&handle_jobs,$type],$self->{params}{jobs}{$type}{regular_frequency}); $self->{params}{jobs}{$type}{current} = 1; } warn "Added ".scalar(@{$ra_renames})." log entries on this pass.\n"; warn "Current total of: ".scalar(@{$self->{params}{jobs}{$type}{log}})."\n"; warn ((($self->{params}{jobs}{$type}{current}) ? ('This is current') : ('This is not current'))."\n\n"); } 1;