2010-09-27, 03:27 PM
Hi,
I have some ideas for the upgrade process of plugins.
How about a functionality that caches the version of installed plugins?
I think, that would be helpful for developers, to write upgrade routines.
Here is an example plugin, to illustrate what I mean.
So the plugin will know,
- whether a new version was uploaded
- which version was installed previously
The users only have to deactivate the plugin and upload the new files.
Upon the next activation, the plugin checks whether upgrading is necessary.
As you can see, the upgrade process depends on the previous installed version.
The implementation (extending the pluginSystem class) would be quite easy.
So that's my idea - I hope my explanation was understandable
And of course, maybe it is interesting only for the larger plugins.
However, what do you think about it?
I have some ideas for the upgrade process of plugins.
How about a functionality that caches the version of installed plugins?
I think, that would be helpful for developers, to write upgrade routines.
Here is an example plugin, to illustrate what I mean.
<?php
if (!defined('IN_MYBB'))
{
if (headers_sent() === false) header('HTTP/1.1 403 Forbidden');
exit();
}
/**
* test_info
*
* @return array Information about the plugin
*/
function test_info()
{
return array(
'name' => 'Test',
'description' => 'A sample plugin.',
'website' => '',
'author' => 'Cayo',
'authorsite' => 'http://community.mybb.com/user-30686.html',
'version' => '1.0.X',
'guid' => '',
'compatibility' => '16*'
);
}
/**
* test_install
*
* @global pluginSystem $plugins
* @return void
*/
function test_install()
{
global $plugins;
// <Installation stuff>
// Write the current version into the cache
$plugins->setVersion('test', '1.0.0');
}
/**
* test_is_installed
*
* @global MyLanguage $lang
* @global pluginSystem $plugins
* @return boolean True if installed, otherwise false
*/
function test_is_installed()
{
global $lang, $plugins;
// <Checking tables, fields etc>
// Get the cached version
$strVersion = $plugins->getVersion('test');
if (is_string($strVersion)) return true;
return false;
}
/**
* test_uninstall
*
* @global pluginSystem $plugins
* @return void
*/
function test_uninstall()
{
global $plugins;
// <Uninstall stuff>
// Remove the plugin from the version cache
$plugins->removeVersion('test');
}
/**
* test_activate
*
* @global boolean $installed
* @global string $message
* @global pluginSystem $plugins
* @return void
*/
function test_activate()
{
global $installed, $message, $plugins;
// <Activation stuff>
if ($installed == true)
{
// Get the cached version
$strVersion = $plugins->getVersion('test');
// Upgrade from v1.0.0 to v1.0.1
if ($strVersion == '1.0.0')
{
// <Upgrade stuff>
// Write the new version into the cache
$plugins->setVersion('test', '1.0.1');
$message = 'The selected plugin has been upgraded (to v1.0.1) and activated successfully.';
flash_message($message, 'success');
}
// Upgrade from v1.0.1 to v1.0.2
elseif ($strVersion == '1.0.1')
{
// <Upgrade stuff>
// Write the new version into the cache
$plugins->setVersion('test', '1.0.2');
$message = 'The selected plugin has been upgraded (to v1.0.2) and activated successfully.';
flash_message($message, 'success');
}
// ...
}
}
?>
So the plugin will know,
- whether a new version was uploaded
- which version was installed previously
The users only have to deactivate the plugin and upload the new files.
Upon the next activation, the plugin checks whether upgrading is necessary.
As you can see, the upgrade process depends on the previous installed version.
The implementation (extending the pluginSystem class) would be quite easy.
/** @var array */
private $arrVersionCache = null;
/**
* getVersion
*
* @global datacache $cache
* @param string $strPlugin
* @return mixed Version on success, false on error, otherwise null
*/
public function getVersion($strPlugin)
{
// The cache hasn't been loaded yet
if ($this->arrVersionCache === null)
{
global $cache;
$this->arrVersionCache = $cache->read('plugins_version');
}
// The cache hasn't been loaded successfully
if ($this->arrVersionCache === false)
{
return false;
}
if (isset($this->arrVersionCache[$strPlugin]))
{
return $this->arrVersionCache[$strPlugin];
}
return null;
}
/**
* setVersion
*
* @global datacache $cache
* @param string $strPlugin
* @param string $strVersion
* @return boolean True on success, otherwise false
*/
public function setVersion($strPlugin, $strVersion)
{
global $cache;
// The cache hasn't been loaded yet
if ($this->arrVersionCache === null)
{
$this->arrVersionCache = $cache->read('plugins_version');
}
$this->arrVersionCache[$strPlugin] = $strVersion;
$cache->update('plugins_version', $this->arrVersionCache);
return true;
}
/**
* removeVersion
*
* @global datacache $cache
* @param string $strPlugin
* @return boolean True on success, otherwise false
*/
public function removeVersion($strPlugin)
{
global $cache;
// The cache hasn't been loaded yet
if ($this->arrVersionCache === null)
{
$this->arrVersionCache = $cache->read('plugins_version');
}
if (isset($this->arrVersionCache[$strPlugin]))
{
unset($this->arrVersionCache[$strPlugin]);
$cache->update('plugins_version', $this->arrVersionCache);
return true;
}
return false;
}
So that's my idea - I hope my explanation was understandable
And of course, maybe it is interesting only for the larger plugins.
However, what do you think about it?