From 75e4baf35a42c27584d990934c887afbbd70962b Mon Sep 17 00:00:00 2001 From: Adam Halfar Date: Fri, 30 Aug 2024 10:15:13 +0200 Subject: [PATCH 1/2] Backport of issue #1021 --- src/Model/Entity/Panel.php | 31 ++++++++++++++++++++++++-- src/Model/Table/RequestsTable.php | 36 +++++++++++++++++++++++++++++-- 2 files changed, 63 insertions(+), 4 deletions(-) diff --git a/src/Model/Entity/Panel.php b/src/Model/Entity/Panel.php index a5cdd591..02879b8f 100644 --- a/src/Model/Entity/Panel.php +++ b/src/Model/Entity/Panel.php @@ -36,7 +36,7 @@ class Panel extends Entity protected $_hidden = ['content']; /** - * Read the stream contents. + * Read the stream contents or inflate deflated data. * * Over certain sizes PDO will return file handles. * For backwards compatibility and consistency we smooth over that difference here. @@ -47,7 +47,34 @@ class Panel extends Entity protected function _getContent($content) { if (is_resource($content)) { - return stream_get_contents($content); + $content = (string)stream_get_contents($content); + } + + if (is_string($content) && function_exists('gzinflate')) { + // phpcs:disable + $contentInflated = @gzinflate($content); + // phpcs:enable + if ($contentInflated !== false) { + return $contentInflated; + } + } + + return $content; + } + + /** + * Deflate the string data before saving it into database + * + * @param mixed $content Content + * @return mixed + */ + protected function _setContent(mixed $content): mixed + { + if (is_string($content) && function_exists('gzdeflate')) { + $contentDeflated = gzdeflate($content, 9); + if ($contentDeflated !== false) { + $content = $contentDeflated; + } } return $content; diff --git a/src/Model/Table/RequestsTable.php b/src/Model/Table/RequestsTable.php index 5675d610..a9d0a0d6 100644 --- a/src/Model/Table/RequestsTable.php +++ b/src/Model/Table/RequestsTable.php @@ -12,6 +12,7 @@ * @link https://cakephp.org CakePHP(tm) Project * @license https://www.opensource.org/licenses/mit-license.php MIT License */ + namespace DebugKit\Model\Table; use Cake\Core\Configure; @@ -86,7 +87,17 @@ public function findRecent(Query $query, array $options) */ protected function shouldGc() { - return rand(1, 100) === 100; + return rand(1, 10) === 10; + } + + /** + * Check if garbage collection vacuum should be run + * + * @return bool + */ + protected function shouldGcVacuum(): bool + { + return rand(1, 10) === 10; } /** @@ -131,7 +142,28 @@ public function gc() $conn = $this->getConnection(); if ($conn->getDriver() instanceof Sqlite) { - $conn->execute('VACUUM;'); + $conn->execute(' + PRAGMA auto_vacuum = FULL; + PRAGMA journal_mode = OFF; + PRAGMA synchronous = OFF; + PRAGMA foreign_keys = OFF; + PRAGMA temp_store = MEMORY; + PRAGMA automatic_index = OFF; + '); + + if (!$this->shouldGcVacuum()) { + return; + } + + try { + $conn->execute('VACUUM;'); + } catch (PDOException $e) { + Log::warning( + 'Unable to run VACUUM on debug kit SQLite database. ' . + 'Please manually remove the database file' + ); + Log::warning((string)$e); + } } } catch (PDOException $e) { Log::warning('Unable to garbage collect requests table. This is probably due to concurrent requests.'); From b24258750ed9392eeb0157499d1b0bfc4cdac982 Mon Sep 17 00:00:00 2001 From: Adam Halfar Date: Fri, 30 Aug 2024 10:20:32 +0200 Subject: [PATCH 2/2] Remove nonexistent type in php 7.4 --- src/Model/Entity/Panel.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Model/Entity/Panel.php b/src/Model/Entity/Panel.php index 02879b8f..9e3f9695 100644 --- a/src/Model/Entity/Panel.php +++ b/src/Model/Entity/Panel.php @@ -68,7 +68,7 @@ protected function _getContent($content) * @param mixed $content Content * @return mixed */ - protected function _setContent(mixed $content): mixed + protected function _setContent($content) { if (is_string($content) && function_exists('gzdeflate')) { $contentDeflated = gzdeflate($content, 9);