]>
Commit | Line | Data |
---|---|---|
b787a775 JB |
1 | <?php |
2 | ||
3 | namespace Wallabag\ImportBundle\Import; | |
4 | ||
b787a775 JB |
5 | use Wallabag\CoreBundle\Entity\Entry; |
6 | use Wallabag\UserBundle\Entity\User; | |
b787a775 | 7 | |
19d9efab | 8 | abstract class WallabagImport extends AbstractImport |
b787a775 JB |
9 | { |
10 | protected $user; | |
b787a775 JB |
11 | protected $skippedEntries = 0; |
12 | protected $importedEntries = 0; | |
13 | protected $filepath; | |
b787a775 JB |
14 | // untitled in all languages from v1 |
15 | protected $untitled = [ | |
16 | 'Untitled', | |
17 | 'Sans titre', | |
18 | 'podle nadpisu', | |
19 | 'Sin título', | |
20 | 'با عنوان', | |
21 | 'per titolo', | |
22 | 'Sem título', | |
23 | 'Без названия', | |
24 | 'po naslovu', | |
25 | 'Без назви', | |
26 | 'No title found', | |
27 | '', | |
28 | ]; | |
29 | ||
b787a775 JB |
30 | /** |
31 | * {@inheritdoc} | |
32 | */ | |
33 | abstract public function getName(); | |
34 | ||
35 | /** | |
36 | * {@inheritdoc} | |
37 | */ | |
38 | abstract public function getUrl(); | |
39 | ||
40 | /** | |
41 | * {@inheritdoc} | |
42 | */ | |
43 | abstract public function getDescription(); | |
44 | ||
45 | /** | |
46 | * {@inheritdoc} | |
47 | */ | |
48 | public function import() | |
49 | { | |
50 | if (!$this->user) { | |
51 | $this->logger->error('WallabagImport: user is not defined'); | |
52 | ||
53 | return false; | |
54 | } | |
55 | ||
56 | if (!file_exists($this->filepath) || !is_readable($this->filepath)) { | |
57 | $this->logger->error('WallabagImport: unable to read file', ['filepath' => $this->filepath]); | |
58 | ||
59 | return false; | |
60 | } | |
61 | ||
62 | $data = json_decode(file_get_contents($this->filepath), true); | |
63 | ||
64 | if (empty($data)) { | |
65 | return false; | |
66 | } | |
67 | ||
c98db1b6 JB |
68 | if ($this->producer) { |
69 | $this->parseEntriesForProducer($data); | |
70 | ||
71 | return true; | |
72 | } | |
73 | ||
b787a775 JB |
74 | $this->parseEntries($data); |
75 | ||
76 | return true; | |
77 | } | |
78 | ||
79 | /** | |
80 | * {@inheritdoc} | |
81 | */ | |
82 | public function getSummary() | |
83 | { | |
84 | return [ | |
85 | 'skipped' => $this->skippedEntries, | |
86 | 'imported' => $this->importedEntries, | |
87 | ]; | |
88 | } | |
89 | ||
90 | /** | |
91 | * Set file path to the json file. | |
92 | * | |
93 | * @param string $filepath | |
94 | */ | |
95 | public function setFilepath($filepath) | |
96 | { | |
97 | $this->filepath = $filepath; | |
98 | ||
99 | return $this; | |
100 | } | |
101 | ||
102 | /** | |
c98db1b6 | 103 | * {@inheritdoc} |
b787a775 | 104 | */ |
c98db1b6 | 105 | public function parseEntry(array $importedEntry) |
b787a775 | 106 | { |
c98db1b6 JB |
107 | $existingEntry = $this->em |
108 | ->getRepository('WallabagCoreBundle:Entry') | |
109 | ->findByUrlAndUserId($importedEntry['url'], $this->user->getId()); | |
b787a775 | 110 | |
c98db1b6 JB |
111 | if (false !== $existingEntry) { |
112 | ++$this->skippedEntries; | |
b787a775 | 113 | |
c98db1b6 JB |
114 | return; |
115 | } | |
116 | ||
117 | $data = $this->prepareEntry($importedEntry); | |
b787a775 | 118 | |
c98db1b6 JB |
119 | $entry = $this->fetchContent( |
120 | new Entry($this->user), | |
121 | $importedEntry['url'], | |
122 | $data | |
123 | ); | |
b787a775 | 124 | |
c98db1b6 JB |
125 | // jump to next entry in case of problem while getting content |
126 | if (false === $entry) { | |
127 | ++$this->skippedEntries; | |
b787a775 | 128 | |
c98db1b6 JB |
129 | return; |
130 | } | |
b787a775 | 131 | |
c98db1b6 JB |
132 | if (array_key_exists('tags', $data)) { |
133 | $this->contentProxy->assignTagsToEntry( | |
134 | $entry, | |
135 | $data['tags'] | |
b787a775 | 136 | ); |
c98db1b6 | 137 | } |
b787a775 | 138 | |
c98db1b6 JB |
139 | if (isset($importedEntry['preview_picture'])) { |
140 | $entry->setPreviewPicture($importedEntry['preview_picture']); | |
b787a775 JB |
141 | } |
142 | ||
c98db1b6 JB |
143 | $entry->setArchived($data['is_archived']); |
144 | $entry->setStarred($data['is_starred']); | |
145 | ||
146 | $this->em->persist($entry); | |
147 | ++$this->importedEntries; | |
148 | ||
149 | return $entry; | |
b787a775 JB |
150 | } |
151 | ||
152 | /** | |
153 | * This should return a cleaned array for a given entry to be given to `updateEntry`. | |
154 | * | |
c98db1b6 | 155 | * @param array $entry Data from the imported file |
b787a775 JB |
156 | * |
157 | * @return array | |
158 | */ | |
c98db1b6 | 159 | abstract protected function prepareEntry($entry = []); |
b787a775 | 160 | } |