wallabag/src/Wallabag/ImportBundle/Import/WallabagImport.php

150 lines
3.3 KiB
PHP
Raw Normal View History

<?php
namespace Wallabag\ImportBundle\Import;
use Wallabag\CoreBundle\Entity\Entry;
abstract class WallabagImport extends AbstractImport
{
protected $filepath;
// untitled in all languages from v1
protected $untitled = [
'Untitled',
'Sans titre',
'podle nadpisu',
'Sin título',
'با عنوان',
'per titolo',
'Sem título',
'Без названия',
'po naslovu',
'Без назви',
'No title found',
'',
];
/**
* {@inheritdoc}
*/
abstract public function getName();
/**
* {@inheritdoc}
*/
abstract public function getUrl();
/**
* {@inheritdoc}
*/
abstract public function getDescription();
/**
* {@inheritdoc}
*/
public function import()
{
if (!$this->user) {
$this->logger->error('WallabagImport: user is not defined');
return false;
}
if (!file_exists($this->filepath) || !is_readable($this->filepath)) {
$this->logger->error('WallabagImport: unable to read file', ['filepath' => $this->filepath]);
return false;
}
$data = json_decode(file_get_contents($this->filepath), true);
if (empty($data)) {
return false;
}
2016-09-04 19:49:21 +00:00
if ($this->producer) {
$this->parseEntriesForProducer($data);
return true;
}
$this->parseEntries($data);
return true;
}
/**
* Set file path to the json file.
*
* @param string $filepath
*/
public function setFilepath($filepath)
{
$this->filepath = $filepath;
return $this;
}
/**
2016-09-04 19:49:21 +00:00
* {@inheritdoc}
*/
2016-09-04 19:49:21 +00:00
public function parseEntry(array $importedEntry)
{
2016-09-04 19:49:21 +00:00
$existingEntry = $this->em
->getRepository('WallabagCoreBundle:Entry')
->findByUrlAndUserId($importedEntry['url'], $this->user->getId());
2016-09-04 19:49:21 +00:00
if (false !== $existingEntry) {
++$this->skippedEntries;
2016-09-04 19:49:21 +00:00
return;
}
$data = $this->prepareEntry($importedEntry);
2016-09-04 19:49:21 +00:00
$entry = $this->fetchContent(
new Entry($this->user),
$importedEntry['url'],
$data
);
2016-09-04 19:49:21 +00:00
// jump to next entry in case of problem while getting content
if (false === $entry) {
++$this->skippedEntries;
2016-09-04 19:49:21 +00:00
return;
}
2016-09-04 19:49:21 +00:00
if (array_key_exists('tags', $data)) {
$this->contentProxy->assignTagsToEntry(
$entry,
$data['tags']
);
2016-09-04 19:49:21 +00:00
}
2016-09-04 19:49:21 +00:00
if (isset($importedEntry['preview_picture'])) {
$entry->setPreviewPicture($importedEntry['preview_picture']);
}
2016-09-04 19:49:21 +00:00
$entry->setArchived($data['is_archived']);
$entry->setStarred($data['is_starred']);
if (!empty($data['created_at'])) {
$entry->setCreatedAt(new \DateTime($data['created_at']));
}
2016-09-04 19:49:21 +00:00
$this->em->persist($entry);
++$this->importedEntries;
return $entry;
}
/**
* This should return a cleaned array for a given entry to be given to `updateEntry`.
*
2016-09-04 19:49:21 +00:00
* @param array $entry Data from the imported file
*
* @return array
*/
2016-09-04 19:49:21 +00:00
abstract protected function prepareEntry($entry = []);
}