4 * StatusNet - a distributed open-source microblogging tool
5 * Copyright (C) 2010 StatusNet, Inc.
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.
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.
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/>.
21 define('INSTALLDIR', realpath(dirname(__FILE__) . '/../../..'));
23 $longoptions = array('dry-run');
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.
30 --dry-run look but don't touch
34 require_once INSTALLDIR.'/scripts/commandline.inc';
36 $dry = have_option('dry-run');
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";
43 $oprofile = new Ostatus_profile();
44 $user->joinAdd($oprofile, 'INNER', 'oprofile', 'uri');
47 echo "Found $count...\n";
49 while ($user->fetch()) {
51 echo "user $user->id ($user->nickname) hidden by $uri";
55 echo " - removing bogus ostatus_profile entry...";
56 $evil = Ostatus_profile::staticGet('uri', $uri);
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'));
71 echo "Found $count...\n";
73 while ($group->fetch()) {
75 echo "group $group->id ($group->nickname) hidden by $uri";
79 echo " - removing bogus ostatus_profile entry...";
80 $evil = Ostatus_profile::staticGet('uri', $uri);
89 echo "Checking for bogus profiles blocking local users/groups by URI pattern match...\n";
90 $oprofile = new Ostatus_profile();
92 $marker = mt_rand(31337, 31337000);
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";
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";
104 $sql = "SELECT * FROM ostatus_profile WHERE uri LIKE '%s' OR uri LIKE '%s'";
105 $oprofile->query(sprintf($sql, $encProfile, $encGroup));
107 $count = $oprofile->N;
108 echo "Found $count...\n";
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);
116 $nick = $group->nickname;
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);
125 $nick = $user->nickname;
129 echo "user $id ($nick) hidden by $uri";
131 echo "$uri matched query, but we don't recognize it.\n";
136 echo " - skipping\n";
138 echo " - removing bogus ostatus_profile entry...";
139 $evil = clone($oprofile);
145 if ($count && $dry) {
146 echo "NO CHANGES MADE -- To delete the bogus entries, run again without --dry-run option.\n";