As you are aware, the admin log, is an audit trail that grows over time. For my new version of the book I'm writing on CMSMS, I wanted to know more about how this pseudocron can be used.
I've prepared a pseudocron task that removes log rows that are older than 60 days.
If you push the code below in directory lib/tasks with the name class.RemoveAdminLogInfo.task.php
you will notice that every day log records are removed (if any). The number removed is mentioned in the same admin log (as an entry of today). Notice that it runs only once a day and that the number of days are hardcoded (set it to your own most favorable value.
If you want to have the logging in a different language again, suite yourself.
Code: Select all
<?php
class RemoveAdminLogInfoTask implements CmsRegularTask
{
const LASTEXECUTE_SITEPREF = 'RemoveAdminLogInfo_lastexecute';
public function get_name()
{
return 'Remove Admin Log Info';
}
public function get_description()
{
return 'Remove Admin Log Info that is older than 60 days';
}
public function test($time = '')
{
// Only perform this task daily.
if( !$time ) $time = time();
$last_execute = get_site_preference(self::LASTEXECUTE_SITEPREF,0);
if( ($time - 24*60*60) >= $last_execute ) {
return TRUE;
}
return FALSE;
}
public function execute($time = '')
{
if( !$time ) $time = time();
// Perform the task.
// -----
global $gCms;
$db = cmsms()->GetDb();
$retiredays = 60;
// Prepare current date minus retiredaysdays
$today = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
// 60 * 60 * 24 = 86400 (is the number of seconds in a day)
$timestamp = $today - (86400 * $retiredays);
// Prepare the number of orders being removed and log it
$query = 'SELECT COUNT(*) FROM '.cms_db_prefix().'adminlog
WHERE timestamp < ?';
$dbresult = $db->Execute($query, array($timestamp) );
$logcount = 0;
if ($dbresult && $row = $dbresult->FetchRow()) {
$logcount = $row['COUNT(*)'];
}
if ($logcount > 0) {
audit(0, 'Pseudocron task: RemoveAdminLogInfo', $logcount. ' log lines
removed from admin log');
$query = 'DELETE FROM '.cms_db_prefix().'adminlog
WHERE timestamp < ?';
$dbresult = $db->Execute($query, array($timestamp) );
}
// -----
// Process remaining part of task
return TRUE;
}
public function on_success($time = '')
{
if( !$time ) $time = time();
set_site_preference(self::LASTEXECUTE_SITEPREF,$time);
}
public function on_failure($time = '')
{
if( !$time ) $time = time();
// nothing here.
}
}
?>
Code: Select all
# This UDT shows the time that the pseudocron task have run.
# Use {ShowLastPseudoCronRun} on a temporary webpage or run it from the backend
# Duketown Dec. 2010
$db = cmsms()->db;
$query = 'SELECT * FROM '.cms_db_prefix().'siteprefs WHERE sitepref_name LIKE "%_lastexecute"';
$dbresult = $db->Execute($query );
// Now show the pseudocron task and the last time it ran
while ($dbresult && $row = $dbresult->FetchRow())
{
echo $row['sitepref_name'].' @ '.date('Y-m-d H:i:s',$row['sitepref_value']).'<br />';
}
For another example see Prepare an pseudocron mail (thanks for sharing that Arnoud).