]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/ImportBundle/Import/ReadabilityImport.php
Merge pull request #3814 from wallabag/2.3.6-dev
[github/wallabag/wallabag.git] / src / Wallabag / ImportBundle / Import / ReadabilityImport.php
CommitLineData
03e3753f
JB
1<?php
2
3namespace Wallabag\ImportBundle\Import;
4
5use Wallabag\CoreBundle\Entity\Entry;
03e3753f
JB
6
7class ReadabilityImport extends AbstractImport
8{
03e3753f 9 private $filepath;
03e3753f
JB
10
11 /**
12 * {@inheritdoc}
13 */
14 public function getName()
15 {
16 return 'Readability';
17 }
18
19 /**
20 * {@inheritdoc}
21 */
22 public function getUrl()
23 {
24 return 'import_readability';
25 }
26
27 /**
28 * {@inheritdoc}
29 */
30 public function getDescription()
31 {
32 return 'import.readability.description';
33 }
34
35 /**
36 * Set file path to the json file.
37 *
38 * @param string $filepath
39 */
40 public function setFilepath($filepath)
41 {
42 $this->filepath = $filepath;
43
44 return $this;
45 }
46
03e3753f
JB
47 /**
48 * {@inheritdoc}
49 */
50 public function import()
51 {
52 if (!$this->user) {
53 $this->logger->error('ReadabilityImport: user is not defined');
54
55 return false;
56 }
57
58 if (!file_exists($this->filepath) || !is_readable($this->filepath)) {
59 $this->logger->error('ReadabilityImport: unable to read file', ['filepath' => $this->filepath]);
60
61 return false;
62 }
63
64 $data = json_decode(file_get_contents($this->filepath), true);
65
66 if (empty($data) || empty($data['bookmarks'])) {
c7ea9b41
JB
67 $this->logger->error('ReadabilityImport: no entries in imported file');
68
03e3753f
JB
69 return false;
70 }
71
c98db1b6
JB
72 if ($this->producer) {
73 $this->parseEntriesForProducer($data['bookmarks']);
74
75 return true;
76 }
77
03e3753f
JB
78 $this->parseEntries($data['bookmarks']);
79
80 return true;
81 }
82
6d65c0a8
JB
83 /**
84 * {@inheritdoc}
85 */
c98db1b6
JB
86 public function parseEntry(array $importedEntry)
87 {
88 $existingEntry = $this->em
89 ->getRepository('WallabagCoreBundle:Entry')
90 ->findByUrlAndUserId($importedEntry['article__url'], $this->user->getId());
91
92 if (false !== $existingEntry) {
93 ++$this->skippedEntries;
94
95 return;
96 }
97
98 $data = [
99 'title' => $importedEntry['article__title'],
100 'url' => $importedEntry['article__url'],
c98db1b6
JB
101 'is_archived' => $importedEntry['archive'] || $this->markAsRead,
102 'is_starred' => $importedEntry['favorite'],
6d65c0a8 103 'created_at' => $importedEntry['date_added'],
36e6ef52 104 'html' => false,
c98db1b6
JB
105 ];
106
59b97fae
JB
107 $entry = new Entry($this->user);
108 $entry->setUrl($data['url']);
109 $entry->setTitle($data['title']);
c98db1b6 110
59b97fae 111 // update entry with content (in case fetching failed, the given entry will be return)
7aba665e 112 $this->fetchContent($entry, $data['url'], $data);
c98db1b6
JB
113
114 $entry->setArchived($data['is_archived']);
115 $entry->setStarred($data['is_starred']);
6d65c0a8 116 $entry->setCreatedAt(new \DateTime($data['created_at']));
c98db1b6
JB
117
118 $this->em->persist($entry);
119 ++$this->importedEntries;
120
121 return $entry;
122 }
123
03e3753f 124 /**
3849a9f3 125 * {@inheritdoc}
03e3753f 126 */
3849a9f3 127 protected function setEntryAsRead(array $importedEntry)
03e3753f 128 {
3849a9f3 129 $importedEntry['archive'] = 1;
03e3753f 130
3849a9f3 131 return $importedEntry;
03e3753f
JB
132 }
133}