update-profile.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 (array_key_exists('feedurl', $hints)) {
  61. // Prefer $hints['feedurl'] to current $feedurl value
  62. $feedurl = $hints['feedurl'];
  63. } elseif (empty($feedurl)) {
  64. // if (empty($feedurl) && !array_key_exists('feedurl', $hints))
  65. throw new FeedSubNoFeedException($acct);
  66. }
  67. $salmonuri = array_key_exists('salmon', $hints) ? $hints['salmon'] : $salmonuri;
  68. // get the hub data too and put it in the FeedDiscovery object
  69. $discover->discoverFromFeedUrl($feedurl);
  70. }
  71. $huburi = $discover->getHubLink();
  72. print " Feed URL: $feedurl\n";
  73. print " Hub URL: $huburi\n";
  74. print " Salmon URL: $salmonuri\n";
  75. if ($feedurl != $oprofile->feeduri || $salmonuri != $oprofile->salmonuri) {
  76. print "\n";
  77. print "Updating...\n";
  78. // @fixme update keys :P
  79. #$orig = clone($oprofile);
  80. #$oprofile->feeduri = $feedurl;
  81. #$oprofile->salmonuri = $salmonuri;
  82. #$ok = $oprofile->update($orig);
  83. $ok = $oprofile->query('UPDATE ostatus_profile SET ' .
  84. 'feeduri=\'' . $oprofile->escape($feedurl) . '\',' .
  85. 'salmonuri=\'' . $oprofile->escape($salmonuri) . '\' ' .
  86. 'WHERE uri=\'' . $oprofile->escape($uri) . '\'');
  87. if (!$ok) {
  88. print "Failed to update profile record...\n";
  89. exit(1);
  90. }
  91. $oprofile->decache();
  92. } else {
  93. print "\n";
  94. print "Ok, ostatus_profile record unchanged.\n\n";
  95. }
  96. $sub = FeedSub::ensureFeed($feedurl);
  97. if ($huburi != $sub->huburi) {
  98. print "\n";
  99. print "Updating hub record for feed; was $sub->huburi\n";
  100. $orig = clone($sub);
  101. $sub->huburi = $huburi;
  102. $ok = $sub->update($orig);
  103. if (!$ok) {
  104. print "Failed to update sub record...\n";
  105. exit(1);
  106. }
  107. } else {
  108. print "\n";
  109. print "Feed record ok, not changing.\n\n";
  110. }
  111. echo "\n";
  112. echo "Pinging hub {$sub->huburi} with new subscription for {$sub->uri}\n";
  113. try {
  114. $sub->subscribe();
  115. echo "ok\n";
  116. } catch (Exception $e) {
  117. echo 'Could not confirm. '.get_class($e).': '.$e->getMessage()."\n";
  118. }
  119. $o2 = Ostatus_profile::getKV('uri', $uri);
  120. print "\n";
  121. print "New profile state:\n";
  122. showProfile($o2);
  123. print "\n";
  124. print "New feed state:\n";
  125. $sub2 = FeedSub::ensureFeed($feedurl);
  126. showSub($sub2);
  127. function showProfile($oprofile)
  128. {
  129. print " Feed URL: $oprofile->feeduri\n";
  130. print " Salmon URL: $oprofile->salmonuri\n";
  131. print " Avatar URL: $oprofile->avatar\n";
  132. print " Profile ID: $oprofile->profile_id\n";
  133. print " Group ID: $oprofile->group_id\n";
  134. print " Record created: $oprofile->created\n";
  135. print " Record modified: $oprofile->modified\n";
  136. }
  137. function showSub($sub)
  138. {
  139. print " Subscription state: $sub->sub_state\n";
  140. print " Signature secret: $sub->secret\n";
  141. print " Sub start date: $sub->sub_start\n";
  142. print " Record created: $sub->created\n";
  143. print " Record modified: $sub->modified\n";
  144. }