]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/scripts/update-profile-data.php
OStatus update profile data script fixes
[quix0rs-gnu-social.git] / plugins / OStatus / scripts / update-profile-data.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 $longoptions = array('all', 'suspicious', 'quiet');
24
25 $helptext = <<<END_OF_HELP
26 update-profile-data.php [options] [http://example.com/profile/url]
27
28 Rerun profile discovery for the given OStatus remote profile, and save the
29 updated profile data (nickname, fullname, avatar, bio, etc).
30 Doesn't touch feed state.
31 Can be used to clean up after breakages.
32
33 Options:
34   --all        Run for all known OStatus profiles
35   --suspicious Run for OStatus profiles with all-numeric nicknames
36                (fixes 0.9.7 prerelease back-compatibility bug)
37
38 END_OF_HELP;
39
40 require_once INSTALLDIR.'/scripts/commandline.inc';
41
42 function showProfileInfo(Ostatus_profile $oprofile) {
43     if ($oprofile->isGroup()) {
44         echo "group\n";
45     } else {
46         $profile = $oprofile->localProfile();
47         foreach (array('nickname', 'fullname', 'bio', 'homepage', 'location') as $field) {
48             print "  $field: {$profile->$field}\n";
49         }
50     }
51     echo "\n";
52 }
53
54 function fixProfile(Ostatus_profile $oprofile) {
55     echo "Before:\n";
56     showProfileInfo($oprofile);
57
58     $feedurl = $oprofile->feeduri;
59     $client = new HTTPClient();
60     $response = $client->get($feedurl);
61     if ($response->isOk()) {
62         echo "Updating profile from feed: $feedurl\n";
63         $dom = new DOMDocument();
64         if ($dom->loadXML($response->getBody())) {
65             $feed = $dom->documentElement;
66             $entries = $dom->getElementsByTagNameNS(Activity::ATOM, 'entry');
67             if ($entries->length) {
68                 $entry = $entries->item(0);
69                 $activity = new Activity($entry, $feed);
70                 $oprofile->checkAuthorship($activity);
71                 echo "  (ok)\n";
72             } else {
73                 echo "  (no entry; skipping)\n";
74                 return false;
75             }
76         } else {
77             echo "  (bad feed; skipping)\n";
78             return false;
79         }
80     } else {
81         echo "Failed feed fetch: {$response->getStatus()} for $feedurl\n";
82         return false;
83     }
84
85     echo "After:\n";
86     showProfileInfo($oprofile);
87     return true;
88 }
89
90 $ok = true;
91 $validate = new Validate();
92 if (have_option('all')) {
93     $oprofile = new Ostatus_profile();
94     $oprofile->find();
95     echo "Found $oprofile->N profiles:\n\n";
96     while ($oprofile->fetch()) {
97         try {
98             $ok = fixProfile($oprofile) && $ok;
99         } catch (Exception $e) {
100             $ok = false;
101             echo "Failed on URI=="._ve($oprofile->uri).": {$e->getMessage()}\n";
102         }
103     }
104 } else if (have_option('suspicious')) {
105     $oprofile = new Ostatus_profile();
106     $oprofile->joinAdd(array('profile_id', 'profile:id'));
107     $oprofile->whereAdd("nickname rlike '^[0-9]$'");
108     $oprofile->find();
109     echo "Found $oprofile->N matching profiles:\n\n";
110     while ($oprofile->fetch()) {
111         try {
112             $ok = fixProfile($oprofile) && $ok;
113         } catch (Exception $e) {
114             $ok = false;
115             echo "Failed on URI=="._ve($oprofile->uri).": {$e->getMessage()}\n";
116         }
117     }
118 } else if (!empty($args[0]) && $validate->uri($args[0])) {
119     $uri = $args[0];
120     $oprofile = Ostatus_profile::getKV('uri', $uri);
121
122     if (!$oprofile instanceof Ostatus_profile) {
123         print "No OStatus remote profile known for URI $uri\n";
124         return false;
125     }
126
127     try {
128         $ok = fixProfile($oprofile) && $ok;
129     } catch (Exception $e) {
130         $ok = false;
131         echo "Failed on URI=="._ve($oprofile->uri).": {$e->getMessage()}\n";
132     }
133 } else {
134     print "$helptext";
135     $ok = false;
136 }
137
138 exit($ok ? 0 : 1);