cvs: pear /PEAR_PackageUpdate PackageUpdate.php package2.xml
| From: | Scott Mattocks | Date: | Tue, 28 Mar 2006 14:53:08 +0000 |
| Subject: | cvs: pear /PEAR_PackageUpdate PackageUpdate.php package2.xml | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-39387@lists.php.net to get a copy of this message | ||
scottmattocks Tue Mar 28 14:53:08 2006 UTC
Added files:
/pear/PEAR_PackageUpdate PackageUpdate.php package2.xml
Log:
Initial commit of PEAR_PackageUpdate.
http://cvs.php.net/viewcvs.cgi/pear/PEAR_PackageUpdate/PackageUpdate.php?view=markup&rev=1.1 Index: pear/PEAR_PackageUpdate/PackageUpdate.php +++ pear/PEAR_PackageUpdate/PackageUpdate.php <?php /** * A package to make adding self updating functionality to other * packages easy. * * The package allows a developer to add a few lines of code to * their existing packages and have the ability to update their * package automatically. This auto-update ability allows users * to stay up to date much more easily than requiring them to * update manually. * * This package keeps track of user preferences such as "don't * remind me again". * * This package is designed to be a backend to different front * ends written for this package. For example, this package can * be used to drive a PHP-GTK 2, CLI or web front end. The API * for this package should be flexible enough to allow any type * of front end to be used and also to allow the package to be * used directly in another package without a front end driver. * * The interface for this package must allow for the following * functionality: * - check to see if a new version is available for a given * package on a given channel * - check minimum state * - present information regarding the upgrade (version, size) * - inform user about dependencies * - allow user to confirm or cancel upgrade * - download and install the package * - track preferences on a per package basis * - don't ask again * - don't ask until next release * - only ask for state XXXX or higher * - bug/minor/major updates only * - update channel automatically * - force application to exit when upgrade complete * - PHP-GTK/CLI apps must exit to allow classes to reload * - web front end could send headers to reload certain page * * This class is simply a wrapper for PEAR classes that actually * do the work. * * EXAMPLE: * <code> * <?php * class Goo { * function __construct() * { * // Check for updates... * require_once 'PEAR/PackageUpdate.php'; * $ppu =& PEAR_PackageUpdate::factory('Gtk2', 'Goo', 'pear'); * if ($ppu !== false) { * if ($ppu->checkUpdate()) { * // Use a dialog window to ask permission to update. * if ($ppu->presentUpdate()) { * if ($ppu->update()) { * // If the update succeeded, the application should * // be restarted. * $ppu->forceExit(); * } * } * } * } * // ... * } * // ... * } * ?> * </code> * * @author Scott Mattocks * @package PEAR_PackageUpdate * @version @version@ * @license PHP License 3 * @copyright Copyright 2006 Scott Mattocks */ // Constants for preferences. define('PEAR_PACKAGEUPDATE_PREF_NOUPDATES', 0); define('PEAR_PACKAGEUPDATE_PREF_NEXTRELEASE', 1); define('PEAR_PACKAGEUPDATE_PREF_TYPE', 2); define('PEAR_PACKAGEUPDATE_PREF_STATE', 3); // Constants for states. define('PEAR_PACKAGEUPDATE_STATE_DEVEL', 'devel'); define('PEAR_PACKAGEUPDATE_STATE_ALPHA', 'alpha'); define('PEAR_PACKAGEUPDATE_STATE_BETA', 'beta'); define('PEAR_PACKAGEUPDATE_STATE_STABLE', 'stable'); // Constants for release types. define('PEAR_PACKAGEUPDATE_TYPE_BUG', 'bug'); define('PEAR_PACKAGEUPDATE_TYPE_MINOR', 'minor'); define('PEAR_PACKAGEUPDATE_TYPE_MAJOR', 'major'); // Constants for errors. define('PEAR_PACKAGEUPDATE_ERROR_NOPACKAGE', -1); define('PEAR_PACKAGEUPDATE_ERROR_NOCHANNEL', -2); define('PEAR_PACKAGEUPDATE_ERROR_NOINFO', -3); define('PEAR_PACKAGEUPDATE_ERROR_NOTINSTALLED', -4); define('PEAR_PACKAGEUPDATE_ERROR_PREFFILE_READACCESS', -5); define('PEAR_PACKAGEUPDATE_ERROR_PREFFILE_WRITEACCESS', -6); define('PEAR_PACKAGEUPDATE_ERROR_PREFFILE_WRITEERROR', -7); define('PEAR_PACKAGEUPDATE_ERROR_PREFFILE_CORRUPTED', -8); define('PEAR_PACKAGEUPDATE_ERROR_INVALIDTYPE', -9); define('PEAR_PACKAGEUPDATE_ERROR_INVALIDSTATE', -10); define('PEAR_PACKAGEUPDATE_ERROR_INVALIDPREF', -11); // Error messages. $GLOBALS['_PEAR_PACKAGEUPDATE_ERRORS'] = array(PEAR_PACKAGEUPDATE_ERROR_NOPACKAGE => 'No package name provided', PEAR_PACKAGEUPDATE_ERROR_NOCHANNEL => 'No channel name provided', PEAR_PACKAGEUPDATE_ERROR_NOINFO => 'No update information is available for %packagename%.', PEAR_PACKAGEUPDATE_ERROR_NOTINSTALLED => '%packagename% is not installed. It cannot be updated.', PEAR_PACKAGEUPDATE_ERROR_PREFFILE_READACCESS => 'Preferences cannot be read from %file%.', PEAR_PACKAGEUPDATE_ERROR_PREFFILE_WRITEACCESS => 'Preferences cannot be written to %file% because of access permission errors.', PEAR_PACKAGEUPDATE_ERROR_PREFFILE_WRITEERROR => 'An error occurred while trying to write the preferences to %file%.', PEAR_PACKAGEUPDATE_ERROR_PREFFILE_CORRUPTED => 'Preferences file is corrupted.', PEAR_PACKAGEUPDATE_ERROR_INVALIDTYPE => 'Invalid release type: %type%', PEAR_PACKAGEUPDATE_ERROR_INVALIDSTATE => 'Invalid release state: %state%', PEAR_PACKAGEUPDATE_ERROR_INVALIDPREF => 'Invalid preference: %preference%' ); class PEAR_PackageUpdate { /** * The user's update preferences. * * @access public * @var array */ var $preferences = array(); /** * The name of the package. * * @access public * @var string */ var $packageName; /** * The channel the package is hosted on. * * @access public * @var string */ var $channel; /** * The latest version available for the given package. * * @access public * @var string */ var $latestVersion; /** * Information about the latest version of the package. * * @access public * @var array */ var $info = array(); /** * The current installed version of the package. * * @access public * @var string */ var $instVersion; /** * A collection of errors that have occurred. * * @access public * @var object */ var $errors; /** * PHP 4 style constructor. Calls the PHP 5 style constructor. * * @access public * @param string $packageName The package to update. * @param string $channel The channel the package resides on. * @return void */ function PEAR_PackageUpdate($packageName, $channel) { PEAR_PackageUpdate::__construct($packageName, $channel); } /** * PHP 5 style constructor. Loads the user preferences. * * @access public * @param string $packageName The package to update. * @param string $channel The channel the package resides on. * @return void */ function __construct($packageName, $channel) { // Create a pear error stack. require_once 'PEAR/ErrorStack.php'; $this->errors =& PEAR_ErrorStack::singleton(get_class($this)); // Set the package name and channel. $this->packageName = $packageName; $this->channel = $channel; // Load the user's preferences. $this->loadPreferences(); } /** * Creates an instance of the given update class. * * @access public * @param string $driver The type of PPU to create. * @param string $packageName The package to update. * @param string $channel The channel the package resides on. * @return object An instance of type PEAR_PackageUpdate_$driver */ function &factory($driver, $packageName, $channel) { // Try to include the driver. $file = 'PEAR/PackageUpdate/' . $driver . '.php'; include_once $file; // See if the class exists now. $class = 'PEAR_PackageUpdate_' . $driver; if (!class_exists($class)) { return false; } // Try to instantiate the class. $instance =& new $class($packageName, $channel); return $instance; } /** * Loads the user's preferences from a file. * * The preferences are stored in the user's home directory * as the file .ppurc. The file contains a serialized array * of preferences for each package that has been checked for * updates so far. * * @access protected * @return boolean true on success, false on error */ function loadPreferences() { // Get the preferences file. $prefFile = PEAR_PackageUpdate::determinePrefFile(); // Make sure the prefFile exists. if (!@file_exists($prefFile)) { // Try to create it by saving the current preferences (probably // just an empty array). $this->savePreferences(); } // Make sure the prefFile is readable. if (!@is_readable($prefFile)) { $this->pushError(PEAR_PACKAGEUPDATE_ERROR_PREFFILE_READACCESS, NULL, array('file' => $prefFile) ); return false; } // Get the contents of the prefFile. $contents = file_get_contents($prefFile); // Unserialize the data. $preferences = unserialize($contents); // Make sure the contents were unserialized properly. // Borrowed from PEAR_Config. if (!$preferences && strlen($contents) > 7) { $this->pushError(PEAR_PACKAGEUPDATE_ERROR_PREFFILE_CORRUPTED); return false; } $this->preferences = $preferences; return true; } /** * Returns the path to the preferences file. * * @access protected * @return string */ function determinePrefFile() { // Determine the preferences file. // Borrowed from PEAR_Config require_once 'PEAR.php'; require_once 'PEAR/Config.php'; $ds = DIRECTORY_SEPARATOR; if (OS_WINDOWS) { $prefFile = PEAR_CONFIG_SYSCONFDIR . $ds . 'ppurc.ini'; } else { $prefFile = getenv('HOME') . $ds . '.ppurc'; } return $prefFile; } /** * Checks to see if an update is available. * * Respects the user preferences when determining if an * update is avaiable. Returns true if an update is available * and the user may want to update the package. * * @access public * @return boolean true if an update is available. */ function checkUpdate() { // Check to see if an update is available. if (empty($this->latestVersion) || empty ($this->info)) { if (!$this->getPackageInfo()) { return false; } } // See if the installed version is older than the current version. if (version_compare($this->latestVersion, $this->instVersion, '>')) { // Check to see if the user's preferences allow an update. if (!$this->preferencesAllowUpdate()) { // User doesn't want to update to the latest version. return false; } return true; } else { return false; } } /** * Returns the latest information about the given package. * * @access protected * @return boolean true on success, false on error */ function getPackageInfo() { // Only check once. if (isset($this->latestVersion) && isset($this->info)) { return true; } // Make sure the channel and package are set. if (empty($this->packageName)) { $this->pushError(PEAR_PACKAGEUPDATE_ERROR_NOPACKAGE); return false; } if (empty($this->channel)) { $this->pushError(PEAR_PACKAGEUPDATE_ERROR_NOCHANNEL); return false; } // Create a config object. require_once 'PEAR/Config.php'; $config = new PEAR_Config(); // Get the config's registry object. $reg = $config->getRegistry(); // Parse the package name. $parsed = $reg->parsePackageName($this->channel . '/' . $this->packageName); // Check for errors. if (PEAR::isError($parsed)) { $this->pushError($parsed); return false; } // Get a PEAR_Remote instance. $r = $config->getRemote(); // Get the package info. $info = $r->call('package.info', $parsed['package']); // Check to make sure the package was found. if (PEAR::isError($info)) { $this->pushError(PEAR_PACKAGEUPDATE_ERROR_NOINFO, NULL, array('packagename' => $this->packageName) ); return false; } // Get the installed version of the package. $this->instVersion = $reg->packageInfo($parsed['package'], 'version', $parsed['channel'] ); // If the package is not installed, create a dummy version. if (empty($this->instVersion)) { $this->instVersion = '0.0.0'; } // Pull out the latest information. $this->latestVersion = reset(array_keys($info['releases'])); $this->info = reset($info['releases']); return true; } /** * Returns the preferences associated with the given package. * * The preferences returned are an array with the folling values: * - don't ask again * - don't ask until next version * - only ask for state x * - bug/minor/major updates only * * @access public * @return array */ function getPackagePreferences() { if (isset($this->preferences[$this->channel][$this->packageName])) { return $this->preferences[$this->channel][$this->packageName]; } else { return array(); } } /** * Saves the current prefernces to the RC file. * * @access public * @return boolean true on success, false on error */ function savePreferences() { // Get the file to save the preferences to. $prefFile = PEAR_PackageUpdate::determinePrefFile(); // Open the file for writing. $fp = fopen($prefFile, 'w'); if ($fp === false) { $this->pushError(PEAR_PACKAGEUPDATE_ERROR_PREFFILE_WRITEACCESS, NULL, array('file' => $prefFile) ); return false; } // Serialize the contents. $serialCont = serialize($this->preferences); // Write the contents to the file. if (fwrite($fp, $serialCont) === false) { $this->pushError(PEAR_PACKAGEUPDATE_ERROR_PREFFILE_WRITEERROR, NULL, array('file' => $prefFile) ); return false; } // Close the file. if (!fclose($fp)) { $this->pushError(PEAR_PACKAGEUPDATE_ERROR_PREFFILE_WRITEERROR, NULL, array('file' => $prefFile) ); return false; } else { return true; } } /** * Returns whether or not the user's preferences will allow an update to * take place. * * The user's preferences may define restrictions such as: * - don't update * - don't ask until next version (remembers last version asked) * - only ask for state XXXX or higher * - minor or higher (no bug fix) * - major only * * @access public * @return boolean true if the preferences will allow an update for the * latest version. */ function preferencesAllowUpdate() { // Get the preferences for the package. $prefs = $this->getPackagePreferences(); // Check to see if the user wants to be notified about any updates for // this package. if (isset($prefs[PEAR_PACKAGEUPDATE_PREF_NOUPDATES]) && $prefs[PEAR_PACKAGEUPDATE_PREF_NOUPDATES] ) { return false; } // Check to see if the user has requested not to be asked until a new // version is released. if (isset($prefs[PEAR_PACKAGEUPDATE_PREF_NEXTRELEASE]) && !version_compare($this->latestVersion, $prefs[PEAR_PACKAGEUPDATE_PREF_NEXTRELEASE], '>') ) { return false; } // Check to see if the user has requested not to be asked about the // state of the latest release. // Create an array of states. $states = array(PEAR_PACKAGEUPDATE_STATE_DEVEL => 0, PEAR_PACKAGEUPDATE_STATE_ALPHA => 1, PEAR_PACKAGEUPDATE_STATE_BETA => 2, PEAR_PACKAGEUPDATE_STATE_STABLE => 3 ); if (isset($prefs[PEAR_PACKAGEUPDATE_PREF_STATE]) && $states[$prefs[PEAR_PACKAGEUPDATE_PREF_STATE]] > $states[$this->info['state']] ) { return false; } // Check to see if the user only wants to be asked about a certain // type of release (minor|major). // Create an array for the types of releases. $releases = array(PEAR_PACKAGEUPDATE_TYPE_BUG => 0, PEAR_PACKAGEUPDATE_TYPE_MINOR => 1, PEAR_PACKAGEUPDATE_TYPE_MAJOR => 2 ); if (isset($prefs[PEAR_PACKAGEUPDATE_PREF_TYPE]) && $releases[$prefs[PEAR_PACKAGEUPDATE_PREF_TYPE]] > $releases[$this->releaseType()] ) { return false; } // If we got down here, either there are no preferences for this // package or everything checked out. return true; } /** * Returns the type of release. (bug|minor|major); * * @access protected * @return string */ function releaseType() { // Break the two versions into pieces. $latest = explode('.', $this->latestVersion); $current = explode('.', $this->instVersion); if ($latest[0] > $current[0]) { $type = PEAR_PACKAGEUPDATE_TYPE_MAJOR; } elseif ($latest[1] > $current[1]) { $type = PEAR_PACKAGEUPDATE_TYPE_MINOR; } else { $type = PEAR_PACKAGEUPDATE_TYPE_BUG; } return $type; } /** * Sets the user's preference for asking about all updates for this * package. * * @access public * @param boolean $dontAsk * @return boolean true on success, false on failure */ function setDontAskAgain($dontAsk) { // Make sure the value is a boolean. settype($dontAsk, 'boolean'); // Set the preference. return $this->setPreference(PEAR_PACKAGEUPDATE_PREF_NOUPDATES, $dontAsk); } /** * Sets the user's preference for asking about updates again until the next * release. * * @access public * @param boolean $nextrelease * @return boolean true on success, false on failure */ function setDontAskUntilNextRelease($nextrelease) { // If nextrelease is true, we have to swap its value out with the // latest version. if ($nextrelease) { // Make sure that the package info was found. if (empty($this->latestVersion)) { $this->pushError(PEAR_PACKAGEUPDATE_ERROR_NOINFO); return false; } $nextrelease = $this->latestVersion; } // Set the preference. return $this->setPreference(PEAR_PACKAGEUPDATE_PREF_NEXTRELEASE, $nextrelease); } /** * Sets the user's preference for asking about release types. * * @access public * @param string $minType The minimum release type to allow. * @return boolean true on success, false on failure */ function setMinimumRleaseType($minType) { // Make sure the type is acceptable. if ($minType != PEAR_PACKAGE_TYPE_BUG && $minType != PEAR_PACKAGE_TYPE_MINOR && $minType != PEAR_PACKAGE_TYPE_MAJOR ) { $this->pushError(PEAR_PACKAGEUPATE_ERROR_INVALIDTYPE, NULL, array('type' => $minType) ); return false; } // Set the preference. return $this->setPreference(PEAR_PACKAGEUPDATE_PREF_TYPE, $minType); } /** * Sets the user's preference for asking about release states. * * @access public * @param string $minState The minimum release state to allow. * @return boolean true on success, false on failure */ function setMinimumState($minState) { // Make sure the type is acceptable. if ($minState != PEAR_PACKAGE_STATE_DEVEL && $minState != PEAR_PACKAGE_STATE_ALPHA && $minState != PEAR_PACKAGE_STATE_BETA && $minState != PEAR_PACKAGE_STATE_STABLE ) { $this->pushError(PEAR_PACKAGEUPDATE_ERROR_INVALIDSTATE, NULL, array('state' => $minState) ); return false; } // Set the preference. return $this->setPreference(PEAR_PACKAGEUPDATE_PREF_STATE, $minState); } /** * Sets the given preference to the given value. * * Don't take any chances. Anytime a preference is set, the preferences are * saved. We can't rely on the developer to call savePreferences. * * @access protected * @param integer $pref One of the preference constants. * @param mixed $value The value of the preference. * @return boolean true if the preference was set and saved properly. */ function setPreference($pref, $value) { // Make sure the preference is valid. if ($pref != PEAR_PACKAGEUPDATE_PREF_NOUPDATES && $pref != PEAR_PACKAGEUPDATE_PREF_NEXTRELEASE && $pref != PEAR_PACKAGEUPDATE_PREF_TYPE && $pref != PEAR_PACKAGEUPDATE_PREF_STATE ) { // Invalid preference! $this->pushError(PEAR_PACKAGEUPDATE_INVALIDPREF, NULL, array('preference' => $pref) ); return false; } // Make sure the preferences for the channel exist. if (!isset($this->preferences[$this->channel])) { $this->preferences[$this->channel] = array(); } // Make sure the preferences for the package exist. if (!isset($this->preferences[$this->channel][$this->packageName])) { $this->preferences[$this->channel][$this->packageName] = array(); } // Set the preference value. $this->preferences[$this->channel][$this->packageName][$pref] = $value; // Save the preferences. return $this->savePreferences(); } /** * Sets all preferences at once. * * @access public * @param array $preferences * @return boolean true if the preferences were set and saved. */ function setPreferences($preferences) { // Make sure preferences is an array. settype($preferences, 'array'); // Make sure there is only valid preference information. foreach ($preferences as $pref => $value) { if ($pref != PEAR_PACKAGEUPDATE_PREF_NOUPDATES && $pref != PEAR_PACKAGEUPDATE_PREF_NEXTRELEASE && $pref != PEAR_PACKAGEUPDATE_PREF_TYPE && $pref != PEAR_PACKAGEUPDATE_PREF_STATE ) { unset($preferences[$pref]); } } // Make sure that next release has the latest release. if ($preferences[PEAR_PACKAGEUPDATE_PREF_NEXTRELEASE]) { $preferences[PEAR_PACKAGEUPDATE_PREF_NEXTRELEASE] = $this->latestVersion; } // Set the preferences. $this->preferences[$this->channel][$this->packageName] = $preferences; // Save the preferences. return $this->savePreferences(); } /** * Updates the source for the package. * * We have to update required dependencies automatically to make sure that * everything still works properly. * * It is the developers responsibility to make sure the user is given the * option to update any optional dependencies if needed. This can be done * by creating a new instance of PEAR_PackageUpdate for the optional * dependency. * * @access public * @return boolean true if the update was successful. */ function update() { // Create a config object. require_once 'PEAR/Config.php'; $config = new PEAR_Config(); // Change the verbosity but keep track of the value to reset it just in // case this does something permanent. $verbose = $config->get('verbose'); $config->set('verbose', 0); // Create a command object to do the upgrade. // If the current version is 0.0.0 don't upgrade. That would be a // sneaky way for devs to install packages without the use knowing. if ($this->instVersion == '0.0.0') { $this->pushError(PEAR_PACKAGEUPDATE_ERROR_NOTINSTALLED, NULL, array('packagename' => $this->packageName) ); return false; } require_once 'PEAR/Command.php'; $upgrade = PEAR_Command::factory('upgrade', $config); // Try to upgrade the application. $channelPackage = $this->channel . '/' . $this->packageName; $result = $upgrade->doInstall('upgrade', array('onlyreqdeps' => true), array($channelPackage)); // Reset the verbose level just to be safe. $config->set('verbose', $verbose); // Check for errors. if (PEAR::isError($result)) { $this->pushError($result); return false; } else { return true; } } /** * Redirects or exits to force the user to restart the application. * * @abstract * @access public * @return void */ function forceRestart() { $this->pushError('forceRestart is an abstract method that must be' . ' overridden in the child class.'); } /** * Presents the user with the option to update. * * @abstract * @access public * @return boolean true if the user wants to update */ function presentUpdate() { $this->pushError('presentUpdate is an abstract method that must be' . ' overridden in the child class.'); // This var_dump is here for demo purposes during the PEPr proposal // process. It should be removed before the first release. var_dump($this->info); // Return false just in case something odd has happened. return false; } /** * Pushes an error onto an error stack. * * This method is just for collecting errors that occur while checking for * updates and updating a package. The child class is responsible for * displaying all errors and handling them properly. This is because the * way errors are handled varies greatly depending on the driver used. * * @access public * @param int $code Package-specific error code * @param string $level Error level. This is NOT spell-checked * @param array $params associative array of error parameters * @param string $msg Error message, or a portion of it if the * message is to be generated * @param array $repackage If this error re-packages an error pushed by * another package, place the array returned from * {@link pop()} in this parameter * @param array $backtrace Protected parameter: use this to pass in the * {@link debug_backtrace()} that should be used * to find error context * @return PEAR_Error|array|Exception * if compatibility mode is on, a PEAR_Error is * also thrown. If the class Exception exists, * then one is returned. */ function pushError($code, $level = 'error', $params = array(), $msg = false, $repackage = false, $backtrace = false) { // Check to see if a PEAR_Error was pushed. if (PEAR::isError($code)) { return $this->repackagePEAR_Error($code); } // Check the arguments to see if just a code was submitted. if (is_int($code) && !(bool) $msg && isset($GLOBALS['_PEAR_PACKAGEUPDATE_ERRORS'][$code]) ) { $msg = $GLOBALS['_PEAR_PACKAGEUPDATE_ERRORS'][$code]; } // Append the error onto the stack. return $this->errors->push($code, null, $params, $msg, $repackage, $backtrace ); } /** * Repackages PEAR_Errors for use with ErrorStack. * * @author Ian Eure * * @access public * @param object $error A PEAR_Error * @return mixed The return from PEAR::ErrorStack::push() */ function repackagePEAR_Error(&$error) { static $map; if (!isset($map)) { $map = array( E_ERROR => 'error', E_WARNING => 'warning', E_PARSE => 'exception', E_NOTICE => 'notice', E_CORE_ERROR => 'error', E_CORE_WARNING => 'warning', E_COMPILE_ERROR => 'exception', E_COMPILE_WARNING => 'warning', E_USER_ERROR => 'error', E_USER_WARNING => 'warning', E_USER_NOTICE => 'notice' ); } // Strip this function from the trace if (is_array($error->backtrace)) { array_shift($error->backtrace); $error->userinfo['backtrace'] =& $error->backtrace; } return $this->errors->push($error->code, $map[$error->level], $error->userinfo, $error->message, false, $error->backtrace ); } /** * Pops an error off the error stack. * * This method is just for collecting errors that occur while checking for * updates and updating a package. The child class is responsible for * displaying all errors and handling them properly. This is because the * way errors are handled varies greatly depending on the driver used. * * @access public * @return object A PEAR_Error instance or false if no errors exist. */ function popError() { return $this->errors->pop(); } /** * Returns whether or not errors have occurred (and been captured). * * @access public * @return boolean */ function hasErrors() { return $this->errors->hasErrors(); } } /* * Local variables: * tab-width: 4 * c-basic-offset: 4 * End: */ ?> http://cvs.php.net/viewcvs.cgi/pear/PEAR_PackageUpdate/package2.xml?view=markup&rev=1.1 Index: pear/PEAR_PackageUpdate/package2.xml +++ pear/PEAR_PackageUpdate/package2.xml <?xml version="1.0"?> <package packagerversion="1.4.0a11" version="2.0" xmlns="http://pear.php.net/dtd/package-2.0" xmlns:tasks="http://pear.php.net/dtd/tasks-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pear.php.net/dtd/tasks-1.0 http://pear.php.net/dtd/tasks-1.0.xsd http://pear.php.net/dtd/package-2.0 http://pear.php.net/dtd/package-2.0.xsd"> <name>PEAR_PackageUpdate</name> <channel>pear.php.net</channel> <summary>A simple way to update packages at run time.</summary> <description>PEAR_PackageUpdate (PPU) is designed to allow developers to easily include auto updating features for other packages and PEAR installable applications. PPU will check to see if a new version of a package is available and then ask the user if they would like to update the package. PPU uses PEAR to communicate with the channel server and to execute the update. PPU allows the end user to take some control over when they are notified about new releases. The PPU Preferences allow a user to tell PPU not to ask about certain types of releases (bug fixes, minor releases, etc.), not to ask about certain release states (devel, alpha, etc.), not to ask until the next release or not to ask again. PPU is just an engine for package updating. It should not be used directly. Instead one of the driver packages such as PEAR_PackageUpdate_Gtk2 should be used depending on the application or other package.</description> <lead> <name>Scott Mattocks</name> <user>scottmattocks</user> <email>scottmattocks@php.net</email> <active>yes</active> </lead> <date>2006-03-28</date> <version> <release>0.4.0a1</release> <api>0.4.0a1</api> </version> <stability> <release>alpha</release> <api>alpha</api> </stability> <license uri="http://www.php.net/license">PHP License</license> <notes>Initial PEAR release.</notes> <contents> <dir baseinstalldir="/PEAR" name="/"> <file baseinstalldir="/PEAR" name="PackageUpdate.php" role="php"> <tasks:replace from="@version@" to="version" type="package-info" /> </file> </dir> </contents> <dependencies> <required> <php> <min>4.1.0</min> </php> <pearinstaller> <min>1.4.8</min> </pearinstaller> <package> <name>PEAR</name> <channel>pear.php.net</channel> <min>1.4.8</min> </package> </required> <group hint="PHP-GTK 2 front end for PPU" name="gtk2"> <package> <name>PEAR_PackageUpdate_Gtk2</name> <channel>pear.php.net</channel> <min>0.3.0a1</min> </package> </group> </dependencies> <phprelease /> <changelog> <release> <version> <release>0.1.0</release> <api>0.1.0</api> </version> <stability> <release>devel</release> <api>devel</api> </stability> <date>2006-03-07</date> <license uri="http://www.php.net/license/3_0.txt">PHP License</license> <notes>Initial release for PEPr proposal.</notes> </release> <release> <version> <release>0.1.1</release> <api>0.1.1</api> </version> <stability> <release>devel</release> <api>devel</api> </stability> <date>2006-03-08</date> <license uri="http://www.php.net/license/3_0.txt">PHP License</license> <notes>Remove PHP 5 public tokens from method signatures.</notes> </release> <release> <version> <release>0.2.0</release> <api>0.2.0</api> </version> <stability> <release>devel</release> <api>devel</api> </stability> <date>2006-03-08</date> <license uri="http://www.php.net/license/3_0.txt">PHP License</license> <notes>Add error constants. Allow error codes and PEAR_Error instances to be passed to pushError. Update return values and docblocks. Fix Windows preference file name. Fix example in opening doc block.</notes> </release> <release> <version> <release>0.3.0</release> <api>0.3.0</api> </version> <stability> <release>devel</release> <api>devel</api> </stability> <date>2006-03-16</date> <license uri="http://www.php.net/license/3_0.txt">PHP License</license> <notes>Use PEAR::ErrorStack for error handling.</notes> </release> <release> <version> <release>0.4.0a1</release> <api>0.4.0a1</api> </version> <stability> <release>alpha</release> <api>alpha</api> </stability> <date>2006-03-28</date> <license uri="http://www.php.net/license/3_0.txt">PHP License</license> <notes>Initial PEAR release.</notes> </release> </changelog> </package>
http://cvs.php.net/viewcvs.cgi/pear/PEAR_PackageUpdate/PackageUpdate.php?view=markup&rev=1.1 Index: pear/PEAR_PackageUpdate/PackageUpdate.php +++ pear/PEAR_PackageUpdate/PackageUpdate.php <?php /** * A package to make adding self updating functionality to other * packages easy. * * The package allows a developer to add a few lines of code to * their existing packages and have the ability to update their * package automatically. This auto-update ability allows users * to stay up to date much more easily than requiring them to * update manually. * * This package keeps track of user preferences such as "don't * remind me again". * * This package is designed to be a backend to different front * ends written for this package. For example, this package can * be used to drive a PHP-GTK 2, CLI or web front end. The API * for this package should be flexible enough to allow any type * of front end to be used and also to allow the package to be * used directly in another package without a front end driver. * * The interface for this package must allow for the following * functionality: * - check to see if a new version is available for a given * package on a given channel * - check minimum state * - present information regarding the upgrade (version, size) * - inform user about dependencies * - allow user to confirm or cancel upgrade * - download and install the package * - track preferences on a per package basis * - don't ask again * - don't ask until next release * - only ask for state XXXX or higher * - bug/minor/major updates only * - update channel automatically * - force application to exit when upgrade complete * - PHP-GTK/CLI apps must exit to allow classes to reload * - web front end could send headers to reload certain page * * This class is simply a wrapper for PEAR classes that actually * do the work. * * EXAMPLE: * <code> * <?php * class Goo { * function __construct() * { * // Check for updates... * require_once 'PEAR/PackageUpdate.php'; * $ppu =& PEAR_PackageUpdate::factory('Gtk2', 'Goo', 'pear'); * if ($ppu !== false) { * if ($ppu->checkUpdate()) { * // Use a dialog window to ask permission to update. * if ($ppu->presentUpdate()) { * if ($ppu->update()) { * // If the update succeeded, the application should * // be restarted. * $ppu->forceExit(); * } * } * } * } * // ... * } * // ... * } * ?> * </code> * * @author Scott Mattocks * @package PEAR_PackageUpdate * @version @version@ * @license PHP License 3 * @copyright Copyright 2006 Scott Mattocks */ // Constants for preferences. define('PEAR_PACKAGEUPDATE_PREF_NOUPDATES', 0); define('PEAR_PACKAGEUPDATE_PREF_NEXTRELEASE', 1); define('PEAR_PACKAGEUPDATE_PREF_TYPE', 2); define('PEAR_PACKAGEUPDATE_PREF_STATE', 3); // Constants for states. define('PEAR_PACKAGEUPDATE_STATE_DEVEL', 'devel'); define('PEAR_PACKAGEUPDATE_STATE_ALPHA', 'alpha'); define('PEAR_PACKAGEUPDATE_STATE_BETA', 'beta'); define('PEAR_PACKAGEUPDATE_STATE_STABLE', 'stable'); // Constants for release types. define('PEAR_PACKAGEUPDATE_TYPE_BUG', 'bug'); define('PEAR_PACKAGEUPDATE_TYPE_MINOR', 'minor'); define('PEAR_PACKAGEUPDATE_TYPE_MAJOR', 'major'); // Constants for errors. define('PEAR_PACKAGEUPDATE_ERROR_NOPACKAGE', -1); define('PEAR_PACKAGEUPDATE_ERROR_NOCHANNEL', -2); define('PEAR_PACKAGEUPDATE_ERROR_NOINFO', -3); define('PEAR_PACKAGEUPDATE_ERROR_NOTINSTALLED', -4); define('PEAR_PACKAGEUPDATE_ERROR_PREFFILE_READACCESS', -5); define('PEAR_PACKAGEUPDATE_ERROR_PREFFILE_WRITEACCESS', -6); define('PEAR_PACKAGEUPDATE_ERROR_PREFFILE_WRITEERROR', -7); define('PEAR_PACKAGEUPDATE_ERROR_PREFFILE_CORRUPTED', -8); define('PEAR_PACKAGEUPDATE_ERROR_INVALIDTYPE', -9); define('PEAR_PACKAGEUPDATE_ERROR_INVALIDSTATE', -10); define('PEAR_PACKAGEUPDATE_ERROR_INVALIDPREF', -11); // Error messages. $GLOBALS['_PEAR_PACKAGEUPDATE_ERRORS'] = array(PEAR_PACKAGEUPDATE_ERROR_NOPACKAGE => 'No package name provided', PEAR_PACKAGEUPDATE_ERROR_NOCHANNEL => 'No channel name provided', PEAR_PACKAGEUPDATE_ERROR_NOINFO => 'No update information is available for %packagename%.', PEAR_PACKAGEUPDATE_ERROR_NOTINSTALLED => '%packagename% is not installed. It cannot be updated.', PEAR_PACKAGEUPDATE_ERROR_PREFFILE_READACCESS => 'Preferences cannot be read from %file%.', PEAR_PACKAGEUPDATE_ERROR_PREFFILE_WRITEACCESS => 'Preferences cannot be written to %file% because of access permission errors.', PEAR_PACKAGEUPDATE_ERROR_PREFFILE_WRITEERROR => 'An error occurred while trying to write the preferences to %file%.', PEAR_PACKAGEUPDATE_ERROR_PREFFILE_CORRUPTED => 'Preferences file is corrupted.', PEAR_PACKAGEUPDATE_ERROR_INVALIDTYPE => 'Invalid release type: %type%', PEAR_PACKAGEUPDATE_ERROR_INVALIDSTATE => 'Invalid release state: %state%', PEAR_PACKAGEUPDATE_ERROR_INVALIDPREF => 'Invalid preference: %preference%' ); class PEAR_PackageUpdate { /** * The user's update preferences. * * @access public * @var array */ var $preferences = array(); /** * The name of the package. * * @access public * @var string */ var $packageName; /** * The channel the package is hosted on. * * @access public * @var string */ var $channel; /** * The latest version available for the given package. * * @access public * @var string */ var $latestVersion; /** * Information about the latest version of the package. * * @access public * @var array */ var $info = array(); /** * The current installed version of the package. * * @access public * @var string */ var $instVersion; /** * A collection of errors that have occurred. * * @access public * @var object */ var $errors; /** * PHP 4 style constructor. Calls the PHP 5 style constructor. * * @access public * @param string $packageName The package to update. * @param string $channel The channel the package resides on. * @return void */ function PEAR_PackageUpdate($packageName, $channel) { PEAR_PackageUpdate::__construct($packageName, $channel); } /** * PHP 5 style constructor. Loads the user preferences. * * @access public * @param string $packageName The package to update. * @param string $channel The channel the package resides on. * @return void */ function __construct($packageName, $channel) { // Create a pear error stack. require_once 'PEAR/ErrorStack.php'; $this->errors =& PEAR_ErrorStack::singleton(get_class($this)); // Set the package name and channel. $this->packageName = $packageName; $this->channel = $channel; // Load the user's preferences. $this->loadPreferences(); } /** * Creates an instance of the given update class. * * @access public * @param string $driver The type of PPU to create. * @param string $packageName The package to update. * @param string $channel The channel the package resides on. * @return object An instance of type PEAR_PackageUpdate_$driver */ function &factory($driver, $packageName, $channel) { // Try to include the driver. $file = 'PEAR/PackageUpdate/' . $driver . '.php'; include_once $file; // See if the class exists now. $class = 'PEAR_PackageUpdate_' . $driver; if (!class_exists($class)) { return false; } // Try to instantiate the class. $instance =& new $class($packageName, $channel); return $instance; } /** * Loads the user's preferences from a file. * * The preferences are stored in the user's home directory * as the file .ppurc. The file contains a serialized array * of preferences for each package that has been checked for * updates so far. * * @access protected * @return boolean true on success, false on error */ function loadPreferences() { // Get the preferences file. $prefFile = PEAR_PackageUpdate::determinePrefFile(); // Make sure the prefFile exists. if (!@file_exists($prefFile)) { // Try to create it by saving the current preferences (probably // just an empty array). $this->savePreferences(); } // Make sure the prefFile is readable. if (!@is_readable($prefFile)) { $this->pushError(PEAR_PACKAGEUPDATE_ERROR_PREFFILE_READACCESS, NULL, array('file' => $prefFile) ); return false; } // Get the contents of the prefFile. $contents = file_get_contents($prefFile); // Unserialize the data. $preferences = unserialize($contents); // Make sure the contents were unserialized properly. // Borrowed from PEAR_Config. if (!$preferences && strlen($contents) > 7) { $this->pushError(PEAR_PACKAGEUPDATE_ERROR_PREFFILE_CORRUPTED); return false; } $this->preferences = $preferences; return true; } /** * Returns the path to the preferences file. * * @access protected * @return string */ function determinePrefFile() { // Determine the preferences file. // Borrowed from PEAR_Config require_once 'PEAR.php'; require_once 'PEAR/Config.php'; $ds = DIRECTORY_SEPARATOR; if (OS_WINDOWS) { $prefFile = PEAR_CONFIG_SYSCONFDIR . $ds . 'ppurc.ini'; } else { $prefFile = getenv('HOME') . $ds . '.ppurc'; } return $prefFile; } /** * Checks to see if an update is available. * * Respects the user preferences when determining if an * update is avaiable. Returns true if an update is available * and the user may want to update the package. * * @access public * @return boolean true if an update is available. */ function checkUpdate() { // Check to see if an update is available. if (empty($this->latestVersion) || empty ($this->info)) { if (!$this->getPackageInfo()) { return false; } } // See if the installed version is older than the current version. if (version_compare($this->latestVersion, $this->instVersion, '>')) { // Check to see if the user's preferences allow an update. if (!$this->preferencesAllowUpdate()) { // User doesn't want to update to the latest version. return false; } return true; } else { return false; } } /** * Returns the latest information about the given package. * * @access protected * @return boolean true on success, false on error */ function getPackageInfo() { // Only check once. if (isset($this->latestVersion) && isset($this->info)) { return true; } // Make sure the channel and package are set. if (empty($this->packageName)) { $this->pushError(PEAR_PACKAGEUPDATE_ERROR_NOPACKAGE); return false; } if (empty($this->channel)) { $this->pushError(PEAR_PACKAGEUPDATE_ERROR_NOCHANNEL); return false; } // Create a config object. require_once 'PEAR/Config.php'; $config = new PEAR_Config(); // Get the config's registry object. $reg = $config->getRegistry(); // Parse the package name. $parsed = $reg->parsePackageName($this->channel . '/' . $this->packageName); // Check for errors. if (PEAR::isError($parsed)) { $this->pushError($parsed); return false; } // Get a PEAR_Remote instance. $r = $config->getRemote(); // Get the package info. $info = $r->call('package.info', $parsed['package']); // Check to make sure the package was found. if (PEAR::isError($info)) { $this->pushError(PEAR_PACKAGEUPDATE_ERROR_NOINFO, NULL, array('packagename' => $this->packageName) ); return false; } // Get the installed version of the package. $this->instVersion = $reg->packageInfo($parsed['package'], 'version', $parsed['channel'] ); // If the package is not installed, create a dummy version. if (empty($this->instVersion)) { $this->instVersion = '0.0.0'; } // Pull out the latest information. $this->latestVersion = reset(array_keys($info['releases'])); $this->info = reset($info['releases']); return true; } /** * Returns the preferences associated with the given package. * * The preferences returned are an array with the folling values: * - don't ask again * - don't ask until next version * - only ask for state x * - bug/minor/major updates only * * @access public * @return array */ function getPackagePreferences() { if (isset($this->preferences[$this->channel][$this->packageName])) { return $this->preferences[$this->channel][$this->packageName]; } else { return array(); } } /** * Saves the current prefernces to the RC file. * * @access public * @return boolean true on success, false on error */ function savePreferences() { // Get the file to save the preferences to. $prefFile = PEAR_PackageUpdate::determinePrefFile(); // Open the file for writing. $fp = fopen($prefFile, 'w'); if ($fp === false) { $this->pushError(PEAR_PACKAGEUPDATE_ERROR_PREFFILE_WRITEACCESS, NULL, array('file' => $prefFile) ); return false; } // Serialize the contents. $serialCont = serialize($this->preferences); // Write the contents to the file. if (fwrite($fp, $serialCont) === false) { $this->pushError(PEAR_PACKAGEUPDATE_ERROR_PREFFILE_WRITEERROR, NULL, array('file' => $prefFile) ); return false; } // Close the file. if (!fclose($fp)) { $this->pushError(PEAR_PACKAGEUPDATE_ERROR_PREFFILE_WRITEERROR, NULL, array('file' => $prefFile) ); return false; } else { return true; } } /** * Returns whether or not the user's preferences will allow an update to * take place. * * The user's preferences may define restrictions such as: * - don't update * - don't ask until next version (remembers last version asked) * - only ask for state XXXX or higher * - minor or higher (no bug fix) * - major only * * @access public * @return boolean true if the preferences will allow an update for the * latest version. */ function preferencesAllowUpdate() { // Get the preferences for the package. $prefs = $this->getPackagePreferences(); // Check to see if the user wants to be notified about any updates for // this package. if (isset($prefs[PEAR_PACKAGEUPDATE_PREF_NOUPDATES]) && $prefs[PEAR_PACKAGEUPDATE_PREF_NOUPDATES] ) { return false; } // Check to see if the user has requested not to be asked until a new // version is released. if (isset($prefs[PEAR_PACKAGEUPDATE_PREF_NEXTRELEASE]) && !version_compare($this->latestVersion, $prefs[PEAR_PACKAGEUPDATE_PREF_NEXTRELEASE], '>') ) { return false; } // Check to see if the user has requested not to be asked about the // state of the latest release. // Create an array of states. $states = array(PEAR_PACKAGEUPDATE_STATE_DEVEL => 0, PEAR_PACKAGEUPDATE_STATE_ALPHA => 1, PEAR_PACKAGEUPDATE_STATE_BETA => 2, PEAR_PACKAGEUPDATE_STATE_STABLE => 3 ); if (isset($prefs[PEAR_PACKAGEUPDATE_PREF_STATE]) && $states[$prefs[PEAR_PACKAGEUPDATE_PREF_STATE]] > $states[$this->info['state']] ) { return false; } // Check to see if the user only wants to be asked about a certain // type of release (minor|major). // Create an array for the types of releases. $releases = array(PEAR_PACKAGEUPDATE_TYPE_BUG => 0, PEAR_PACKAGEUPDATE_TYPE_MINOR => 1, PEAR_PACKAGEUPDATE_TYPE_MAJOR => 2 ); if (isset($prefs[PEAR_PACKAGEUPDATE_PREF_TYPE]) && $releases[$prefs[PEAR_PACKAGEUPDATE_PREF_TYPE]] > $releases[$this->releaseType()] ) { return false; } // If we got down here, either there are no preferences for this // package or everything checked out. return true; } /** * Returns the type of release. (bug|minor|major); * * @access protected * @return string */ function releaseType() { // Break the two versions into pieces. $latest = explode('.', $this->latestVersion); $current = explode('.', $this->instVersion); if ($latest[0] > $current[0]) { $type = PEAR_PACKAGEUPDATE_TYPE_MAJOR; } elseif ($latest[1] > $current[1]) { $type = PEAR_PACKAGEUPDATE_TYPE_MINOR; } else { $type = PEAR_PACKAGEUPDATE_TYPE_BUG; } return $type; } /** * Sets the user's preference for asking about all updates for this * package. * * @access public * @param boolean $dontAsk * @return boolean true on success, false on failure */ function setDontAskAgain($dontAsk) { // Make sure the value is a boolean. settype($dontAsk, 'boolean'); // Set the preference. return $this->setPreference(PEAR_PACKAGEUPDATE_PREF_NOUPDATES, $dontAsk); } /** * Sets the user's preference for asking about updates again until the next * release. * * @access public * @param boolean $nextrelease * @return boolean true on success, false on failure */ function setDontAskUntilNextRelease($nextrelease) { // If nextrelease is true, we have to swap its value out with the // latest version. if ($nextrelease) { // Make sure that the package info was found. if (empty($this->latestVersion)) { $this->pushError(PEAR_PACKAGEUPDATE_ERROR_NOINFO); return false; } $nextrelease = $this->latestVersion; } // Set the preference. return $this->setPreference(PEAR_PACKAGEUPDATE_PREF_NEXTRELEASE, $nextrelease); } /** * Sets the user's preference for asking about release types. * * @access public * @param string $minType The minimum release type to allow. * @return boolean true on success, false on failure */ function setMinimumRleaseType($minType) { // Make sure the type is acceptable. if ($minType != PEAR_PACKAGE_TYPE_BUG && $minType != PEAR_PACKAGE_TYPE_MINOR && $minType != PEAR_PACKAGE_TYPE_MAJOR ) { $this->pushError(PEAR_PACKAGEUPATE_ERROR_INVALIDTYPE, NULL, array('type' => $minType) ); return false; } // Set the preference. return $this->setPreference(PEAR_PACKAGEUPDATE_PREF_TYPE, $minType); } /** * Sets the user's preference for asking about release states. * * @access public * @param string $minState The minimum release state to allow. * @return boolean true on success, false on failure */ function setMinimumState($minState) { // Make sure the type is acceptable. if ($minState != PEAR_PACKAGE_STATE_DEVEL && $minState != PEAR_PACKAGE_STATE_ALPHA && $minState != PEAR_PACKAGE_STATE_BETA && $minState != PEAR_PACKAGE_STATE_STABLE ) { $this->pushError(PEAR_PACKAGEUPDATE_ERROR_INVALIDSTATE, NULL, array('state' => $minState) ); return false; } // Set the preference. return $this->setPreference(PEAR_PACKAGEUPDATE_PREF_STATE, $minState); } /** * Sets the given preference to the given value. * * Don't take any chances. Anytime a preference is set, the preferences are * saved. We can't rely on the developer to call savePreferences. * * @access protected * @param integer $pref One of the preference constants. * @param mixed $value The value of the preference. * @return boolean true if the preference was set and saved properly. */ function setPreference($pref, $value) { // Make sure the preference is valid. if ($pref != PEAR_PACKAGEUPDATE_PREF_NOUPDATES && $pref != PEAR_PACKAGEUPDATE_PREF_NEXTRELEASE && $pref != PEAR_PACKAGEUPDATE_PREF_TYPE && $pref != PEAR_PACKAGEUPDATE_PREF_STATE ) { // Invalid preference! $this->pushError(PEAR_PACKAGEUPDATE_INVALIDPREF, NULL, array('preference' => $pref) ); return false; } // Make sure the preferences for the channel exist. if (!isset($this->preferences[$this->channel])) { $this->preferences[$this->channel] = array(); } // Make sure the preferences for the package exist. if (!isset($this->preferences[$this->channel][$this->packageName])) { $this->preferences[$this->channel][$this->packageName] = array(); } // Set the preference value. $this->preferences[$this->channel][$this->packageName][$pref] = $value; // Save the preferences. return $this->savePreferences(); } /** * Sets all preferences at once. * * @access public * @param array $preferences * @return boolean true if the preferences were set and saved. */ function setPreferences($preferences) { // Make sure preferences is an array. settype($preferences, 'array'); // Make sure there is only valid preference information. foreach ($preferences as $pref => $value) { if ($pref != PEAR_PACKAGEUPDATE_PREF_NOUPDATES && $pref != PEAR_PACKAGEUPDATE_PREF_NEXTRELEASE && $pref != PEAR_PACKAGEUPDATE_PREF_TYPE && $pref != PEAR_PACKAGEUPDATE_PREF_STATE ) { unset($preferences[$pref]); } } // Make sure that next release has the latest release. if ($preferences[PEAR_PACKAGEUPDATE_PREF_NEXTRELEASE]) { $preferences[PEAR_PACKAGEUPDATE_PREF_NEXTRELEASE] = $this->latestVersion; } // Set the preferences. $this->preferences[$this->channel][$this->packageName] = $preferences; // Save the preferences. return $this->savePreferences(); } /** * Updates the source for the package. * * We have to update required dependencies automatically to make sure that * everything still works properly. * * It is the developers responsibility to make sure the user is given the * option to update any optional dependencies if needed. This can be done * by creating a new instance of PEAR_PackageUpdate for the optional * dependency. * * @access public * @return boolean true if the update was successful. */ function update() { // Create a config object. require_once 'PEAR/Config.php'; $config = new PEAR_Config(); // Change the verbosity but keep track of the value to reset it just in // case this does something permanent. $verbose = $config->get('verbose'); $config->set('verbose', 0); // Create a command object to do the upgrade. // If the current version is 0.0.0 don't upgrade. That would be a // sneaky way for devs to install packages without the use knowing. if ($this->instVersion == '0.0.0') { $this->pushError(PEAR_PACKAGEUPDATE_ERROR_NOTINSTALLED, NULL, array('packagename' => $this->packageName) ); return false; } require_once 'PEAR/Command.php'; $upgrade = PEAR_Command::factory('upgrade', $config); // Try to upgrade the application. $channelPackage = $this->channel . '/' . $this->packageName; $result = $upgrade->doInstall('upgrade', array('onlyreqdeps' => true), array($channelPackage)); // Reset the verbose level just to be safe. $config->set('verbose', $verbose); // Check for errors. if (PEAR::isError($result)) { $this->pushError($result); return false; } else { return true; } } /** * Redirects or exits to force the user to restart the application. * * @abstract * @access public * @return void */ function forceRestart() { $this->pushError('forceRestart is an abstract method that must be' . ' overridden in the child class.'); } /** * Presents the user with the option to update. * * @abstract * @access public * @return boolean true if the user wants to update */ function presentUpdate() { $this->pushError('presentUpdate is an abstract method that must be' . ' overridden in the child class.'); // This var_dump is here for demo purposes during the PEPr proposal // process. It should be removed before the first release. var_dump($this->info); // Return false just in case something odd has happened. return false; } /** * Pushes an error onto an error stack. * * This method is just for collecting errors that occur while checking for * updates and updating a package. The child class is responsible for * displaying all errors and handling them properly. This is because the * way errors are handled varies greatly depending on the driver used. * * @access public * @param int $code Package-specific error code * @param string $level Error level. This is NOT spell-checked * @param array $params associative array of error parameters * @param string $msg Error message, or a portion of it if the * message is to be generated * @param array $repackage If this error re-packages an error pushed by * another package, place the array returned from * {@link pop()} in this parameter * @param array $backtrace Protected parameter: use this to pass in the * {@link debug_backtrace()} that should be used * to find error context * @return PEAR_Error|array|Exception * if compatibility mode is on, a PEAR_Error is * also thrown. If the class Exception exists, * then one is returned. */ function pushError($code, $level = 'error', $params = array(), $msg = false, $repackage = false, $backtrace = false) { // Check to see if a PEAR_Error was pushed. if (PEAR::isError($code)) { return $this->repackagePEAR_Error($code); } // Check the arguments to see if just a code was submitted. if (is_int($code) && !(bool) $msg && isset($GLOBALS['_PEAR_PACKAGEUPDATE_ERRORS'][$code]) ) { $msg = $GLOBALS['_PEAR_PACKAGEUPDATE_ERRORS'][$code]; } // Append the error onto the stack. return $this->errors->push($code, null, $params, $msg, $repackage, $backtrace ); } /** * Repackages PEAR_Errors for use with ErrorStack. * * @author Ian Eure * * @access public * @param object $error A PEAR_Error * @return mixed The return from PEAR::ErrorStack::push() */ function repackagePEAR_Error(&$error) { static $map; if (!isset($map)) { $map = array( E_ERROR => 'error', E_WARNING => 'warning', E_PARSE => 'exception', E_NOTICE => 'notice', E_CORE_ERROR => 'error', E_CORE_WARNING => 'warning', E_COMPILE_ERROR => 'exception', E_COMPILE_WARNING => 'warning', E_USER_ERROR => 'error', E_USER_WARNING => 'warning', E_USER_NOTICE => 'notice' ); } // Strip this function from the trace if (is_array($error->backtrace)) { array_shift($error->backtrace); $error->userinfo['backtrace'] =& $error->backtrace; } return $this->errors->push($error->code, $map[$error->level], $error->userinfo, $error->message, false, $error->backtrace ); } /** * Pops an error off the error stack. * * This method is just for collecting errors that occur while checking for * updates and updating a package. The child class is responsible for * displaying all errors and handling them properly. This is because the * way errors are handled varies greatly depending on the driver used. * * @access public * @return object A PEAR_Error instance or false if no errors exist. */ function popError() { return $this->errors->pop(); } /** * Returns whether or not errors have occurred (and been captured). * * @access public * @return boolean */ function hasErrors() { return $this->errors->hasErrors(); } } /* * Local variables: * tab-width: 4 * c-basic-offset: 4 * End: */ ?> http://cvs.php.net/viewcvs.cgi/pear/PEAR_PackageUpdate/package2.xml?view=markup&rev=1.1 Index: pear/PEAR_PackageUpdate/package2.xml +++ pear/PEAR_PackageUpdate/package2.xml <?xml version="1.0"?> <package packagerversion="1.4.0a11" version="2.0" xmlns="http://pear.php.net/dtd/package-2.0" xmlns:tasks="http://pear.php.net/dtd/tasks-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pear.php.net/dtd/tasks-1.0 http://pear.php.net/dtd/tasks-1.0.xsd http://pear.php.net/dtd/package-2.0 http://pear.php.net/dtd/package-2.0.xsd"> <name>PEAR_PackageUpdate</name> <channel>pear.php.net</channel> <summary>A simple way to update packages at run time.</summary> <description>PEAR_PackageUpdate (PPU) is designed to allow developers to easily include auto updating features for other packages and PEAR installable applications. PPU will check to see if a new version of a package is available and then ask the user if they would like to update the package. PPU uses PEAR to communicate with the channel server and to execute the update. PPU allows the end user to take some control over when they are notified about new releases. The PPU Preferences allow a user to tell PPU not to ask about certain types of releases (bug fixes, minor releases, etc.), not to ask about certain release states (devel, alpha, etc.), not to ask until the next release or not to ask again. PPU is just an engine for package updating. It should not be used directly. Instead one of the driver packages such as PEAR_PackageUpdate_Gtk2 should be used depending on the application or other package.</description> <lead> <name>Scott Mattocks</name> <user>scottmattocks</user> <email>scottmattocks@php.net</email> <active>yes</active> </lead> <date>2006-03-28</date> <version> <release>0.4.0a1</release> <api>0.4.0a1</api> </version> <stability> <release>alpha</release> <api>alpha</api> </stability> <license uri="http://www.php.net/license">PHP License</license> <notes>Initial PEAR release.</notes> <contents> <dir baseinstalldir="/PEAR" name="/"> <file baseinstalldir="/PEAR" name="PackageUpdate.php" role="php"> <tasks:replace from="@version@" to="version" type="package-info" /> </file> </dir> </contents> <dependencies> <required> <php> <min>4.1.0</min> </php> <pearinstaller> <min>1.4.8</min> </pearinstaller> <package> <name>PEAR</name> <channel>pear.php.net</channel> <min>1.4.8</min> </package> </required> <group hint="PHP-GTK 2 front end for PPU" name="gtk2"> <package> <name>PEAR_PackageUpdate_Gtk2</name> <channel>pear.php.net</channel> <min>0.3.0a1</min> </package> </group> </dependencies> <phprelease /> <changelog> <release> <version> <release>0.1.0</release> <api>0.1.0</api> </version> <stability> <release>devel</release> <api>devel</api> </stability> <date>2006-03-07</date> <license uri="http://www.php.net/license/3_0.txt">PHP License</license> <notes>Initial release for PEPr proposal.</notes> </release> <release> <version> <release>0.1.1</release> <api>0.1.1</api> </version> <stability> <release>devel</release> <api>devel</api> </stability> <date>2006-03-08</date> <license uri="http://www.php.net/license/3_0.txt">PHP License</license> <notes>Remove PHP 5 public tokens from method signatures.</notes> </release> <release> <version> <release>0.2.0</release> <api>0.2.0</api> </version> <stability> <release>devel</release> <api>devel</api> </stability> <date>2006-03-08</date> <license uri="http://www.php.net/license/3_0.txt">PHP License</license> <notes>Add error constants. Allow error codes and PEAR_Error instances to be passed to pushError. Update return values and docblocks. Fix Windows preference file name. Fix example in opening doc block.</notes> </release> <release> <version> <release>0.3.0</release> <api>0.3.0</api> </version> <stability> <release>devel</release> <api>devel</api> </stability> <date>2006-03-16</date> <license uri="http://www.php.net/license/3_0.txt">PHP License</license> <notes>Use PEAR::ErrorStack for error handling.</notes> </release> <release> <version> <release>0.4.0a1</release> <api>0.4.0a1</api> </version> <stability> <release>alpha</release> <api>alpha</api> </stability> <date>2006-03-28</date> <license uri="http://www.php.net/license/3_0.txt">PHP License</license> <notes>Initial PEAR release.</notes> </release> </changelog> </package>