]>
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)) { | |
c7ea9b41 JB |
61 | $this->logger->error('WallabagImport: no entries in imported file'); |
62 | ||
b787a775 JB |
63 | return false; |
64 | } | |
65 | ||
c98db1b6 JB |
66 | if ($this->producer) { |
67 | $this->parseEntriesForProducer($data); | |
68 | ||
69 | return true; | |
70 | } | |
71 | ||
b787a775 JB |
72 | $this->parseEntries($data); |
73 | ||
74 | return true; | |
75 | } | |
76 | ||
b787a775 JB |
77 | /** |
78 | * Set file path to the json file. | |
79 | * | |
80 | * @param string $filepath | |
81 | */ | |
82 | public function setFilepath($filepath) | |
83 | { | |
84 | $this->filepath = $filepath; | |
85 | ||
86 | return $this; | |
87 | } | |
88 | ||
89 | /** | |
c98db1b6 | 90 | * {@inheritdoc} |
b787a775 | 91 | */ |
c98db1b6 | 92 | public function parseEntry(array $importedEntry) |
b787a775 | 93 | { |
c98db1b6 JB |
94 | $existingEntry = $this->em |
95 | ->getRepository('WallabagCoreBundle:Entry') | |
96 | ->findByUrlAndUserId($importedEntry['url'], $this->user->getId()); | |
b787a775 | 97 | |
c98db1b6 JB |
98 | if (false !== $existingEntry) { |
99 | ++$this->skippedEntries; | |
b787a775 | 100 | |
c98db1b6 JB |
101 | return; |
102 | } | |
103 | ||
104 | $data = $this->prepareEntry($importedEntry); | |
b787a775 | 105 | |
59b97fae JB |
106 | $entry = new Entry($this->user); |
107 | $entry->setUrl($data['url']); | |
108 | $entry->setTitle($data['title']); | |
b787a775 | 109 | |
59b97fae JB |
110 | // update entry with content (in case fetching failed, the given entry will be return) |
111 | $entry = $this->fetchContent($entry, $data['url'], $data); | |
b787a775 | 112 | |
c98db1b6 JB |
113 | if (array_key_exists('tags', $data)) { |
114 | $this->contentProxy->assignTagsToEntry( | |
115 | $entry, | |
40113585 JB |
116 | $data['tags'], |
117 | $this->em->getUnitOfWork()->getScheduledEntityInsertions() | |
b787a775 | 118 | ); |
c98db1b6 | 119 | } |
b787a775 | 120 | |
c98db1b6 JB |
121 | if (isset($importedEntry['preview_picture'])) { |
122 | $entry->setPreviewPicture($importedEntry['preview_picture']); | |
b787a775 JB |
123 | } |
124 | ||
c98db1b6 JB |
125 | $entry->setArchived($data['is_archived']); |
126 | $entry->setStarred($data['is_starred']); | |
127 | ||
6d65c0a8 JB |
128 | if (!empty($data['created_at'])) { |
129 | $entry->setCreatedAt(new \DateTime($data['created_at'])); | |
130 | } | |
131 | ||
c98db1b6 JB |
132 | $this->em->persist($entry); |
133 | ++$this->importedEntries; | |
134 | ||
135 | return $entry; | |
b787a775 JB |
136 | } |
137 | ||
138 | /** | |
139 | * This should return a cleaned array for a given entry to be given to `updateEntry`. | |
140 | * | |
c98db1b6 | 141 | * @param array $entry Data from the imported file |
b787a775 JB |
142 | * |
143 | * @return array | |
144 | */ | |
c98db1b6 | 145 | abstract protected function prepareEntry($entry = []); |
b787a775 | 146 | } |