Add Threads into DB Directly

NickH

Member
I am looking to create a script that will automatically generate threads based on another API. I just wondered the best way to create a thread? Is there any documentation for creating the thread / first post?
 
Thanks, I have got as far as getting a list of threads using my script, so I just need to insert the thread which I am trying to find out how!

Do I insert the post and then the thread? as I can see the thread needs a post_id ?
 
This is basically the data writer call from someones addon.

Code:
$threadDw = XenForo_DataWriter::create('XenForo_DataWriter_Discussion_Thread');
      $threadDw->set('user_id', $user['user_id']);
      $threadDw->set('username', $user['username']);
      $threadDw->set('title', $title);
      $postDw = $threadDw->getFirstMessageDw();
      $postDw->set('message', $message);
      $threadDw->set('node_id', $forumId);
      $threadDw->preSave();
      $threadDw->save();
      return $threadDw->getMergedData();

The important thing here is to set the fact this is an automated post. If you don't it will record the random IP of someone visiting the site at the time of the cron or event triggering the thread being posted. I need to get you the setting for that because I don't see it on this example.

The above is 100% of what you need other than that. No SQL calls at all. Just insert your variables and go.
 
Don't forget to add the automated flag to the postDw before the set('message'...) if the post is automated.
Code:
$postDw->setOption(XenForo_DataWriter_DiscussionMessage::OPTION_IS_AUTOMATED, true);
 
Don't forget to add the automated flag to the postDw before the set('message'...) if the post is automated.
Code:
$postDw->setOption(XenForo_DataWriter_DiscussionMessage::OPTION_IS_AUTOMATED, true);

Thanks Snog I was trying to find that for him lol. When he said he cracked it im like not so fast lol.
 
Next question, am I best to use a server cron or try and figure out xen crons?

You can if you call up the autoloader, look for an example of running scripts ot crons outside xenforo. As far as the board crons just put the board into debug mode and add a cron pointing to a script contained within xenforo. The class name is the folder structure within Xenforo. I use Brand_Product_Filetype so mine would be RainDD_Whatever_Cron and then the next box is the function name.

Up to you.
 
Adding to what i said Just start the xenforo cron script like:
Code:
<?php
/**
* Created with PhpStorm.
* Date: 11/5/2014
* Product:
*/

class RainDD_Product_Cron
{
    public static function ProcessThreads()
    {
    }
}

The function name for the cron callback entry in this example is ProcessThreads
 
A good way to decide is if the cron MUST run at an exact time, use the server cron. If it's not critical that the cron runs at an exact time, use the xen cron.
 
Thanks, I will just go down the server cron route :)

With things like auto tweeting and other processes that occur after adding, is there a way to ensure that post thread processes happen too?
 
Back
Top Bottom