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\textreparser;
15
16class manager
17{
18	/**
19	 * @var \phpbb\config\config
20	 */
21	protected $config;
22
23	/**
24	 * @var \phpbb\config\db_text
25	 */
26	protected $config_text;
27
28	/**
29	 * @var \phpbb\di\service_collection
30	 */
31	protected $reparsers;
32
33	/**
34	 * @var array
35	 */
36	protected $resume_data;
37
38	/**
39	 * Constructor
40	 *
41	 * @param \phpbb\config\config			$config
42	 * @param \phpbb\config\db_text			$config_text
43	 * @param \phpbb\di\service_collection	$reparsers
44	 */
45	public function __construct(\phpbb\config\config $config, \phpbb\config\db_text $config_text, \phpbb\di\service_collection $reparsers)
46	{
47		$this->config = $config;
48		$this->config_text = $config_text;
49		$this->reparsers = $reparsers;
50	}
51
52	/**
53	 * Loads resume data from the database
54	 *
55	 * @param string	$name	Name of the reparser to which the resume data belongs
56	 *
57	 * @return array
58	 */
59	public function get_resume_data($name)
60	{
61		if ($this->resume_data === null)
62		{
63			$resume_data = $this->config_text->get('reparser_resume');
64			$this->resume_data = !empty($resume_data) ? unserialize($resume_data) : array();
65		}
66
67		return isset($this->resume_data[$name]) ? $this->resume_data[$name] : array();
68	}
69
70	/**
71	 * Updates the resume data in the database
72	 *
73	 * @param string	$name		Name of the reparser to which the resume data belongs
74	 * @param int		$min		Lowest record ID
75	 * @param int		$current	Current record ID
76	 * @param int		$size		Number of records to process at a time
77	 * @param bool		$update_db	True if the resume data should be written to the database, false if not. (default: true)
78	 */
79	public function update_resume_data($name, $min, $current, $size, $update_db = true)
80	{
81		// Prevent overwriting the old, stored array
82		if ($this->resume_data === null)
83		{
84			$this->get_resume_data('');
85		}
86
87		$this->resume_data[$name] = array(
88			'range-min'		=> $min,
89			'range-max'		=> $current,
90			'range-size'	=> $size,
91		);
92
93		if ($update_db)
94		{
95			$this->config_text->set('reparser_resume', serialize($this->resume_data));
96		}
97	}
98
99	/**
100	 * Sets the interval for a text_reparser cron task
101	 *
102	 * @param string	$name		Name of the reparser to schedule
103	 * @param int		$interval	Interval in seconds, 0 to disable the cron task
104	 */
105	public function schedule($name, $interval)
106	{
107		if (isset($this->reparsers[$name]) && isset($this->config[$name . '_cron_interval']))
108		{
109			$this->config->set($name . '_cron_interval', $interval);
110		}
111	}
112
113	/**
114	 * Sets the interval for all text_reparser cron tasks
115	 *
116	 * @param int	$interval	Interval in seconds, 0 to disable the cron task
117	 */
118	public function schedule_all($interval)
119	{
120		// This way we don't construct every registered reparser
121		$reparser_array = array_keys($this->reparsers->getArrayCopy());
122
123		foreach ($reparser_array as $reparser)
124		{
125			$this->schedule($reparser, $interval);
126		}
127	}
128
129	/**
130	 * Finds a reparser by name.
131	 *
132	 * If there is no reparser with the specified name, null is returned.
133	 *
134	 * @param string $name Name of the reparser to look up.
135	 * @return string A reparser service name, or null.
136	 */
137	public function find_reparser($name)
138	{
139		foreach ($this->reparsers as $service => $reparser)
140		{
141			if ($reparser->get_name() == $name)
142			{
143				return $service;
144			}
145		}
146		return null;
147	}
148}
149