Script to import addons and styles in one swoop

Rasmus Vind

Well-known member
Hey guys, I wrote this script and it's almost working.

The purpose of it is to import all addons and styles in my library folder. That part works fine. At the end, it is supposed to rebuild all the information that usually happens after you import an addon/style but it does not seem to work correctly. Sometimes phrases are missing and styles are not updated. I think templates don't get rebuilt properly. Can you help me properly rebuild the caches?

PHP:
<?php

// The dir of the XF root
$fileDir = dirname(__FILE__);

// Don't change anything below
$startTime = microtime(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);

function error($message)
{
    echo $message;
    exit(1);
}

if (!isset($argc))
{
    error("Must be called from command-line\n");
}

$libraryDir = $fileDir . '/library';

$dh = opendir($libraryDir);

if ($dh === false)
{
    error('Failed to open library directory');
}

$addonModel = XenForo_Model::create('XenForo_Model_AddOn');
$styleModel = XenForo_Model::create('XenForo_Model_Style');
$deferredModel = XenForo_Model::create('XenForo_Model_Deferred');

XenForo_Db::beginTransaction();

while (($f = readdir($dh)) !== false)
{
    if (preg_match('#^addon\-(.*?)\.xml$#', $f, $matches))
    {
        $id = $matches[1];
        $existing = $addonModel->getAddOnById($id);
        if ($existing)
        {
            echo "Upgrade addon $id\n";
        }
        else
        {
            echo "Import addon $id\n";
        }
        $addonModel->installAddOnXmlFromFile("$libraryDir/$f", $existing ? $id : false);
    }
    if (preg_match('#^style\-(.*?)\.xml$#', $f, $matches))
    {
        $id = $matches[1];
        $existingStyles = $styleModel->getAllStyles();
        $existing = null;
        foreach ($existingStyles as $styleId => $style)
        {
            if ($style['title'] == str_replace('-', ' ', $id))
            {
                $existing = $style;
                break;
            }
        }
        $document = XenForo_Helper_DevelopmentXml::scanFile("$libraryDir/$f");
        if ($existing)
        {
            echo "Upgrade style $id\n";
            $styleModel->importStyleXml($document, 0, $existing['style_id']);
        }
        else
        {
            echo "Import style $id\n";
            $styleModel->importStyleXml($document);
        }
    }
}

XenForo_Db::commit();

echo "Running deferred\n";
$deferredModel->run(true);

echo "Running deferred (2)\n";
$deferredModel->run(false);
 
Now I know. I had to do this at the bottom
PHP:
echo "Running deferred\n";
$deferredModel->run(false, 0);
echo "Done\n";
It forces it to run the deferred task to the end.

The final script looks like this
PHP:
<?php

// The dir of the XF root
$fileDir = dirname(__FILE__);

// Don't change anything below
$startTime = microtime(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);

function error($message)
{
    echo $message;
    exit(1);
}

if (!isset($argc))
{
    error("Must be called from command-line\n");
}

$libraryDir = $fileDir . '/library';

$dh = opendir($libraryDir);

if ($dh === false)
{
    error('Failed to open library directory');
}

$addonModel = XenForo_Model::create('XenForo_Model_AddOn');
$styleModel = XenForo_Model::create('XenForo_Model_Style');
$deferredModel = XenForo_Model::create('XenForo_Model_Deferred');

XenForo_Db::beginTransaction();

while (($f = readdir($dh)) !== false)
{
    if (preg_match('#^addon\-(.*?)\.xml$#', $f, $matches))
    {
        $id = $matches[1];
        $existing = $addonModel->getAddOnById($id);
        if ($existing)
        {
            echo "Upgrade addon $id\n";
        }
        else
        {
            echo "Import addon $id\n";
        }
        $addonModel->installAddOnXmlFromFile("$libraryDir/$f", $existing ? $id : false);
    }
    if (preg_match('#^style\-(.*?)\.xml$#', $f, $matches))
    {
        $id = $matches[1];
        $existingStyles = $styleModel->getAllStyles();
        $existing = null;
        foreach ($existingStyles as $styleId => $style)
        {
            if ($style['title'] == str_replace('-', ' ', $id))
            {
                $existing = $style;
                break;
            }
        }
        $document = XenForo_Helper_DevelopmentXml::scanFile("$libraryDir/$f");
        if ($existing)
        {
            echo "Upgrade style $id\n";
            $styleModel->importStyleXml($document, 0, $existing['style_id']);
        }
        else
        {
            echo "Import style $id\n";
            $styleModel->importStyleXml($document);
        }
    }
}

XenForo_Db::commit();

echo "Running deferred\n";
$deferredModel->run(false, 0);
echo "Done\n";
 
Top Bottom