From 72b61b92c0e909f98466c9355beefb579420b17e Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 26 Oct 2026 21:41:09 +0000 Subject: [PATCH 1/2] Phase 7: import the data of a running iForum installation Importer copies categories, forums, moderators, topics, posts, edit histories, attachments, reports, suspensions, read marks, digests, the archive, rights, subscriptions, preferences and the permission template, keeping all ids and never touching the source. admin/import.php offers check, import, verify and roll back. Module version 1.0.0 Alpha 7. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01MZy45V5RV9nx6Buq8vqKHr --- CHANGELOG.md | 3 +- README.md | 2 +- src/admin/import.php | 82 +++ src/admin/menu.php | 1 + src/class/Importer.php | 970 ++++++++++++++++++++++++++++ src/icms_version.php | 4 +- src/include/admin.php | 42 ++ src/language/english/admin.php | 87 +++ src/language/english/modinfo.php | 1 + src/language/nederlands/admin.php | 87 +++ src/language/nederlands/modinfo.php | 1 + tests/Integration/Database.php | 13 +- tests/Integration/ImportTest.php | 385 +++++++++++ tests/Integration/OldIforum.php | 185 ++++++ tests/Unit/ImportReportTest.php | 44 ++ tests/phpstan-bootstrap.php | 1 + 16 files changed, 1901 insertions(+), 7 deletions(-) create mode 100644 src/admin/import.php create mode 100644 src/class/Importer.php create mode 100644 tests/Integration/ImportTest.php create mode 100644 tests/Integration/OldIforum.php create mode 100644 tests/Unit/ImportReportTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index d264849..5972876 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ## 1.0.0 Alpha (unreleased) -While the module is in development the status carries the phase: 1.0.0 Alpha 5 is phase 5. +While the module is in development the status carries the phase: 1.0.0 Alpha 7 is phase 7. - Phase 0: module skeleton for ImpressCMS 2.0 in IPF mode, install and update routines, English and Dutch language files, CI on PHP 8.2 to 8.5. - Phase 1: data model. 13 IPF objects with handlers (category, forum, moderator, topic, post, attachment, report, suspension, readmark, online, digest, archive, permtemplate), group permissions on categories and forums, counters and cascading deletes through handler events, indexes in `imforum_db_upgrade_1()`, labels in English and Dutch, integration tests against MySQL 5.5, MySQL 5.7 (MyISAM) and MariaDB 10.11. @@ -13,3 +13,4 @@ While the module is in development the status carries the phase: 1.0.0 Alpha 5 i - Phase 6: background tasks. Two autotasks, registered by the core when the module is installed or updated: the digest mail and a cleanup. Members subscribe to the digest with the notification "Digest" for all forums. `DigestMailer` covers the time since the last digest (one day for the first), lists titles, counts and links but never the text of a post, sends each member only the forums that member may read, and stores the complete digest for the administrators before it sends, so a failure halfway never causes a second mail. The cleanup removes visitors that are no longer online, read marks older than a preference (90 days; 0 keeps them) and suspensions that ended more than 30 days ago. A topic without a read mark counts as unread, so posts older than the limit first count as read for members who have marks, and only then the old marks go. - Database access: all hand-written SQL now goes through `mod_imforum_Db` over the core's PDO connection (`icms::$db`) with bound parameters, instead of `queryF()` on the legacy adapter (`icms::$xoopsDB`) with values glued into the SQL. The IPF handlers keep the legacy adapter the core gives them for their own queries; both share one PDO connection. Without PDO (`XOOPS_DB_TYPE` not `pdo.*`) the module stops with a clear message. Prepared statements do not show up in the debug console of the core, which only logs `query()`. - Editor: the post, forum and category forms use the `dhtmltextarea` control, as the content module does, so the WYSIWYG editor chosen in the site preferences loads (the plain `textarea` control of the IPF ignores the editor). A post written in an editor is stored as HTML (`dohtml` on, line breaks off) and is shown through the purifier; without an editor the text still stays as typed. The quick reply and the moderation forms stay plain textareas. +- Phase 7: importer. `admin/import.php` and `Importer` copy the data of a running iForum installation into imforum, keeping every id, and never change iForum: categories, forums, the serialized moderator lists (one row per member), topics (ratings, polls and tags are left out), posts joined with their text (IP addresses become text), edit histories (base64 serialized lists become JSON), attachments from the posts and from the reserved table (names that could leave the upload folder are refused, files are copied and measured), reports, suspensions that have not ended, read marks (one per member and item), digests, the archive, group permissions of the forums and categories under the id of imforum (poll rights are left out), subscriptions that imforum has events for, the preferences that exist in both modules (the `NONE` entry of the subject prefixes is dropped, as imforum counts prefixes from 1) and the cached permission template. The check writes nothing: it reports missing tables and columns, character sets, orphans, posts that grow past 64 KB when converted, missing attachment files and text that looks like UTF-8 in latin1 columns, for which the import can read the bytes as UTF-8. Importing and rolling back ask for a confirmation and run only on a POST with a token. Every import starts by emptying the imforum tables, so an interrupted run is simply repeated; rolling back empties them again and removes the copied rights, subscriptions and files. Afterwards the counters are recounted and `verify()` compares row counts, posts per forum and per topic, a checksum of 200 random posts, the rights and the attachment files. Tests build an old installation, check every table, run the largest case (43,778 posts) and prove that iForum's tables are unchanged. diff --git a/README.md b/README.md index 6f78741..e079360 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ Community forum module for ImpressCMS 2.0, built on the ImpressCMS Persistable Framework (IPF). imforum is the successor of iForum: same features, new code, and an importer that takes over the data of a running iForum installation. -Status: phase 6 (background tasks), module version 1.0.0 Alpha 6. Members start topics, reply (with a quick reply under the topic), quote, edit and delete their own posts, attach files and follow topics and forums by notification; guests can post where the group permissions allow it, behind a captcha. Posts wait for a moderator when the group lacks forum_noapprove. Moderators of a forum lock, pin and feature topics, move, merge and split them, delete topics, approve waiting posts, handle reports from members and suspend members, all from the front end through `moderate.php`; members report posts through `report.php`. Two autotasks (Systeem > Autotasks, daily by default) send a digest of new topics and posts to members who subscribe to it and clean up old read marks, the visitors list and ended suspensions. Categories, forums, moderators, permissions, reports, suspensions, digests, the archive and pruning are managed from the admin area. +Status: phase 7 (importer), module version 1.0.0 Alpha 7. Members start topics, reply (with a quick reply under the topic), quote, edit and delete their own posts, attach files and follow topics and forums by notification; guests can post where the group permissions allow it, behind a captcha. Posts wait for a moderator when the group lacks forum_noapprove. Moderators of a forum lock, pin and feature topics, move, merge and split them, delete topics, approve waiting posts, handle reports from members and suspend members, all from the front end through `moderate.php`; members report posts through `report.php`. Two autotasks (Systeem > Autotasks, daily by default) send a digest of new topics and posts to members who subscribe to it and clean up old read marks, the visitors list and ended suspensions. Categories, forums, moderators, permissions, reports, suspensions, digests, the archive and pruning are managed from the admin area. The import page copies the data of a running iForum installation into imforum: check, import, verify and roll back. ## Requirements diff --git a/src/admin/import.php b/src/admin/import.php new file mode 100644 index 0000000..c62a74d --- /dev/null +++ b/src/admin/import.php @@ -0,0 +1,82 @@ + htmlspecialchars($text, ENT_QUOTES, 'UTF-8'); + $form = new icms_form_Theme(_AM_IMFORUM_IMPORT, 'import', imforum_admin_url('import.php'), 'post', true); + $select = new icms_form_elements_Select(_AM_IMFORUM_IMPORT_ENCODING, 'encoding', $encoding); + $select->addOptionArray([ + mod_imforum_Importer::ENCODING_AS_IS => _AM_IMFORUM_IMPORT_ENCODING_AS_IS, + mod_imforum_Importer::ENCODING_UTF8_BYTES => _AM_IMFORUM_IMPORT_ENCODING_BYTES, + ]); + imforum_form_add($form, new icms_form_elements_Label('', $e(_AM_IMFORUM_IMPORT_STEPS))); + imforum_form_add($form, $select); + $buttons = ''; + foreach (['check' => _AM_IMFORUM_IMPORT_CHECK, 'askrun' => _AM_IMFORUM_IMPORT_RUN, 'verify' => _AM_IMFORUM_IMPORT_VERIFY, 'askrollback' => _AM_IMFORUM_IMPORT_ROLLBACK] as $value => $label) { + $buttons .= ' '; + } + imforum_form_add($form, new icms_form_elements_Label('', $buttons)); + return $form->render(); +}; + +switch ($op) { + case 'askrun': + case 'askrollback': + icms_cp_header(); + imforum_admin_menu(9, _AM_IMFORUM_IMPORT); + icms_core_Message::confirm( + ['op' => $op === 'askrun' ? 'run' : 'rollback', 'encoding' => $encoding, 'confirm' => 1], + imforum_admin_url('import.php'), + $op === 'askrun' ? _AM_IMFORUM_IMPORT_CONFIRM_RUN : _AM_IMFORUM_IMPORT_CONFIRM_ROLLBACK, + _AM_IMFORUM_IMPORT_GO + ); + icms_cp_footer(); + break; + + case 'run': + // a long import must not stop when the browser gives up waiting + @set_time_limit(0); + ignore_user_abort(true); + $report = $importer->run(); + $html = imforum_import_report_html($report); + if ($report['ok']) { + $html .= '

' . htmlspecialchars(_AM_IMFORUM_IMPORT_VERIFY, ENT_QUOTES, 'UTF-8') . '

' . imforum_import_report_html($importer->verify()); + } + imforum_admin_page(9, _AM_IMFORUM_IMPORT, static fn (): string => $form() . $html, $report['ok'] ? _AM_IMFORUM_IMPORT_DONE : ''); + break; + + case 'rollback': + imforum_admin_page(9, _AM_IMFORUM_IMPORT, static fn (): string => $form() . imforum_import_report_html($importer->rollback()), _AM_IMFORUM_IMPORT_ROLLED_BACK); + break; + + case 'check': + imforum_admin_page(9, _AM_IMFORUM_IMPORT, static fn (): string => $form() . imforum_import_report_html($importer->check())); + break; + + case 'verify': + imforum_admin_page(9, _AM_IMFORUM_IMPORT, static fn (): string => $form() . imforum_import_report_html($importer->verify())); + break; + + default: + imforum_admin_page(9, _AM_IMFORUM_IMPORT, $form, _AM_IMFORUM_IMPORT_DSC); + break; +} diff --git a/src/admin/menu.php b/src/admin/menu.php index d6f51e7..d650abe 100644 --- a/src/admin/menu.php +++ b/src/admin/menu.php @@ -23,6 +23,7 @@ 'digest' => _MI_IMFORUM_MENU_DIGESTS, 'archive' => _MI_IMFORUM_MENU_ARCHIVE, 'prune' => _MI_IMFORUM_MENU_PRUNE, + 'import' => _MI_IMFORUM_MENU_IMPORT, ] as $script => $title) { $adminmenu[] = [ 'title' => $title, diff --git a/src/class/Importer.php b/src/class/Importer.php new file mode 100644 index 0000000..9392035 --- /dev/null +++ b/src/class/Importer.php @@ -0,0 +1,970 @@ + ['cat_id', 'cat_title', 'cat_description', 'cat_order', 'cat_image', 'cat_url'], + 'forums' => ['forum_id', 'forum_name', 'forum_desc', 'parent_forum', 'forum_moderator', 'cat_id', 'forum_type', 'allow_html', 'allow_sig', 'allow_subject_prefix', 'hot_threshold', 'forum_order', 'attach_maxkb', 'attach_ext'], + 'topics' => ['topic_id', 'topic_title', 'topic_poster', 'topic_time', 'topic_views', 'topic_replies', 'topic_last_post_id', 'forum_id', 'topic_status', 'topic_subject', 'topic_sticky', 'topic_digest', 'digest_time', 'approved', 'poster_name'], + 'posts' => ['post_id', 'pid', 'topic_id', 'forum_id', 'post_time', 'uid', 'poster_name', 'poster_ip', 'subject', 'dohtml', 'dosmiley', 'doxcode', 'dobr', 'doimage', 'icon', 'attachsig', 'approved', 'post_karma', 'attachment', 'require_reply'], + 'posts_text' => ['post_id', 'post_text', 'post_edit'], + ]; + + /** Tables iForum may not have; a missing one is skipped, one with other columns is an error */ + private const OPTIONAL = [ + 'attachments' => ['attach_id', 'post_id', 'name_saved', 'name_disp', 'mimetype', 'online', 'attach_time', 'download'], + 'report' => ['report_id', 'post_id', 'reporter_uid', 'reporter_ip', 'report_time', 'report_text', 'report_result', 'report_memo'], + 'moderates' => ['mod_id', 'mod_start', 'mod_end', 'mod_desc', 'uid', 'ip', 'forum_id'], + 'reads_forum' => ['uid', 'read_time', 'read_item', 'post_id'], + 'reads_topic' => ['uid', 'read_time', 'read_item', 'post_id'], + 'digest' => ['digest_id', 'digest_time', 'digest_content'], + 'archive' => ['topic_id', 'post_id', 'post_text'], + ]; + + /** Preferences that carry over under the same name */ + private const SAME_SETTINGS = ['topics_per_page', 'posts_per_page', 'quickreply_enabled', 'edit_timelimit', 'disclaimer']; + + private int $now; + + /** @var array */ + private array $present = []; + + public function __construct(private string $encoding = self::ENCODING_AS_IS, ?int $now = null, private string $sourceDirname = self::SOURCE_DIRNAME) { + if (function_exists('icms_loadLanguageFile')) { + icms_loadLanguageFile(IMFORUM_DIRNAME, 'admin'); + } + $this->now = $now ?? time(); + if (!in_array($this->encoding, [self::ENCODING_AS_IS, self::ENCODING_UTF8_BYTES], true)) { + $this->encoding = self::ENCODING_AS_IS; + } + } + + /** + * The dry run: reads the source and the target and writes nothing + * + * @return array{ok: bool, errors: list, warnings: list, rows: list, log: list} + */ + public function check(): array { + $report = self::newReport(); + $this->checkStructure($report); + if (!$report['errors']) { + $this->checkContent($report); + } + $report['ok'] = !$report['errors']; + return $report; + } + + /** + * Empty the target and copy everything + * + * Refuses to start when check() finds errors. + * + * @return array{ok: bool, errors: list, warnings: list, rows: list, log: list} + */ + public function run(): array { + $report = self::newReport(); + $this->checkStructure($report); + if ($report['errors']) { + return $report; + } + $oldMid = $this->moduleId($this->sourceDirname); + $newMid = $this->moduleId(IMFORUM_DIRNAME); + + $this->clearTarget($newMid); + $report['log'][] = _AM_IMFORUM_IMPORT_LOG_EMPTIED; + + $steps = [ + [_AM_IMFORUM_IMPORT_CATEGORIES, fn (): array => $this->copyCategories()], + [_AM_IMFORUM_IMPORT_FORUMS, fn (): array => $this->copyForums()], + [_AM_IMFORUM_IMPORT_MODERATORS, fn (): array => $this->copyModerators()], + [_AM_IMFORUM_IMPORT_TOPICS, fn (): array => $this->copyTopics()], + [_AM_IMFORUM_IMPORT_POSTS, fn (): array => $this->copyPosts()], + [_AM_IMFORUM_IMPORT_EDITS, fn (): array => $this->copyEdits()], + [_AM_IMFORUM_IMPORT_ATTACHMENTS, fn (): array => $this->copyAttachments()], + [_AM_IMFORUM_IMPORT_REPORTS, fn (): array => $this->copyReports()], + [_AM_IMFORUM_IMPORT_SUSPENSIONS, fn (): array => $this->copySuspensions()], + [_AM_IMFORUM_IMPORT_READMARKS, fn (): array => $this->copyReadmarks()], + [_AM_IMFORUM_IMPORT_DIGESTS, fn (): array => $this->copyDigests()], + [_AM_IMFORUM_IMPORT_ARCHIVE, fn (): array => $this->copyArchive()], + [_AM_IMFORUM_IMPORT_PERMISSIONS, fn (): array => $this->copyPermissions($oldMid, $newMid)], + [_AM_IMFORUM_IMPORT_NOTIFICATIONS, fn (): array => $this->copyNotifications($oldMid, $newMid)], + [_AM_IMFORUM_IMPORT_SETTINGS, fn (): array => $this->copySettings($oldMid, $newMid)], + [_AM_IMFORUM_IMPORT_TEMPLATE, fn (): array => $this->copyTemplate()], + [_AM_IMFORUM_IMPORT_FILES, fn (): array => $this->copyFiles()], + ]; + $current = ''; + try { + foreach ($steps as [$label, $step]) { + $current = $label; + $result = $step(); + $report['rows'][] = ['label' => $label, 'source' => $result['source'], 'target' => $result['target'], 'note' => $result['note']]; + array_push($report['warnings'], ...$result['warnings']); + $report['log'][] = sprintf(_AM_IMFORUM_IMPORT_LOG_STEP, $label, $result['target']); + } + } catch (RuntimeException $e) { + $report['errors'][] = sprintf(_AM_IMFORUM_IMPORT_ERR_STEP, $current, $e->getMessage()); + return $report; + } + + // the counters of the old data are not taken over: they are recounted from the copied rows + $ok = icms_getModuleHandler('topic', IMFORUM_DIRNAME)->synchroniseAll() + && icms_getModuleHandler('forum', IMFORUM_DIRNAME)->synchroniseAll(); + $this->keepPendingReferences(); + if (!$ok) { + $report['errors'][] = _AM_IMFORUM_RECOUNT_ERROR; + } + $report['log'][] = _AM_IMFORUM_IMPORT_LOG_RECOUNTED; + $report['ok'] = !$report['errors']; + return $report; + } + + /** + * Compare source and target after a run + * + * @return array{ok: bool, errors: list, warnings: list, rows: list, log: list} + */ + public function verify(): array { + $report = self::newReport(); + $this->checkStructure($report); + if ($report['errors']) { + return $report; + } + $oldMid = $this->moduleId($this->sourceDirname); + $newMid = $this->moduleId(IMFORUM_DIRNAME); + $t = static fn (string $item): string => mod_imforum_Db::table($item); + + $compare = function (string $label, int $source, int $target, bool $atLeast = false, string $note = '') use (&$report): void { + $same = $atLeast ? $target >= $source : $target === $source; + $report['rows'][] = ['label' => $label, 'source' => $source, 'target' => $target, 'note' => $same ? $note : _AM_IMFORUM_IMPORT_DIFFERS]; + if (!$same) { + $report['errors'][] = sprintf(_AM_IMFORUM_IMPORT_ERR_COUNT, $label, $source, $target); + } + }; + + $count = static fn (string $sql, array $params = []): int => mod_imforum_Db::value($sql, $params); + $compare(_AM_IMFORUM_IMPORT_CATEGORIES, $count('SELECT COUNT(*) FROM ' . $this->src('categories')), $count('SELECT COUNT(*) FROM ' . $t('category'))); + $compare(_AM_IMFORUM_IMPORT_FORUMS, $count('SELECT COUNT(*) FROM ' . $this->src('forums')), $count('SELECT COUNT(*) FROM ' . $t('forum'))); + $compare(_AM_IMFORUM_IMPORT_MODERATORS, count($this->moderatorPairs()), $count('SELECT COUNT(*) FROM ' . $t('moderator'))); + $compare(_AM_IMFORUM_IMPORT_TOPICS, $count('SELECT COUNT(*) FROM ' . $this->src('topics')), $count('SELECT COUNT(*) FROM ' . $t('topic'))); + $compare(_AM_IMFORUM_IMPORT_POSTS, $count('SELECT COUNT(*) FROM ' . $this->src('posts')), $count('SELECT COUNT(*) FROM ' . $t('post'))); + $compare(_AM_IMFORUM_IMPORT_ATTACHMENTS, $this->countAttachments(), $count('SELECT COUNT(*) FROM ' . $t('attachment'))); + if ($this->has('report')) { + $compare(_AM_IMFORUM_IMPORT_REPORTS, $count('SELECT COUNT(*) FROM ' . $this->src('report')), $count('SELECT COUNT(*) FROM ' . $t('report'))); + } + if ($this->has('moderates')) { + $compare(_AM_IMFORUM_IMPORT_SUSPENSIONS, $count('SELECT COUNT(*) FROM ' . $this->src('moderates') . ' WHERE mod_end >= ?', [$this->now]), $count('SELECT COUNT(*) FROM ' . $t('suspension')), true, _AM_IMFORUM_IMPORT_NOTE_EXPIRED); + } + $compare(_AM_IMFORUM_IMPORT_READMARKS, $this->countReadmarks(), $count('SELECT COUNT(*) FROM ' . $t('readmark')), false, _AM_IMFORUM_IMPORT_NOTE_MERGED); + if ($this->has('digest')) { + $compare(_AM_IMFORUM_IMPORT_DIGESTS, $count('SELECT COUNT(*) FROM ' . $this->src('digest')), $count('SELECT COUNT(*) FROM ' . $t('digest'))); + } + if ($this->has('archive')) { + $compare(_AM_IMFORUM_IMPORT_ARCHIVE, $count('SELECT COUNT(*) FROM ' . $this->src('archive')), $count('SELECT COUNT(*) FROM ' . $t('archive'))); + } + $compare(_AM_IMFORUM_IMPORT_PERMISSIONS, $this->countPermissions($oldMid), $count('SELECT COUNT(*) FROM ' . $this->core('group_permission') . ' WHERE gperm_modid = ? AND gperm_name IN (' . mod_imforum_Db::marks($this->permissionNames()) . ')', array_merge([$newMid], $this->permissionNames()))); + + // posts per forum and per topic + $perForum = $this->groupMismatches('forum_id', $this->src('posts'), $t('post')); + $report['rows'][] = ['label' => _AM_IMFORUM_IMPORT_POSTS_PER_FORUM, 'source' => '', 'target' => '', 'note' => $perForum ? _AM_IMFORUM_IMPORT_DIFFERS : _AM_IMFORUM_IMPORT_SAME]; + if ($perForum) { + $report['errors'][] = sprintf(_AM_IMFORUM_IMPORT_ERR_GROUPS, _AM_IMFORUM_IMPORT_POSTS_PER_FORUM, implode(', ', array_slice($perForum, 0, 10))); + } + $perTopic = $this->groupMismatches('topic_id', $this->src('posts'), $t('post')); + $report['rows'][] = ['label' => _AM_IMFORUM_IMPORT_POSTS_PER_TOPIC, 'source' => '', 'target' => '', 'note' => $perTopic ? _AM_IMFORUM_IMPORT_DIFFERS : _AM_IMFORUM_IMPORT_SAME]; + if ($perTopic) { + $report['errors'][] = sprintf(_AM_IMFORUM_IMPORT_ERR_GROUPS, _AM_IMFORUM_IMPORT_POSTS_PER_TOPIC, implode(', ', array_slice($perTopic, 0, 10))); + } + + // the counters of the old data against the recounted ones: deviations are reported, not wrong + $forumDeviations = $count('SELECT COUNT(*) FROM ' . $this->src('forums') . ' o JOIN ' . $t('forum') . ' n ON n.forum_id = o.forum_id' + . ' WHERE o.forum_posts <> n.forum_posts OR o.forum_topics <> n.forum_topics'); + $topicDeviations = $count('SELECT COUNT(*) FROM ' . $this->src('topics') . ' o JOIN ' . $t('topic') . ' n ON n.topic_id = o.topic_id WHERE o.topic_replies <> n.topic_replies'); + $report['rows'][] = ['label' => _AM_IMFORUM_IMPORT_COUNTER_DEVIATIONS, 'source' => $forumDeviations . ' / ' . $topicDeviations, 'target' => '', 'note' => _AM_IMFORUM_IMPORT_NOTE_COUNTERS]; + if ($forumDeviations || $topicDeviations) { + $report['warnings'][] = sprintf(_AM_IMFORUM_IMPORT_WARN_COUNTERS, $forumDeviations, $topicDeviations); + } + + // a sample of posts must be the same text + $ids = array_map('intval', array_column(mod_imforum_Db::rows('SELECT post_id FROM ' . $t('post') . ' ORDER BY RAND() LIMIT ' . self::SAMPLE), 0)); + $different = []; + if ($ids) { + $in = mod_imforum_Db::marks($ids); + $source = []; + foreach (mod_imforum_Db::rows('SELECT post_id, MD5(COALESCE(' . $this->text('post_text') . ", '')) FROM " . $this->src('posts_text') . " WHERE post_id IN ($in)", $ids) as $row) { + $source[(int) $row[0]] = (string) $row[1]; + } + foreach (mod_imforum_Db::rows('SELECT post_id, MD5(post_text) FROM ' . $t('post') . " WHERE post_id IN ($in)", $ids) as $row) { + if (($source[(int) $row[0]] ?? md5('')) !== (string) $row[1]) { + $different[] = (int) $row[0]; + } + } + } + $report['rows'][] = ['label' => sprintf(_AM_IMFORUM_IMPORT_CHECKSUMS, count($ids)), 'source' => '', 'target' => '', 'note' => $different ? _AM_IMFORUM_IMPORT_DIFFERS : _AM_IMFORUM_IMPORT_SAME]; + if ($different) { + $report['errors'][] = sprintf(_AM_IMFORUM_IMPORT_ERR_TEXT, implode(', ', array_slice($different, 0, 10))); + } + + // every attachment needs its file + $missing = $this->missingFiles(IMFORUM_UPLOAD_PATH, 'attachment'); + $report['rows'][] = ['label' => _AM_IMFORUM_IMPORT_FILES_PRESENT, 'source' => '', 'target' => '', 'note' => $missing ? sprintf(_AM_IMFORUM_IMPORT_NOTE_MISSING, count($missing)) : _AM_IMFORUM_IMPORT_SAME]; + if ($missing) { + $report['warnings'][] = sprintf(_AM_IMFORUM_IMPORT_WARN_FILES, count($missing), implode(', ', array_slice($missing, 0, 10))); + } + + $report['ok'] = !$report['errors']; + return $report; + } + + /** + * Undo a run: empty imforum's tables and remove what was copied next to them + * + * The tables of iForum are never touched. + * + * @return array{ok: bool, errors: list, warnings: list, rows: list, log: list} + */ + public function rollback(): array { + $report = self::newReport(); + $newMid = $this->moduleId(IMFORUM_DIRNAME); + if ($newMid === 0) { + $report['errors'][] = _AM_IMFORUM_IMPORT_ERR_NO_TARGET; + return $report; + } + $removed = 0; + foreach (mod_imforum_Db::rows('SELECT DISTINCT name_saved FROM ' . mod_imforum_Db::table('attachment')) as $row) { + $name = (string) $row[0]; + $file = IMFORUM_UPLOAD_PATH . $name; + if (self::isSafeName($name) && is_file($file) && @unlink($file)) { + $removed++; + } + } + $this->clearTarget($newMid); + $report['rows'][] = ['label' => _AM_IMFORUM_IMPORT_FILES, 'source' => '', 'target' => $removed, 'note' => _AM_IMFORUM_IMPORT_NOTE_REMOVED]; + $report['log'][] = _AM_IMFORUM_IMPORT_LOG_EMPTIED; + $report['ok'] = true; + return $report; + } + + /** + * Does the source module exist, are its tables complete and does the target accept the copy? + * + * @param array{ok: bool, errors: list, warnings: list, rows: list, log: list} $report + */ + private function checkStructure(array &$report): void { + $this->present = []; + if ($this->moduleId($this->sourceDirname) === 0) { + $report['errors'][] = sprintf(_AM_IMFORUM_IMPORT_ERR_NO_SOURCE, $this->sourceDirname); + } + if ($this->moduleId(IMFORUM_DIRNAME) === 0) { + $report['errors'][] = _AM_IMFORUM_IMPORT_ERR_NO_TARGET; + } + foreach (self::REQUIRED as $table => $columns) { + if (!$this->tableExists($this->src($table))) { + $report['errors'][] = sprintf(_AM_IMFORUM_IMPORT_ERR_NO_TABLE, $this->src($table)); + continue; + } + $this->present[$table] = true; + $this->checkColumns($report, $table, $columns); + } + foreach (self::OPTIONAL as $table => $columns) { + if (!$this->tableExists($this->src($table))) { + $report['warnings'][] = sprintf(_AM_IMFORUM_IMPORT_WARN_NO_TABLE, $this->src($table)); + continue; + } + $this->present[$table] = true; + $this->checkColumns($report, $table, $columns); + } + foreach (self::TARGETS as $item) { + $collation = (string) (mod_imforum_Db::row('SELECT TABLE_COLLATION FROM information_schema.TABLES WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = ?', [mod_imforum_Db::table($item)])[0] ?? ''); + if ($collation !== '' && !str_starts_with($collation, 'utf8mb4')) { + $report['errors'][] = sprintf(_AM_IMFORUM_IMPORT_ERR_CHARSET, mod_imforum_Db::table($item), $collation); + } + } + } + + /** + * @param array{ok: bool, errors: list, warnings: list, rows: list, log: list} $report + * @param list $columns + */ + private function checkColumns(array &$report, string $table, array $columns): void { + $have = array_map('strval', array_column(mod_imforum_Db::rows('SELECT COLUMN_NAME FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = ?', [$this->src($table)]), 0)); + $missing = array_diff($columns, $have); + if ($missing) { + $report['errors'][] = sprintf(_AM_IMFORUM_IMPORT_ERR_COLUMNS, $this->src($table), implode(', ', $missing)); + } + } + + /** + * Row counts, leftovers in the source and files that are gone + * + * @param array{ok: bool, errors: list, warnings: list, rows: list, log: list} $report + */ + private function checkContent(array &$report): void { + $labels = [ + 'categories' => _AM_IMFORUM_IMPORT_CATEGORIES, 'forums' => _AM_IMFORUM_IMPORT_FORUMS, 'topics' => _AM_IMFORUM_IMPORT_TOPICS, + 'posts' => _AM_IMFORUM_IMPORT_POSTS, 'attachments' => _AM_IMFORUM_IMPORT_ATTACHMENTS, 'report' => _AM_IMFORUM_IMPORT_REPORTS, + 'moderates' => _AM_IMFORUM_IMPORT_SUSPENSIONS, 'reads_forum' => _AM_IMFORUM_IMPORT_READMARKS . ' (forum)', 'reads_topic' => _AM_IMFORUM_IMPORT_READMARKS . ' (topic)', + 'digest' => _AM_IMFORUM_IMPORT_DIGESTS, 'archive' => _AM_IMFORUM_IMPORT_ARCHIVE, + ]; + foreach ($labels as $table => $label) { + if ($this->has($table)) { + $report['rows'][] = ['label' => $label, 'source' => mod_imforum_Db::value('SELECT COUNT(*) FROM ' . $this->src($table)), 'target' => '', 'note' => '']; + } + } + + $full = []; + foreach (self::TARGETS as $item) { + $rows = mod_imforum_Db::value('SELECT COUNT(*) FROM ' . mod_imforum_Db::table($item)); + if ($rows > 0) { + $full[] = mod_imforum_Db::table($item) . ' (' . $rows . ')'; + } + } + if ($full) { + $report['warnings'][] = sprintf(_AM_IMFORUM_IMPORT_WARN_EMPTIED, implode(', ', $full)); + } + + $orphans = [ + _AM_IMFORUM_IMPORT_ORPHAN_POSTS => 'SELECT COUNT(*) FROM ' . $this->src('posts') . ' p LEFT JOIN ' . $this->src('topics') . ' t ON t.topic_id = p.topic_id WHERE t.topic_id IS NULL', + _AM_IMFORUM_IMPORT_ORPHAN_TOPICS => 'SELECT COUNT(*) FROM ' . $this->src('topics') . ' t LEFT JOIN ' . $this->src('forums') . ' f ON f.forum_id = t.forum_id WHERE f.forum_id IS NULL', + _AM_IMFORUM_IMPORT_ORPHAN_FORUMS => 'SELECT COUNT(*) FROM ' . $this->src('forums') . ' f LEFT JOIN ' . $this->src('categories') . ' c ON c.cat_id = f.cat_id WHERE c.cat_id IS NULL', + _AM_IMFORUM_IMPORT_NO_TEXT => 'SELECT COUNT(*) FROM ' . $this->src('posts') . ' p LEFT JOIN ' . $this->src('posts_text') . ' t ON t.post_id = p.post_id WHERE t.post_id IS NULL', + ]; + foreach ($orphans as $label => $sql) { + $n = mod_imforum_Db::value($sql); + if ($n > 0) { + $report['warnings'][] = sprintf(_AM_IMFORUM_IMPORT_WARN_ORPHANS, $n, $label); + } + } + + // text that no longer fits a TEXT column once it is converted + $long = mod_imforum_Db::value('SELECT COUNT(*) FROM ' . $this->src('posts_text') . ' WHERE LENGTH(' . $this->text('post_text') . ') > 65535'); + if ($long > 0) { + $report['errors'][] = sprintf(_AM_IMFORUM_IMPORT_ERR_LONG, $long); + } + + $charset = (string) (mod_imforum_Db::row('SELECT CHARACTER_SET_NAME FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = ? AND COLUMN_NAME = ?', [$this->src('posts_text'), 'post_text'])[0] ?? ''); + $report['rows'][] = ['label' => _AM_IMFORUM_IMPORT_CHARSET, 'source' => $charset, 'target' => 'utf8mb4', 'note' => $this->encoding === self::ENCODING_UTF8_BYTES ? _AM_IMFORUM_IMPORT_NOTE_BYTES : '']; + if ($this->encoding === self::ENCODING_AS_IS && $charset !== '' && !str_starts_with($charset, 'utf8')) { + $suspect = mod_imforum_Db::value('SELECT COUNT(*) FROM ' . $this->src('posts_text') . ' WHERE LOCATE(UNHEX(?), CAST(post_text AS BINARY)) > 0', ['C3']); + if ($suspect > 0) { + $report['warnings'][] = sprintf(_AM_IMFORUM_IMPORT_WARN_ENCODING, $charset, $suspect); + } + } + + $missing = $this->missingSourceFiles(); + if ($missing) { + $report['warnings'][] = sprintf(_AM_IMFORUM_IMPORT_WARN_FILES, count($missing), implode(', ', array_slice($missing, 0, 10))); + } + if (!is_file(XOOPS_CACHE_PATH . '/' . $this->sourceDirname . '_perm_template.php')) { + $report['warnings'][] = _AM_IMFORUM_IMPORT_WARN_NO_TEMPLATE; + } + } + + /** + * @return array{ok: bool, errors: list, warnings: list, rows: list, log: list} + */ + private static function newReport(): array { + return ['ok' => false, 'errors' => [], 'warnings' => [], 'rows' => [], 'log' => []]; + } + + /** + * @param list $warnings + * @return array{source: int, target: int, note: string, warnings: list} + */ + private static function result(int $source, int $target, string $note = '', array $warnings = []): array { + return ['source' => $source, 'target' => $target, 'note' => $note, 'warnings' => $warnings]; + } + + /** + * Run a statement that must work; a failure stops the import with the database's own message + * + * @param list $params + * @throws RuntimeException + */ + private function write(string $sql, array $params = []): int { + $rows = mod_imforum_Db::execute($sql, $params); + if ($rows === null) { + throw new RuntimeException(implode(' ', array_map('strval', mod_imforum_Db::connection()->errorInfo())) . ' | ' . substr($sql, 0, 120)); + } + return $rows; + } + + private function clearTarget(int $newMid): void { + foreach (self::TARGETS as $item) { + mod_imforum_Db::execute('TRUNCATE TABLE ' . mod_imforum_Db::table($item)); + } + mod_imforum_Db::execute('DELETE FROM ' . $this->core('group_permission') . ' WHERE gperm_modid = ? AND gperm_name IN (' . mod_imforum_Db::marks($this->permissionNames()) . ')', array_merge([$newMid], $this->permissionNames())); + mod_imforum_Db::execute('DELETE FROM ' . $this->core('xoopsnotifications') . ' WHERE not_modid = ?', [$newMid]); + } + + /** @return array{source: int, target: int, note: string, warnings: list} */ + private function copyCategories(): array { + $n = $this->write('INSERT INTO ' . mod_imforum_Db::table('category') . ' (cat_id, cat_title, cat_description, cat_image, cat_url, weight, meta_keywords, meta_description, short_url)' + . ' SELECT cat_id, ' . $this->text('cat_title') . ', ' . $this->text('cat_description') . ', cat_image, cat_url, cat_order, \'\', \'\', \'\' FROM ' . $this->src('categories')); + return self::result(mod_imforum_Db::value('SELECT COUNT(*) FROM ' . $this->src('categories')), $n); + } + + /** @return array{source: int, target: int, note: string, warnings: list} */ + private function copyForums(): array { + $n = $this->write('INSERT INTO ' . mod_imforum_Db::table('forum') + . ' (forum_id, cat_id, parent_forum, forum_name, forum_desc, forum_active, weight, allow_html, allow_sig, allow_subject_prefix, hot_threshold, attach_maxkb, attach_ext, forum_topics, forum_posts, forum_last_post_id, meta_keywords, meta_description, short_url)' + . ' SELECT forum_id, cat_id, parent_forum, ' . $this->text('forum_name') . ', ' . $this->text('forum_desc') + . ', IF(forum_type = 0, 1, 0), forum_order, IF(allow_html > 0, 1, 0), allow_sig, allow_subject_prefix, hot_threshold, attach_maxkb, attach_ext, 0, 0, 0, \'\', \'\', \'\' FROM ' . $this->src('forums')); + return self::result(mod_imforum_Db::value('SELECT COUNT(*) FROM ' . $this->src('forums')), $n); + } + + /** + * The moderators sit in one serialized list per forum + * + * @return list forum id and member id + */ + private function moderatorPairs(): array { + $pairs = []; + foreach (mod_imforum_Db::rows('SELECT forum_id, forum_moderator FROM ' . $this->src('forums') . " WHERE forum_moderator <> ''") as $row) { + $list = @unserialize((string) $row[1], ['allowed_classes' => false]); + foreach (is_array($list) ? array_unique(array_map('intval', array_filter($list, 'is_numeric'))) : [] as $uid) { + if ($uid > 0) { + $pairs[] = [(int) $row[0], $uid]; + } + } + } + return $pairs; + } + + /** @return array{source: int, target: int, note: string, warnings: list} */ + private function copyModerators(): array { + $pairs = $this->moderatorPairs(); + foreach (array_chunk($pairs, self::BATCH) as $chunk) { + $this->insertRows('moderator', ['forum_id', 'uid'], $chunk); + } + return self::result(count($pairs), count($pairs)); + } + + /** @return array{source: int, target: int, note: string, warnings: list} */ + private function copyTopics(): array { + $n = $this->write('INSERT INTO ' . mod_imforum_Db::table('topic') + . ' (topic_id, forum_id, topic_title, topic_subject, topic_poster, poster_name, topic_time, topic_views, topic_status, topic_sticky, topic_digest, digest_time, approved, topic_replies, topic_first_post_id, topic_last_post_id, meta_keywords, meta_description, short_url)' + . ' SELECT topic_id, forum_id, ' . $this->text('topic_title') . ', topic_subject, topic_poster, ' . $this->text('poster_name') + . ', topic_time, topic_views, topic_status, topic_sticky, topic_digest, digest_time, IF(approved > 0, 1, 0), topic_replies, 0, topic_last_post_id, \'\', \'\', \'\' FROM ' . $this->src('topics')); + return self::result(mod_imforum_Db::value('SELECT COUNT(*) FROM ' . $this->src('topics')), (int) $n, _AM_IMFORUM_IMPORT_NOTE_DROPPED); + } + + /** @return array{source: int, target: int, note: string, warnings: list} */ + private function copyPosts(): array { + $p = $this->src('posts'); + $n = $this->write('INSERT INTO ' . mod_imforum_Db::table('post') + . ' (post_id, topic_id, forum_id, pid, uid, poster_name, poster_ip, post_time, subject, post_text, post_edit, icon, attachsig, approved, post_karma, require_reply, dohtml, dosmiley, doxcode, doimage, dobr)' + . ' SELECT p.post_id, p.topic_id, p.forum_id, p.pid, p.uid, ' . $this->text('p.poster_name') + . ", IF(p.poster_ip = 0, '', INET_NTOA(IF(p.poster_ip < 0, p.poster_ip + 4294967296, p.poster_ip))), p.post_time, " . $this->text('p.subject') + . ', COALESCE(' . $this->text('t.post_text') . ", ''), '', p.icon, p.attachsig, IF(p.approved > 0, 1, 0), p.post_karma, p.require_reply, p.dohtml, p.dosmiley, p.doxcode, p.doimage, p.dobr" + . " FROM $p p LEFT JOIN " . $this->src('posts_text') . ' t ON t.post_id = p.post_id'); + return self::result(mod_imforum_Db::value("SELECT COUNT(*) FROM $p"), (int) $n, _AM_IMFORUM_IMPORT_NOTE_MERGED_TEXT); + } + + /** + * iForum keeps the edits as a base64 encoded serialized list of name and time; imforum as a JSON list + * + * @return array{source: int, target: int, note: string, warnings: list} + */ + private function copyEdits(): array { + $found = $converted = 0; + $last = 0; + do { + $rows = mod_imforum_Db::rows('SELECT post_id, post_edit FROM ' . $this->src('posts_text') . " WHERE post_edit IS NOT NULL AND post_edit <> '' AND post_id > ? ORDER BY post_id LIMIT " . self::BATCH, [$last]); + foreach ($rows as $row) { + $last = (int) $row[0]; + $found++; + $raw = base64_decode((string) $row[1], true); + $list = $raw === false ? false : @unserialize($raw, ['allowed_classes' => false]); + if (!is_array($list)) { + continue; + } + $edits = []; + foreach ($list as $edit) { + if (is_array($edit) && isset($edit['edit_time'])) { + $name = (string) ($edit['edit_user'] ?? ''); + $edits[] = ['uid' => 0, 'name' => self::asUtf8($name), 'time' => (int) $edit['edit_time']]; + } + } + if ($edits && mod_imforum_Db::ok('UPDATE ' . mod_imforum_Db::table('post') . ' SET post_edit = ? WHERE post_id = ?', [json_encode($edits, JSON_UNESCAPED_UNICODE), $last])) { + $converted++; + } + } + } while (count($rows) === self::BATCH); + + return self::result($found, $converted, '', $found > $converted ? [sprintf(_AM_IMFORUM_IMPORT_WARN_EDITS, $found - $converted)] : []); + } + + /** + * The attachments of all posts: the list inside the post and the table iForum had reserved + * + * Only names that cannot leave the upload folder are accepted. + * + * @return Generator + */ + private function attachments(): Generator { + $last = 0; + do { + $rows = mod_imforum_Db::rows('SELECT post_id, post_time, attachment FROM ' . $this->src('posts') . " WHERE attachment IS NOT NULL AND attachment <> '' AND post_id > ? ORDER BY post_id LIMIT " . self::BATCH, [$last]); + foreach ($rows as $row) { + $last = (int) $row[0]; + $raw = base64_decode((string) $row[2], true); + $list = $raw === false ? false : @unserialize($raw, ['allowed_classes' => false]); + foreach (is_array($list) ? $list : [] as $item) { + if (is_array($item) && self::isSafeName((string) ($item['name_saved'] ?? ''))) { + yield [ + 'post_id' => $last, + 'name_saved' => (string) $item['name_saved'], + 'name_display' => (string) ($item['name_display'] ?? $item['name_saved']), + 'mimetype' => (string) ($item['mimetype'] ?? ''), + 'downloads' => (int) ($item['num_download'] ?? 0), + 'time' => (int) $row[1], + ]; + } + } + } + } while (count($rows) === self::BATCH); + + if ($this->has('attachments')) { + $seen = []; + foreach (mod_imforum_Db::rows('SELECT post_id, name_saved, name_disp, mimetype, download, attach_time FROM ' . $this->src('attachments') . ' ORDER BY attach_id') as $row) { + $name = (string) $row[1]; + if (self::isSafeName($name) && !isset($seen[$row[0] . '/' . $name])) { + $seen[$row[0] . '/' . $name] = true; + yield ['post_id' => (int) $row[0], 'name_saved' => $name, 'name_display' => (string) $row[2], 'mimetype' => (string) $row[3], 'downloads' => (int) $row[4], 'time' => (int) $row[5]]; + } + } + } + } + + private function countAttachments(): int { + $seen = []; + foreach ($this->attachments() as $attachment) { + $seen[$attachment['post_id'] . '/' . $attachment['name_saved']] = true; + } + return count($seen); + } + + /** @return array{source: int, target: int, note: string, warnings: list} */ + private function copyAttachments(): array { + $seen = []; + $batch = []; + $flush = function () use (&$batch): void { + $this->insertRows('attachment', ['post_id', 'name_saved', 'name_display', 'mimetype', 'filesize', 'downloads', 'online', 'attach_time'], $batch); + $batch = []; + }; + foreach ($this->attachments() as $attachment) { + $key = $attachment['post_id'] . '/' . $attachment['name_saved']; + if (isset($seen[$key])) { + continue; + } + $seen[$key] = true; + $batch[] = [ + $attachment['post_id'], $attachment['name_saved'], self::asUtf8($attachment['name_display']), + $attachment['mimetype'], 0, $attachment['downloads'], 1, $attachment['time'], + ]; + if (count($batch) >= self::BATCH) { + $flush(); + } + } + if ($batch) { + $flush(); + } + return self::result(count($seen), count($seen), _AM_IMFORUM_IMPORT_NOTE_FILES_LATER); + } + + /** @return array{source: int, target: int, note: string, warnings: list} */ + private function copyReports(): array { + if (!$this->has('report')) { + return self::result(0, 0, _AM_IMFORUM_IMPORT_NOTE_ABSENT); + } + $n = $this->write('INSERT INTO ' . mod_imforum_Db::table('report') + . ' (report_id, post_id, reporter_uid, reporter_ip, report_time, report_text, report_result, report_memo)' + . " SELECT report_id, post_id, reporter_uid, IF(reporter_ip = 0, '', INET_NTOA(IF(reporter_ip < 0, reporter_ip + 4294967296, reporter_ip))), report_time, " + . $this->text('report_text') . ', IF(report_result > 0, 1, 0), ' . $this->text('report_memo') . ' FROM ' . $this->src('report')); + return self::result(mod_imforum_Db::value('SELECT COUNT(*) FROM ' . $this->src('report')), $n); + } + + /** @return array{source: int, target: int, note: string, warnings: list} */ + private function copySuspensions(): array { + if (!$this->has('moderates')) { + return self::result(0, 0, _AM_IMFORUM_IMPORT_NOTE_ABSENT); + } + $n = $this->write('INSERT INTO ' . mod_imforum_Db::table('suspension') . ' (suspension_id, uid, ip, forum_id, start_time, end_time, description)' + . ' SELECT mod_id, uid, ip, forum_id, mod_start, mod_end, ' . $this->text('mod_desc') . ' FROM ' . $this->src('moderates') . ' WHERE mod_end >= ?', [$this->now]); + return self::result(mod_imforum_Db::value('SELECT COUNT(*) FROM ' . $this->src('moderates')), (int) $n, _AM_IMFORUM_IMPORT_NOTE_EXPIRED); + } + + /** @return array{source: int, target: int, note: string, warnings: list} */ + private function copyReadmarks(): array { + $total = 0; + foreach ([1 => 'reads_forum', 2 => 'reads_topic'] as $type => $table) { + if ($this->has($table)) { + $total += $this->write('INSERT INTO ' . mod_imforum_Db::table('readmark') . ' (read_type, uid, item_id, post_id, read_time)' + . ' SELECT ?, uid, read_item, MAX(post_id), MAX(read_time) FROM ' . $this->src($table) . ' WHERE uid > 0 GROUP BY uid, read_item', [$type]); + } + } + return self::result($this->countReadmarks(), $total, _AM_IMFORUM_IMPORT_NOTE_MERGED); + } + + /** Marks of members, one per member and item */ + private function countReadmarks(): int { + $total = 0; + foreach (['reads_forum', 'reads_topic'] as $table) { + if ($this->has($table)) { + $total += mod_imforum_Db::value('SELECT COUNT(*) FROM (SELECT 1 FROM ' . $this->src($table) . ' WHERE uid > 0 GROUP BY uid, read_item) marks'); + } + } + return $total; + } + + /** @return array{source: int, target: int, note: string, warnings: list} */ + private function copyDigests(): array { + if (!$this->has('digest')) { + return self::result(0, 0, _AM_IMFORUM_IMPORT_NOTE_ABSENT); + } + $n = $this->write('INSERT INTO ' . mod_imforum_Db::table('digest') . ' (digest_id, digest_time, digest_content)' + . ' SELECT digest_id, digest_time, ' . $this->text('digest_content') . ' FROM ' . $this->src('digest')); + return self::result(mod_imforum_Db::value('SELECT COUNT(*) FROM ' . $this->src('digest')), $n); + } + + /** @return array{source: int, target: int, note: string, warnings: list} */ + private function copyArchive(): array { + if (!$this->has('archive')) { + return self::result(0, 0, _AM_IMFORUM_IMPORT_NOTE_ABSENT); + } + $n = $this->write('INSERT INTO ' . mod_imforum_Db::table('archive') . ' (topic_id, post_id, post_text, archived_time)' + . ' SELECT topic_id, post_id, ' . $this->text('post_text') . ', ? FROM ' . $this->src('archive'), [$this->now]); + return self::result(mod_imforum_Db::value('SELECT COUNT(*) FROM ' . $this->src('archive')), $n); + } + + /** + * The permissions iForum and imforum share, now under the id of imforum + * + * The poll permissions are left out. Rights on forums or categories that are gone are not copied. + * + * @return list + */ + private function permissionNames(): array { + return array_merge(mod_imforum_ForumHandler::PERMISSIONS, ['category_access']); + } + + private function countPermissions(int $oldMid): int { + $g = $this->core('group_permission'); + return mod_imforum_Db::value("SELECT COUNT(*) FROM (SELECT DISTINCT gperm_groupid, gperm_itemid, gperm_name FROM $g WHERE gperm_modid = ? AND (" + . '(gperm_name = ? AND gperm_itemid IN (SELECT cat_id FROM ' . $this->src('categories') . '))' + . ' OR (gperm_name IN (' . mod_imforum_Db::marks(mod_imforum_ForumHandler::PERMISSIONS) . ') AND gperm_itemid IN (SELECT forum_id FROM ' . $this->src('forums') . '))' + . ')) rights', array_merge([$oldMid, 'category_access'], mod_imforum_ForumHandler::PERMISSIONS)); + } + + /** @return array{source: int, target: int, note: string, warnings: list} */ + private function copyPermissions(int $oldMid, int $newMid): array { + $g = $this->core('group_permission'); + $columns = " (gperm_groupid, gperm_itemid, gperm_modid, gperm_name) SELECT DISTINCT g.gperm_groupid, g.gperm_itemid, $newMid, g.gperm_name FROM $g g"; + $n = $this->write("INSERT INTO $g" . $columns . ' JOIN ' . mod_imforum_Db::table('category') . ' c ON c.cat_id = g.gperm_itemid WHERE g.gperm_modid = ? AND g.gperm_name = ?', [$oldMid, 'category_access']); + $n += $this->write("INSERT INTO $g" . $columns . ' JOIN ' . mod_imforum_Db::table('forum') . ' f ON f.forum_id = g.gperm_itemid WHERE g.gperm_modid = ? AND g.gperm_name IN (' . mod_imforum_Db::marks(mod_imforum_ForumHandler::PERMISSIONS) . ')', array_merge([$oldMid], mod_imforum_ForumHandler::PERMISSIONS)); + return self::result($this->countPermissions($oldMid), $n, _AM_IMFORUM_IMPORT_NOTE_POLL_RIGHTS); + } + + /** @return array{source: int, target: int, note: string, warnings: list} */ + private function copyNotifications(int $oldMid, int $newMid): array { + $n = $total = 0; + $table = $this->core('xoopsnotifications'); + foreach (self::NOTIFICATIONS as [$category, $event, $kind]) { + $total += mod_imforum_Db::value("SELECT COUNT(*) FROM $table WHERE not_modid = ? AND not_category = ? AND not_event = ?", [$oldMid, $category, $event]); + $join = match ($kind) { + 'topic' => ' JOIN ' . mod_imforum_Db::table('topic') . ' i ON i.topic_id = n.not_itemid', + 'forum' => ' JOIN ' . mod_imforum_Db::table('forum') . ' i ON i.forum_id = n.not_itemid', + default => '', + }; + $n += $this->write("INSERT INTO $table (not_modid, not_itemid, not_category, not_event, not_uid, not_mode)" + . " SELECT $newMid, n.not_itemid, n.not_category, n.not_event, n.not_uid, n.not_mode FROM $table n$join" + . ' WHERE n.not_modid = ? AND n.not_category = ? AND n.not_event = ?', [$oldMid, $category, $event]); + } + $all = mod_imforum_Db::value("SELECT COUNT(*) FROM $table WHERE not_modid = ?", [$oldMid]); + return self::result($all, $n, $all > $total ? _AM_IMFORUM_IMPORT_NOTE_EVENTS : ''); + } + + /** + * The preferences with an equal in imforum + * + * @return array{source: int, target: int, note: string, warnings: list} + */ + private function copySettings(int $oldMid, int $newMid): array { + $config = $this->core('config'); + $old = []; + foreach (mod_imforum_Db::rows("SELECT conf_name, conf_value FROM $config WHERE conf_modid = ?", [$oldMid]) as $row) { + $old[(string) $row[0]] = (string) $row[1]; + } + $new = []; + foreach (self::SAME_SETTINGS as $name) { + if (isset($old[$name])) { + $new[$name] = $old[$name]; + } + } + if (isset($old['rss_maxitems'])) { + $new['rss_items'] = $old['rss_maxitems']; + } + if (isset($old['wol_enabled'])) { + $new['show_online'] = $old['wol_enabled']; + } + if (isset($old['view_mode'])) { + $new['default_view'] = (int) $old['view_mode'] === 2 ? 'tree' : 'flat'; + } + if (isset($old['disc_show'])) { + $new['disclaimer_show'] = (int) $old['disc_show'] > 0 ? 1 : 0; + } + if (isset($old['subject_prefix'])) { + // iForum counts the first entry ("NONE") as prefix 0, imforum starts at the first real one + $entries = explode(',', $old['subject_prefix']); + array_shift($entries); + $new['subject_prefix'] = implode(',', $entries); + } + $done = 0; + foreach ($new as $name => $value) { + // an update that changes nothing reports no row, so ask whether the preference exists + if (mod_imforum_Db::value("SELECT COUNT(*) FROM $config WHERE conf_modid = ? AND conf_name = ?", [$newMid, $name]) > 0 + && mod_imforum_Db::ok("UPDATE $config SET conf_value = ? WHERE conf_modid = ? AND conf_name = ?", [(string) $value, $newMid, $name])) { + $done++; + } + } + return self::result(count($new), $done); + } + + /** + * The permission template iForum kept in a cache file: group id => permission name => allowed + * + * @return array{source: int, target: int, note: string, warnings: list} + */ + private function copyTemplate(): array { + $file = XOOPS_CACHE_PATH . '/' . $this->sourceDirname . '_perm_template.php'; + $template = is_file($file) ? (static fn (string $path): mixed => @include $path)($file) : null; + if (!is_array($template)) { + return self::result(0, 0, _AM_IMFORUM_IMPORT_NOTE_ABSENT); + } + $rows = []; + foreach ($template as $groupId => $permissions) { + foreach (is_array($permissions) ? $permissions : [] as $name => $allowed) { + if ($allowed && in_array($name, mod_imforum_ForumHandler::PERMISSIONS, true) && (int) $groupId > 0) { + $rows[] = [(string) $name, (int) $groupId]; + } + } + } + $this->insertRows('permtemplate', ['perm_name', 'groupid'], $rows); + return self::result(count($rows), count($rows)); + } + + /** + * Copy the files of the attachments next to the other uploads, the sizes come from the copies + * + * @return array{source: int, target: int, note: string, warnings: list} + */ + private function copyFiles(): array { + $from = $this->sourceUploadPath(); + $copied = $total = 0; + $missing = []; + if (!imforum_create_upload_folder()) { + return self::result(0, 0, '', [_AM_IMFORUM_IMPORT_ERR_UPLOAD_FOLDER]); + } + $attachments = mod_imforum_Db::table('attachment'); + foreach (mod_imforum_Db::rows("SELECT DISTINCT name_saved FROM $attachments") as $row) { + $name = (string) $row[0]; + $total++; + $source = $from . $name; + $target = IMFORUM_UPLOAD_PATH . $name; + if (!is_file($target) && is_file($source) && @copy($source, $target)) { + $copied++; + } + if (is_file($target)) { + mod_imforum_Db::ok("UPDATE $attachments SET filesize = ? WHERE name_saved = ?", [(int) filesize($target), $name]); + } else { + $missing[] = $name; + } + } + return self::result($total, $total - count($missing), '', $missing ? [sprintf(_AM_IMFORUM_IMPORT_WARN_FILES, count($missing), implode(', ', array_slice($missing, 0, 10)))] : []); + } + + /** + * Topics and forums that have only posts waiting for approval keep a reference to them + * + * The recount counts approved posts only, which would leave such a topic without a first and last post. + */ + private function keepPendingReferences(): void { + $topic = mod_imforum_Db::table('topic'); + mod_imforum_Db::execute("UPDATE $topic t JOIN (SELECT topic_id, MIN(post_id) first, MAX(post_id) last FROM " . mod_imforum_Db::table('post') . ' GROUP BY topic_id) p ON p.topic_id = t.topic_id' + . ' SET t.topic_first_post_id = p.first, t.topic_last_post_id = p.last WHERE t.topic_first_post_id = 0'); + } + + /** + * Names of attachments without a file in the old upload folder + * + * @return list + */ + private function missingSourceFiles(): array { + $from = $this->sourceUploadPath(); + $missing = []; + foreach ($this->attachments() as $attachment) { + if (!is_file($from . $attachment['name_saved'])) { + $missing[$attachment['name_saved']] = true; + } + } + return array_map('strval', array_keys($missing)); + } + + /** + * Names in a table of imforum that have no file in a folder + * + * @return list + */ + private function missingFiles(string $folder, string $item): array { + $missing = []; + foreach (mod_imforum_Db::rows('SELECT DISTINCT name_saved FROM ' . mod_imforum_Db::table($item)) as $row) { + if (!self::isSafeName((string) $row[0]) || !is_file($folder . $row[0])) { + $missing[] = (string) $row[0]; + } + } + return $missing; + } + + /** + * The upload folder of iForum, with a slash at the end + */ + private function sourceUploadPath(): string { + $config = mod_imforum_Db::row('SELECT conf_value FROM ' . $this->core('config') . ' WHERE conf_modid = ? AND conf_name = ?', [$this->moduleId($this->sourceDirname), 'dir_attachments']); + $folder = trim((string) ($config[0] ?? ''), '/\\'); + if ($folder === '' || str_contains($folder, '..')) { + $folder = 'uploads/' . $this->sourceDirname; + } + return ICMS_ROOT_PATH . '/' . $folder . '/'; + } + + /** + * Posts per group in the source that differ from the target + * + * @return list the group ids with the two counts + */ + private function groupMismatches(string $column, string $source, string $target): array { + $mismatches = []; + $rows = mod_imforum_Db::rows("SELECT s.$column, s.n, COALESCE(t.n, 0) FROM (SELECT $column, COUNT(*) n FROM $source GROUP BY $column) s" + . " LEFT JOIN (SELECT $column, COUNT(*) n FROM $target GROUP BY $column) t ON t.$column = s.$column WHERE s.n <> COALESCE(t.n, 0) LIMIT 50"); + foreach ($rows as $row) { + $mismatches[] = $row[0] . ': ' . $row[1] . '/' . $row[2]; + } + return $mismatches; + } + + /** + * Insert rows in one statement per call + * + * @param list $columns + * @param list> $rows + */ + private function insertRows(string $item, array $columns, array $rows): void { + if (!$rows) { + return; + } + $one = '(' . mod_imforum_Db::marks($columns) . ')'; + $params = []; + foreach ($rows as $row) { + array_push($params, ...$row); + } + $this->write('INSERT INTO ' . mod_imforum_Db::table($item) . ' (' . implode(', ', $columns) . ') VALUES ' . implode(', ', array_fill(0, count($rows), $one)), $params); + } + + /** + * A text column as it is copied + * + * Old XOOPS sites often have UTF-8 text in columns that say latin1; copying those + * as they are turns every accent into two characters. Reading the bytes as UTF-8 + * puts that right. + */ + private function text(string $column): string { + return $this->encoding === self::ENCODING_UTF8_BYTES ? "CONVERT(CAST($column AS BINARY) USING utf8mb4)" : $column; + } + + /** + * A name taken out of a serialized list + * + * It travelled as base64, so it is exactly the bytes iForum wrote: UTF-8 on most sites, + * latin1 on some. imforum stores UTF-8 only. + */ + private static function asUtf8(string $text): string { + return mb_check_encoding($text, 'UTF-8') ? $text : mb_convert_encoding($text, 'UTF-8', 'ISO-8859-1'); + } + + /** + * Names that stay inside the upload folder + */ + private static function isSafeName(string $name): bool { + return preg_match('/^[A-Za-z0-9][A-Za-z0-9_.-]{0,254}$/', $name) === 1 && !str_contains($name, '..'); + } + + private function src(string $table): string { + return XOOPS_DB_PREFIX . '_bb_' . $table; + } + + private function core(string $table): string { + return XOOPS_DB_PREFIX . '_' . $table; + } + + private function has(string $table): bool { + return !empty($this->present[$table]); + } + + private function tableExists(string $table): bool { + return mod_imforum_Db::value('SELECT COUNT(*) FROM information_schema.TABLES WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = ?', [$table]) > 0; + } + + private function moduleId(string $dirname): int { + return mod_imforum_Db::value('SELECT mid FROM ' . $this->core('modules') . ' WHERE dirname = ?', [$dirname]); + } +} diff --git a/src/icms_version.php b/src/icms_version.php index ab3c9cd..d6db963 100644 --- a/src/icms_version.php +++ b/src/icms_version.php @@ -19,8 +19,8 @@ $modversion = [ 'name' => _MI_IMFORUM_NAME, 'version' => '1.0.0', - 'status' => 'Alpha 6', - 'status_version' => '1.0.0 Alpha 6', + 'status' => 'Alpha 7', + 'status_version' => '1.0.0 Alpha 7', 'date' => '25 October 2026', 'description' => _MI_IMFORUM_DESC, 'author' => 'fiammybe', diff --git a/src/include/admin.php b/src/include/admin.php index 21fbf2f..96a0b63 100644 --- a/src/include/admin.php +++ b/src/include/admin.php @@ -142,3 +142,45 @@ function imforum_admin_page(int $menuIndex, string $title, string|Closure $conte function imforum_form_add(icms_form_Base $form, icms_form_Element $element, bool $required = false): void { $form->addElement($element, $required); } + +/** + * The report of the importer as HTML: errors, warnings, the overview and the log + * + * @param array{ok: bool, errors: list, warnings: list, rows: list, log: list} $report + */ +function imforum_import_report_html(array $report): string { + $e = static fn (mixed $text): string => htmlspecialchars((string) $text, ENT_QUOTES, 'UTF-8'); + $html = ''; + if ($report['errors']) { + $html .= '
' . $e(_AM_IMFORUM_IMPORT_RESULT_ERRORS) . '
    '; + foreach ($report['errors'] as $message) { + $html .= '
  • ' . $e($message) . '
  • '; + } + $html .= '
'; + } else { + $html .= '
' . $e(_AM_IMFORUM_IMPORT_RESULT_OK) . '
'; + } + if ($report['warnings']) { + $html .= '
' . $e(_AM_IMFORUM_IMPORT_RESULT_WARNINGS) . '
    '; + foreach ($report['warnings'] as $message) { + $html .= '
  • ' . $e($message) . '
  • '; + } + $html .= '
'; + } + if ($report['rows']) { + $html .= '

' . $e(_AM_IMFORUM_IMPORT_RESULT_ROWS) . '

' + . ''; + foreach ($report['rows'] as $i => $row) { + $html .= ''; + } + $html .= '
' . $e(_AM_IMFORUM_IMPORT_COL_ITEM) . '' . $e(_AM_IMFORUM_IMPORT_COL_SOURCE) . '' . $e(_AM_IMFORUM_IMPORT_COL_TARGET) . '' . $e(_AM_IMFORUM_IMPORT_COL_NOTE) . '
' . $e($row['label']) . '' . $e($row['source']) . '' . $e($row['target']) . '' . $e($row['note']) . '
'; + } + if ($report['log']) { + $html .= '

' . $e(_AM_IMFORUM_IMPORT_RESULT_LOG) . '

    '; + foreach ($report['log'] as $line) { + $html .= '
  • ' . $e($line) . '
  • '; + } + $html .= '
'; + } + return $html; +} diff --git a/src/language/english/admin.php b/src/language/english/admin.php index bc44e8d..0241f10 100644 --- a/src/language/english/admin.php +++ b/src/language/english/admin.php @@ -68,3 +68,90 @@ define('_AM_IMFORUM_RECOUNT_DSC', 'Recounts the topics, posts and last posts of every forum and topic. Useful after an import.'); define('_AM_IMFORUM_RECOUNT_DONE', 'All counters are up to date.'); define('_AM_IMFORUM_RECOUNT_ERROR', 'Recounting failed.'); +define('_AM_IMFORUM_IMPORT', 'Import from iForum'); +define('_AM_IMFORUM_IMPORT_DSC', 'Copies the categories, forums, topics, posts, attachments, permissions and subscriptions of the iForum module into imforum. iForum itself is never changed. Start with the check, which writes nothing.'); +define('_AM_IMFORUM_IMPORT_STEPS', 'Make a database dump of all bb_* tables, group_permission, xoopsnotifications and config, and a copy of the iForum upload folder, close the iForum forums (or put the site in maintenance) and keep iForum installed until the import is verified: uninstalling it drops its tables.'); +define('_AM_IMFORUM_IMPORT_ENCODING', 'Text encoding of the old tables'); +define('_AM_IMFORUM_IMPORT_ENCODING_AS_IS', 'Copy as the database reads it'); +define('_AM_IMFORUM_IMPORT_ENCODING_BYTES', 'The text is UTF-8, even where the old columns say latin1'); +define('_AM_IMFORUM_IMPORT_CHECK', 'Check (nothing is written)'); +define('_AM_IMFORUM_IMPORT_RUN', 'Import'); +define('_AM_IMFORUM_IMPORT_VERIFY', 'Verify'); +define('_AM_IMFORUM_IMPORT_ROLLBACK', 'Roll back'); +define('_AM_IMFORUM_IMPORT_CONFIRM_RUN', 'Empty the imforum tables and import everything again? Everything in them now is lost.'); +define('_AM_IMFORUM_IMPORT_CONFIRM_ROLLBACK', 'Empty the imforum tables and remove the copied attachment files? iForum is not touched.'); +define('_AM_IMFORUM_IMPORT_GO', 'Go ahead'); +define('_AM_IMFORUM_IMPORT_RESULT_OK', 'No errors.'); +define('_AM_IMFORUM_IMPORT_RESULT_ERRORS', 'Errors'); +define('_AM_IMFORUM_IMPORT_RESULT_WARNINGS', 'Warnings'); +define('_AM_IMFORUM_IMPORT_RESULT_ROWS', 'Overview'); +define('_AM_IMFORUM_IMPORT_RESULT_LOG', 'Log'); +define('_AM_IMFORUM_IMPORT_COL_ITEM', 'Item'); +define('_AM_IMFORUM_IMPORT_COL_SOURCE', 'iForum'); +define('_AM_IMFORUM_IMPORT_COL_TARGET', 'imforum'); +define('_AM_IMFORUM_IMPORT_COL_NOTE', 'Note'); +define('_AM_IMFORUM_IMPORT_DONE', 'The import has finished. The check below compares the result with iForum.'); +define('_AM_IMFORUM_IMPORT_ROLLED_BACK', 'The imforum tables are empty again.'); +define('_AM_IMFORUM_IMPORT_CATEGORIES', 'Categories'); +define('_AM_IMFORUM_IMPORT_FORUMS', 'Forums'); +define('_AM_IMFORUM_IMPORT_MODERATORS', 'Moderators'); +define('_AM_IMFORUM_IMPORT_TOPICS', 'Topics'); +define('_AM_IMFORUM_IMPORT_POSTS', 'Posts'); +define('_AM_IMFORUM_IMPORT_EDITS', 'Edit history'); +define('_AM_IMFORUM_IMPORT_ATTACHMENTS', 'Attachments'); +define('_AM_IMFORUM_IMPORT_REPORTS', 'Reports'); +define('_AM_IMFORUM_IMPORT_SUSPENSIONS', 'Suspensions'); +define('_AM_IMFORUM_IMPORT_READMARKS', 'Read marks'); +define('_AM_IMFORUM_IMPORT_DIGESTS', 'Digests'); +define('_AM_IMFORUM_IMPORT_ARCHIVE', 'Archive'); +define('_AM_IMFORUM_IMPORT_PERMISSIONS', 'Group permissions'); +define('_AM_IMFORUM_IMPORT_NOTIFICATIONS', 'Subscriptions'); +define('_AM_IMFORUM_IMPORT_SETTINGS', 'Preferences'); +define('_AM_IMFORUM_IMPORT_TEMPLATE', 'Permission template'); +define('_AM_IMFORUM_IMPORT_FILES', 'Attachment files'); +define('_AM_IMFORUM_IMPORT_FILES_PRESENT', 'Every attachment has its file'); +define('_AM_IMFORUM_IMPORT_CHARSET', 'Character set of the post text'); +define('_AM_IMFORUM_IMPORT_POSTS_PER_FORUM', 'Posts per forum'); +define('_AM_IMFORUM_IMPORT_POSTS_PER_TOPIC', 'Posts per topic'); +define('_AM_IMFORUM_IMPORT_COUNTER_DEVIATIONS', 'Old counters that differ from the recount (forums / topics)'); +define('_AM_IMFORUM_IMPORT_CHECKSUMS', 'Checksum of %d random posts'); +define('_AM_IMFORUM_IMPORT_ORPHAN_POSTS', 'posts without a topic'); +define('_AM_IMFORUM_IMPORT_ORPHAN_TOPICS', 'topics without a forum'); +define('_AM_IMFORUM_IMPORT_ORPHAN_FORUMS', 'forums without a category'); +define('_AM_IMFORUM_IMPORT_NO_TEXT', 'posts without text'); +define('_AM_IMFORUM_IMPORT_SAME', 'same'); +define('_AM_IMFORUM_IMPORT_DIFFERS', 'DIFFERENT'); +define('_AM_IMFORUM_IMPORT_NOTE_ABSENT', 'not present in iForum'); +define('_AM_IMFORUM_IMPORT_NOTE_BYTES', 'read as UTF-8 bytes'); +define('_AM_IMFORUM_IMPORT_NOTE_COUNTERS', 'imforum counts approved posts only; the new numbers are used'); +define('_AM_IMFORUM_IMPORT_NOTE_DROPPED', 'ratings, polls and tags are left out'); +define('_AM_IMFORUM_IMPORT_NOTE_EVENTS', 'events imforum does not have are left out'); +define('_AM_IMFORUM_IMPORT_NOTE_EXPIRED', 'suspensions that have ended are left out'); +define('_AM_IMFORUM_IMPORT_NOTE_FILES_LATER', 'the files follow in the last step'); +define('_AM_IMFORUM_IMPORT_NOTE_MERGED', 'guests are left out, one mark per member and item'); +define('_AM_IMFORUM_IMPORT_NOTE_MERGED_TEXT', 'posts and their texts in one row'); +define('_AM_IMFORUM_IMPORT_NOTE_MISSING', '%d files missing'); +define('_AM_IMFORUM_IMPORT_NOTE_POLL_RIGHTS', 'poll rights are left out'); +define('_AM_IMFORUM_IMPORT_NOTE_REMOVED', 'files removed'); +define('_AM_IMFORUM_IMPORT_LOG_EMPTIED', 'The imforum tables are empty.'); +define('_AM_IMFORUM_IMPORT_LOG_STEP', '%s: %d rows'); +define('_AM_IMFORUM_IMPORT_LOG_RECOUNTED', 'Counters recalculated.'); +define('_AM_IMFORUM_IMPORT_ERR_NO_SOURCE', 'The module "%s" is not installed. The import reads from the tables of a module that is still installed.'); +define('_AM_IMFORUM_IMPORT_ERR_NO_TARGET', 'imforum is not installed.'); +define('_AM_IMFORUM_IMPORT_ERR_NO_TABLE', 'The table %s is missing.'); +define('_AM_IMFORUM_IMPORT_ERR_COLUMNS', 'The table %s lacks the columns: %s.'); +define('_AM_IMFORUM_IMPORT_ERR_CHARSET', 'The table %s is %s, not utf8mb4. Update imforum, then try again.'); +define('_AM_IMFORUM_IMPORT_ERR_LONG', '%d posts are longer than 65,535 bytes once converted.'); +define('_AM_IMFORUM_IMPORT_ERR_COUNT', '%s: %d in iForum, %d in imforum.'); +define('_AM_IMFORUM_IMPORT_ERR_GROUPS', '%s differ (id: iForum/imforum): %s'); +define('_AM_IMFORUM_IMPORT_ERR_TEXT', 'The text of these posts differs: %s'); +define('_AM_IMFORUM_IMPORT_ERR_UPLOAD_FOLDER', 'The upload folder of imforum could not be created.'); +define('_AM_IMFORUM_IMPORT_WARN_NO_TABLE', 'The table %s does not exist and is skipped.'); +define('_AM_IMFORUM_IMPORT_WARN_EMPTIED', 'These imforum tables are emptied first: %s'); +define('_AM_IMFORUM_IMPORT_WARN_ORPHANS', '%d %s in iForum. They are copied as they are.'); +define('_AM_IMFORUM_IMPORT_WARN_ENCODING', 'The post text is %s and %d posts hold the byte C3, which usually means UTF-8 text in latin1 columns. Check a few posts; if they show wrong accents, choose "The text is UTF-8".'); +define('_AM_IMFORUM_IMPORT_WARN_FILES', '%d attachment files are missing: %s'); +define('_AM_IMFORUM_IMPORT_WARN_NO_TEMPLATE', 'There is no cached permission template in iForum; the template of imforum stays empty.'); +define('_AM_IMFORUM_IMPORT_WARN_EDITS', '%d edit histories could not be read and are left out.'); +define('_AM_IMFORUM_IMPORT_WARN_COUNTERS', 'The counters of %d forums and %d topics differed from the recount.'); +define('_AM_IMFORUM_IMPORT_ERR_STEP', 'The step "%s" failed: %s'); diff --git a/src/language/english/modinfo.php b/src/language/english/modinfo.php index 3270b1f..f1066f2 100644 --- a/src/language/english/modinfo.php +++ b/src/language/english/modinfo.php @@ -14,6 +14,7 @@ define('_MI_IMFORUM_MENU_DIGESTS', 'Digests'); define('_MI_IMFORUM_MENU_ARCHIVE', 'Archive'); define('_MI_IMFORUM_MENU_PRUNE', 'Prune and recount'); +define('_MI_IMFORUM_MENU_IMPORT', 'Import from iForum'); define('_MI_IMFORUM_BLOCK_RECENT', 'Latest topics'); define('_MI_IMFORUM_BLOCK_RECENT_DSC', 'The topics with the newest posts'); define('_MI_IMFORUM_BLOCK_HOT', 'Busiest topics'); diff --git a/src/language/nederlands/admin.php b/src/language/nederlands/admin.php index 3c8b000..8d44922 100644 --- a/src/language/nederlands/admin.php +++ b/src/language/nederlands/admin.php @@ -68,3 +68,90 @@ define('_AM_IMFORUM_RECOUNT_DSC', 'Telt de onderwerpen, berichten en laatste berichten van elk forum en onderwerp opnieuw. Handig na een import.'); define('_AM_IMFORUM_RECOUNT_DONE', 'Alle tellers zijn bijgewerkt.'); define('_AM_IMFORUM_RECOUNT_ERROR', 'Hertellen is mislukt.'); +define('_AM_IMFORUM_IMPORT', 'Importeren uit iForum'); +define('_AM_IMFORUM_IMPORT_DSC', 'Kopieert de categorieën, forums, onderwerpen, berichten, bijlagen, rechten en abonnementen van de iForum-module naar imforum. iForum zelf wordt nooit gewijzigd. Begin met de controle, die niets schrijft.'); +define('_AM_IMFORUM_IMPORT_STEPS', 'Maak een databasedump van alle bb_*-tabellen, group_permission, xoopsnotifications en config, en een kopie van de uploadmap van iForum, sluit de iForum-forums (of zet de site in onderhoud) en laat iForum geïnstalleerd tot de import is geverifieerd: bij het deïnstalleren verdwijnen de tabellen.'); +define('_AM_IMFORUM_IMPORT_ENCODING', 'Tekenset van de oude tabellen'); +define('_AM_IMFORUM_IMPORT_ENCODING_AS_IS', 'Kopiëren zoals de database het leest'); +define('_AM_IMFORUM_IMPORT_ENCODING_BYTES', 'De tekst is UTF-8, ook waar de oude kolommen latin1 zeggen'); +define('_AM_IMFORUM_IMPORT_CHECK', 'Controleren (er wordt niets geschreven)'); +define('_AM_IMFORUM_IMPORT_RUN', 'Importeren'); +define('_AM_IMFORUM_IMPORT_VERIFY', 'Verifiëren'); +define('_AM_IMFORUM_IMPORT_ROLLBACK', 'Terugdraaien'); +define('_AM_IMFORUM_IMPORT_CONFIRM_RUN', 'De imforum-tabellen leegmaken en alles opnieuw importeren? Wat er nu in staat gaat verloren.'); +define('_AM_IMFORUM_IMPORT_CONFIRM_ROLLBACK', 'De imforum-tabellen leegmaken en de gekopieerde bijlagebestanden verwijderen? iForum blijft onaangeroerd.'); +define('_AM_IMFORUM_IMPORT_GO', 'Doorgaan'); +define('_AM_IMFORUM_IMPORT_RESULT_OK', 'Geen fouten.'); +define('_AM_IMFORUM_IMPORT_RESULT_ERRORS', 'Fouten'); +define('_AM_IMFORUM_IMPORT_RESULT_WARNINGS', 'Waarschuwingen'); +define('_AM_IMFORUM_IMPORT_RESULT_ROWS', 'Overzicht'); +define('_AM_IMFORUM_IMPORT_RESULT_LOG', 'Logboek'); +define('_AM_IMFORUM_IMPORT_COL_ITEM', 'Onderdeel'); +define('_AM_IMFORUM_IMPORT_COL_SOURCE', 'iForum'); +define('_AM_IMFORUM_IMPORT_COL_TARGET', 'imforum'); +define('_AM_IMFORUM_IMPORT_COL_NOTE', 'Opmerking'); +define('_AM_IMFORUM_IMPORT_DONE', 'De import is klaar. De verificatie hieronder vergelijkt het resultaat met iForum.'); +define('_AM_IMFORUM_IMPORT_ROLLED_BACK', 'De imforum-tabellen zijn weer leeg.'); +define('_AM_IMFORUM_IMPORT_CATEGORIES', 'Categorieën'); +define('_AM_IMFORUM_IMPORT_FORUMS', 'Forums'); +define('_AM_IMFORUM_IMPORT_MODERATORS', 'Moderatoren'); +define('_AM_IMFORUM_IMPORT_TOPICS', 'Onderwerpen'); +define('_AM_IMFORUM_IMPORT_POSTS', 'Berichten'); +define('_AM_IMFORUM_IMPORT_EDITS', 'Bewerkgeschiedenis'); +define('_AM_IMFORUM_IMPORT_ATTACHMENTS', 'Bijlagen'); +define('_AM_IMFORUM_IMPORT_REPORTS', 'Meldingen'); +define('_AM_IMFORUM_IMPORT_SUSPENSIONS', 'Schorsingen'); +define('_AM_IMFORUM_IMPORT_READMARKS', 'Leesmarkeringen'); +define('_AM_IMFORUM_IMPORT_DIGESTS', 'Digests'); +define('_AM_IMFORUM_IMPORT_ARCHIVE', 'Archief'); +define('_AM_IMFORUM_IMPORT_PERMISSIONS', 'Groepsrechten'); +define('_AM_IMFORUM_IMPORT_NOTIFICATIONS', 'Abonnementen'); +define('_AM_IMFORUM_IMPORT_SETTINGS', 'Voorkeuren'); +define('_AM_IMFORUM_IMPORT_TEMPLATE', 'Rechtensjabloon'); +define('_AM_IMFORUM_IMPORT_FILES', 'Bijlagebestanden'); +define('_AM_IMFORUM_IMPORT_FILES_PRESENT', 'Elke bijlage heeft haar bestand'); +define('_AM_IMFORUM_IMPORT_CHARSET', 'Tekenset van de berichttekst'); +define('_AM_IMFORUM_IMPORT_POSTS_PER_FORUM', 'Berichten per forum'); +define('_AM_IMFORUM_IMPORT_POSTS_PER_TOPIC', 'Berichten per onderwerp'); +define('_AM_IMFORUM_IMPORT_COUNTER_DEVIATIONS', 'Oude tellers die afwijken van de hertelling (forums / onderwerpen)'); +define('_AM_IMFORUM_IMPORT_CHECKSUMS', 'Controlesom van %d willekeurige berichten'); +define('_AM_IMFORUM_IMPORT_ORPHAN_POSTS', 'berichten zonder onderwerp'); +define('_AM_IMFORUM_IMPORT_ORPHAN_TOPICS', 'onderwerpen zonder forum'); +define('_AM_IMFORUM_IMPORT_ORPHAN_FORUMS', 'forums zonder categorie'); +define('_AM_IMFORUM_IMPORT_NO_TEXT', 'berichten zonder tekst'); +define('_AM_IMFORUM_IMPORT_SAME', 'gelijk'); +define('_AM_IMFORUM_IMPORT_DIFFERS', 'VERSCHILT'); +define('_AM_IMFORUM_IMPORT_NOTE_ABSENT', 'niet aanwezig in iForum'); +define('_AM_IMFORUM_IMPORT_NOTE_BYTES', 'gelezen als UTF-8-bytes'); +define('_AM_IMFORUM_IMPORT_NOTE_COUNTERS', 'imforum telt alleen goedgekeurde berichten; de nieuwe getallen gelden'); +define('_AM_IMFORUM_IMPORT_NOTE_DROPPED', 'ratings, polls en tags vervallen'); +define('_AM_IMFORUM_IMPORT_NOTE_EVENTS', 'gebeurtenissen die imforum niet kent vervallen'); +define('_AM_IMFORUM_IMPORT_NOTE_EXPIRED', 'afgelopen schorsingen vervallen'); +define('_AM_IMFORUM_IMPORT_NOTE_FILES_LATER', 'de bestanden volgen in de laatste stap'); +define('_AM_IMFORUM_IMPORT_NOTE_MERGED', 'gasten vervallen, één markering per lid en item'); +define('_AM_IMFORUM_IMPORT_NOTE_MERGED_TEXT', 'berichten en hun tekst in één rij'); +define('_AM_IMFORUM_IMPORT_NOTE_MISSING', '%d bestanden ontbreken'); +define('_AM_IMFORUM_IMPORT_NOTE_POLL_RIGHTS', 'pollrechten vervallen'); +define('_AM_IMFORUM_IMPORT_NOTE_REMOVED', 'bestanden verwijderd'); +define('_AM_IMFORUM_IMPORT_LOG_EMPTIED', 'De imforum-tabellen zijn leeg.'); +define('_AM_IMFORUM_IMPORT_LOG_STEP', '%s: %d rijen'); +define('_AM_IMFORUM_IMPORT_LOG_RECOUNTED', 'Tellers opnieuw berekend.'); +define('_AM_IMFORUM_IMPORT_ERR_NO_SOURCE', 'De module "%s" is niet geïnstalleerd. De import leest uit de tabellen van een module die nog geïnstalleerd is.'); +define('_AM_IMFORUM_IMPORT_ERR_NO_TARGET', 'imforum is niet geïnstalleerd.'); +define('_AM_IMFORUM_IMPORT_ERR_NO_TABLE', 'De tabel %s ontbreekt.'); +define('_AM_IMFORUM_IMPORT_ERR_COLUMNS', 'De tabel %s mist de kolommen: %s.'); +define('_AM_IMFORUM_IMPORT_ERR_CHARSET', 'De tabel %s is %s, niet utf8mb4. Werk imforum bij en probeer opnieuw.'); +define('_AM_IMFORUM_IMPORT_ERR_LONG', '%d berichten zijn na omzetting langer dan 65.535 bytes.'); +define('_AM_IMFORUM_IMPORT_ERR_COUNT', '%s: %d in iForum, %d in imforum.'); +define('_AM_IMFORUM_IMPORT_ERR_GROUPS', '%s verschillen (id: iForum/imforum): %s'); +define('_AM_IMFORUM_IMPORT_ERR_TEXT', 'De tekst van deze berichten verschilt: %s'); +define('_AM_IMFORUM_IMPORT_ERR_UPLOAD_FOLDER', 'De uploadmap van imforum kon niet worden aangemaakt.'); +define('_AM_IMFORUM_IMPORT_WARN_NO_TABLE', 'De tabel %s bestaat niet en wordt overgeslagen.'); +define('_AM_IMFORUM_IMPORT_WARN_EMPTIED', 'Deze imforum-tabellen worden eerst leeggemaakt: %s'); +define('_AM_IMFORUM_IMPORT_WARN_ORPHANS', '%d %s in iForum. Ze worden ongewijzigd gekopieerd.'); +define('_AM_IMFORUM_IMPORT_WARN_ENCODING', 'De berichttekst is %s en %d berichten bevatten de byte C3, wat meestal betekent dat UTF-8-tekst in latin1-kolommen staat. Bekijk enkele berichten; staan de accenten fout, kies dan "De tekst is UTF-8".'); +define('_AM_IMFORUM_IMPORT_WARN_FILES', '%d bijlagebestanden ontbreken: %s'); +define('_AM_IMFORUM_IMPORT_WARN_NO_TEMPLATE', 'iForum heeft geen opgeslagen rechtensjabloon; het sjabloon van imforum blijft leeg.'); +define('_AM_IMFORUM_IMPORT_WARN_EDITS', '%d bewerkgeschiedenissen konden niet worden gelezen en vervallen.'); +define('_AM_IMFORUM_IMPORT_WARN_COUNTERS', 'De tellers van %d forums en %d onderwerpen weken af van de hertelling.'); +define('_AM_IMFORUM_IMPORT_ERR_STEP', 'De stap "%s" is mislukt: %s'); diff --git a/src/language/nederlands/modinfo.php b/src/language/nederlands/modinfo.php index 9e5b99c..231c310 100644 --- a/src/language/nederlands/modinfo.php +++ b/src/language/nederlands/modinfo.php @@ -14,6 +14,7 @@ define('_MI_IMFORUM_MENU_DIGESTS', 'Digests'); define('_MI_IMFORUM_MENU_ARCHIVE', 'Archief'); define('_MI_IMFORUM_MENU_PRUNE', 'Opruimen en hertellen'); +define('_MI_IMFORUM_MENU_IMPORT', 'Importeren uit iForum'); define('_MI_IMFORUM_BLOCK_RECENT', 'Laatste onderwerpen'); define('_MI_IMFORUM_BLOCK_RECENT_DSC', 'De onderwerpen met de nieuwste berichten'); define('_MI_IMFORUM_BLOCK_HOT', 'Drukste onderwerpen'); diff --git a/tests/Integration/Database.php b/tests/Integration/Database.php index f71d7c1..e461894 100644 --- a/tests/Integration/Database.php +++ b/tests/Integration/Database.php @@ -13,8 +13,8 @@ */ final class Database { - /** Core tables the IPF handlers touch: the module row and group permissions */ - private const CORE_TABLES = ['modules', 'group_permission']; + /** Core tables the IPF handlers touch: the module row and group permissions; the importer reads the preferences and subscriptions too */ + private const CORE_TABLES = ['modules', 'group_permission', 'config', 'xoopsnotifications']; public static function install(): void { @@ -58,7 +58,14 @@ public static function reset(): void foreach (self::tables('imforum_') as $table) { self::exec("TRUNCATE TABLE `$table`"); } - self::exec('TRUNCATE TABLE `' . icms::$xoopsDB->prefix('group_permission') . '`'); + foreach (['group_permission', 'config', 'xoopsnotifications'] as $table) { + self::exec('TRUNCATE TABLE `' . icms::$xoopsDB->prefix($table) . '`'); + } + // the importer tests leave tables of an old iForum behind + foreach (self::tables('bb_') as $table) { + self::exec("DROP TABLE `$table`"); + } + self::exec('DELETE FROM `' . icms::$xoopsDB->prefix('modules') . "` WHERE dirname <> 'imforum'"); } /** diff --git a/tests/Integration/ImportTest.php b/tests/Integration/ImportTest.php new file mode 100644 index 0000000..20c8d86 --- /dev/null +++ b/tests/Integration/ImportTest.php @@ -0,0 +1,385 @@ +old = OldIforum::fill(ICMS_ROOT_PATH . '/uploads/iforum'); + $this->newMid = Db::value('SELECT mid FROM ' . XOOPS_DB_PREFIX . "_modules WHERE dirname = 'imforum'"); + foreach (['topics_per_page', 'rss_items', 'default_view', 'disclaimer_show', 'subject_prefix'] as $name) { + Db::execute('INSERT INTO ' . XOOPS_DB_PREFIX . '_config (conf_modid, conf_name, conf_value) VALUES (?, ?, ?)', [$this->newMid, $name, 'default']); + } + } + + protected function tearDown(): void + { + foreach (glob($this->old['uploads'] . '/*') ?: [] as $file) { + unlink($file); + } + @rmdir($this->old['uploads']); + foreach (glob(IMFORUM_UPLOAD_PATH . '*') ?: [] as $file) { + unlink($file); + } + @unlink(XOOPS_CACHE_PATH . '/iforum_perm_template.php'); + } + + private function importer(string $encoding = Importer::ENCODING_AS_IS): Importer + { + return new Importer($encoding, $this->old['now']); + } + + /** + * @return array checksum per table of the old module and the core tables it reads + */ + private function sourceChecksums(): array + { + $sums = []; + foreach (Database::tables('bb_') as $table) { + $sums[$table] = (int) Db::row("CHECKSUM TABLE `$table`")[1]; + } + foreach (['config', 'group_permission', 'xoopsnotifications'] as $core) { + $table = XOOPS_DB_PREFIX . '_' . $core; + $sums[$core] = Db::value("SELECT COUNT(*) FROM `$table` WHERE " . ($core === 'config' ? 'conf_modid' : ($core === 'group_permission' ? 'gperm_modid' : 'not_modid')) . ' = ?', [$this->old['mid']]); + } + return $sums; + } + + /** + * @return list> + */ + private static function query(string $sql, array $params = []): array + { + return Db::rows($sql, $params); + } + + public function testTheCheckReportsAndWritesNothing(): void + { + $report = $this->importer()->check(); + + self::assertSame([], $report['errors']); + self::assertTrue($report['ok']); + $warnings = implode(' | ', $report['warnings']); + self::assertStringContainsString('1 posts without a topic', $warnings); + self::assertStringContainsString('1 posts without text', $warnings); + self::assertStringContainsString('gone.zip', $warnings, 'the missing attachment file is named'); + self::assertSame(0, self::rows('post')); + self::assertSame(0, self::rows('forum')); + } + + public function testTheCheckStopsWhenIForumIsNotInstalled(): void + { + Db::execute('DELETE FROM ' . XOOPS_DB_PREFIX . "_modules WHERE dirname = 'iforum'"); + + $report = $this->importer()->check(); + + self::assertFalse($report['ok']); + self::assertStringContainsString('iforum', $report['errors'][0]); + self::assertFalse($this->importer()->run()['ok']); + self::assertSame(0, self::rows('category')); + } + + public function testTheCheckStopsOnAMissingTableOrColumn(): void + { + OldIforum::drop('posts_text'); + Db::execute('ALTER TABLE ' . OldIforum::name('topics') . ' DROP COLUMN topic_sticky'); + + $report = $this->importer()->check(); + + self::assertFalse($report['ok']); + self::assertCount(2, $report['errors']); + self::assertStringContainsString('bb_posts_text', implode(' ', $report['errors'])); + self::assertStringContainsString('topic_sticky', implode(' ', $report['errors'])); + } + + public function testAMissingOptionalTableIsOnlyAWarning(): void + { + OldIforum::drop('archive'); + OldIforum::drop('digest'); + + $report = $this->importer()->run(); + + self::assertTrue($report['ok'], implode(' ', $report['errors'])); + self::assertSame(0, self::rows('archive')); + self::assertSame(0, self::rows('digest')); + } + + public function testEverythingIsCopiedWithItsIds(): void + { + $report = $this->importer()->run(); + + self::assertSame([], $report['errors']); + self::assertSame([3, 7], array_map('intval', array_column(self::query('SELECT cat_id FROM ' . Db::table('category') . ' ORDER BY cat_id'), 0))); + self::assertSame(['General', 'Chat here', 2], array_map(static fn ($v) => is_numeric($v) ? (int) $v : $v, self::query('SELECT cat_title, cat_description, weight FROM ' . Db::table('category') . ' WHERE cat_id = 3')[0])); + + // forums: type 1 means inactive, "checked by default" for HTML becomes plain yes + $forums = []; + foreach (self::query('SELECT forum_id, forum_name, parent_forum, forum_active, allow_html, weight, hot_threshold, attach_ext FROM ' . Db::table('forum') . ' ORDER BY forum_id') as $row) { + $forums[(int) $row[0]] = array_map('strval', $row); + } + self::assertSame(['10', 'Lounge', '0', '1', '1', '4', '8', 'zip|jpg'], $forums[10]); + self::assertSame(['11', 'Archive room', '10', '0', '0', '1', '9', ''], $forums[11]); + + // the moderator list is unserialized, without doubles and without member 0 + self::assertSame([[10, 5], [10, 6], [12, 7]], array_map(static fn (array $r): array => [(int) $r[0], (int) $r[1]], self::query('SELECT forum_id, uid FROM ' . Db::table('moderator') . ' ORDER BY forum_id, uid'))); + + $topic = self::query('SELECT topic_title, topic_poster, topic_views, topic_status, topic_sticky, topic_digest, topic_subject, approved, poster_name FROM ' . Db::table('topic') . ' WHERE topic_id = 100')[0]; + self::assertSame(['First topic', 5, 40, 1, 1, 1, 1, 1, 'Ann'], array_map(static fn ($v) => is_numeric($v) && !preg_match('/[a-z]/i', (string) $v) ? (int) $v : $v, $topic)); + self::assertSame(3, self::rows('topic')); + self::assertSame(6, self::rows('post')); + } + + public function testPostsKeepTheirTextFlagsAndReadableIpAddresses(): void + { + $this->importer()->run(); + $posts = []; + foreach (self::query('SELECT post_id, pid, topic_id, uid, poster_name, poster_ip, subject, post_text, dohtml, approved, post_karma FROM ' . Db::table('post') . ' ORDER BY post_id') as $row) { + $posts[(int) $row[0]] = $row; + } + + self::assertSame('192.168.1.1', $posts[1000][5], 'a negative integer is an address above 127.255.255.255'); + self::assertSame('', $posts[1001][5], 'zero means unknown'); + self::assertSame('1.2.3.4', $posts[1002][5]); + self::assertSame('Hello [b]world[/b] café', $posts[1000][7]); + self::assertSame("Line one\nLine two", $posts[1001][7]); + self::assertSame(1, (int) $posts[1000][8]); + self::assertSame('Guest', $posts[1001][4]); + self::assertSame(1000, (int) $posts[1001][1]); + self::assertSame(0, (int) $posts[1004][9], 'a waiting post stays waiting'); + self::assertSame(50, (int) $posts[1003][10]); + self::assertSame('', $posts[1005][7], 'a post without a text row gets an empty text'); + } + + public function testEditHistoriesBecomeJson(): void + { + $this->importer()->run(); + + $edits = json_decode((string) Db::row('SELECT post_edit FROM ' . Db::table('post') . ' WHERE post_id = 1001')[0], true); + + self::assertSame([['uid' => 0, 'name' => 'Ann', 'time' => $this->old['now'] - 3600], ['uid' => 0, 'name' => 'Bert', 'time' => $this->old['now'] - 60]], $edits); + self::assertSame('', (string) Db::row('SELECT post_edit FROM ' . Db::table('post') . ' WHERE post_id = 1000')[0]); + } + + public function testAttachmentsAreCopiedWithTheirFilesAndOnlySafeNames(): void + { + $report = $this->importer()->run(); + + $rows = []; + foreach (self::query('SELECT post_id, name_saved, name_display, mimetype, filesize, downloads, online FROM ' . Db::table('attachment') . ' ORDER BY name_saved') as $row) { + $rows[(string) $row[1]] = $row; + } + self::assertCount(3, $rows, 'two from the posts and the reserved table; the path that leaves the folder is dropped'); + self::assertArrayNotHasKey('../../mainfile.php', $rows); + self::assertSame(1002, (int) $rows['a1b2c3.jpg'][0]); + self::assertSame('holiday.jpg', $rows['a1b2c3.jpg'][2]); + self::assertSame(4, (int) $rows['a1b2c3.jpg'][5]); + self::assertSame(3, (int) $rows['a1b2c3.jpg'][4], 'the size comes from the copy on disk'); + self::assertSame(12, (int) $rows['reserved.txt'][4]); + self::assertSame(0, (int) $rows['gone.zip'][4]); + self::assertFileExists(IMFORUM_UPLOAD_PATH . 'a1b2c3.jpg'); + self::assertFileExists($this->old['uploads'] . '/a1b2c3.jpg', 'the original stays'); + self::assertFileDoesNotExist(IMFORUM_UPLOAD_PATH . 'gone.zip'); + self::assertStringContainsString('gone.zip', implode(' ', $report['warnings'])); + } + + public function testReportsSuspensionsReadMarksDigestsAndArchive(): void + { + $this->importer()->run(); + + self::assertSame([['1001', '5', '1.2.3.4', 'Rude', '1', 'Handled']], array_map(static fn (array $r): array => array_map('strval', $r), self::query('SELECT post_id, reporter_uid, reporter_ip, report_text, report_result, report_memo FROM ' . Db::table('report')))); + self::assertSame([[9, 'Spam']], array_map(static fn (array $r): array => [(int) $r[0], (string) $r[1]], self::query('SELECT uid, description FROM ' . Db::table('suspension'))), 'a suspension that ended is left out'); + + // guests have no marks and a member has one mark per item + self::assertSame([[1, 5, 10, 1003], [2, 5, 100, 1002], [2, 6, 101, 1003]], array_map(static fn (array $r): array => array_map('intval', $r), self::query('SELECT read_type, uid, item_id, post_id FROM ' . Db::table('readmark') . ' ORDER BY read_type, uid, item_id'))); + self::assertSame('Yesterday', (string) Db::row('SELECT digest_content FROM ' . Db::table('digest'))[0]); + self::assertSame([[55, 555, 'Archived text', $this->old['now']]], array_map(static fn (array $r): array => [(int) $r[0], (int) $r[1], (string) $r[2], (int) $r[3]], self::query('SELECT topic_id, post_id, post_text, archived_time FROM ' . Db::table('archive')))); + } + + public function testPermissionsSubscriptionsPreferencesAndTheTemplate(): void + { + $this->importer()->run(); + $perm = XOOPS_DB_PREFIX . '_group_permission'; + + // the poll right and the rights on things that are gone stay behind + $rights = array_map(static fn (array $r): string => implode('/', array_map('strval', $r)), self::query("SELECT gperm_groupid, gperm_itemid, gperm_name FROM $perm WHERE gperm_modid = ? ORDER BY gperm_name, gperm_groupid, gperm_itemid", [$this->newMid])); + self::assertSame(['2/3/category_access', '1/10/forum_access', '2/10/forum_access', '3/10/forum_access', '1/12/forum_noapprove', '2/10/forum_post', '2/10/forum_view'], $rights); + self::assertSame(10, Db::value("SELECT COUNT(*) FROM $perm WHERE gperm_modid = ?", [$this->old['mid']]), 'the rights of iForum are not touched'); + + $notes = array_map(static fn (array $r): string => implode('/', array_map('strval', $r)), self::query('SELECT not_category, not_itemid, not_event, not_uid FROM ' . XOOPS_DB_PREFIX . '_xoopsnotifications WHERE not_modid = ? ORDER BY not_category, not_itemid', [$this->newMid])); + self::assertSame(['forum/10/new_thread/5', 'global/0/digest/6', 'thread/100/new_post/5'], $notes); + + $config = []; + foreach (self::query('SELECT conf_name, conf_value FROM ' . XOOPS_DB_PREFIX . '_config WHERE conf_modid = ?', [$this->newMid]) as $row) { + $config[(string) $row[0]] = (string) $row[1]; + } + self::assertSame('25', $config['topics_per_page']); + self::assertSame('tree', $config['default_view']); + self::assertSame('Question,Tip', $config['subject_prefix'], 'the first entry (NONE) was prefix 0 in iForum'); + self::assertSame('1', $config['disclaimer_show']); + self::assertSame('15', $config['rss_items'], 'rss_maxitems is called rss_items in imforum'); + + $template = self::query('SELECT perm_name, groupid FROM ' . Db::table('permtemplate') . ' ORDER BY perm_name, groupid'); + self::assertSame([['forum_access', 1], ['forum_access', 2], ['forum_post', 1], ['forum_view', 1]], array_map(static fn (array $r): array => [(string) $r[0], (int) $r[1]], $template), 'group 0 and the poll right are dropped'); + } + + public function testCountersAreRecountedAndPendingTopicsKeepTheirPosts(): void + { + $this->importer()->run(); + + $forum = self::query('SELECT forum_topics, forum_posts, forum_last_post_id FROM ' . Db::table('forum') . ' WHERE forum_id = 10')[0]; + self::assertSame([2, 5, 1005], array_map('intval', $forum), 'the 99 of the old data is not taken over; the post of the missing topic counts for its forum'); + $topic = self::query('SELECT topic_replies, topic_first_post_id, topic_last_post_id FROM ' . Db::table('topic') . ' WHERE topic_id = 100')[0]; + self::assertSame([2, 1000, 1002], array_map('intval', $topic)); + + $waiting = self::query('SELECT topic_first_post_id, topic_last_post_id FROM ' . Db::table('topic') . ' WHERE topic_id = 102')[0]; + self::assertSame([1004, 1004], array_map('intval', $waiting), 'a topic that waits for approval still points at its post'); + } + + public function testVerifyConfirmsTheCopyAndNoticesDamage(): void + { + $importer = $this->importer(); + $importer->run(); + + $report = $importer->verify(); + self::assertSame([], $report['errors']); + self::assertTrue($report['ok']); + self::assertNotEmpty($report['rows']); + + Db::execute('DELETE FROM ' . Db::table('post') . ' WHERE post_id = 1002'); + Db::execute('UPDATE ' . Db::table('post') . " SET post_text = 'changed'"); + $report = $importer->verify(); + self::assertFalse($report['ok']); + $errors = implode(' | ', $report['errors']); + self::assertStringContainsString('Posts: 6 in iForum, 5 in imforum', $errors); + self::assertStringContainsString('The text of these posts differs: 1000', $errors); + } + + public function testTheSourceIsNeverChanged(): void + { + $before = $this->sourceChecksums(); + $importer = $this->importer(); + $importer->check(); + $importer->run(); + $importer->verify(); + $importer->rollback(); + + self::assertSame($before, $this->sourceChecksums()); + self::assertFileExists($this->old['uploads'] . '/a1b2c3.jpg'); + } + + public function testRunningTwiceGivesTheSameResult(): void + { + $importer = $this->importer(); + $importer->run(); + $first = [self::rows('post'), self::rows('attachment'), self::rows('readmark'), self::rows('moderator'), Db::value('SELECT COUNT(*) FROM ' . XOOPS_DB_PREFIX . '_group_permission WHERE gperm_modid = ?', [$this->newMid])]; + + $report = $importer->run(); + + self::assertTrue($report['ok']); + self::assertSame($first, [self::rows('post'), self::rows('attachment'), self::rows('readmark'), self::rows('moderator'), Db::value('SELECT COUNT(*) FROM ' . XOOPS_DB_PREFIX . '_group_permission WHERE gperm_modid = ?', [$this->newMid])]); + self::assertTrue($importer->verify()['ok']); + } + + public function testRollbackEmptiesImforumAndLeavesIForumAlone(): void + { + $this->importer()->run(); + $posts = Db::value('SELECT COUNT(*) FROM ' . OldIforum::name('posts')); + + $report = $this->importer()->rollback(); + + self::assertTrue($report['ok']); + foreach (['category', 'forum', 'moderator', 'topic', 'post', 'attachment', 'report', 'suspension', 'readmark', 'digest', 'archive', 'permtemplate'] as $item) { + self::assertSame(0, self::rows($item), $item); + } + self::assertSame(0, Db::value('SELECT COUNT(*) FROM ' . XOOPS_DB_PREFIX . '_group_permission WHERE gperm_modid = ?', [$this->newMid])); + self::assertSame(0, Db::value('SELECT COUNT(*) FROM ' . XOOPS_DB_PREFIX . '_xoopsnotifications WHERE not_modid = ?', [$this->newMid])); + self::assertFileDoesNotExist(IMFORUM_UPLOAD_PATH . 'a1b2c3.jpg'); + self::assertFileExists($this->old['uploads'] . '/a1b2c3.jpg'); + self::assertSame($posts, Db::value('SELECT COUNT(*) FROM ' . OldIforum::name('posts'))); + self::assertSame(10, Db::value('SELECT COUNT(*) FROM ' . XOOPS_DB_PREFIX . '_group_permission WHERE gperm_modid = ?', [$this->old['mid']])); + } + + public function testRollbackOnlyRemovesFilesInTheUploadFolder(): void + { + $this->importer()->run(); + $outside = ICMS_ROOT_PATH . '/uploads/precious.txt'; + file_put_contents($outside, 'keep'); + Db::execute('UPDATE ' . Db::table('attachment') . " SET name_saved = '../precious.txt' WHERE name_saved = 'reserved.txt'"); + + $this->importer()->rollback(); + + self::assertFileExists($outside); + unlink($outside); + } + + public function testUtf8StoredInLatin1ColumnsCanBeReadAsBytes(): void + { + foreach (['posts_text', 'posts', 'topics', 'forums', 'categories'] as $table) { + Db::execute('ALTER TABLE ' . OldIforum::name($table) . ' CONVERT TO CHARACTER SET latin1'); + } + // the bytes of "café" in UTF-8, written into a latin1 column + Db::execute('UPDATE ' . OldIforum::name('posts_text') . " SET post_text = CONCAT('caf', CAST(UNHEX('C3A9') AS CHAR CHARACTER SET latin1)) WHERE post_id = 1003"); + Db::execute('UPDATE ' . OldIforum::name('topics') . " SET topic_title = CONCAT('Caf', CAST(UNHEX('C3A9') AS CHAR CHARACTER SET latin1)) WHERE topic_id = 101"); + + $check = $this->importer()->check(); + self::assertStringContainsString('latin1', implode(' ', $check['warnings']), 'the check suspects UTF-8 bytes'); + + $this->importer(Importer::ENCODING_AS_IS)->run(); + self::assertSame('café', (string) Db::row('SELECT post_text FROM ' . Db::table('post') . ' WHERE post_id = 1003')[0]); + + $importer = $this->importer(Importer::ENCODING_UTF8_BYTES); + self::assertTrue($importer->run()['ok']); + self::assertSame('café', (string) Db::row('SELECT post_text FROM ' . Db::table('post') . ' WHERE post_id = 1003')[0]); + self::assertSame('Café', (string) Db::row('SELECT topic_title FROM ' . Db::table('topic') . ' WHERE topic_id = 101')[0]); + self::assertTrue($importer->verify()['ok'], implode(' ', $importer->verify()['errors'])); + } + + public function testALargeInstallationImportsInOneGo(): void + { + $posts = 43778; + $perTopic = 9; + $topics = (int) ceil($posts / $perTopic); + Db::execute('CREATE TABLE ' . XOOPS_DB_PREFIX . '_bb_digits (d int NOT NULL)'); + Db::execute('INSERT INTO ' . XOOPS_DB_PREFIX . '_bb_digits VALUES (0),(1),(2),(3),(4),(5),(6),(7),(8),(9)'); + $numbers = '(SELECT a.d + b.d * 10 + c.d * 100 + e.d * 1000 + f.d * 10000 + 1 AS n FROM ' . XOOPS_DB_PREFIX . '_bb_digits a, ' . XOOPS_DB_PREFIX . '_bb_digits b, ' + . XOOPS_DB_PREFIX . '_bb_digits c, ' . XOOPS_DB_PREFIX . '_bb_digits e, ' . XOOPS_DB_PREFIX . '_bb_digits f) x'; + foreach (['posts', 'posts_text', 'topics', 'attachments', 'reads_topic', 'reads_forum'] as $table) { + Db::execute('TRUNCATE TABLE ' . OldIforum::name($table)); + } + Db::execute('INSERT INTO ' . OldIforum::name('topics') . " (topic_id, topic_title, topic_poster, topic_time, forum_id, topic_replies, topic_last_post_id) SELECT n, CONCAT('Topic ', n), 5, 1700000000 + n, 10 + n % 3, 8, 0 FROM $numbers WHERE n <= ?", [$topics]); + Db::execute('INSERT INTO ' . OldIforum::name('posts') . " (post_id, pid, topic_id, forum_id, post_time, uid, subject, poster_ip) SELECT n, 0, FLOOR((n - 1) / $perTopic) + 1, 10 + FLOOR((n - 1) / $perTopic) % 3 + 1 - 1, 1700000000 + n, 5, CONCAT('Post ', n), 0 FROM $numbers WHERE n <= ?", [$posts]); + Db::execute('INSERT INTO ' . OldIforum::name('posts_text') . " (post_id, post_text) SELECT n, CONCAT('Text of post ', n, REPEAT(' lorem ipsum', 20)) FROM $numbers WHERE n <= ?", [$posts]); + Db::execute('UPDATE ' . OldIforum::name('posts') . ' SET forum_id = 10 + (topic_id % 3)'); + Db::execute('UPDATE ' . OldIforum::name('topics') . ' SET forum_id = 10 + (topic_id % 3)'); + Db::execute('INSERT INTO ' . OldIforum::name('reads_topic') . " (uid, read_time, read_item, post_id) SELECT 5, 1700000000, n, n FROM $numbers WHERE n <= ?", [$topics]); + Db::execute('DROP TABLE ' . XOOPS_DB_PREFIX . '_bb_digits'); + + $importer = $this->importer(); + $started = microtime(true); + $report = $importer->run(); + $seconds = microtime(true) - $started; + + self::assertSame([], $report['errors']); + self::assertSame($posts, self::rows('post')); + self::assertSame($topics, self::rows('topic')); + $verify = $importer->verify(); + self::assertSame([], $verify['errors']); + self::assertLessThan(120, $seconds, 'the import of the largest installation takes seconds, not minutes'); + } +} diff --git a/tests/Integration/OldIforum.php b/tests/Integration/OldIforum.php new file mode 100644 index 0000000..0b9cc1b --- /dev/null +++ b/tests/Integration/OldIforum.php @@ -0,0 +1,185 @@ + "cat_id smallint(3) unsigned NOT NULL auto_increment, cat_image varchar(50) NOT NULL default '', cat_title varchar(100) NOT NULL default '', cat_description text NOT NULL, cat_order smallint(3) unsigned NOT NULL default '0', cat_url varchar(255) NOT NULL default '', PRIMARY KEY (cat_id)", + 'forums' => "forum_id smallint(4) unsigned NOT NULL auto_increment, forum_name varchar(150) NOT NULL default '', forum_desc text, parent_forum smallint(4) unsigned NOT NULL default '0', forum_moderator varchar(255) NOT NULL default '', forum_topics int(8) unsigned NOT NULL default '0', forum_posts int(10) unsigned NOT NULL default '0', forum_last_post_id int(10) unsigned NOT NULL default '0', cat_id smallint(3) unsigned NOT NULL default '0', forum_type tinyint(1) unsigned NOT NULL default '0', allow_html tinyint(1) unsigned NOT NULL default '1', allow_sig tinyint(1) unsigned NOT NULL default '1', allow_subject_prefix tinyint(1) unsigned NOT NULL default '0', hot_threshold tinyint(3) unsigned NOT NULL default '10', forum_order smallint(4) unsigned NOT NULL default '0', attach_maxkb smallint(3) unsigned NOT NULL default '1000', attach_ext varchar(255) NOT NULL default '', allow_polls tinyint(1) unsigned NOT NULL default '0', PRIMARY KEY (forum_id)", + 'topics' => "topic_id int(8) unsigned NOT NULL auto_increment, topic_title varchar(255) NOT NULL default '', topic_poster int(10) unsigned NOT NULL default '0', topic_time int(10) unsigned NOT NULL default '0', topic_views int(10) unsigned NOT NULL default '0', topic_replies mediumint(8) unsigned NOT NULL default '0', topic_last_post_id int(8) unsigned NOT NULL default '0', forum_id smallint(4) unsigned NOT NULL default '0', topic_status tinyint(1) unsigned NOT NULL default '0', topic_subject smallint(3) unsigned NOT NULL default '0', topic_sticky tinyint(1) unsigned NOT NULL default '0', topic_digest tinyint(1) unsigned NOT NULL default '0', digest_time int(10) unsigned NOT NULL default '0', approved tinyint(2) NOT NULL default '1', poster_name varchar(255) NOT NULL default '', rating double(6,4) NOT NULL default '0.0000', votes int(11) unsigned NOT NULL default '0', topic_haspoll tinyint(1) unsigned NOT NULL default '0', poll_id mediumint(8) unsigned NOT NULL default '0', topic_tags varchar(255) NOT NULL default '', PRIMARY KEY (topic_id)", + 'posts' => "post_id int(10) unsigned NOT NULL auto_increment, pid int(10) unsigned NOT NULL default '0', topic_id int(8) unsigned NOT NULL default '0', forum_id smallint(4) unsigned NOT NULL default '0', post_time int(10) unsigned NOT NULL default '0', uid int(10) unsigned NOT NULL default '0', poster_name varchar(255) NOT NULL default '', poster_ip int(11) NOT NULL default '0', subject varchar(255) NOT NULL default '', dohtml tinyint(1) unsigned NOT NULL default '0', dosmiley tinyint(1) unsigned NOT NULL default '1', doxcode tinyint(1) unsigned NOT NULL default '1', dobr tinyint(1) unsigned NOT NULL default '1', doimage tinyint(1) unsigned NOT NULL default '1', icon varchar(25) NOT NULL default '', attachsig tinyint(1) unsigned NOT NULL default '0', approved smallint(2) NOT NULL default '1', post_karma int(10) unsigned NOT NULL default '0', attachment text, require_reply tinyint(1) unsigned NOT NULL default '0', PRIMARY KEY (post_id), KEY topic_id (topic_id)", + 'posts_text' => 'post_id int(10) unsigned NOT NULL default 0, post_text text, post_edit text, PRIMARY KEY (post_id)', + 'attachments' => "attach_id int(8) unsigned NOT NULL auto_increment, post_id int(10) unsigned NOT NULL default '0', name_saved varchar(255) NOT NULL default '', name_disp varchar(255) NOT NULL default '', mimetype varchar(255) NOT NULL default '', online tinyint(1) unsigned NOT NULL default '1', attach_time int(10) unsigned NOT NULL default '0', download int(10) unsigned NOT NULL default '0', PRIMARY KEY (attach_id)", + 'report' => "report_id int(8) unsigned NOT NULL auto_increment, post_id int(10) unsigned NOT NULL default '0', reporter_uid int(10) unsigned NOT NULL default '0', reporter_ip int(11) NOT NULL default '0', report_time int(10) unsigned NOT NULL default '0', report_text varchar(255) NOT NULL default '', report_result tinyint(1) unsigned NOT NULL default '0', report_memo varchar(255) NOT NULL default '', PRIMARY KEY (report_id)", + 'moderates' => "mod_id int(10) unsigned NOT NULL auto_increment, mod_start int(10) unsigned NOT NULL default '0', mod_end int(10) unsigned NOT NULL default '0', mod_desc varchar(255) NOT NULL default '', uid int(10) unsigned NOT NULL default '0', ip varchar(32) NOT NULL default '', forum_id smallint(4) unsigned NOT NULL default '0', PRIMARY KEY (mod_id)", + 'reads_forum' => "read_id int(10) unsigned NOT NULL auto_increment, uid int(10) unsigned NOT NULL default '0', read_time int(10) unsigned NOT NULL default '0', read_item smallint(4) unsigned NOT NULL default '0', post_id int(10) unsigned NOT NULL default '0', PRIMARY KEY (read_id)", + 'reads_topic' => "read_id int(10) unsigned NOT NULL auto_increment, uid int(10) unsigned NOT NULL default '0', read_time int(10) unsigned NOT NULL default '0', read_item int(8) unsigned NOT NULL default '0', post_id int(10) unsigned NOT NULL default '0', PRIMARY KEY (read_id)", + 'digest' => 'digest_id int(8) unsigned NOT NULL auto_increment, digest_time int(10) unsigned NOT NULL default 0, digest_content text, PRIMARY KEY (digest_id)', + 'archive' => 'topic_id int(8) unsigned NOT NULL default 0, post_id int(10) unsigned NOT NULL default 0, post_text text NOT NULL', + ]; + + /** + * @param string $charset the character set of the text columns: old sites often still have latin1 + */ + public static function create(string $charset = 'utf8mb4'): void + { + foreach (self::TABLES as $table => $columns) { + Database::exec('CREATE TABLE `' . self::name($table) . "` ($columns) DEFAULT CHARSET=$charset"); + } + } + + public static function name(string $table): string + { + return XOOPS_DB_PREFIX . '_bb_' . $table; + } + + public static function drop(string $table): void + { + Database::exec('DROP TABLE `' . self::name($table) . '`'); + } + + /** + * Register the old module the way the core does, and return its id + */ + public static function register(): int + { + Database::exec(sprintf( + "INSERT INTO %s (name, version, isactive, dirname, hasmain, hasadmin, dbversion, modname, ipf) VALUES ('iForum', '2.4', 1, '%s', 1, 1, 0, '%s', 0)", + XOOPS_DB_PREFIX . '_modules', + self::MODULE, + self::MODULE + )); + return Db::value('SELECT mid FROM ' . XOOPS_DB_PREFIX . '_modules WHERE dirname = ?', [self::MODULE]); + } + + /** + * Insert rows into a bb_* table + * + * @param array $row + */ + public static function insert(string $table, array $row): void + { + Db::execute( + 'INSERT INTO ' . self::name($table) . ' (' . implode(', ', array_keys($row)) . ') VALUES (' . Db::marks($row) . ')', + array_values($row) + ); + } + + /** + * A small installation that has a bit of everything + * + * @return array{mid: int, uploads: string, now: int} + */ + public static function fill(string $uploads): array + { + $now = time(); + $mid = self::register(); + $edits = base64_encode(serialize([['edit_user' => 'Ann', 'edit_time' => $now - 3600], ['edit_user' => 'Bert', 'edit_time' => $now - 60]])); + $attachments = base64_encode(serialize([ + '1700000000' => ['name_saved' => 'a1b2c3.jpg', 'name_display' => 'holiday.jpg', 'mimetype' => 'image/jpeg', 'num_download' => 4], + '1700000001' => ['name_saved' => 'gone.zip', 'name_display' => 'old.zip', 'mimetype' => 'application/zip', 'num_download' => 0], + '1700000002' => ['name_saved' => '../../mainfile.php', 'name_display' => 'evil.php', 'mimetype' => 'text/plain', 'num_download' => 0], + ])); + + foreach ([[3, 'General', 'Chat here', 2], [7, 'Support', '', 1]] as [$id, $title, $description, $order]) { + self::insert('categories', ['cat_id' => $id, 'cat_title' => $title, 'cat_description' => $description, 'cat_order' => $order]); + } + foreach ([ + [10, 'Lounge', 'Welcome', 0, serialize([5, 6, 6, 0]), 3, 0, 2, 'zip|jpg', 4, 8], + [11, 'Archive room', '', 10, '', 3, 1, 0, '', 1, 9], + [12, 'Help', 'Ask', 0, serialize([7]), 7, 0, 1, 'zip', 2, 5], + ] as [$id, $name, $description, $parent, $moderators, $cat, $type, $html, $ext, $order, $threshold]) { + self::insert('forums', [ + 'forum_id' => $id, 'forum_name' => $name, 'forum_desc' => $description, 'parent_forum' => $parent, 'forum_moderator' => $moderators, 'cat_id' => $cat, + 'forum_type' => $type, 'allow_html' => $html, 'attach_ext' => $ext, 'forum_order' => $order, 'hot_threshold' => $threshold, + 'forum_posts' => 99, 'forum_topics' => 99, + ]); + } + foreach ([ + [100, 'First topic', 5, $now - 5000, 40, 2, 1002, 10, 1, 1, 1, 1, 1, 'Ann'], + [101, 'Second topic', 6, $now - 4000, 3, 0, 1003, 10, 0, 0, 0, 0, 1, ''], + [102, 'Waiting topic', 0, $now - 3000, 0, 0, 1004, 12, 0, 0, 0, 0, 0, 'Guest'], + ] as [$id, $title, $poster, $time, $views, $replies, $last, $forum, $status, $subject, $sticky, $digest, $approved, $name]) { + self::insert('topics', [ + 'topic_id' => $id, 'topic_title' => $title, 'topic_poster' => $poster, 'topic_time' => $time, 'topic_views' => $views, 'topic_replies' => $replies, + 'topic_last_post_id' => $last, 'forum_id' => $forum, 'topic_status' => $status, 'topic_subject' => $subject, 'topic_sticky' => $sticky, + 'topic_digest' => $digest, 'digest_time' => $digest ? $now - 100 : 0, 'approved' => $approved, 'poster_name' => $name, 'rating' => 3.5, 'votes' => 2, + ]); + } + foreach ([ + // post, pid, topic, forum, time, uid, name, ip, subject, html, approved, attachment, text, edit + [1000, 0, 100, 10, $now - 5000, 5, '', -1062731519, 'First topic', 1, 1, '', 'Hello [b]world[/b] café', null], + [1001, 1000, 100, 10, $now - 4900, 0, 'Guest', 0, 'Re: First topic', 0, 1, '', "Line one\nLine two", $edits], + [1002, 1001, 100, 10, $now - 4800, 6, '', 16909060, 'Re: First topic', 0, 1, $attachments, 'With a file', null], + [1003, 0, 101, 10, $now - 4000, 6, '', 0, 'Second topic', 0, 1, '', 'Alone', null], + [1004, 0, 102, 12, $now - 3000, 0, 'Guest', 0, 'Waiting topic', 0, 0, '', 'Needs approval', null], + ] as [$id, $pid, $topic, $forum, $time, $uid, $name, $ip, $subject, $html, $approved, $attachment, $text, $edit]) { + self::insert('posts', [ + 'post_id' => $id, 'pid' => $pid, 'topic_id' => $topic, 'forum_id' => $forum, 'post_time' => $time, 'uid' => $uid, 'poster_name' => $name, 'poster_ip' => $ip, + 'subject' => $subject, 'dohtml' => $html, 'approved' => $approved, 'attachment' => $attachment, 'post_karma' => $id === 1003 ? 50 : 0, + ]); + self::insert('posts_text', ['post_id' => $id, 'post_text' => $text, 'post_edit' => $edit]); + } + // a post of a topic that is gone, and one without text row + self::insert('posts', ['post_id' => 1005, 'topic_id' => 999, 'forum_id' => 10, 'post_time' => $now, 'subject' => 'Orphan']); + + self::insert('attachments', ['post_id' => 1003, 'name_saved' => 'reserved.txt', 'name_disp' => 'notes.txt', 'mimetype' => 'text/plain', 'attach_time' => $now, 'download' => 2]); + self::insert('report', ['post_id' => 1001, 'reporter_uid' => 5, 'reporter_ip' => 16909060, 'report_time' => $now - 10, 'report_text' => 'Rude', 'report_result' => 1, 'report_memo' => 'Handled']); + self::insert('moderates', ['mod_start' => $now - 100, 'mod_end' => $now + 86400, 'mod_desc' => 'Spam', 'uid' => 9, 'ip' => '', 'forum_id' => 10]); + self::insert('moderates', ['mod_start' => $now - 900, 'mod_end' => $now - 800, 'mod_desc' => 'Over', 'uid' => 8, 'ip' => '', 'forum_id' => 10]); + foreach ([[5, 10, 1002], [5, 10, 1003], [0, 10, 1003]] as [$uid, $item, $post]) { + self::insert('reads_forum', ['uid' => $uid, 'read_time' => $now - $post, 'read_item' => $item, 'post_id' => $post]); + } + foreach ([[5, 100, 1001], [5, 100, 1002], [6, 101, 1003]] as [$uid, $item, $post]) { + self::insert('reads_topic', ['uid' => $uid, 'read_time' => $now - $post, 'read_item' => $item, 'post_id' => $post]); + } + self::insert('digest', ['digest_time' => $now - 86400, 'digest_content' => 'Yesterday']); + self::insert('archive', ['topic_id' => 55, 'post_id' => 555, 'post_text' => 'Archived text']); + + // preferences, permissions and subscriptions live in core tables + $config = XOOPS_DB_PREFIX . '_config'; + foreach ([ + [$mid, 'dir_attachments', 'uploads/' . basename($uploads)], [$mid, 'topics_per_page', '25'], [$mid, 'rss_maxitems', '15'], [$mid, 'view_mode', '2'], + [$mid, 'disc_show', '3'], [$mid, 'subject_prefix', 'NONE,Question,Tip'], + ] as [$modid, $name, $value]) { + Db::execute("INSERT INTO $config (conf_modid, conf_name, conf_value) VALUES (?, ?, ?)", [$modid, $name, $value]); + } + $permission = XOOPS_DB_PREFIX . '_group_permission'; + foreach ([ + [1, 10, 'forum_access'], [2, 10, 'forum_access'], [3, 10, 'forum_access'], [2, 10, 'forum_view'], [2, 10, 'forum_post'], + [2, 10, 'forum_addpoll'], [2, 99, 'forum_access'], [2, 3, 'category_access'], [2, 55, 'category_access'], [1, 12, 'forum_noapprove'], + ] as [$group, $item, $name]) { + Db::execute("INSERT INTO $permission (gperm_groupid, gperm_itemid, gperm_modid, gperm_name) VALUES (?, ?, ?, ?)", [$group, $item, $mid, $name]); + } + $notification = XOOPS_DB_PREFIX . '_xoopsnotifications'; + foreach ([ + ['thread', 100, 'new_post', 5], ['thread', 999, 'new_post', 5], ['forum', 10, 'new_thread', 5], ['global', 0, 'digest', 6], + ['global', 0, 'new_fullpost', 6], ['forum', 10, 'new_forum', 6], + ] as [$category, $item, $event, $uid]) { + Db::execute("INSERT INTO $notification (not_modid, not_itemid, not_category, not_event, not_uid) VALUES (?, ?, ?, ?, ?)", [$mid, $item, $category, $event, $uid]); + } + + // the files of iForum and its cached permission template + @mkdir($uploads, 0755, true); + file_put_contents($uploads . '/a1b2c3.jpg', 'abc'); + file_put_contents($uploads . '/reserved.txt', 'twelve bytes'); + file_put_contents( + XOOPS_CACHE_PATH . '/' . self::MODULE . '_perm_template.php', + " ['forum_access' => 1, 'forum_view' => 1, 'forum_post' => 1], 2 => ['forum_access' => 1, 'forum_view' => 0, 'forum_addpoll' => 1], 0 => ['forum_access' => 1]], true) . ";\n" + ); + return ['mid' => $mid, 'uploads' => $uploads, 'now' => $now]; + } +} diff --git a/tests/Unit/ImportReportTest.php b/tests/Unit/ImportReportTest.php new file mode 100644 index 0000000..4a65470 --- /dev/null +++ b/tests/Unit/ImportReportTest.php @@ -0,0 +1,44 @@ + false, + 'errors' => [''], + 'warnings' => ['gone.zip'], + 'rows' => [['label' => 'Posts', 'source' => 3, 'target' => 2, 'note' => '"x"']], + 'log' => [''], + ]); + + self::assertStringNotContainsString('