]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/scripts/update-profile.php
a0d007a8af1191f7bf72c1f973a276ced639b59b
[quix0rs-gnu-social.git] / plugins / OStatus / scripts / update-profile.php
1 #!/usr/bin/env php
2 <?php
3 /*
4  * StatusNet - a distributed open-source microblogging tool
5  * Copyright (C) 2010, 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 $helptext = <<<END_OF_HELP
24 update-profile.php [options] http://example.com/profile/url
25
26 Rerun profile and feed info discovery for the given OStatus remote profile,
27 and reinitialize its PuSH subscription for the given feed. This may help get
28 things restarted if the hub or feed URLs have changed for the profile.
29
30
31 END_OF_HELP;
32
33 require_once INSTALLDIR.'/scripts/commandline.inc';
34
35 if (empty($args[0]) || !Validate::uri($args[0])) {
36     print "$helptext";
37     exit(1);
38 }
39
40 $uri = $args[0];
41
42
43 $oprofile = Ostatus_profile::getKV('uri', $uri);
44
45 if (!$oprofile) {
46     print "No OStatus remote profile known for URI $uri\n";
47     exit(1);
48 }
49
50 print "Old profile state for $oprofile->uri\n";
51 showProfile($oprofile);
52
53 print "\n";
54 print "Re-running feed discovery for profile URL $oprofile->uri\n";
55 // @fixme will bork where the URI isn't the profile URL for now
56 $discover = new FeedDiscovery();
57 $feedurl = $discover->discoverFromURL($oprofile->uri);
58 $huburi = $discover->getHubLink();
59 $salmonuri = $discover->getAtomLink(Salmon::REL_SALMON)
60                 ?: $discover->getAtomLink(Salmon::NS_REPLIES);
61
62 print "  Feed URL: $feedurl\n";
63 print "  Hub URL: $huburi\n";
64 print "  Salmon URL: $salmonuri\n";
65
66 if ($feedurl != $oprofile->feeduri || $salmonuri != $oprofile->salmonuri) {
67     print "\n";
68     print "Updating...\n";
69     // @fixme update keys :P
70     #$orig = clone($oprofile);
71     #$oprofile->feeduri = $feedurl;
72     #$oprofile->salmonuri = $salmonuri;
73     #$ok = $oprofile->update($orig);
74     $ok = $oprofile->query('UPDATE ostatus_profile SET ' .
75         'feeduri=\'' . $oprofile->escape($feedurl) . '\',' .
76         'salmonuri=\'' . $oprofile->escape($salmonuri) . '\' ' .
77         'WHERE uri=\'' . $oprofile->escape($uri) . '\'');
78
79     if (!$ok) {
80         print "Failed to update profile record...\n";
81         exit(1);
82     }
83
84     $oprofile->decache();
85 } else {
86     print "\n";
87     print "Ok, ostatus_profile record unchanged.\n\n";
88 }
89
90 $sub = FeedSub::ensureFeed($feedurl);
91
92 if ($huburi != $sub->huburi) {
93     print "\n";
94     print "Updating hub record for feed; was $sub->huburi\n";
95     $orig = clone($sub);
96     $sub->huburi = $huburi;
97     $ok = $sub->update($orig);
98
99     if (!$ok) {
100         print "Failed to update sub record...\n";
101         exit(1);
102     }
103 } else {
104     print "\n";
105     print "Feed record ok, not changing.\n\n";
106 }
107
108 print "\n";
109 print "Pinging hub $sub->huburi with new subscription for $sub->uri\n";
110 $ok = $sub->subscribe();
111
112 if ($ok) {
113     print "ok\n";
114 } else {
115     print "Could not confirm.\n";
116 }
117
118 $o2 = Ostatus_profile::getKV('uri', $uri);
119
120 print "\n";
121 print "New profile state:\n";
122 showProfile($o2);
123
124 print "\n";
125 print "New feed state:\n";
126 $sub2 = FeedSub::ensureFeed($feedurl);
127 showSub($sub2);
128
129 function showProfile($oprofile)
130 {
131     print "  Feed URL: $oprofile->feeduri\n";
132     print "  Salmon URL: $oprofile->salmonuri\n";
133     print "  Avatar URL: $oprofile->avatar\n";
134     print "  Profile ID: $oprofile->profile_id\n";
135     print "  Group ID: $oprofile->group_id\n";
136     print "  Record created: $oprofile->created\n";
137     print "  Record modified: $oprofile->modified\n";
138 }
139
140 function showSub($sub)
141 {
142     print "  Subscription state: $sub->sub_state\n";
143     print "  Verify token: $sub->verify_token\n";
144     print "  Signature secret: $sub->secret\n";
145     print "  Sub start date: $sub->sub_start\n";
146     print "  Record created: $sub->created\n";
147     print "  Record modified: $sub->modified\n";
148 }