<?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();
}