Cron job to move threads...Works with all prefixes except one?

Marcel

Active member
I used @Furhmann 's guide from here to create a plugin that moves all threads with a particular prefix in one forum, to another.....daily at a particular time (It's a forum tidying up thing we do).

I went through, set it all up, but it just won't work.
If I change the options to a different prefixid other than '1' it works. It does it's job brilliantly.
If I set it to work on threads with a prefixid of 1 it just doesn't do anything. It runs, and the threads are still where they were.

Does anyone have any idea why? Here is the code for CronEntry.php, as modified from the above link....

Code:
<?php
/**
* Cron entry of the tutorial How to create a Cron Entry to move threds (with options!).
*
* MoveThreadCron = Name of our folder (and add-on too!)
* CronEntry = name of this file!
*
*/
class MoveSortedThreadsCron_CronEntry
{
    /**
    * Move ammount of threads with a specified prefix from forum X to forum Y.
    */
    public static function runMoveThreads()
    {
        /* We have options, remember? So, we will just get all options using the line bellow. */
        $options = XenForo_Application::get('options');

        /*
        *
        * Since we already have created the options, we just have to get them and use it in our cron.
        * Let's put all our custom options in an array, so can be easy to use it before in the function.
        * If you don't remember the Options ID, just go back to Step 2 and see.
        *
        */
        $cronOptions = array(
            'sourceForum'    => $options->mstcSourceForum,
            'destinationForum'        => $options->mstcDestinationForum,
            'sourcePrefix'    => $options->mstcSourcePrefix,
            'destinationPrefix'        => $options->mstcDestinationPrefix,
            'amount'        => $options->mstcAmount
        );

        /*
        *
        * To get the thrads we want, we must use a model. There is some ways to create models, but since we are in a static class
        * we will use the above method. The model is the XenForo_Model_Threads which has various functions to interact with threads.
        * The file of this model is located at: library/XenForo/Model/Thread.php. You can take a look at there if you want.
        *
        */
        $threadModel = XenForo_Model::create('XenForo_Model_Thread');

        /*
        *
        * Now we have our model, we can use a method inside of it to get threads from a forum. But what method?
        * This model has the method getThreads() which we can use conditionals and/or options to get threads.
        * So this will be the method that we'll use. First of all let's declare the conditionals variable so we can
        * filter the results and get only certain specific threads based on our options.
        *
        */
        $conditionals = array(
            // we just want to get threads from one forum. This is set in the options
            'forum_id'    => $cronOptions['sourceForum'],

            // same thing, we want only threads with a certain prefix id
            'prefix_id' => $cronOptions['sourcePrefix']
        );

        /*
        * Now let's just set the option to fetch the threads. The only option to fetch that we have to use is the "amount of threads".
        * This will limit the number of returned threads.
        */
        $fetchOptions = array(
            // only get this ammount of threads, please!
            'limit' => $cronOptions ['amount']
        );

        /*
        * Now, let's get those threads!
        */
        $threads = $threadModel->getThreads($conditionals, $fetchOptions);

        /* If the method return any threads, let's move them to the destination forum! */
        if ($threads)
        {
            /* Now, for each thread found, let's change the prefix and move the thread to the destination forum! */
            foreach ($threads as $thread)
            {
                /*
                * To move threads, we need to change a field in the database. To do that, we've always to use (almost always) the DataWriter.
                * The DataWriter will do almost all the job for us. We just need to create it.
                */
                /* Let's create the DataWriter associated on Threads */
                $dw = XenForo_DataWriter::create('XenForo_DataWriter_Discussion_Thread');

                /* Associate the data that we have in the thread to the DataWriter instance */
                $dw->setExistingData($thread);

                /* Here it is the code to move this thread to the destination forum. Simple, eh?
                * We are just changing the ID of the NODE of this thread to the new ID, which is the destination forum ID
                */
                $dw->set('node_id', $cronOptions['destinationForum']);

                /* Now, let's change the prefix id. We already have the destination prefix set in the options, so let's use it. */
                $dw->set('prefix_id', $cronOptions['destinationPrefix']);

                /* Save all changes and we are done! */
                $dw->save();
            }
        }
    }
}
 
Nope, tried everything I can think of.
I thought it may have been a prefix permissions thing...relating to either usergroup or node.
Including them all didn't make a difference.

It seems to be just that particular prefix id, no matter how I configure it.
 
Well I removed the options and literally just stripped all the code back to this

Code:
<?php
class MoveSortedThreadsCron_CronEntry
{
    public static function runMoveThreads()
    {
    $db = XenForo_Application::get('db');
    $db->query('
        UPDATE xf_thread SET
        node_id = 99
        WHERE node_id = 98
        AND prefix_id = 1
            ');
    }
}
 
@Fuhrmann - Why not releast just that zip file in the Resources/Addons section? Lots of us who aren't ready to take on a tutorial project can benefit from what you've made.

Also, would you consider making a similar cron addon that takes threads older than X days from node Y and moves them to node Z? I'm assuming it could be done in the same manner as this one, and I'd be willing to pay for development depending on pricing.

EDIT: Oops, looks like that already exists: https://xenforo.com/community/threads/advanced-thread-archiver.82818/
 
Last edited:
Top Bottom