2016-11-04 21:44:31 +00:00
|
|
|
<?php
|
|
|
|
|
2024-02-19 00:30:12 +00:00
|
|
|
namespace Wallabag\Import;
|
2016-11-04 21:44:31 +00:00
|
|
|
|
2024-02-19 00:30:12 +00:00
|
|
|
use Wallabag\Entity\Entry;
|
2016-11-04 21:44:31 +00:00
|
|
|
|
|
|
|
class PinboardImport extends AbstractImport
|
|
|
|
{
|
|
|
|
private $filepath;
|
|
|
|
|
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
return 'Pinboard';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getUrl()
|
|
|
|
{
|
|
|
|
return 'import_pinboard';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getDescription()
|
|
|
|
{
|
|
|
|
return 'import.pinboard.description';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set file path to the json file.
|
|
|
|
*
|
|
|
|
* @param string $filepath
|
|
|
|
*/
|
|
|
|
public function setFilepath($filepath)
|
|
|
|
{
|
|
|
|
$this->filepath = $filepath;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function import()
|
|
|
|
{
|
|
|
|
if (!$this->user) {
|
|
|
|
$this->logger->error('PinboardImport: user is not defined');
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!file_exists($this->filepath) || !is_readable($this->filepath)) {
|
|
|
|
$this->logger->error('PinboardImport: unable to read file', ['filepath' => $this->filepath]);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = json_decode(file_get_contents($this->filepath), true);
|
|
|
|
|
|
|
|
if (empty($data)) {
|
|
|
|
$this->logger->error('PinboardImport: no entries in imported file');
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->producer) {
|
|
|
|
$this->parseEntriesForProducer($data);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->parseEntries($data);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-18 12:14:42 +00:00
|
|
|
public function validateEntry(array $importedEntry)
|
|
|
|
{
|
|
|
|
if (empty($importedEntry['href'])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-11-04 21:44:31 +00:00
|
|
|
public function parseEntry(array $importedEntry)
|
|
|
|
{
|
|
|
|
$existingEntry = $this->em
|
2022-08-25 19:37:10 +00:00
|
|
|
->getRepository(Entry::class)
|
2016-11-04 21:44:31 +00:00
|
|
|
->findByUrlAndUserId($importedEntry['href'], $this->user->getId());
|
|
|
|
|
|
|
|
if (false !== $existingEntry) {
|
|
|
|
++$this->skippedEntries;
|
|
|
|
|
2023-08-08 01:27:21 +00:00
|
|
|
return null;
|
2016-11-04 21:44:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'title' => $importedEntry['description'],
|
|
|
|
'url' => $importedEntry['href'],
|
|
|
|
'is_archived' => ('no' === $importedEntry['toread']) || $this->markAsRead,
|
|
|
|
'is_starred' => false,
|
|
|
|
'created_at' => $importedEntry['time'],
|
|
|
|
'tags' => explode(' ', $importedEntry['tags']),
|
|
|
|
];
|
|
|
|
|
|
|
|
$entry = new Entry($this->user);
|
|
|
|
$entry->setUrl($data['url']);
|
|
|
|
$entry->setTitle($data['title']);
|
|
|
|
|
|
|
|
// update entry with content (in case fetching failed, the given entry will be return)
|
2016-12-07 03:17:44 +00:00
|
|
|
$this->fetchContent($entry, $data['url'], $data);
|
2016-11-04 21:44:31 +00:00
|
|
|
|
|
|
|
if (!empty($data['tags'])) {
|
2017-05-27 20:08:14 +00:00
|
|
|
$this->tagsAssigner->assignTagsToEntry(
|
2016-11-04 21:44:31 +00:00
|
|
|
$entry,
|
|
|
|
$data['tags'],
|
|
|
|
$this->em->getUnitOfWork()->getScheduledEntityInsertions()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-04-11 09:42:52 +00:00
|
|
|
$entry->updateArchived($data['is_archived']);
|
2016-11-04 21:44:31 +00:00
|
|
|
$entry->setStarred($data['is_starred']);
|
|
|
|
$entry->setCreatedAt(new \DateTime($data['created_at']));
|
|
|
|
|
|
|
|
$this->em->persist($entry);
|
|
|
|
++$this->importedEntries;
|
|
|
|
|
|
|
|
return $entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function setEntryAsRead(array $importedEntry)
|
|
|
|
{
|
|
|
|
$importedEntry['toread'] = 'no';
|
|
|
|
|
|
|
|
return $importedEntry;
|
|
|
|
}
|
|
|
|
}
|