1
#!/usr/bin/env php
2
<?php
3
/*
4
 * StatusNet - a distributed open-source microblogging tool
5
 * Copyright (C) 2008, 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:a';
24
$longoptions = array('id=', 'nickname=', 'all');
25
26
$helptext = <<<END_OF_UPDATEAVATARURL_HELP
27
updateavatarurl.php [options]
28
update the URLs of all avatars in the system
29
30
  -i --id       ID of user to update
31
  -n --nickname nickname of the user to update
32
  -a --all      update all
33
34
END_OF_UPDATEAVATARURL_HELP;
35
36
require_once INSTALLDIR.'/scripts/commandline.inc';
37
38
try {
39
    $user = null;
40
41
    if (have_option('i', 'id')) {
42
        $id = get_option_value('i', 'id');
43
        $user = User::staticGet('id', $id);
44
        if (empty($user)) {
45
            throw new Exception("Can't find user with id '$id'.");
46
        }
47
        updateAvatars($user);
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
            throw new Exception("Can't find user with nickname '$nickname'");
53
        }
54
        updateAvatars($user);
55
    } else if (have_option('a', 'all')) {
56
        $user = new User();
57
        if ($user->find()) {
58
            while ($user->fetch()) {
59
                updateAvatars($user);
60
            }
61
        }
62
    } else {
63
        show_help();
64
        exit(1);
65
    }
66
} catch (Exception $e) {
67
    print $e->getMessage()."\n";
68
    exit(1);
69
}
70
71
function updateAvatars($user)
72
{
73
    $touched = false;
74
75
    if (!have_option('q', 'quiet')) {
76
        print "Updating avatars for user '".$user->nickname."' (".$user->id.")...";
77
    }
78
79
    $avatar = new Avatar();
80
81
    $avatar->profile_id = $user->id;
82
83
    if (!$avatar->find()) {
84
        if (have_option('v', 'verbose')) {
85
                print "(none found)...";
86
        }
87
    } else {
88
        while ($avatar->fetch()) {
89
            if (have_option('v', 'verbose')) {
90
                if ($avatar->original) {
91
                    print "original...";
92
                } else {
93
                    print $avatar->width."...";
94
                }
95
            }
96
97
            $orig_url = $avatar->url;
98
99
            $avatar->url = Avatar::url($avatar->filename);
100
101
            if ($avatar->url != $orig_url) {
102
                $sql =
103
                  "UPDATE avatar SET url = '" . $avatar->url . "' ".
104
                  "WHERE profile_id = " . $avatar->profile_id . " ".
105
                  "AND width = " . $avatar->width . " " .
106
                  "AND height = " . $avatar->height . " ";
107
108
                if ($avatar->original) {
109
                    $sql .= "AND original = 1 ";
110
                }
111
112
                if (!$avatar->query($sql)) {
113
                    throw new Exception("Can't update avatar for user " . $user->nickname . ".");
114
                } else {
115
                    $touched = true;
116
                }
117
            }
118
        }
119
    }
120
121
    if ($touched) {
122
        $profile = $user->getProfile();
123
        common_broadcast_profile($profile);
124
    }
125
126
    if (have_option('v', 'verbose')) {
127
        print "DONE.";
128
    }
129
    if (!have_option('q', 'quiet') || have_option('v', 'verbose')) {
130
        print "\n";
131
    }
132
}