]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/scripts/updateostatus.php
The overloaded DB_DataObject function staticGet is now called getKV
[quix0rs-gnu-social.git] / plugins / OStatus / scripts / updateostatus.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 define('INSTALLDIR', realpath(dirname(__FILE__) . '/../../..'));
22
23 $shortoptions = 'i:n:a';
24 $longoptions = array('id=', 'nickname=', 'all');
25
26 $helptext = <<<END_OF_UPDATEOSTATUS_HELP
27 updateostatus.php [options]
28 update the OMB subscriptions of a user to use OStatus if possible
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_UPDATEOSTATUS_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::getKV('id', $id);
44         if (empty($user)) {
45             throw new Exception("Can't find user with id '$id'.");
46         }
47         updateOStatus($user);
48     } else if (have_option('n', 'nickname')) {
49         $nickname = get_option_value('n', 'nickname');
50         $user = User::getKV('nickname', $nickname);
51         if (empty($user)) {
52             throw new Exception("Can't find user with nickname '$nickname'.");
53         }
54         updateOStatus($user);
55     } else if (have_option('a', 'all')) {
56         $user = new User();
57         if ($user->find()) {
58             while ($user->fetch()) {
59                 try {
60                     updateOStatus($user);
61                 } catch (Exception $e) {
62                     common_log(LOG_NOTICE, "Couldn't convert OMB subscriptions ".
63                                "for {$user->nickname} to OStatus: " . $e->getMessage());
64                 }
65             }
66         }
67     } else {
68         show_help();
69         exit(1);
70     }
71 } catch (Exception $e) {
72     print $e->getMessage()."\n";
73     exit(1);
74 }
75
76 function updateOStatus($user)
77 {
78     if (!have_option('q', 'quiet')) {
79         echo "{$user->nickname}...";
80     }
81
82     $up = $user->getProfile();
83
84     $sp = $user->getSubscriptions();
85
86     $rps = array();
87
88     while ($sp->fetch()) {
89         $remote = Remote_profile::getKV('id', $sp->id);
90
91         if (!empty($remote)) {
92             $rps[] = clone($sp);
93         }
94     }
95
96     if (!have_option('q', 'quiet')) {
97         echo count($rps) . "\n";
98     }
99
100     foreach ($rps as $rp) {
101         try {
102             if (!have_option('q', 'quiet')) {
103                 echo "Checking {$rp->nickname}...";
104             }
105
106             $op = Ostatus_profile::ensureProfileURL($rp->profileurl);
107
108             if (empty($op)) {
109                 echo "can't convert.\n";
110                 continue;
111             } else {
112                 if (!have_option('q', 'quiet')) {
113                     echo "Converting...";
114                 }
115                 Subscription::start($up, $op->localProfile());
116                 Subscription::cancel($up, $rp);
117                 if (!have_option('q', 'quiet')) {
118                     echo "done.\n";
119                 }
120             }
121
122         } catch (Exception $e) {
123             if (!have_option('q', 'quiet')) {
124                 echo "fail.\n";
125             }
126             common_log(LOG_NOTICE, "Couldn't convert OMB subscription (" . $up->nickname . ", " . $rp->nickname .
127                        ") to OStatus: " . $e->getMessage());
128             continue;
129         }
130     }
131 }