#!/usr/bin/perl # # Copyright (c) 2006-2008 Dominic Hargreaves # Permission is hereby granted, free of charge, to any person obtaining a # copy of this software and associated documentation files (the "Software"), # to deal in the Software without restriction, including without limitation # the rights to use, copy, modify, merge, publish, distribute, sublicense, # and/or sell copies of the Software, and to permit persons to whom the # Software is furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS # IN THE SOFTWARE. # use strict; use warnings; use FindBin; use AppConfig qw(:argcount :expand); # Find our private perl libraries use lib "$FindBin::Bin/../perllib"; use NPEMap; my $config = AppConfig->new( { GLOBAL => { ARGCOUNT => ARGCOUNT_ONE, EXPAND => EXPAND_ALL } }, 'sameonly|s!', 'quiet|q!', 'report|r!', ); $config->args; # Optionally, limit by the postcode ID my $postcode_id = shift; my $postcode_where = ""; if($postcode_id && $postcode_id =~ /^\d+$/) { $postcode_where = " AND p.id = $postcode_id "; } $postcode_where .= " AND p.ip = b.ip " if $config->sameonly; # Get the postcodes my $dbh = setup_dbh() or die $!; my $sql = <prepare($sql); $sth->execute or die $dbh->errstr; if ($sth->rows == 0) { print "No problems\n" unless $config->quiet; exit 0; } # Build up a list of allowed IDs to delete my %probids; my $hr; while ($hr = $sth->fetchrow_hashref) { $probids{$hr->{'id'}}++; $hr->{'reporter_email'} = 'anon' unless $hr->{'reporter_email'}; $hr->{'prob_ip'} = 'unknown IP' unless $hr->{'prob_ip'}; $hr->{'sub_ip'} = '' unless $hr->{'sub_ip'}; print $hr->{'id'} . ': ' . $hr->{'outward'} . ' ' . $hr->{'inward'} . ': '; print "by " . $hr->{'reporter_email'}; print " at " . $hr->{'prob_ip'}; print ' (SAME IP)' if ($hr->{'prob_ip'} eq $hr->{'sub_ip'}); print "\n"; print " "; print "Reason: " . $hr->{'reason'} . ' ' if $hr->{'reason'}; print "(". $hr->{'prob_date'} . ")\n"; print " http://www.npemap.org.uk/tiles/map.html#" . int($hr->{'easting'} / 1000) . ',' . int($hr->{'northing'} / 1000) . ",1\n"; print " Source: " . $hr->{'source_name'} . "\n"; } my $delsth = $dbh->prepare("UPDATE postcodes SET deleted = 't', delete_reason = 1 WHERE id = ?"); my $actionsth = $dbh->prepare("UPDATE bad_postcodes SET actioned = 't' WHERE postcode = ?"); my $updsth = $dbh->prepare("UPDATE postcodes SET outward = ?, inward = ? WHERE id = ?"); # Now prompt for deletions exit if $config->report; print "Warning: no validation of postcodes input here\n"; while (1) { unless (%probids) { print "No more problems.\n" unless $config->quiet; last; } print "ID to resolve? (^C to exit): "; my $input = ; chomp $input; if ($probids{$input}) { print "d for delete, i to ignore, comma separated postcode to update: "; my $input2 = ; chomp $input2; if ($input2 =~ /^d$/i) { $delsth->execute($input) or warn $dbh->errstr; } elsif ($input2 =~ /^i$/i) { # Nothing. We'll mark it as actioned below } elsif ($input2 =~ /(\w*),(\w*)/) { $updsth->execute($1, $2, $input) or warn $dbh->errstr; } else { print "invalid input\n"; next; } $actionsth->execute($input) or warn $dbh->errstr; delete $probids{$input}; } else { print "$input is not a valid deletion candidate.\n"; } } $dbh->disconnect;