update-profile-data.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. try {
  40. foreach (array('nickname', 'fullname', 'bio', 'homepage', 'location') as $field) {
  41. print " $field: {$profile->$field}\n";
  42. }
  43. } catch (NoProfileException $e) {
  44. print "local profile not found";
  45. }
  46. }
  47. echo "\n";
  48. }
  49. function fixProfile($uri) {
  50. $oprofile = Ostatus_profile::getKV('uri', $uri);
  51. if (!$oprofile) {
  52. print "No OStatus remote profile known for URI $uri\n";
  53. return false;
  54. }
  55. echo "Before:\n";
  56. showProfileInfo($oprofile);
  57. $feedurl = $oprofile->feeduri;
  58. $client = new HttpClient();
  59. $response = $client->get($feedurl);
  60. if ($response->isOk()) {
  61. echo "Updating profile from feed: $feedurl\n";
  62. $dom = new DOMDocument();
  63. if ($dom->loadXML($response->getBody())) {
  64. $feed = $dom->documentElement;
  65. $entries = $dom->getElementsByTagNameNS(Activity::ATOM, 'entry');
  66. if ($entries->length) {
  67. $entry = $entries->item(0);
  68. $activity = new Activity($entry, $feed);
  69. $oprofile->checkAuthorship($activity);
  70. echo " (ok)\n";
  71. } else {
  72. echo " (no entry; skipping)\n";
  73. return false;
  74. }
  75. } else {
  76. echo " (bad feed; skipping)\n";
  77. return false;
  78. }
  79. } else {
  80. echo "Failed feed fetch: {$response->getStatus()} for $feedurl\n";
  81. return false;
  82. }
  83. echo "After:\n";
  84. showProfileInfo($oprofile);
  85. return true;
  86. }
  87. $ok = true;
  88. $validate = new Validate();
  89. if (have_option('all')) {
  90. $oprofile = new Ostatus_profile();
  91. $oprofile->find();
  92. echo "Found $oprofile->N profiles:\n\n";
  93. while ($oprofile->fetch()) {
  94. try {
  95. $ok = fixProfile($oprofile->uri) && $ok;
  96. } catch (Exception $e) {
  97. $ok = false;
  98. echo "Failed on URI=="._ve($oprofile->uri).": {$e->getMessage()}\n";
  99. }
  100. }
  101. } else if (have_option('suspicious')) {
  102. $oprofile = new Ostatus_profile();
  103. $oprofile->joinAdd(array('profile_id', 'profile:id'));
  104. $oprofile->whereAdd("nickname rlike '^[0-9]$'");
  105. $oprofile->find();
  106. echo "Found $oprofile->N matching profiles:\n\n";
  107. while ($oprofile->fetch()) {
  108. try {
  109. $ok = fixProfile($oprofile->uri) && $ok;
  110. } catch (Exception $e) {
  111. $ok = false;
  112. echo "Failed on URI=="._ve($oprofile->uri).": {$e->getMessage()}\n";
  113. }
  114. }
  115. } else if (!empty($args[0]) && $validate->uri($args[0])) {
  116. $uri = $args[0];
  117. try {
  118. $ok = fixProfile($oprofile->uri) && $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);