update-profile-data.php 4.4 KB

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