first commit

This commit is contained in:
2025-06-17 11:53:18 +02:00
commit 9f0f7ba12b
8804 changed files with 1369176 additions and 0 deletions

View File

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<form>
<fields name="params">
<fieldset name="task_params">
<field
name="logstokeep"
type="integer"
label="PLG_TASK_ROTATELOGS_LOGSTOKEEP_LABEL"
first="1"
last="10"
step="1"
default="1"
filter="int"
validate="number"
/>
</fieldset>
</fields>
</form>

View File

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<extension type="plugin" group="task" method="upgrade">
<name>plg_task_rotatelogs</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>admin@joomla.org</authorEmail>
<authorUrl>www.joomla.org</authorUrl>
<version>5.0.0</version>
<description>PLG_TASK_ROTATELOGS_XML_DESCRIPTION</description>
<namespace path="src">Joomla\Plugin\Task\RotateLogs</namespace>
<files>
<folder>forms</folder>
<folder plugin="rotatelogs">services</folder>
<folder>src</folder>
</files>
<languages>
<language tag="en-GB">language/en-GB/plg_task_rotatelogs.ini</language>
<language tag="en-GB">language/en-GB/plg_task_rotatelogs.sys.ini</language>
</languages>
</extension>

View File

@ -0,0 +1,48 @@
<?php
/**
* @package Joomla.Plugin
* @subpackage Task.rotatelogs
*
* @copyright (C) 2023 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
\defined('_JEXEC') or die;
use Joomla\CMS\Extension\PluginInterface;
use Joomla\CMS\Factory;
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\Database\DatabaseInterface;
use Joomla\DI\Container;
use Joomla\DI\ServiceProviderInterface;
use Joomla\Event\DispatcherInterface;
use Joomla\Plugin\Task\RotateLogs\Extension\RotateLogs;
return new class () implements ServiceProviderInterface {
/**
* Registers the service provider with a DI container.
*
* @param Container $container The DI container.
*
* @return void
*
* @since 5.0.0
*/
public function register(Container $container)
{
$container->set(
PluginInterface::class,
function (Container $container) {
$plugin = new RotateLogs(
$container->get(DispatcherInterface::class),
(array) PluginHelper::getPlugin('task', 'rotatelogs')
);
$plugin->setApplication(Factory::getApplication());
$plugin->setDatabase($container->get(DatabaseInterface::class));
return $plugin;
}
);
}
};

View File

@ -0,0 +1,183 @@
<?php
/**
* @package Joomla.Plugin
* @subpackage Task.rotatelogs
*
* @copyright (C) 2023 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\Plugin\Task\RotateLogs\Extension;
use Joomla\CMS\Filesystem\File;
use Joomla\CMS\Filesystem\Folder;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\Component\Scheduler\Administrator\Event\ExecuteTaskEvent;
use Joomla\Component\Scheduler\Administrator\Task\Status;
use Joomla\Component\Scheduler\Administrator\Task\Task;
use Joomla\Component\Scheduler\Administrator\Traits\TaskPluginTrait;
use Joomla\Database\DatabaseAwareTrait;
use Joomla\Event\SubscriberInterface;
use Joomla\Filesystem\Path;
// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
// phpcs:enable PSR1.Files.SideEffects
/**
* A task plugin. Offers 1 task routines Rotate Logs
* {@see ExecuteTaskEvent}.
*
* @since 5.0.0
*/
final class RotateLogs extends CMSPlugin implements SubscriberInterface
{
use DatabaseAwareTrait;
use TaskPluginTrait;
/**
* @var string[]
* @since 5.0.0
*/
private const TASKS_MAP = [
'rotation.logs' => [
'langConstPrefix' => 'PLG_TASK_ROTATELOGS_ROTATION',
'method' => 'rotateLogs',
'form' => 'rotateForm',
],
];
/**
* @var boolean
* @since 5.0.0
*/
protected $autoloadLanguage = true;
/**
* @inheritDoc
*
* @return string[]
*
* @since 5.0.0
*/
public static function getSubscribedEvents(): array
{
return [
'onTaskOptionsList' => 'advertiseRoutines',
'onExecuteTask' => 'standardRoutineHandler',
'onContentPrepareForm' => 'enhanceTaskItemForm',
];
}
/**
* Method for the logs rotation task.
*
* @param ExecuteTaskEvent $event The `onExecuteTask` event.
*
* @return integer The routine exit code.
*
* @since 5.0.0
* @throws \Exception
*/
private function rotateLogs(ExecuteTaskEvent $event): int
{
$logsToKeep = (int) $event->getArgument('params')->logstokeep ?? 1;
// Get the log path
$logPath = Path::clean($this->getApplication()->get('log_path'));
// Invalid path, stop processing further
if (!is_dir($logPath)) {
return Status::KNOCKOUT;
}
$logFiles = $this->getLogFiles($logPath);
// Sort log files by version number in reverse order
krsort($logFiles, SORT_NUMERIC);
foreach ($logFiles as $version => $files) {
if ($version >= $logsToKeep) {
// Delete files which have version greater than or equals $logsToKeep
foreach ($files as $file) {
File::delete($logPath . '/' . $file);
}
} else {
// For files which have version smaller than $logsToKeep, rotate (increase version number)
foreach ($files as $file) {
$this->rotate($logPath, $file, $version);
}
}
}
return Status::OK;
}
/**
* Method to rotate (increase version) of a log file
*
* @param string $path Path to file to rotate
* @param string $filename Name of file to rotate
* @param int $currentVersion The current version number
*
* @return void
*
* @since 5.0.0
*/
private function rotate($path, $filename, $currentVersion)
{
if ($currentVersion === 0) {
$rotatedFile = $path . '/1.' . $filename;
} else {
/*
* Rotated log file has this filename format [VERSION].[FILENAME].php. To rotate it, we just need to explode
* the filename into an array, increase value of first element (keep version) and implode it back to get the
* rotated file name
*/
$parts = explode('.', $filename);
$parts[0] = $currentVersion + 1;
$rotatedFile = $path . '/' . implode('.', $parts);
}
File::move($path . '/' . $filename, $rotatedFile);
}
/**
* Get log files from log folder
*
* @param string $path The folder to get log files
*
* @return array The log files in the given path grouped by version number (not rotated files have number 0)
*
* @since 5.0.0
*/
private function getLogFiles($path)
{
$logFiles = [];
$files = Folder::files($path, '\.php$');
foreach ($files as $file) {
$parts = explode('.', $file);
/*
* Rotated log file has this filename format [VERSION].[FILENAME].php. So if $parts has at least 3 elements
* and the first element is a number, we know that it's a rotated file and can get it's current version
*/
if (\count($parts) >= 3 && is_numeric($parts[0])) {
$version = (int) $parts[0];
} else {
$version = 0;
}
if (!isset($logFiles[$version])) {
$logFiles[$version] = [];
}
$logFiles[$version][] = $file;
}
return $logFiles;
}
}