From 6b7535498cc9dd7645db5e781c7d32a6b4750cb1 Mon Sep 17 00:00:00 2001 From: mistic100 Date: Mon, 9 Jun 2014 17:20:43 +0000 Subject: feature 3076: Enhance plugin update system git-svn-id: http://piwigo.org/svn/trunk@28651 68402e56-0260-453c-a942-63ccdbb3a9ee --- admin/include/plugins.class.php | 41 +++++----- admin/include/themes.class.php | 14 +--- include/functions_plugins.inc.php | 157 +++++++++++++++++++------------------- 3 files changed, 103 insertions(+), 109 deletions(-) diff --git a/admin/include/plugins.class.php b/admin/include/plugins.class.php index f1a6ac510..036957e31 100644 --- a/admin/include/plugins.class.php +++ b/admin/include/plugins.class.php @@ -55,6 +55,7 @@ class DummyPlugin_maintain extends PluginMaintain return plugin_uninstall($this->plugin_id); } } + function update($old_version, $new_version, &$errors=array()) {} } @@ -85,28 +86,26 @@ class 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); } /** @@ -121,7 +120,7 @@ class plugins { $crt_db_plugin = $this->db_plugins_by_id[$plugin_id]; } - + $plugin_maintain = self::build_maintain_class($plugin_id); $errors = array(); @@ -187,7 +186,7 @@ UPDATE '. PLUGINS_TABLE .' WHERE id=\''. $plugin_id .'\' ;'; pwg_query($query); - + $plugin_maintain->deactivate(); break; @@ -206,7 +205,7 @@ DELETE FROM '. PLUGINS_TABLE .' WHERE id=\''. $plugin_id .'\' ;'; pwg_query($query); - + $plugin_maintain->uninstall(); break; @@ -235,7 +234,7 @@ DELETE FROM '. PLUGINS_TABLE .' /** * Get plugins defined in the plugin directory - */ + */ function get_fs_plugins() { $dir = opendir(PHPWG_PLUGINS_PATH); @@ -256,7 +255,7 @@ DELETE FROM '. PLUGINS_TABLE .' '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) ) { @@ -326,7 +325,7 @@ DELETE FROM '. PLUGINS_TABLE .' 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)) @@ -423,7 +422,7 @@ DELETE FROM '. PLUGINS_TABLE .' { return false; } - + global $conf; // Plugins to check @@ -477,7 +476,7 @@ DELETE FROM '. PLUGINS_TABLE .' } return false; } - + /** * Sort $server_plugins */ diff --git a/admin/include/themes.class.php b/admin/include/themes.class.php index 824ccf4b9..5389eef58 100644 --- a/admin/include/themes.class.php +++ b/admin/include/themes.class.php @@ -86,19 +86,11 @@ class themes 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); } /** diff --git a/include/functions_plugins.inc.php b/include/functions_plugins.inc.php index 91e5ec9fe..0ba15be2b 100644 --- a/include/functions_plugins.inc.php +++ b/include/functions_plugins.inc.php @@ -35,7 +35,7 @@ define('EVENT_HANDLER_PRIORITY_NEUTRAL', 50); /** * Used to declare maintenance methods of a plugin. */ -class PluginMaintain +class PluginMaintain { /** @var string $plugin_id */ protected $plugin_id; @@ -65,46 +65,17 @@ class PluginMaintain 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; @@ -126,43 +97,6 @@ class ThemeMaintain 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); - } - } - } } @@ -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) { @@ -399,23 +333,92 @@ SELECT * FROM '.PLUGINS_TABLE; $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 "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); + } + } + } } } -- cgit v1.2.3