update-profile.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 PuSH 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. if (empty($args[0]) || !Validate::uri($args[0])) {
  29. print "$helptext";
  30. exit(1);
  31. }
  32. $uri = $args[0];
  33. $oprofile = Ostatus_profile::getKV('uri', $uri);
  34. if (!$oprofile) {
  35. print "No OStatus remote profile known for URI $uri\n";
  36. exit(1);
  37. }
  38. print "Old profile state for $oprofile->uri\n";
  39. showProfile($oprofile);
  40. print "\n";
  41. print "Re-running feed discovery for profile URL $oprofile->uri\n";
  42. // @fixme will bork where the URI isn't the profile URL for now
  43. $discover = new FeedDiscovery();
  44. $feedurl = $discover->discoverFromURL($oprofile->uri);
  45. $huburi = $discover->getHubLink();
  46. $salmonuri = $discover->getAtomLink(Salmon::REL_SALMON)
  47. ?: $discover->getAtomLink(Salmon::NS_REPLIES);
  48. print " Feed URL: $feedurl\n";
  49. print " Hub URL: $huburi\n";
  50. print " Salmon URL: $salmonuri\n";
  51. if ($feedurl != $oprofile->feeduri || $salmonuri != $oprofile->salmonuri) {
  52. print "\n";
  53. print "Updating...\n";
  54. // @fixme update keys :P
  55. #$orig = clone($oprofile);
  56. #$oprofile->feeduri = $feedurl;
  57. #$oprofile->salmonuri = $salmonuri;
  58. #$ok = $oprofile->update($orig);
  59. $ok = $oprofile->query('UPDATE ostatus_profile SET ' .
  60. 'feeduri=\'' . $oprofile->escape($feedurl) . '\',' .
  61. 'salmonuri=\'' . $oprofile->escape($salmonuri) . '\' ' .
  62. 'WHERE uri=\'' . $oprofile->escape($uri) . '\'');
  63. if (!$ok) {
  64. print "Failed to update profile record...\n";
  65. exit(1);
  66. }
  67. $oprofile->decache();
  68. } else {
  69. print "\n";
  70. print "Ok, ostatus_profile record unchanged.\n\n";
  71. }
  72. $sub = FeedSub::ensureFeed($feedurl);
  73. if ($huburi != $sub->huburi) {
  74. print "\n";
  75. print "Updating hub record for feed; was $sub->huburi\n";
  76. $orig = clone($sub);
  77. $sub->huburi = $huburi;
  78. $ok = $sub->update($orig);
  79. if (!$ok) {
  80. print "Failed to update sub record...\n";
  81. exit(1);
  82. }
  83. } else {
  84. print "\n";
  85. print "Feed record ok, not changing.\n\n";
  86. }
  87. echo "\n";
  88. echo "Pinging hub {$sub->huburi} with new subscription for {$sub->uri}\n";
  89. try {
  90. $sub->subscribe();
  91. echo "ok\n";
  92. } catch (Exception $e) {
  93. echo 'Could not confirm. '.get_class($e).': '.$e->getMessage()."\n";
  94. }
  95. $o2 = Ostatus_profile::getKV('uri', $uri);
  96. print "\n";
  97. print "New profile state:\n";
  98. showProfile($o2);
  99. print "\n";
  100. print "New feed state:\n";
  101. $sub2 = FeedSub::ensureFeed($feedurl);
  102. showSub($sub2);
  103. function showProfile($oprofile)
  104. {
  105. print " Feed URL: $oprofile->feeduri\n";
  106. print " Salmon URL: $oprofile->salmonuri\n";
  107. print " Avatar URL: $oprofile->avatar\n";
  108. print " Profile ID: $oprofile->profile_id\n";
  109. print " Group ID: $oprofile->group_id\n";
  110. print " Record created: $oprofile->created\n";
  111. print " Record modified: $oprofile->modified\n";
  112. }
  113. function showSub($sub)
  114. {
  115. print " Subscription state: $sub->sub_state\n";
  116. print " Verify token: $sub->verify_token\n";
  117. print " Signature secret: $sub->secret\n";
  118. print " Sub start date: $sub->sub_start\n";
  119. print " Record created: $sub->created\n";
  120. print " Record modified: $sub->modified\n";
  121. }