2023-07-26 12:49:30 +02:00
|
|
|
<?php
|
|
|
|
|
|
2024-02-19 01:30:12 +01:00
|
|
|
namespace Wallabag\Import;
|
2023-07-26 12:49:30 +02:00
|
|
|
|
2024-11-19 23:30:37 +01:00
|
|
|
use Symfony\Component\DomCrawler\Crawler;
|
|
|
|
|
|
2023-07-26 12:49:30 +02:00
|
|
|
class PocketHtmlImport extends HtmlImport
|
|
|
|
|
{
|
|
|
|
|
protected $filepath;
|
|
|
|
|
|
|
|
|
|
public function getName()
|
|
|
|
|
{
|
|
|
|
|
return 'Pocket HTML';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getUrl()
|
|
|
|
|
{
|
|
|
|
|
return 'import_pocket_html';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getDescription()
|
|
|
|
|
{
|
|
|
|
|
return 'import.pocket_html.description';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function validateEntry(array $importedEntry)
|
|
|
|
|
{
|
|
|
|
|
if (empty($importedEntry['url'])) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function import()
|
|
|
|
|
{
|
|
|
|
|
if (!$this->user) {
|
|
|
|
|
$this->logger->error('Pocket HTML Import: user is not defined');
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!file_exists($this->filepath) || !is_readable($this->filepath)) {
|
|
|
|
|
$this->logger->error('Pocket HTML Import: unable to read file', ['filepath' => $this->filepath]);
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-19 23:30:37 +01:00
|
|
|
$crawler = new Crawler(file_get_contents($this->filepath));
|
2023-07-26 12:49:30 +02:00
|
|
|
|
2024-11-19 23:30:37 +01:00
|
|
|
$hrefs = $crawler->filterXPath('//a');
|
2023-07-26 12:49:30 +02:00
|
|
|
|
2024-11-19 23:30:37 +01:00
|
|
|
if (0 === $hrefs->count()) {
|
2023-07-26 12:49:30 +02:00
|
|
|
$this->logger->error('Pocket HTML: no entries in imported file');
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-19 23:30:37 +01:00
|
|
|
$entries = $hrefs->each(function (Crawler $node) {
|
|
|
|
|
return [
|
|
|
|
|
'url' => $node->attr('href'),
|
|
|
|
|
'tags' => $node->attr('tags'),
|
|
|
|
|
'created_at' => $node->attr('time_added'),
|
|
|
|
|
];
|
|
|
|
|
});
|
2023-07-26 12:49:30 +02:00
|
|
|
|
|
|
|
|
if ($this->producer) {
|
|
|
|
|
$this->parseEntriesForProducer($entries);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->parseEntries($entries);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function prepareEntry(array $entry = [])
|
|
|
|
|
{
|
|
|
|
|
$data = [
|
|
|
|
|
'title' => '',
|
|
|
|
|
'html' => false,
|
|
|
|
|
'url' => $entry['url'],
|
|
|
|
|
'is_archived' => (int) $this->markAsRead,
|
|
|
|
|
'is_starred' => false,
|
|
|
|
|
'tags' => '',
|
|
|
|
|
'created_at' => $entry['created_at'],
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
if (\array_key_exists('tags', $entry) && '' !== $entry['tags']) {
|
|
|
|
|
$data['tags'] = $entry['tags'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
|
}
|
|
|
|
|
}
|