Skip to content

Instantly share code, notes, and snippets.

@thomaslarsson
Last active August 29, 2015 14:23
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 thomaslarsson/e8ef19e24be534cd6998 to your computer and use it in GitHub Desktop.
Save thomaslarsson/e8ef19e24be534cd6998 to your computer and use it in GitHub Desktop.
place in extension/x/bin/php/purgeOldCache.php. Change dir to ezpublish_legacy and run using $ php extension/x/bin/php/purgeOldCache.php.
<?php
require_once('autoload.php');
// Configuration
$removeOlderThanDays = 90;
$siteaccess = "admin_siteaccess";
// CLI initialization.
$cli = eZCLI::instance();
$script = eZScript::instance(array(
'description' => "eZ Publish - Purge old trash",
'use-session' => true,
'use-modules' => true,
'site-access' => $siteaccess,
'use-extensions' => true
));
// Initialize script
$script->startup();
$script->initialize();
echo "Running script, \n";
$db = eZDB::instance();
// Update database and allow failures before performing transactions
$db->query("ALTER TABLE ezcontentobject_trash ADD deletedAt INT(11) null");
// Log in to API
$user = eZUser::fetchByName('admin');
$userID = $user->attribute('contentobject_id');
eZUser::setCurrentlyLoggedInUser($user, $userID);
// Calculate timestamps
$date = new DateTime('now');
$date->modify("-{$removeOlderThanDays} days");
$deleteFromTimestamp = $date->getTimestamp();
$deleteFromDate = $date->format('d.m.Y');
// Run queries
$db->begin();
$updateResult = $db->arrayQuery("SELECT * FROM ezcontentobject_trash WHERE deletedAt IS null");
$deleteResult = $db->arrayQuery("SELECT * FROM ezcontentobject_trash WHERE deletedAt < {$deleteFromTimestamp}");
$db->arrayQuery("UPDATE ezcontentobject_trash SET deletedAt = UNIX_TIMESTAMP() WHERE deletedAt IS null");
// Log update results
$updateCount = (int) count($updateResult);
$removeCount = (int) count($deleteResult);
$updateMessage = sprintf('- Timestamped a total of %s trashed objects', $updateCount);
eZLog::write($updateMessage, 'trash.log');
echo "{$updateMessage} \n";
// Perform delete
foreach ( $deleteResult as $trashData )
{
$trash = eZContentObject::fetch($trashData['contentobject_id'], true);
$trash->purge();
}
$db->commit();
// Log purging
$deleteMessage = sprintf('- Purged a total of %s trashed objects older than %s days (%s)', $removeCount, $removeOlderThanDays, $deleteFromDate);
eZLog::write($deleteMessage, 'trash.log');
echo "{$deleteMessage} \n";
// Clear cache
eZCache::clearByTag('content');
// Finish execution
echo "Script finished. \n";
eZExecution::cleanExit();
flush();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment