]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/ImportBundle/Import/ReadabilityImport.php
CS
[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 getSummary()
51 {
52 return [
53 'skipped' => $this->skippedEntries,
54 'imported' => $this->importedEntries,
55 ];
56 }
57
58 /**
59 * {@inheritdoc}
60 */
61 public function import()
62 {
63 if (!$this->user) {
64 $this->logger->error('ReadabilityImport: user is not defined');
65
66 return false;
67 }
68
69 if (!file_exists($this->filepath) || !is_readable($this->filepath)) {
70 $this->logger->error('ReadabilityImport: unable to read file', ['filepath' => $this->filepath]);
71
72 return false;
73 }
74
75 $data = json_decode(file_get_contents($this->filepath), true);
76
77 if (empty($data) || empty($data['bookmarks'])) {
78 return false;
79 }
80
c98db1b6
JB
81 if ($this->producer) {
82 $this->parseEntriesForProducer($data['bookmarks']);
83
84 return true;
85 }
86
03e3753f
JB
87 $this->parseEntries($data['bookmarks']);
88
89 return true;
90 }
91
c98db1b6
JB
92 public function parseEntry(array $importedEntry)
93 {
94 $existingEntry = $this->em
95 ->getRepository('WallabagCoreBundle:Entry')
96 ->findByUrlAndUserId($importedEntry['article__url'], $this->user->getId());
97
98 if (false !== $existingEntry) {
99 ++$this->skippedEntries;
100
101 return;
102 }
103
104 $data = [
105 'title' => $importedEntry['article__title'],
106 'url' => $importedEntry['article__url'],
107 'content_type' => '',
108 'language' => '',
109 'is_archived' => $importedEntry['archive'] || $this->markAsRead,
110 'is_starred' => $importedEntry['favorite'],
111 ];
112
113 $entry = $this->fetchContent(
114 new Entry($this->user),
115 $data['url'],
116 $data
117 );
118
119 // jump to next entry in case of problem while getting content
120 if (false === $entry) {
121 ++$this->skippedEntries;
122
123 return;
124 }
125
126 $entry->setArchived($data['is_archived']);
127 $entry->setStarred($data['is_starred']);
128
129 $this->em->persist($entry);
130 ++$this->importedEntries;
131
132 return $entry;
133 }
134
03e3753f 135 /**
3849a9f3 136 * {@inheritdoc}
03e3753f 137 */
3849a9f3 138 protected function setEntryAsRead(array $importedEntry)
03e3753f 139 {
3849a9f3 140 $importedEntry['archive'] = 1;
03e3753f 141
3849a9f3 142 return $importedEntry;
03e3753f
JB
143 }
144}