From 986af43f37342953bff548630aa33904c21234f4 Mon Sep 17 00:00:00 2001 From: JoshyPHP Date: Fri, 1 May 2015 08:47:01 +0200 Subject: [ticket/13803] Added plugins PHPBB3-13803 --- phpBB/phpbb/textreparser/row_based_plugin.php | 128 ++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 phpBB/phpbb/textreparser/row_based_plugin.php (limited to 'phpBB/phpbb/textreparser/row_based_plugin.php') diff --git a/phpBB/phpbb/textreparser/row_based_plugin.php b/phpBB/phpbb/textreparser/row_based_plugin.php new file mode 100644 index 0000000000..2317c79e4f --- /dev/null +++ b/phpBB/phpbb/textreparser/row_based_plugin.php @@ -0,0 +1,128 @@ + +* @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; + +abstract class row_based_plugin extends base +{ + /** + * @var \phpbb\db\driver\driver_interface + */ + protected $db; + + /** + * Constructor + * + * @param \phpbb\db\driver\driver_interface $db Database connection + */ + public function __construct(\phpbb\db\driver\driver_interface $db) + { + $this->db = $db; + } + + /** + * Return the name of the column that correspond to each field + * + * @return array + */ + abstract protected function get_columns(); + + /** + * Return the name of the table used by this plugin + * + * @return string + */ + abstract protected function get_table_name(); + + /** + * {@inheritdoc} + */ + public function get_max_id() + { + $columns = $this->get_columns(); + + $sql = 'SELECT MAX(' . $columns['id'] . ' AS max_id FROM ' . $this->get_table_name(); + $result = $this->db->sql_query($sql); + $max_id = (int) $this->db->sql_fetchfield('max_id'); + $this->db->sql_freeresult($result); + + return $max_id; + } + + /** + * {@inheritdoc} + */ + protected function get_records($min_id, $max_id) + { + $result = $this->db->sql_query($this->get_records_query($min_id, $max_id)); + while ($row = $this->db->sql_fetchrow($result)) + { + if (!isset($row['enable_bbcode'], $row['enable_smilies'], $row['enable_magic_url'])) + { + // Those fields are not saved to the database, we need to guess their original value + $row += array( + 'enable_bbcode' => !empty($row['bbcode_uid']), + 'enable_smilies' => (strpos($row['text'], '') !== false) + ); + } + $records[] = $row; + } + $this->db->sql_freeresult($result); + + return $records; + } + + /** + * Generate the query that retrieves all records for given range + * + * @param integer $min_id Lower bound + * @param integer $max_id Upper bound + * @return string SQL query + */ + protected function get_records_query($min_id, $max_id) + { + $columns = $this->get_columns(); + $fields = array(); + foreach ($columns as $field_name => $column_name) + { + if ($column_name === $field_name) + { + $fields[] = $column_name; + } + else + { + $fields[] = $column_name . ' AS ' . $field_name; + } + } + + $sql = 'SELECT ' . implode(', ', $fields) . ' + FROM ' . $this->get_table_name() . ' + WHERE ' . $columns['id'] . ' BETWEEN ' . $min_id . ' AND ' . $max_id; + + return $sql; + } + + /** + * {@inheritdoc} + */ + protected function save_record(array $record) + { + $columns = $this->get_columns(); + + $sql = 'UPDATE ' . $this->get_table_name() . ' + SET ' . $columns['text'] . " = '" . $this->db->sql_escape($record['text']) . "' + WHERE " . $columns['id'] . ' = ' . $record['id']; + $this->db->sql_query($sql); + } +} -- cgit v1.2.1 From b5911281ae175340817345e63ddbfaf43abb3cec Mon Sep 17 00:00:00 2001 From: JoshyPHP Date: Fri, 1 May 2015 19:21:01 +0200 Subject: [ticket/13803] Added tests, fixed param order in generate_text_for_storage() PHPBB3-13803 --- phpBB/phpbb/textreparser/row_based_plugin.php | 34 +++++++++++++++++++-------- 1 file changed, 24 insertions(+), 10 deletions(-) (limited to 'phpBB/phpbb/textreparser/row_based_plugin.php') diff --git a/phpBB/phpbb/textreparser/row_based_plugin.php b/phpBB/phpbb/textreparser/row_based_plugin.php index 2317c79e4f..b946d6532b 100644 --- a/phpBB/phpbb/textreparser/row_based_plugin.php +++ b/phpBB/phpbb/textreparser/row_based_plugin.php @@ -44,6 +44,29 @@ abstract class row_based_plugin extends base */ abstract protected function get_table_name(); + /** + * Add fields to given row, if applicable + * + * The enable_* fields are not always saved to the database. Sometimes we need to guess their + * original value based on the text content or possibly other fields + * + * @param array $row Original row + * @return array Complete row + */ + protected function add_missing_fields(array $row) + { + if (!isset($row['enable_bbcode'], $row['enable_smilies'], $row['enable_magic_url'])) + { + $row += array( + 'enable_bbcode' => !empty($row['bbcode_uid']), + 'enable_smilies' => (strpos($row['text'], '') !== false), + ); + } + + return $row; + } + /** * {@inheritdoc} */ @@ -67,16 +90,7 @@ abstract class row_based_plugin extends base $result = $this->db->sql_query($this->get_records_query($min_id, $max_id)); while ($row = $this->db->sql_fetchrow($result)) { - if (!isset($row['enable_bbcode'], $row['enable_smilies'], $row['enable_magic_url'])) - { - // Those fields are not saved to the database, we need to guess their original value - $row += array( - 'enable_bbcode' => !empty($row['bbcode_uid']), - 'enable_smilies' => (strpos($row['text'], '') !== false) - ); - } - $records[] = $row; + $records[] = $this->add_missing_fields($row); } $this->db->sql_freeresult($result); -- cgit v1.2.1 From 459f1d4c1f26658c70d29ac7c4e3f3389a973a59 Mon Sep 17 00:00:00 2001 From: JoshyPHP Date: Fri, 1 May 2015 20:05:15 +0200 Subject: [ticket/13803] Refactored test PHPBB3-13803 --- phpBB/phpbb/textreparser/row_based_plugin.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'phpBB/phpbb/textreparser/row_based_plugin.php') diff --git a/phpBB/phpbb/textreparser/row_based_plugin.php b/phpBB/phpbb/textreparser/row_based_plugin.php index b946d6532b..2be0b68411 100644 --- a/phpBB/phpbb/textreparser/row_based_plugin.php +++ b/phpBB/phpbb/textreparser/row_based_plugin.php @@ -35,14 +35,14 @@ abstract class row_based_plugin extends base * * @return array */ - abstract protected function get_columns(); + abstract public function get_columns(); /** * Return the name of the table used by this plugin * * @return string */ - abstract protected function get_table_name(); + abstract public function get_table_name(); /** * Add fields to given row, if applicable -- cgit v1.2.1 From ea445ffa4776b7ce0b1d13485f113c7e1ec28af0 Mon Sep 17 00:00:00 2001 From: JoshyPHP Date: Sat, 2 May 2015 01:08:32 +0200 Subject: [ticket/13803] Added methods to detect whether a given feature is in use They test whether a given BBCode was enabled and has been used in a text, or smilies, or magic URLs. PHPBB3-13803 --- phpBB/phpbb/textreparser/row_based_plugin.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'phpBB/phpbb/textreparser/row_based_plugin.php') diff --git a/phpBB/phpbb/textreparser/row_based_plugin.php b/phpBB/phpbb/textreparser/row_based_plugin.php index 2be0b68411..80525a404e 100644 --- a/phpBB/phpbb/textreparser/row_based_plugin.php +++ b/phpBB/phpbb/textreparser/row_based_plugin.php @@ -59,11 +59,20 @@ abstract class row_based_plugin extends base { $row += array( 'enable_bbcode' => !empty($row['bbcode_uid']), - 'enable_smilies' => (strpos($row['text'], '') !== false), + 'enable_smilies' => $this->guess_smilies($row), + 'enable_magic_url' => $this->guess_magic_url($row), ); } + // Those BBCodes are disabled based on context and user permissions and that value is never + // stored in the database. Here we test whether they were used in the original text. + $bbcodes = array('flash', 'img', 'quote', 'url'); + foreach ($bbcodes as $bbcode) + { + $field_name = 'enable_' . $bbcode; + $row[$field_name] = $this->guess_bbcode($row, $bbcode); + } + return $row; } -- cgit v1.2.1 From 9bf0f794b5876b10491c91548f1a92bc0dff7400 Mon Sep 17 00:00:00 2001 From: JoshyPHP Date: Sat, 2 May 2015 02:55:45 +0200 Subject: [ticket/13803] Added pm_text tests PHPBB3-13803 --- phpBB/phpbb/textreparser/row_based_plugin.php | 32 --------------------------- 1 file changed, 32 deletions(-) (limited to 'phpBB/phpbb/textreparser/row_based_plugin.php') diff --git a/phpBB/phpbb/textreparser/row_based_plugin.php b/phpBB/phpbb/textreparser/row_based_plugin.php index 80525a404e..e39ec4d5d3 100644 --- a/phpBB/phpbb/textreparser/row_based_plugin.php +++ b/phpBB/phpbb/textreparser/row_based_plugin.php @@ -44,38 +44,6 @@ abstract class row_based_plugin extends base */ abstract public function get_table_name(); - /** - * Add fields to given row, if applicable - * - * The enable_* fields are not always saved to the database. Sometimes we need to guess their - * original value based on the text content or possibly other fields - * - * @param array $row Original row - * @return array Complete row - */ - protected function add_missing_fields(array $row) - { - if (!isset($row['enable_bbcode'], $row['enable_smilies'], $row['enable_magic_url'])) - { - $row += array( - 'enable_bbcode' => !empty($row['bbcode_uid']), - 'enable_smilies' => $this->guess_smilies($row), - 'enable_magic_url' => $this->guess_magic_url($row), - ); - } - - // Those BBCodes are disabled based on context and user permissions and that value is never - // stored in the database. Here we test whether they were used in the original text. - $bbcodes = array('flash', 'img', 'quote', 'url'); - foreach ($bbcodes as $bbcode) - { - $field_name = 'enable_' . $bbcode; - $row[$field_name] = $this->guess_bbcode($row, $bbcode); - } - - return $row; - } - /** * {@inheritdoc} */ -- cgit v1.2.1 From ae6ad754a430309796e1963684b59c01739ba4dd Mon Sep 17 00:00:00 2001 From: JoshyPHP Date: Sat, 9 May 2015 18:54:57 +0200 Subject: [ticket/13803] Moved the add_missing_fields() call to the base class PHPBB3-13803 --- phpBB/phpbb/textreparser/row_based_plugin.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'phpBB/phpbb/textreparser/row_based_plugin.php') diff --git a/phpBB/phpbb/textreparser/row_based_plugin.php b/phpBB/phpbb/textreparser/row_based_plugin.php index e39ec4d5d3..6b63ffeeaf 100644 --- a/phpBB/phpbb/textreparser/row_based_plugin.php +++ b/phpBB/phpbb/textreparser/row_based_plugin.php @@ -65,10 +65,7 @@ abstract class row_based_plugin extends base protected function get_records($min_id, $max_id) { $result = $this->db->sql_query($this->get_records_query($min_id, $max_id)); - while ($row = $this->db->sql_fetchrow($result)) - { - $records[] = $this->add_missing_fields($row); - } + $records = $this->db->sql_fetchrowset($result); $this->db->sql_freeresult($result); return $records; -- cgit v1.2.1 From 7a8ac4bb714299d2544ec215c24018fe07caa0ec Mon Sep 17 00:00:00 2001 From: JoshyPHP Date: Sat, 9 May 2015 19:06:48 +0200 Subject: [ticket/13803] Added get_max_id() tests PHPBB3-13803 --- phpBB/phpbb/textreparser/row_based_plugin.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'phpBB/phpbb/textreparser/row_based_plugin.php') diff --git a/phpBB/phpbb/textreparser/row_based_plugin.php b/phpBB/phpbb/textreparser/row_based_plugin.php index 6b63ffeeaf..05a6e19553 100644 --- a/phpBB/phpbb/textreparser/row_based_plugin.php +++ b/phpBB/phpbb/textreparser/row_based_plugin.php @@ -51,7 +51,7 @@ abstract class row_based_plugin extends base { $columns = $this->get_columns(); - $sql = 'SELECT MAX(' . $columns['id'] . ' AS max_id FROM ' . $this->get_table_name(); + $sql = 'SELECT MAX(' . $columns['id'] . ') AS max_id FROM ' . $this->get_table_name(); $result = $this->db->sql_query($sql); $max_id = (int) $this->db->sql_fetchfield('max_id'); $this->db->sql_freeresult($result); -- cgit v1.2.1 From 75eb283f8d7a7afcd3945d0d28bd0f58aa4f0cd6 Mon Sep 17 00:00:00 2001 From: JoshyPHP Date: Tue, 19 May 2015 09:48:29 +0200 Subject: [ticket/13803] Renamed methods PHPBB3-13803 --- phpBB/phpbb/textreparser/row_based_plugin.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'phpBB/phpbb/textreparser/row_based_plugin.php') diff --git a/phpBB/phpbb/textreparser/row_based_plugin.php b/phpBB/phpbb/textreparser/row_based_plugin.php index 05a6e19553..d3ca334591 100644 --- a/phpBB/phpbb/textreparser/row_based_plugin.php +++ b/phpBB/phpbb/textreparser/row_based_plugin.php @@ -62,9 +62,10 @@ abstract class row_based_plugin extends base /** * {@inheritdoc} */ - protected function get_records($min_id, $max_id) + protected function get_records_by_range($min_id, $max_id) { - $result = $this->db->sql_query($this->get_records_query($min_id, $max_id)); + $sql = $this->get_records_by_range_query($min_id, $max_id); + $result = $this->db->sql_query($sql); $records = $this->db->sql_fetchrowset($result); $this->db->sql_freeresult($result); @@ -78,7 +79,7 @@ abstract class row_based_plugin extends base * @param integer $max_id Upper bound * @return string SQL query */ - protected function get_records_query($min_id, $max_id) + protected function get_records_by_range_query($min_id, $max_id) { $columns = $this->get_columns(); $fields = array(); -- cgit v1.2.1