diff options
-rw-r--r-- | phpBB/language/en/cli.php | 1 | ||||
-rw-r--r-- | phpBB/phpbb/console/command/reparser/reparse.php | 14 | ||||
-rw-r--r-- | phpBB/phpbb/textreparser/base.php | 27 | ||||
-rw-r--r-- | tests/text_reparser/plugins/contact_admin_info_test.php | 33 | ||||
-rw-r--r-- | tests/text_reparser/plugins/poll_option_test.php | 33 | ||||
-rw-r--r-- | tests/text_reparser/plugins/test_row_based_plugin.php | 41 |
6 files changed, 116 insertions, 33 deletions
diff --git a/phpBB/language/en/cli.php b/phpBB/language/en/cli.php index 9eca60fb68..d45c52ac5d 100644 --- a/phpBB/language/en/cli.php +++ b/phpBB/language/en/cli.php @@ -64,6 +64,7 @@ $lang = array_merge($lang, array( 'CLI_DESCRIPTION_REPARSER_LIST' => 'Lists the types of text that can be reparsed.', 'CLI_DESCRIPTION_REPARSER_REPARSE' => 'Reparses stored text with the current text_formatter services.', 'CLI_DESCRIPTION_REPARSER_REPARSE_ARG_1' => 'Type of text to reparse. Leave blank to reparse everything.', + 'CLI_DESCRIPTION_REPARSER_REPARSE_OPT_DRY_RUN' => 'Do not save any changes; just print what would happen', 'CLI_DESCRIPTION_REPARSER_REPARSE_OPT_RANGE_MIN' => 'Lowest record ID to process', 'CLI_DESCRIPTION_REPARSER_REPARSE_OPT_RANGE_MAX' => 'Highest record ID to process', 'CLI_DESCRIPTION_REPARSER_REPARSE_OPT_RANGE_SIZE' => 'Approximate number of records to process at a time', diff --git a/phpBB/phpbb/console/command/reparser/reparse.php b/phpBB/phpbb/console/command/reparser/reparse.php index 52075dd0ac..151e196358 100644 --- a/phpBB/phpbb/console/command/reparser/reparse.php +++ b/phpBB/phpbb/console/command/reparser/reparse.php @@ -57,6 +57,12 @@ class reparse extends \phpbb\console\command\command ->setDescription($this->user->lang('CLI_DESCRIPTION_REPARSER_REPARSE')) ->addArgument('reparser-name', InputArgument::OPTIONAL, $this->user->lang('CLI_DESCRIPTION_REPARSER_REPARSE_ARG_1')) ->addOption( + 'dry-run', + null, + InputOption::VALUE_NONE, + $this->user->lang('CLI_DESCRIPTION_REPARSER_REPARSE_OPT_DRY_RUN') + ) + ->addOption( 'range-min', null, InputOption::VALUE_REQUIRED, @@ -124,6 +130,14 @@ class reparse extends \phpbb\console\command\command protected function reparse(InputInterface $input, OutputInterface $output, $name) { $reparser = $this->reparsers[$name]; + if ($input->getOption('dry-run')) + { + $reparser->disable_save(); + } + else + { + $reparser->enable_save(); + } // Start at range-max if specified or at the highest ID otherwise $max = (is_null($input->getOption('range-max'))) ? $reparser->get_max_id() : $input->getOption('range-max'); diff --git a/phpBB/phpbb/textreparser/base.php b/phpBB/phpbb/textreparser/base.php index ed6c2376c7..3e5ee248a1 100644 --- a/phpBB/phpbb/textreparser/base.php +++ b/phpBB/phpbb/textreparser/base.php @@ -16,6 +16,11 @@ namespace phpbb\textreparser; abstract class base implements reparser_interface { /** + * @var bool Whether to save changes to the database + */ + protected $save_changes = true; + + /** * {@inheritdoc} */ abstract public function get_max_id(); @@ -85,6 +90,22 @@ abstract class base implements reparser_interface } /** + * Disable saving changes to the database + */ + public function disable_save() + { + $this->save_changes = false; + } + + /** + * Enable saving changes to the database + */ + public function enable_save() + { + $this->save_changes = true; + } + + /** * Guess whether given BBCode is in use in given record * * @param array $record @@ -181,7 +202,7 @@ abstract class base implements reparser_interface /** * Reparse given record * - * @param array $record Associative array containing the record's data + * @param array $record Associative array containing the record's data */ protected function reparse_record(array $record) { @@ -212,8 +233,8 @@ abstract class base implements reparser_interface $unparsed['enable_url_bbcode'] ); - // Save the new text if it has changed - if ($text !== $record['text']) + // Save the new text if it has changed and it's not a dry run + if ($text !== $record['text'] && $this->save_changes) { $record['text'] = $text; $this->save_record($record); diff --git a/tests/text_reparser/plugins/contact_admin_info_test.php b/tests/text_reparser/plugins/contact_admin_info_test.php index e577d2fd3d..1dc03834b6 100644 --- a/tests/text_reparser/plugins/contact_admin_info_test.php +++ b/tests/text_reparser/plugins/contact_admin_info_test.php @@ -28,6 +28,18 @@ class phpbb_textreparser_contact_admin_info_test extends phpbb_database_test_cas return new \phpbb\textreparser\plugins\contact_admin_info(new \phpbb\config\db_text($this->db, CONFIG_TEXT_TABLE)); } + protected function get_rows() + { + $sql = 'SELECT config_name, config_value + FROM ' . CONFIG_TEXT_TABLE . ' + ORDER BY config_name'; + $result = $this->db->sql_query($sql); + $rows = $this->db->sql_fetchrowset($result); + $this->db->sql_freeresult($result); + + return $rows; + } + public function setUp() { global $config; @@ -46,18 +58,21 @@ class phpbb_textreparser_contact_admin_info_test extends phpbb_database_test_cas $this->assertEquals(1, $reparser->get_max_id()); } - public function testReparse() + public function test_dry_run() { + $old_rows = $this->get_rows(); $reparser = $this->get_reparser(); + $reparser->disable_save(); $reparser->reparse_range(1, 1); + $new_rows = $this->get_rows(); + $this->assertEquals($old_rows, $new_rows); + } - $sql = 'SELECT config_name, config_value - FROM ' . CONFIG_TEXT_TABLE . ' - ORDER BY config_name'; - $result = $this->db->sql_query($sql); - $rows = $this->db->sql_fetchrowset($result); - $this->db->sql_freeresult($result); - + public function test_reparse() + { + $reparser = $this->get_reparser(); + $reparser->enable_save(); + $reparser->reparse_range(1, 1); $expected = array( array( 'config_name' => 'contact_admin_info', @@ -76,6 +91,6 @@ class phpbb_textreparser_contact_admin_info_test extends phpbb_database_test_cas 'config_value' => '1a2hbwf5', ), ); - $this->assertEquals($expected, $rows); + $this->assertEquals($expected, $this->get_rows()); } } diff --git a/tests/text_reparser/plugins/poll_option_test.php b/tests/text_reparser/plugins/poll_option_test.php index acabda2146..dfa3a030ed 100644 --- a/tests/text_reparser/plugins/poll_option_test.php +++ b/tests/text_reparser/plugins/poll_option_test.php @@ -28,6 +28,18 @@ class phpbb_textreparser_poll_option_test extends phpbb_database_test_case return new \phpbb\textreparser\plugins\poll_option($this->db); } + protected function get_rows() + { + $sql = 'SELECT topic_id, poll_option_id, poll_option_text + FROM ' . POLL_OPTIONS_TABLE . ' + ORDER BY topic_id, poll_option_id'; + $result = $this->db->sql_query($sql); + $rows = $this->db->sql_fetchrowset($result); + $this->db->sql_freeresult($result); + + return $rows; + } + public function setUp() { global $config; @@ -46,18 +58,21 @@ class phpbb_textreparser_poll_option_test extends phpbb_database_test_case $this->assertEquals(123, $reparser->get_max_id()); } + public function test_dry_run() + { + $old_rows = $this->get_rows(); + $reparser = $this->get_reparser(); + $reparser->disable_save(); + $reparser->reparse_range(1, 1); + $new_rows = $this->get_rows(); + $this->assertEquals($old_rows, $new_rows); + } + public function testReparse() { $reparser = $this->get_reparser(); + $reparser->enable_save(); $reparser->reparse_range(2, 13); - - $sql = 'SELECT topic_id, poll_option_id, poll_option_text - FROM ' . POLL_OPTIONS_TABLE . ' - ORDER BY topic_id, poll_option_id'; - $result = $this->db->sql_query($sql); - $rows = $this->db->sql_fetchrowset($result); - $this->db->sql_freeresult($result); - $expected = array( array( 'topic_id' => 1, @@ -110,6 +125,6 @@ class phpbb_textreparser_poll_option_test extends phpbb_database_test_case 'poll_option_text' => 'This row should be [b:abcd1234]ignored[/b:abcd1234]', ), ); - $this->assertEquals($expected, $rows); + $this->assertEquals($expected, $this->get_rows()); } } diff --git a/tests/text_reparser/plugins/test_row_based_plugin.php b/tests/text_reparser/plugins/test_row_based_plugin.php index befcb48bda..bbae44c8e0 100644 --- a/tests/text_reparser/plugins/test_row_based_plugin.php +++ b/tests/text_reparser/plugins/test_row_based_plugin.php @@ -20,6 +20,21 @@ abstract class phpbb_textreparser_test_row_based_plugin extends phpbb_database_t abstract protected function get_reparser(); + protected function get_rows(array $ids) + { + $reparser = $this->get_reparser(); + $columns = $reparser->get_columns(); + $sql = 'SELECT ' . $columns['id'] . ' AS id, ' . $columns['text'] . ' AS text + FROM ' . $reparser->get_table_name() . ' + WHERE ' . $this->db->sql_in_set($columns['id'], $ids) . ' + ORDER BY id'; + $result = $this->db->sql_query($sql); + $rows = $this->db->sql_fetchrowset($result); + $this->db->sql_freeresult($result); + + return $rows; + } + public function setUp() { global $config; @@ -38,10 +53,20 @@ abstract class phpbb_textreparser_test_row_based_plugin extends phpbb_database_t $this->assertEquals(1000, $reparser->get_max_id()); } + public function test_dry_run() + { + $old_rows = $this->get_rows(array(1)); + $reparser = $this->get_reparser(); + $reparser->disable_save(); + $reparser->reparse_range(1, 1); + $new_rows = $this->get_rows(array(1)); + $this->assertEquals($old_rows, $new_rows); + } + /** - * @dataProvider getReparseTests + * @dataProvider get_reparse_tests */ - public function testReparse($min_id, $max_id, $expected) + public function test_reparse($min_id, $max_id, $expected) { $reparser = $this->get_reparser(); $reparser->reparse_range($min_id, $max_id); @@ -52,18 +77,10 @@ abstract class phpbb_textreparser_test_row_based_plugin extends phpbb_database_t $ids[] = $row['id']; } - $columns = $reparser->get_columns(); - $sql = 'SELECT ' . $columns['id'] . ' AS id, ' . $columns['text'] . ' AS text - FROM ' . $reparser->get_table_name() . ' - WHERE ' . $this->db->sql_in_set($columns['id'], $ids) . ' - ORDER BY id'; - $result = $this->db->sql_query($sql); - $rows = $this->db->sql_fetchrowset($result); - $this->db->sql_freeresult($result); - $this->assertEquals($expected, $rows); + $this->assertEquals($expected, $this->get_rows($ids)); } - public function getReparseTests() + public function get_reparse_tests() { return array( array( |