From d453ec2b922a088cfa41a81753c37b7f038ecb77 Mon Sep 17 00:00:00 2001 From: Yu 'noyle' Liu Date: Thu, 6 Aug 2020 03:53:06 +0800 Subject: [PATCH] Fix #238 Error on importing sub-forum before its parent's been merged. And code improvement. (#239) * Fix mybb/merge-system:#238 SMF2 module's error on processing sub-forum before its parent's been merged. * Fix mybb/merge-system:#238 SMF module's error on importing sub-forum before its parent's been merged. Improve code format for SMF2's forum module. * Add a TODO mark to lines relating to mybb/merge-system:#238. * Improve code quality: remove some redundant code in `import_forums` module of vbulletin3/vbulletin4/vbulletin5. * Code improvement. * Revert "Add a TODO mark to lines relating to mybb/merge-system:#238." This reverts commit eb09a980e79dea1e4175ed6caee2e9521574ce5e. * Add default value (integer) for field `pid` in forums base module --- boards/smf/forums.php | 29 ++++++++++++++++++++++++++++- boards/smf2/forums.php | 29 ++++++++++++++++++++++++++++- boards/vbulletin3/forums.php | 1 - boards/vbulletin4/forums.php | 1 - boards/vbulletin5/forums.php | 1 - resources/modules/forums.php | 2 ++ 6 files changed, 58 insertions(+), 5 deletions(-) diff --git a/boards/smf/forums.php b/boards/smf/forums.php index bb3e9e8..65f29ef 100644 --- a/boards/smf/forums.php +++ b/boards/smf/forums.php @@ -43,10 +43,17 @@ function convert_data($data) if($data['ID_PARENT']) { - $insert_data['pid'] = $this->get_import->fid_f($data['ID_PARENT']); + // Parent forum is a board. + $insert_data['import_pid'] = $data['ID_PARENT']; + + // Assign the already merged parent board's ID, otherwise 0. + $pid = $this->get_import->fid_f($data['ID_PARENT']); + $insert_data['pid'] = empty($pid) ? 0 : $pid; } else { + // Parent forum is a category. All categories should have been already merged. + $insert_data['import_pid'] = $data['ID_CAT']; // TODO: may needn't this, and this could be confusing. $insert_data['pid'] = $this->get_import->fid_c($data['ID_CAT']); } @@ -71,6 +78,26 @@ function fetch_total() return $import_session['total_forums']; } + /** + * Update imported forums that don't have a parent forum assigned. + */ + function finish() + { + global $db; + + // 'f' type forum. Column `pid`'s value is 0 if this forum is merged before its parent being merged. + $query = $db->simple_select("forums", "fid,import_pid", "type = 'f' AND import_fid != 0 AND pid = 0"); + while($forum = $db->fetch_array($query)) + { + $pid = $this->get_import->fid($forum['import_pid']); + if(!empty($pid)) // Do another check, failure will leave dirty work to parent class's cleanup() function. + { + $db->update_query("forums", array('pid' => $pid), "fid='{$forum['fid']}'", 1); + } + } + $db->free_result($query); + } + /** * Correctly associate any forums with their correct parent ids. This is automagically run after importing * forums. diff --git a/boards/smf2/forums.php b/boards/smf2/forums.php index a4d918f..7ef6110 100644 --- a/boards/smf2/forums.php +++ b/boards/smf2/forums.php @@ -43,10 +43,17 @@ function convert_data($data) if($data['id_parent']) { - $insert_data['pid'] = $this->get_import->fid_f($data['id_parent']); + // Parent forum is a board. + $insert_data['import_pid'] = $data['id_parent']; + + // Assign the already merged parent board's ID, otherwise 0. + $pid = $this->get_import->fid_f($data['id_parent']); + $insert_data['pid'] = empty($pid) ? 0 : $pid; } else { + // Parent forum is a category. All categories should have been already merged. + $insert_data['import_pid'] = $data['id_cat']; // TODO: may needn't this, and this could be confusing. $insert_data['pid'] = $this->get_import->fid_c($data['id_cat']); } @@ -71,6 +78,26 @@ function fetch_total() return $import_session['total_forums']; } + /** + * Update imported forums that don't have a parent forum assigned. + */ + function finish() + { + global $db; + + // 'f' type forum. Column `pid`'s value is 0 if this forum is merged before its parent being merged. + $query = $db->simple_select("forums", "fid,import_pid", "type = 'f' AND import_fid != 0 AND pid = 0"); + while($forum = $db->fetch_array($query)) + { + $pid = $this->get_import->fid($forum['import_pid']); + if(!empty($pid)) // Do another check, failure will leave dirty work to parent class's cleanup() function. + { + $db->update_query("forums", array('pid' => $pid), "fid='{$forum['fid']}'", 1); + } + } + $db->free_result($query); + } + /** * Correctly associate any forums with their correct parent ids. This is automagically run after importing * forums. diff --git a/boards/vbulletin3/forums.php b/boards/vbulletin3/forums.php index a19f52e..298ab69 100644 --- a/boards/vbulletin3/forums.php +++ b/boards/vbulletin3/forums.php @@ -59,7 +59,6 @@ function convert_data($data) if($data['parentid'] == '-1') { $insert_data['type'] = 'c'; - $insert_data['import_fid'] = $data['forumid']; } // We have a forum else diff --git a/boards/vbulletin4/forums.php b/boards/vbulletin4/forums.php index 797d108..9871dc6 100644 --- a/boards/vbulletin4/forums.php +++ b/boards/vbulletin4/forums.php @@ -59,7 +59,6 @@ function convert_data($data) if($data['parentid'] == '-1') { $insert_data['type'] = 'c'; - $insert_data['import_fid'] = $data['forumid']; } // We have a forum else diff --git a/boards/vbulletin5/forums.php b/boards/vbulletin5/forums.php index b246f90..d1ce8dd 100644 --- a/boards/vbulletin5/forums.php +++ b/boards/vbulletin5/forums.php @@ -59,7 +59,6 @@ function convert_data($data) if($data['parentid'] == '-1') { $insert_data['type'] = 'c'; - $insert_data['import_fid'] = $data['forumid']; } // We have a forum else diff --git a/resources/modules/forums.php b/resources/modules/forums.php index de8ba01..517ae68 100644 --- a/resources/modules/forums.php +++ b/resources/modules/forums.php @@ -25,6 +25,7 @@ abstract class Converter_Module_Forums extends Converter_Module 'threads' => 0, 'posts' => 0, 'type' => 'f', + 'pid' => 0, 'active' => 1, 'open' => 1, 'allowhtml' => 0, @@ -58,6 +59,7 @@ abstract class Converter_Module_Forums extends Converter_Module 'lastposteruid', 'threads', 'posts', + 'pid', 'active', 'open', 'allowhtml',