]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/ImportBundle/Import/WallabagImport.php
Validate imported entry to avoid error on import
[github/wallabag/wallabag.git] / src / Wallabag / ImportBundle / Import / WallabagImport.php
CommitLineData
b787a775
JB
1<?php
2
3namespace Wallabag\ImportBundle\Import;
4
b787a775 5use Wallabag\CoreBundle\Entity\Entry;
b787a775 6
19d9efab 7abstract 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
9f8f188d
JB
89 /**
90 * {@inheritdoc}
91 */
92 public function validateEntry(array $importedEntry)
93 {
94 if (empty($importedEntry['url'])) {
95 return false;
96 }
97
98 return true;
99 }
100
b787a775 101 /**
c98db1b6 102 * {@inheritdoc}
b787a775 103 */
c98db1b6 104 public function parseEntry(array $importedEntry)
b787a775 105 {
c98db1b6
JB
106 $existingEntry = $this->em
107 ->getRepository('WallabagCoreBundle:Entry')
108 ->findByUrlAndUserId($importedEntry['url'], $this->user->getId());
b787a775 109
c98db1b6
JB
110 if (false !== $existingEntry) {
111 ++$this->skippedEntries;
b787a775 112
c98db1b6
JB
113 return;
114 }
115
116 $data = $this->prepareEntry($importedEntry);
b787a775 117
59b97fae
JB
118 $entry = new Entry($this->user);
119 $entry->setUrl($data['url']);
120 $entry->setTitle($data['title']);
b787a775 121
59b97fae 122 // update entry with content (in case fetching failed, the given entry will be return)
7aba665e 123 $this->fetchContent($entry, $data['url'], $data);
b787a775 124
c98db1b6 125 if (array_key_exists('tags', $data)) {
6bc6fb1f 126 $this->tagsAssigner->assignTagsToEntry(
c98db1b6 127 $entry,
40113585
JB
128 $data['tags'],
129 $this->em->getUnitOfWork()->getScheduledEntityInsertions()
b787a775 130 );
c98db1b6 131 }
b787a775 132
c98db1b6
JB
133 if (isset($importedEntry['preview_picture'])) {
134 $entry->setPreviewPicture($importedEntry['preview_picture']);
b787a775
JB
135 }
136
c98db1b6
JB
137 $entry->setArchived($data['is_archived']);
138 $entry->setStarred($data['is_starred']);
139
6d65c0a8
JB
140 if (!empty($data['created_at'])) {
141 $entry->setCreatedAt(new \DateTime($data['created_at']));
142 }
143
c98db1b6
JB
144 $this->em->persist($entry);
145 ++$this->importedEntries;
146
147 return $entry;
b787a775
JB
148 }
149
150 /**
151 * This should return a cleaned array for a given entry to be given to `updateEntry`.
152 *
c98db1b6 153 * @param array $entry Data from the imported file
b787a775
JB
154 *
155 * @return array
156 */
c98db1b6 157 abstract protected function prepareEntry($entry = []);
b787a775 158}