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