From: soufiane Date: Fri, 7 Nov 2025 15:04:45 +0000 (+0100) Subject: wip #7819 @3:00 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=e69bea469cefa879051fc5b3e86a02852283b533;p=fluidbook-toolbox.git wip #7819 @3:00 --- diff --git a/app/Console/Commands/AuditLink.php b/app/Console/Commands/AuditLink.php index 97c7ff856..6c5acb589 100644 --- a/app/Console/Commands/AuditLink.php +++ b/app/Console/Commands/AuditLink.php @@ -13,7 +13,7 @@ class AuditLink extends ToolboxCommand * * @var string */ - protected $signature = 'collection:audit-link {id} {--sync}'; + protected $signature = 'collection:audit-links {--sync}'; /** * The console command description. @@ -28,7 +28,6 @@ class AuditLink extends ToolboxCommand public function handle() { // - $c = FluidbookCollection::find($this->argument('id')); - $c->runAuditLink($this->option('sync', false)); + FluidbookCollection::runAuditLink(); } } diff --git a/app/Jobs/AuditLink.php b/app/Jobs/AuditLink.php index 1c8817bbe..756d58ebf 100644 --- a/app/Jobs/AuditLink.php +++ b/app/Jobs/AuditLink.php @@ -9,39 +9,61 @@ use App\Models\FluidbookCollection; use Cubist\Net\Util; use Cubist\Util\CommandLine; use Cubist\Util\Files\Files; +use Illuminate\Support\Arr; +use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Log; class AuditLink extends Base { - public $c; /** * Create a new job instance. */ - public function __construct(FluidbookCollection $collection) { - $this->c = $collection; + public function __construct() { + // } public function handle() { - $allLinks = FluidbookAuditLink::all(); + $allLinks = FluidbookAuditLink::orderBy('last_date_test', 'asc')->limit(10); + $allLinks = $allLinks->inRandomOrder()->get()->toArray(); + $externalLinks = []; foreach ($allLinks as $fb => $link) { // Error code start with 4 or 5 // Redirection code start with 3 - $httpCode = FluidbookAuditLink::getHttpCode($link['url']); + $curlResponse = FluidbookAuditLink::getHttpCode($link['url']); + $httpCode = $curlResponse['httpcode']; + $finalurl = ''; + $finalcodeurl = ''; + + if(str_starts_with($httpCode, 3)) { + $finalurl = $curlResponse['finalurl']; + $finalcodeurl = FluidbookAuditLink::getHttpCode($curlResponse['finalurl'])['httpcode']; + } + $comment = FluidbookAuditLink::getHttpCodeComment($httpCode); + $firstTimeError = ''; + if(str_starts_with($httpCode, 3) || str_starts_with($httpCode, 4) || str_starts_with($httpCode, 5)) { + $firstTimeError = null === $link['first_time_error'] || $httpCode !== $link['error_code'] ? date('Y-m-d H:i:s') : $link['first_time_error']; + } + $externalLinks[] = [ + 'id' => $link['id'], 'error_code' => str_starts_with($httpCode, 4) || str_starts_with($httpCode, 5) ? $httpCode.$comment : "", - 'first_time_error' => "", // Datetime of the first time we saw this error + 'first_time_error' => $firstTimeError, // Datetime of the first time we saw this error 'last_date_test' => date('Y-m-d H:i:s'), 'redirection_code' => str_starts_with($httpCode, 3) ? $httpCode : "", - 'final_code_url' => '', - 'final_target' => '', + 'final_code_url' => $finalcodeurl, + 'final_target' => $finalurl, 'updated_at'=> date('Y-m-d H:i:s') ]; } - FluidbookAuditLink::update($externalLinks); + $keys = array_slice(array_keys($externalLinks[0]), 1); + + DB::table('fluidbook_audit_link')->upsert($externalLinks, ['id'], $keys); + + dd($externalLinks); Log::info('Job exécuté avec succès'); } diff --git a/app/Models/FluidbookAuditLink.php b/app/Models/FluidbookAuditLink.php index c9d856b1f..c47da3376 100644 --- a/app/Models/FluidbookAuditLink.php +++ b/app/Models/FluidbookAuditLink.php @@ -56,12 +56,16 @@ class FluidbookAuditLink extends ToolboxModel curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_NOBODY, true); curl_setopt($ch, CURLOPT_TIMEOUT, 5); + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); + curl_setopt($ch, CURLOPT_HEADER, true); curl_exec($ch); $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); + $finalUrl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); + curl_close($ch); - return $httpcode; + return ['httpcode' => $httpcode, 'finalurl' => $finalUrl]; } public static function getHttpCodeComment($httpcode) { diff --git a/app/Models/FluidbookCollection.php b/app/Models/FluidbookCollection.php index ec6a70932..5da41def1 100644 --- a/app/Models/FluidbookCollection.php +++ b/app/Models/FluidbookCollection.php @@ -311,9 +311,9 @@ class FluidbookCollection extends ToolboxStatusModel return $res; } - public function runAuditLink($sync = false) + public static function runAuditLink($sync = false) { - dispatch_sync(new AuditLink($this)); + dispatch_sync(new AuditLink()); } public static function registerLinks() {