diff --git a/README.md b/README.md index cebdbf1..bd07617 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ Set up the Laravel `.env` file and SQLite database: composer install cp .env.example .env php artisan key:generate -php artisan migrate +php artisan migrate # database schema ``` Build the CSS with Vite: @@ -47,6 +47,12 @@ Start the web server: php artisan serve ``` +If you need to process queued jobs (e.g. import bookmarks): + +``` +php artisan queue:work -v +``` + ### Laravel Herd If you have [Laravel Herd](https://herd.laravel.com/) installed, you can clone the repository to one of your Herd diff --git a/app/Console/Commands/ImportBookmarks.php b/app/Console/Commands/ImportBookmarks.php index 60eb6e7..53b7f3d 100644 --- a/app/Console/Commands/ImportBookmarks.php +++ b/app/Console/Commands/ImportBookmarks.php @@ -2,22 +2,20 @@ namespace App\Console\Commands; -use App\Models\Bookmark; -use App\Models\BookmarkTag; -use App\Models\Tag; -use DateTimeImmutable; -use DateTimeInterface; +use App\Jobs\ImportBookmark; use Illuminate\Console\Command; -use Illuminate\Support\Facades\DB; +use RuntimeException; class ImportBookmarks extends Command { + static $perBatch = 100; + /** * The name and signature of the console command. * * @var string */ - protected $signature = 'app:import-bookmarks'; + protected $signature = 'app:import-bookmarks {json_file}'; /** * The console command description. @@ -31,34 +29,16 @@ class ImportBookmarks extends Command */ public function handle() { - $json = json_decode(file_get_contents("../pinboard_export.2024.05.24_19.25.json"), true); - - foreach ($json as $bookmark_json) { - $created_at = DateTimeImmutable::createFromFormat(DateTimeInterface::ISO8601_EXPANDED, $bookmark_json['time']); - - $bookmark = new Bookmark; - $bookmark->href = $bookmark_json['href']; - $bookmark->title = $bookmark_json['description']; - $bookmark->description = $bookmark_json['extended']; - $bookmark->created_at = $created_at; - $bookmark->updated_at = $created_at; - $bookmark->save(); - - $tokens = explode(' ', $bookmark_json['tags']); - foreach ($tokens as $tag_raw) { - $tag = Tag::firstOrCreate( - [ - 'name' => $tag_raw, - ] - ); - - $bookmark_tag = new BookmarkTag; - $bookmark_tag->bookmark_id = $bookmark->id; - $bookmark_tag->tag_id = $tag->id; - $bookmark_tag->save(); - } + $json_file_path = $this->argument('json_file'); + if (!file_exists($json_file_path)) { + throw new RuntimeException('Unable to find JSON file'); } - DB::rollBack(); + $json = file_get_contents($json_file_path); + $bookmarks = json_decode($json, true); + + foreach (array_chunk($bookmarks, self::$perBatch) as $bookmarks_json) { + ImportBookmark::dispatch(...$bookmarks_json); + } } } diff --git a/app/Jobs/ImportBookmark.php b/app/Jobs/ImportBookmark.php new file mode 100644 index 0000000..edc4d46 --- /dev/null +++ b/app/Jobs/ImportBookmark.php @@ -0,0 +1,61 @@ +bookmarks_json = $bookmarks_json; + } + + /** + * Execute the job. + */ + public function handle(): void + { + foreach ($this->bookmarks_json as $bookmark_json) { + $created_at = DateTimeImmutable::createFromFormat(DateTimeInterface::ISO8601_EXPANDED, $bookmark_json['time']); + + $bookmark = new Bookmark; + $bookmark->href = $bookmark_json['href']; + $bookmark->title = $bookmark_json['description']; + $bookmark->description = $bookmark_json['extended']; + $bookmark->created_at = $created_at; + $bookmark->updated_at = $created_at; + $bookmark->save(); + + $tokens = explode(' ', $bookmark_json['tags']); + foreach ($tokens as $tag_raw) { + $tag = Tag::firstOrCreate( + [ + 'name' => $tag_raw, + ] + ); + + $bookmark_tag = new BookmarkTag; + $bookmark_tag->bookmark_id = $bookmark->id; + $bookmark_tag->tag_id = $tag->id; + $bookmark_tag->save(); + } + } + } +}