From 17b39ca05a35991122ee46bac23ad99c313971de Mon Sep 17 00:00:00 2001 From: Lachlan Reynolds Date: Thu, 21 May 2026 20:12:33 +1200 Subject: [PATCH 1/8] enable spec generation by default --- Configuration.php | 51 +++++++++++++++++++++++++++++++++++++++++++++++ Tasks.php | 29 +++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 Configuration.php diff --git a/Configuration.php b/Configuration.php new file mode 100644 index 0000000..a169e4a --- /dev/null +++ b/Configuration.php @@ -0,0 +1,51 @@ +getConfig(); + + $apiReferenceConfig = $config->ApiReference; + if (empty($reports)) { + $apiReferenceConfig = array(); + } + + // we make sure to set a value only if none has been configured yet, eg in common config. + if (empty($apiReferenceConfig[self::KEY_ENABLE_SPEC_GENERATION])) { + $apiReferenceConfig[self::KEY_ENABLE_SPEC_GENERATION] = self::DEFAULT_ENABLE_SPEC_GENERATION; + } + + $config->ApiReference = $apiReferenceConfig; + $config->forceSave(); + } + + public function uninstall() + { + $config = $this->getConfig(); + $config->CustomReports = array(); + $config->forceSave(); + } +} diff --git a/Tasks.php b/Tasks.php index b4e1a6d..61bf35b 100644 --- a/Tasks.php +++ b/Tasks.php @@ -74,7 +74,36 @@ public function generateConfiguredPluginSpecs(): void } } } + public function getArchiveMaxRowsDefault() + { + $value = $this->getConfigValue(self::KEY_ARCHIVE_MAXIMUM_ROWS_CUSTOM_DIMENSIONS_DEFAULT, self::DEFAULT_ARCHIVE_MAXIMUM_ROWS_CUSTOM_DIMENSIONS_DEFAULT); + + if ($value === false || $value === '' || $value === null) { + $value = self::DEFAULT_ARCHIVE_MAXIMUM_ROWS_CUSTOM_DIMENSIONS_DEFAULT; + } + return (int) $value; + } + public function getArchiveMaxRowsDefault() + { + $value = $this->getConfigValue(self::KEY_ARCHIVE_MAXIMUM_ROWS_CUSTOM_DIMENSIONS_DEFAULT, self::DEFAULT_ARCHIVE_MAXIMUM_ROWS_CUSTOM_DIMENSIONS_DEFAULT); + + if ($value === false || $value === '' || $value === null) { + $value = self::DEFAULT_ARCHIVE_MAXIMUM_ROWS_CUSTOM_DIMENSIONS_DEFAULT; + } + + return (int) $value; + } + + private function getConfigValue($name, $default) + { + $config = $this->getConfig(); + $attribution = $config->CustomReports; + if (isset($attribution[$name])) { + return $attribution[$name]; + } + return $default; + } private function isSpecGenerationEnabled(): bool { return (bool) (Config::getInstance()->ApiReference['enable_spec_generation_task'] ?? 0); From 2c1352354dac1994e120c7adc816309fe03edbaa Mon Sep 17 00:00:00 2001 From: Lachlan Reynolds Date: Thu, 21 May 2026 20:15:28 +1200 Subject: [PATCH 2/8] Clean up code --- Tasks.php | 29 ----------------------------- 1 file changed, 29 deletions(-) diff --git a/Tasks.php b/Tasks.php index 61bf35b..b4e1a6d 100644 --- a/Tasks.php +++ b/Tasks.php @@ -74,36 +74,7 @@ public function generateConfiguredPluginSpecs(): void } } } - public function getArchiveMaxRowsDefault() - { - $value = $this->getConfigValue(self::KEY_ARCHIVE_MAXIMUM_ROWS_CUSTOM_DIMENSIONS_DEFAULT, self::DEFAULT_ARCHIVE_MAXIMUM_ROWS_CUSTOM_DIMENSIONS_DEFAULT); - - if ($value === false || $value === '' || $value === null) { - $value = self::DEFAULT_ARCHIVE_MAXIMUM_ROWS_CUSTOM_DIMENSIONS_DEFAULT; - } - return (int) $value; - } - public function getArchiveMaxRowsDefault() - { - $value = $this->getConfigValue(self::KEY_ARCHIVE_MAXIMUM_ROWS_CUSTOM_DIMENSIONS_DEFAULT, self::DEFAULT_ARCHIVE_MAXIMUM_ROWS_CUSTOM_DIMENSIONS_DEFAULT); - - if ($value === false || $value === '' || $value === null) { - $value = self::DEFAULT_ARCHIVE_MAXIMUM_ROWS_CUSTOM_DIMENSIONS_DEFAULT; - } - - return (int) $value; - } - - private function getConfigValue($name, $default) - { - $config = $this->getConfig(); - $attribution = $config->CustomReports; - if (isset($attribution[$name])) { - return $attribution[$name]; - } - return $default; - } private function isSpecGenerationEnabled(): bool { return (bool) (Config::getInstance()->ApiReference['enable_spec_generation_task'] ?? 0); From f34a98742ed65e62a9bcc36399606165996abb20 Mon Sep 17 00:00:00 2001 From: Lachlan Reynolds Date: Thu, 21 May 2026 20:19:22 +1200 Subject: [PATCH 3/8] add install logic --- ApiReference.php | 15 +++++++++++++++ Configuration.php | 7 ++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/ApiReference.php b/ApiReference.php index 6559037..29f2a4c 100644 --- a/ApiReference.php +++ b/ApiReference.php @@ -9,6 +9,8 @@ namespace Piwik\Plugins\ApiReference; +use Piwik\Plugins\CustomReports\Configuration; + class ApiReference extends \Piwik\Plugin { public const DEFAULT_SPEC_VERSION = '1.0.0'; @@ -43,4 +45,17 @@ public function getClientSideTranslationKeys(&$translationKeys): void $translationKeys[] = 'ApiReference_UserAuthenticationManageTokens'; $translationKeys[] = 'ApiReference_UserAuthenticationUsingTokenAuth'; } + + + public function install() + { + $config = new Configuration(); + $config->install(); + } + + public function uninstall() + { + $config = new Configuration(); + $config->uninstall(); + } } diff --git a/Configuration.php b/Configuration.php index a169e4a..6207a97 100644 --- a/Configuration.php +++ b/Configuration.php @@ -45,7 +45,12 @@ public function install() public function uninstall() { $config = $this->getConfig(); - $config->CustomReports = array(); + $config->ApiReference = array(); $config->forceSave(); } + + private function getConfig() + { + return Config::getInstance(); + } } From ec2654528877b1e6b2f9899e6fc39079af7d7248 Mon Sep 17 00:00:00 2001 From: Lachlan Reynolds Date: Thu, 21 May 2026 20:22:13 +1200 Subject: [PATCH 4/8] clean code --- ApiReference.php | 1 - Configuration.php | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/ApiReference.php b/ApiReference.php index 29f2a4c..b85009a 100644 --- a/ApiReference.php +++ b/ApiReference.php @@ -46,7 +46,6 @@ public function getClientSideTranslationKeys(&$translationKeys): void $translationKeys[] = 'ApiReference_UserAuthenticationUsingTokenAuth'; } - public function install() { $config = new Configuration(); diff --git a/Configuration.php b/Configuration.php index 6207a97..24d4274 100644 --- a/Configuration.php +++ b/Configuration.php @@ -29,7 +29,7 @@ public function install() $config = $this->getConfig(); $apiReferenceConfig = $config->ApiReference; - if (empty($reports)) { + if (empty($apiReferenceConfig)) { $apiReferenceConfig = array(); } From 31dba1593b52c0e06e26743e1e474c378c8e2330 Mon Sep 17 00:00:00 2001 From: Lachlan Reynolds Date: Thu, 21 May 2026 20:30:05 +1200 Subject: [PATCH 5/8] fix issues --- Configuration.php | 25 +++++++++++++++++++++++-- Tasks.php | 11 +++++++++-- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/Configuration.php b/Configuration.php index 24d4274..bccc077 100644 --- a/Configuration.php +++ b/Configuration.php @@ -17,11 +17,10 @@ namespace Piwik\Plugins\ApiReference; use Piwik\Config; -use Piwik\Piwik; class Configuration { - public const DEFAULT_ENABLE_SPEC_GENERATION= 1; + public const DEFAULT_ENABLE_SPEC_GENERATION = 1; public const KEY_ENABLE_SPEC_GENERATION = 'enable_spec_generation_task'; public function install() @@ -49,6 +48,28 @@ public function uninstall() $config->forceSave(); } + + public function specGenerationEnabled() + { + $value = $this->getConfigValue(self::KEY_ENABLE_SPEC_GENERATION, self::DEFAULT_ENABLE_SPEC_GENERATION); + + if ($value === false || $value === '' || $value === null) { + $value = self::DEFAULT_ENABLE_SPEC_GENERATION; + } + + return (bool) $value; + } + + private function getConfigValue($name, $default) + { + $config = $this->getConfig(); + $attribution = $config->ApiReference; + if (isset($attribution[$name])) { + return $attribution[$name]; + } + return $default; + } + private function getConfig() { return Config::getInstance(); diff --git a/Tasks.php b/Tasks.php index b4e1a6d..4401d4f 100644 --- a/Tasks.php +++ b/Tasks.php @@ -15,6 +15,7 @@ use Piwik\Log\LoggerInterface; use Piwik\Plugins\ApiReference\Generation\PluginListProvider; use Piwik\Plugins\ApiReference\Generation\SpecGenerationService; +use Piwik\Plugins\ApiReference\Configuration; class Tasks extends \Piwik\Plugin\Tasks { @@ -32,15 +33,21 @@ class Tasks extends \Piwik\Plugin\Tasks * @var PluginListProvider */ private $pluginListProvider; + /** + * @var Configuration + */ + private $config; public function __construct( SpecGenerationService $specGenerationService, LoggerInterface $logger, - ?PluginListProvider $pluginListProvider = null + ?PluginListProvider $pluginListProvider = null, + ?Configuration $config = null ) { $this->specGenerationService = $specGenerationService; $this->logger = $logger; $this->pluginListProvider = $pluginListProvider ?? new PluginListProvider(); + $this->config = $config ?? new Configuration(); } public function schedule() @@ -77,6 +84,6 @@ public function generateConfiguredPluginSpecs(): void private function isSpecGenerationEnabled(): bool { - return (bool) (Config::getInstance()->ApiReference['enable_spec_generation_task'] ?? 0); + return $this->config->specGenerationEnabled(); } } From 02d43957ece931191d518fd0bd6964b00fe590fe Mon Sep 17 00:00:00 2001 From: Lachlan Reynolds Date: Thu, 21 May 2026 20:36:52 +1200 Subject: [PATCH 6/8] Tests and fixes --- ApiReference.php | 2 - Configuration.php | 2 +- Tasks.php | 2 - tests/Unit/ConfigurationTest.php | 83 ++++++++++++++++++++++++++++++++ 4 files changed, 84 insertions(+), 5 deletions(-) create mode 100644 tests/Unit/ConfigurationTest.php diff --git a/ApiReference.php b/ApiReference.php index b85009a..79dabd5 100644 --- a/ApiReference.php +++ b/ApiReference.php @@ -9,8 +9,6 @@ namespace Piwik\Plugins\ApiReference; -use Piwik\Plugins\CustomReports\Configuration; - class ApiReference extends \Piwik\Plugin { public const DEFAULT_SPEC_VERSION = '1.0.0'; diff --git a/Configuration.php b/Configuration.php index bccc077..821f7db 100644 --- a/Configuration.php +++ b/Configuration.php @@ -33,7 +33,7 @@ public function install() } // we make sure to set a value only if none has been configured yet, eg in common config. - if (empty($apiReferenceConfig[self::KEY_ENABLE_SPEC_GENERATION])) { + if (!array_key_exists(self::KEY_ENABLE_SPEC_GENERATION, $apiReferenceConfig)) { $apiReferenceConfig[self::KEY_ENABLE_SPEC_GENERATION] = self::DEFAULT_ENABLE_SPEC_GENERATION; } diff --git a/Tasks.php b/Tasks.php index 4401d4f..199c159 100644 --- a/Tasks.php +++ b/Tasks.php @@ -11,11 +11,9 @@ namespace Piwik\Plugins\ApiReference; -use Piwik\Config; use Piwik\Log\LoggerInterface; use Piwik\Plugins\ApiReference\Generation\PluginListProvider; use Piwik\Plugins\ApiReference\Generation\SpecGenerationService; -use Piwik\Plugins\ApiReference\Configuration; class Tasks extends \Piwik\Plugin\Tasks { diff --git a/tests/Unit/ConfigurationTest.php b/tests/Unit/ConfigurationTest.php new file mode 100644 index 0000000..4a99add --- /dev/null +++ b/tests/Unit/ConfigurationTest.php @@ -0,0 +1,83 @@ +originalConfig = Config::getInstance()->ApiReference ?? null; + } + + protected function tearDown(): void + { + Config::getInstance()->ApiReference = $this->originalConfig; + + parent::tearDown(); + } + + public function testInstallSetsDefaultWhenConfigKeyIsMissing(): void + { + Config::getInstance()->ApiReference = []; + + $configuration = new Configuration(); + $configuration->install(); + + $this->assertSame( + Configuration::DEFAULT_ENABLE_SPEC_GENERATION, + Config::getInstance()->ApiReference[Configuration::KEY_ENABLE_SPEC_GENERATION] + ); + } + + public function testInstallPreservesExplicitDisabledValue(): void + { + Config::getInstance()->ApiReference = [ + Configuration::KEY_ENABLE_SPEC_GENERATION => 0, + ]; + + $configuration = new Configuration(); + $configuration->install(); + + $this->assertSame(0, Config::getInstance()->ApiReference[Configuration::KEY_ENABLE_SPEC_GENERATION]); + } + + public function testUninstallClearsApiReferenceConfig(): void + { + Config::getInstance()->ApiReference = [ + Configuration::KEY_ENABLE_SPEC_GENERATION => 1, + 'another_key' => 'value', + ]; + + $configuration = new Configuration(); + $configuration->uninstall(); + + $this->assertSame([], Config::getInstance()->ApiReference); + } +} From a59244d6e2682bd6a23dcf45f8bc5b76263b2da1 Mon Sep 17 00:00:00 2001 From: Lachlan Reynolds Date: Fri, 22 May 2026 10:30:01 +1200 Subject: [PATCH 7/8] Fix licence --- Configuration.php | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/Configuration.php b/Configuration.php index 821f7db..7046fcf 100644 --- a/Configuration.php +++ b/Configuration.php @@ -1,19 +1,13 @@ Date: Fri, 22 May 2026 10:31:22 +1200 Subject: [PATCH 8/8] phpcs --- Configuration.php | 1 - 1 file changed, 1 deletion(-) diff --git a/Configuration.php b/Configuration.php index 7046fcf..7c94f15 100644 --- a/Configuration.php +++ b/Configuration.php @@ -7,7 +7,6 @@ * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later */ - namespace Piwik\Plugins\ApiReference; use Piwik\Config;