4 * StatusNet - the distributed open-source microblogging tool
5 * Copyright (C) 2008, 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 # Abort if called from a web server
23 define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
25 $helptext = <<<ENDOFHELP
26 inbox_users.php <idfile>
28 Update users to use inbox table. Listed in an ID file, default 'ids.txt'.
32 require_once INSTALLDIR.'/scripts/commandline.inc';
34 $id_file = (count($args) > 1) ? $args[0] : 'ids.txt';
36 common_log(LOG_INFO, 'Updating user inboxes.');
38 $ids = file($id_file);
40 foreach ($ids as $id) {
42 $user = User::staticGet('id', $id);
45 common_log(LOG_WARNING, 'No such user: ' . $id);
50 common_log(LOG_WARNING, 'Already inboxed: ' . $id);
54 common_log(LOG_INFO, 'Updating inbox for user ' . $user->id);
56 $user->query('BEGIN');
58 $old_inbox = new Notice_inbox();
59 $old_inbox->user_id = $user->id;
61 $result = $old_inbox->delete();
63 if (is_null($result) || $result === false) {
64 common_log_db_error($old_inbox, 'DELETE', __FILE__);
70 $inbox = new Notice_inbox();
72 $result = $inbox->query('INSERT INTO notice_inbox (user_id, notice_id, created) ' .
73 'SELECT ' . $user->id . ', notice.id, notice.created ' .
74 'FROM subscription JOIN notice ON subscription.subscribed = notice.profile_id ' .
75 'WHERE subscription.subscriber = ' . $user->id . ' ' .
76 'AND notice.created >= subscription.created ' .
77 'AND NOT EXISTS (SELECT user_id, notice_id ' .
78 'FROM notice_inbox ' .
79 'WHERE user_id = ' . $user->id . ' ' .
80 'AND notice_id = notice.id) ' .
81 'ORDER BY notice.created DESC ' .
84 if (is_null($result) || $result === false) {
85 common_log_db_error($inbox, 'INSERT', __FILE__);
91 $result = $user->update($orig);
94 common_log_db_error($user, 'UPDATE', __FILE__);
98 $user->query('COMMIT');
104 $cache->delete(common_cache_key('user:notices_with_friends:' . $user->id));
105 $cache->delete(common_cache_key('user:notices_with_friends:' . $user->id . ';last'));