Skip to content

Instantly share code, notes, and snippets.

@kemoc
Created April 17, 2017 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kemoc/5c880bfbe4beb28a2bc5e75197027ced to your computer and use it in GitHub Desktop.
Save kemoc/5c880bfbe4beb28a2bc5e75197027ced to your computer and use it in GitHub Desktop.
File to clear HTTP cache (enabled TTL cache) on publish content object in legacy, missing it in legacy core.
<?php
class radekz_ezpHandler extends eZContentObjectEditHandler
{
static function storeActionList()
{
return array();
}
function fetchInput( $http, &$module, &$class, $object, &$version, $contentObjectAttributes, $editVersion, $editLanguage, $fromLanguage ) {
}
function publish( $contentObjectID, $contentObjectVersion ) {
$contentObject = eZContentObject::fetch($contentObjectID);
$classIdentifier = $contentObject->attribute("class_identifier");
$lastRequestTimeSessionKey = __METHOD__ . "-last_request_time-$contentObjectID";
$lastRequestTime = null;
if(eZSession::issetkey($lastRequestTimeSessionKey))
{
$lastRequestTime = (int)eZSession::get($lastRequestTimeSessionKey);
}
if($_SERVER["REQUEST_TIME"] != $lastRequestTime)
{
eZSession::set($lastRequestTimeSessionKey, $_SERVER["REQUEST_TIME"]);
if(isset($contentObject)) {
unset ($contentObject);
}
eZContentObject::clearCache($contentObjectID);
$contentObject = eZContentObject::fetch($contentObjectID);
$this->purgeHTTPCache($contentObject);
eZSession::unsetkey($lastRequestTimeSessionKey);
}
}
/**
* Clear current content all caches, caches of parents, parents multilocations
* and parents of that multilocations to the root nodes configured in content.ini
*
* @todo check if purge_type is local:
* ezpublish:
### https://doc.ez.no/display/DEVELOPER/HTTP+Cache
http_cache:
purge_type: local
*
* @see vendor/ezsystems/ezplatform-http-cache/src/Resources/config/services.yml
*
* @param eZContentObject $item
*/
public function purgeHTTPCache(eZContentObject $item)
{
$httpCacheLocalService = $this->getHTTPCacheLocalService();
$rootsNodesIds = $this->getRootsNodeIds();
$nodes = $item->assignedNodes();
$locationIds = [];
$parentsIdsFiltered = [];
foreach($nodes as $keyX => $itemX)
{
$locationIds[] = (int)$itemX->attribute("node_id");
$parentsIds = $itemX->pathArray();
end($parentsIds);
$lastKey = key($parentsIds);
unset($parentsIds[0], $parentsIds[$lastKey]);
reset($parentsIds);
$parentsIds2 = array_values($parentsIds);
/*
* item path /1/2/55/56/77/65/204/
* roots ids: 55, 79, 101
*
*/
foreach($rootsNodesIds as $keyX2 => $locationIdX2)
{
if(in_array($locationIdX2, $parentsIds2))
{
$doCollect = false;
foreach($parentsIds2 as $keyX3 => $locationIdX3)
{
if(in_array($locationIdX3, $rootsNodesIds))
{
$doCollect = true;
}
if($doCollect)
{
$locationIdX3 = (int)$locationIdX3;
$locationIds[] = $locationIdX3;
$parentsIdsFiltered[] = $locationIdX3;
}
}
if($parentsIdsFiltered)
{
foreach($parentsIdsFiltered as $parentLocationId)
{
$contentItem = eZContentObject::fetchByNodeID($parentLocationId);
$parentLocationsX2 = $contentItem->assignedNodes();
foreach($parentLocationsX2 as $parentLocationX2)
{
$parentLocationsX2Id = (int)$parentLocationX2->attribute("node_id");
if(!in_array($parentLocationsX2Id, $locationIds))
{
$locationIds[] = $parentLocationsX2Id;
$parentLocationPathArray = $parentLocationX2->pathArray();
$parentLocationPathArrayFiltered = $parentLocationPathArray;
end($parentLocationPathArrayFiltered);
$parentPathLastKey = key($parentLocationPathArrayFiltered);
reset($parentLocationPathArrayFiltered);
unset($parentLocationPathArrayFiltered[0],
$parentLocationPathArrayFiltered[$parentPathLastKey]
);
$parentLocationPathArrayFiltered = array_values($parentLocationPathArrayFiltered);
foreach($rootsNodesIds as $rootNodeIdX2)
{
if(in_array($rootNodeIdX2, $parentLocationPathArrayFiltered))
{
$doCollect2 = false;
foreach($parentLocationPathArrayFiltered as $anotherLocationOfParentId)
{
$anotherLocationOfParentId = (int)$anotherLocationOfParentId;
if(in_array($anotherLocationOfParentId, $rootsNodesIds))
{
$doCollect2 = true;
}
if($doCollect2)
{
$locationIds[] = $anotherLocationOfParentId;
}
}
break;
}
}
}
}
unset($contentItem, $parentLocationsX2);
}
}
break;
}
}
}
$locationIds = array_unique($locationIds);
$httpCacheLocalService->purge($locationIds);
}
public function getRootsNodeIds()
{
$contentINI = eZINI::instance("content.ini");
$contentRootNodeId = (int)$contentINI->variable("NodeSettings", "RootNode");
$mediaRootNodeId = (int)$contentINI->variable("NodeSettings", "MediaRootNode");
$usersRootNodeId = (int)$contentINI->variable("NodeSettings", "UserRootNode");
$result = [];
$result['content'] = $contentRootNodeId;
$result['media'] = $mediaRootNodeId;
$result['users'] = $usersRootNodeId;
return $result;
}
public function getHTTPCacheLocalService()
{
/** @var EzSystems\PlatformHttpCacheBundle\PurgeClient\LocalPurgeClient $service */
$service = $this->getContainer()->get("ezplatform.http_cache.purge_client");
return $service;
}
public function getContainer()
{
$container = ezpKernel::instance()->getServiceContainer();
return $container;
}
public function getConfigResolver()
{
$container = $this->getContainer();
/** @var \eZ\Publish\Core\MVC\ConfigResolverInterface $configResolver */
$configResolver = $container->get("ezpublish.config.resolver");
return $configResolver;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment