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