source: trunk/npemap.org.uk/scripts/process-problems @ 628

Last change on this file since 628 was 628, checked in by Dominic Hargreaves, 12 years ago

add --quiet and --report options

  • Property svn:executable set to *
File size: 3.4 KB
Line 
1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use FindBin;
7use AppConfig qw(:argcount :expand);
8
9# Find our private perl libraries
10use lib "$FindBin::Bin/../perllib";
11use NPEMap;
12
13my $config =  AppConfig->new(
14    {   GLOBAL => {
15            ARGCOUNT => ARGCOUNT_ONE,
16            EXPAND   => EXPAND_ALL
17        }
18    },
19    'sameonly|s!',
20    'quiet|q!',
21    'report|r!',
22);
23
24$config->args;
25
26# Optionally, limit by the postcode ID
27my $postcode_id = shift;
28my $postcode_where = "";
29if($postcode_id && $postcode_id =~ /^\d+$/) {
30        $postcode_where = " AND p.id = $postcode_id ";
31}
32
33$postcode_where .= " AND p.ip = b.ip " if $config->sameonly;
34
35# Get the postcodes
36my $dbh = setup_dbh() or die $!;
37
38my $sql = <<EOF;
39SELECT   p.id, outward, inward, p.ip AS sub_ip, b.ip AS prob_ip, b.reason,
40         reporter_email, p.created_at AS sub_date, b.created_at AS prob_date,
41         p.easting, p.northing, s.name AS source_name
42FROM     bad_postcodes AS b INNER JOIN postcodes AS p ON (b.postcode = p.id)
43         INNER JOIN sources AS s ON (p.source = s.id)
44WHERE    not b.actioned   $postcode_where
45ORDER BY p.id, b.created_at
46EOF
47
48my $sth = $dbh->prepare($sql);
49$sth->execute or die $dbh->errstr;
50
51if ($sth->rows == 0) {
52    print "No problems\n" unless $config->quiet;
53    exit 0;
54}
55
56# Build up a list of allowed IDs to delete
57my %probids;
58
59my $hr;
60while ($hr = $sth->fetchrow_hashref) {
61    $probids{$hr->{'id'}}++;
62    $hr->{'reporter_email'} = 'anon' unless $hr->{'reporter_email'};
63    $hr->{'prob_ip'} = 'unknown IP' unless $hr->{'prob_ip'};
64    $hr->{'sub_ip'} = '' unless $hr->{'sub_ip'};
65    print $hr->{'id'} . ': ' . $hr->{'outward'} . ' ' . $hr->{'inward'} . ': ';
66    print "by " . $hr->{'reporter_email'};
67    print " at " . $hr->{'prob_ip'};
68    print ' (SAME IP)' if ($hr->{'prob_ip'} eq $hr->{'sub_ip'});
69    print "\n";
70    print " ";
71    print "Reason: " . $hr->{'reason'} . ' ' if $hr->{'reason'};
72    print "(". $hr->{'prob_date'} . ")\n";
73    print " http://www.npemap.org.uk/tiles/map.html#" .
74        int($hr->{'easting'} / 1000) . ',' . int($hr->{'northing'} / 1000) . ",1\n";
75    print " Source: " . $hr->{'source_name'} . "\n";
76}
77
78my $delsth = $dbh->prepare("UPDATE postcodes SET deleted = 't', delete_reason = 1 WHERE id = ?");
79my $actionsth = $dbh->prepare("UPDATE bad_postcodes SET actioned = 't' WHERE postcode = ?");
80my $updsth = $dbh->prepare("UPDATE postcodes SET outward = ?, inward = ? WHERE id = ?");
81
82# Now prompt for deletions
83
84exit if $config->report;
85
86print "Warning: no validation of postcodes input here\n";
87while (1) {
88    unless (%probids) {
89        print "No more problems.\n" unless $config->quiet;
90        last;
91    }
92    print "ID to resolve? (^C to exit): ";
93    my $input = <STDIN>;
94    chomp $input;
95    if ($probids{$input}) {
96        print "d for delete, i to ignore, comma separated postcode to update: ";
97        my $input2 = <STDIN>;
98        chomp $input2;
99        if ($input2 =~ /^d$/i) {
100            $delsth->execute($input) or warn $dbh->errstr;
101        } elsif ($input2 =~ /^i$/i) {
102            # Nothing. We'll mark it as actioned below
103        } elsif ($input2 =~ /(\w*),(\w*)/) {
104            $updsth->execute($1, $2, $input) or warn $dbh->errstr;
105        } else {
106            print "invalid input\n";
107            next;
108        }
109        $actionsth->execute($input) or warn $dbh->errstr;
110        delete $probids{$input};
111    } else {
112        print "$input is not a valid deletion candidate.\n";
113    }
114}
115
116$dbh->disconnect;
Note: See TracBrowser for help on using the repository browser.