• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

0 query, check addon version id

tenants

Well-known member
#1
I thought this might be useful

If you are designing a plugin for boards that are busy, and those plugins are updated, sometimes you might run into what some people refer to as "install errors", but they are not real errors, they are errors due the the plugin files not currently matching the installed version of the plugin. (There might be a mismatch between functions /tables /acp options.. which will all be sorted out after the xml file has been used and the upgrade has gone to completion).

So if you wait a long time, or do not run the xml, or you have lots of visitors before the upgrade is complete and the board is not turned off (which often admins don't seem to do for upgrades), then these errors can be seen in the ACP server errors

I now return the user an "upgrade message" if there is a mismatch, and do this with 0 query overhead


Code:
<?php
class YourPlugin_Model_Version extends XenForo_Model
{
    /*
    *    To prevent errors from being raised when admins replace the plugin files, but do not upgrade the xml (yet)
    *    There is a need for a 0 query method to check the current installed version of the add-on (compared to the expected version from plugin files)
    */

    protected static $_yourPuginVersion = 1202026; // the current version_id for your plugin, update before release

    public function isCorrectPluginVersion()
    {
        $pluginVersionId = self::$_yourPuginVersion;
        $cachedPluginVersionId = XenForo_Application::getSimpleCacheData('yourPluginName-version');
        if($cachedPluginVersionId >= $pluginVersionId)
        {
            return true;  // 0 query
        }

        if(!$cachedPluginVersionId || $cachedPluginVersionId < $pluginVersionId)
        {
            $addon = $this->getModelFromCache('XenForo_Model_AddOn')->getAddOnById('yourPluginName');
            $currentPluginVersionId = $addon['version_id']; // the version Id that this has been currently updated to
            if($currentPluginVersionId < $pluginVersionId)
            {
                return false;
            }
            XenForo_Application::setSimpleCacheData('yourPluginName-version', $currentPluginVersionId);
            return true;
        }
    }
}

You can then use something like this in your controller public:

Code:
    public function _preDispatch($action)
    {
        parent::_preDispatch($action);
        $this->_assertCorrectPluginVersion();
    }

    protected function _assertCorrectPluginVersion()
    {
        if(!$this->_getVersionModel()->isCorrectPluginVersion())
        {
            throw $this->getErrorOrNoPermissionResponseException('Plugin is currently being upgraded');
        }
    }

   protected function _getVersionModel(){return $this->getModelFromCache('YourPlugin_Model_Version');}
 
Last edited:

Chris D

XenForo developer
Staff member
#2
I already do something similar :)

It's a good idea (y)

(I'd recommend this being added to the Resource Manager as it is easier to find)