2016-09-27 05:57:53 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Wallabag\ImportBundle\Import;
|
|
|
|
|
|
|
|
use Wallabag\CoreBundle\Entity\Entry;
|
|
|
|
|
|
|
|
class InstapaperImport extends AbstractImport
|
|
|
|
{
|
|
|
|
private $filepath;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
return 'Instapaper';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getUrl()
|
|
|
|
{
|
|
|
|
return 'import_instapaper';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getDescription()
|
|
|
|
{
|
|
|
|
return 'import.instapaper.description';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set file path to the json file.
|
|
|
|
*
|
|
|
|
* @param string $filepath
|
|
|
|
*/
|
|
|
|
public function setFilepath($filepath)
|
|
|
|
{
|
|
|
|
$this->filepath = $filepath;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function import()
|
|
|
|
{
|
|
|
|
if (!$this->user) {
|
|
|
|
$this->logger->error('InstapaperImport: user is not defined');
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!file_exists($this->filepath) || !is_readable($this->filepath)) {
|
|
|
|
$this->logger->error('InstapaperImport: unable to read file', ['filepath' => $this->filepath]);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$entries = [];
|
|
|
|
$handle = fopen($this->filepath, 'r');
|
|
|
|
while (($data = fgetcsv($handle, 10240)) !== false) {
|
|
|
|
if ('URL' === $data[0]) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-05-31 08:38:00 +00:00
|
|
|
// last element in the csv is the folder where the content belong
|
|
|
|
// BUT it can also be the status (since status = folder in Instapaper)
|
|
|
|
// and we don't want archive, unread & starred to become a tag
|
|
|
|
$tags = null;
|
2017-07-01 07:52:38 +00:00
|
|
|
if (false === in_array($data[3], ['Archive', 'Unread', 'Starred'], true)) {
|
2017-05-31 08:38:00 +00:00
|
|
|
$tags = [$data[3]];
|
|
|
|
}
|
|
|
|
|
2016-09-27 05:57:53 +00:00
|
|
|
$entries[] = [
|
|
|
|
'url' => $data[0],
|
|
|
|
'title' => $data[1],
|
|
|
|
'status' => $data[3],
|
|
|
|
'is_archived' => $data[3] === 'Archive' || $data[3] === 'Starred',
|
|
|
|
'is_starred' => $data[3] === 'Starred',
|
2016-12-03 03:42:36 +00:00
|
|
|
'html' => false,
|
2017-05-31 08:38:00 +00:00
|
|
|
'tags' => $tags,
|
2016-09-27 05:57:53 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
fclose($handle);
|
|
|
|
|
2016-09-27 15:01:14 +00:00
|
|
|
if (empty($entries)) {
|
|
|
|
$this->logger->error('InstapaperImport: no entries in imported file');
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-09-27 05:57:53 +00:00
|
|
|
if ($this->producer) {
|
|
|
|
$this->parseEntriesForProducer($entries);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->parseEntries($entries);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function parseEntry(array $importedEntry)
|
|
|
|
{
|
|
|
|
$existingEntry = $this->em
|
|
|
|
->getRepository('WallabagCoreBundle:Entry')
|
|
|
|
->findByUrlAndUserId($importedEntry['url'], $this->user->getId());
|
|
|
|
|
|
|
|
if (false !== $existingEntry) {
|
|
|
|
++$this->skippedEntries;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$entry = new Entry($this->user);
|
|
|
|
$entry->setUrl($importedEntry['url']);
|
|
|
|
$entry->setTitle($importedEntry['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, $importedEntry['url'], $importedEntry);
|
2016-09-27 05:57:53 +00:00
|
|
|
|
2017-05-31 08:38:00 +00:00
|
|
|
if (!empty($importedEntry['tags'])) {
|
|
|
|
$this->tagsAssigner->assignTagsToEntry(
|
|
|
|
$entry,
|
|
|
|
$importedEntry['tags'],
|
|
|
|
$this->em->getUnitOfWork()->getScheduledEntityInsertions()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-09-27 05:57:53 +00:00
|
|
|
$entry->setArchived($importedEntry['is_archived']);
|
|
|
|
$entry->setStarred($importedEntry['is_starred']);
|
|
|
|
|
|
|
|
$this->em->persist($entry);
|
|
|
|
++$this->importedEntries;
|
|
|
|
|
|
|
|
return $entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
protected function setEntryAsRead(array $importedEntry)
|
|
|
|
{
|
|
|
|
$importedEntry['is_archived'] = 1;
|
|
|
|
|
|
|
|
return $importedEntry;
|
|
|
|
}
|
|
|
|
}
|