Commit 52231254 authored by Dmitrii Metelkin's avatar Dmitrii Metelkin
Browse files

Bug 1470708: Add a "clear caches" admin script

behatnotneeded

Change-Id: Ib373e765d30a265f78944ef0066bc9895b3d68b9
parent 25edbfbf
<?php
/**
* Clear all mahara caches.
*
* @package mahara
* @subpackage core
* @author Dmitrii Metelkin <dmitriim@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later
* @copyright For copyright information on Mahara, please see the README file distributed with this software.
*
*/
define('CLI', 1);
define('INTERNAL', 1);
require(dirname(dirname(dirname(__FILE__))) . '/init.php');
require(get_config('libroot') . 'cli.php');
require(get_config('libroot') . 'upgrade.php');
$cli = get_cli();
$options = array();
$settings = new stdClass();
$settings->options = $options;
$settings->info = get_string('cliclearcachesdescription', 'admin');
$cli->setup($settings);
log_info(get_string('cliclearingcaches', 'admin'));
$result = clear_all_caches();
if ($result) {
log_info(get_string('clearingcachessucceed', 'admin'));
}
......@@ -60,6 +60,19 @@ $closeform = pieform(array(
),
));
$clearcachesform = pieform(array(
'name' => 'clear_caches',
'renderer' => 'oneline',
'autofocus' => 'false',
'elements' => array(
'submit' => array(
'type' => 'submit',
'value' => get_string('clearcachessubmit', 'admin'),
'class' => 'btn-default',
),
),
));
if (get_config('installed')) {
$sitedata = site_statistics();
}
......@@ -79,6 +92,7 @@ if (isset($register)) {
$smarty->assign('closed', $closed);
$smarty->assign('closeform', $closeform);
$smarty->assign('clearcachesform', $clearcachesform);
$smarty->assign('warnings', site_warnings());
......@@ -96,3 +110,18 @@ function close_site_submit(Pieform $form, $values) {
}
redirect(get_config('wwwroot') . 'admin/index.php');
}
function clear_caches_submit() {
global $SESSION;
$result = clear_all_caches();
if (!$result) {
$SESSION->add_error_msg(get_string('clearingcacheserror', 'admin'));
}
else {
$SESSION->add_ok_msg(get_string('clearingcachessucceed', 'admin'));
}
redirect(get_config('wwwroot') . 'admin/index.php');
}
......@@ -61,6 +61,12 @@ $string['cliinstallerdescription'] = 'Install Mahara and create required data di
$string['cliinstallingmahara'] = 'Installing Mahara';
$string['cliupgraderdescription'] = 'Upgrade the Mahara database and data to the version of Mahara installed';
$string['cliupgradingmahara'] = 'Upgrading Mahara';
$string['cliclearingcaches'] = 'Clearing Mahara caches.';
$string['cliclearcachesdescription'] = 'Clearing caches will delete cached data from the server. There is no danger in clearing caches, but your site may appear slower for a while until the server and clients calculate new information and cache it.';
$string['clearcachesheading'] = 'Clear caches';
$string['clearcachessubmit'] = 'Clear caches';
$string['clearingcachessucceed'] = 'All caches were cleared.';
$string['clearingcacheserror'] = 'Error while clearing caches. Please check logs to get more information about this error.';
$string['maharanotinstalled'] = 'Mahara is not currently installed. Please install Mahara before trying to upgrade.';
// Admin navigation menu
......
......@@ -4476,5 +4476,12 @@ function xmldb_core_upgrade($oldversion=0) {
}
}
if ($oldversion < 2016061700) {
log_debug('Add a "clearcaches" event');
$e = new StdClass;
$e->name = 'clearcaches';
insert_record('event_type', $e);
}
return $status;
}
......@@ -4549,3 +4549,33 @@ function is_valid_serialized_skin_attribute($sobj) {
}
return false;
}
/*
* Crear all Mahara chaches.
*
* @return bool True if success, false otherwise.
*/
function clear_all_caches() {
require_once(get_config('libroot') . 'file.php');
try {
clear_menu_cache();
update_safe_iframe_regex();
bump_cache_version();
$dwoo_dir = get_config('dataroot') . 'dwoo';
if (check_dir_exists($dwoo_dir) && !rmdirr($dwoo_dir)) {
throw new SystemException('Can not remove dwoo directory ' . $dwoo_dir);
}
handle_event('clearcaches', array());
$result = true;
}
catch (Exception $e) {
log_info("Error while cleaning caches: " . $e->GetMessage());
$result = false;
}
return $result;
}
......@@ -880,6 +880,7 @@ function core_install_firstcoredata_defaults() {
'removefriendrequest',
'creategroup',
'loginas',
'clearcaches',
);
foreach ($eventtypes as $et) {
......
......@@ -16,7 +16,7 @@ $config = new stdClass();
// See https://wiki.mahara.org/wiki/Developer_Area/Version_Numbering_Policy
// For upgrades on stable branches, increment the version by one. On master, use the date.
$config->version = 2016061000;
$config->version = 2016061700;
$config->series = '16.10';
$config->release = '16.10dev';
$config->minupgradefrom = 2012080604;
......
......@@ -110,6 +110,14 @@
{/if}
</div>
<div class="panel panel-default">
<h3 class="panel-heading">{str tag=clearcachesheading section=admin} <span class="icon icon-refresh pull-right" role="presentation" aria-hidden="true"></span></h3>
<div class="panel-body">
<p>{str tag=cliclearcachesdescription section=admin}</p>
{$clearcachesform|safe}
</div>
</div>
<div class="panel panel-default">
<h3 class="panel-heading">{str tag=configsite section=admin} <span class="icon icon-cogs pull-right" role="presentation" aria-hidden="true"></span></h3>
<ul class="list-group">
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment