Skip to content

Commit 25ce58a

Browse files
authored
Merge branch '5.x-dev' into security-fixes
2 parents 884f608 + 51a5c5a commit 25ce58a

18 files changed

Lines changed: 545 additions & 113 deletions

.github/workflows/matomo-tests.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Action for running tests
22
# This file has been automatically created.
33
# To recreate it you can run this command
4-
# ./console generate:test-action --plugin="ApiReference" --php-versions="7.2,8.4" --schedule-cron="0 19 * * 6"
4+
# ./console generate:test-action --plugin="ApiReference" --php-versions="7.4,8.4" --schedule-cron="0 19 * * 6"
55

66
name: Plugin ApiReference Tests
77

@@ -37,7 +37,7 @@ jobs:
3737
strategy:
3838
fail-fast: false
3939
matrix:
40-
php: [ '7.2', '8.4' ]
40+
php: [ '7.4', 'matomo5_max_php' ]
4141
target: ['minimum_required_matomo', 'maximum_supported_matomo']
4242
steps:
4343
- uses: actions/checkout@v3
@@ -55,4 +55,4 @@ jobs:
5555
matomo-test-branch: ${{ matrix.target }}
5656
redis-service: true
5757
artifacts-pass: ${{ secrets.ARTIFACTS_PASS }}
58-
upload-artifacts: ${{ matrix.php == '7.2' && matrix.target == 'maximum_supported_matomo' }}
58+
upload-artifacts: ${{ matrix.php == '7.4' && matrix.target == 'maximum_supported_matomo' }}

ApiReference.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,32 @@ public function registerEvents()
2929

3030
public function getClientSideTranslationKeys(&$translationKeys): void
3131
{
32+
$translationKeys[] = 'General_API';
3233
$translationKeys[] = 'CoreHome_LearnMoreFullStop';
34+
$translationKeys[] = 'ApiReference_LookingForLegacyApiReference';
3335
$translationKeys[] = 'ApiReference_ReportingApiMoreInformation';
3436
$translationKeys[] = 'ApiReference_ReportingApiReference';
3537
$translationKeys[] = 'ApiReference_ReportingApiSummary';
36-
$translationKeys[] = 'ApiReference_SwaggerApi';
3738
$translationKeys[] = 'ApiReference_SwaggerPagePluginEmpty';
3839
$translationKeys[] = 'ApiReference_SwaggerPageRequestFailed';
3940
$translationKeys[] = 'ApiReference_SwaggerPageSpecLoadFailed';
41+
$translationKeys[] = 'ApiReference_SwaggerPageSpecNotAvailable';
4042
$translationKeys[] = 'ApiReference_SwaggerPageSearchNoResults';
4143
$translationKeys[] = 'ApiReference_SwaggerPageSearchPlaceholder';
4244
$translationKeys[] = 'ApiReference_UserAuthentication';
4345
$translationKeys[] = 'ApiReference_UserAuthenticationManageTokens';
4446
$translationKeys[] = 'ApiReference_UserAuthenticationUsingTokenAuth';
4547
}
48+
49+
public function install()
50+
{
51+
$config = new Configuration();
52+
$config->install();
53+
}
54+
55+
public function uninstall()
56+
{
57+
$config = new Configuration();
58+
$config->uninstall();
59+
}
4660
}

Configuration.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
/**
4+
* Matomo - free/libre analytics platform
5+
*
6+
* @link https://matomo.org
7+
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
8+
*/
9+
10+
namespace Piwik\Plugins\ApiReference;
11+
12+
use Piwik\Config;
13+
14+
class Configuration
15+
{
16+
public const DEFAULT_ENABLE_SPEC_GENERATION = 1;
17+
public const KEY_ENABLE_SPEC_GENERATION = 'enable_spec_generation_task';
18+
19+
public function install()
20+
{
21+
$config = $this->getConfig();
22+
23+
$apiReferenceConfig = $config->ApiReference;
24+
if (empty($apiReferenceConfig)) {
25+
$apiReferenceConfig = array();
26+
}
27+
28+
// we make sure to set a value only if none has been configured yet, eg in common config.
29+
if (!array_key_exists(self::KEY_ENABLE_SPEC_GENERATION, $apiReferenceConfig)) {
30+
$apiReferenceConfig[self::KEY_ENABLE_SPEC_GENERATION] = self::DEFAULT_ENABLE_SPEC_GENERATION;
31+
}
32+
33+
$config->ApiReference = $apiReferenceConfig;
34+
$config->forceSave();
35+
}
36+
37+
public function uninstall()
38+
{
39+
$config = $this->getConfig();
40+
$config->ApiReference = array();
41+
$config->forceSave();
42+
}
43+
44+
45+
public function specGenerationEnabled()
46+
{
47+
$value = $this->getConfigValue(self::KEY_ENABLE_SPEC_GENERATION, self::DEFAULT_ENABLE_SPEC_GENERATION);
48+
49+
if ($value === false || $value === '' || $value === null) {
50+
$value = self::DEFAULT_ENABLE_SPEC_GENERATION;
51+
}
52+
53+
return (bool) $value;
54+
}
55+
56+
private function getConfigValue($name, $default)
57+
{
58+
$config = $this->getConfig();
59+
$attribution = $config->ApiReference;
60+
if (isset($attribution[$name])) {
61+
return $attribution[$name];
62+
}
63+
return $default;
64+
}
65+
66+
private function getConfig()
67+
{
68+
return Config::getInstance();
69+
}
70+
}

Controller.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Piwik\Plugins\ApiReference;
1313

1414
use Piwik\Piwik;
15+
use Piwik\Plugins\UsersManager\UserPreferences;
1516
use Piwik\View;
1617

1718
class Controller extends \Piwik\Plugin\ControllerAdmin
@@ -22,7 +23,19 @@ public function swagger(): string
2223

2324
$view = new View('@ApiReference/swagger');
2425
$this->setBasicVariablesView($view);
26+
$view->defaultWebsiteId = $this->getDefaultWebsiteId();
2527

2628
return $view->render();
2729
}
30+
31+
private function getDefaultWebsiteId(): ?int
32+
{
33+
$defaultWebsiteId = (new UserPreferences())->getDefaultWebsiteId();
34+
35+
if (is_numeric($defaultWebsiteId)) {
36+
return (int) $defaultWebsiteId;
37+
}
38+
39+
return null;
40+
}
2841
}

Menu.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ public function configureAdminMenu(MenuAdmin $menu): void
2020
return;
2121
}
2222

23-
$menu->addPlatformItem(
24-
'ApiReference_SwaggerApi',
25-
$this->urlForAction('swagger'),
26-
30
23+
$menu->editUrl(
24+
'CorePluginsAdmin_MenuPlatform',
25+
'General_API',
26+
$this->urlForAction('swagger')
2727
);
2828
}
2929
}

Tasks.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
namespace Piwik\Plugins\ApiReference;
1313

14-
use Piwik\Config;
1514
use Piwik\Log\LoggerInterface;
1615
use Piwik\Plugins\ApiReference\Generation\PluginListProvider;
1716
use Piwik\Plugins\ApiReference\Generation\SpecGenerationService;
@@ -32,15 +31,21 @@ class Tasks extends \Piwik\Plugin\Tasks
3231
* @var PluginListProvider
3332
*/
3433
private $pluginListProvider;
34+
/**
35+
* @var Configuration
36+
*/
37+
private $config;
3538

3639
public function __construct(
3740
SpecGenerationService $specGenerationService,
3841
LoggerInterface $logger,
39-
?PluginListProvider $pluginListProvider = null
42+
?PluginListProvider $pluginListProvider = null,
43+
?Configuration $config = null
4044
) {
4145
$this->specGenerationService = $specGenerationService;
4246
$this->logger = $logger;
4347
$this->pluginListProvider = $pluginListProvider ?? new PluginListProvider();
48+
$this->config = $config ?? new Configuration();
4449
}
4550

4651
public function schedule()
@@ -77,6 +82,6 @@ public function generateConfiguredPluginSpecs(): void
7782

7883
private function isSpecGenerationEnabled(): bool
7984
{
80-
return (bool) (Config::getInstance()->ApiReference['enable_spec_generation_task'] ?? 0);
85+
return $this->config->specGenerationEnabled();
8186
}
8287
}

lang/en.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
{
22
"ApiReference": {
3+
"LookingForLegacyApiReference": "Looking for the legacy API reference? The %1$slegacy API reference%2$s page is still available while we continue improving the new API documentation. If anything is missing or behaving unexpectedly in this new version, %3$splease let us know%4$s so we can improve the experience.",
34
"ReportingApiMoreInformation": "More info about the Matomo APIs available in %1$sIntroduction to Matomo API%2$s and the %3$sMatomo API Reference%4$s.",
45
"ReportingApiReference": "Reporting API Reference",
56
"ReportingApiSummary": "All the data in Matomo is available through simple APIs.",
6-
"SwaggerApi": "Swagger API",
77
"SwaggerPagePluginEmpty": "No plugins are configured in the ApiReference whitelist.",
88
"SwaggerPageRequestFailed": "The plugin whitelist could not be loaded.",
9-
"SwaggerPageSpecLoadFailed": "The OpenAPI spec could not be loaded for this plugin.",
109
"SwaggerPageSearchNoResults": "No plugins match your search.",
1110
"SwaggerPageSearchPlaceholder": "Search by plugin name",
1211
"UseTryItOutForLiveResponse": "Example responses require Super User access. Use Try it out to see a live response.",
12+
"SwaggerPageSpecLoadFailed": "The OpenAPI spec could not be loaded for this plugin.",
13+
"SwaggerPageSpecNotAvailable": "The OpenAPI spec is not available for this plugin yet. %1$sLearn more%2$s",
1314
"UserAuthentication": "User authentication",
1415
"UserAuthenticationManageTokens": "You can manage your authentication tokens on your security page.",
1516
"UserAuthenticationUsingTokenAuth": "If you want to request data within a script, a crontab, etc. you need to add the '%3$s' URL parameter to the API calls for URLs that require authentication."

plugin.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"license": "GPL v3+",
1414
"homepage": "https:\/\/matomo.org",
1515
"require": {
16+
"php": ">=7.4.0",
1617
"matomo": ">=5.0.0-stable,<6.0.0-b1"
1718
},
1819
"authors": [

templates/swagger.twig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77
<script src="plugins/ApiReference/vue/lib/swagger-ui/swagger-ui-bundle.js"></script>
88
{% endblock %}
99

10-
{% set title %}{{ 'ApiReference_SwaggerApi'|translate }}{% endset %}
10+
{% set title %}{{ 'General_API'|translate }}{% endset %}
1111

1212
{% block content %}
1313
<div
1414
vue-entry="ApiReference.SwaggerPage"
15+
default-website-id="{{ defaultWebsiteId|default(null)|json_encode }}"
1516
piwik-url="{{ piwikUrl|default(null)|json_encode }}"
1617
></div>
1718
{% endblock %}

tests/Integration/ControllerTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ public function testSwaggerRendersAdminPageForViewAccess(): void
9191

9292
$this->assertNotSame('', $html);
9393
$this->assertStringContainsString('vue-entry="ApiReference.SwaggerPage"', $html);
94+
$this->assertStringContainsString('default-website-id="1"', $html);
9495
$this->assertStringContainsString('piwik-url=', $html);
9596
$this->assertStringContainsString('plugins/ApiReference/vue/lib/swagger-ui/swagger-ui.css', $html);
9697
$this->assertStringContainsString('plugins/ApiReference/vue/src/SwaggerPage/swagger-ui-overrides.css', $html);

0 commit comments

Comments
 (0)