123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- <?php
- if (!defined('STATUSNET')) {
-
-
- exit(1);
- }
- class FeedImporter extends QueueHandler
- {
-
- public function transport()
- {
- return 'feedimp';
- }
- function handle($data)
- {
- list($user, $xml, $trusted) = $data;
- try {
- $doc = DOMDocument::loadXML($xml);
- $feed = $doc->documentElement;
- if ($feed->namespaceURI != Activity::ATOM ||
- $feed->localName != 'feed') {
-
- throw new ClientException(_("Not an Atom feed."));
- }
- $author = ActivityUtils::getFeedAuthor($feed);
- if (empty($author)) {
-
- throw new ClientException(_("No author in the feed."));
- }
- if (empty($user)) {
- if ($trusted) {
- $user = $this->userFromAuthor($author);
- } else {
-
-
- throw new ClientException(_("Cannot import without a user."));
- }
- }
- $activities = $this->getActivities($feed);
- $qm = QueueManager::get();
- foreach ($activities as $activity) {
- $qm->enqueue(array($user, $author, $activity, $trusted), 'actimp');
- }
- } catch (ClientException $ce) {
- common_log(LOG_WARNING, $ce->getMessage());
- return true;
- } catch (ServerException $se) {
- common_log(LOG_ERR, $ce->getMessage());
- return false;
- } catch (Exception $e) {
- common_log(LOG_ERR, $ce->getMessage());
- return false;
- }
- }
- function getActivities($feed)
- {
- $entries = $feed->getElementsByTagNameNS(Activity::ATOM, 'entry');
- $activities = array();
- for ($i = 0; $i < $entries->length; $i++) {
- $activities[] = new Activity($entries->item($i));
- }
- usort($activities, array("FeedImporter", "activitySort"));
- return $activities;
- }
-
- static function activitySort($a, $b)
- {
- if ($a->time == $b->time) {
- return 0;
- } else if ($a->time < $b->time) {
- return -1;
- } else {
- return 1;
- }
- }
- function userFromAuthor($author)
- {
- $user = User::getKV('uri', $author->id);
- if (empty($user)) {
- $attrs =
- array('nickname' => Ostatus_profile::getActivityObjectNickname($author),
- 'uri' => $author->id);
- $user = User::register($attrs);
- }
- $profile = $user->getProfile();
- Ostatus_profile::updateProfile($profile, $author);
-
- return $user;
- }
- }
|