]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - scripts/updateurls.php
01b3c2283bdbe371ca37b6d4d5aa049d1af60578
[quix0rs-gnu-social.git] / scripts / updateurls.php
1 #!/usr/bin/env php
2 <?php
3 /*
4  * StatusNet - a distributed open-source microblogging tool
5  * Copyright (C) 2008-2011, 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 = '';
24 $longoptions = array();
25
26 $helptext = <<<END_OF_UPDATEURLS_HELP
27 updateurls.php [options]
28 update stored URLs in the system
29
30 END_OF_UPDATEURLS_HELP;
31
32 require_once INSTALLDIR.'/scripts/commandline.inc';
33
34 function main()
35 {
36     updateUserUrls();
37     updateGroupUrls();
38 }
39
40 function updateUserUrls()
41 {
42     printfnq("Updating user URLs...\n");
43
44     // XXX: only update user URLs where out-of-date
45
46     $user = new User();
47     if ($user->find()) {
48         while ($user->fetch()) {
49             printfv("Updating user {$user->nickname}...");
50             try {
51                 $profile = $user->getProfile();
52
53                 updateProfileUrl($profile);
54                 updateAvatarUrls($profile);
55
56                 // Broadcast for remote users
57
58                 common_broadcast_profile($profile);
59
60             } catch (Exception $e) {
61                 printv("Error updating URLs: " . $e->getMessage());
62             }
63             printfv("DONE.");
64         }
65     }
66 }
67
68 function updateProfileUrl($profile)
69 {
70     $orig = clone($profile);
71     $profile->profileurl = common_profile_url($profile->nickname);
72     $profile->update($orig);
73 }
74
75 function updateAvatarUrls($profile)
76 {
77     $avatar = new Avatar();
78
79     $avatar->profile_id = $profile->id;
80     if ($avatar->find()) {
81         while ($avatar->fetch()) {
82             $orig_url = $avatar->url;
83             $avatar->url = Avatar::url($avatar->filename);
84             if ($avatar->url != $orig_url) {
85                 $sql =
86                   "UPDATE avatar SET url = '" . $avatar->url . "' ".
87                   "WHERE profile_id = " . $avatar->profile_id . " ".
88                   "AND width = " . $avatar->width . " " .
89                   "AND height = " . $avatar->height . " ";
90
91                 if ($avatar->original) {
92                     $sql .= "AND original = 1 ";
93                 }
94
95                 if (!$avatar->query($sql)) {
96                     throw new Exception("Can't update avatar for user " . $profile->nickname . ".");
97                 } else {
98                     $touched = true;
99                 }
100             }
101         }
102     }
103 }
104
105 function updateGroupUrls()
106 {
107     printfnq("Updating group URLs...\n");
108
109     $group = new User_group();
110
111     if ($group->find()) {
112         while ($group->fetch()) {
113             try {
114                 printfv("Updating group {$group->nickname}...");
115                 $orig = User_group::staticGet('id', $group->id);
116                 if (!empty($group->original_logo)) {
117                     $group->original_logo = Avatar::url(basename($group->original_logo));
118                     $group->homepage_logo = Avatar::url(basename($group->homepage_logo));
119                     $group->stream_logo = Avatar::url(basename($group->stream_logo));
120                     $group->mini_logo = Avatar::url(basename($group->mini_logo));
121                 }
122                 // XXX: this is a hack to see if a group is local or not
123                 $localUri = common_local_url('groupbyid',
124                                              array('id' => $group->id));
125                 if ($group->getUri() != $localUri) {
126                     $group->mainpage = common_local_url('showgroup',
127                                                         array('nickname' => $group->nickname));
128                 }
129                 $group->update($orig);
130                 printfv("DONE.");
131             } catch (Exception $e) {
132                 printv("Can't update avatars for group " . $group->nickname . ": ". $e->getMessage());
133             }
134         }
135     }
136 }
137
138 main();