]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - scripts/inbox_users.php
change Laconica and Control Yourself to StatusNet in PHP files
[quix0rs-gnu-social.git] / scripts / inbox_users.php
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 # Abort if called from a web server
22
23 define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
24
25 $helptext = <<<ENDOFHELP
26 inbox_users.php <idfile>
27
28 Update users to use inbox table. Listed in an ID file, default 'ids.txt'.
29
30 ENDOFHELP;
31
32 require_once INSTALLDIR.'/scripts/commandline.inc';
33
34 $id_file = (count($args) > 1) ? $args[0] : 'ids.txt';
35
36 common_log(LOG_INFO, 'Updating user inboxes.');
37
38 $ids = file($id_file);
39
40 foreach ($ids as $id) {
41
42         $user = User::staticGet('id', $id);
43
44         if (!$user) {
45                 common_log(LOG_WARNING, 'No such user: ' . $id);
46                 continue;
47         }
48
49         if ($user->inboxed) {
50                 common_log(LOG_WARNING, 'Already inboxed: ' . $id);
51                 continue;
52         }
53
54     common_log(LOG_INFO, 'Updating inbox for user ' . $user->id);
55
56         $user->query('BEGIN');
57
58         $old_inbox = new Notice_inbox();
59         $old_inbox->user_id = $user->id;
60
61         $result = $old_inbox->delete();
62
63         if (is_null($result) || $result === false) {
64                 common_log_db_error($old_inbox, 'DELETE', __FILE__);
65                 continue;
66         }
67
68         $old_inbox->free();
69
70         $inbox = new Notice_inbox();
71
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 ' .
82                                                         'LIMIT 0, 1000');
83
84         if (is_null($result) || $result === false) {
85                 common_log_db_error($inbox, 'INSERT', __FILE__);
86                 continue;
87         }
88
89         $orig = clone($user);
90         $user->inboxed = 1;
91         $result = $user->update($orig);
92
93         if (!$result) {
94                 common_log_db_error($user, 'UPDATE', __FILE__);
95                 continue;
96         }
97
98         $user->query('COMMIT');
99
100         $inbox->free();
101         unset($inbox);
102
103         if ($cache) {
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'));
106         }
107 }