[ticket/13987] Add --dry-run option to reparser CLI

PHPBB3-13987
This commit is contained in:
JoshyPHP 2015-07-06 01:43:43 +02:00
parent ca5d4fd310
commit 7ccb638912
7 changed files with 96 additions and 44 deletions

View file

@ -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_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' => '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_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_MIN' => 'Lowest record ID to process',
'CLI_DESCRIPTION_REPARSER_REPARSE_OPT_RANGE_MAX' => 'Highest 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', 'CLI_DESCRIPTION_REPARSER_REPARSE_OPT_RANGE_SIZE' => 'Approximate number of records to process at a time',

View file

@ -56,6 +56,12 @@ class reparse extends \phpbb\console\command\command
->setName('reparser:reparse') ->setName('reparser:reparse')
->setDescription($this->user->lang('CLI_DESCRIPTION_REPARSER_REPARSE')) ->setDescription($this->user->lang('CLI_DESCRIPTION_REPARSER_REPARSE'))
->addArgument('reparser-name', InputArgument::OPTIONAL, $this->user->lang('CLI_DESCRIPTION_REPARSER_REPARSE_ARG_1')) ->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( ->addOption(
'range-min', 'range-min',
null, null,
@ -126,9 +132,10 @@ class reparse extends \phpbb\console\command\command
$reparser = $this->reparsers[$name]; $reparser = $this->reparsers[$name];
// Start at range-max if specified or at the highest ID otherwise // 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'); $max = (is_null($input->getOption('range-max'))) ? $reparser->get_max_id() : $input->getOption('range-max');
$min = $input->getOption('range-min'); $min = $input->getOption('range-min');
$size = $input->getOption('range-size'); $size = $input->getOption('range-size');
$dry_run = $input->getOption('dry-run');
if ($max === 0) if ($max === 0)
{ {
@ -176,7 +183,7 @@ class reparse extends \phpbb\console\command\command
$end = max($min, $current); $end = max($min, $current);
$progress->setMessage($this->user->lang('CLI_REPARSER_REPARSE_REPARSING', str_replace('text_reparser.', '', $name), $start, $end)); $progress->setMessage($this->user->lang('CLI_REPARSER_REPARSE_REPARSING', str_replace('text_reparser.', '', $name), $start, $end));
$reparser->reparse_range($start, $end); $reparser->reparse_range($start, $end, $dry_run);
$current = $start - 1; $current = $start - 1;
$progress->setProgress($max + 1 - $start); $progress->setProgress($max + 1 - $start);

View file

@ -170,20 +170,21 @@ abstract class base implements reparser_interface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function reparse_range($min_id, $max_id) public function reparse_range($min_id, $max_id, $dry_run = false)
{ {
foreach ($this->get_records_by_range($min_id, $max_id) as $record) foreach ($this->get_records_by_range($min_id, $max_id) as $record)
{ {
$this->reparse_record($record); $this->reparse_record($record, $dry_run);
} }
} }
/** /**
* Reparse given record * Reparse given record
* *
* @param array $record Associative array containing the record's data * @param array $record Associative array containing the record's data
* @param integer $dry_run If TRUE, do not save the changes
*/ */
protected function reparse_record(array $record) protected function reparse_record(array $record, $dry_run)
{ {
$record = $this->add_missing_fields($record); $record = $this->add_missing_fields($record);
$flags = ($record['enable_bbcode']) ? OPTION_FLAG_BBCODE : 0; $flags = ($record['enable_bbcode']) ? OPTION_FLAG_BBCODE : 0;
@ -212,8 +213,8 @@ abstract class base implements reparser_interface
$unparsed['enable_url_bbcode'] $unparsed['enable_url_bbcode']
); );
// Save the new text if it has changed // Save the new text if it has changed and it's not a dry run
if ($text !== $record['text']) if ($text !== $record['text'] && !$dry_run)
{ {
$record['text'] = $text; $record['text'] = $text;
$this->save_record($record); $this->save_record($record);

View file

@ -25,8 +25,9 @@ interface reparser_interface
/** /**
* Reparse all records in given range * Reparse all records in given range
* *
* @param integer $min_id Lower bound * @param integer $min_id Lower bound
* @param integer $max_id Upper bound * @param integer $max_id Upper bound
* @param integer $dry_run If TRUE, do not save the changes
*/ */
public function reparse_range($min_id, $max_id); public function reparse_range($min_id, $max_id, $dry_run = false);
} }

View file

@ -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)); 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() public function setUp()
{ {
global $config; global $config;
@ -46,18 +58,19 @@ class phpbb_textreparser_contact_admin_info_test extends phpbb_database_test_cas
$this->assertEquals(1, $reparser->get_max_id()); $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->reparse_range(1, 1, true);
$new_rows = $this->get_rows();
$this->assertEquals($old_rows, $new_rows);
}
public function test_reparse()
{ {
$reparser = $this->get_reparser(); $reparser = $this->get_reparser();
$reparser->reparse_range(1, 1); $reparser->reparse_range(1, 1);
$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);
$expected = array( $expected = array(
array( array(
'config_name' => 'contact_admin_info', 'config_name' => 'contact_admin_info',
@ -76,6 +89,6 @@ class phpbb_textreparser_contact_admin_info_test extends phpbb_database_test_cas
'config_value' => '1a2hbwf5', 'config_value' => '1a2hbwf5',
), ),
); );
$this->assertEquals($expected, $rows); $this->assertEquals($expected, $this->get_rows());
} }
} }

View file

@ -28,6 +28,18 @@ class phpbb_textreparser_poll_option_test extends phpbb_database_test_case
return new \phpbb\textreparser\plugins\poll_option($this->db); 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() public function setUp()
{ {
global $config; global $config;
@ -46,18 +58,19 @@ class phpbb_textreparser_poll_option_test extends phpbb_database_test_case
$this->assertEquals(123, $reparser->get_max_id()); $this->assertEquals(123, $reparser->get_max_id());
} }
public function test_dry_run()
{
$old_rows = $this->get_rows();
$reparser = $this->get_reparser();
$reparser->reparse_range(1, 1, true);
$new_rows = $this->get_rows();
$this->assertEquals($old_rows, $new_rows);
}
public function testReparse() public function testReparse()
{ {
$reparser = $this->get_reparser(); $reparser = $this->get_reparser();
$reparser->reparse_range(2, 13); $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( $expected = array(
array( array(
'topic_id' => 1, 'topic_id' => 1,
@ -110,6 +123,6 @@ class phpbb_textreparser_poll_option_test extends phpbb_database_test_case
'poll_option_text' => 'This row should be [b:abcd1234]ignored[/b:abcd1234]', 'poll_option_text' => 'This row should be [b:abcd1234]ignored[/b:abcd1234]',
), ),
); );
$this->assertEquals($expected, $rows); $this->assertEquals($expected, $this->get_rows());
} }
} }

View file

@ -20,6 +20,21 @@ abstract class phpbb_textreparser_test_row_based_plugin extends phpbb_database_t
abstract protected function get_reparser(); 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() public function setUp()
{ {
global $config; global $config;
@ -38,10 +53,19 @@ abstract class phpbb_textreparser_test_row_based_plugin extends phpbb_database_t
$this->assertEquals(1000, $reparser->get_max_id()); $this->assertEquals(1000, $reparser->get_max_id());
} }
public function test_dry_run()
{
$old_rows = $this->get_rows(array(1));
$reparser = $this->get_reparser();
$reparser->reparse_range(1, 1, true);
$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 = $this->get_reparser();
$reparser->reparse_range($min_id, $max_id); $reparser->reparse_range($min_id, $max_id);
@ -52,18 +76,10 @@ abstract class phpbb_textreparser_test_row_based_plugin extends phpbb_database_t
$ids[] = $row['id']; $ids[] = $row['id'];
} }
$columns = $reparser->get_columns(); $this->assertEquals($expected, $this->get_rows($ids));
$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);
} }
public function getReparseTests() public function get_reparse_tests()
{ {
return array( return array(
array( array(