150 lines
3.5 KiB
PHP
150 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands\Url;
|
|
|
|
use App\Models\Url;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Str;
|
|
|
|
use function Laravel\Prompts\progress;
|
|
|
|
class Update extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'url:update
|
|
{ --c|clean : Remove URLs that are not in use anymore (implies rendering the bundles without cache which takes long time) }
|
|
{ --d|dead-only : Only update dead links }
|
|
{ --f|force : Force updating even if not due }
|
|
';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = 'Update Urls';
|
|
|
|
protected int $updatedCount = 0;
|
|
|
|
protected int $notUpdatedCount = 0;
|
|
|
|
protected array $deleted = [];
|
|
|
|
protected array $dead = [];
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*/
|
|
public function handle()
|
|
{
|
|
progress('Checking URLs...', Url::all(), function (Url $url, $progress) {
|
|
if (!$this->option('dead-only') || $url->is_dead) {
|
|
$this->checkUrl($url, $progress);
|
|
}
|
|
});
|
|
|
|
$this->report();
|
|
}
|
|
|
|
/**
|
|
* Check specified url
|
|
*/
|
|
private function checkUrl(Url $url, $progress)
|
|
{
|
|
if ($url->check($this->option('force'))) {
|
|
$this->updatedCount++;
|
|
} else {
|
|
$this->notUpdatedCount++;
|
|
}
|
|
|
|
if ($this->option('clean')) {
|
|
$bundles = $url->bundlesReferencingUrl();
|
|
|
|
if (empty($bundles)) {
|
|
$this->deleted[] = $url;
|
|
|
|
$url->delete();
|
|
|
|
return;
|
|
} else {
|
|
$url->bundles = $bundles;
|
|
}
|
|
}
|
|
|
|
$url->save();
|
|
|
|
if ($url->is_dead && !in_array($url, $this->dead)) {
|
|
$this->dead[] = $url;
|
|
}
|
|
|
|
$progress->hint(sprintf('Checked %s', urldecode($url->initial_url)));
|
|
}
|
|
|
|
/**
|
|
* Show a report
|
|
*/
|
|
private function report()
|
|
{
|
|
$this->line(sprintf('Updated URLs: <info>%d</info>', $this->updatedCount));
|
|
$this->line(sprintf('Update not needed: <comment>%d</comment>', $this->notUpdatedCount));
|
|
|
|
$this->reportDead();
|
|
$this->reportDeleted();
|
|
}
|
|
|
|
/**
|
|
* Show report of dead links
|
|
*/
|
|
private function reportDead()
|
|
{
|
|
$count = count($this->dead);
|
|
|
|
if ($count === 0) {
|
|
return;
|
|
}
|
|
|
|
$this->newLine();
|
|
$this->line(sprintf('Dead URLs: <error>%s</error>', $count));
|
|
|
|
foreach ($this->dead as $url) {
|
|
$this->newLine();
|
|
$this->comment($url->initial_url);
|
|
$this->line(sprintf(' > %s', $url->dead_reason));
|
|
$this->line('Bundles:');
|
|
|
|
foreach ($url->bundles ?? [] as $bundlePaths) {
|
|
$this->line(sprintf(' - %s', Str::unwrap($bundlePaths, '/')));
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Show report of deleted urls
|
|
*/
|
|
private function reportDeleted()
|
|
{
|
|
$count = count($this->deleted);
|
|
|
|
if ($count === 0) {
|
|
return;
|
|
}
|
|
|
|
$this->newLine();
|
|
$this->line(sprintf('Deleted URLs: <comment>%s</comment>', $count));
|
|
|
|
foreach ($this->deleted as $url) {
|
|
$this->newLine();
|
|
$this->comment($url->initial_url);
|
|
$this->line('Bundles:');
|
|
|
|
foreach ($url->bundles ?? [] as $bundlePaths) {
|
|
$this->line(sprintf(' - %s', $bundlePaths));
|
|
}
|
|
}
|
|
}
|
|
}
|