update-profile-data.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #!/usr/bin/env php
  2. <?php
  3. // This file is part of GNU social - https://www.gnu.org/software/social
  4. //
  5. // GNU social is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Affero General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // GNU social is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Affero General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  17. /**
  18. * @copyright 2010 StatusNet, Inc.
  19. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  20. */
  21. define('INSTALLDIR', realpath(dirname(__FILE__) . '/../../..'));
  22. $longoptions = array('all', 'suspicious', 'quiet');
  23. $helptext = <<<END_OF_HELP
  24. update-profile-data.php [options] [http://example.com/profile/url]
  25. Rerun profile discovery for the given OStatus remote profile, and save the
  26. updated profile data (nickname, fullname, avatar, bio, etc).
  27. Doesn't touch feed state.
  28. Can be used to clean up after breakages.
  29. Options:
  30. --all Run for all known OStatus profiles
  31. --suspicious Run for OStatus profiles with all-numeric nicknames
  32. (fixes 0.9.7 prerelease back-compatibility bug)
  33. END_OF_HELP;
  34. require_once INSTALLDIR.'/scripts/commandline.inc';
  35. function showProfileInfo(Ostatus_profile $oprofile)
  36. {
  37. if ($oprofile->isGroup()) {
  38. echo "group\n";
  39. } else {
  40. $profile = $oprofile->localProfile();
  41. foreach (array('nickname', 'fullname', 'bio', 'homepage', 'location') as $field) {
  42. print " $field: {$profile->$field}\n";
  43. }
  44. }
  45. echo "\n";
  46. }
  47. function fixProfile(Ostatus_profile $oprofile)
  48. {
  49. echo "Before:\n";
  50. showProfileInfo($oprofile);
  51. $feedurl = $oprofile->feeduri;
  52. $client = new HTTPClient();
  53. $response = $client->get($feedurl);
  54. if ($response->isOk()) {
  55. echo "Updating profile from feed: $feedurl\n";
  56. $dom = new DOMDocument();
  57. if ($dom->loadXML($response->getBody())) {
  58. if ($dom->documentElement->tagName !== 'feed') {
  59. echo " (no <feed> element in feed URL response; skipping)\n";
  60. return false;
  61. }
  62. $actorObj = ActivityUtils::getFeedAuthor($dom->documentElement);
  63. if ($actorObj) {
  64. $oprofile->updateFromActivityObject($actorObj);
  65. echo " (ok)\n";
  66. } else {
  67. echo " (no author on feed; skipping)\n";
  68. return false;
  69. }
  70. } else {
  71. echo " (bad feed; skipping)\n";
  72. return false;
  73. }
  74. } else {
  75. echo "Failed feed fetch: {$response->getStatus()} for $feedurl\n";
  76. return false;
  77. }
  78. echo "After:\n";
  79. showProfileInfo($oprofile);
  80. return true;
  81. }
  82. $ok = true;
  83. $validate = new Validate();
  84. if (have_option('all')) {
  85. $oprofile = new Ostatus_profile();
  86. $oprofile->find();
  87. echo "Found $oprofile->N profiles:\n\n";
  88. while ($oprofile->fetch()) {
  89. try {
  90. $ok = fixProfile($oprofile) && $ok;
  91. } catch (Exception $e) {
  92. $ok = false;
  93. echo "Failed on URI=="._ve($oprofile->uri).": {$e->getMessage()}\n";
  94. }
  95. }
  96. } elseif (have_option('suspicious')) {
  97. $oprofile = new Ostatus_profile();
  98. $oprofile->joinAdd(['profile_id', 'profile:id']);
  99. $oprofile->whereAdd("CHAR_LENGTH(nickname) = 1 AND nickname BETWEEN '0' AND '9'");
  100. $oprofile->find();
  101. echo "Found $oprofile->N matching profiles:\n\n";
  102. while ($oprofile->fetch()) {
  103. try {
  104. $ok = fixProfile($oprofile) && $ok;
  105. } catch (Exception $e) {
  106. $ok = false;
  107. echo "Failed on URI=="._ve($oprofile->uri).": {$e->getMessage()}\n";
  108. }
  109. }
  110. } elseif (!empty($args[0]) && $validate->uri($args[0])) {
  111. $uri = $args[0];
  112. $oprofile = Ostatus_profile::getKV('uri', $uri);
  113. if (!$oprofile instanceof Ostatus_profile) {
  114. print "No OStatus remote profile known for URI $uri\n";
  115. return false;
  116. }
  117. try {
  118. $ok = fixProfile($oprofile) && $ok;
  119. } catch (Exception $e) {
  120. $ok = false;
  121. echo "Failed on URI=="._ve($oprofile->uri).": {$e->getMessage()}\n";
  122. }
  123. } else {
  124. print "$helptext";
  125. $ok = false;
  126. }
  127. exit($ok ? 0 : 1);