1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-05-15 12:05:21 +02:00

[ticket/11495] Rename fix function to regenerate_left_right_ids()

This method regenerates the left/right ids for the nested set based on the
parent/child relations. This function executes three queries per item, so
it should only be called, when the set has one of the following problems:
- The set has a duplicated value inside the left/right id chain
- The set has a missing value inside the left/right id chain
- The set has items that do not have a left/right is set

When regenerating the items, the items are sorted by parent id and their
current left id, so the current child/parent relationships are kept and
running the function on a working set will not change any orders.

PHPBB3-11495
This commit is contained in:
Joas Schilling 2013-04-25 13:40:25 +02:00
parent 61e72d3a10
commit 4bff28a0ee
3 changed files with 21 additions and 9 deletions

View File

@ -674,7 +674,7 @@ abstract class phpbb_nestedset_base implements phpbb_nestedset_interface
/** /**
* @inheritdoc * @inheritdoc
*/ */
public function recalculate_nested_set($new_id, $parent_id = 0, $reset_ids = false) public function regenerate_left_right_ids($new_id, $parent_id = 0, $reset_ids = false)
{ {
if ($reset_ids) if ($reset_ids)
{ {
@ -716,7 +716,7 @@ abstract class phpbb_nestedset_base implements phpbb_nestedset_interface
$new_id++; $new_id++;
// Then we go through any children and update their left/right id's // Then we go through any children and update their left/right id's
$new_id = $this->recalculate_nested_set($new_id, $row[$this->column_item_id]); $new_id = $this->regenerate_left_right_ids($new_id, $row[$this->column_item_id]);
// Then we come back and update the right_id for this module // Then we come back and update the right_id for this module
if ($row[$this->column_right_id] != $new_id) if ($row[$this->column_right_id] != $new_id)

View File

@ -124,12 +124,24 @@ interface phpbb_nestedset_interface
public function get_parent_data(array $item); public function get_parent_data(array $item);
/** /**
* Recalculate Nested Sets * Regenerate left/right ids from parent/child relationship
*
* This method regenerates the left/right ids for the nested set based on
* the parent/child relations. This function executes three queries per
* item, so it should only be called, when the set has one of the following
* problems:
* - The set has a duplicated value inside the left/right id chain
* - The set has a missing value inside the left/right id chain
* - The set has items that do not have a left/right is set
*
* When regenerating the items, the items are sorted by parent id and their
* current left id, so the current child/parent relationships are kept
* and running the function on a working set will not change any orders.
* *
* @param int $new_id First left_id to be used (should start with 1) * @param int $new_id First left_id to be used (should start with 1)
* @param int $parent_id parent_id of the current set (default = 0) * @param int $parent_id parent_id of the current set (default = 0)
* @param bool $reset_ids Should we reset all left_id/right_id on the first call? * @param bool $reset_ids Should we reset all left_id/right_id on the first call?
* @return int $new_id The next left_id/right_id that should be used * @return int $new_id The next left_id/right_id that should be used
*/ */
public function recalculate_nested_set($new_id, $parent_id = 0, $reset_ids = false); public function regenerate_left_right_ids($new_id, $parent_id = 0, $reset_ids = false);
} }

View File

@ -9,7 +9,7 @@
require_once dirname(__FILE__) . '/set_forum_base.php'; require_once dirname(__FILE__) . '/set_forum_base.php';
class phpbb_tests_nestedset_set_forum_recalculate_test extends phpbb_tests_nestedset_set_forum_base class phpbb_tests_nestedset_set_forum_regenerate_test extends phpbb_tests_nestedset_set_forum_base
{ {
protected $fixed_set = array( protected $fixed_set = array(
array('forum_id' => 1, 'parent_id' => 0, 'left_id' => 1, 'right_id' => 6, 'forum_parents' => ''), array('forum_id' => 1, 'parent_id' => 0, 'left_id' => 1, 'right_id' => 6, 'forum_parents' => ''),
@ -27,7 +27,7 @@ class phpbb_tests_nestedset_set_forum_recalculate_test extends phpbb_tests_neste
array('forum_id' => 11, 'parent_id' => 7, 'left_id' => 20, 'right_id' => 21, 'forum_parents' => ''), array('forum_id' => 11, 'parent_id' => 7, 'left_id' => 20, 'right_id' => 21, 'forum_parents' => ''),
); );
public function recalculate_nested_set_data() public function regenerate_left_right_ids_data()
{ {
return array( return array(
array('UPDATE phpbb_forums array('UPDATE phpbb_forums
@ -56,13 +56,13 @@ class phpbb_tests_nestedset_set_forum_recalculate_test extends phpbb_tests_neste
} }
/** /**
* @dataProvider recalculate_nested_set_data * @dataProvider regenerate_left_right_ids_data
*/ */
public function test_recalculate_nested_set($breaking_query, $reset_ids) public function test_regenerate_left_right_ids($breaking_query, $reset_ids)
{ {
$result = $this->db->sql_query($breaking_query); $result = $this->db->sql_query($breaking_query);
$this->assertEquals(23, $this->set->recalculate_nested_set(1, 0, $reset_ids)); $this->assertEquals(23, $this->set->regenerate_left_right_ids(1, 0, $reset_ids));
$result = $this->db->sql_query('SELECT forum_id, parent_id, left_id, right_id, forum_parents $result = $this->db->sql_query('SELECT forum_id, parent_id, left_id, right_id, forum_parents
FROM phpbb_forums FROM phpbb_forums