]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - scripts/fixup_group_uri.php
scripts/fixup_group_uri.php to fill in empty user_group.uri entries; needed before...
[quix0rs-gnu-social.git] / scripts / fixup_group_uri.php
1 #!/usr/bin/env php
2 <?php
3 /*
4  * StatusNet - a distributed open-source microblogging tool
5  * Copyright (C) 2008, 2009, 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 $shortoptions = 'i:n:a';
24 $longoptions = array('id=', 'nickname=', 'all', 'dry-run');
25
26 $helptext = <<<END_OF_UPDATEAVATARURL_HELP
27 fixup_group_uri.php [options]
28 Fill in unstored URIs for groups in the system (added during 0.9)
29
30   -i --id       ID of group to update
31   -n --nickname nickname of the group to update
32   -a --all      update all
33      --dry-run  don't change anything
34
35 END_OF_UPDATEAVATARURL_HELP;
36
37 require_once INSTALLDIR.'/scripts/commandline.inc';
38
39 try {
40     $user = null;
41
42     if (have_option('i', 'id')) {
43         $id = get_option_value('i', 'id');
44         $group = User_group::staticGet('id', $id);
45         if (empty($group)) {
46             throw new Exception("Can't find group with id '$id'.");
47         }
48         updateGroupUri($group);
49     } else if (have_option('n', 'nickname')) {
50         $nickname = get_option_value('n', 'nickname');
51         $group = User_group::staticGet('nickname', $nickname);
52         if (empty($group)) {
53             throw new Exception("Can't find group with nickname '$nickname'");
54         }
55         updateGroupUri($group);
56     } else if (have_option('a', 'all')) {
57         $group = new User_group();
58         $group->whereAdd('uri IS NULL');
59         if ($group->find()) {
60             while ($group->fetch()) {
61                 updateGroupUri($group);
62             }
63         }
64     } else {
65         show_help();
66         exit(1);
67     }
68 } catch (Exception $e) {
69     print $e->getMessage()."\n";
70     exit(1);
71 }
72
73 function updateGroupUri($group)
74 {
75     if (!have_option('q', 'quiet')) {
76         print "Updating URI for group '".$group->nickname."' (".$group->id.")...";
77     }
78
79     if (empty($group->uri)) {
80         // Using clone here was screwing up the group->find() iteration
81         $orig = User_group::staticGet('id', $group->id);
82
83         $group->uri = $group->getUri();
84         if (have_option('dry_run')) {
85             echo " would have set $group->uri ";
86         } else {
87             if (!$group->update($orig)) {
88                 throw new Exception("Can't update uri for group " . $group->nickname . ".");
89             }
90             echo " set $group->uri ";
91         }
92     } else {
93         print " already set, keeping $group->uri ";
94     }
95
96     if (have_option('v', 'verbose')) {
97         print "DONE.";
98     }
99     if (!have_option('q', 'quiet') || have_option('v', 'verbose')) {
100         print "\n";
101     }
102 }