Importing a CSV file of Topics and Replies to a PHPBB 3.3.12 Board | Chat GPT PHP Script

Converting from other board software? Good decision! Need help? Have a question about a convertor? Wish to offer a convertor package? Post here.
stubbo79
Registered User
Posts: 9
Joined: Wed Jul 03, 2024 11:56 am

Importing a CSV file of Topics and Replies to a PHPBB 3.3.12 Board | Chat GPT PHP Script

Post by stubbo79 »

So today I've been working on trying to find a way to import a CSV file of Topics and Replies to a Forum (I've a migration to do) with just ChatGPT for guidance.

I saw on another thread an approach that can be taken is to loop through the CSV and call the submit_post() function in doing this, and whilst I've only tested it on a couple of relatively short Topic's this has now worked.

In case it is useful to someone else:

Pre-requisites for using this PHP:
  • All Users are in the DB, and their User IDs are mapped into the CSV file against their topic/replies as part of the data file prep.
  • All dates are in Unix timestamp
  • Topics and their replies are given an arbitrary unique identifier to support grouping them together
CSV File format: (saved as importtopics.csv)

Code: Select all

User ID,Date (Unix Timestamp),Forum ID,Post Type,Topic Reference ID,Topic Title,Content,Enable BBCode,Enable Smilies,Enable URLs,Enable Signature
1,1622476800,2,topic,101,First Topic Title,This is the first post in the first topic.,1,1,1,1
2,1622480400,2,reply,101,Re: First Topic Title,This is a reply to the first topic.,1,1,1,1
3,1622484000,2,reply,101,Re: First Topic Title,Another reply to the first topic.,1,1,1,1
1,1622487600,2,topic,102,Second Topic Title,This is the first post in the second topic.,1,1,1,1
2,1622491200,2,reply,102,Re: Second Topic Title,This is a reply to the second topic.,1,1,1,1
3,1622494800,2,reply,102,Re: Second Topic Title,Another reply to the second topic.,1,1,1,1
PHP File Code:

Code: Select all

<?php
// Enable error reporting
error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('log_errors', 1);
ini_set('error_log', __DIR__ . '/error_log.txt');

// Path to phpBB root directory
define('IN_PHPBB', true);
$phpbb_root_path = './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);

// Include necessary files
require_once($phpbb_root_path . 'common.' . $phpEx);
require_once($phpbb_root_path . 'includes/functions_posting.' . $phpEx);
require_once($phpbb_root_path . 'includes/functions_user.' . $phpEx);
require_once($phpbb_root_path . 'includes/functions.' . $phpEx);

// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup();

function import_posts_from_csv($csv_file)
{
    global $db, $user, $auth;

    $topics = [];

    // Open the CSV file
    if (($handle = fopen($csv_file, "r")) !== FALSE) {
        // Skip the header row
        fgetcsv($handle, 1000, ",");

        // Loop through the CSV rows
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            // CSV columns: User ID; Date (Unix Timestamp); Forum ID; Post Type; Topic Reference ID; Topic Title; Content; Enable BBCode; Enable Smilies; Enable URLs; Enable Signature
            $user_id = (int)$data[0];
            $date = $data[1];
            $forum_id = $data[2];
            $post_type = $data[3];
            $topic_ref_id = $data[4];
            $topic_title = $data[5];
            $content = $data[6];
            $enable_bbcode = $data[7];
            $enable_smilies = $data[8];
            $enable_urls = $data[9];
            $enable_signature = $data[10];

            // Debugging: Output the data being processed
            echo "Processing: User ID=$user_id, Date=$date, Forum ID=$forum_id, Post Type=$post_type, Topic Ref ID=$topic_ref_id, Topic Title=$topic_title\n";

            // Check if user ID exists
            if (!user_exists($user_id)) {
                echo "User ID $user_id not found\n";
                continue;
            }

            // Switch to the user
            switch_user($user_id);

            // Create the post data array
            $post_data = array(
                'forum_id' => $forum_id,
                'post_text' => $content,
                'poster_id' => $user_id,
                'post_time' => $date,
                'enable_bbcode' => $enable_bbcode,
                'enable_smilies' => $enable_smilies,
                'enable_urls' => $enable_urls,
                'enable_sig' => $enable_signature,
                'icon_id' => 0, // Default value for icon_id
                'message' => $content, // Required by submit_post
                'message_md5' => md5($content), // Required by submit_post
                'bbcode_bitfield' => '', // Default value
                'bbcode_uid' => '', // Default value
                'post_edit_locked' => 0, // Default value
                'enable_indexing' => true, // Default value
                'notify_set' => false, // Default value
                'notify' => false, // Default value
                'forum_name' => '', // Default value to prevent warning in notification
            );

            // Define an empty poll array
            $poll = array();

            if ($post_type == 'topic') {
                // Create new topic
                $post_data['topic_title'] = $topic_title;
                submit_post('post', $topic_title, '', POST_NORMAL, $poll, $post_data);

                // Capture the Topic ID directly after creation
                $topic_id = $post_data['topic_id'];
                if ($topic_id) {
                    $topics[$topic_ref_id] = $topic_id;
                    echo "Created topic with ID: $topic_id for reference ID: $topic_ref_id\n";
                } else {
                    echo "Failed to capture topic ID for topic reference ID $topic_ref_id\n";
                }
            } else if ($post_type == 'reply') {
                // Ensure the topic has been created and get the Topic ID
                if (!isset($topics[$topic_ref_id])) {
                    echo "Topic with reference ID $topic_ref_id not found\n";
                    continue;
                }
                $post_data['topic_id'] = $topics[$topic_ref_id];
                $post_data['topic_title'] = $topic_title;
                submit_post('reply', $topic_title, '', POST_NORMAL, $poll, $post_data);
                echo "Added reply to topic ID: {$topics[$topic_ref_id]}\n";
            }
        }
        fclose($handle);
    } else {
        echo "Failed to open CSV file.";
    }
}

function user_exists($user_id)
{
    global $db;
    $sql = 'SELECT user_id FROM ' . USERS_TABLE . ' WHERE user_id = ' . (int)$user_id;
    $result = $db->sql_query($sql);
    $row = $db->sql_fetchrow($result);
    $db->sql_freeresult($result);

    return ($row) ? true : false;
}

function switch_user($user_id)
{
    global $db, $user, $auth;

    // Get user data
    $sql = 'SELECT * FROM ' . USERS_TABLE . ' WHERE user_id = ' . (int)$user_id;
    $result = $db->sql_query($sql);
    $row = $db->sql_fetchrow($result);
    $db->sql_freeresult($result);

    if ($row) {
        $user->data = array_merge($user->data, $row);
        $auth->acl($user->data);
        $user->ip = '127.0.0.1'; // Example IP, adjust if needed
        return true;
    }

    return false;
}

function get_latest_topic_id_by_user($user_id)
{
    global $db;
    // SQL query to fetch the latest topic_id created by the user
    $sql = 'SELECT topic_id FROM ' . TOPICS_TABLE . ' WHERE topic_poster = ' . (int)$user_id . ' ORDER BY topic_time DESC LIMIT 1';
    $result = $db->sql_query($sql);
    $row = $db->sql_fetchrow($result);
    $db->sql_freeresult($result);

    return ($row) ? (int)$row['topic_id'] : false;
}

// Run the import
import_posts_from_csv('importtopics.csv');
?>
Steps to Execute:

1. Prepare the CSV File:

Ensure your CSV file is named importtopics.csv and follows the format shown in the example, including multiple topics and their replies with correct reference IDs.

2. Place the Script and CSV:

Ensure your script import_topics.php and CSV file importtopics.csv are in the same directory.

3. Run the Script:

Open your web browser and navigate to the script URL:

http://yourdomain.com/phpbb/import_topics.php

This will execute the script and start importing the topics and replies based on your CSV file.

Watch the output in your browser or console to see the debug information.

4. Verify the Import:

Check your phpBB forum to ensure that multiple topics and their replies have been imported correctly and are posted by the correct users.

5. Check Error Log:

If the script still fails to perform as expected, check the error_log.txt file in the same directory for detailed error messages.
Remove the Script:

For security reasons, delete import_topics.php from your server after the import is complete.

What the script does: (as specified by ChatGPT

Overview of the Script
This PHP script is designed to import topics and replies from a CSV file into a phpBB forum. The script processes each row of the CSV file, creating new topics and adding replies to the appropriate topics while ensuring the correct user is attributed to each post.

Key Tasks and Workflow

1. Initialization and Setup:

The script starts by enabling error reporting and setting up the path to the phpBB root directory.
Necessary phpBB files (common.php, functions_posting.php, functions_user.php, functions.php) are included using require_once.

2. Start phpBB Session:

The script initiates a phpBB session using $user->session_begin().
User authentication and permissions are set up with $auth->acl($user->data) and $user->setup().

3. CSV File Processing:

The script opens the CSV file and reads each row, skipping the header row.

Each row is parsed to extract relevant data: User ID, Date, Forum ID, Post Type, Topic Reference ID, Topic Title, Content, Enable BBCode, Enable Smilies, Enable URLs, and Enable Signature.

4. User Verification and Session Switching:

The script checks if the user exists using the user_exists() function.
If the user exists, the script switches to that user using the switch_user() function, which sets up the session for the specified user ID.

5. Post Creation:

A post_data array is created to store all the necessary data for creating a post.
If the post type is 'topic', the script creates a new topic using the submit_post() function and captures the topic ID.
If the post type is 'reply', the script ensures that the corresponding topic exists and adds the reply to that topic using the submit_post() function.

6. Logging and Debugging:

Throughout the process, the script outputs debug information to the console or browser to track the progress and any potential issues.
Errors and warnings are logged to error_log.txt for further analysis.


Functions in the Script

import_posts_from_csv($csv_file):

Main function that processes the CSV file, creates topics, and adds replies.

user_exists($user_id):

Checks if a user exists in the phpBB database.

switch_user($user_id):

Switches the session to the specified user by merging the user data into the current session data.

get_latest_topic_id_by_user($user_id):

Retrieves the latest topic ID created by the specified user.



Hope this helps someone to achieve something they need to do. Quite incredible that me, someone with zero coding experience but experience with data, csv files, and an understanding of data structures, can produce a coded script like this in a matter of about 90 mins with ChatGPT.

Obviously use at your own risk in a test environment first.
User avatar
Galandas
Registered User
Posts: 743
Joined: Thu Jul 23, 2009 4:11 pm
Location: Italy
Name: Rey

Re: Importing a CSV file of Topics and Replies to a PHPBB 3.3.12 Board | Chat GPT PHP Script

Post by Galandas »

I'll try it thanks
English is not my native language My CDB Contributions My RC extensions
stubbo79
Registered User
Posts: 9
Joined: Wed Jul 03, 2024 11:56 am

Re: Importing a CSV file of Topics and Replies to a PHPBB 3.3.12 Board | Chat GPT PHP Script

Post by stubbo79 »

Cool. Let me know if you have success with it.

Return to “[3.3.x] Convertors”