diff options
Diffstat (limited to 'src/Wallabag/ImportBundle/Import')
-rw-r--r-- | src/Wallabag/ImportBundle/Import/BrowserImport.php | 205 | ||||
-rw-r--r-- | src/Wallabag/ImportBundle/Import/ChromeImport.php | 53 | ||||
-rw-r--r-- | src/Wallabag/ImportBundle/Import/FirefoxImport.php | 53 |
3 files changed, 311 insertions, 0 deletions
diff --git a/src/Wallabag/ImportBundle/Import/BrowserImport.php b/src/Wallabag/ImportBundle/Import/BrowserImport.php new file mode 100644 index 00000000..e15443c4 --- /dev/null +++ b/src/Wallabag/ImportBundle/Import/BrowserImport.php | |||
@@ -0,0 +1,205 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\ImportBundle\Import; | ||
4 | |||
5 | use Wallabag\CoreBundle\Entity\Entry; | ||
6 | use Wallabag\UserBundle\Entity\User; | ||
7 | use Wallabag\CoreBundle\Helper\ContentProxy; | ||
8 | |||
9 | abstract class BrowserImport extends AbstractImport | ||
10 | { | ||
11 | protected $filepath; | ||
12 | |||
13 | /** | ||
14 | * {@inheritdoc} | ||
15 | */ | ||
16 | abstract public function getName(); | ||
17 | |||
18 | /** | ||
19 | * {@inheritdoc} | ||
20 | */ | ||
21 | abstract public function getUrl(); | ||
22 | |||
23 | /** | ||
24 | * {@inheritdoc} | ||
25 | */ | ||
26 | abstract public function getDescription(); | ||
27 | |||
28 | /** | ||
29 | * {@inheritdoc} | ||
30 | */ | ||
31 | public function import() | ||
32 | { | ||
33 | if (!$this->user) { | ||
34 | $this->logger->error('Wallabag Browser Import: user is not defined'); | ||
35 | |||
36 | return false; | ||
37 | } | ||
38 | |||
39 | if (!file_exists($this->filepath) || !is_readable($this->filepath)) { | ||
40 | $this->logger->error('Wallabag Browser Import: unable to read file', ['filepath' => $this->filepath]); | ||
41 | |||
42 | return false; | ||
43 | } | ||
44 | |||
45 | $data = json_decode(file_get_contents($this->filepath), true); | ||
46 | |||
47 | if (empty($data)) { | ||
48 | return false; | ||
49 | } | ||
50 | |||
51 | if ($this->producer) { | ||
52 | $this->parseEntriesForProducer($data); | ||
53 | |||
54 | return true; | ||
55 | } | ||
56 | |||
57 | $this->parseEntries($data); | ||
58 | |||
59 | return true; | ||
60 | } | ||
61 | |||
62 | /** | ||
63 | * Set file path to the json file. | ||
64 | * | ||
65 | * @param string $filepath | ||
66 | */ | ||
67 | public function setFilepath($filepath) | ||
68 | { | ||
69 | $this->filepath = $filepath; | ||
70 | |||
71 | return $this; | ||
72 | } | ||
73 | |||
74 | /** | ||
75 | * Parse and insert all given entries. | ||
76 | * | ||
77 | * @param $entries | ||
78 | */ | ||
79 | protected function parseEntries($entries) | ||
80 | { | ||
81 | $i = 1; | ||
82 | |||
83 | foreach ($entries as $importedEntry) { | ||
84 | if ((array) $importedEntry !== $importedEntry) { | ||
85 | continue; | ||
86 | } | ||
87 | |||
88 | $entry = $this->parseEntry($importedEntry); | ||
89 | |||
90 | if (null === $entry) { | ||
91 | continue; | ||
92 | } | ||
93 | |||
94 | // flush every 20 entries | ||
95 | if (($i % 20) === 0) { | ||
96 | $this->em->flush(); | ||
97 | } | ||
98 | ++$i; | ||
99 | } | ||
100 | |||
101 | $this->em->flush(); | ||
102 | } | ||
103 | |||
104 | /** | ||
105 | * Parse entries and send them to the queue. | ||
106 | * It should just be a simple loop on all item, no call to the database should be done | ||
107 | * to speedup queuing. | ||
108 | * | ||
109 | * Faster parse entries for Producer. | ||
110 | * We don't care to make check at this time. They'll be done by the consumer. | ||
111 | * | ||
112 | * @param array $entries | ||
113 | */ | ||
114 | protected function parseEntriesForProducer(array $entries) | ||
115 | { | ||
116 | foreach ($entries as $importedEntry) { | ||
117 | if ((array) $importedEntry !== $importedEntry) { | ||
118 | continue; | ||
119 | } | ||
120 | |||
121 | // set userId for the producer (it won't know which user is connected) | ||
122 | $importedEntry['userId'] = $this->user->getId(); | ||
123 | |||
124 | if ($this->markAsRead) { | ||
125 | $importedEntry = $this->setEntryAsRead($importedEntry); | ||
126 | } | ||
127 | |||
128 | ++$this->queuedEntries; | ||
129 | |||
130 | $this->producer->publish(json_encode($importedEntry)); | ||
131 | } | ||
132 | } | ||
133 | |||
134 | /** | ||
135 | * {@inheritdoc} | ||
136 | */ | ||
137 | public function parseEntry(array $importedEntry) | ||
138 | { | ||
139 | if ((!array_key_exists('guid', $importedEntry) || (!array_key_exists('id', $importedEntry))) && is_array(reset($importedEntry))) { | ||
140 | $this->parseEntries($importedEntry); | ||
141 | |||
142 | return; | ||
143 | } | ||
144 | |||
145 | if (array_key_exists('children', $importedEntry)) { | ||
146 | $this->parseEntries($importedEntry['children']); | ||
147 | |||
148 | return; | ||
149 | } | ||
150 | |||
151 | if (!array_key_exists('uri', $importedEntry) && !array_key_exists('url', $importedEntry)) { | ||
152 | return; | ||
153 | } | ||
154 | |||
155 | $url = array_key_exists('uri', $importedEntry) ? $importedEntry['uri'] : $importedEntry['url']; | ||
156 | |||
157 | $existingEntry = $this->em | ||
158 | ->getRepository('WallabagCoreBundle:Entry') | ||
159 | ->findByUrlAndUserId($url, $this->user->getId()); | ||
160 | |||
161 | if (false !== $existingEntry) { | ||
162 | ++$this->skippedEntries; | ||
163 | |||
164 | return; | ||
165 | } | ||
166 | |||
167 | $data = $this->prepareEntry($importedEntry); | ||
168 | |||
169 | $entry = new Entry($this->user); | ||
170 | $entry->setUrl($data['url']); | ||
171 | $entry->setTitle($data['title']); | ||
172 | |||
173 | // update entry with content (in case fetching failed, the given entry will be return) | ||
174 | $entry = $this->fetchContent($entry, $data['url'], $data); | ||
175 | |||
176 | if (array_key_exists('tags', $data)) { | ||
177 | $this->contentProxy->assignTagsToEntry( | ||
178 | $entry, | ||
179 | $data['tags'] | ||
180 | ); | ||
181 | } | ||
182 | |||
183 | $entry->setArchived($data['is_archived']); | ||
184 | |||
185 | if (!empty($data['created_at'])) { | ||
186 | $dt = new \DateTime(); | ||
187 | $entry->setCreatedAt($dt->setTimestamp($data['created_at'])); | ||
188 | } | ||
189 | |||
190 | $this->em->persist($entry); | ||
191 | ++$this->importedEntries; | ||
192 | |||
193 | return $entry; | ||
194 | } | ||
195 | |||
196 | /** | ||
197 | * {@inheritdoc} | ||
198 | */ | ||
199 | protected function setEntryAsRead(array $importedEntry) | ||
200 | { | ||
201 | $importedEntry['is_archived'] = 1; | ||
202 | |||
203 | return $importedEntry; | ||
204 | } | ||
205 | } | ||
diff --git a/src/Wallabag/ImportBundle/Import/ChromeImport.php b/src/Wallabag/ImportBundle/Import/ChromeImport.php new file mode 100644 index 00000000..d7620bcb --- /dev/null +++ b/src/Wallabag/ImportBundle/Import/ChromeImport.php | |||
@@ -0,0 +1,53 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\ImportBundle\Import; | ||
4 | |||
5 | class ChromeImport extends BrowserImport | ||
6 | { | ||
7 | protected $filepath; | ||
8 | |||
9 | /** | ||
10 | * {@inheritdoc} | ||
11 | */ | ||
12 | public function getName() | ||
13 | { | ||
14 | return 'Chrome'; | ||
15 | } | ||
16 | |||
17 | /** | ||
18 | * {@inheritdoc} | ||
19 | */ | ||
20 | public function getUrl() | ||
21 | { | ||
22 | return 'import_chrome'; | ||
23 | } | ||
24 | |||
25 | /** | ||
26 | * {@inheritdoc} | ||
27 | */ | ||
28 | public function getDescription() | ||
29 | { | ||
30 | return 'import.chrome.description'; | ||
31 | } | ||
32 | |||
33 | /** | ||
34 | * {@inheritdoc} | ||
35 | */ | ||
36 | protected function prepareEntry(array $entry = []) | ||
37 | { | ||
38 | $data = [ | ||
39 | 'title' => $entry['name'], | ||
40 | 'html' => '', | ||
41 | 'url' => $entry['url'], | ||
42 | 'is_archived' => $this->markAsRead, | ||
43 | 'tags' => '', | ||
44 | 'created_at' => substr($entry['date_added'], 0, 10), | ||
45 | ]; | ||
46 | |||
47 | if (array_key_exists('tags', $entry) && $entry['tags'] != '') { | ||
48 | $data['tags'] = $entry['tags']; | ||
49 | } | ||
50 | |||
51 | return $data; | ||
52 | } | ||
53 | } | ||
diff --git a/src/Wallabag/ImportBundle/Import/FirefoxImport.php b/src/Wallabag/ImportBundle/Import/FirefoxImport.php new file mode 100644 index 00000000..e010f5a4 --- /dev/null +++ b/src/Wallabag/ImportBundle/Import/FirefoxImport.php | |||
@@ -0,0 +1,53 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\ImportBundle\Import; | ||
4 | |||
5 | class FirefoxImport extends BrowserImport | ||
6 | { | ||
7 | protected $filepath; | ||
8 | |||
9 | /** | ||
10 | * {@inheritdoc} | ||
11 | */ | ||
12 | public function getName() | ||
13 | { | ||
14 | return 'Firefox'; | ||
15 | } | ||
16 | |||
17 | /** | ||
18 | * {@inheritdoc} | ||
19 | */ | ||
20 | public function getUrl() | ||
21 | { | ||
22 | return 'import_firefox'; | ||
23 | } | ||
24 | |||
25 | /** | ||
26 | * {@inheritdoc} | ||
27 | */ | ||
28 | public function getDescription() | ||
29 | { | ||
30 | return 'import.firefox.description'; | ||
31 | } | ||
32 | |||
33 | /** | ||
34 | * {@inheritdoc} | ||
35 | */ | ||
36 | protected function prepareEntry(array $entry = []) | ||
37 | { | ||
38 | $data = [ | ||
39 | 'title' => $entry['title'], | ||
40 | 'html' => '', | ||
41 | 'url' => $entry['uri'], | ||
42 | 'is_archived' => $this->markAsRead, | ||
43 | 'tags' => '', | ||
44 | 'created_at' => substr($entry['dateAdded'], 0, 10), | ||
45 | ]; | ||
46 | |||
47 | if (array_key_exists('tags', $entry) && $entry['tags'] != '') { | ||
48 | $data['tags'] = $entry['tags']; | ||
49 | } | ||
50 | |||
51 | return $data; | ||
52 | } | ||
53 | } | ||