Example #1
0
 /**
  * @param array $source
  * @return array
  */
 protected function fetchSource(array $source)
 {
     $fetcher = new Fetcher();
     $fetcher->setClient(new Client());
     $fetcher->setTimeout($this->app->config('fetcher.timeout'));
     $content = $fetcher->get($source['uri']);
     if (!empty($content)) {
         $defaultWorkerClass = $this->app->config('worker.default');
         /**
          * @var $worker \Nogo\Feedbox\Feed\Worker
          */
         $worker = new $defaultWorkerClass();
         $worker->setSanitizer($this->sanitizer);
         $worker->setContent($content);
         try {
             $items = $worker->execute();
         } catch (\Exception $e) {
             $items = null;
         }
         if ($items != null) {
             foreach ($items as $item) {
                 if (isset($item['uid'])) {
                     $dbItem = $this->itemRepository->findBy('uid', $item['uid']);
                     if (!empty($dbItem)) {
                         if ($item['content'] !== $dbItem['content'] || $item['title'] !== $dbItem['title']) {
                             $item['id'] = $dbItem['id'];
                             $item['starred'] = $dbItem['starred'];
                             $item['created_at'] = $dbItem['created_at'];
                         } else {
                             continue;
                         }
                     }
                 }
                 $item['source_id'] = $source['id'];
                 $this->itemRepository->persist($item);
             }
         }
         $source['last_update'] = date('Y-m-d H:i:s');
         if (empty($source['period'])) {
             $source['period'] = $worker->getUpdateInterval();
         }
         $source['errors'] = $worker->getErrors();
     } else {
         $source['errors'] = $fetcher->getError();
     }
     $source['unread'] = $this->itemRepository->countSourceUnread([$source['id']]);
     $this->sourceRepository->persist($source);
     // update tag unread counter
     if (!empty($source['tag_id'])) {
         $tag = $this->tagRepository->find($source['tag_id']);
         if ($tag) {
             $tag['unread'] = $this->sourceRepository->countTagUnread([$tag['id']]);
             $this->tagRepository->persist($tag);
         }
     }
     // clean up double uids in this source
     $uids = $this->itemRepository->findDoubleUid($source['id']);
     foreach ($uids as $uid) {
         $items = $this->itemRepository->findAllBy('uid', $uid);
         for ($i = 1; $i < count($items); $i++) {
             $this->itemRepository->remove($items[$i]['id']);
         }
     }
     return $source;
 }
Example #2
0
                 $itemRepository->persist($item);
             }
             $source['last_update'] = date('Y-m-d H:i:s');
             if (empty($source['period'])) {
                 $source['period'] = $worker->getUpdateInterval();
             }
         }
         $source['errors'] = $worker->getErrors();
     } else {
         $error = true;
         $source['errors'] = $fetcher->getError();
     }
 }
 // update source unread counter
 $count = $source['unread'];
 $source['unread'] = $itemRepository->countSourceUnread([$source['id']]);
 $sourceRepository->setUserScope($user_id);
 $sourceRepository->persist($source);
 // update tag unread counter
 if (!empty($source['tag_id'])) {
     $tag = $tagRepository->find($source['tag_id']);
     if ($tag) {
         $tag['unread'] = $sourceRepository->countTagUnread([$tag['id']]);
         $tagRepository->setUserScope($user_id);
         $tagRepository->persist($tag);
     }
 }
 if ($config['debug']) {
     if ($error) {
         echo sprintf("%s\n", $source['errors']);
     } else {