XF 1.5 Is it possible to create new threads using SQL commands?

Chromaniac

Well-known member
What I am basically aiming to do is to create a batch of threads from data stored in spreadsheet. I am looking for advice on whether SQL statements can be created that would create multiple threads on running them. I have the content for title and post content stored in cells of a spreadsheet. I mean I can use Excel/Sheets to create custom MySQL Statements that I can run from terminals. I just need to know if it is possible, safe to do. TIA!
 
Conversely, I'd never recommend it. There's a ton of other related data (message counts, forum counters). Some can potentially be rebuilt, but I'd always recommend using a PHP script to insert if possible.
 
That was what I was worried about. Customized PHP script would be fine as well as long as I can batch post multiple threads in one go. Manual is taking way too much time and effort. Though I guess you were talking about the form rendered by XenForo for new threads :P If not, it would be great to get some sample code that I can play with. Thanks!
 
@OperaManiac, a "some assembly required" script.

Basically, you need to provide a list of messages with the right format, a thread title/post date/user, and the target forum. This is intended as a quick & dirty example, and can't run as-is.

Code:
<?php


$startTime = microtime(true);
$fileDir = dirname(__FILE__);

@set_time_limit(120);
ignore_user_abort(true);

require($fileDir . '/library/XenForo/Autoloader.php');
XenForo_Autoloader::getInstance()->setupAutoloader($fileDir . '/library');

XenForo_Application::initialize($fileDir . '/library', $fileDir);
XenForo_Application::set('page_start_time', $startTime);

$dependencies = new XenForo_Dependencies_Public();
$dependencies->preLoadData();

$nodeId = 5; // Current Affairs
$messages = ... go here ...
$title = .. thread title ..
$date = .. thread post date (unix epoc) ..

$forumModel = XenForo_Model::create('XenForo_Model_Forum');
$forum = $forumModel->getForumById($nodeId);
$initialMessage = reset($messages);

/** @var XenForo_DataWriter_Discussion_Thread $threadDw */
$threadDw = XenForo_DataWriter::create('XenForo_DataWriter_Discussion_Thread');
$threadDw->setExtraData(XenForo_DataWriter_Discussion_Thread::DATA_FORUM, $forum);
$threadDw->set('node_id', $forum['node_id']);
$threadDw->set('user_id', $initialMessage['user_id']);
$threadDw->set('username', $initialMessage['username']);
$threadDw->set('title', $title);
$threadDw->set('post_date', $date);
$threadDw->set('discussion_state', 'visible');

$threadMessageDw = $threadDw->getFirstMessageDw();
$threadMessageDw->setOption(XenForo_DataWriter_DiscussionMessage_Post::OPTION_IS_AUTOMATED, true);
$threadMessageDw->setExtraData(XenForo_DataWriter_DiscussionMessage_Post::DATA_FORUM, $forum);
$threadMessageDw->set('user_id', $initialMessage['user_id']);
$threadMessageDw->set('username', $initialMessage['username']);
$threadMessageDw->set('post_date', $initialMessage['date']);
$threadMessageDw->set('message',  $initialMessage['message']);

$threadDw->save();

$thread = $threadDw->getMergedData();

unset($messages[$initialMessage['message_id']]);

foreach ($messages as $message)
{
    /** @var XenForo_DataWriter_DiscussionMessage_Post $threadMessageDw */
    $threadMessageDw = XenForo_DataWriter::create('XenForo_DataWriter_DiscussionMessage_Post');
    $threadMessageDw->setOption(XenForo_DataWriter_DiscussionMessage_Post::OPTION_IS_AUTOMATED, true);
    $threadMessageDw->setExtraData(XenForo_DataWriter_DiscussionMessage_Post::DATA_FORUM, $forum);
    $threadMessageDw->set('thread_id', $thread['thread_id']);
    $threadMessageDw->set('user_id', $message['user_id']);
    $threadMessageDw->set('username', $message['username']);
    $threadMessageDw->set('message_state', 'visible');
    $threadMessageDw->set('post_date', $message['date']);
    $threadMessageDw->set('message', $message['message']);

    $threadMessageDw->save();
}
 
Back
Top Bottom