feedimporter.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2010, StatusNet, Inc.
  5. *
  6. * Importer for feeds of activities
  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. * Importer for feeds of activities
  37. *
  38. * Takes an XML file representing a feed of activities and imports each
  39. * activity to the user in question.
  40. *
  41. * @category Account
  42. * @package StatusNet
  43. * @author Evan Prodromou <evan@status.net>
  44. * @copyright 2010 StatusNet, Inc.
  45. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  46. * @link http://status.net/
  47. */
  48. class FeedImporter extends QueueHandler
  49. {
  50. /**
  51. * Transport identifier
  52. *
  53. * @return string identifier for this queue handler
  54. */
  55. public function transport()
  56. {
  57. return 'feedimp';
  58. }
  59. function handle($data)
  60. {
  61. list($user, $xml, $trusted) = $data;
  62. try {
  63. $doc = DOMDocument::loadXML($xml);
  64. $feed = $doc->documentElement;
  65. if ($feed->namespaceURI != Activity::ATOM ||
  66. $feed->localName != 'feed') {
  67. // TRANS: Client exception thrown when an imported feed is not an Atom feed.
  68. throw new ClientException(_("Not an Atom feed."));
  69. }
  70. $author = ActivityUtils::getFeedAuthor($feed);
  71. if (empty($author)) {
  72. // TRANS: Client exception thrown when an imported feed does not have an author.
  73. throw new ClientException(_("No author in the feed."));
  74. }
  75. if (empty($user)) {
  76. if ($trusted) {
  77. $user = $this->userFromAuthor($author);
  78. } else {
  79. // TRANS: Client exception thrown when an imported feed does not have an author that
  80. // TRANS: can be associated with a user.
  81. throw new ClientException(_("Cannot import without a user."));
  82. }
  83. }
  84. $activities = $this->getActivities($feed);
  85. $qm = QueueManager::get();
  86. foreach ($activities as $activity) {
  87. $qm->enqueue(array($user, $author, $activity, $trusted), 'actimp');
  88. }
  89. } catch (ClientException $ce) {
  90. common_log(LOG_WARNING, $ce->getMessage());
  91. return true;
  92. } catch (ServerException $se) {
  93. common_log(LOG_ERR, $ce->getMessage());
  94. return false;
  95. } catch (Exception $e) {
  96. common_log(LOG_ERR, $ce->getMessage());
  97. return false;
  98. }
  99. }
  100. function getActivities($feed)
  101. {
  102. $entries = $feed->getElementsByTagNameNS(Activity::ATOM, 'entry');
  103. $activities = array();
  104. for ($i = 0; $i < $entries->length; $i++) {
  105. $activities[] = new Activity($entries->item($i));
  106. }
  107. usort($activities, array("FeedImporter", "activitySort"));
  108. return $activities;
  109. }
  110. /**
  111. * Sort activities oldest-first
  112. */
  113. static function activitySort($a, $b)
  114. {
  115. if ($a->time == $b->time) {
  116. return 0;
  117. } else if ($a->time < $b->time) {
  118. return -1;
  119. } else {
  120. return 1;
  121. }
  122. }
  123. function userFromAuthor($author)
  124. {
  125. $user = User::getKV('uri', $author->id);
  126. if (empty($user)) {
  127. $attrs =
  128. array('nickname' => Ostatus_profile::getActivityObjectNickname($author),
  129. 'uri' => $author->id);
  130. $user = User::register($attrs);
  131. }
  132. $profile = $user->getProfile();
  133. Ostatus_profile::updateProfile($profile, $author);
  134. // @todo FIXME: Update avatar
  135. return $user;
  136. }
  137. }