1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-04-19 23:32:02 +02:00

Merge pull request #2035 from prototech/ticket/12138

[ticket/12138] Include file time limit when generating attachment management pagination and stats.
This commit is contained in:
Nathan Guse 2014-02-20 21:13:06 -06:00
commit c0c19fbb29

View File

@ -20,14 +20,37 @@ if (!defined('IN_PHPBB'))
*/
class acp_attachments
{
var $u_action;
var $new_config;
/** @var \phpbb\db\driver\driver */
protected $db;
/** @var \phpbb\config\config */
protected $config;
/** @var ContainerBuilder */
protected $phpbb_container;
/** @var \phpbb\template\template */
protected $template;
/** @var \phpbb\user */
protected $user;
public $id;
public $u_action;
protected $new_config;
function main($id, $mode)
{
global $db, $user, $auth, $template, $cache, $phpbb_container;
global $config, $phpbb_admin_path, $phpbb_root_path, $phpEx;
$this->id = $id;
$this->db = $db;
$this->config = $config;
$this->template = $template;
$this->user = $user;
$this->phpbb_container = $phpbb_container;
$user->add_lang(array('posting', 'viewtopic', 'acp/attachments'));
$error = $notify = array();
@ -1082,9 +1105,26 @@ class acp_attachments
}
}
if ($action == 'stats')
{
$this->handle_stats_resync();
}
$stats_error = $this->check_stats_accuracy();
if ($stats_error)
{
$error[] = $stats_error;
// Show option to resync stats
$this->template->assign_vars(array(
'S_ACTION_OPTIONS' => $auth->acl_get('a_board'),
));
}
$template->assign_vars(array(
'S_MANAGE' => true)
);
'S_MANAGE' => true,
));
$start = request_var('start', 0);
@ -1107,66 +1147,11 @@ class acp_attachments
$attachments_per_page = (int) $config['topics_per_page'];
// Handle files stats resync
$action = request_var('action', '');
$resync_files_stats = false;
if ($action && $action = 'stats')
{
if (!confirm_box(true))
{
confirm_box(false, $user->lang['RESYNC_FILES_STATS_CONFIRM'], build_hidden_fields(array(
'i' => $id,
'mode' => $mode,
'action' => $action,
)));
}
else
{
$resync_files_stats = true;
add_log('admin', 'LOG_RESYNC_FILES_STATS');
}
}
// Check if files stats are accurate
$sql = 'SELECT COUNT(attach_id) as num_files
FROM ' . ATTACHMENTS_TABLE . '
WHERE is_orphan = 0';
$result = $db->sql_query($sql, 600);
$num_files_real = (int) $db->sql_fetchfield('num_files');
if ($resync_files_stats === true)
{
set_config('num_files', $num_files_real, true);
}
$db->sql_freeresult($result);
$sql = 'SELECT SUM(filesize) as upload_dir_size
FROM ' . ATTACHMENTS_TABLE . '
WHERE is_orphan = 0';
$result = $db->sql_query($sql, 600);
$total_size_real = (float) $db->sql_fetchfield('upload_dir_size');
if ($resync_files_stats === true)
{
set_config('upload_dir_size', $total_size_real, true);
}
$db->sql_freeresult($result);
// Get current files stats
$num_files = (int) $config['num_files'];
$total_size = (float) $config['upload_dir_size'];
// Issue warning message if files stats are inaccurate
if (($num_files != $num_files_real) || ($total_size != $total_size_real))
{
$error[] = $user->lang('FILES_STATS_WRONG', (int) $num_files_real, get_formatted_filesize($total_size_real));
$template->assign_vars(array(
'S_ACTION_OPTIONS' => ($auth->acl_get('a_board')) ? true : false,
'U_ACTION' => $this->u_action,)
);
}
$stats = $this->get_attachment_stats($limit_filetime);
$num_files = $stats['num_files'];
$total_size = $stats['upload_dir_size'];
// Make sure $start is set to the last page if it exceeds the amount
$pagination = $phpbb_container->get('pagination');
$start = $pagination->validate_start($start, $attachments_per_page, $num_files);
@ -1282,6 +1267,89 @@ class acp_attachments
}
}
/**
* Get attachment file count and size of upload directory
*
* @param $limit string Additional limit for WHERE clause to filter stats by.
* @return array Returns array with stats: num_files and upload_dir_size
*/
public function get_attachment_stats($limit = '')
{
$sql = 'SELECT COUNT(a.attach_id) AS num_files, SUM(a.filesize) AS upload_dir_size
FROM ' . ATTACHMENTS_TABLE . " a
WHERE a.is_orphan = 0
$limit";
$result = $this->db->sql_query($sql);
$row = $this->db->sql_fetchrow($result);
$this->db->sql_freeresult($result);
return array(
'num_files' => (int) $row['num_files'],
'upload_dir_size' => (float) $row['upload_dir_size'],
);
}
/**
* Set config attachment stat values
*
* @param $stats array Array of config key => value pairs to set.
* @return null
*/
public function set_attachment_stats($stats)
{
foreach ($stats as $key => $value)
{
$this->config->set($key, $value, true);
}
}
/**
* Check accuracy of attachment statistics.
*
* @param $resync bool Resync stats if they're incorrect.
* @return bool|string Returns false if stats are correct or error message
* otherwise.
*/
public function check_stats_accuracy()
{
// Get fresh stats.
$stats = $this->get_attachment_stats();
// Get current files stats
$num_files = (int) $this->config['num_files'];
$total_size = (float) $this->config['upload_dir_size'];
if (($num_files != $stats['num_files']) || ($total_size != $stats['upload_dir_size']))
{
return $this->user->lang('FILES_STATS_WRONG', (int) $stats['num_files'], get_formatted_filesize($stats['upload_dir_size']));
}
return false;
}
/**
* Handle stats resync.
*
* @return null
*/
public function handle_stats_resync()
{
if (!confirm_box(true))
{
confirm_box(false, $this->user->lang['RESYNC_FILES_STATS_CONFIRM'], build_hidden_fields(array(
'i' => $this->id,
'mode' => 'manage',
'action' => 'stats',
)));
}
else
{
$this->set_attachment_stats($this->get_attachment_stats());
$log = $this->phpbb_container->get('log');
$log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_RESYNC_FILES_STATS');
}
}
/**
* Build Select for category items
*/