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