1
0
mirror of https://github.com/Kovah/LinkAce.git synced 2025-04-22 07:52:43 +02:00

Merge pull request #201 from hnnweb/importcommand

Import command
This commit is contained in:
Kevin Woblick 2021-01-12 23:42:02 +01:00 committed by GitHub
commit 20ce10106d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 152 additions and 0 deletions

View File

@ -0,0 +1,150 @@
<?php
namespace App\Console\Commands;
use App\Helper\HtmlMeta;
use App\Helper\LinkIconMapper;
use App\Models\Link;
use App\Models\Tag;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Log;
use Shaarli\NetscapeBookmarkParser\NetscapeBookmarkParser;
/**
* Class ImportCommand
*
* @package App\Console\Commands
*/
class ImportCommand extends Command
{
protected $signature = 'links:import
{filename : Bookmarks file to import}
{--skip-lookup : Whether the lookup should be skipped.}
{--skip-check : Whether the links checking should be skipped afterwards}';
public function handle(): void
{
$lookup = true;
$check = true;
if ($this->argument('filename')) {
$filename = $this->argument('filename');
} else {
return;
}
// Check if option "-skip-lookup" is present
if ($this->option('skip-lookup')) {
$this->info("Skipping lookup");
$lookup = !$this->option('skip-lookup');
}
// Check if option "-skip-check" is present
if ($this->option('skip-check')) {
$this->info("Skipping link check");
$check = !$this->option('skip-check');
}
// Read file
$this->info('Reading file "' . $filename . '"...');
$data = file_get_contents($filename);
$parser = new NetscapeBookmarkParser(true, [], '0', storage_path('logs'));
try {
$links = $parser->parseString($data);
} catch (Exception $e) {
Log::error($e->getMessage());
$this->error(trans('import.import_error'));
return;
}
if (empty($links)) {
// This will never be reached at the moment because the bookmark parser is not capable of handling
// empty bookmarks exports. See https://github.com/shaarli/netscape-bookmark-parser/issues/50
$this->error(trans('import.import_empty'));
return;
}
$userId = 1;
$imported = 0;
$skipped = 0;
$bar = $this->output->createProgressBar(count($links));
$bar->start();
foreach ($links as $link) {
if (Link::whereUrl($link['uri'])->first()) {
$skipped++;
continue;
}
$bar->advance();
if ($lookup) {
$linkMeta = HtmlMeta::getFromUrl($link['uri']);
$title = $link['title'] ?: $linkMeta['title'];
$description = $link['note'] ?: $linkMeta['description'];
} else {
$title = $link['title'];
$description = $link['note'];
}
$newLink = Link::create([
'user_id' => $userId,
'url' => $link['uri'],
'title' => $title,
'description' => $description,
'icon' => LinkIconMapper::mapLink($link['uri']),
'is_private' => $link['pub'],
'created_at' => Carbon::createFromTimestamp($link['time']),
'updated_at' => Carbon::now(),
]);
// Get all tags
if (!empty($link['tags'])) {
$tags = explode(' ', $link['tags']);
$newTags = [];
foreach ($tags as $tag) {
$newTag = Tag::firstOrCreate([
'user_id' => $userId,
'name' => $tag,
]);
$newTags[] = $newTag->id;
}
$newLink->tags()->sync($newTags);
}
$imported++;
}
$bar->finish();
// Force new line
$this->comment(PHP_EOL);
if ($check) {
// Queue link check
Artisan::queue('links:check');
}
$this->info(trans('import.import_successfully', [
'imported' => $imported,
'skipped' => $skipped,
]));
}
}

View File

@ -6,6 +6,7 @@ use App\Console\Commands\CheckLinksCommand;
use App\Console\Commands\CleanupLinkHistoriesCommand;
use App\Console\Commands\RegisterUserCommand;
use App\Console\Commands\ResetPasswordCommand;
use App\Console\Commands\ImportCommand;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
@ -26,6 +27,7 @@ class Kernel extends ConsoleKernel
CheckLinksCommand::class,
ResetPasswordCommand::class,
CleanupLinkHistoriesCommand::class,
ImportCommand::class,
];
/**