1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-08-08 17:56:52 +02:00

Merge remote-tracking branch 'Marc/ticket/12150' into develop-ascraeus

* Marc/ticket/12150:
  [ticket/12150] Use correct license URL in prune shadow migrations file
  [ticket/12150] Remove 'after' for columns from migrations file
  [ticket/12150] Update schema files for prune shadow topics
  [ticket/12150] Use log service instead of add_log() function
  [ticket/12150] Use shorter column names for prune settings
  [ticket/12150] Add functional tests for pruning shadow topics
  [ticket/12150] Add missing space to query for shadow topics
  [ticket/12150] Add missing prune settings variables in acp_forums
  [ticket/12150] Add file and caller for pruning shadow topics
  [ticket/12150] Add prune columns to schema files and migration file
  [ticket/12150] Add options to acp
This commit is contained in:
Joas Schilling
2014-04-06 22:26:27 +02:00
11 changed files with 515 additions and 0 deletions

View File

@@ -0,0 +1,191 @@
<?php
/**
*
* @package phpBB3
* @copyright (c) 2014 phpBB Group
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/
namespace phpbb\cron\task\core;
/**
* Prune one forum of its shadow topics cron task.
*
* It is intended to be used when cron is invoked via web.
* This task can decide whether it should be run using data obtained by viewforum
* code, without making additional database queries.
*
* @package phpBB3
*/
class prune_shadow_topics extends \phpbb\cron\task\base implements \phpbb\cron\task\parametrized
{
protected $phpbb_root_path;
protected $php_ext;
protected $config;
protected $db;
protected $log;
/**
* If $forum_data is given, it is assumed to contain necessary information
* about a single forum that is to be pruned.
*
* If $forum_data is not given, forum id will be retrieved via request_var
* and a database query will be performed to load the necessary information
* about the forum.
*/
protected $forum_data;
/**
* Constructor.
*
* @param string $phpbb_root_path The root path
* @param string $php_ext The PHP extension
* @param \phpbb\config\config $config The config
* @param \phpbb\db\driver\driver $db The db connection
* @param \phpbb\log\log $log The phpBB log system
*/
public function __construct($phpbb_root_path, $php_ext, \phpbb\config\config $config, \phpbb\db\driver\driver $db, \phpbb\log\log $log)
{
$this->phpbb_root_path = $phpbb_root_path;
$this->php_ext = $php_ext;
$this->config = $config;
$this->db = $db;
$this->log = $log;
}
/**
* Manually set forum data.
*
* @param array $forum_data Information about a forum to be pruned.
*/
public function set_forum_data($forum_data)
{
$this->forum_data = $forum_data;
}
/**
* Runs this cron task.
*
* @return null
*/
public function run()
{
if (!function_exists('auto_prune'))
{
include($this->phpbb_root_path . 'includes/functions_admin.' . $this->php_ext);
}
if ($this->forum_data['prune_shadow_days'])
{
$this->auto_prune_shadow_topics($this->forum_data['forum_id'], 'shadow', $this->forum_data['forum_flags'], $this->forum_data['prune_shadow_days'], $this->forum_data['prune_shadow_freq']);
}
}
/**
* Returns whether this cron task can run, given current board configuration.
*
* This cron task will not run when system cron is utilised, as in
* such cases prune_all_forums task would run instead.
*
* Additionally, this task must be given the forum data, either via
* the constructor or parse_parameters method.
*
* @return bool
*/
public function is_runnable()
{
return !$this->config['use_system_cron'] && $this->forum_data;
}
/**
* Returns whether this cron task should run now, because enough time
* has passed since it was last run.
*
* Forum pruning interval is specified in the forum data.
*
* @return bool
*/
public function should_run()
{
return $this->forum_data['enable_shadow_prune'] && $this->forum_data['prune_shadow_next'] < time();
}
/**
* Returns parameters of this cron task as an array.
* The array has one key, f, whose value is id of the forum to be pruned.
*
* @return array
*/
public function get_parameters()
{
return array('f' => $this->forum_data['forum_id']);
}
/**
* Parses parameters found in $request, which is an instance of
* \phpbb\request\request_interface.
*
* It is expected to have a key f whose value is id of the forum to be pruned.
*
* @param \phpbb\request\request_interface $request Request object.
*
* @return null
*/
public function parse_parameters(\phpbb\request\request_interface $request)
{
$this->forum_data = null;
if ($request->is_set('f'))
{
$forum_id = $request->variable('f', 0);
$sql = 'SELECT forum_id, prune_shadow_next, enable_shadow_prune, prune_shadow_days, forum_flags, prune_shadow_freq
FROM ' . FORUMS_TABLE . "
WHERE forum_id = $forum_id";
$result = $this->db->sql_query($sql);
$row = $this->db->sql_fetchrow($result);
$this->db->sql_freeresult($result);
if ($row)
{
$this->forum_data = $row;
}
}
}
/**
* Automatically prune shadow topics
* Based on fuunction auto_prune()
* @param int $forum_id Forum ID of forum that should be pruned
* @param string $prune_mode Prune mode
* @param int $prune_flags Prune flags
* @param int $prune_freq Prune frequency
* @return null
*/
protected function auto_prune_shadow_topics($forum_id, $prune_mode, $prune_flags, $prune_days, $prune_freq)
{
$sql = 'SELECT forum_name
FROM ' . FORUMS_TABLE . "
WHERE forum_id = $forum_id";
$result = $this->db->sql_query($sql, 3600);
$row = $this->db->sql_fetchrow($result);
$this->db->sql_freeresult($result);
if ($row)
{
$prune_date = time() - ($prune_days * 86400);
$next_prune = time() + ($prune_freq * 86400);
prune($forum_id, $prune_mode, $prune_date, $prune_flags, true);
$sql = 'UPDATE ' . FORUMS_TABLE . "
SET prune_shadow_next = $next_prune
WHERE forum_id = $forum_id";
$this->db->sql_query($sql);
$this->log->add('admin', 'LOG_PRUNE_SHADOW', $row['forum_name']);
}
return;
}
}

View File

@@ -0,0 +1,46 @@
<?php
/**
*
* @package migration
* @copyright (c) 2014 phpBB Group
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/
namespace phpbb\db\migration\data\v310;
class prune_shadow_topics extends \phpbb\db\migration\migration
{
static public function depends_on()
{
return array('\phpbb\db\migration\data\v310\dev');
}
public function update_schema()
{
return array(
'add_columns' => array(
$this->table_prefix . 'forums' => array(
'enable_shadow_prune' => array('BOOL', 0),
'prune_shadow_days' => array('UINT', 7),
'prune_shadow_freq' => array('UINT', 1),
'prune_shadow_next' => array('INT:11', 0),
),
),
);
}
public function revert_schema()
{
return array(
'drop_columns' => array(
$this->table_prefix . 'forums' => array(
'enable_shadow_prune',
'prune_shadow_days',
'prune_shadow_freq',
'prune_shadow_next',
),
),
);
}
}