Skip to content
This repository has been archived by the owner on Aug 19, 2024. It is now read-only.

Use post process hook #8

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions Classes/Agents/PageMovedAgent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Pluswerk\CacheAutomation\Agents;

use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Database\QueryGenerator;

class PageMovedAgent extends AbstractAgent
{
public function getExpiredPages(string $table, $uid, array $agentConfiguration, array $changedFields): array
{
$pagesUidList = [];
if ($table === 'pages') {
if ($this->pageMovedInTree($changedFields)) {
$queryGenerator = GeneralUtility::makeInstance(QueryGenerator::class);
$pages = $queryGenerator->getTreeList($uid, 99, 0, 1);
$pagesUidList = array_merge($pagesUidList, explode(',', $pages));
}
}

return $pagesUidList;
}

/**
* Returns true, if the page was moved via the page tree
*
* @param array $changedFields
* @return bool
*/
protected function pageMovedInTree(array $changedFields): bool
{
return array_keys($changedFields) === ['uid'];
}
}
35 changes: 33 additions & 2 deletions Classes/Hook/DataHandlerDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,32 @@ public function __construct()
*/
public function processDatamap_afterDatabaseOperations($status, string $table, $id, array $changedFields, DataHandler $dataHandler)
{
// @codingStandardsIgnoreEnd
$this->clearCachedPagesFoundByAgents($table, $id, $changedFields);

}

/**
* @param string $status The status of the record
* @param string $table Database table name
* @param int|string $id The uid of the record or something like "NEW59785a1ec52" if the record is new
* @param int $pageIdentifier The id of the page
* @throws \Exception
*/
public function processCmdmap_postProcess($status, $table, $id, $pageIdentifier)
{
$this->clearCachedPagesFoundByAgents($table, $id, ['uid' => $pageIdentifier]);

}

/**
* @param string $table
* @param $id
* @param array $changedFields of an array containing just the uid of a page
* @throws \Exception
*/
protected function clearCachedPagesFoundByAgents(string $table, $id, array $changedFields)
{
// @codingStandardsIgnoreEnd
$expiredPages = [];
if ($this->configuration->isConfigured($table)) {
$agentConfigurations = $this->configuration->getAgentsForTable($table);
Expand All @@ -74,7 +99,13 @@ public function processDatamap_afterDatabaseOperations($status, string $table, $
}

if (count($expiredPages) !== 0) {
$this->cacheService->clearPageCache(array_unique($expiredPages));
$expiredPages = array_unique($expiredPages);
// TODO: use new API in TYPO3 V9
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please no TODO comments in a pull request for master

$extensionConfiguration = $extensionConfiguration = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['cache_automation']);
$arrayChunks = array_chunk($expiredPages, $extensionConfiguration['numberOfCachedPagesToClear']);
foreach ($arrayChunks as $singleChunk) {
$this->cacheService->clearPageCache($singleChunk);
}
}
}
}
2 changes: 2 additions & 0 deletions ext_conf_template.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# cat=basic/enable; type=string; label=Number of cached pages that will be cleared in one chunk
numberOfCachedPagesToClear =