Skip to content

Commit

Permalink
[5] add2scheduler-deleteactionlogs (joomla#41064)
Browse files Browse the repository at this point in the history
* add2schedulerdeleteactionlogs

* cs

* Rename 5.0.0-2023-06-27.sql to 5.0.0-2023-08-05.sql

shift

* Rename 5.0.0-2023-06-27.sql to 5.0.0-2023-08-05.sql

shift

* Rename 5.0.0-2023-08-05.sql to 5.0.0-2023-08-25.sql

* Rename 5.0.0-2023-08-05.sql to 5.0.0-2023-08-25.sql

* Update plugins/task/deleteactionlogs/src/Extension/DeleteActionLogs.php

Co-authored-by: heelc29 <[email protected]>

* blank-line

* blank-line

* blank-line

* remove config node

* Rename 5.0.0-2023-08-25.sql to 5.0.0-2023-08-30.sql

* Rename 5.0.0-2023-08-25.sql to 5.0.0-2023-08-30.sql

* Deprecate language strings

* Single quotes

* Remove extra reference assignment for dispatcher argument

* Do it in the same way as in PR joomla#40553

* Rename 5.0.0-2023-08-30.sql to 5.0.0-2023-09-02.sql

* Rename 5.0.0-2023-08-30.sql to 5.0.0-2023-09-02.sql

* Fix lastrun default on update

* CS

* deploy version

* Do nothing if params is an empty JSON

* Fix migration methods and comment typos

* Use the right task type

* Fix undefined array element "exec-day"

---------

Co-authored-by: Richard Fath <[email protected]>
Co-authored-by: heelc29 <[email protected]>
Co-authored-by: Richard Fath <[email protected]>
  • Loading branch information
4 people authored Sep 3, 2023
1 parent 7d5237c commit 9ea35c7
Show file tree
Hide file tree
Showing 15 changed files with 308 additions and 164 deletions.
79 changes: 76 additions & 3 deletions administrator/components/com_admin/script.php
Original file line number Diff line number Diff line change
Expand Up @@ -2346,6 +2346,10 @@ public function postflight($action, $installer)
return false;
}

if (!$this->migrateDeleteActionlogsConfiguration()) {
return false;
}

if (!$this->migratePrivacyconsentConfiguration()) {
return false;
}
Expand All @@ -2355,6 +2359,75 @@ public function postflight($action, $installer)
return true;
}

/**
* Migrate Deleteactionlogs plugin configuration
*
* @return boolean True on success
*
* @since __DEPLOY_VERSION__
*/
private function migrateDeleteActionlogsConfiguration(): bool
{
$db = Factory::getDbo();

try {
// Get the ActionLogs system plugin's parameters
$row = $db->setQuery(
$db->getQuery(true)
->select([$db->quotename('enabled'), $db->quoteName('params')])
->from($db->quoteName('#__extensions'))
->where($db->quoteName('type') . ' = ' . $db->quote('plugin'))
->where($db->quoteName('folder') . ' = ' . $db->quote('system'))
->where($db->quoteName('element') . ' = ' . $db->quote('actionlogs'))
)->loadObject();
} catch (Exception $e) {
echo Text::sprintf('JLIB_DATABASE_ERROR_FUNCTION_FAILED', $e->getCode(), $e->getMessage()) . '<br>';

return false;
}

// If not existing or disabled there is nothing to migrate
if (!$row || !$row->enabled) {
return true;
}

$params = new Registry($row->params);

// If deletion of outdated logs was disabled there is nothing to migrate
if (!$params->get('logDeletePeriod', 0)) {
return true;
}

/** @var SchedulerComponent $component */
$component = Factory::getApplication()->bootComponent('com_scheduler');

/** @var TaskModel $model */
$model = $component->getMVCFactory()->createModel('Task', 'Administrator', ['ignore_request' => true]);
$task = [
'title' => 'DeleteActionLogs',
'type' => 'delete.actionlogs',
'execution_rules' => [
'rule-type' => 'interval-hours',
'interval-hours' => 24,
'exec-time' => gmdate('H:i', $params->get('lastrun', time())),
'exec-day' => gmdate('d'),
],
'state' => 1,
'params' => [
'logDeletePeriod' => $params->get('logDeletePeriod', 0),
],
];

try {
$model->save($task);
} catch (Exception $e) {
echo Text::sprintf('JLIB_DATABASE_ERROR_FUNCTION_FAILED', $e->getCode(), $e->getMessage()) . '<br>';

return false;
}

return true;
}
/**
* Migrate privacyconsents system plugin configuration
*
Expand All @@ -2370,7 +2443,7 @@ private function migratePrivacyconsentConfiguration(): bool
// Get the PrivacyConsent system plugin's parameters
$row = $db->setQuery(
$db->getQuery(true)
->select($db->quotename('enabled'), $db->quoteName('params'))
->select([$db->quotename('enabled'), $db->quoteName('params')])
->from($db->quoteName('#__extensions'))
->where($db->quoteName('type') . ' = ' . $db->quote('plugin'))
->where($db->quoteName('folder') . ' = ' . $db->quote('system'))
Expand All @@ -2382,14 +2455,14 @@ private function migratePrivacyconsentConfiguration(): bool
return false;
}

// If not existing or disbled there is nothing to migrate
// If not existing or disabled there is nothing to migrate
if (!$row || !$row->enabled) {
return true;
}

$params = new Registry($row->params);

// If consent expiration was disbled there is nothing to migrate
// If consent expiration was disabled there is nothing to migrate
if (!$params->get('enabled', 0)) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
INSERT INTO `#__extensions` (`name`, `type`, `element`, `folder`, `client_id`, `enabled`, `access`, `protected`, `locked`, `manifest_cache`, `params`, `custom_data`, `checked_out`, `checked_out_time`, `ordering`, `state`) VALUES
('plg_task_deleteactionlogs', 'plugin', 'deleteactionlogs', 'task', 0, 1, 1, 0, 1, '', '{}', '', NULL, NULL, 0, 0),
('plg_task_privacyconsent', 'plugin', 'privacyconsent', 'task', 0, 1, 1, 0, 1, '', '{}', '', NULL, NULL, 0, 0),
('plg_task_rotatelogs', 'plugin', 'rotatelogs', 'task', 0, 1, 1, 0, 1, '', '{}', '', NULL, NULL, 0, 0),
('plg_task_updatenotification', 'plugin', 'updatenotification', 'task', 0, 1, 1, 0, 1, '', '{}', '', NULL, NULL, 0, 0);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
INSERT INTO "#__extensions" ("name", "type", "element", "folder", "client_id", "enabled", "access", "protected", "locked", "manifest_cache", "params", "custom_data", "checked_out", "checked_out_time", "ordering", "state") VALUES
('plg_task_deleteactionlogs', 'plugin', 'deleteactionlogs', 'task', 0, 1, 1, 0, 1, '', '{}', '', NULL, NULL, 0, 0),
('plg_task_privacyconsent', 'plugin', 'privacyconsent', 'task', 0, 1, 1, 0, 1, '', '{}', '', NULL, NULL, 0, 0),
('plg_task_rotatelogs', 'plugin', 'rotatelogs', 'task', 0, 1, 1, 0, 1, '', '{}', '', NULL, NULL, 0, 0),
('plg_task_updatenotification', 'plugin', 'updatenotification', 'task', 0, 1, 1, 0, 1, '', '{}', '', NULL, NULL, 0, 0);
Expand Down
6 changes: 4 additions & 2 deletions administrator/language/en-GB/plg_system_actionlogs.ini
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ PLG_SYSTEM_ACTIONLOGS_INFO_DESC="The Action Log - Joomla plugin is disabled"
PLG_SYSTEM_ACTIONLOGS_INFO_LABEL="Information"
PLG_SYSTEM_ACTIONLOGS_JOOMLA_ACTIONLOG_DISABLED="Action Log - Joomla"
PLG_SYSTEM_ACTIONLOGS_JOOMLA_ACTIONLOG_DISABLED_REDIRECT="The %s plugin is disabled."
PLG_SYSTEM_ACTIONLOGS_LOG_DELETE_PERIOD="Days to delete logs after"
PLG_SYSTEM_ACTIONLOGS_LOG_DELETE_PERIOD_DESC="Enter 0 if you don't want to delete the logs."
PLG_SYSTEM_ACTIONLOGS_NOTIFICATIONS="Email Notifications"
PLG_SYSTEM_ACTIONLOGS_OPTIONS="User Actions Log Options"
PLG_SYSTEM_ACTIONLOGS_XML_DESCRIPTION="Records the actions of users on the site so they can be reviewed if required."
Expand All @@ -22,3 +20,7 @@ PLG_SYSTEM_ACTIONLOGS_CONTENT_PUBLISHED="User <a href=\"{accountlink}\">{usernam
PLG_SYSTEM_ACTIONLOGS_CONTENT_TRASHED="User <a href=\"{accountlink}\">{username}</a> trashed the {type} <a href=\"{itemlink}\">{title}</a>"
PLG_SYSTEM_ACTIONLOGS_CONTENT_UNPUBLISHED="User <a href=\"{accountlink}\">{username}</a> unpublished the {type} <a href=\"{itemlink}\">{title}</a>"
PLG_SYSTEM_ACTIONLOGS_CONTENT_UPDATED="User <a href=\"{accountlink}\">{username}</a> updated the {type} <a href=\"{itemlink}\">{title}</a>"

; All the following strings are deprecated and will be removed with 6.0
PLG_SYSTEM_ACTIONLOGS_LOG_DELETE_PERIOD="Days to delete logs after"
PLG_SYSTEM_ACTIONLOGS_LOG_DELETE_PERIOD_DESC="Enter 0 if you don't want to delete the logs."
10 changes: 10 additions & 0 deletions administrator/language/en-GB/plg_task_deleteactionlogs.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
; Joomla! Project
; (C) 2023 Open Source Matters, Inc. <https://www.joomla.org>
; License GNU General Public License version 2 or later; see LICENSE.txt
; Note : All ini files need to be saved as UTF-8

PLG_TASK_DELETEACTIONLOGS="Task - Delete Action Logs"
PLG_TASK_DELETEACTIONLOGS_DELETE_DESC="Delete Action logs after days"
PLG_TASK_DELETEACTIONLOGS_DELETE_TITLE="Delete ActionLogs - Task"
PLG_TASK_DELETEACTIONLOGS_LOG_DELETE_PERIOD="Days to delete action logs after"
PLG_TASK_DELETEACTIONLOGS_XML_DESCRIPTION="This plugin for schedule Action Logs delete Tasks."
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
; Joomla! Project
; (C) 2023 Open Source Matters, Inc. <https://www.joomla.org>
; License GNU General Public License version 2 or later; see LICENSE.txt
; Note : All ini files need to be saved as UTF-8

PLG_TASK_DELETEACTIONLOGS="Task - Delete Action Logs"
PLG_TASK_DELETEACTIONLOGS_XML_DESCRIPTION="This plugin for schedule Action Logs delete Tasks."
13 changes: 7 additions & 6 deletions installation/sql/mysql/base.sql
Original file line number Diff line number Diff line change
Expand Up @@ -364,12 +364,13 @@ INSERT INTO `#__extensions` (`package_id`, `name`, `type`, `element`, `folder`,
(0, 'plg_system_tasknotification', 'plugin', 'tasknotification', 'system', 0, 1, 1, 0, 1, '', '', '', 24, 0),
(0, 'plg_system_webauthn', 'plugin', 'webauthn', 'system', 0, 1, 1, 0, 1, '', '{}', '', 26, 0),
(0, 'plg_task_checkfiles', 'plugin', 'checkfiles', 'task', 0, 1, 1, 0, 1, '', '{}', '', 1, 0),
(0, 'plg_task_globalcheckin', 'plugin', 'globalcheckin', 'task', 0, 1, 1, 0, 0, '', '{}', '', 2, 0),
(0, 'plg_task_requests', 'plugin', 'requests', 'task', 0, 1, 1, 0, 1, '', '{}', '', 3, 0),
(0, 'plg_task_privacyconsent', 'plugin', 'privacyconsent', 'task', 0, 1, 1, 0, 1, '', '{}', '', 4, 0),
(0, 'plg_task_rotatelogs', 'plugin', 'rotatelogs', 'task', 0, 1, 1, 0, 1, '', '{}', '', 5, 0),
(0, 'plg_task_sitestatus', 'plugin', 'sitestatus', 'task', 0, 1, 1, 0, 1, '', '{}', '', 6, 0),
(0, 'plg_task_updatenotification', 'plugin', 'updatenotification', 'task', 0, 1, 1, 0, 1, '', '{}', '', 7, 0),
(0, 'plg_task_deleteactionlogs', 'plugin', 'deleteactionlogs', 'task', 0, 1, 1, 0, 1, '', '{}', '', 2, 0),
(0, 'plg_task_globalcheckin', 'plugin', 'globalcheckin', 'task', 0, 1, 1, 0, 0, '', '{}', '', 3, 0),
(0, 'plg_task_requests', 'plugin', 'requests', 'task', 0, 1, 1, 0, 1, '', '{}', '', 4, 0),
(0, 'plg_task_privacyconsent', 'plugin', 'privacyconsent', 'task', 0, 1, 1, 0, 1, '', '{}', '', 5, 0),
(0, 'plg_task_rotatelogs', 'plugin', 'rotatelogs', 'task', 0, 1, 1, 0, 1, '', '{}', '', 6, 0),
(0, 'plg_task_sitestatus', 'plugin', 'sitestatus', 'task', 0, 1, 1, 0, 1, '', '{}', '', 7, 0),
(0, 'plg_task_updatenotification', 'plugin', 'updatenotification', 'task', 0, 1, 1, 0, 1, '', '{}', '', 8, 0),
(0, 'plg_multifactorauth_totp', 'plugin', 'totp', 'multifactorauth', 0, 1, 1, 0, 1, '', '', '', 1, 0),
(0, 'plg_multifactorauth_yubikey', 'plugin', 'yubikey', 'multifactorauth', 0, 1, 1, 0, 1, '', '', '', 2, 0),
(0, 'plg_multifactorauth_webauthn', 'plugin', 'webauthn', 'multifactorauth', 0, 1, 1, 0, 1, '', '', '', 3, 0),
Expand Down
13 changes: 7 additions & 6 deletions installation/sql/postgresql/base.sql
Original file line number Diff line number Diff line change
Expand Up @@ -370,12 +370,13 @@ INSERT INTO "#__extensions" ("package_id", "name", "type", "element", "folder",
(0, 'plg_system_tasknotification', 'plugin', 'tasknotification', 'system', 0, 1, 1, 0, 1, '', '', '', 24, 0),
(0, 'plg_system_webauthn', 'plugin', 'webauthn', 'system', 0, 1, 1, 0, 1, '', '{}', '', 26, 0),
(0, 'plg_task_checkfiles', 'plugin', 'checkfiles', 'task', 0, 1, 1, 0, 1, '', '{}', '', 1, 0),
(0, 'plg_task_globalcheckin', 'plugin', 'globalcheckin', 'task', 0, 1, 1, 0, 0, '', '{}', '', 2, 0),
(0, 'plg_task_requests', 'plugin', 'requests', 'task', 0, 1, 1, 0, 1, '', '{}', '', 3, 0),
(0, 'plg_task_privacyconsent', 'plugin', 'privacyconsent', 'task', 0, 1, 1, 0, 1, '', '{}', '', 4, 0),
(0, 'plg_task_rotatelogs', 'plugin', 'rotatelogs', 'task', 0, 1, 1, 0, 1, '', '{}', '', 5, 0),
(0, 'plg_task_sitestatus', 'plugin', 'sitestatus', 'task', 0, 1, 1, 0, 1, '', '{}', '', 6, 0),
(0, 'plg_task_updatenotification', 'plugin', 'updatenotification', 'task', 0, 1, 1, 0, 1, '', '{}', '', 7, 0),
(0, 'plg_task_deleteactionlogs', 'plugin', 'deleteactionlogs', 'task', 0, 1, 1, 0, 1, '', '{}', '', 2, 0),
(0, 'plg_task_globalcheckin', 'plugin', 'globalcheckin', 'task', 0, 1, 1, 0, 0, '', '{}', '', 3, 0),
(0, 'plg_task_requests', 'plugin', 'requests', 'task', 0, 1, 1, 0, 1, '', '{}', '', 4, 0),
(0, 'plg_task_privacyconsent', 'plugin', 'privacyconsent', 'task', 0, 1, 1, 0, 1, '', '{}', '', 5, 0),
(0, 'plg_task_rotatelogs', 'plugin', 'rotatelogs', 'task', 0, 1, 1, 0, 1, '', '{}', '', 6, 0),
(0, 'plg_task_sitestatus', 'plugin', 'sitestatus', 'task', 0, 1, 1, 0, 1, '', '{}', '', 7, 0),
(0, 'plg_task_updatenotification', 'plugin', 'updatenotification', 'task', 0, 1, 1, 0, 1, '', '{}', '', 8, 0),
(0, 'plg_multifactorauth_totp', 'plugin', 'totp', 'multifactorauth', 0, 1, 1, 0, 1, '', '', '', 1, 0),
(0, 'plg_multifactorauth_yubikey', 'plugin', 'yubikey', 'multifactorauth', 0, 1, 1, 0, 1, '', '', '', 2, 0),
(0, 'plg_multifactorauth_webauthn', 'plugin', 'webauthn', 'multifactorauth', 0, 1, 1, 0, 1, '', '', '', 3, 0),
Expand Down
1 change: 1 addition & 0 deletions libraries/src/Extension/ExtensionHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ class ExtensionHelper

// Core plugin extensions - task scheduler
['plugin', 'checkfiles', 'task', 0],
['plugin', 'deleteactionlogs', 'task', 0],
['plugin', 'globalcheckin', 'task', 0],
['plugin', 'privacyconsent', 'task', 0],
['plugin', 'requests', 'task', 0],
Expand Down
22 changes: 0 additions & 22 deletions plugins/system/actionlogs/actionlogs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,4 @@
<language tag="en-GB">language/en-GB/plg_system_actionlogs.ini</language>
<language tag="en-GB">language/en-GB/plg_system_actionlogs.sys.ini</language>
</languages>
<config>
<fields name="params">
<fieldset name="basic">
<field
name="logDeletePeriod"
type="number"
label="PLG_SYSTEM_ACTIONLOGS_LOG_DELETE_PERIOD"
description="PLG_SYSTEM_ACTIONLOGS_LOG_DELETE_PERIOD_DESC"
default="0"
min="0"
filter="int"
validate="number"
/>
<field
name="lastrun"
type="hidden"
default="0"
filter="integer"
/>
</fieldset>
</fields>
</config>
</extension>
125 changes: 0 additions & 125 deletions plugins/system/actionlogs/src/Extension/ActionLogs.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

namespace Joomla\Plugin\System\ActionLogs\Extension;

use Joomla\CMS\Cache\Cache;
use Joomla\CMS\Factory;
use Joomla\CMS\Form\Form;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Language\Text;
Expand Down Expand Up @@ -192,129 +190,6 @@ public function onContentPrepareData($context, $data)
return true;
}

/**
* Runs after the HTTP response has been sent to the client and delete log records older than certain days
*
* @return void
*
* @since 3.9.0
*/
public function onAfterRespond()
{
$daysToDeleteAfter = (int) $this->params->get('logDeletePeriod', 0);

if ($daysToDeleteAfter <= 0) {
return;
}

// The delete frequency will be once per day
$deleteFrequency = 3600 * 24;

// Do we need to run? Compare the last run timestamp stored in the plugin's options with the current
// timestamp. If the difference is greater than the cache timeout we shall not execute again.
$now = time();
$last = (int) $this->params->get('lastrun', 0);

if (abs($now - $last) < $deleteFrequency) {
return;
}

// Update last run status
$this->params->set('lastrun', $now);

$db = $this->getDatabase();
$params = $this->params->toString('JSON');
$query = $db->getQuery(true)
->update($db->quoteName('#__extensions'))
->set($db->quoteName('params') . ' = :params')
->where($db->quoteName('type') . ' = ' . $db->quote('plugin'))
->where($db->quoteName('folder') . ' = ' . $db->quote('system'))
->where($db->quoteName('element') . ' = ' . $db->quote('actionlogs'))
->bind(':params', $params);

try {
// Lock the tables to prevent multiple plugin executions causing a race condition
$db->lockTable('#__extensions');
} catch (\Exception $e) {
// If we can't lock the tables it's too risky to continue execution
return;
}

try {
// Update the plugin parameters
$result = $db->setQuery($query)->execute();

$this->clearCacheGroups(['com_plugins'], [0, 1]);
} catch (\Exception $exc) {
// If we failed to execute
$db->unlockTables();
$result = false;
}

try {
// Unlock the tables after writing
$db->unlockTables();
} catch (\Exception $e) {
// If we can't lock the tables assume we have somehow failed
$result = false;
}

// Stop on failure
if (!$result) {
return;
}

$daysToDeleteAfter = (int) $this->params->get('logDeletePeriod', 0);
$now = Factory::getDate()->toSql();

if ($daysToDeleteAfter > 0) {
$days = -1 * $daysToDeleteAfter;

$query->clear()
->delete($db->quoteName('#__action_logs'))
->where($db->quoteName('log_date') . ' < ' . $query->dateAdd($db->quote($now), $days, 'DAY'));

$db->setQuery($query);

try {
$db->execute();
} catch (\RuntimeException $e) {
// Ignore it
return;
}
}
}

/**
* Clears cache groups. We use it to clear the plugins cache after we update the last run timestamp.
*
* @param array $clearGroups The cache groups to clean
* @param array $cacheClients The cache clients (site, admin) to clean
*
* @return void
*
* @since 3.9.0
*/
private function clearCacheGroups(array $clearGroups, array $cacheClients = [0, 1])
{
foreach ($clearGroups as $group) {
foreach ($cacheClients as $clientId) {
try {
$options = [
'defaultgroup' => $group,
'cachebase' => $clientId ? JPATH_ADMINISTRATOR . '/cache' :
$this->getApplication()->get('cache_path', JPATH_SITE . '/cache'),
];

$cache = Cache::getInstance('callback', $options);
$cache->clean();
} catch (\Exception $e) {
// Ignore it
}
}
}
}

/**
* Utility method to act on a user after it has been saved.
*
Expand Down
22 changes: 22 additions & 0 deletions plugins/task/deleteactionlogs/deleteactionlogs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<extension type="plugin" group="task" method="upgrade">
<name>plg_task_deleteactionlogs</name>
<author>Joomla! Project</author>
<creationDate>2023-07</creationDate>
<copyright>(C) 2023 Open Source Matters, Inc.</copyright>
<license>GNU General Public License version 2 or later; see LICENSE.txt</license>
<authorEmail>[email protected]</authorEmail>
<authorUrl>www.joomla.org</authorUrl>
<version>5.0.0</version>
<description>PLG_TASK_DELETEACTIONLOGS_XML_DESCRIPTION</description>
<namespace path="src">Joomla\Plugin\Task\DeleteActionLogs</namespace>
<files>
<folder>forms</folder>
<folder plugin="deleteactionlogs">services</folder>
<folder>src</folder>
</files>
<languages>
<language tag="en-GB">language/en-GB/plg_task_deleteactionlogs.ini</language>
<language tag="en-GB">language/en-GB/plg_task_deleteactionlogs.sys.ini</language>
</languages>
</extension>
Loading

0 comments on commit 9ea35c7

Please sign in to comment.