]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - scripts/updatelocation.php
Make attachment fit better in notice: drop text and link
[quix0rs-gnu-social.git] / scripts / updatelocation.php
1 #!/usr/bin/env php
2 <?php
3 /*
4  * StatusNet - a distributed open-source microblogging tool
5  * Copyright (C) 2009, 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:af';
25 $longoptions = array('id=', 'nickname=', 'all', 'force');
26
27 $helptext = <<<END_OF_UPDATELOCATION_HELP
28 updatelocation.php [options]
29 set the location for a profile
30
31   -i --id       ID of user to update
32   -n --nickname nickname of the user to update
33   -f --force    force update even if user already has a location
34   -a --all      update all
35
36 END_OF_UPDATELOCATION_HELP;
37
38 require_once INSTALLDIR.'/scripts/commandline.inc';
39
40 try {
41     $user = null;
42
43     if (have_option('i', 'id')) {
44         $id = get_option_value('i', 'id');
45         $user = User::getKV('id', $id);
46         if (empty($user)) {
47             throw new Exception("Can't find user with id '$id'.");
48         }
49         updateLocation($user);
50     } else if (have_option('n', 'nickname')) {
51         $nickname = get_option_value('n', 'nickname');
52         $user = User::getKV('nickname', $nickname);
53         if (empty($user)) {
54             throw new Exception("Can't find user with nickname '$nickname'");
55         }
56         updateLocation($user);
57     } else if (have_option('a', 'all')) {
58         $user = new User();
59         if ($user->find()) {
60             while ($user->fetch()) {
61                 updateLocation($user);
62             }
63         }
64     } else {
65         show_help();
66         exit(1);
67     }
68 } catch (Exception $e) {
69     print $e->getMessage()."\n";
70     exit(1);
71 }
72
73 function updateLocation($user)
74 {
75     $profile = $user->getProfile();
76
77     if (empty($profile)) {
78         throw new Exception("User has no profile: " . $user->nickname);
79     }
80
81     if (empty($profile->location)) {
82         if (have_option('v', 'verbose')) {
83             print "No location string for '".$user->nickname."'\n";
84         }
85         return;
86     }
87
88     if (!empty($profile->location_id) && !have_option('f', 'force')) {
89         if (have_option('v', 'verbose')) {
90             print "Location ID already set for '".$user->nickname."'\n";
91         }
92         return;
93     }
94
95     $loc = Location::fromName($profile->location);
96
97     if (empty($loc)) {
98         if (have_option('v', 'verbose')) {
99             print "No structured location for string '".$profile->location."' for user '".$user->nickname."'\n";
100         }
101         return;
102     } else {
103         $orig = clone($profile);
104
105         $profile->lat         = $loc->lat;
106         $profile->lon         = $loc->lon;
107         $profile->location_id = $loc->location_id;
108         $profile->location_ns = $loc->location_ns;
109
110         $result = $profile->update($orig);
111
112         if (!$result) {
113             common_log_db_error($profile, 'UPDATE', __FILE__);
114         }
115
116         if (!have_option('q', 'quiet')) {
117             print "Location ID " . $profile->location_id . " set for user " . $user->nickname . "\n";
118         }
119     }
120
121     $profile->free();
122     unset($loc);
123     unset($profile);
124
125     return;
126 }