]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/scripts/update-profile-data.php
Update Profile Data script fixes, might work for groups too now
[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             if ($dom->documentElement->tagName !== 'feed') {
66                 echo "  (no <feed> element in feed URL response; skipping)\n";
67                 return false;
68             }
69             $actorObj = ActivityUtils::getFeedAuthor($dom->documentElement);
70             if ($actorObj) {
71                 $oprofile->updateFromActivityObject($actorObj);
72                 echo "  (ok)\n";
73             } else {
74                 echo "  (no author on feed; skipping)\n";
75                 return false;
76             }
77         } else {
78             echo "  (bad feed; skipping)\n";
79             return false;
80         }
81     } else {
82         echo "Failed feed fetch: {$response->getStatus()} for $feedurl\n";
83         return false;
84     }
85
86     echo "After:\n";
87     showProfileInfo($oprofile);
88     return true;
89 }
90
91 $ok = true;
92 $validate = new Validate();
93 if (have_option('all')) {
94     $oprofile = new Ostatus_profile();
95     $oprofile->find();
96     echo "Found $oprofile->N profiles:\n\n";
97     while ($oprofile->fetch()) {
98         try {
99             $ok = fixProfile($oprofile) && $ok;
100         } catch (Exception $e) {
101             $ok = false;
102             echo "Failed on URI=="._ve($oprofile->uri).": {$e->getMessage()}\n";
103         }
104     }
105 } else if (have_option('suspicious')) {
106     $oprofile = new Ostatus_profile();
107     $oprofile->joinAdd(array('profile_id', 'profile:id'));
108     $oprofile->whereAdd("nickname rlike '^[0-9]$'");
109     $oprofile->find();
110     echo "Found $oprofile->N matching profiles:\n\n";
111     while ($oprofile->fetch()) {
112         try {
113             $ok = fixProfile($oprofile) && $ok;
114         } catch (Exception $e) {
115             $ok = false;
116             echo "Failed on URI=="._ve($oprofile->uri).": {$e->getMessage()}\n";
117         }
118     }
119 } else if (!empty($args[0]) && $validate->uri($args[0])) {
120     $uri = $args[0];
121     $oprofile = Ostatus_profile::getKV('uri', $uri);
122
123     if (!$oprofile instanceof Ostatus_profile) {
124         print "No OStatus remote profile known for URI $uri\n";
125         return false;
126     }
127
128     try {
129         $ok = fixProfile($oprofile) && $ok;
130     } catch (Exception $e) {
131         $ok = false;
132         echo "Failed on URI=="._ve($oprofile->uri).": {$e->getMessage()}\n";
133     }
134 } else {
135     print "$helptext";
136     $ok = false;
137 }
138
139 exit($ok ? 0 : 1);