update-profile.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #!/usr/bin/env php
  2. <?php
  3. // This file is part of GNU social - https://www.gnu.org/software/social
  4. //
  5. // GNU social is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Affero General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // GNU social is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Affero General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  17. /**
  18. * @package GNUsocial
  19. * @copyright 2010 StatusNet, Inc.
  20. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  21. */
  22. define('INSTALLDIR', dirname(__DIR__, 3));
  23. define('PUBLICDIR', INSTALLDIR . DIRECTORY_SEPARATOR . 'public');
  24. $helptext = <<<END_OF_HELP
  25. update-profile.php [options] http://example.com/profile/url
  26. Rerun profile and feed info discovery for the given OStatus remote profile,
  27. and reinitialize its WebSub subscription for the given feed. This may help get
  28. things restarted if the hub or feed URLs have changed for the profile.
  29. END_OF_HELP;
  30. require_once INSTALLDIR.'/scripts/commandline.inc';
  31. $validate = new Validate();
  32. if (empty($args[0]) || !$validate->uri($args[0])) {
  33. print "$helptext";
  34. exit(1);
  35. }
  36. $uri = $args[0];
  37. $oprofile = Ostatus_profile::getKV('uri', $uri);
  38. if (!$oprofile) {
  39. print "No OStatus remote profile known for URI $uri\n";
  40. exit(1);
  41. }
  42. print "Old profile state for $oprofile->uri\n";
  43. showProfile($oprofile);
  44. print "\n";
  45. print "Re-running feed discovery for profile URL $oprofile->uri\n";
  46. $feedurl = null;
  47. $salmonuri = null;
  48. // @fixme will bork where the URI isn't the profile URL for now
  49. $discover = new FeedDiscovery();
  50. try {
  51. $feedurl = $discover->discoverFromURL($oprofile->uri);
  52. $salmonuri = $discover->getAtomLink(Salmon::REL_SALMON)
  53. ?: $discover->getAtomLink(Salmon::NS_REPLIES); // NS_REPLIES is deprecated
  54. if (empty($salmonuri)) {
  55. throw new FeedSubNoSalmonException('No salmon upstream URI was found');
  56. }
  57. } catch (FeedSubException $e) {
  58. $acct = $oprofile->localProfile()->getAcctUri();
  59. print "Could not discover feeds HTML response, trying reconstructed acct URI: {$acct}\n";
  60. $disco = new Discovery();
  61. $xrd = $disco->lookup($acct);
  62. $hints = DiscoveryHints::fromXRD($xrd);
  63. if (array_key_exists('feedurl', $hints)) {
  64. // Prefer $hints['feedurl'] to current $feedurl value
  65. $feedurl = $hints['feedurl'];
  66. } elseif (empty($feedurl)) {
  67. // if (empty($feedurl) && !array_key_exists('feedurl', $hints))
  68. throw new FeedSubNoFeedException($acct);
  69. }
  70. $salmonuri = array_key_exists('salmon', $hints) ? $hints['salmon'] : $salmonuri;
  71. // get the hub data too and put it in the FeedDiscovery object
  72. $discover->discoverFromFeedUrl($feedurl);
  73. }
  74. $huburi = $discover->getHubLink();
  75. print " Feed URL: $feedurl\n";
  76. print " Hub URL: $huburi\n";
  77. print " Salmon URL: $salmonuri\n";
  78. if ($feedurl != $oprofile->feeduri || $salmonuri != $oprofile->salmonuri) {
  79. print "\n";
  80. print "Updating...\n";
  81. // @fixme update keys :P
  82. //$orig = clone($oprofile);
  83. //$oprofile->feeduri = $feedurl;
  84. //$oprofile->salmonuri = $salmonuri;
  85. //$ok = $oprofile->update($orig);
  86. $ok = $oprofile->query(
  87. <<<END
  88. UPDATE ostatus_profile
  89. SET feeduri = {$oprofile->_quote($feedurl)},
  90. salmonuri = {$oprofile->_quote($salmonuri)},
  91. modified = CURRENT_TIMESTAMP
  92. WHERE uri = {$oprofile->_quote($uri)}
  93. END
  94. );
  95. if (!$ok) {
  96. print "Failed to update profile record...\n";
  97. exit(1);
  98. }
  99. $oprofile->decache();
  100. } else {
  101. print "\n";
  102. print "Ok, ostatus_profile record unchanged.\n\n";
  103. }
  104. $sub = FeedSub::ensureFeed($feedurl);
  105. if ($huburi != $sub->huburi) {
  106. print "\n";
  107. print "Updating hub record for feed; was $sub->huburi\n";
  108. $orig = clone($sub);
  109. $sub->huburi = $huburi;
  110. $ok = $sub->update($orig);
  111. if (!$ok) {
  112. print "Failed to update sub record...\n";
  113. exit(1);
  114. }
  115. } else {
  116. print "\n";
  117. print "Feed record ok, not changing.\n\n";
  118. }
  119. echo "\n";
  120. echo "Pinging hub {$sub->huburi} with new subscription for {$sub->uri}\n";
  121. try {
  122. $sub->subscribe();
  123. echo "ok\n";
  124. } catch (Exception $e) {
  125. echo 'Could not confirm. '.get_class($e).': '.$e->getMessage()."\n";
  126. }
  127. $o2 = Ostatus_profile::getKV('uri', $uri);
  128. print "\n";
  129. print "New profile state:\n";
  130. showProfile($o2);
  131. print "\n";
  132. print "New feed state:\n";
  133. $sub2 = FeedSub::ensureFeed($feedurl);
  134. showSub($sub2);
  135. function showProfile($oprofile)
  136. {
  137. print " Feed URL: $oprofile->feeduri\n";
  138. print " Salmon URL: $oprofile->salmonuri\n";
  139. print " Avatar URL: $oprofile->avatar\n";
  140. print " Profile ID: $oprofile->profile_id\n";
  141. print " Group ID: $oprofile->group_id\n";
  142. print " Record created: $oprofile->created\n";
  143. print " Record modified: $oprofile->modified\n";
  144. }
  145. function showSub($sub)
  146. {
  147. print " Subscription state: $sub->sub_state\n";
  148. print " Signature secret: $sub->secret\n";
  149. print " Sub start date: $sub->sub_start\n";
  150. print " Record created: $sub->created\n";
  151. print " Record modified: $sub->modified\n";
  152. }