]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/scripts/fixup-shadow.php
* i18n/L10n updates and FIXMEs added
[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 $oprofile = new Ostatus_profile();
39
40 $marker = mt_rand(31337, 31337000);
41
42 $profileTemplate = common_local_url('userbyid', array('id' => $marker));
43 $encProfile = $oprofile->escape($profileTemplate, true);
44 $encProfile = str_replace($marker, '%', $encProfile);
45
46 $groupTemplate = common_local_url('groupbyid', array('id' => $marker));
47 $encGroup = $oprofile->escape($groupTemplate, true);
48 $encGroup = str_replace($marker, '%', $encGroup);
49
50 $sql = "SELECT * FROM ostatus_profile WHERE uri LIKE '%s' OR uri LIKE '%s'";
51 $oprofile->query(sprintf($sql, $encProfile, $encGroup));
52
53 $count = $oprofile->N;
54 echo "Found $count bogus ostatus_profile entries shadowing local users and groups:\n";
55
56 while ($oprofile->fetch()) {
57     $uri = $oprofile->uri;
58     if (preg_match('!/group/(\d+)/id!', $oprofile->uri, $matches)) {
59         $id = intval($matches[1]);
60         $group = Local_group::staticGet('group_id', $id);
61         if ($group) {
62             $nick = $group->nickname;
63         } else {
64             $nick = '<deleted>';
65         }
66         echo "group $id ($nick) hidden by $uri";
67     } else if (preg_match('!/user/(\d+)!', $uri, $matches)) {
68         $id = intval($matches[1]);
69         $user = User::staticGet('id', $id);
70         if ($user) {
71             $nick = $user->nickname;
72         } else {
73             $nick = '<deleted>';
74         }
75         echo "user $id ($nick) hidden by $uri";
76     } else {
77         echo "$uri matched query, but we don't recognize it.\n";
78         continue;
79     }
80
81     if ($dry) {
82         echo " - skipping\n";
83     } else {
84         echo " - removing bogus ostatus_profile entry...";
85         $evil = clone($oprofile);
86         $evil->delete();
87         echo "  ok\n";
88     }
89 }
90
91 if ($count && $dry) {
92     echo "NO CHANGES MADE -- To delete the bogus entries, run again without --dry-run option.\n";
93 } else {
94     echo "done.\n";
95 }