update-profile-data.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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', dirname(__DIR__, 3));
  22. define('PUBLICDIR', INSTALLDIR . DIRECTORY_SEPARATOR . 'public');
  23. $longoptions = array('all', 'suspicious', 'quiet');
  24. $helptext = <<<END_OF_HELP
  25. update-profile-data.php [options] [http://example.com/profile/url]
  26. Rerun profile discovery for the given OStatus remote profile, and save the
  27. updated profile data (nickname, fullname, avatar, bio, etc).
  28. Doesn't touch feed state.
  29. Can be used to clean up after breakages.
  30. Options:
  31. --all Run for all known OStatus profiles
  32. --suspicious Run for OStatus profiles with all-numeric nicknames
  33. (fixes 0.9.7 prerelease back-compatibility bug)
  34. END_OF_HELP;
  35. require_once INSTALLDIR.'/scripts/commandline.inc';
  36. function showProfileInfo(Ostatus_profile $oprofile)
  37. {
  38. if ($oprofile->isGroup()) {
  39. echo "group\n";
  40. } else {
  41. $profile = $oprofile->localProfile();
  42. foreach (array('nickname', 'fullname', 'bio', 'homepage', 'location') as $field) {
  43. print " $field: {$profile->$field}\n";
  44. }
  45. }
  46. echo "\n";
  47. }
  48. function fixProfile(Ostatus_profile $oprofile)
  49. {
  50. echo "Before:\n";
  51. showProfileInfo($oprofile);
  52. $feedurl = $oprofile->feeduri;
  53. $client = new HTTPClient();
  54. $response = $client->get($feedurl);
  55. if ($response->isOk()) {
  56. echo "Updating profile from feed: $feedurl\n";
  57. $dom = new DOMDocument();
  58. if ($dom->loadXML($response->getBody())) {
  59. if ($dom->documentElement->tagName !== 'feed') {
  60. echo " (no <feed> element in feed URL response; skipping)\n";
  61. return false;
  62. }
  63. $actorObj = ActivityUtils::getFeedAuthor($dom->documentElement);
  64. if ($actorObj) {
  65. $oprofile->updateFromActivityObject($actorObj);
  66. echo " (ok)\n";
  67. } else {
  68. echo " (no author on feed; skipping)\n";
  69. return false;
  70. }
  71. } else {
  72. echo " (bad feed; skipping)\n";
  73. return false;
  74. }
  75. } else {
  76. echo "Failed feed fetch: {$response->getStatus()} for $feedurl\n";
  77. return false;
  78. }
  79. echo "After:\n";
  80. showProfileInfo($oprofile);
  81. return true;
  82. }
  83. $ok = true;
  84. $validate = new Validate();
  85. if (have_option('all')) {
  86. $oprofile = new Ostatus_profile();
  87. $oprofile->find();
  88. echo "Found $oprofile->N profiles:\n\n";
  89. while ($oprofile->fetch()) {
  90. try {
  91. $ok = fixProfile($oprofile) && $ok;
  92. } catch (Exception $e) {
  93. $ok = false;
  94. echo "Failed on URI=="._ve($oprofile->uri).": {$e->getMessage()}\n";
  95. }
  96. }
  97. } elseif (have_option('suspicious')) {
  98. $oprofile = new Ostatus_profile();
  99. $oprofile->joinAdd(['profile_id', 'profile:id']);
  100. $oprofile->whereAdd("CHAR_LENGTH(nickname) = 1 AND nickname BETWEEN '0' AND '9'");
  101. $oprofile->find();
  102. echo "Found $oprofile->N matching profiles:\n\n";
  103. while ($oprofile->fetch()) {
  104. try {
  105. $ok = fixProfile($oprofile) && $ok;
  106. } catch (Exception $e) {
  107. $ok = false;
  108. echo "Failed on URI=="._ve($oprofile->uri).": {$e->getMessage()}\n";
  109. }
  110. }
  111. } elseif (!empty($args[0]) && $validate->uri($args[0])) {
  112. $uri = $args[0];
  113. $oprofile = Ostatus_profile::getKV('uri', $uri);
  114. if (!$oprofile instanceof Ostatus_profile) {
  115. print "No OStatus remote profile known for URI $uri\n";
  116. return false;
  117. }
  118. try {
  119. $ok = fixProfile($oprofile) && $ok;
  120. } catch (Exception $e) {
  121. $ok = false;
  122. echo "Failed on URI=="._ve($oprofile->uri).": {$e->getMessage()}\n";
  123. }
  124. } else {
  125. print "$helptext";
  126. $ok = false;
  127. }
  128. exit($ok ? 0 : 1);