Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 42
0.00% covered (danger)
0.00%
0 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
prune_shadow_topics
0.00% covered (danger)
0.00%
0 / 42
0.00% covered (danger)
0.00%
0 / 8
306
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 set_forum_data
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 run
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
12
 is_runnable
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
 should_run
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
 get_parameters
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 parse_parameters
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
12
 auto_prune_shadow_topics
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2/**
3*
4* This file is part of the phpBB Forum Software package.
5*
6* @copyright (c) phpBB Limited <https://www.phpbb.com>
7* @license GNU General Public License, version 2 (GPL-2.0)
8*
9* For full copyright and license information, please see
10* the docs/CREDITS.txt file.
11*
12*/
13
14namespace phpbb\cron\task\core;
15
16/**
17* Prune one forum of its shadow topics cron task.
18*
19* It is intended to be used when cron is invoked via web.
20* This task can decide whether it should be run using data obtained by viewforum
21* code, without making additional database queries.
22*/
23class prune_shadow_topics extends \phpbb\cron\task\base implements \phpbb\cron\task\parametrized
24{
25    protected $phpbb_root_path;
26    protected $php_ext;
27    protected $config;
28    protected $db;
29    protected $log;
30    protected $user;
31
32    /**
33    * If $forum_data is given, it is assumed to contain necessary information
34    * about a single forum that is to be pruned.
35    *
36    * If $forum_data is not given, forum id will be retrieved via $request->variable()
37    * and a database query will be performed to load the necessary information
38    * about the forum.
39    */
40    protected $forum_data;
41
42    /**
43    * Constructor.
44    *
45    * @param string $phpbb_root_path The root path
46    * @param string $php_ext PHP file extension
47    * @param \phpbb\config\config $config The config
48    * @param \phpbb\db\driver\driver_interface $db The db connection
49    * @param \phpbb\log\log $log The phpBB log system
50    * @param \phpbb\user $user The phpBB user object
51    */
52    public function __construct($phpbb_root_path, $php_ext, \phpbb\config\config $config, \phpbb\db\driver\driver_interface $db, \phpbb\log\log $log, \phpbb\user $user)
53    {
54        $this->phpbb_root_path = $phpbb_root_path;
55        $this->php_ext = $php_ext;
56        $this->config = $config;
57        $this->db = $db;
58        $this->log = $log;
59        $this->user = $user;
60    }
61
62    /**
63    * Manually set forum data.
64    *
65    * @param array $forum_data Information about a forum to be pruned.
66    */
67    public function set_forum_data($forum_data)
68    {
69        $this->forum_data = $forum_data;
70    }
71
72    /**
73    * Runs this cron task.
74    *
75    * @return void
76    */
77    public function run()
78    {
79        if (!function_exists('auto_prune'))
80        {
81            include($this->phpbb_root_path . 'includes/functions_admin.' . $this->php_ext);
82        }
83
84        if ($this->forum_data['prune_shadow_days'])
85        {
86            $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']);
87        }
88    }
89
90    /**
91    * Returns whether this cron task can run, given current board configuration.
92    *
93    * This cron task will not run when system cron is utilised, as in
94    * such cases prune_all_forums task would run instead.
95    *
96    * Additionally, this task must be given the forum data, either via
97    * the constructor or parse_parameters method.
98    *
99    * @return bool
100    */
101    public function is_runnable()
102    {
103        return !$this->config['use_system_cron'] && $this->forum_data;
104    }
105
106    /**
107    * Returns whether this cron task should run now, because enough time
108    * has passed since it was last run.
109    *
110    * Forum pruning interval is specified in the forum data.
111    *
112    * @return bool
113    */
114    public function should_run()
115    {
116        return $this->forum_data['enable_shadow_prune'] && $this->forum_data['prune_shadow_next'] < time();
117    }
118
119    /**
120    * Returns parameters of this cron task as an array.
121    * The array has one key, f, whose value is id of the forum to be pruned.
122    *
123    * @return array
124    */
125    public function get_parameters()
126    {
127        return array('f' => $this->forum_data['forum_id']);
128    }
129
130    /**
131    * Parses parameters found in $request, which is an instance of
132    * \phpbb\request\request_interface.
133    *
134    * It is expected to have a key f whose value is id of the forum to be pruned.
135    *
136    * @param \phpbb\request\request_interface $request Request object.
137    *
138    * @return void
139    */
140    public function parse_parameters(\phpbb\request\request_interface $request)
141    {
142        $this->forum_data = null;
143        if ($request->is_set('f'))
144        {
145            $forum_id = $request->variable('f', 0);
146
147            $sql = 'SELECT forum_id, prune_shadow_next, enable_shadow_prune, prune_shadow_days, forum_flags, prune_shadow_freq
148                FROM ' . FORUMS_TABLE . "
149                WHERE forum_id = $forum_id";
150            $result = $this->db->sql_query($sql);
151            $row = $this->db->sql_fetchrow($result);
152            $this->db->sql_freeresult($result);
153
154            if ($row)
155            {
156                $this->forum_data = $row;
157            }
158        }
159    }
160
161    /**
162    * Automatically prune shadow topics
163    * Based on fuunction auto_prune()
164    * @param int $forum_id Forum ID of forum that should be pruned
165    * @param string $prune_mode Prune mode
166    * @param int $prune_flags Prune flags
167    * @param int $prune_days Prune date in days
168    * @param int $prune_freq Prune frequency
169    *
170    * @return void
171    */
172    protected function auto_prune_shadow_topics($forum_id, $prune_mode, $prune_flags, $prune_days, $prune_freq)
173    {
174        $sql = 'SELECT forum_name
175            FROM ' . FORUMS_TABLE . "
176            WHERE forum_id = $forum_id";
177        $result = $this->db->sql_query($sql, 3600);
178        $row = $this->db->sql_fetchrow($result);
179        $this->db->sql_freeresult($result);
180
181        if ($row)
182        {
183            $prune_date = time() - ($prune_days * 86400);
184            $next_prune = time() + ($prune_freq * 86400);
185
186            prune($forum_id, $prune_mode, $prune_date, $prune_flags, true);
187
188            $sql = 'UPDATE ' . FORUMS_TABLE . "
189                SET prune_shadow_next = $next_prune
190                WHERE forum_id = $forum_id";
191            $this->db->sql_query($sql);
192
193            $user_id = (empty($this->user->data)) ? ANONYMOUS : $this->user->data['user_id'];
194            $user_ip = (empty($this->user->ip)) ? '' : $this->user->ip;
195
196            $this->log->add('admin', $user_id, $user_ip, 'LOG_PRUNE_SHADOW', false, array($row['forum_name']));
197        }
198    }
199}