diff --git a/Classes/Domain/Model/Pageview.php b/Classes/Domain/Model/Pageview.php new file mode 100644 index 0000000..6f39b83 --- /dev/null +++ b/Classes/Domain/Model/Pageview.php @@ -0,0 +1,91 @@ + + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +use TYPO3\CMS\Core\Site\Entity\SiteLanguage; + +class Pageview +{ + /** + * @var int + */ + private $pageUid; + + /** + * @var SiteLanguage + */ + private $language; + + /** + * @var \DateTimeImmutable + */ + private $crdate; + + /** + * @var int + */ + private $pageType; + + /** + * @var string + */ + private $url; + + public function __construct( + int $pageUid, + SiteLanguage $language, + \DateTimeImmutable $crdate, + int $pageType, + string $url + ) { + $this->pageUid = $pageUid; + $this->language = $language; + $this->crdate = $crdate; + $this->pageType = $pageType; + $this->url = $url; + } + + public function getPageUid(): int + { + return $this->pageUid; + } + + public function getLanguage(): SiteLanguage + { + return $this->language; + } + + public function getCrdate(): \DateTimeImmutable + { + return $this->crdate; + } + + public function getPageType(): int + { + return $this->pageType; + } + + public function getUrl(): string + { + return $this->url; + } +} diff --git a/Classes/Domain/Pageview/Factory.php b/Classes/Domain/Pageview/Factory.php new file mode 100644 index 0000000..cd5faea --- /dev/null +++ b/Classes/Domain/Pageview/Factory.php @@ -0,0 +1,46 @@ + + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +use DanielSiepmann\Tracking\Domain\Model\Pageview; +use Psr\Http\Message\ServerRequestInterface; +use TYPO3\CMS\Core\Routing\PageArguments; +use TYPO3\CMS\Core\Site\Entity\SiteLanguage; + +class Factory implements FromRequest +{ + public static function fromRequest(ServerRequestInterface $request): Pageview + { + return new PageView( + static::getRouting($request)->getPageId(), + $request->getAttribute('language'), + new \DateTimeImmutable(), + static::getRouting($request)->getPageType(), + (string) $request->getUri() + ); + } + + private static function getRouting(ServerRequestInterface $request): PageArguments + { + return $request->getAttribute('routing'); + } +} diff --git a/Classes/Domain/Pageview/FromRequest.php b/Classes/Domain/Pageview/FromRequest.php new file mode 100644 index 0000000..de4967e --- /dev/null +++ b/Classes/Domain/Pageview/FromRequest.php @@ -0,0 +1,30 @@ + + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +use DanielSiepmann\Tracking\Domain\Model\Pageview; +use Psr\Http\Message\ServerRequestInterface; + +interface FromRequest +{ + public static function fromRequest(ServerRequestInterface $request): Pageview; +} diff --git a/Classes/Domain/Repository/Pageview.php b/Classes/Domain/Repository/Pageview.php new file mode 100644 index 0000000..b6b7793 --- /dev/null +++ b/Classes/Domain/Repository/Pageview.php @@ -0,0 +1,53 @@ + + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +use DanielSiepmann\Tracking\Domain\Model\Pageview as Model; +use TYPO3\CMS\Core\Database\Connection; + +class Pageview +{ + /** + * @var Connection + */ + private $connection; + + public function __construct(Connection $connection) + { + $this->connection = $connection; + } + + public function add(Model $pageview) + { + $this->connection->insert( + 'tx_tracking_pageview', + [ + 'pid' => $pageview->getPageUid(), + 'crdate' => $pageview->getCrdate()->format('U'), + 'tstamp' => $pageview->getCrdate()->format('U'), + 'type' => $pageview->getPageType(), + 'sys_language_uid' => $pageview->getLanguage()->getLanguageId(), + 'url' => $pageview->getUrl(), + ] + ); + } +} diff --git a/Classes/Middleware/Pageview.php b/Classes/Middleware/Pageview.php new file mode 100644 index 0000000..d251d90 --- /dev/null +++ b/Classes/Middleware/Pageview.php @@ -0,0 +1,51 @@ + + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +use DanielSiepmann\Tracking\Domain\Pageview\Factory; +use DanielSiepmann\Tracking\Domain\Repository\Pageview as Repository; +use Psr\Http\Message\ResponseInterface; +use Psr\Http\Message\ServerRequestInterface; +use Psr\Http\Server\MiddlewareInterface; +use Psr\Http\Server\RequestHandlerInterface; + +class Pageview implements MiddlewareInterface +{ + /** + * @var Repository + */ + private $repository; + + public function __construct(Repository $repository) + { + $this->repository = $repository; + } + + public function process( + ServerRequestInterface $request, + RequestHandlerInterface $handler + ): ResponseInterface { + $this->repository->add(Factory::fromRequest($request)); + + return $handler->handle($request); + } +} diff --git a/Configuration/RequestMiddlewares.php b/Configuration/RequestMiddlewares.php new file mode 100644 index 0000000..a0d00ff --- /dev/null +++ b/Configuration/RequestMiddlewares.php @@ -0,0 +1,15 @@ + [ + 'tracking-pageview' => [ + 'target' => \DanielSiepmann\Tracking\Middleware\Pageview::class, + 'before' => [ + 'typo3/cms-frontend/content-length-headers', + ], + 'after' => [ + 'typo3/cms-frontend/shortcut-and-mountpoint-redirect', + ], + ], + ], +]; diff --git a/Configuration/Services.yaml b/Configuration/Services.yaml new file mode 100644 index 0000000..80bf2ef --- /dev/null +++ b/Configuration/Services.yaml @@ -0,0 +1,20 @@ +services: + _defaults: + autowire: true + autoconfigure: true + public: false + + DanielSiepmann\Tracking\: + resource: '../Classes/*' + + DanielSiepmann\DI\DatabaseConnection\Pageview: + factory: + - '@TYPO3\CMS\Core\Database\ConnectionPool' + - 'getConnectionForTable' + arguments: + - 'tx_tracking_pageview' + + DanielSiepmann\Tracking\Domain\Repository\Pageview: + public: true + arguments: + - '@DanielSiepmann\DI\DatabaseConnection\Pageview' diff --git a/Configuration/TCA/tx_tracking_pageview.php b/Configuration/TCA/tx_tracking_pageview.php new file mode 100644 index 0000000..686b416 --- /dev/null +++ b/Configuration/TCA/tx_tracking_pageview.php @@ -0,0 +1,71 @@ + [ + 'label' => 'url', + 'label_alt' => 'crdate', + 'label_alt_force' => true, + 'sortby' => 'crdate', + 'tstamp' => 'tstamp', + 'crdate' => 'crdate', + 'cruser_id' => 'cruser_id', + 'languageField' => 'sys_language_uid', + 'title' => 'LLL:EXT:tracking/Resources/Private/Language/locallang_tca.xlf:table.pageview', + 'searchFields' => 'uid, url' + ], + 'interface' => [ + 'always_description' => 0, + 'showRecordFieldList' => 'url, type, sys_language_uid, crdate, tstamp, crdate, cruser_id' + ], + 'types' => [ + '0' => [ + 'showitem' => 'sys_language_uid, pid, url, type, crdate', + ], + ], + 'columns' => [ + 'pid' => [ + 'label' => 'LLL:EXT:tracking/Resources/Private/Language/locallang_tca.xlf:table.pageview.pid', + 'config' => [ + 'type' => 'select', + 'readOnly' => true, + 'renderType' => 'selectSingle', + 'foreign_table' => 'pages', + ], + ], + 'crdate' => [ + 'label' => 'LLL:EXT:tracking/Resources/Private/Language/locallang_tca.xlf:table.pageview.crdate', + 'config' => [ + 'type' => 'input', + 'eval' => 'datetime', + ], + ], + 'sys_language_uid' => [ + 'label' => 'LLL:EXT:tracking/Resources/Private/Language/locallang_tca.xlf:table.pageview.sys_language', + 'config' => [ + 'type' => 'select', + 'renderType' => 'selectSingle', + 'foreign_table' => 'sys_language', + 'items' => [ + ['LLL:EXT:tracking/Resources/Private/Language/locallang_tca.xlf:table.pageview.sys_language.0', 0], + ], + 'readOnly' => true, + ] + ], + 'type' => [ + 'label' => 'LLL:EXT:tracking/Resources/Private/Language/locallang_tca.xlf:table.pageview.type', + 'config' => [ + 'type' => 'input', + 'readOnly' => true, + 'eval' => 'int', + ], + ], + 'url' => [ + 'label' => 'LLL:EXT:tracking/Resources/Private/Language/locallang_tca.xlf:table.pageview.url', + 'config' => [ + 'readOnly' => true, + 'type' => 'input', + 'size' => 50, + 'max' => 255, + ], + ], + ], +]; diff --git a/Resources/Private/Language/locallang_tca.xlf b/Resources/Private/Language/locallang_tca.xlf new file mode 100644 index 0000000..69269df --- /dev/null +++ b/Resources/Private/Language/locallang_tca.xlf @@ -0,0 +1,29 @@ + + + +
+ + + Pageview + + + Page + + + URL + + + System language + + + Default system language + + + Date + Time + + + Pagetype + + + + diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..08d996a --- /dev/null +++ b/composer.json @@ -0,0 +1,26 @@ +{ + "name": "danielsiepmann/tracking", + "description": "Tracking for TYPO3", + "type": "typo3-cms-extension", + "license": "GPL-2.0-or-later", + "authors": [ + { + "name": "Daniel Siepmann", + "email": "coding@daniel-siepmann.de" + } + ], + "autoload": { + "psr-4": { + "DanielSiepmann\\Tracking\\": "Classes/" + } + }, + "require": { + "php": "^7.3.0", + "typo3/cms-core": "10.x-dev" + }, + "extra": { + "typo3/cms": { + "extension-key": "tracking" + } + } +} diff --git a/ext_emconf.php b/ext_emconf.php new file mode 100644 index 0000000..3ae44bf --- /dev/null +++ b/ext_emconf.php @@ -0,0 +1,21 @@ + 'Tracking', + 'description' => 'Tracks page visits in TYPO3.', + 'category' => 'fe', + 'state' => 'stable', + 'uploadfolder' => 0, + 'createDirs' => '', + 'clearCacheOnLoad' => 0, + 'author' => 'Daniel Siepmann', + 'author_email' => 'coding@daniel-siepmann.de', + 'author_company' => '', + 'version' => '1.0.0', + 'constraints' => [ + 'depends' => [ + 'core' => '', + ], + 'conflicts' => [], + 'suggests' => [], + ], +]; diff --git a/ext_localconf.php b/ext_localconf.php new file mode 100644 index 0000000..cbdb66c --- /dev/null +++ b/ext_localconf.php @@ -0,0 +1,3 @@ +