update-profile-data.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. if (have_option('all')) {
  89. $oprofile = new Ostatus_profile();
  90. $oprofile->find();
  91. echo "Found $oprofile->N profiles:\n\n";
  92. while ($oprofile->fetch()) {
  93. $ok = fixProfile($oprofile->uri) && $ok;
  94. }
  95. } else if (have_option('suspicious')) {
  96. $oprofile = new Ostatus_profile();
  97. $oprofile->joinAdd(array('profile_id', 'profile:id'));
  98. $oprofile->whereAdd("nickname rlike '^[0-9]$'");
  99. $oprofile->find();
  100. echo "Found $oprofile->N matching profiles:\n\n";
  101. while ($oprofile->fetch()) {
  102. $ok = fixProfile($oprofile->uri) && $ok;
  103. }
  104. } else if (!empty($args[0]) && Validate::uri($args[0])) {
  105. $uri = $args[0];
  106. $ok = fixProfile($uri);
  107. } else {
  108. print "$helptext";
  109. $ok = false;
  110. }
  111. exit($ok ? 0 : 1);