1
#!/usr/bin/env php
2
<?php
3
/*
4
 * StatusNet - a distributed open-source microblogging tool
5
 * Copyright (C) 2009-2010, StatusNet, Inc.
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as published by
9
 * the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
22
23
$shortoptions = 'i::n::y';
24
$longoptions = array('id=', 'nickname=', 'yes', 'dry-run', 'all');
25
26
$helptext = <<<END_OF_HELP
27
strip_geo.php [options]
28
Removes geolocation info from the given user's notices.
29
30
  -i --id       ID of the user (may be a remote profile)
31
  -n --nickname nickname of the user
32
  -y --yes      do not wait for confirmation
33
     --dry-run  list affected notices without deleting
34
     --all      run over and decache all messages, even if they don't
35
                have geo data now (helps to fix cache bugs)
36
37
END_OF_HELP;
38
39
require_once INSTALLDIR.'/scripts/commandline.inc';
40
41
if (have_option('i', 'id')) {
42
    $id = get_option_value('i', 'id');
43
    $profile = Profile::staticGet('id', $id);
44
    if (empty($profile)) {
45
        print "Can't find local or remote profile with ID $id\n";
46
        exit(1);
47
    }
48
} else if (have_option('n', 'nickname')) {
49
    $nickname = get_option_value('n', 'nickname');
50
    $user = User::staticGet('nickname', $nickname);
51
    if (empty($user)) {
52
        print "Can't find local user with nickname '$nickname'\n";
53
        exit(1);
54
    }
55
    $profile = $user->getProfile();
56
} else {
57
    print "You must provide either an ID or a nickname.\n\n";
58
    show_help();
59
    exit(1);
60
}
61
62
if (!have_option('y', 'yes') && !have_option('--dry-run')) {
63
    print "About to PERMANENTLY remove geolocation data from user '{$profile->nickname}' ({$profile->id})'s notices. Are you sure? [y/N] ";
64
    $response = fgets(STDIN);
65
    if (strtolower(trim($response)) != 'y') {
66
        print "Aborting.\n";
67
        exit(0);
68
    }
69
}
70
71
// @fixme for a very prolific poster this could be too many.
72
$notice = new Notice();
73
$notice->profile_id = $profile->id;
74
if (have_option('--all')) {
75
    print "Finding all notices by $profile->nickname...";
76
} else {
77
    print "Finding notices by $profile->nickname with geolocation data...";
78
    $notice->whereAdd("lat != ''");
79
}
80
$notice->find();
81
82
if ($notice->N) {
83
    print " $notice->N found.\n";
84
    while ($notice->fetch()) {
85
        print "notice id $notice->id ";
86
        if (have_option('v') || have_option('--verbose')) {
87
            print "({$notice->lat},{$notice->lon}) ";
88
            if ($notice->location_ns) {
89
                print "ns {$notice->location_ns} id {$notice->location_id} ";
90
            }
91
        }
92
        if (have_option('--dry-run')) {
93
            // sucka
94
            echo "(skipped)";
95
        } else {
96
            // note: setting fields to null and calling update() doesn't save the nulled fields
97
            $orig = clone($notice);
98
            $update = clone($notice);
99
100
            // In theory we could hit a chunk of notices at once in the UPDATE,
101
            // but we're going to have to decache them individually anyway and
102
            // it doesn't hurt to make sure we don't hold up replication with
103
            // what might be a very slow single UPDATE.
104
            $query = sprintf('UPDATE notice ' .
105
                             'SET lat=NULL,lon=NULL,location_ns=NULL,location_id=NULL ' .
106
                             'WHERE id=%d', $notice->id);
107
            $ok = $update->query($query);
108
            if ($ok) {
109
                // And now we decache him manually, as query() doesn't know what we're doing...
110
                $orig->decache();
111
                echo "(removed)";
112
            } else {
113
                echo "(unchanged?)";
114
            }
115
        }
116
        print "\n";
117
    }
118
} else {
119
    print " none found.\n";
120
}
121
122
print "DONE.\n";