Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 33
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
reparser
0.00% covered (danger)
0.00%
0 / 33
0.00% covered (danger)
0.00%
0 / 5
380
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_reparser
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
12
 is_runnable
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
20
 should_run
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
20
 run
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
56
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\text_reparser;
15
16/**
17 * Reparse text cron task
18 */
19class reparser extends \phpbb\cron\task\base
20{
21    const MIN = 1;
22    const SIZE = 100;
23
24    /**
25     * @var \phpbb\config\config
26     */
27    protected $config;
28
29    /**
30     * @var \phpbb\lock\db
31     */
32    protected $reparse_lock;
33
34    /**
35     * @var \phpbb\textreparser\manager
36     */
37    protected $reparser_manager;
38
39    /**
40     * @var string
41     */
42    protected $reparser_name;
43
44    /**
45     * @var \phpbb\di\service_collection
46     */
47    protected $reparsers;
48
49    /**
50     * @var array
51     */
52    protected $resume_data;
53
54    /**
55     * Constructor
56     *
57     * @param \phpbb\config\config            $config
58     * @param \phpbb\lock\db                $reparse_lock
59     * @param \phpbb\textreparser\manager    $reparser_manager
60     * @param \phpbb\di\service_collection    $reparsers
61     */
62    public function __construct(\phpbb\config\config $config, \phpbb\lock\db $reparse_lock, \phpbb\textreparser\manager $reparser_manager, \phpbb\di\service_collection $reparsers)
63    {
64        $this->config = $config;
65        $this->reparse_lock = $reparse_lock;
66        $this->reparser_manager = $reparser_manager;
67        $this->reparsers = $reparsers;
68    }
69
70    /**
71     * Sets the reparser for this cron task
72     *
73     * @param string    $reparser
74     */
75    public function set_reparser($reparser)
76    {
77        $this->reparser_name = !isset($this->reparsers[$reparser]) ? $this->reparser_manager->find_reparser($reparser) : $reparser;
78
79        if ($this->resume_data === null)
80        {
81            $this->resume_data = $this->reparser_manager->get_resume_data($this->reparser_name);
82        }
83    }
84
85    /**
86     * {@inheritdoc}
87     */
88    public function is_runnable()
89    {
90        if ($this->resume_data === null)
91        {
92            $this->resume_data = $this->reparser_manager->get_resume_data($this->reparser_name);
93        }
94
95        if (!isset($this->resume_data['range-max']) || $this->resume_data['range-max'] >= $this->resume_data['range-min'])
96        {
97            return true;
98        }
99
100        return false;
101    }
102
103    /**
104     * {@inheritdoc}
105     */
106    public function should_run()
107    {
108        if (!empty($this->config['reparse_lock']))
109        {
110            $last_run = explode(' ', $this->config['reparse_lock']);
111
112            if ($last_run[0] + 3600 >= time())
113            {
114                return false;
115            }
116        }
117
118        if ($this->config[$this->reparser_name . '_cron_interval'])
119        {
120            return $this->config[$this->reparser_name . '_last_cron'] < time() - $this->config[$this->reparser_name . '_cron_interval'];
121        }
122
123        return false;
124    }
125
126    /**
127     * {@inheritdoc}
128     */
129    public function run()
130    {
131        if ($this->reparse_lock->acquire())
132        {
133            if ($this->resume_data === null)
134            {
135                $this->resume_data = $this->reparser_manager->get_resume_data($this->reparser_name);
136            }
137
138            /**
139             * @var \phpbb\textreparser\reparser_interface $reparser
140             */
141            $reparser = $this->reparsers[$this->reparser_name];
142
143            $min = isset($this->resume_data['range-min']) ? $this->resume_data['range-min'] : self::MIN;
144            $current = isset($this->resume_data['range-max']) ? $this->resume_data['range-max'] : $reparser->get_max_id();
145            $size = isset($this->resume_data['range-size']) ? $this->resume_data['range-size'] : self::SIZE;
146
147            if ($current >= $min)
148            {
149                $start = max($min, $current + 1 - $size);
150                $end = max($min, $current);
151
152                $reparser->reparse_range($start, $end);
153
154                $this->reparser_manager->update_resume_data($this->reparser_name, $min, $start - 1, $size);
155            }
156
157            $this->config->set($this->reparser_name . '_last_cron', time());
158            $this->reparse_lock->release();
159        }
160    }
161}