aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/phpbb/search/sphinx/config_section.php
blob: 2fc8b2da1780aad51ca182d2baf005db188e1247 (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
149
150
151
152
153
154
155
156
157
158
159
160
<?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\search\sphinx;

/**
* \phpbb\search\sphinx\config_section
* Represents a single section inside the sphinx configuration
*/
class config_section
{
	private $name;
	private $comment;
	private $end_comment;
	private $variables = array();

	/**
	* Construct a new section
	*
	* @param	string	$name		Name of the section
	* @param	string	$comment	Comment that should be appended after the name in the
	*								textual format.
	*
	* @access	public
	*/
	function __construct($name, $comment)
	{
		$this->name = $name;
		$this->comment = $comment;
		$this->end_comment = '';
	}

	/**
	* Add a variable object to the list of variables in this section
	*
	* @param	\phpbb\search\sphinx\config_variable	$variable	The variable object
	*
	* @access	public
	*/
	function add_variable($variable)
	{
		$this->variables[] = $variable;
	}

	/**
	* Adds a comment after the closing bracket in the textual representation
	*
	* @param	string	$end_comment
	*
	* @access	public
	*/
	function set_end_comment($end_comment)
	{
		$this->end_comment = $end_comment;
	}

	/**
	* Getter for the name of this section
	*
	* @return	string	Section's name
	*
	* @access	public
	*/
	function get_name()
	{
		return $this->name;
	}

	/**
	* Get a variable object by its name
	*
	* @param	string 								$name	The name of the variable that shall be returned
	* @return	\phpbb\search\sphinx\config_section			The first variable object from this section with the
	*														given name or null if none was found
	*
	* @access	public
	*/
	function get_variable_by_name($name)
	{
		for ($i = 0, $size = count($this->variables); $i < $size; $i++)
		{
			// Make sure this is a variable object and not a comment
			if (($this->variables[$i] instanceof \phpbb\search\sphinx\config_variable) && $this->variables[$i]->get_name() == $name)
			{
				return $this->variables[$i];
			}
		}
	}

	/**
	* Deletes all variables with the given name
	*
	* @param	string	$name	The name of the variable objects that are supposed to be removed
	*
	* @access	public
	*/
	function delete_variables_by_name($name)
	{
		for ($i = 0, $size = count($this->variables); $i < $size; $i++)
		{
			// Make sure this is a variable object and not a comment
			if (($this->variables[$i] instanceof \phpbb\search\sphinx\config_variable) && $this->variables[$i]->get_name() == $name)
			{
				array_splice($this->variables, $i, 1);
				$i--;
			}
		}
	}

	/**
	* Create a new variable object and append it to the variable list of this section
	*
	* @param	string								$name	The name for the new variable
	* @param	string								$value	The value for the new variable
	* @return	\phpbb\search\sphinx\config_variable			Variable object that was created
	*
	* @access	public
	*/
	function create_variable($name, $value)
	{
		$this->variables[] = new \phpbb\search\sphinx\config_variable($name, $value, '');
		return $this->variables[count($this->variables) - 1];
	}

	/**
	* Turns this object into a string which can be written to a config file
	*
	* @return	string	Config data in textual form, parsable for sphinx
	*
	* @access	public
	*/
	function to_string()
	{
		$content = $this->name . ' ' . $this->comment . "\n{\n";

		// Make sure we don't get too many newlines after the opening bracket
		while (trim($this->variables[0]->to_string()) == '')
		{
			array_shift($this->variables);
		}

		foreach ($this->variables as $variable)
		{
			$content .= $variable->to_string();
		}
		$content .= '}' . $this->end_comment . "\n";

		return $content;
	}
}