]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - scripts/strip_geo.php
Make attachment fit better in notice: drop text and link
[quix0rs-gnu-social.git] / scripts / strip_geo.php
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', dirname(__DIR__));
22 define('PUBLICDIR', INSTALLDIR . DIRECTORY_SEPARATOR . 'public');
23
24 $shortoptions = 'i::n::y';
25 $longoptions = array('id=', 'nickname=', 'yes', 'dry-run', 'all');
26
27 $helptext = <<<END_OF_HELP
28 strip_geo.php [options]
29 Removes geolocation info from the given user's notices.
30
31   -i --id       ID of the user (may be a remote profile)
32   -n --nickname nickname of the user
33   -y --yes      do not wait for confirmation
34      --dry-run  list affected notices without deleting
35      --all      run over and decache all messages, even if they don't
36                 have geo data now (helps to fix cache bugs)
37
38 END_OF_HELP;
39
40 require_once INSTALLDIR.'/scripts/commandline.inc';
41
42 if (have_option('i', 'id')) {
43     $id = get_option_value('i', 'id');
44     $profile = Profile::getKV('id', $id);
45     if (empty($profile)) {
46         print "Can't find local or remote profile with ID $id\n";
47         exit(1);
48     }
49 } else if (have_option('n', 'nickname')) {
50     $nickname = get_option_value('n', 'nickname');
51     $user = User::getKV('nickname', $nickname);
52     if (empty($user)) {
53         print "Can't find local user with nickname '$nickname'\n";
54         exit(1);
55     }
56     $profile = $user->getProfile();
57 } else {
58     print "You must provide either an ID or a nickname.\n\n";
59     show_help();
60     exit(1);
61 }
62
63 if (!have_option('y', 'yes') && !have_option('--dry-run')) {
64     print "About to PERMANENTLY remove geolocation data from user '{$profile->nickname}' ({$profile->id})'s notices. Are you sure? [y/N] ";
65     $response = fgets(STDIN);
66     if (strtolower(trim($response)) != 'y') {
67         print "Aborting.\n";
68         exit(0);
69     }
70 }
71
72 // @fixme for a very prolific poster this could be too many.
73 $notice = new Notice();
74 $notice->profile_id = $profile->id;
75 if (have_option('--all')) {
76     print "Finding all notices by $profile->nickname...";
77 } else {
78     print "Finding notices by $profile->nickname with geolocation data...";
79     $notice->whereAdd("lat != ''");
80 }
81 $notice->find();
82
83 if ($notice->N) {
84     print " $notice->N found.\n";
85     while ($notice->fetch()) {
86         print "notice id $notice->id ";
87         if (have_option('v') || have_option('--verbose')) {
88             print "({$notice->lat},{$notice->lon}) ";
89             if ($notice->location_ns) {
90                 print "ns {$notice->location_ns} id {$notice->location_id} ";
91             }
92         }
93         if (have_option('--dry-run')) {
94             // sucka
95             echo "(skipped)";
96         } else {
97             // note: setting fields to null and calling update() doesn't save the nulled fields
98             $orig = clone($notice);
99             $update = clone($notice);
100
101             // In theory we could hit a chunk of notices at once in the UPDATE,
102             // but we're going to have to decache them individually anyway and
103             // it doesn't hurt to make sure we don't hold up replication with
104             // what might be a very slow single UPDATE.
105             $query = sprintf('UPDATE notice ' .
106                              'SET lat=NULL,lon=NULL,location_ns=NULL,location_id=NULL ' .
107                              'WHERE id=%d', $notice->id);
108             $ok = $update->query($query);
109             if ($ok) {
110                 // And now we decache him manually, as query() doesn't know what we're doing...
111                 $orig->decache();
112                 echo "(removed)";
113             } else {
114                 echo "(unchanged?)";
115             }
116         }
117         print "\n";
118     }
119 } else {
120     print " none found.\n";
121 }
122
123 print "DONE.\n";