update-profile.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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->escape($feedurl)}',
  90. salmonuri = '{$oprofile->escape($salmonuri)}'
  91. WHERE uri = '{$oprofile->escape($uri)}'
  92. END
  93. );
  94. if (!$ok) {
  95. print "Failed to update profile record...\n";
  96. exit(1);
  97. }
  98. $oprofile->decache();
  99. } else {
  100. print "\n";
  101. print "Ok, ostatus_profile record unchanged.\n\n";
  102. }
  103. $sub = FeedSub::ensureFeed($feedurl);
  104. if ($huburi != $sub->huburi) {
  105. print "\n";
  106. print "Updating hub record for feed; was $sub->huburi\n";
  107. $orig = clone($sub);
  108. $sub->huburi = $huburi;
  109. $ok = $sub->update($orig);
  110. if (!$ok) {
  111. print "Failed to update sub record...\n";
  112. exit(1);
  113. }
  114. } else {
  115. print "\n";
  116. print "Feed record ok, not changing.\n\n";
  117. }
  118. echo "\n";
  119. echo "Pinging hub {$sub->huburi} with new subscription for {$sub->uri}\n";
  120. try {
  121. $sub->subscribe();
  122. echo "ok\n";
  123. } catch (Exception $e) {
  124. echo 'Could not confirm. '.get_class($e).': '.$e->getMessage()."\n";
  125. }
  126. $o2 = Ostatus_profile::getKV('uri', $uri);
  127. print "\n";
  128. print "New profile state:\n";
  129. showProfile($o2);
  130. print "\n";
  131. print "New feed state:\n";
  132. $sub2 = FeedSub::ensureFeed($feedurl);
  133. showSub($sub2);
  134. function showProfile($oprofile)
  135. {
  136. print " Feed URL: $oprofile->feeduri\n";
  137. print " Salmon URL: $oprofile->salmonuri\n";
  138. print " Avatar URL: $oprofile->avatar\n";
  139. print " Profile ID: $oprofile->profile_id\n";
  140. print " Group ID: $oprofile->group_id\n";
  141. print " Record created: $oprofile->created\n";
  142. print " Record modified: $oprofile->modified\n";
  143. }
  144. function showSub($sub)
  145. {
  146. print " Subscription state: $sub->sub_state\n";
  147. print " Signature secret: $sub->secret\n";
  148. print " Sub start date: $sub->sub_start\n";
  149. print " Record created: $sub->created\n";
  150. print " Record modified: $sub->modified\n";
  151. }