4 * StatusNet - a distributed open-source microblogging tool
5 * Copyright (C) 2009, StatusNet, Inc.
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.
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.
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/>.
21 define('INSTALLDIR', dirname(__DIR__));
22 define('PUBLICDIR', INSTALLDIR . DIRECTORY_SEPARATOR . 'public');
24 $shortoptions = 'i:n:af';
25 $longoptions = array('id=', 'nickname=', 'all', 'force');
27 $helptext = <<<END_OF_UPDATELOCATION_HELP
28 updatelocation.php [options]
29 set the location for a profile
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
36 END_OF_UPDATELOCATION_HELP;
38 require_once INSTALLDIR.'/scripts/commandline.inc';
43 if (have_option('i', 'id')) {
44 $id = get_option_value('i', 'id');
45 $user = User::getKV('id', $id);
47 throw new Exception("Can't find user with id '$id'.");
49 updateLocation($user);
50 } else if (have_option('n', 'nickname')) {
51 $nickname = get_option_value('n', 'nickname');
52 $user = User::getKV('nickname', $nickname);
54 throw new Exception("Can't find user with nickname '$nickname'");
56 updateLocation($user);
57 } else if (have_option('a', 'all')) {
60 while ($user->fetch()) {
61 updateLocation($user);
68 } catch (Exception $e) {
69 print $e->getMessage()."\n";
73 function updateLocation($user)
75 $profile = $user->getProfile();
77 if (empty($profile)) {
78 throw new Exception("User has no profile: " . $user->nickname);
81 if (empty($profile->location)) {
82 if (have_option('v', 'verbose')) {
83 print "No location string for '".$user->nickname."'\n";
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";
95 $loc = Location::fromName($profile->location);
98 if (have_option('v', 'verbose')) {
99 print "No structured location for string '".$profile->location."' for user '".$user->nickname."'\n";
103 $orig = clone($profile);
105 $profile->lat = $loc->lat;
106 $profile->lon = $loc->lon;
107 $profile->location_id = $loc->location_id;
108 $profile->location_ns = $loc->location_ns;
110 $result = $profile->update($orig);
113 common_log_db_error($profile, 'UPDATE', __FILE__);
116 if (!have_option('q', 'quiet')) {
117 print "Location ID " . $profile->location_id . " set for user " . $user->nickname . "\n";