Skip to content

Commit

Permalink
feat: add redirect mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
maikschneider committed Feb 15, 2024
1 parent f491f01 commit 2432a95
Show file tree
Hide file tree
Showing 6 changed files with 251 additions and 113 deletions.
43 changes: 39 additions & 4 deletions Classes/Command/CreateMappingsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,18 @@ public function execute(InputInterface $input, OutputInterface $output): int
{
$qb = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');

Check failure on line 39 in Classes/Command/CreateMappingsCommand.php

View workflow job for this annotation

GitHub Actions / php-stan

Call to an undefined method object::getQueryBuilderForTable().
$qb->getRestrictions()->removeAll();
$result = $qb->select('uid', 'slug')
$result = $qb->select('uid', 'slug', 'sys_language_uid')
->from('pages')
->execute();

$pages = $result->fetchAllAssociative();

$qb = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_redirect');

Check failure on line 46 in Classes/Command/CreateMappingsCommand.php

View workflow job for this annotation

GitHub Actions / php-stan

Call to an undefined method object::getQueryBuilderForTable().
$qb->getRestrictions()->removeAll();
$result = $qb->select('uid', 'source_path', 'is_regexp')
->from('sys_redirect')
->execute();
$redirects = $result->fetchAllAssociative();

$mappingPaths = $this->mappingRepository->getAllNonRegexPaths();
$paths = $this->dataProviderService->readJsonData();

Expand All @@ -56,20 +63,48 @@ public function execute(InputInterface $input, OutputInterface $output): int
continue;
}

foreach ($pages as $page) {
foreach ($pages as $key2 => $page) {
// no match
if ($page['slug'] !== $pathData->data) {
continue;
}

// create mapping
$data['tx_xmgoaccess_domain_model_mapping']['NEW' . $key] = [
$data['tx_xmgoaccess_domain_model_mapping']['NEW' . $key . $key2] = [
'pid' => 0,
'path' => $page['slug'],
'record_type' => 0,
'page' => $page['uid'],
];
}

foreach ($redirects as $key2 => $redirect) {
if (!$redirect['source_path'] || !is_string($redirect['source_path'])) {
continue;
}

// check regex
if ($redirect['is_regexp']) {
$matchResult = preg_match($redirect['source_path'], $pathData->data, $matches);
if ($matchResult === 0) {
continue;
}
}

// no match
if (!$redirect['is_regexp'] && $redirect['source_path'] !== $pathData->data) {
continue;
}

// create mapping
$data['tx_xmgoaccess_domain_model_mapping']['NEW' . $key . $key2] = [
'pid' => 0,
'path' => $pathData->data,
'record_type' => 3,
'foreign_uid' => $redirect['uid'],
'foreign_table' => 'sys_redirect',
];
}
}

if (!count($data['tx_xmgoaccess_domain_model_mapping'])) {
Expand Down
5 changes: 5 additions & 0 deletions Classes/Domain/Model/Dto/Demand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Demand
public bool $showPages = true;

public bool $showActions = true;
public bool $showRedirects = false;

public static function createFromRequest(Request $request): self
{
Expand All @@ -31,6 +32,10 @@ public static function createFromRequest(Request $request): self
$demand->showActions = false;
}

if (isset($postData['showRedirects']) && (int)$postData['showRedirects']) {
$demand->showRedirects = true;
}

return $demand;
}
}
42 changes: 26 additions & 16 deletions Classes/Service/DataProviderService.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,6 @@ public function getRequestList(?Demand $demand = null)
{
$goaccessData = $this->readJsonData();

$this->mappings = $this->mappingRepository->findAll();

$items = [];

foreach ($goaccessData['requests']->data as $pathData) {
Expand All @@ -90,18 +88,26 @@ public function getRequestList(?Demand $demand = null)
'hits' => $pathData->hits->count,
'visitors' => $pathData->visitors->count,
'path' => $path,
'mapping' => $this->resolvePathMapping($path),
'mappings' => $this->resolvePathMapping($path),
];

if ($demand && $item['mapping']) {
if (!$demand->showPages && $item['mapping']->getRecordType() === 0) {
continue;
if (!$demand || empty($item['mappings'])) {
$items[] = $item;
continue;
}

foreach ($item['mappings'] as $mapping) {
if (!$demand->showPages && $mapping->getRecordType() === 0) {
continue 2;
}
if (!$demand->showActions && $item['mapping']->getRecordType() === 1) {
continue;
if (!$demand->showActions && $mapping->getRecordType() === 1) {
continue 2;
}
if (!$demand->showIgnored && $item['mapping']->getRecordType() === 2) {
continue;
if (!$demand->showIgnored && $mapping->getRecordType() === 2) {
continue 2;
}
if (!$demand->showRedirects && $mapping->getRecordType() === 3) {
continue 2;
}
}

Expand Down Expand Up @@ -136,27 +142,31 @@ public function readJsonData(): array
return $content ? (array)json_decode($content) : [];
}

private function resolvePathMapping(string $path): ?Mapping
/**
* @return Mapping[]
*/
private function resolvePathMapping(string $path): array
{
foreach ($this->mappings as $mapping) {
$mappings = [];
foreach ($this->mappingRepository->findAll() as $mapping) {
if ($mapping->isRegex()) {
preg_match('/' . $mapping->getPath() . '/', $path, $matches);
if ($matches) {
$this->enrichMapping($mapping);
return $mapping;
$mappings[] = $mapping;
}
}

if (!$mapping->isRegex() && $path === $mapping->getPath()) {
$this->enrichMapping($mapping);
return $mapping;
$mappings[] = $mapping;
}
}

return null;
return $mappings;
}

private function enrichMapping(Mapping &$mapping): void
private function enrichMapping(Mapping $mapping): void
{
if ($mapping->getRecordType() === 0 && $mapping->getPage()) {
$pageRecord = BackendUtility::readPageAccess($mapping->getPage(), '1=1');
Expand Down
15 changes: 15 additions & 0 deletions Configuration/TCA/tx_xmgoaccess_domain_model_mapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
2 => [
'showitem' => '--palette--;;pathSettings, record_type',
],
3 => [
'showitem' => '--palette--;;pathSettings, record_type, foreign_table, foreign_uid',
],
],
'palettes' => [
'pathSettings' => [
Expand Down Expand Up @@ -98,5 +101,17 @@
'default' => 0,
],
],
'foreign_uid' => [
'label' => 'Foreign UID',
'config' => [
'type' => 'input',
],
],
'foreign_table' => [
'label' => 'Foreign table',
'config' => [
'type' => 'input',
],
],
],
];
Loading

0 comments on commit 2432a95

Please sign in to comment.