primo commit

This commit is contained in:
2024-12-17 17:34:10 +01:00
commit e650f8df99
16435 changed files with 2451012 additions and 0 deletions

View File

@ -0,0 +1,157 @@
<?php
/**
* @package Joomla.Administrator
* @subpackage com_cache
*
* @copyright (C) 2008 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\Component\Cache\Administrator\Controller;
use Joomla\CMS\Event\Cache\AfterPurgeEvent;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Language\Text;
use Joomla\CMS\MVC\Controller\BaseController;
use Joomla\CMS\Response\JsonResponse;
// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
// phpcs:enable PSR1.Files.SideEffects
/**
* Cache Controller
*
* @since 1.6
*/
class DisplayController extends BaseController
{
/**
* The default view for the display method.
*
* @var string
* @since 4.0.0
*/
protected $default_view = 'cache';
/**
* Method to get The Cache Size
*
* @since 4.0.0
*/
public function getQuickiconContent()
{
$model = $this->getModel('Cache');
$data = $model->getData();
$size = 0;
if (!empty($data)) {
foreach ($data as $d) {
$size += $d->size;
}
}
// Number bytes are returned in format xxx.xx MB
$bytes = HTMLHelper::_('number.bytes', $size, 'MB', 1);
if (!empty($bytes)) {
$result['amount'] = $bytes;
$result['sronly'] = Text::sprintf('COM_CACHE_QUICKICON_SRONLY', $bytes);
} else {
$result['amount'] = 0;
$result['sronly'] = Text::sprintf('COM_CACHE_QUICKICON_SRONLY_NOCACHE');
}
echo new JsonResponse($result);
}
/**
* Method to delete a list of cache groups.
*
* @return void
*/
public function delete()
{
// Check for request forgeries
$this->checkToken();
$cid = (array) $this->input->post->get('cid', [], 'string');
if (empty($cid)) {
$this->app->enqueueMessage(Text::_('JERROR_NO_ITEMS_SELECTED'), 'warning');
} else {
$result = $this->getModel('cache')->cleanlist($cid);
if ($result !== []) {
$this->app->enqueueMessage(Text::sprintf('COM_CACHE_EXPIRED_ITEMS_DELETE_ERROR', implode(', ', $result)), 'error');
} else {
$this->app->enqueueMessage(Text::_('COM_CACHE_EXPIRED_ITEMS_HAVE_BEEN_DELETED'), 'message');
}
}
$this->setRedirect('index.php?option=com_cache');
}
/**
* Method to delete all cache groups.
*
* @return void
*
* @since 3.6.0
*/
public function deleteAll()
{
// Check for request forgeries
$this->checkToken();
/** @var \Joomla\Component\Cache\Administrator\Model\CacheModel $model */
$model = $this->getModel('cache');
$allCleared = true;
$mCache = $model->getCache();
foreach ($mCache->getAll() as $cache) {
if ($mCache->clean($cache->group) === false) {
$this->app->enqueueMessage(
Text::sprintf(
'COM_CACHE_EXPIRED_ITEMS_DELETE_ERROR',
Text::_('JADMINISTRATOR') . ' > ' . $cache->group
),
'error'
);
$allCleared = false;
}
}
if ($allCleared) {
$this->app->enqueueMessage(Text::_('COM_CACHE_MSG_ALL_CACHE_GROUPS_CLEARED'), 'message');
} else {
$this->app->enqueueMessage(Text::_('COM_CACHE_MSG_SOME_CACHE_GROUPS_CLEARED'), 'warning');
}
$this->getDispatcher()->dispatch('onAfterPurge', new AfterPurgeEvent('onAfterPurge'));
$this->setRedirect('index.php?option=com_cache&view=cache');
}
/**
* Purge the cache.
*
* @return void
*/
public function purge()
{
// Check for request forgeries
$this->checkToken();
if (!$this->getModel('cache')->purge()) {
$this->app->enqueueMessage(Text::_('COM_CACHE_EXPIRED_ITEMS_PURGING_ERROR'), 'error');
} else {
$this->app->enqueueMessage(Text::_('COM_CACHE_EXPIRED_ITEMS_HAVE_BEEN_PURGED'), 'message');
}
$this->setRedirect('index.php?option=com_cache&view=cache');
}
}

View File

@ -0,0 +1,275 @@
<?php
/**
* @package Joomla.Administrator
* @subpackage com_cache
*
* @copyright (C) 2006 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\Component\Cache\Administrator\Model;
use Joomla\CMS\Cache\Cache;
use Joomla\CMS\Cache\CacheController;
use Joomla\CMS\Cache\Exception\CacheConnectingException;
use Joomla\CMS\Cache\Exception\UnsupportedCacheException;
use Joomla\CMS\Event\Cache\AfterPurgeEvent;
use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomla\CMS\MVC\Model\ListModel;
use Joomla\CMS\Pagination\Pagination;
use Joomla\Utilities\ArrayHelper;
// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
// phpcs:enable PSR1.Files.SideEffects
/**
* Cache Model
*
* @since 1.6
*/
class CacheModel extends ListModel
{
/**
* An Array of CacheItems indexed by cache group ID
*
* @var array
*/
protected $_data = [];
/**
* Group total
*
* @var integer
*/
protected $_total = null;
/**
* Pagination object
*
* @var object
*/
protected $_pagination = null;
/**
* Constructor.
*
* @param array $config An optional associative array of configuration settings.
*
* @since 3.5
*/
public function __construct($config = [])
{
if (empty($config['filter_fields'])) {
$config['filter_fields'] = [
'group',
'count',
'size',
'client_id',
];
}
parent::__construct($config);
}
/**
* Method to auto-populate the model state.
*
* Note. Calling getState in this method will result in recursion.
*
* @param string $ordering Field for ordering.
* @param string $direction Direction of ordering.
*
* @return void
*
* @since 1.6
*/
protected function populateState($ordering = 'group', $direction = 'asc')
{
parent::populateState($ordering, $direction);
}
/**
* Method to get a store id based on model configuration state.
*
* This is necessary because the model is used by the component and
* different modules that might need different sets of data or different
* ordering requirements.
*
* @param string $id A prefix for the store id.
*
* @return string A store id.
*
* @since 3.5
*/
protected function getStoreId($id = '')
{
// Compile the store id.
$id .= ':' . $this->getState('filter.search');
return parent::getStoreId($id);
}
/**
* Method to get cache data
*
* @return array
*/
public function getData()
{
if (empty($this->_data)) {
try {
$cache = $this->getCache();
$data = $cache->getAll();
if ($data && \count($data) > 0) {
// Process filter by search term.
if ($search = $this->getState('filter.search')) {
foreach ($data as $key => $cacheItem) {
if (stripos($cacheItem->group, $search) === false) {
unset($data[$key]);
}
}
}
// Process ordering.
$listOrder = $this->getState('list.ordering', 'group');
$listDirn = $this->getState('list.direction', 'ASC');
$this->_data = ArrayHelper::sortObjects($data, $listOrder, strtolower($listDirn) === 'desc' ? -1 : 1, true, true);
// Process pagination.
$limit = (int) $this->getState('list.limit', 25);
if ($limit !== 0) {
$start = (int) $this->getState('list.start', 0);
return \array_slice($this->_data, $start, $limit);
}
} else {
$this->_data = [];
}
} catch (CacheConnectingException $exception) {
$this->setError(Text::_('COM_CACHE_ERROR_CACHE_CONNECTION_FAILED'));
$this->_data = [];
} catch (UnsupportedCacheException $exception) {
$this->setError(Text::_('COM_CACHE_ERROR_CACHE_DRIVER_UNSUPPORTED'));
$this->_data = [];
}
}
return $this->_data;
}
/**
* Method to get cache instance.
*
* @return CacheController
*/
public function getCache()
{
$app = Factory::getApplication();
$options = [
'defaultgroup' => '',
'storage' => $app->get('cache_handler', ''),
'caching' => true,
'cachebase' => $app->get('cache_path', JPATH_CACHE),
];
return Cache::getInstance('', $options);
}
/**
* Get the number of current Cache Groups.
*
* @return integer
*/
public function getTotal()
{
if (empty($this->_total)) {
$this->_total = \count($this->getData());
}
return $this->_total;
}
/**
* Method to get a pagination object for the cache.
*
* @return Pagination
*/
public function getPagination()
{
if (empty($this->_pagination)) {
$this->_pagination = new Pagination($this->getTotal(), $this->getState('list.start'), $this->getState('list.limit'));
}
return $this->_pagination;
}
/**
* Clean out a cache group as named by param.
* If no param is passed clean all cache groups.
*
* @param string $group Cache group name.
*
* @return boolean True on success, false otherwise
*/
public function clean($group = '')
{
try {
$this->getCache()->clean($group);
} catch (CacheConnectingException $exception) {
return false;
} catch (UnsupportedCacheException $exception) {
return false;
}
$this->getDispatcher()->dispatch('onAfterPurge', new AfterPurgeEvent('onAfterPurge', ['subject' => $group]));
return true;
}
/**
* Purge an array of cache groups.
*
* @param array $array Array of cache group names.
*
* @return array Array with errors, if they exist.
*/
public function cleanlist($array)
{
$errors = [];
foreach ($array as $group) {
if (!$this->clean($group)) {
$errors[] = $group;
}
}
return $errors;
}
/**
* Purge all cache items.
*
* @return boolean True if successful; false otherwise.
*/
public function purge()
{
try {
Factory::getCache('')->gc();
} catch (CacheConnectingException $exception) {
return false;
} catch (UnsupportedCacheException $exception) {
return false;
}
$this->getDispatcher()->dispatch('onAfterPurge', new AfterPurgeEvent('onAfterPurge'));
return true;
}
}

View File

@ -0,0 +1,153 @@
<?php
/**
* @package Joomla.Administrator
* @subpackage com_cache
*
* @copyright (C) 2008 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\Component\Cache\Administrator\View\Cache;
use Joomla\CMS\Form\Form;
use Joomla\CMS\Language\Text;
use Joomla\CMS\MVC\View\GenericDataException;
use Joomla\CMS\MVC\View\HtmlView as BaseHtmlView;
use Joomla\CMS\Pagination\Pagination;
use Joomla\CMS\Toolbar\ToolbarHelper;
use Joomla\Component\Cache\Administrator\Model\CacheModel;
// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
// phpcs:enable PSR1.Files.SideEffects
/**
* HTML View class for the Cache component
*
* @since 1.6
*/
class HtmlView extends BaseHtmlView
{
/**
* The search tools form
*
* @var Form
* @since 1.6
*/
public $filterForm;
/**
* The active search filters
*
* @var array
* @since 1.6
*/
public $activeFilters = [];
/**
* The cache data
*
* @var array
* @since 1.6
*/
protected $data = [];
/**
* The pagination object
*
* @var Pagination
* @since 1.6
*/
protected $pagination;
/**
* Total number of cache groups
*
* @var integer
* @since 1.6
*/
protected $total = 0;
/**
* The model state
*
* @var \Joomla\Registry\Registry
* @since 1.6
*/
protected $state;
/**
* Display a view.
*
* @param string $tpl The name of the template file to parse; automatically searches through the template paths.
*
* @return void
*
* @since 1.6
*
* @throws GenericDataException
*/
public function display($tpl = null): void
{
/** @var CacheModel $model */
$model = $this->getModel();
$this->data = $model->getData();
$this->pagination = $model->getPagination();
$this->total = $model->getTotal();
$this->state = $model->getState();
$this->filterForm = $model->getFilterForm();
$this->activeFilters = $model->getActiveFilters();
// Check for errors.
if (\count($errors = $this->get('Errors'))) {
throw new GenericDataException(implode("\n", $errors), 500);
}
if (!\count($this->data) && ($this->state->get('filter.search') === null || $this->state->get('filter.search') === '')) {
$this->setLayout('emptystate');
}
$this->addToolbar();
parent::display($tpl);
}
/**
* Add the page title and toolbar.
*
* @return void
*
* @since 1.6
*/
protected function addToolbar(): void
{
ToolbarHelper::title(Text::_('COM_CACHE_CLEAR_CACHE'), 'bolt clear');
// Get the toolbar object instance
$toolbar = $this->getDocument()->getToolbar();
if (\count($this->data)) {
$toolbar->delete('delete')
->listCheck(true);
$toolbar->confirmButton('delete', 'JTOOLBAR_DELETE_ALL', 'deleteAll')
->icon('icon-remove')
->listCheck(false)
->buttonClass('button-remove btn btn-primary');
$toolbar->confirmButton('delete', 'COM_CACHE_PURGE_EXPIRED', 'purge')
->name('delete')
->message('COM_CACHE_RESOURCE_INTENSIVE_WARNING');
$toolbar->divider();
}
if ($this->getCurrentUser()->authorise('core.admin', 'com_cache')) {
$toolbar->preferences('com_cache');
$toolbar->divider();
}
$toolbar->help('Maintenance:_Clear_Cache');
}
}