]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/scripts/update-profile-data.php
The overloaded DB_DataObject function staticGet is now called getKV
[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, avatar, bio, etc). Doesn't touch feed state.
30 Can be used to clean up after breakages.
31
32 Options:
33   --all        Run for all known OStatus profiles
34   --suspicious Run for OStatus profiles with all-numeric nicknames
35                (fixes 0.9.7 prerelease back-compatibility bug)
36
37 END_OF_HELP;
38
39 require_once INSTALLDIR.'/scripts/commandline.inc';
40
41 function showProfileInfo($oprofile) {
42     if ($oprofile->isGroup()) {
43         echo "group\n";
44     } else {
45         $profile = $oprofile->localProfile();
46         foreach (array('nickname', 'bio', 'homepage', 'location') as $field) {
47             print "  $field: {$profile->$field}\n";
48         }
49     }
50     echo "\n";
51 }
52
53 function fixProfile($uri) {
54     $oprofile = Ostatus_profile::getKV('uri', $uri);
55
56     if (!$oprofile) {
57         print "No OStatus remote profile known for URI $uri\n";
58         return false;
59     }
60
61     echo "Before:\n";
62     showProfileInfo($oprofile);
63
64     $feedurl = $oprofile->feeduri;
65     $client = new HttpClient();
66     $response = $client->get($feedurl);
67     if ($response->isOk()) {
68         echo "Updating profile from feed: $feedurl\n";
69         $dom = new DOMDocument();
70         if ($dom->loadXML($response->getBody())) {
71             $feed = $dom->documentElement;
72             $entries = $dom->getElementsByTagNameNS(Activity::ATOM, 'entry');
73             if ($entries->length) {
74                 $entry = $entries->item(0);
75                 $activity = new Activity($entry, $feed);
76                 $oprofile->checkAuthorship($activity);
77                 echo "  (ok)\n";
78             } else {
79                 echo "  (no entry; skipping)\n";
80                 return false;
81             }
82         } else {
83             echo "  (bad feed; skipping)\n";
84             return false;
85         }
86     } else {
87         echo "Failed feed fetch: {$response->getStatus()} for $feedurl\n";
88         return false;
89     }
90
91     echo "After:\n";
92     showProfileInfo($oprofile);
93     return true;
94 }
95
96 $ok = true;
97 if (have_option('all')) {
98     $oprofile = new Ostatus_profile();
99     $oprofile->find();
100     echo "Found $oprofile->N profiles:\n\n";
101     while ($oprofile->fetch()) {
102         $ok = fixProfile($oprofile->uri) && $ok;
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         $ok = fixProfile($oprofile->uri) && $ok;
112     }
113 } else if (!empty($args[0]) && Validate::uri($args[0])) {
114     $uri = $args[0];
115     $ok = fixProfile($uri);
116 } else {
117     print "$helptext";
118     $ok = false;
119 }
120
121 exit($ok ? 0 : 1);