1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* Ajax Shoutbox extension for the phpBB Forum Software package. |
5
|
|
|
* |
6
|
|
|
* @copyright (c) 2014 Paul Sohier <http://www.ajax-shoutbox.com> |
7
|
|
|
* @license GNU General Public License, version 2 (GPL-2.0) |
8
|
|
|
* |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace paul999\ajaxshoutbox\cron; |
12
|
|
|
|
13
|
|
|
class shoutbox_prune extends \phpbb\cron\task\base { |
14
|
|
|
|
15
|
|
|
/** @var \phpbb\config\config */ |
16
|
|
|
private $config; |
17
|
|
|
|
18
|
|
|
/** @var \phpbb\db\driver\driver_interface */ |
19
|
|
|
private $db; |
20
|
|
|
|
21
|
|
|
/** @var string */ |
22
|
|
|
private $table; |
23
|
|
|
|
24
|
|
|
/** @var \phpbb\log\log */ |
25
|
|
|
private $log; |
26
|
|
|
|
27
|
|
|
/** @var \phpbb\user */ |
28
|
|
|
private $user; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param \phpbb\config\config $config |
32
|
|
|
* @param \phpbb\db\driver\driver_interface $db |
33
|
|
|
* @param \phpbb\log\log $log |
34
|
|
|
* @param \phpbb\user $user |
35
|
|
|
* @param $table |
36
|
|
|
*/ |
37
|
|
View Code Duplication |
public function __construct(\phpbb\config\config $config, \phpbb\db\driver\driver_interface $db, |
|
|
|
|
38
|
|
|
\phpbb\log\log $log, \phpbb\user $user, $table) |
39
|
|
|
{ |
40
|
|
|
$this->config = $config; |
41
|
|
|
$this->db = $db; |
42
|
|
|
$this->log = $log; |
43
|
|
|
$this->table = $table; |
44
|
|
|
$this->user = $user; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Run the cronjob. |
49
|
|
|
*/ |
50
|
|
|
public function run() |
51
|
|
|
{ |
52
|
|
|
$time = strtotime('- ' . (int) $this->config['ajaxshoutbox_prune_days'] . ' days'); |
53
|
|
|
$sql = 'SELECT * |
54
|
|
|
FROM ' . $this->table . ' |
55
|
|
|
WHERE |
56
|
|
|
post_time <= ' . (int) $time; |
57
|
|
|
|
58
|
|
|
$result = $this->db->sql_query($sql); |
59
|
|
|
$delete = array(); |
60
|
|
|
|
61
|
|
|
while ($row = $this->db->sql_fetchrow($result)) |
62
|
|
|
{ |
63
|
|
|
$delete[] = $row['shout_id']; |
64
|
|
|
} |
65
|
|
|
$this->db->sql_freeresult(); |
66
|
|
|
|
67
|
|
|
if (sizeof($delete)) |
68
|
|
|
{ |
69
|
|
|
$sql = 'DELETE FROM ' . $this->table . ' |
70
|
|
|
WHERE ' . $this->db->sql_in_set('shout_id', $delete); |
71
|
|
|
$this->db->sql_query($sql); |
72
|
|
|
$uuid = $this->user->data['user_id']; |
73
|
|
|
|
74
|
|
|
if (!$uuid) |
75
|
|
|
{ |
76
|
|
|
$uuid = ANONYMOUS; // the log interface doesn't fall back to ANONYMOUS as the add_log function did... |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$this->log->add('admin', $uuid, $this->user->ip, 'LOG_AJAX_SHOUTBOX_PRUNED', time(), array(sizeof($delete))); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$this->config->set('shoutbox_prune_gc', time(), false); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Should this cron run? |
87
|
|
|
* @return bool |
88
|
|
|
*/ |
89
|
|
|
public function is_runnable() |
90
|
|
|
{ |
91
|
|
|
return (bool) $this->config['ajaxshoutbox_enable_prune']; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return bool |
96
|
|
|
*/ |
97
|
|
|
public function should_run() |
98
|
|
|
{ |
99
|
|
|
return (int) $this->config['shoutbox_prune_gc'] < strtotime('24 hours ago'); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.