3 namespace Wallabag\ImportBundle\Import
;
5 use Psr\Log\LoggerInterface
;
6 use Psr\Log\NullLogger
;
7 use Doctrine\ORM\EntityManager
;
8 use Wallabag\CoreBundle\Entity\Entry
;
9 use Wallabag\UserBundle\Entity\User
;
10 use Wallabag\CoreBundle\Helper\ContentProxy
;
12 abstract class BrowserImport
extends AbstractImport
19 abstract public function getName();
24 abstract public function getUrl();
29 abstract public function getDescription();
34 public function import()
37 $this->logger
->error('WallabagImport: user is not defined');
42 if (!file_exists($this->filepath
) || !is_readable($this->filepath
)) {
43 $this->logger
->error('WallabagImport: unable to read file', ['filepath' => $this->filepath
]);
48 $data = json_decode(file_get_contents($this->filepath
), true);
54 if ($this->producer
) {
55 $this->parseEntriesForProducer($data);
60 $this->parseEntries($data);
66 * Set file path to the json file.
68 * @param string $filepath
70 public function setFilepath($filepath)
72 $this->filepath
= $filepath;
78 * Parse and insert all given entries.
82 protected function parseEntries($entries)
86 foreach ($entries as $importedEntry) {
87 if ((array) $importedEntry !== $importedEntry) {
91 $entry = $this->parseEntry($importedEntry);
93 if (null === $entry) {
97 // flush every 20 entries
98 if (($i %
20) === 0) {
101 // clear only affected entities
102 $this->em
->clear(Entry
::class);
103 $this->em
->clear(Tag
::class);
112 * Parse entries and send them to the queue.
113 * It should just be a simple loop on all item, no call to the database should be done
114 * to speedup queuing.
116 * Faster parse entries for Producer.
117 * We don't care to make check at this time. They'll be done by the consumer.
119 * @param array $entries
121 protected function parseEntriesForProducer(array $entries)
123 foreach ($entries as $importedEntry) {
125 if ((array) $importedEntry !== $importedEntry) {
129 // set userId for the producer (it won't know which user is connected)
130 $importedEntry['userId'] = $this->user
->getId();
132 if ($this->markAsRead
) {
133 $importedEntry = $this->setEntryAsRead($importedEntry);
136 ++
$this->queuedEntries
;
138 $this->producer
->publish(json_encode($importedEntry));
145 public function parseEntry(array $importedEntry)
148 if ((!key_exists('guid', $importedEntry) || (!key_exists('id', $importedEntry))) && is_array(reset($importedEntry))) {
149 $this->parseEntries($importedEntry);
153 if (key_exists('children', $importedEntry)) {
154 $this->parseEntries($importedEntry['children']);
158 if (!key_exists('uri', $importedEntry) && !key_exists('url', $importedEntry)) {
162 $firefox = key_exists('uri', $importedEntry);
164 $existingEntry = $this->em
165 ->getRepository('WallabagCoreBundle:Entry')
166 ->findByUrlAndUserId(($firefox) ? $importedEntry['uri'] : $importedEntry['url'], $this->user
->getId());
168 if (false !== $existingEntry) {
169 ++
$this->skippedEntries
;
174 $data = $this->prepareEntry($importedEntry);
176 $entry = new Entry($this->user
);
177 $entry->setUrl($data['url']);
178 $entry->setTitle($data['title']);
180 // update entry with content (in case fetching failed, the given entry will be return)
181 $entry = $this->fetchContent($entry, $data['url'], $data);
183 if (array_key_exists('tags', $data)) {
184 $this->contentProxy
->assignTagsToEntry(
190 $entry->setArchived($data['is_archived']);
192 if (!empty($data['created_at'])) {
193 $dt = new \
DateTime();
194 $entry->setCreatedAt($dt->setTimestamp($data['created_at']/1000));
197 $this->em
->persist($entry);
198 ++
$this->importedEntries
;
206 protected function setEntryAsRead(array $importedEntry)
208 $importedEntry['is_archived'] = 1;
210 return $importedEntry;