Skip to content

Commit

Permalink
feature 3076: Enhance plugin update system
Browse files Browse the repository at this point in the history
git-svn-id: http://piwigo.org/svn/trunk@28651 68402e56-0260-453c-a942-63ccdbb3a9ee
  • Loading branch information
mistic100 committed Jun 9, 2014
1 parent 3f1cd56 commit 6b75354
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 109 deletions.
41 changes: 20 additions & 21 deletions admin/include/plugins.class.php
Expand Up @@ -55,6 +55,7 @@ function uninstall()
return plugin_uninstall($this->plugin_id);
}
}
function update($old_version, $new_version, &$errors=array()) {}
}


Expand Down Expand Up @@ -85,28 +86,26 @@ function plugins()
*/
private static function build_maintain_class($plugin_id)
{
$file_to_include = PHPWG_PLUGINS_PATH . $plugin_id . '/maintain.inc.php';
$file_to_include = PHPWG_PLUGINS_PATH . $plugin_id . '/maintain';
$classname = $plugin_id.'_maintain';

if (file_exists($file_to_include))
if (file_exists($file_to_include.'.class.php'))
{
include_once($file_to_include);
include_once($file_to_include.'.class.php');
return new $classname($plugin_id);
}

if (file_exists($file_to_include.'.inc.php'))
{
include_once($file_to_include.'.inc.php');

if (class_exists($classname))
{
$plugin_maintain = new $classname($plugin_id);
}
else
{
$plugin_maintain = new DummyPlugin_maintain($plugin_id);
return new $classname($plugin_id);
}
}
else
{
$plugin_maintain = new DummyPlugin_maintain($plugin_id);
}

return $plugin_maintain;
return new DummyPlugin_maintain($plugin_id);
}

/**
Expand All @@ -121,7 +120,7 @@ function perform_action($action, $plugin_id)
{
$crt_db_plugin = $this->db_plugins_by_id[$plugin_id];
}

$plugin_maintain = self::build_maintain_class($plugin_id);

$errors = array();
Expand Down Expand Up @@ -187,7 +186,7 @@ function perform_action($action, $plugin_id)
WHERE id=\''. $plugin_id .'\'
;';
pwg_query($query);

$plugin_maintain->deactivate();
break;

Expand All @@ -206,7 +205,7 @@ function perform_action($action, $plugin_id)
WHERE id=\''. $plugin_id .'\'
;';
pwg_query($query);

$plugin_maintain->uninstall();
break;

Expand Down Expand Up @@ -235,7 +234,7 @@ function perform_action($action, $plugin_id)

/**
* Get plugins defined in the plugin directory
*/
*/
function get_fs_plugins()
{
$dir = opendir(PHPWG_PLUGINS_PATH);
Expand All @@ -256,7 +255,7 @@ function get_fs_plugins()
'description'=>'',
'author'=>'',
);
$plg_data = implode( '', file($path.'/main.inc.php') );
$plg_data = file_get_contents($path.'/main.inc.php', null, null, 0, 2048);

if ( preg_match("|Plugin Name: (.*)|", $plg_data, $val) )
{
Expand Down Expand Up @@ -326,7 +325,7 @@ function sort_fs_plugins($order='name')
function get_versions_to_check($version=PHPWG_VERSION)
{
global $conf;

$versions_to_check = array();
$url = PEM_URL . '/api/get_version_list.php?category_id='. $conf['pem_plugins_category'] .'&format=php';
if (fetchRemote($url, $result) and $pem_versions = @unserialize($result))
Expand Down Expand Up @@ -423,7 +422,7 @@ function get_incompatible_plugins($actualize=false)
{
return false;
}

global $conf;

// Plugins to check
Expand Down Expand Up @@ -477,7 +476,7 @@ function get_incompatible_plugins($actualize=false)
}
return false;
}

/**
* Sort $server_plugins
*/
Expand Down
14 changes: 3 additions & 11 deletions admin/include/themes.class.php
Expand Up @@ -86,19 +86,11 @@ private static function build_maintain_class($theme_id)

if (class_exists($classname))
{
$theme_maintain = new $classname($theme_id);
}
else
{
$theme_maintain = new DummyTheme_maintain($theme_id);
return new $classname($theme_id);
}
}
else
{
$theme_maintain = new DummyTheme_maintain($theme_id);
}

return $theme_maintain;

return new DummyTheme_maintain($theme_id);
}

/**
Expand Down
157 changes: 80 additions & 77 deletions include/functions_plugins.inc.php
Expand Up @@ -35,7 +35,7 @@
/**
* Used to declare maintenance methods of a plugin.
*/
class PluginMaintain
class PluginMaintain
{
/** @var string $plugin_id */
protected $plugin_id;
Expand Down Expand Up @@ -65,46 +65,17 @@ function deactivate() {}
function uninstall() {}

/**
* Tests if the plugin needs to be updated and call an update function
*
* @param string $version version exposed by the plugin (potentially new)
* @param string $on_update name of a method to call when an update is needed
* it receives the previous version as first parameter
* @param string $old_version
* @param string $new_version
* @param array &$errors - used to return error messages
*/
function autoUpdate($version, $on_update=null)
{
global $pwg_loaded_plugins;

$current_version = $pwg_loaded_plugins[$this->plugin_id]['version'];

if ( $version == 'auto' or $current_version == 'auto'
or safe_version_compare($current_version, $version, '<')
)
{
if (!empty($on_update))
{
call_user_func(array(&$this, $on_update), $current_version);
}

if ($version != 'auto')
{
$query = '
UPDATE '. PLUGINS_TABLE .'
SET version = "'. $version .'"
WHERE id = "'. $this->plugin_id .'"
;';
pwg_query($query);

$pwg_loaded_plugins[$this->plugin_id]['version'] = $version;
}
}
}
function update($old_version, $new_version, &$errors=array()) {}
}

/**
* Used to declare maintenance methods of a theme.
*/
class ThemeMaintain
class ThemeMaintain
{
/** @var string $theme_id */
protected $theme_id;
Expand All @@ -126,43 +97,6 @@ function activate($theme_version, &$errors=array()) {}
function deactivate() {}

function delete() {}

/**
* Tests if the theme needs to be updated and call an update function
*
* @param string $version version exposed by the theme (potentially new)
* @param string $on_update name of a method to call when an update is needed
* it receives the previous version as first parameter
*/
function autoUpdate($version, $on_update=null)
{
$query = '
SELECT version
FROM '. THEMES_TABLE .'
WHERE id = "'. $this->theme_id .'"
;';
list($current_version) = pwg_db_fetch_row(pwg_query($query));

if ( $version == 'auto' or $current_version == 'auto'
or safe_version_compare($current_version, $version, '<')
)
{
if (!empty($on_update))
{
call_user_func(array(&$this, $on_update), $current_version);
}

if ($version != 'auto')
{
$query = '
UPDATE '. THEMES_TABLE .'
SET version = "'. $version .'"
WHERE id = "'. $this->theme_id .'"
;';
pwg_query($query);
}
}
}
}


Expand Down Expand Up @@ -250,7 +184,7 @@ function remove_event_handler($event, $func,
* @param string $event
* @param mixed $data data to transmit to all handlers
* @param mixed $args,... optional arguments
* @return mixed $data
* @return mixed $data
*/
function trigger_change($event, $data=null)
{
Expand Down Expand Up @@ -399,23 +333,92 @@ function get_db_plugins($state='', $id='')
$query .= '
WHERE '. implode(' AND ', $clauses);
}

return query2array($query);
}

/**
* Loads a plugin, it includes the main.inc.php file and updates _$pwg_loaded_plugins_.
* Loads a plugin in memory.
* It performs autoupdate, includes the main.inc.php file and updates *$pwg_loaded_plugins*.
*
* @param string $plugin
*/
function load_plugin($plugin)
{
$file_name = PHPWG_PLUGINS_PATH.$plugin['id'].'/main.inc.php';
if ( file_exists($file_name) )
if (file_exists($file_name))
{
autoupdate_plugin($plugin);
global $pwg_loaded_plugins;
$pwg_loaded_plugins[ $plugin['id'] ] = $plugin;
include_once( $file_name );
include_once($file_name);
}
}

/**
* Performs update task of a plugin.
* Autoupdate is only performed if the plugin has a maintain.class.php file.
*
* @since 2.7
*
* @param array &$plugin (id, version, state) will be updated if version changes
*/
function autoupdate_plugin(&$plugin)
{
$maintain_file = PHPWG_PLUGINS_PATH.$plugin['id'].'/maintain.class.php';

// autoupdate is applicable only to plugins with 2.7 architecture
if (file_exists($maintain_file))
{
// try to find the filesystem version in lines 2 to 10 of main.inc.php
$fh = fopen(PHPWG_PLUGINS_PATH.$plugin['id'].'/main.inc.php', 'r');
$fs_version = null;
$i = -1;

while (($line = fgets($fh))!==false && $fs_version==null && $i<10)
{
$i++;
if ($i < 2) continue; // first lines are typically "<?php" and "/*"

if (preg_match('#Version: ([\\w.-]+)#', $line, $matches))
{
$fs_version = $matches[1];
}
}

fclose($fh);

if ($fs_version != null)
{
global $pwg_loaded_plugins, $page;

// if version is auto (dev) or superior
if (
$fs_version == 'auto' or $plugin['version'] == 'auto'
or safe_version_compare($plugin['version'], $fs_version, '<')
)
{
// call update method
include_once($maintain_file);

$classname = $plugin['id'].'_maintain';
$plugin_maintain = new $classname($plugin['id']);
$plugin_maintain->update($plugin['version'], $fs_version, $page['errors']);

$plugin['version'] = $fs_version;

// update database (only on production)
if ($plugin['version'] != 'auto')
{
$query = '
UPDATE '. PLUGINS_TABLE .'
SET version = "'. $plugin['version'] .'"
WHERE id = "'. $plugin['id'] .'"
;';
pwg_query($query);
}
}
}
}
}

Expand Down

0 comments on commit 6b75354

Please sign in to comment.