38 lines
2.2 KiB
PHP
38 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Services\ShadowrocketNaming;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Support\Str;
|
|
|
|
final class AlignShadowrocketNaming extends Command
|
|
{
|
|
protected $signature = 'shadowrocket:align-naming {--apply}';
|
|
protected $description = 'Align resource category and article metadata without changing account payloads';
|
|
|
|
public function handle(): int
|
|
{
|
|
$count = DB::transaction(function () {
|
|
$category = DB::table('categories')->where('sSlug', 'shadowrocketids')->lockForUpdate()->sole();
|
|
$rows = DB::table('articles')->where('iCategoryId', $category->id)->lockForUpdate()->get(['id', 'sTitle', 'sTitleSub']);
|
|
$changes = $rows->filter(fn ($r) => ShadowrocketNaming::align($r->sTitle) !== $r->sTitle || ShadowrocketNaming::align($r->sTitleSub ?? '') !== ($r->sTitleSub ?? ''));
|
|
if (! $this->option('apply')) return $changes->count();
|
|
if ($changes->isEmpty() && $category->sName === '免费小火箭ID') return 0;
|
|
$directory = storage_path('app/private/category-backups');
|
|
if (! is_dir($directory)) mkdir($directory, 0700, true);
|
|
$backup = $directory.'/shadowrocket-naming-'.Str::uuid().'.json';
|
|
if (file_put_contents($backup, json_encode(['category' => $category, 'articles' => $rows], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT)) === false) throw new \RuntimeException('Backup failed');
|
|
chmod($backup, 0600);
|
|
DB::table('categories')->where('id', $category->id)->update(['sName' => '免费小火箭ID', 'sDescription' => ShadowrocketNaming::align($category->sDescription ?? '')]);
|
|
foreach ($changes as $row) DB::table('articles')->where('id', $row->id)->update(['sTitle' => ShadowrocketNaming::align($row->sTitle), 'sTitleSub' => ShadowrocketNaming::align($row->sTitleSub ?? '')]);
|
|
return $changes->count();
|
|
});
|
|
if ($this->option('apply')) Cache::forget('global_nav_tree');
|
|
$this->info(json_encode(['apply' => (bool) $this->option('apply'), 'articles' => $count], JSON_UNESCAPED_UNICODE));
|
|
return self::SUCCESS;
|
|
}
|
|
}
|