Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
prune_forum
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 7
210
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
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 / 8
0.00% covered (danger)
0.00%
0 / 1
20
 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
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 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_forum 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
30    /**
31    * If $forum_data is given, it is assumed to contain necessary information
32    * about a single forum that is to be pruned.
33    *
34    * If $forum_data is not given, forum id will be retrieved via $request->variable()
35    * and a database query will be performed to load the necessary information
36    * about the forum.
37    */
38    protected $forum_data;
39
40    /**
41    * Constructor.
42    *
43    * @param string $phpbb_root_path The root path
44    * @param string $php_ext PHP file extension
45    * @param \phpbb\config\config $config The config
46    * @param \phpbb\db\driver\driver_interface $db The db connection
47    */
48    public function __construct($phpbb_root_path, $php_ext, \phpbb\config\config $config, \phpbb\db\driver\driver_interface $db)
49    {
50        $this->phpbb_root_path = $phpbb_root_path;
51        $this->php_ext = $php_ext;
52        $this->config = $config;
53        $this->db = $db;
54    }
55
56    /**
57    * Manually set forum data.
58    *
59    * @param array $forum_data Information about a forum to be pruned.
60    */
61    public function set_forum_data($forum_data)
62    {
63        $this->forum_data = $forum_data;
64    }
65
66    /**
67    * Runs this cron task.
68    *
69    * @return void
70    */
71    public function run()
72    {
73        if (!function_exists('auto_prune'))
74        {
75            include($this->phpbb_root_path . 'includes/functions_admin.' . $this->php_ext);
76        }
77
78        $log_prune = true;
79
80        if ($this->forum_data['prune_days'])
81        {
82            auto_prune($this->forum_data['forum_id'], 'posted', $this->forum_data['forum_flags'], $this->forum_data['prune_days'], $this->forum_data['prune_freq'], $log_prune);
83            $log_prune = false;
84        }
85
86        if ($this->forum_data['prune_viewed'])
87        {
88            auto_prune($this->forum_data['forum_id'], 'viewed', $this->forum_data['forum_flags'], $this->forum_data['prune_viewed'], $this->forum_data['prune_freq'], $log_prune);
89        }
90    }
91
92    /**
93    * Returns whether this cron task can run, given current board configuration.
94    *
95    * This cron task will not run when system cron is utilised, as in
96    * such cases prune_all_forums task would run instead.
97    *
98    * Additionally, this task must be given the forum data, either via
99    * the constructor or parse_parameters method.
100    *
101    * @return bool
102    */
103    public function is_runnable()
104    {
105        return !$this->config['use_system_cron'] && $this->forum_data;
106    }
107
108    /**
109    * Returns whether this cron task should run now, because enough time
110    * has passed since it was last run.
111    *
112    * Forum pruning interval is specified in the forum data.
113    *
114    * @return bool
115    */
116    public function should_run()
117    {
118        return $this->forum_data['enable_prune'] && $this->forum_data['prune_next'] < time();
119    }
120
121    /**
122    * Returns parameters of this cron task as an array.
123    * The array has one key, f, whose value is id of the forum to be pruned.
124    *
125    * @return array
126    */
127    public function get_parameters()
128    {
129        return array('f' => $this->forum_data['forum_id']);
130    }
131
132    /**
133    * Parses parameters found in $request, which is an instance of
134    * \phpbb\request\request_interface.
135    *
136    * It is expected to have a key f whose value is id of the forum to be pruned.
137    *
138    * @param \phpbb\request\request_interface $request Request object.
139    *
140    * @return void
141    */
142    public function parse_parameters(\phpbb\request\request_interface $request)
143    {
144        $this->forum_data = null;
145        if ($request->is_set('f'))
146        {
147            $forum_id = $request->variable('f', 0);
148
149            $sql = 'SELECT forum_id, prune_next, enable_prune, prune_days, prune_viewed, forum_flags, prune_freq
150                FROM ' . FORUMS_TABLE . "
151                WHERE forum_id = $forum_id";
152            $result = $this->db->sql_query($sql);
153            $row = $this->db->sql_fetchrow($result);
154            $this->db->sql_freeresult($result);
155
156            if ($row)
157            {
158                $this->forum_data = $row;
159            }
160        }
161    }
162}