]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | namespace Wallabag\ImportBundle\Import; | |
4 | ||
5 | use Wallabag\CoreBundle\Entity\Entry; | |
6 | ||
7 | abstract class WallabagImport extends AbstractImport | |
8 | { | |
9 | protected $filepath; | |
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 | ||
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 | $this->logger->error('WallabagImport: no entries in imported file'); | |
62 | ||
63 | return false; | |
64 | } | |
65 | ||
66 | if ($this->producer) { | |
67 | $this->parseEntriesForProducer($data); | |
68 | ||
69 | return true; | |
70 | } | |
71 | ||
72 | $this->parseEntries($data); | |
73 | ||
74 | return true; | |
75 | } | |
76 | ||
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 | /** | |
90 | * {@inheritdoc} | |
91 | */ | |
92 | public function parseEntry(array $importedEntry) | |
93 | { | |
94 | $existingEntry = $this->em | |
95 | ->getRepository('WallabagCoreBundle:Entry') | |
96 | ->findByUrlAndUserId($importedEntry['url'], $this->user->getId()); | |
97 | ||
98 | if (false !== $existingEntry) { | |
99 | ++$this->skippedEntries; | |
100 | ||
101 | return; | |
102 | } | |
103 | ||
104 | $data = $this->prepareEntry($importedEntry); | |
105 | ||
106 | $entry = new Entry($this->user); | |
107 | $entry->setUrl($data['url']); | |
108 | $entry->setTitle($data['title']); | |
109 | ||
110 | // update entry with content (in case fetching failed, the given entry will be return) | |
111 | $entry = $this->fetchContent($entry, $data['url'], $data); | |
112 | ||
113 | if (array_key_exists('tags', $data)) { | |
114 | $this->contentProxy->assignTagsToEntry( | |
115 | $entry, | |
116 | $data['tags'], | |
117 | $this->em->getUnitOfWork()->getScheduledEntityInsertions() | |
118 | ); | |
119 | } | |
120 | ||
121 | if (isset($importedEntry['preview_picture'])) { | |
122 | $entry->setPreviewPicture($importedEntry['preview_picture']); | |
123 | } | |
124 | ||
125 | $entry->setArchived($data['is_archived']); | |
126 | $entry->setStarred($data['is_starred']); | |
127 | ||
128 | if (!empty($data['created_at'])) { | |
129 | $entry->setCreatedAt(new \DateTime($data['created_at'])); | |
130 | } | |
131 | ||
132 | $this->em->persist($entry); | |
133 | ++$this->importedEntries; | |
134 | ||
135 | return $entry; | |
136 | } | |
137 | ||
138 | /** | |
139 | * This should return a cleaned array for a given entry to be given to `updateEntry`. | |
140 | * | |
141 | * @param array $entry Data from the imported file | |
142 | * | |
143 | * @return array | |
144 | */ | |
145 | abstract protected function prepareEntry($entry = []); | |
146 | } |