aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/phpbb/textreparser/manager.php
blob: 7ca65d708d68da59c8277a90031f407bfc59c902 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
/**
 *
 * This file is part of the phpBB Forum Software package.
 *
 * @copyright (c) phpBB Limited <https://www.phpbb.com>
 * @license GNU General Public License, version 2 (GPL-2.0)
 *
 * For full copyright and license information, please see
 * the docs/CREDITS.txt file.
 *
 */

namespace phpbb\textreparser;

class manager
{
	/**
	 * @var \phpbb\config\config
	 */
	protected $config;

	/**
	 * @var \phpbb\config\db_text
	 */
	protected $config_text;

	/**
	 * @var \phpbb\di\service_collection
	 */
	protected $reparsers;

	/**
	 * @var array
	 */
	protected $resume_data;

	/**
	 * Constructor
	 *
	 * @param \phpbb\config\config			$config
	 * @param \phpbb\config\db_text			$config_text
	 * @param \phpbb\di\service_collection	$reparsers
	 */
	public function __construct(\phpbb\config\config $config, \phpbb\config\db_text $config_text, \phpbb\di\service_collection $reparsers)
	{
		$this->config = $config;
		$this->config_text = $config_text;
		$this->reparsers = $reparsers;
	}

	/**
	 * Loads resume data from the database
	 *
	 * @param string	$name	Name of the reparser to which the resume data belongs
	 *
	 * @return array
	 */
	public function get_resume_data($name)
	{
		if ($this->resume_data === null)
		{
			$resume_data = $this->config_text->get('reparser_resume');
			$this->resume_data = !empty($resume_data) ? unserialize($resume_data) : array();
		}

		return isset($this->resume_data[$name]) ? $this->resume_data[$name] : array();
	}

	/**
	 * Updates the resume data in the database
	 *
	 * @param string	$name		Name of the reparser to which the resume data belongs
	 * @param int		$min		Lowest record ID
	 * @param int		$current	Current record ID
	 * @param int		$size		Number of records to process at a time
	 * @param bool		$update_db	True if the resume data should be written to the database, false if not. (default: true)
	 */
	public function update_resume_data($name, $min, $current, $size, $update_db = true)
	{
		// Prevent overwriting the old, stored array
		if ($this->resume_data === null)
		{
			$this->get_resume_data('');
		}

		$this->resume_data[$name] = array(
			'range-min'		=> $min,
			'range-max'		=> $current,
			'range-size'	=> $size,
		);

		if ($update_db)
		{
			$this->config_text->set('reparser_resume', serialize($this->resume_data));
		}
	}

	/**
	 * Sets the interval for a text_reparser cron task
	 *
	 * @param string	$name		Name of the reparser to schedule
	 * @param int		$interval	Interval in seconds, 0 to disable the cron task
	 */
	public function schedule($name, $interval)
	{
		if (isset($this->reparsers[$name]) && isset($this->config[$name . '_cron_interval']))
		{
			$this->config->set($name . '_cron_interval', $interval);
		}
	}

	/**
	 * Sets the interval for all text_reparser cron tasks
	 *
	 * @param int	$interval	Interval in seconds, 0 to disable the cron task
	 */
	public function schedule_all($interval)
	{
		// This way we don't construct every registered reparser
		$reparser_array = array_keys($this->reparsers->getArrayCopy());

		foreach ($reparser_array as $reparser)
		{
			$this->schedule($reparser, $interval);
		}
	}

	/**
	 * Finds a reparser by name.
	 *
	 * If there is no reparser with the specified name, null is returned.
	 *
	 * @param string $name Name of the reparser to look up.
	 * @return string A reparser service name, or null.
	 */
	public function find_reparser($name)
	{
		foreach ($this->reparsers as $service => $reparser)
		{
			if ($reparser->get_name() == $name)
			{
				return $service;
			}
		}
		return null;
	}
}