Use Guzzle to download the SFS index
authorTim Düsterhus <duesterhus@woltlab.com>
Fri, 18 Jun 2021 10:29:51 +0000 (12:29 +0200)
committerTim Düsterhus <duesterhus@woltlab.com>
Fri, 18 Jun 2021 10:35:13 +0000 (12:35 +0200)
see #4281

wcfsetup/install/files/lib/data/blacklist/status/BlacklistStatus.class.php

index eb1bf117fd1d01105315b7e6eecede9a249607f1..494d59cd4fb164eca8bbf5448d1414902deecc05 100644 (file)
@@ -2,13 +2,12 @@
 
 namespace wcf\data\blacklist\status;
 
+use GuzzleHttp\ClientInterface;
+use GuzzleHttp\Psr7\Request;
+use Psr\Http\Client\ClientExceptionInterface;
 use wcf\data\DatabaseObject;
-use wcf\system\exception\HTTPNotFoundException;
-use wcf\system\exception\HTTPServerErrorException;
-use wcf\system\exception\HTTPUnauthorizedException;
 use wcf\system\exception\SystemException;
-use wcf\util\exception\HTTPException;
-use wcf\util\HTTPRequest;
+use wcf\system\io\HttpFactory;
 use wcf\util\JSON;
 
 /**
@@ -88,60 +87,60 @@ class BlacklistStatus extends DatabaseObject
      * @return string|null
      * @throws SystemException
      */
-    public static function getNextDelta(array $status)
+    public static function getNextDelta(array $status, ?ClientInterface $client = null)
     {
+        if (!$client) {
+            $client = HttpFactory::makeClientWithTimeout(5);
+        }
+
         // Fetch the index file to determine the oldest possible value that can be retrieved.
-        $request = new HTTPRequest('https://assets.woltlab.com/blacklist/index.json');
+        $request = new Request(
+            'GET',
+            'https://assets.woltlab.com/blacklist/index.json'
+        );
+
         try {
-            $request->execute();
-        } catch (SystemException $e) {
-            if (
-                $e instanceof HTTPNotFoundException
-                || $e instanceof HTTPUnauthorizedException
-                || $e instanceof HTTPServerErrorException
-                || $e instanceof HTTPException
-            ) {
-                \wcf\functions\exception\logThrowable($e);
-
-                return null;
-            }
+            $response = $client->send($request);
+        } catch (ClientExceptionInterface $e) {
+            \wcf\functions\exception\logThrowable($e);
 
-            throw $e;
+            return null;
         }
 
-        $response = $request->getReply();
-        if ($response['statusCode'] == 200) {
-            $data = JSON::decode($response['body']);
-            if (\is_array($data)) {
-                $deltas = ['delta1', 'delta2', 'delta3', 'delta4'];
-
-                // The array is ordered from "now" to "14 days ago".
-                foreach (\array_reverse($data) as $entry) {
-                    $date = $entry['date'];
-                    if (isset($status[$date])) {
-                        $dateStatus = $status[$date];
-                        if ($dateStatus->isComplete()) {
-                            continue;
-                        }
+        if ($response->getStatusCode() !== 200) {
+            return null;
+        }
 
-                        foreach ($deltas as $delta) {
-                            if ($entry['files'][$delta] && !$dateStatus->{$delta}) {
-                                return "{$date}/{$delta}.json";
-                            }
+        $data = JSON::decode((string)$response->getBody());
+        if (\is_array($data)) {
+            $deltas = ['delta1', 'delta2', 'delta3', 'delta4'];
+
+            // The array is ordered from "now" to "14 days ago".
+            foreach (\array_reverse($data) as $entry) {
+                $date = $entry['date'];
+                if (isset($status[$date])) {
+                    $dateStatus = $status[$date];
+                    if ($dateStatus->isComplete()) {
+                        continue;
+                    }
+
+                    foreach ($deltas as $delta) {
+                        if ($entry['files'][$delta] && !$dateStatus->{$delta}) {
+                            return "{$date}/{$delta}.json";
                         }
-                    } else {
-                        foreach ($deltas as $delta) {
-                            if ($entry['files'][$delta]) {
-                                return "{$date}/{$delta}.json";
-                            }
+                    }
+                } else {
+                    foreach ($deltas as $delta) {
+                        if ($entry['files'][$delta]) {
+                            return "{$date}/{$delta}.json";
                         }
                     }
-
-                    // The `full.json` file is not considered for now, because it is very unlikely that none of the
-                    // delta files are available. Also, it's significant larger than the delta updates and we cannot
-                    // reliably predict if we're able to import it at all: slow hosts or max_execution_time almost
-                    // exhausted by other cronjobs.
                 }
+
+                // The `full.json` file is not considered for now, because it is very unlikely that none of the
+                // delta files are available. Also, it's significant larger than the delta updates and we cannot
+                // reliably predict if we're able to import it at all: slow hosts or max_execution_time almost
+                // exhausted by other cronjobs.
             }
         }