]> _ Git - fluidbook-toolbox.git/commitdiff
wip #7819 @3:00
authorsoufiane <soufiane@cubedesigners.com>
Fri, 7 Nov 2025 15:04:45 +0000 (16:04 +0100)
committersoufiane <soufiane@cubedesigners.com>
Fri, 7 Nov 2025 15:04:45 +0000 (16:04 +0100)
app/Console/Commands/AuditLink.php
app/Jobs/AuditLink.php
app/Models/FluidbookAuditLink.php
app/Models/FluidbookCollection.php

index 97c7ff85609f38c8ba7e1e38e558cc53eb771940..6c5acb589776b2e4e9e6d6684782d3046115754f 100644 (file)
@@ -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();
     }
 }
index 1c8817bbe333134b9f320a8f4fcfd7bd5bea957c..756d58ebf7e0329a0ada24218573de28bd6f12cf 100644 (file)
@@ -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');
     }
index c9d856b1f3866cdc9107d90677fc20c2cb63ce9e..c47da3376870a4de673b47145c83e581a914145e 100644 (file)
@@ -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) {
index ec6a709323aca138c17849eb73b6d3b44d1de7b6..5da41def13b6655516287160767ed14ec9fcd8a8 100644 (file)
@@ -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() {