From 03c36eb134602f06833d893fa6f09f16ab21cb0f Mon Sep 17 00:00:00 2001
From: Jan-Hendrik Willms <tleilax+github@gmail.com>
Date: Mon, 20 Mar 2023 15:45:42 +0100
Subject: [PATCH] add link to installation documentation and increase filesize
 speed, fixes #45

---
 controllers/releases.php | 67 +++++++++++++++++++++++++---------------
 plugin.manifest          |  2 +-
 2 files changed, 43 insertions(+), 26 deletions(-)

diff --git a/controllers/releases.php b/controllers/releases.php
index 305a659..322542c 100644
--- a/controllers/releases.php
+++ b/controllers/releases.php
@@ -4,6 +4,7 @@ final class ReleasesController extends \TracToGitlab\Controller
     const CACHE_KEY = 'studip-releases';
 
     private static $curl_handle = null;
+    private $filesize_cache;
 
     public function before_filter(&$action, &$args)
     {
@@ -26,7 +27,18 @@ final class ReleasesController extends \TracToGitlab\Controller
             Icon::create('link2'),
             ['target' => '_blank']
         );
+        $widget->addLink(
+            _('Installationsanleitung'),
+            'https://hilfe.studip.de/admin/Admins/Installationsanleitung',
+            Icon::create('rescue'),
+            ['target' => '_blank']
+        );
         Sidebar::get()->addWidget($widget);
+
+        $this->filesize_cache = new StudipCachedArray(
+            self::CACHE_KEY . '/filesizes',
+            strtotime('+10 years')
+        );
     }
 
     public function after_filter($action, $args)
@@ -40,7 +52,7 @@ final class ReleasesController extends \TracToGitlab\Controller
 
     public function index_action()
     {
-        $version = Request::get('version');
+        $version = Request::get('version', Request::get('v'));
 
         $this->releases = $this->getReleases();
 
@@ -58,6 +70,7 @@ final class ReleasesController extends \TracToGitlab\Controller
     {
         if ($this->isAdmin()) {
             StudipCacheFactory::getCache()->expire(self::CACHE_KEY);
+            $this->filesize_cache->expire();
         }
 
         $this->relocate($this->indexURL());
@@ -159,37 +172,41 @@ final class ReleasesController extends \TracToGitlab\Controller
             return $hardcoded_size;
         }
 
-        // Init curl if necessary
-        if (self::$curl_handle === null) {
-            self::$curl_handle = curl_init(null);
+        if (!isset($this->filesize_cache[$url])) {
+            // Init curl if necessary
+            if (self::$curl_handle === null) {
+                self::$curl_handle = curl_init(null);
 
-            curl_setopt(self::$curl_handle, CURLOPT_HEADER, true);
-            curl_setopt(self::$curl_handle, CURLOPT_NOBODY, true);
-            curl_setopt(self::$curl_handle, CURLOPT_RETURNTRANSFER, true);
-            curl_setopt(self::$curl_handle, CURLOPT_FOLLOWLOCATION, true);
-        }
+                curl_setopt(self::$curl_handle, CURLOPT_HEADER, true);
+                curl_setopt(self::$curl_handle, CURLOPT_NOBODY, true);
+                curl_setopt(self::$curl_handle, CURLOPT_RETURNTRANSFER, true);
+                curl_setopt(self::$curl_handle, CURLOPT_FOLLOWLOCATION, true);
+            }
 
-        // Set url and send request
-        curl_setopt(self::$curl_handle, CURLOPT_URL, $url);
-        $response = curl_exec(self::$curl_handle);
+            // Set url and send request
+            curl_setopt(self::$curl_handle, CURLOPT_URL, $url);
+            $response = curl_exec(self::$curl_handle);
 
-        // Get status
-        $line = strtok($response, "\r\n");
-        $status = trim($line);
+            // Get status
+            $line = strtok($response, "\r\n");
+            $status = trim($line);
 
-        // Parse headers
-        $headers = [];
-        while (($line = strtok("\r\n")) !== false) {
-            $chunks = explode(':', $line, 2);
-            if ($chunks !== false && count($chunks) === 2) {
-                $headers[$chunks[0]] = $chunks[1];
+            // Parse headers
+            $headers = [];
+            while (($line = strtok("\r\n")) !== false) {
+                $chunks = explode(':', $line, 2);
+                if ($chunks !== false && count($chunks) === 2) {
+                    $headers[$chunks[0]] = $chunks[1];
+                }
             }
-        }
 
-        // TODO: Check status
+            // TODO: Check status
+
+            // Get filesize by Content-Length header
+            $this->filesize_cache[$url] = (int) ($headers['Content-Length'] ?? -1);
+        }
 
-        // Get filesize by Content-Length header
-        return (int) ($headers['Content-Length'] ?? -1);
+        return $this->filesize_cache[$url];
     }
 
     private function addVersionFilter(array $releases, ?string $version): void
diff --git a/plugin.manifest b/plugin.manifest
index 14b02dd..8a3c757 100644
--- a/plugin.manifest
+++ b/plugin.manifest
@@ -2,5 +2,5 @@ pluginname=Trac to gitlab converter
 pluginclassname=TracToGitlabPlugin
 pluginclassname=StudipReleasesPlugin
 origin=UOL
-version=1.4.2
+version=1.4.3
 studipMinVersion=5.0
-- 
GitLab