]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/scripts/fixup-shadow.php
Merge branch '1.0.x' into schema-x
[quix0rs-gnu-social.git] / plugins / OStatus / scripts / fixup-shadow.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('dry-run');
24
25 $helptext = <<<END_OF_USERROLE_HELP
26 fixup_shadow.php [options]
27 Patches up stray ostatus_profile entries with corrupted shadow entries
28 for local users and groups.
29
30      --dry-run  look but don't touch
31
32 END_OF_USERROLE_HELP;
33
34 require_once INSTALLDIR.'/scripts/commandline.inc';
35
36 $dry = have_option('dry-run');
37
38 // Look for user.uri matches... These may not match up with the current
39 // URL schema if the site has changed names.
40 echo "Checking for bogus ostatus_profile entries matching user.uri...\n";
41
42 $user = new User();
43 $oprofile = new Ostatus_profile();
44 $user->joinAdd($oprofile, 'INNER', 'oprofile', 'uri');
45 $user->find();
46 $count = $user->N;
47 echo "Found $count...\n";
48
49 while ($user->fetch()) {
50     $uri = $user->uri;
51     echo "user $user->id ($user->nickname) hidden by $uri";
52     if ($dry) {
53         echo " - skipping\n";
54     } else {
55         echo " - removing bogus ostatus_profile entry...";
56         $evil = Ostatus_profile::staticGet('uri', $uri);
57         $evil->delete();
58         echo "  ok\n";
59     }
60 }
61 echo "\n";
62
63 // Also try user_group.uri matches for local groups.
64 // Not all group entries will have this filled out, though, as it's new!
65 echo "Checking for bogus ostatus_profile entries matching local user_group.uri...\n";
66 $group = new User_group();
67 $group->joinAdd(array('uri', 'ostatus_profile:uri'));
68 $group->joinAdd(array('id', 'local_group:group_id'));
69 $group->find();
70 $count = $group->N;
71 echo "Found $count...\n";
72
73 while ($group->fetch()) {
74     $uri = $group->uri;
75     echo "group $group->id ($group->nickname) hidden by $uri";
76     if ($dry) {
77         echo " - skipping\n";
78     } else {
79         echo " - removing bogus ostatus_profile entry...";
80         $evil = Ostatus_profile::staticGet('uri', $uri);
81         $evil->delete();
82         echo "  ok\n";
83     }
84 }
85 echo "\n";
86
87
88 // Fallback?
89 echo "Checking for bogus profiles blocking local users/groups by URI pattern match...\n";
90 $oprofile = new Ostatus_profile();
91
92 $marker = mt_rand(31337, 31337000);
93
94 $profileTemplate = common_local_url('userbyid', array('id' => $marker));
95 $encProfile = $oprofile->escape($profileTemplate, true);
96 $encProfile = str_replace($marker, '%', $encProfile);
97 echo "  LIKE '$encProfile'\n";
98
99 $groupTemplate = common_local_url('groupbyid', array('id' => $marker));
100 $encGroup = $oprofile->escape($groupTemplate, true);
101 $encGroup = str_replace($marker, '%', $encGroup);
102 echo "  LIKE '$encGroup'\n";
103
104 $sql = "SELECT * FROM ostatus_profile WHERE uri LIKE '%s' OR uri LIKE '%s'";
105 $oprofile->query(sprintf($sql, $encProfile, $encGroup));
106
107 $count = $oprofile->N;
108 echo "Found $count...\n";
109
110 while ($oprofile->fetch()) {
111     $uri = $oprofile->uri;
112     if (preg_match('!/group/(\d+)/id!', $oprofile->uri, $matches)) {
113         $id = intval($matches[1]);
114         $group = Local_group::staticGet('group_id', $id);
115         if ($group) {
116             $nick = $group->nickname;
117         } else {
118             $nick = '<deleted>';
119         }
120         echo "group $id ($nick) hidden by $uri";
121     } else if (preg_match('!/user/(\d+)!', $uri, $matches)) {
122         $id = intval($matches[1]);
123         $user = User::staticGet('id', $id);
124         if ($user) {
125             $nick = $user->nickname;
126         } else {
127             $nick = '<deleted>';
128         }
129         echo "user $id ($nick) hidden by $uri";
130     } else {
131         echo "$uri matched query, but we don't recognize it.\n";
132         continue;
133     }
134
135     if ($dry) {
136         echo " - skipping\n";
137     } else {
138         echo " - removing bogus ostatus_profile entry...";
139         $evil = clone($oprofile);
140         $evil->delete();
141         echo "  ok\n";
142     }
143 }
144
145 if ($count && $dry) {
146     echo "NO CHANGES MADE -- To delete the bogus entries, run again without --dry-run option.\n";
147 } else {
148     echo "done.\n";
149 }