]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - scripts/fixup_group_profiles.php
Merge remote-tracking branch 'upstream/master' into social-master
[quix0rs-gnu-social.git] / scripts / fixup_group_profiles.php
1 #!/usr/bin/env php
2 <?php
3 /*
4  * StatusNet - the distributed open-source microblogging tool
5  * Copyright (C) 2008, 2009, 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 // Abort if called from a web server
22 if (isset($_SERVER) && array_key_exists('REQUEST_METHOD', $_SERVER)) {
23     print "This script must be run from the command line\n";
24     exit();
25 }
26
27 define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
28
29 require_once(INSTALLDIR . '/scripts/commandline.inc.php');
30 error_reporting(E_ALL | E_STRICT);
31
32 $relative = common_path('group/');
33 print('Searching for profiles with relative=' . $relative . ' ...' . PHP_EOL);
34
35 $profile = new Profile();
36 $profile->whereAdd("`profileurl` LIKE '" . $relative . "%'");
37 $cnt = $profile->find();
38
39 print('Found ' . $cnt . ' profiles.' . PHP_EOL);
40
41 if ($cnt == 0) {
42     // Nothing to do
43     print('All profiles are fixed. :-)' . PHP_EOL);
44     exit;
45 }
46
47 $profiles = array();
48
49 while ($profile->fetch()) {
50     array_push($profiles, "'" . $profile->nickname . "'");
51 }
52
53 // Cleanup
54 unset($profile);
55
56 print('Have ' . count($profiles) . ' profiles found.' . PHP_EOL);
57
58 // Make sure both are equal
59 assert(count($profiles) == $cnt);
60
61 print('Looking for matching user groups without local groups ...' . PHP_EOL);
62
63 $group = new User_group();
64 $group->whereAdd("`nickname` IN (" . implode(', ', $profiles) . ") AND `uri` NOT LIKE '" . $relative . "%'");
65 $cnt = $group->find();
66
67 print('Found ' . $cnt . ' groups.' . PHP_EOL);
68
69 while ($group->fetch()) {
70     print('Fixing profile for group #' . $group->id . ',nickname=' . $group->nickname . ' to ' . $group->uri . ' ...' . PHP_EOL);
71
72     if ($group->profile_id < 1) {
73         print('INCONSISTENCY - Group ' . $group->nickname . ' has no profile_id set. Cannot fix!' . PHP_EOL);
74     }
75
76     $profile = new Profile();
77     $profile->id = $group->profile_id;
78     $cnt2 = $profile->find();
79
80     if ($cnt2 == 0) {
81         print('INCONSISTENCY - Cannot find profile ' . $group->nickname . ' - skipped!' . PHP_EOL);
82         continue;
83     } elseif ($cnt2 > 1) {
84         print('INCONSISTENCY - Group ' . $group->nickname . ' has more than one (' . $cnt2 . ') profiles - skipped!' . PHP_EOL);
85         continue;
86     } else {
87        // Dummy fetch
88        $profile->fetch();
89     }
90
91     $original = clone($profile);
92     $profile->profileurl = $group->uri;
93
94     $result = $profile->update($original);
95     if (!$result) {
96         common_log_db_error($profile, 'UPDATE', __FILE__);
97     }
98 }