From 9c15561a6b2c93111688962b94f8c55b874a04c0 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Sat, 11 Dec 2021 22:54:22 +0100 Subject: [PATCH] [ticket/16935] Refactor sphinx to use new clases and remove unused parts PHPBB3-16935 --- .../phpbb/search/backend/fulltext_sphinx.php | 6 +- phpBB/phpbb/search/backend/sphinx/config.php | 238 +----------------- .../search/backend/sphinx/config_comment.php | 47 ---- .../search/backend/sphinx/config_item.php | 41 +++ .../search/backend/sphinx/config_section.php | 80 ++---- .../search/backend/sphinx/config_variable.php | 35 +-- 6 files changed, 90 insertions(+), 357 deletions(-) delete mode 100644 phpBB/phpbb/search/backend/sphinx/config_comment.php create mode 100644 phpBB/phpbb/search/backend/sphinx/config_item.php diff --git a/phpBB/phpbb/search/backend/fulltext_sphinx.php b/phpBB/phpbb/search/backend/fulltext_sphinx.php index 6a2a603abf..e32ccfaea5 100644 --- a/phpBB/phpbb/search/backend/fulltext_sphinx.php +++ b/phpBB/phpbb/search/backend/fulltext_sphinx.php @@ -57,8 +57,8 @@ class fulltext_sphinx implements search_backend_interface protected $indexes; /** - * Sphinx searchd client object - * @var SphinxClient + * Sphinx search client object + * @var \SphinxClient */ protected $sphinx; @@ -857,7 +857,7 @@ class fulltext_sphinx implements search_backend_interface /* Now that we're sure everything was entered correctly, generate a config for the index. We use a config value fulltext_sphinx_id for this, as it should be unique. */ - $config_object = new \phpbb\search\sphinx\config($this->config_file_data); + $config_object = new \phpbb\search\backend\sphinx\config(); $config_data = array( 'source source_phpbb_' . $this->id . '_main' => array( array('type', $this->dbtype . ' # mysql or pgsql'), diff --git a/phpBB/phpbb/search/backend/sphinx/config.php b/phpBB/phpbb/search/backend/sphinx/config.php index 68a6c8684c..2dac3a8687 100644 --- a/phpBB/phpbb/search/backend/sphinx/config.php +++ b/phpBB/phpbb/search/backend/sphinx/config.php @@ -19,259 +19,47 @@ namespace phpbb\search\backend\sphinx; */ class config { - private $sections = array(); - - /** - * Constructor which optionally loads data from a variable - * - * @param string $config_data Variable containing the sphinx configuration data - * - * @access public - */ - function __construct($config_data) - { - if ($config_data != '') - { - $this->read($config_data); - } - } + /** @var array Sections array */ + private $sections = []; /** * Get a section object by its name * - * @param string $name The name of the section that shall be returned - * @return \phpbb\search\sphinx\config_section The section object or null if none was found - * - * @access public + * @param string $name The name of the section that shall be returned + * @return config_section|null The section object or null if none was found */ - function get_section_by_name($name) + public function get_section_by_name(string $name): ?config_section { for ($i = 0, $size = count($this->sections); $i < $size; $i++) { // Make sure this is really a section object and not a comment - if (($this->sections[$i] instanceof \phpbb\search\sphinx\config_section) && $this->sections[$i]->get_name() == $name) + if (($this->sections[$i] instanceof config_section) && $this->sections[$i]->get_name() == $name) { return $this->sections[$i]; } } + + return null; } /** * Appends a new empty section to the end of the config * - * @param string $name The name for the new section - * @return \phpbb\search\sphinx\config_section The newly created section object - * - * @access public + * @param string $name The name for the new section + * @return config_section The newly created section object */ - function add_section($name) + public function add_section(string $name): config_section { - $this->sections[] = new \phpbb\search\sphinx\config_section($name, ''); + $this->sections[] = new config_section($name, ''); return $this->sections[count($this->sections) - 1]; } - /** - * Reads the config file data - * - * @param string $config_data The config file data - * - * @access private - */ - function read($config_data) - { - $this->sections = array(); - - $section = null; - $found_opening_bracket = false; - $in_value = false; - - foreach ($config_data as $i => $line) - { - // If the value of a variable continues to the next line because the line - // break was escaped then we don't trim leading space but treat it as a part of the value - if ($in_value) - { - $line = rtrim($line); - } - else - { - $line = trim($line); - } - - // If we're not inside a section look for one - if (!$section) - { - // Add empty lines and comments as comment objects to the section list - // that way they're not deleted when reassembling the file from the sections - if (!$line || $line[0] == '#') - { - $this->sections[] = new \phpbb\search\sphinx\config_comment($config_file[$i]); - continue; - } - else - { - // Otherwise we scan the line reading the section name until we find - // an opening curly bracket or a comment - $section_name = ''; - $section_name_comment = ''; - $found_opening_bracket = false; - for ($j = 0, $length = strlen($line); $j < $length; $j++) - { - if ($line[$j] == '#') - { - $section_name_comment = substr($line, $j); - break; - } - - if ($found_opening_bracket) - { - continue; - } - - if ($line[$j] == '{') - { - $found_opening_bracket = true; - continue; - } - - $section_name .= $line[$j]; - } - - // And then we create the new section object - $section_name = trim($section_name); - $section = new \phpbb\search\sphinx\config_section($section_name, $section_name_comment); - } - } - else - { - // If we're looking for variables inside a section - $skip_first = false; - - // If we're not in a value continuing over the line feed - if (!$in_value) - { - // Then add empty lines and comments as comment objects to the variable list - // of this section so they're not deleted on reassembly - if (!$line || $line[0] == '#') - { - $section->add_variable(new \phpbb\search\sphinx\config_comment($config_file[$i])); - continue; - } - - // As long as we haven't yet actually found an opening bracket for this section - // we treat everything as comments so it's not deleted either - if (!$found_opening_bracket) - { - if ($line[0] == '{') - { - $skip_first = true; - $line = substr($line, 1); - $found_opening_bracket = true; - } - else - { - $section->add_variable(new \phpbb\search\sphinx\config_comment($config_file[$i])); - continue; - } - } - } - - // If we did not find a comment in this line or still add to the previous - // line's value ... - if ($line || $in_value) - { - if (!$in_value) - { - $name = ''; - $value = ''; - $comment = ''; - $found_assignment = false; - } - $in_value = false; - $end_section = false; - - /* ... then we should prase this line char by char: - - first there's the variable name - - then an equal sign - - the variable value - - possibly a backslash before the linefeed in this case we need to continue - parsing the value in the next line - - a # indicating that the rest of the line is a comment - - a closing curly bracket indicating the end of this section*/ - for ($j = 0, $length = strlen($line); $j < $length; $j++) - { - if ($line[$j] == '#') - { - $comment = substr($line, $j); - break; - } - else if ($line[$j] == '}') - { - $comment = substr($line, $j + 1); - $end_section = true; - break; - } - else if (!$found_assignment) - { - if ($line[$j] == '=') - { - $found_assignment = true; - } - else - { - $name .= $line[$j]; - } - } - else - { - if ($line[$j] == '\\' && $j == $length - 1) - { - $value .= "\n"; - $in_value = true; - // Go to the next line and keep processing the value in there - continue 2; - } - $value .= $line[$j]; - } - } - - // If a name and an equal sign were found then we have append a - // new variable object to the section - if ($name && $found_assignment) - { - $section->add_variable(new \phpbb\search\sphinx\config_variable(trim($name), trim($value), ($end_section) ? '' : $comment)); - continue; - } - - /* If we found a closing curly bracket this section has been completed - and we can append it to the section list and continue with looking for - the next section */ - if ($end_section) - { - $section->set_end_comment($comment); - $this->sections[] = $section; - $section = null; - continue; - } - } - - // If we did not find anything meaningful up to here, then just treat it - // as a comment - $comment = ($skip_first) ? "\t" . substr(ltrim($config_file[$i]), 1) : $config_file[$i]; - $section->add_variable(new \phpbb\search\sphinx\config_comment($comment)); - } - } - - } - /** * Returns the config data * * @return string $data The config data that is generated - * - * @access public */ - function get_data() + public function get_data(): string { $data = ""; foreach ($this->sections as $section) diff --git a/phpBB/phpbb/search/backend/sphinx/config_comment.php b/phpBB/phpbb/search/backend/sphinx/config_comment.php deleted file mode 100644 index b49b9f903f..0000000000 --- a/phpBB/phpbb/search/backend/sphinx/config_comment.php +++ /dev/null @@ -1,47 +0,0 @@ - -* @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\backend\sphinx; - -/** -* \phpbb\search\sphinx\config_comment -* Represents a comment inside the sphinx configuration -*/ -class config_comment -{ - private $exact_string; - - /** - * Create a new comment - * - * @param string $exact_string The content of the comment including newlines, leading whitespace, etc. - * - * @access public - */ - function __construct($exact_string) - { - $this->exact_string = $exact_string; - } - - /** - * Simply returns the comment as it was created - * - * @return string The exact string that was specified in the constructor - * - * @access public - */ - function to_string() - { - return $this->exact_string; - } -} diff --git a/phpBB/phpbb/search/backend/sphinx/config_item.php b/phpBB/phpbb/search/backend/sphinx/config_item.php new file mode 100644 index 0000000000..f8b39ad3c5 --- /dev/null +++ b/phpBB/phpbb/search/backend/sphinx/config_item.php @@ -0,0 +1,41 @@ + + * @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\backend\sphinx; + +/** + * \phpbb\search\backend\sphinx\config_item + * Represents a single config item inside the sphinx configuration + */ +abstract class config_item +{ + /** @var string Item name */ + protected $name = ''; + + /** + * Getter for the item's name + * + * @return string The item object's name + */ + public function get_name(): string + { + return $this->name; + } + + /** + * Return string representation of config item + * + * @return string String representation of config item + */ + abstract public function to_string(): string; +} diff --git a/phpBB/phpbb/search/backend/sphinx/config_section.php b/phpBB/phpbb/search/backend/sphinx/config_section.php index 56409809f4..a3f522209f 100644 --- a/phpBB/phpbb/search/backend/sphinx/config_section.php +++ b/phpBB/phpbb/search/backend/sphinx/config_section.php @@ -14,15 +14,19 @@ namespace phpbb\search\backend\sphinx; /** -* \phpbb\search\sphinx\config_section +* \phpbb\search\backend\sphinx\config_section * Represents a single section inside the sphinx configuration */ -class config_section +class config_section extends config_item { - private $name; + /** @var string Section comment */ private $comment; + + /** @var string Section end comment */ private $end_comment; - private $variables = array(); + + /** @var array Section variables array */ + private $variables = []; /** * Construct a new section @@ -30,86 +34,57 @@ class config_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) + public function __construct(string $name, string $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) + public function set_end_comment(string $end_comment): void { $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 + * @param string $name The name of the variable that shall be returned * - * @access public + * @return config_variable|null The first variable object from this section with the + * given name or null if none was found */ - function get_variable_by_name($name) + public function get_variable_by_name(string $name): ?config_variable { 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) + if ($this->variables[$i]->get_name() == $name) { return $this->variables[$i]; } } + + return null; } /** * 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) + public function delete_variables_by_name(string $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) + if ($this->variables[$i]->get_name() == $name) { array_splice($this->variables, $i, 1); $i--; @@ -118,17 +93,16 @@ class config_section } /** - * Create a new variable object and append it to the variable list of this section + * Create a new variable object and append it to the variables 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 + * @param string $name The name for the new variable + * @param string $value The value for the new variable * - * @access public + * @return config_variable Variable object that was created */ - function create_variable($name, $value) + public function create_variable(string $name, string $value): config_variable { - $this->variables[] = new \phpbb\search\sphinx\config_variable($name, $value, ''); + $this->variables[] = new config_variable($name, $value); return $this->variables[count($this->variables) - 1]; } @@ -136,10 +110,8 @@ class config_section * 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() + public function to_string(): string { $content = $this->name . ' ' . $this->comment . "\n{\n"; diff --git a/phpBB/phpbb/search/backend/sphinx/config_variable.php b/phpBB/phpbb/search/backend/sphinx/config_variable.php index 8ff237d676..fe9b2bd5a4 100644 --- a/phpBB/phpbb/search/backend/sphinx/config_variable.php +++ b/phpBB/phpbb/search/backend/sphinx/config_variable.php @@ -14,12 +14,11 @@ namespace phpbb\search\backend\sphinx; /** -* \phpbb\search\sphinx\config_variable +* \phpbb\search\backend\sphinx\config_variable * Represents a single variable inside the sphinx configuration */ -class config_variable +class config_variable extends config_item { - private $name; private $value; private $comment; @@ -30,49 +29,29 @@ class config_variable * @param string $value Value of the variable * @param string $comment Optional comment after the variable in the * config file - * - * @access public */ - function __construct($name, $value, $comment) + public function __construct(string $name, string $value, string $comment = '') { $this->name = $name; $this->value = $value; $this->comment = $comment; } - /** - * Getter for the variable's name - * - * @return string The variable object's name - * - * @access public - */ - function get_name() - { - return $this->name; - } - /** * Allows changing the variable's value * * @param string $value New value for this variable - * - * @access public */ - function set_value($value) + public function set_value(string $value): void { $this->value = $value; } /** - * Turns this object into a string readable by sphinx - * - * @return string Config data in textual form - * - * @access public + * {@inheritDoc} */ - function to_string() + public function to_string(): string { - return "\t" . $this->name . ' = ' . str_replace("\n", " \\\n", $this->value) . ' ' . $this->comment . "\n"; + return "\t" . $this->name . ' = ' . str_replace("\n", " \\\n", $this->value) . ($this->comment ? ' ' . $this->comment : '') . "\n"; } }