mirror of
https://github.com/phpbb/phpbb.git
synced 2025-07-31 14:00:31 +02:00
[ticket/11373] Prune old read notifications with cron
PHPBB3-11373
This commit is contained in:
75
phpBB/phpbb/cron/task/core/prune_notifications.php
Normal file
75
phpBB/phpbb/cron/task/core/prune_notifications.php
Normal file
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
/**
|
||||
*
|
||||
* @package phpBB3
|
||||
* @copyright (c) 2013 phpBB Group
|
||||
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
if (!defined('IN_PHPBB'))
|
||||
{
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prune notifications cron task.
|
||||
*
|
||||
* @package phpBB3
|
||||
*/
|
||||
class phpbb_cron_task_core_prune_notifications extends phpbb_cron_task_base
|
||||
{
|
||||
protected $config;
|
||||
protected $notification_manager;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param phpbb_config $config The config
|
||||
* @param phpbb_notification_manager $notification_manager Notification manager
|
||||
*/
|
||||
public function __construct(phpbb_config $config, phpbb_notification_manager $notification_manager)
|
||||
{
|
||||
$this->config = $config;
|
||||
$this->notification_manager = $notification_manager;
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs this cron task.
|
||||
*
|
||||
* @return null
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
// time minus expire days in seconds
|
||||
$timestamp = time() - ($this->config['read_notification_expire_days'] * 60 * 60 * 24);
|
||||
$this->notification_manager->prune_notifications($timestamp);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether this cron task can run, given current board configuration.=
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function is_runnable()
|
||||
{
|
||||
return (bool) $this->config['read_notification_expire_days'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether this cron task should run now, because enough time
|
||||
* has passed since it was last run.
|
||||
*
|
||||
* The interval between prune notifications is specified in board
|
||||
* configuration.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function should_run()
|
||||
{
|
||||
return $this->config['read_notification_last_gc'] < time() - $this->config['read_notification_gc'];
|
||||
}
|
||||
}
|
25
phpBB/phpbb/db/migration/data/310/notifications_cron.php
Normal file
25
phpBB/phpbb/db/migration/data/310/notifications_cron.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
/**
|
||||
*
|
||||
* @package migration
|
||||
* @copyright (c) 2013 phpBB Group
|
||||
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
|
||||
*
|
||||
*/
|
||||
|
||||
class phpbb_db_migration_data_310_notifications_cron extends phpbb_db_migration
|
||||
{
|
||||
static public function depends_on()
|
||||
{
|
||||
return array('phpbb_db_migration_data_310_notifications');
|
||||
}
|
||||
|
||||
public function update_data()
|
||||
{
|
||||
return array(
|
||||
array('config.add', array('read_notification_expire_days', 30)),
|
||||
array('config.add', array('read_notification_last_gc', 0)), // last run
|
||||
array('config.add', array('read_notification_gc', (60 * 60 * 24))), // seconds between run; 1 day
|
||||
);
|
||||
}
|
||||
}
|
@@ -59,7 +59,7 @@ class phpbb_notification_manager
|
||||
|
||||
/**
|
||||
* Notification Constructor
|
||||
*
|
||||
*
|
||||
* @param array $notification_types
|
||||
* @param array $notification_methods
|
||||
* @param ContainerBuilder $phpbb_container
|
||||
@@ -796,11 +796,13 @@ class phpbb_notification_manager
|
||||
* Delete all notifications older than a certain time
|
||||
*
|
||||
* @param int $timestamp Unix timestamp to delete all notifications that were created before
|
||||
* @param bool $only_unread True (default) to only prune read notifications
|
||||
*/
|
||||
public function prune_notifications($timestamp)
|
||||
public function prune_notifications($timestamp, $only_read = true)
|
||||
{
|
||||
$sql = 'DELETE FROM ' . $this->notifications_table . '
|
||||
WHERE notification_time < ' . (int) $timestamp;
|
||||
WHERE notification_time < ' . (int) $timestamp .
|
||||
(($only_read) ? ' AND notification_read = 1' : '');
|
||||
$this->db->sql_query($sql);
|
||||
}
|
||||
|
||||
@@ -834,12 +836,12 @@ class phpbb_notification_manager
|
||||
protected function load_object($object_name)
|
||||
{
|
||||
$object = $this->phpbb_container->get($object_name);
|
||||
|
||||
|
||||
if (method_exists($object, 'set_notification_manager'))
|
||||
{
|
||||
$object->set_notification_manager($this);
|
||||
}
|
||||
|
||||
|
||||
return $object;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user