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', 'file=');
25
26
$helptext = <<<END_OF_INITIALIZEINBOX_HELP
27
initializeinbox.php [options]
28
initialize the inbox for a user
29
30
  -i --id         ID of user to update
31
  -n --nickname   nickname of the user to update
32
  -f FILENAME     read list of IDs from FILENAME (1 per line)
33
  --file=FILENAME ditto
34
  -a --all        update all
35
36
END_OF_INITIALIZEINBOX_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::staticGet('id', $id);
46
        if (empty($user)) {
47
            throw new Exception("Can't find user with id '$id'.");
48
        }
49
        initializeInbox($user);
50
    } else if (have_option('n', 'nickname')) {
51
        $nickname = get_option_value('n', 'nickname');
52
        $user = User::staticGet('nickname', $nickname);
53
        if (empty($user)) {
54
            throw new Exception("Can't find user with nickname '$nickname'");
55
        }
56
        initializeInbox($user);
57
    } else if (have_option('a', 'all')) {
58
        $user = new User();
59
        if ($user->find()) {
60
            while ($user->fetch()) {
61
                initializeInbox($user);
62
            }
63
        }
64
    } else if (have_option('f', 'file')) {
65
        $filename = get_option_value('f', 'file');
66
        if (!file_exists($filename)) {
67
            throw new Exception("No such file '$filename'.");
68
        } else if (!is_readable($filename)) {
69
            throw new Exception("Can't read '$filename'.");
70
        }
71
        $ids = file($filename);
72
        foreach ($ids as $id) {
73
            $user = User::staticGet('id', $id);
74
            if (empty($user)) {
75
                print "Can't find user with id '$id'.\n";
76
            }
77
            initializeInbox($user);
78
        }
79
    } else {
80
        show_help();
81
        exit(1);
82
    }
83
} catch (Exception $e) {
84
    print $e->getMessage()."\n";
85
    exit(1);
86
}
87
88
function initializeInbox($user)
89
{
90
    if (!have_option('q', 'quiet')) {
91
        print "Initializing inbox for $user->nickname...";
92
    }
93
94
    $inbox = Inbox::staticGet('user_id', $user->id);
95
96
    if ($inbox && !empty($inbox->fake)) {
97
        if (!have_option('q', 'quiet')) {
98
            echo "(replacing faux cached inbox)";
99
        }
100
        $inbox = false;
101
    }
102
    if (!empty($inbox)) {
103
        if (!have_option('q', 'quiet')) {
104
            print "SKIP\n";
105
        }
106
    } else {
107
        $inbox = Inbox::initialize($user->id);
108
        if (!have_option('q', 'quiet')) {
109
            if (empty($inbox)) {
110
                print "ERR\n";
111
            } else {
112
                print "DONE\n";
113
            }
114
        }
115
    }
116
}