updateurls.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
  21. $shortoptions = '';
  22. $longoptions = array();
  23. $helptext = <<<END_OF_UPDATEURLS_HELP
  24. updateurls.php [options]
  25. update stored URLs in the system
  26. END_OF_UPDATEURLS_HELP;
  27. require_once INSTALLDIR.'/scripts/commandline.inc';
  28. function main()
  29. {
  30. updateUserUrls();
  31. updateGroupUrls();
  32. }
  33. function updateUserUrls()
  34. {
  35. printfnq("Updating user URLs...\n");
  36. // XXX: only update user URLs where out-of-date
  37. $user = new User();
  38. if ($user->find()) {
  39. while ($user->fetch()) {
  40. printfv("Updating user {$user->nickname}...");
  41. try {
  42. $profile = $user->getProfile();
  43. updateProfileUrl($profile);
  44. updateAvatarUrls($profile);
  45. } catch (Exception $e) {
  46. echo "Error updating URLs: " . $e->getMessage();
  47. }
  48. printfv("DONE.");
  49. }
  50. }
  51. }
  52. function updateProfileUrl($profile)
  53. {
  54. $orig = clone($profile);
  55. $profile->profileurl = common_profile_url($profile->nickname);
  56. $profile->update($orig);
  57. }
  58. function updateAvatarUrls($profile)
  59. {
  60. $avatar = new Avatar();
  61. $avatar->profile_id = $profile->id;
  62. if ($avatar->find()) {
  63. while ($avatar->fetch()) {
  64. $orig_url = $avatar->url;
  65. $avatar->url = Avatar::url($avatar->filename);
  66. if ($avatar->url != $orig_url) {
  67. $sql =
  68. "UPDATE avatar SET url = '" . $avatar->url . "' ".
  69. "WHERE profile_id = " . $avatar->profile_id . " ".
  70. "AND width = " . $avatar->width . " " .
  71. "AND height = " . $avatar->height . " ";
  72. if ($avatar->original) {
  73. $sql .= "AND original = 1 ";
  74. }
  75. if (!$avatar->query($sql)) {
  76. throw new Exception("Can't update avatar for user " . $profile->nickname . ".");
  77. } else {
  78. $touched = true;
  79. }
  80. }
  81. }
  82. }
  83. }
  84. function updateGroupUrls()
  85. {
  86. printfnq("Updating group URLs...\n");
  87. $group = new User_group();
  88. if ($group->find()) {
  89. while ($group->fetch()) {
  90. try {
  91. printfv("Updating group {$group->nickname}...");
  92. $orig = User_group::getKV('id', $group->id);
  93. if (!empty($group->original_logo)) {
  94. $group->original_logo = Avatar::url(basename($group->original_logo));
  95. $group->homepage_logo = Avatar::url(basename($group->homepage_logo));
  96. $group->stream_logo = Avatar::url(basename($group->stream_logo));
  97. $group->mini_logo = Avatar::url(basename($group->mini_logo));
  98. }
  99. // XXX: this is a hack to see if a group is local or not
  100. $localUri = common_local_url('groupbyid',
  101. array('id' => $group->id));
  102. if ($group->getUri() != $localUri) {
  103. $group->mainpage = common_local_url('showgroup',
  104. array('nickname' => $group->nickname));
  105. }
  106. $group->update($orig);
  107. printfv("DONE.");
  108. } catch (Exception $e) {
  109. echo "Can't update avatars for group " . $group->nickname . ": ". $e->getMessage();
  110. }
  111. }
  112. }
  113. }
  114. main();