accountmover.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2010, StatusNet, Inc.
  5. *
  6. * A class for moving an account to a new server
  7. *
  8. * PHP version 5
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. * @category Account
  24. * @package StatusNet
  25. * @author Evan Prodromou <evan@status.net>
  26. * @copyright 2010 StatusNet, Inc.
  27. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  28. * @link http://status.net/
  29. */
  30. if (!defined('STATUSNET')) {
  31. // This check helps protect against security problems;
  32. // your code file can't be executed directly from the web.
  33. exit(1);
  34. }
  35. /**
  36. * Moves an account from this server to another
  37. *
  38. * @category Account
  39. * @package StatusNet
  40. * @author Evan Prodromou <evan@status.net>
  41. * @copyright 2010 StatusNet, Inc.
  42. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  43. * @link http://status.net/
  44. */
  45. class AccountMover extends QueueHandler
  46. {
  47. function transport()
  48. {
  49. return 'acctmove';
  50. }
  51. function handle($object)
  52. {
  53. list($user, $remote, $password) = $object;
  54. $remote = Discovery::normalize($remote);
  55. $oprofile = Ostatus_profile::ensureProfileURI($remote);
  56. if (empty($oprofile)) {
  57. // TRANS: Exception thrown when an account could not be located when it should be moved.
  58. // TRANS: %s is the remote site.
  59. throw new Exception(sprintf(_("Cannot locate account %s."),$remote));
  60. }
  61. list($svcDocUrl, $username) = self::getServiceDocument($remote);
  62. $sink = new ActivitySink($svcDocUrl, $username, $password);
  63. $this->log(LOG_INFO,
  64. "Moving user {$user->nickname} ".
  65. "to {$remote}.");
  66. $stream = new UserActivityStream($user);
  67. // Reverse activities to run in correct chron order
  68. $acts = array_reverse($stream->activities);
  69. $this->log(LOG_INFO,
  70. "Got ".count($acts)." activities ".
  71. "for {$user->nickname}.");
  72. $qm = QueueManager::get();
  73. foreach ($acts as $act) {
  74. $qm->enqueue(array($act, $sink, $user->getUri(), $remote), 'actmove');
  75. }
  76. $this->log(LOG_INFO,
  77. "Finished moving user {$user->nickname} ".
  78. "to {$remote}.");
  79. }
  80. static function getServiceDocument($remote)
  81. {
  82. $discovery = new Discovery();
  83. $xrd = $discovery->lookup($remote);
  84. if (empty($xrd)) {
  85. // TRANS: Exception thrown when a service document could not be located account move.
  86. // TRANS: %s is the remote site.
  87. throw new Exception(sprintf(_("Cannot find XRD for %s."),$remote));
  88. }
  89. $svcDocUrl = null;
  90. $username = null;
  91. $link = $xrd->links->get('http://apinamespace.org/atom', 'application/atomsvc+xml');
  92. if (!is_null($link)) {
  93. $svcDocUrl = $link->href;
  94. if (isset($link['http://apinamespace.org/atom/username'])) {
  95. $username = $link['http://apinamespace.org/atom/username'];
  96. }
  97. }
  98. if (empty($svcDocUrl)) {
  99. // TRANS: Exception thrown when an account could not be located when it should be moved.
  100. // TRANS: %s is the remote site.
  101. throw new Exception(sprintf(_("No AtomPub API service for %s."),$remote));
  102. }
  103. return array($svcDocUrl, $username);
  104. }
  105. /**
  106. * Log some data
  107. *
  108. * Add a header for our class so we know who did it.
  109. *
  110. * @param int $level Log level, like LOG_ERR or LOG_INFO
  111. * @param string $message Message to log
  112. *
  113. * @return void
  114. */
  115. protected function log($level, $message)
  116. {
  117. common_log($level, "AccountMover: " . $message);
  118. }
  119. }