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