]>
Commit | Line | Data |
---|---|---|
03e3753f JB |
1 | <?php |
2 | ||
3 | namespace Wallabag\ImportBundle\Import; | |
4 | ||
5 | use Wallabag\CoreBundle\Entity\Entry; | |
03e3753f JB |
6 | |
7 | class 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'])) { | |
67 | return false; | |
68 | } | |
69 | ||
c98db1b6 JB |
70 | if ($this->producer) { |
71 | $this->parseEntriesForProducer($data['bookmarks']); | |
72 | ||
73 | return true; | |
74 | } | |
75 | ||
03e3753f JB |
76 | $this->parseEntries($data['bookmarks']); |
77 | ||
78 | return true; | |
79 | } | |
80 | ||
6d65c0a8 JB |
81 | /** |
82 | * {@inheritdoc} | |
83 | */ | |
c98db1b6 JB |
84 | public function parseEntry(array $importedEntry) |
85 | { | |
86 | $existingEntry = $this->em | |
87 | ->getRepository('WallabagCoreBundle:Entry') | |
88 | ->findByUrlAndUserId($importedEntry['article__url'], $this->user->getId()); | |
89 | ||
90 | if (false !== $existingEntry) { | |
91 | ++$this->skippedEntries; | |
92 | ||
93 | return; | |
94 | } | |
95 | ||
96 | $data = [ | |
97 | 'title' => $importedEntry['article__title'], | |
98 | 'url' => $importedEntry['article__url'], | |
99 | 'content_type' => '', | |
100 | 'language' => '', | |
101 | 'is_archived' => $importedEntry['archive'] || $this->markAsRead, | |
102 | 'is_starred' => $importedEntry['favorite'], | |
6d65c0a8 | 103 | 'created_at' => $importedEntry['date_added'], |
c98db1b6 JB |
104 | ]; |
105 | ||
106 | $entry = $this->fetchContent( | |
107 | new Entry($this->user), | |
108 | $data['url'], | |
109 | $data | |
110 | ); | |
111 | ||
112 | // jump to next entry in case of problem while getting content | |
113 | if (false === $entry) { | |
114 | ++$this->skippedEntries; | |
115 | ||
116 | return; | |
117 | } | |
118 | ||
119 | $entry->setArchived($data['is_archived']); | |
120 | $entry->setStarred($data['is_starred']); | |
6d65c0a8 | 121 | $entry->setCreatedAt(new \DateTime($data['created_at'])); |
c98db1b6 JB |
122 | |
123 | $this->em->persist($entry); | |
124 | ++$this->importedEntries; | |
125 | ||
126 | return $entry; | |
127 | } | |
128 | ||
03e3753f | 129 | /** |
3849a9f3 | 130 | * {@inheritdoc} |
03e3753f | 131 | */ |
3849a9f3 | 132 | protected function setEntryAsRead(array $importedEntry) |
03e3753f | 133 | { |
3849a9f3 | 134 | $importedEntry['archive'] = 1; |
03e3753f | 135 | |
3849a9f3 | 136 | return $importedEntry; |
03e3753f JB |
137 | } |
138 | } |