MyBB Community Forums

Full Version: External rebuild of default_theme cache?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey all. I'm not sure if this is 100% the right place to post this since it's not an integration question, per se, but still interacting with the system externally. If I need to post it elsewhere though, please let me know.

I have a couple of holiday themes and decided last night to automate the switch to them. I currently have a script set to run automatically at a specified date and time (the Christmas script, for instance, runs at midnight on December 1). It's currently executing the following queries which work correctly so far:

UPDATE themes SET def = 0
UPDATE themes SET def = 1, allowed groups = 'all' WHERE did = 9
UPDATE users SET style = 0

This all executes and updates just fine but it doesn't actually take affect until the cache is rebuilt (I presume it's just the default_theme cache that needs to be rebuilt in this case but I did a Rebuild & Reload all initially), which makes sense. I'm not sure if I'm just missing it but I can't seem to find the code that actually executes to make this happen so I'm wondering if either of the following is possible:

  1. Rebuild a cache (or caches, if necessary) from a non-interactive script that isn't actually "logged in" to the system.
  2. Have said non-interactive script execute a call to the "Force on Users" action available form the theme manager (again while not logged in) via something like PHP's http_get() or cURL functions. I'm assuming this takes care of the cache rebuild in the process but, if not, a second call to the rebuild action on the default_theme cache would likely suffice if this is possible.
I imagine for option 2 to work, I'd need to retrieve the post key but I can't seem to find the value it's using in the database anywhere obvious so I'm not sure how to go about getting it or if I should even bother trying given that I don't know if that second option will work.

Any advice? Thanks in advance!
I have a task file (inc/tasks/dvz_set_theme.php) that works within the MyBB's own system:
<?php

function task_dvz_set_theme ($task)
{
    global $db, $cache;

    $name = 'Default'; // theme name
    $force = false; // force theme change on users (true/false)

    $query = $db->simple_select('themes', '*', "name='" . $db->escape_string($name) . "'");
    $theme = $db->fetch_array($query);

    if ($theme['tid'] && $theme['tid'] != 1) {

        // set default
        $db->update_query('themes', array('def' => 0));
        $db->update_query('themes', array('def' => 1), 'tid=' . $theme['tid']);

        // update cache
        $cache->update('default_theme', $theme);

        // force users
        if ($force) {
            $db->update_query('users', array('style' => $theme['tid']));
        }

        add_task_log($task, 'Default theme set' . ($force ? ' & forced' : null) . ' to: ' . $db->escape_string($theme['name']));

    }

}

If you can't use the task system for some reason, the easiest way to do it is to include the required core files, get the theme's data and run the cache update as shown above.
I originally looked at the task system but didn't realize it was so easy to add new ones (I saw the dropdown but didn't actually bother to check the filesystem for the files themselves - oops...). With some minor modifications to the code you provided (namely using the one function to handle both holiday themes as well as the reset to normal), it's now up and running using the built in task scheduler which is a much better solution than I was trying before. Thanks for your help!