Skip to content

Commit

Permalink
[FEATURE] Added TYPO3 13.4 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
derhansen committed Mar 4, 2025
1 parent cbd7b09 commit ba184ac
Show file tree
Hide file tree
Showing 17 changed files with 91 additions and 62 deletions.
24 changes: 24 additions & 0 deletions .Build/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "mittwald/mw-matomo-widget-composer",
"description": "composer.json for TYPO3 build",
"repositories": [
{
"type": "path",
"url": "../"
}
],
"require": {
"typo3/cms-core": "^13.4",
"typo3/cms-dashboard": "^13.4",
"mittwald/mw_matomo_widget": "@dev"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.49"
},
"config": {
"allow-plugins": {
"typo3/class-alias-loader": true,
"typo3/cms-composer-installers": true
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
->setRiskyAllowed(true)
->setRules([
'@DoctrineAnnotation' => true,
'@PER' => true,
'@PER-CS' => true,
'array_syntax' => ['syntax' => 'short'],
'cast_spaces' => ['space' => 'none'],
'concat_space' => ['spacing' => 'one'],
'declare_equal_normalize' => ['space' => 'none'],
'declare_parentheses' => true,
'dir_constant' => true,
'function_to_constant' => ['functions' => ['get_called_class', 'get_class', 'get_class_this', 'php_sapi_name', 'phpversion', 'pi']],
'function_typehint_space' => true,
'type_declaration_spaces' => true,
'modernize_strpos' => true,
'modernize_types_casting' => true,
'native_function_casing' => true,
Expand Down Expand Up @@ -60,7 +60,7 @@
])
->setFinder(
PhpCsFixer\Finder::create()
->in(__DIR__)
->in(dirname(__DIR__ . '/../../../'))
->exclude(['.Build', 'Documentation', 'Resources'])
->notName('ext_emconf.php')
);
4 changes: 2 additions & 2 deletions .github/workflows/code.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jobs:
build:
strategy:
matrix:
php: ['8.1', '8.2']
php: ['8.2', '8.3']
runs-on: ubuntu-latest
name: PHP ${{ matrix.php }}

Expand All @@ -33,4 +33,4 @@ jobs:
- name: Validate PHP coding guidelines
run: |
.Build/bin/php-cs-fixer fix --config=.php-cs-fixer.dist.php -v --dry-run --stop-on-violation --using-cache=no
.Build/vendor/bin/php-cs-fixer fix --config=.Build/php-cs-fixer/.php-cs-fixer.php -v --dry-run --stop-on-violation --using-cache=no
10 changes: 5 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# IDE
.idea

# Local files
.Build
.php-cs-fixer.cache

composer.lock
# Build files
.Build/public
.Build/vendor
.Build/composer.lock
.php-cs-fixer.cache
54 changes: 32 additions & 22 deletions Classes/Service/MatomoService.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use TYPO3\CMS\Core\Cache\CacheManager;
use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
use TYPO3\CMS\Core\Http\RequestFactory;
use TYPO3\CMS\Core\Utility\GeneralUtility;

class MatomoService
Expand All @@ -40,47 +41,52 @@ class MatomoService
protected FrontendInterface $cache;
protected array $extensionConfiguration = [];

public function __construct()
{
public function __construct(
protected readonly RequestFactory $requestFactory
) {
$this->cache = GeneralUtility::makeInstance(CacheManager::class)->getCache(self::EXT_KEY);
$this->extensionConfiguration = GeneralUtility::makeInstance(ExtensionConfiguration::class)
->get(self::EXT_KEY);
}

public function getMatomoData(string $data, bool $absolute = false): array
public function getMatomoData(string $method, bool $absolute = false): array
{
$content30days = $this->matomoApiRequest(['lastMinutes' => 43200]);
$content7days = $this->matomoApiRequest(['lastMinutes' => 10080]);
$content1day = $this->matomoApiRequest(['lastMinutes' => 1440]);
$content30days = $this->matomoApiRequest($method, ['period' => 'day', 'date' => 'last30']);
$content7days = $this->matomoApiRequest($method, ['period' => 'day', 'date' => 'last7']);
$content1day = $this->matomoApiRequest($method, ['period' => 'day', 'date' => 'today']);

if ($content1day === '' || $content7days === '' || $content30days === '') {
return [0, 0, 0];
}

if ($absolute) {
$actions30days = json_decode($content30days)[0]->$data;
$actions7days = json_decode($content7days)[0]->$data;
} else {
$actions30days = number_format(json_decode($content30days)[0]->$data / 30, 2);
$actions7days = number_format(json_decode($content7days)[0]->$data / 7, 2);
$actions30days = array_sum(array_values(json_decode($content30days, true)));
$actions7days = array_sum(array_values(json_decode($content7days, true)));
$actions1day = array_sum(array_values(json_decode($content1day, true)));
if (!$absolute) {
$actions30days = number_format($actions30days / 30, 2);
$actions7days = number_format($actions7days / 7, 2);
}
$actions1day = json_decode($content1day)[0]->$data;

return [$actions1day, $actions7days, $actions30days];
}

protected function matomoApiRequest(array $arguments = []): string
protected function matomoApiRequest(string $method, array $arguments = []): string
{
$siteId = $this->extensionConfiguration['matomoSiteId'] ?? 1;
$apiToken = $this->extensionConfiguration['matomoToken'] ?? '';
$baseUrl = $this->getBaseUrl();

$apiArguments = [
'module' => 'API',
'method' => 'Live.getCounters',
'method' => $method,
'idSite' => $siteId,
'format' => 'JSON',
'token_auth' => $apiToken,
];

$requestOptions = [
'form_params' => [
'token_auth' => $apiToken,
],
];

$apiArguments += $arguments;
Expand All @@ -89,14 +95,18 @@ protected function matomoApiRequest(array $arguments = []): string

$result = $this->cache->get($cacheHash);
if ($result === false || $result === null) {
$result = GeneralUtility::getUrl($url);
}

if ($result === false || !$this->isJson($result)) {
return '';
$response = $this->requestFactory->request($url, 'POST', $requestOptions);
if ($response->getStatusCode() !== 200) {
return '';
}
$result = (string)$response->getBody();

if (!$this->isJson($result)) {
return '';
}
$this->cache->set($cacheHash, $result, [], self::CACHE_LIFETIME);
}

$this->cache->set($cacheHash, $result, [], self::CACHE_LIFETIME);
return $result;
}

Expand Down
9 changes: 2 additions & 7 deletions Classes/Widgets/AbstractMatomoWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,13 @@ abstract class AbstractMatomoWidget implements ChartDataProviderInterface
{
private const LLL = 'LLL:EXT:mw_matomo_widget/Resources/Private/Language/locallang.xlf:';

protected MatomoService $matomoService;

public function __construct(MatomoService $matomoService)
{
$this->matomoService = $matomoService;
}
public function __construct(protected readonly MatomoService $matomoService) {}

public function getChartData(): array
{
return [
'labels' => [
'24 ' . $this->getLanguageService()->sL(self::LLL . 'label.hours'),
$this->getLanguageService()->sL(self::LLL . 'label.today'),
'7 ' . $this->getLanguageService()->sL(self::LLL . 'label.days'),
'30 ' . $this->getLanguageService()->sL(self::LLL . 'label.days'),
],
Expand Down
2 changes: 1 addition & 1 deletion Classes/Widgets/PageViewAbsoluteWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ class PageViewAbsoluteWidget extends AbstractMatomoWidget
{
protected function getData(): array
{
return $this->matomoService->getMatomoData('actions', true);
return $this->matomoService->getMatomoData('VisitsSummary.getActions', true);
}
}
2 changes: 1 addition & 1 deletion Classes/Widgets/PageViewWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ class PageViewWidget extends AbstractMatomoWidget
{
protected function getData(): array
{
return $this->matomoService->getMatomoData('actions');
return $this->matomoService->getMatomoData('VisitsSummary.getActions');
}
}
2 changes: 1 addition & 1 deletion Classes/Widgets/VisitorCountAbsoluteWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ class VisitorCountAbsoluteWidget extends AbstractMatomoWidget
{
protected function getData(): array
{
return $this->matomoService->getMatomoData('visitors', true);
return $this->matomoService->getMatomoData('VisitsSummary.getUniqueVisitors', true);
}
}
2 changes: 1 addition & 1 deletion Classes/Widgets/VisitorCountWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ class VisitorCountWidget extends AbstractMatomoWidget
{
protected function getData(): array
{
return $this->matomoService->getMatomoData('visitors');
return $this->matomoService->getMatomoData('VisitsSummary.getUniqueVisitors');
}
}
4 changes: 0 additions & 4 deletions Configuration/Services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ services:
class: 'TYPO3\CMS\Dashboard\Widgets\BarChartWidget'
arguments:
$dataProvider: '@Mittwald\MatomoWidget\Widgets\PageViewWidget'
$view: '@dashboard.views.widget'
tags:
- name: dashboard.widget
identifier: 'pageView'
Expand All @@ -25,7 +24,6 @@ services:
class: 'TYPO3\CMS\Dashboard\Widgets\BarChartWidget'
arguments:
$dataProvider: '@Mittwald\MatomoWidget\Widgets\PageViewAbsoluteWidget'
$view: '@dashboard.views.widget'
tags:
- name: dashboard.widget
identifier: 'pageViewAbsolute'
Expand All @@ -39,7 +37,6 @@ services:
class: 'TYPO3\CMS\Dashboard\Widgets\BarChartWidget'
arguments:
$dataProvider: '@Mittwald\MatomoWidget\Widgets\VisitorCountWidget'
$view: '@dashboard.views.widget'
tags:
- name: dashboard.widget
identifier: 'visitorCount'
Expand All @@ -53,7 +50,6 @@ services:
class: 'TYPO3\CMS\Dashboard\Widgets\BarChartWidget'
arguments:
$dataProvider: '@Mittwald\MatomoWidget\Widgets\VisitorCountAbsoluteWidget'
$view: '@dashboard.views.widget'
tags:
- name: dashboard.widget
identifier: 'visitorCountAbsolute'
Expand Down
Binary file modified Documentation/Images/mw-matomo-widget.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ Mittwald Matomo Widget is an extension for TYPO3 CMS to show visitor and page vi
The extension fetches statistics via the Matomo API and uses the TYPO3 caching framework to cache results
for one hour.

## Requirements

The extension is only compatible with Matomo version 5 or greater.

## Screenshots

![Screenshot of the dashboard widgets](Documentation/Images/mw-matomo-widget.png "Screenshot of the dashboard widgets")
Expand All @@ -29,6 +33,7 @@ In order to use the extension, it is required to configure Matomo settings.

| Version | TYPO3 | PHP | Support/Development |
|---------|-------|-----------|--------------------------------------|
| 4.x | 13.4 | >= 8.2 | Features, Bugfixes, Security Updates |
| 3.x | 12.4 | >= 8.1 | Features, Bugfixes, Security Updates |
| 2.x | 11.5 | 7.4 - 8.x | Features, Bugfixes, Security Updates |
| 2.x | 11.5 | 7.4 - 8.x | Bugfixes, Security Updates |
| 1.x | 10.4 | 7.2 - 7.4 | Bugfixes, Security Updates |
4 changes: 4 additions & 0 deletions Resources/Private/Language/de.locallang.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
<target>Zeigt die Seitenaufrufe aus Ihrer Matomo Instanz an.</target>
</trans-unit>

<trans-unit id="label.today" xml:space="preserve">
<source>Today</source>
<target>Heute</target>
</trans-unit>
<trans-unit id="label.hours" xml:space="preserve">
<source>hours</source>
<target>Stunden</target>
Expand Down
3 changes: 3 additions & 0 deletions Resources/Private/Language/locallang.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
<source>Shows visitor statistics from your Matomo instance.</source>
</trans-unit>

<trans-unit id="label.today" xml:space="preserve">
<source>Today</source>
</trans-unit>
<trans-unit id="label.hours" xml:space="preserve">
<source>hours</source>
</trans-unit>
Expand Down
16 changes: 4 additions & 12 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,24 @@
"type": "typo3-cms-extension",
"license": "GPL-2.0-or-later",
"require": {
"php": ">=8.1.0",
"typo3/cms-core": "^12.4",
"typo3/cms-dashboard": "^12.4"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.0"
"php": ">=8.2.0",
"typo3/cms-core": "^13.4",
"typo3/cms-dashboard": "^13.4"
},
"autoload": {
"psr-4": {
"Mittwald\\MatomoWidget\\": "Classes"
}
},
"config": {
"vendor-dir": ".Build/vendor",
"bin-dir": ".Build/bin",
"allow-plugins": {
"typo3/class-alias-loader": true,
"typo3/cms-composer-installers": true
}
},
"extra": {
"typo3/cms": {
"cms-package-dir": "{$vendor-dir}/typo3/cms",
"extension-key": "mw_matomo_widget",
"app-dir": ".Build",
"web-dir": ".Build/public"
"extension-key": "mw_matomo_widget"
}
}
}
4 changes: 2 additions & 2 deletions ext_emconf.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
'author_company' => 'Mittwald CM Service GmbH',
'state' => 'stable',
'clearCacheOnLoad' => true,
'version' => '3.0.0',
'version' => '4.0.0',
'uploadfolder' => false,
'autoload' => [
'psr-4' => [
Expand All @@ -18,7 +18,7 @@
],
'constraints' => [
'depends' => [
'typo3' => '12.4.0-12.4.99',
'typo3' => '13.4.0-13.4.99',
],
'conflicts' => [],
'suggests' => [],
Expand Down

0 comments on commit ba184ac

Please sign in to comment.