update-profile.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. $helptext = <<<END_OF_HELP
  22. update-profile.php [options] http://example.com/profile/url
  23. Rerun profile and feed info discovery for the given OStatus remote profile,
  24. and reinitialize its WebSub subscription for the given feed. This may help get
  25. things restarted if the hub or feed URLs have changed for the profile.
  26. END_OF_HELP;
  27. require_once INSTALLDIR.'/scripts/commandline.inc';
  28. $validate = new Validate();
  29. if (empty($args[0]) || !$validate->uri($args[0])) {
  30. print "$helptext";
  31. exit(1);
  32. }
  33. $uri = $args[0];
  34. $oprofile = Ostatus_profile::getKV('uri', $uri);
  35. if (!$oprofile) {
  36. print "No OStatus remote profile known for URI $uri\n";
  37. exit(1);
  38. }
  39. print "Old profile state for $oprofile->uri\n";
  40. showProfile($oprofile);
  41. print "\n";
  42. print "Re-running feed discovery for profile URL $oprofile->uri\n";
  43. $feedurl = null;
  44. $salmonuri = null;
  45. // @fixme will bork where the URI isn't the profile URL for now
  46. $discover = new FeedDiscovery();
  47. try {
  48. $feedurl = $discover->discoverFromURL($oprofile->uri);
  49. $salmonuri = $discover->getAtomLink(Salmon::REL_SALMON)
  50. ?: $discover->getAtomLink(Salmon::NS_REPLIES); // NS_REPLIES is deprecated
  51. if (empty($salmonuri) ) {
  52. throw new FeedSubNoSalmonException('No salmon upstream URI was found');
  53. }
  54. } catch (FeedSubException $e) {
  55. $acct = $oprofile->localProfile()->getAcctUri();
  56. print "Could not discover feeds HTML response, trying reconstructed acct URI: {$acct}\n";
  57. $disco = new Discovery();
  58. $xrd = $disco->lookup($acct);
  59. $hints = DiscoveryHints::fromXRD($xrd);
  60. if (empty($feedurl) && !array_key_exists('feedurl', $hints)) {
  61. throw new FeedSubNoFeedException($acct);
  62. }
  63. $feedurl = $feedurl ?: $hints['feedurl'];
  64. $salmonuri = array_key_exists('salmon', $hints) ? $hints['salmon'] : $salmonuri;
  65. // get the hub data too and put it in the FeedDiscovery object
  66. $discover->discoverFromFeedUrl($feedurl);
  67. }
  68. $huburi = $discover->getHubLink();
  69. print " Feed URL: $feedurl\n";
  70. print " Hub URL: $huburi\n";
  71. print " Salmon URL: $salmonuri\n";
  72. if ($feedurl != $oprofile->feeduri || $salmonuri != $oprofile->salmonuri) {
  73. print "\n";
  74. print "Updating...\n";
  75. // @fixme update keys :P
  76. #$orig = clone($oprofile);
  77. #$oprofile->feeduri = $feedurl;
  78. #$oprofile->salmonuri = $salmonuri;
  79. #$ok = $oprofile->update($orig);
  80. $ok = $oprofile->query('UPDATE ostatus_profile SET ' .
  81. 'feeduri=\'' . $oprofile->escape($feedurl) . '\',' .
  82. 'salmonuri=\'' . $oprofile->escape($salmonuri) . '\' ' .
  83. 'WHERE uri=\'' . $oprofile->escape($uri) . '\'');
  84. if (!$ok) {
  85. print "Failed to update profile record...\n";
  86. exit(1);
  87. }
  88. $oprofile->decache();
  89. } else {
  90. print "\n";
  91. print "Ok, ostatus_profile record unchanged.\n\n";
  92. }
  93. $sub = FeedSub::ensureFeed($feedurl);
  94. if ($huburi != $sub->huburi) {
  95. print "\n";
  96. print "Updating hub record for feed; was $sub->huburi\n";
  97. $orig = clone($sub);
  98. $sub->huburi = $huburi;
  99. $ok = $sub->update($orig);
  100. if (!$ok) {
  101. print "Failed to update sub record...\n";
  102. exit(1);
  103. }
  104. } else {
  105. print "\n";
  106. print "Feed record ok, not changing.\n\n";
  107. }
  108. echo "\n";
  109. echo "Pinging hub {$sub->huburi} with new subscription for {$sub->uri}\n";
  110. try {
  111. $sub->subscribe();
  112. echo "ok\n";
  113. } catch (Exception $e) {
  114. echo 'Could not confirm. '.get_class($e).': '.$e->getMessage()."\n";
  115. }
  116. $o2 = Ostatus_profile::getKV('uri', $uri);
  117. print "\n";
  118. print "New profile state:\n";
  119. showProfile($o2);
  120. print "\n";
  121. print "New feed state:\n";
  122. $sub2 = FeedSub::ensureFeed($feedurl);
  123. showSub($sub2);
  124. function showProfile($oprofile)
  125. {
  126. print " Feed URL: $oprofile->feeduri\n";
  127. print " Salmon URL: $oprofile->salmonuri\n";
  128. print " Avatar URL: $oprofile->avatar\n";
  129. print " Profile ID: $oprofile->profile_id\n";
  130. print " Group ID: $oprofile->group_id\n";
  131. print " Record created: $oprofile->created\n";
  132. print " Record modified: $oprofile->modified\n";
  133. }
  134. function showSub($sub)
  135. {
  136. print " Subscription state: $sub->sub_state\n";
  137. print " Verify token: $sub->verify_token\n";
  138. print " Signature secret: $sub->secret\n";
  139. print " Sub start date: $sub->sub_start\n";
  140. print " Record created: $sub->created\n";
  141. print " Record modified: $sub->modified\n";
  142. }