3 namespace Wallabag\ImportBundle\Import
;
5 use Wallabag\CoreBundle\Entity\Entry
;
6 use Wallabag\UserBundle\Entity\User
;
8 class ReadabilityImport
extends AbstractImport
10 private $skippedEntries = 0;
11 private $importedEntries = 0;
17 public function getName()
25 public function getUrl()
27 return 'import_readability';
33 public function getDescription()
35 return 'import.readability.description';
39 * Set file path to the json file.
41 * @param string $filepath
43 public function setFilepath($filepath)
45 $this->filepath
= $filepath;
53 public function getSummary()
56 'skipped' => $this->skippedEntries
,
57 'imported' => $this->importedEntries
,
64 public function import()
67 $this->logger
->error('ReadabilityImport: user is not defined');
72 if (!file_exists($this->filepath
) || !is_readable($this->filepath
)) {
73 $this->logger
->error('ReadabilityImport: unable to read file', ['filepath' => $this->filepath
]);
78 $data = json_decode(file_get_contents($this->filepath
), true);
80 if (empty($data) || empty($data['bookmarks'])) {
84 if ($this->producer
) {
85 $this->parseEntriesForProducer($data['bookmarks']);
90 $this->parseEntries($data['bookmarks']);
95 public function parseEntry(array $importedEntry)
97 $existingEntry = $this->em
98 ->getRepository('WallabagCoreBundle:Entry')
99 ->findByUrlAndUserId($importedEntry['article__url'], $this->user
->getId());
101 if (false !== $existingEntry) {
102 ++
$this->skippedEntries
;
108 'title' => $importedEntry['article__title'],
109 'url' => $importedEntry['article__url'],
110 'content_type' => '',
112 'is_archived' => $importedEntry['archive'] || $this->markAsRead
,
113 'is_starred' => $importedEntry['favorite'],
116 $entry = $this->fetchContent(
117 new Entry($this->user
),
122 // jump to next entry in case of problem while getting content
123 if (false === $entry) {
124 ++
$this->skippedEntries
;
129 $entry->setArchived($data['is_archived']);
130 $entry->setStarred($data['is_starred']);
132 $this->em
->persist($entry);
133 ++
$this->importedEntries
;
139 * Faster parse entries for Producer.
140 * We don't care to make check at this time. They'll be done by the consumer.
142 * @param array $entries
144 protected function parseEntriesForProducer($entries)
146 foreach ($entries as $importedEntry) {
147 // set userId for the producer (it won't know which user is connected)
148 $importedEntry['userId'] = $this->user
->getId();
150 if ($this->markAsRead
) {
151 $importedEntry['archive'] = 1;
154 ++
$this->importedEntries
;
156 // flush every 20 entries
157 if (($i %
20) === 0) {