X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=src%2FWallabag%2FImportBundle%2FImport%2FAbstractImport.php;h=5ae4aa8d654373843a61b0276576a800fea9fd49;hb=3784688a88230d9c3aec4ca518be52ea1c70aeb9;hp=167853aaef541b8955c8cd6ab0a63e2198f81159;hpb=d0e9b3d640acce49068d1a2c5603b92c1bda363e;p=github%2Fwallabag%2Fwallabag.git diff --git a/src/Wallabag/ImportBundle/Import/AbstractImport.php b/src/Wallabag/ImportBundle/Import/AbstractImport.php index 167853aa..5ae4aa8d 100644 --- a/src/Wallabag/ImportBundle/Import/AbstractImport.php +++ b/src/Wallabag/ImportBundle/Import/AbstractImport.php @@ -2,17 +2,17 @@ namespace Wallabag\ImportBundle\Import; +use Doctrine\ORM\EntityManager; +use OldSound\RabbitMqBundle\RabbitMq\ProducerInterface; use Psr\Log\LoggerInterface; use Psr\Log\NullLogger; -use Doctrine\ORM\EntityManager; -use Wallabag\CoreBundle\Helper\ContentProxy; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Wallabag\CoreBundle\Entity\Entry; use Wallabag\CoreBundle\Entity\Tag; +use Wallabag\CoreBundle\Event\EntrySavedEvent; +use Wallabag\CoreBundle\Helper\ContentProxy; use Wallabag\CoreBundle\Helper\TagsAssigner; use Wallabag\UserBundle\Entity\User; -use OldSound\RabbitMqBundle\RabbitMq\ProducerInterface; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; -use Wallabag\CoreBundle\Event\EntrySavedEvent; abstract class AbstractImport implements ImportInterface { @@ -24,7 +24,7 @@ abstract class AbstractImport implements ImportInterface protected $producer; protected $user; protected $markAsRead; - protected $disableContentUpdate; + protected $disableContentUpdate = false; protected $skippedEntries = 0; protected $importedEntries = 0; protected $queuedEntries = 0; @@ -88,7 +88,7 @@ abstract class AbstractImport implements ImportInterface /** * Set whether articles should be fetched for updated content. * - * @param bool $markAsRead + * @param bool $disableContentUpdate */ public function setDisableContentUpdate($disableContentUpdate) { @@ -98,13 +98,34 @@ abstract class AbstractImport implements ImportInterface } /** - * Get whether articles should be fetched for updated content. + * {@inheritdoc} */ - public function getDisableContentUpdate() + public function getSummary() { - return $this->disableContentUpdate; + return [ + 'skipped' => $this->skippedEntries, + 'imported' => $this->importedEntries, + 'queued' => $this->queuedEntries, + ]; } + /** + * Parse one entry. + * + * @param array $importedEntry + * + * @return Entry + */ + abstract public function parseEntry(array $importedEntry); + + /** + * Validate that an entry is valid (like has some required keys, etc.). + * + * @param array $importedEntry + * + * @return bool + */ + abstract public function validateEntry(array $importedEntry); /** * Fetch content from the ContentProxy (using graby). @@ -117,10 +138,10 @@ abstract class AbstractImport implements ImportInterface protected function fetchContent(Entry $entry, $url, array $content = []) { try { - $this->contentProxy->importEntry($entry, $content, $this->disableContentUpdate); + $this->contentProxy->updateEntry($entry, $url, $content, $this->disableContentUpdate); } catch (\Exception $e) { $this->logger->error('Error trying to import an entry.', [ - 'entry_url' => $content['url'], + 'entry_url' => $url, 'error_msg' => $e->getMessage(), ]); } @@ -129,9 +150,9 @@ abstract class AbstractImport implements ImportInterface /** * Parse and insert all given entries. * - * @param $entries + * @param array $entries */ - protected function parseEntries($entries) + protected function parseEntries(array $entries) { $i = 1; $entryToBeFlushed = []; @@ -141,10 +162,14 @@ abstract class AbstractImport implements ImportInterface $importedEntry = $this->setEntryAsRead($importedEntry); } + if (false === $this->validateEntry($importedEntry)) { + continue; + } + $entry = $this->parseEntry($importedEntry); if (null === $entry) { - continue; + break; } // store each entry to be flushed so we can trigger the entry.saved event for each of them @@ -153,7 +178,7 @@ abstract class AbstractImport implements ImportInterface $entryToBeFlushed[] = $entry; // flush every 20 entries - if (($i % 20) === 0) { + if (0 === ($i % 20)) { $this->em->flush(); foreach ($entryToBeFlushed as $entry) { @@ -204,27 +229,6 @@ abstract class AbstractImport implements ImportInterface } } - /** - * {@inheritdoc} - */ - public function getSummary() - { - return [ - 'skipped' => $this->skippedEntries, - 'imported' => $this->importedEntries, - 'queued' => $this->queuedEntries, - ]; - } - - /** - * Parse one entry. - * - * @param array $importedEntry - * - * @return Entry - */ - abstract public function parseEntry(array $importedEntry); - /** * Set current imported entry to archived / read. * Implementation is different accross all imports.