Esempio n. 1
0
 public static function daoToEntry($listDAO)
 {
     $list = array();
     if (!is_array($listDAO)) {
         $listDAO = array($listDAO);
     }
     foreach ($listDAO as $key => $dao) {
         $entry = new FreshRSS_Entry($dao['id_feed'], $dao['guid'], $dao['title'], $dao['author'], $dao['content'], $dao['link'], $dao['date'], $dao['is_read'], $dao['is_favorite'], $dao['tags']);
         if (isset($dao['id'])) {
             $entry->_id($dao['id']);
         }
         $list[] = $entry;
     }
     unset($listDAO);
     return $list;
 }
 /**
  * This method import a JSON-based file (Google Reader format).
  *
  * @param string $article_file the JSON file content.
  * @param boolean $starred true if articles from the file must be starred.
  * @return boolean true if an error occured, false else.
  */
 private function importJson($article_file, $starred = false)
 {
     $article_object = json_decode($article_file, true);
     if (is_null($article_object)) {
         Minz_Log::warning('Try to import a non-JSON file');
         return true;
     }
     $is_read = FreshRSS_Context::$user_conf->mark_when['reception'] ? 1 : 0;
     $google_compliant = strpos($article_object['id'], 'com.google') !== false;
     $error = false;
     $article_to_feed = array();
     $nb_feeds = count($this->feedDAO->listFeeds());
     $limits = FreshRSS_Context::$system_conf->limits;
     // First, we check feeds of articles are in DB (and add them if needed).
     foreach ($article_object['items'] as $item) {
         $key = $google_compliant ? 'htmlUrl' : 'feedUrl';
         $feed = new FreshRSS_Feed($item['origin'][$key]);
         $feed = $this->feedDAO->searchByUrl($feed->url());
         if (is_null($feed)) {
             // Feed does not exist in DB,we should to try to add it.
             if ($nb_feeds >= $limits['max_feeds']) {
                 // Oops, no more place!
                 Minz_Log::warning(_t('feedback.sub.feed.over_max', $limits['max_feeds']));
             } else {
                 $feed = $this->addFeedJson($item['origin'], $google_compliant);
             }
             if (is_null($feed)) {
                 // Still null? It means something went wrong.
                 $error = true;
             } else {
                 // Nice! Increase the counter.
                 $nb_feeds += 1;
             }
         }
         if (!is_null($feed)) {
             $article_to_feed[$item['id']] = $feed->id();
         }
     }
     // Then, articles are imported.
     $prepared_statement = $this->entryDAO->addEntryPrepare();
     $this->entryDAO->beginTransaction();
     foreach ($article_object['items'] as $item) {
         if (!isset($article_to_feed[$item['id']])) {
             // Related feed does not exist for this entry, do nothing.
             continue;
         }
         $feed_id = $article_to_feed[$item['id']];
         $author = isset($item['author']) ? $item['author'] : '';
         $key_content = $google_compliant && !isset($item['content']) ? 'summary' : 'content';
         $tags = $item['categories'];
         if ($google_compliant) {
             // Remove tags containing "/state/com.google" which are useless.
             $tags = array_filter($tags, function ($var) {
                 return strpos($var, '/state/com.google') === false;
             });
         }
         $entry = new FreshRSS_Entry($feed_id, $item['id'], $item['title'], $author, $item[$key_content]['content'], $item['alternate'][0]['href'], $item['published'], $is_read, $starred);
         $entry->_id(min(time(), $entry->date(true)) . uSecString());
         $entry->_tags($tags);
         $entry = Minz_ExtensionManager::callHook('entry_before_insert', $entry);
         if (is_null($entry)) {
             // An extension has returned a null value, there is nothing to insert.
             continue;
         }
         $values = $entry->toArray();
         $id = $this->entryDAO->addEntry($values, $prepared_statement);
         if (!$error && $id === false) {
             $error = true;
         }
     }
     $this->entryDAO->commit();
     return $error;
 }