Source for file PackageUpdate.php
Documentation is available at PackageUpdate.php
* A package to make adding self updating functionality to other
* LICENSE: This source file is subject to version 3.01 of the PHP license
* that is available through the world-wide-web at the following URI:
* http://www.php.net/license/3_01.txt. If you did not receive a copy of
* the PHP License and are unable to obtain it through the web, please
* send a note to license@php.net so we can mail you a copy immediately.
* CREDITS: To Ian Eure <ieure@php.net>
* for repackage PEAR_Errors for use with ErrorStack
* see repackagePEARError()
* @package PEAR_PackageUpdate
* @author Scott Mattocks <scottmattocks@php.net>
* @author Laurent Laville <pear@laurent-laville.org>
* @copyright 2006-2008 Scott Mattocks
* @license http://www.php.net/license/3_01.txt PHP License 3.01
* @version CVS: $Id: PackageUpdate.php,v 1.34 2008/06/14 22:01:45 farell Exp $
* @link http://pear.php.net/package/PEAR_PackageUpdate
* @since File available since Release 0.4.0a1
require_once 'PEAR/ErrorStack.php';
require_once 'PEAR/Config.php';
// PEAR_PACKAGEUPDATE_PREF_* Constants for preferences.
* Check to see if the user wants to be notified about any updates for
define('PEAR_PACKAGEUPDATE_PREF_NOUPDATES', 0 );
* Check to see if the user has requested not to be asked until a new
define('PEAR_PACKAGEUPDATE_PREF_NEXTRELEASE', 1 );
* Check to see if the user only wants to be asked about a certain
* type of release (bug|minor|major).
define('PEAR_PACKAGEUPDATE_PREF_TYPE', 2 );
* Check to see if the user has requested not to be asked about the
* state of the latest release.
define('PEAR_PACKAGEUPDATE_PREF_STATE', 3 );
// PEAR_PACKAGEUPDATE_STATE_* Constants for states.
* Check to see if the user has requested to be asked when
* the latest release is a snapshot release.
define('PEAR_PACKAGEUPDATE_STATE_SNAPSHOT', 'snapshot');
* Check to see if the user has requested to be asked when
* the latest release is a devel release.
define('PEAR_PACKAGEUPDATE_STATE_DEVEL', 'devel');
* Check to see if the user has requested to be asked when
* the latest release is an alpha release.
define('PEAR_PACKAGEUPDATE_STATE_ALPHA', 'alpha');
* Check to see if the user has requested to be asked when
* the latest release is an beta release.
define('PEAR_PACKAGEUPDATE_STATE_BETA', 'beta');
* Check to see if the user has requested to be asked when
* the latest release is a stable release.
define('PEAR_PACKAGEUPDATE_STATE_STABLE', 'stable');
// PEAR_PACKAGEUPDATE_TYPE_* Constants for release types.
* Check to see if the user only wants to be asked about a bug release
define('PEAR_PACKAGEUPDATE_TYPE_BUG', 'bug');
* Check to see if the user only wants to be asked about a minor release
define('PEAR_PACKAGEUPDATE_TYPE_MINOR', 'minor');
* Check to see if the user only wants to be asked about a major release
define('PEAR_PACKAGEUPDATE_TYPE_MAJOR', 'major');
// PEAR_PACKAGEUPDATE_ERROR_* Constants for errors.
* No package name provided
define('PEAR_PACKAGEUPDATE_ERROR_NOPACKAGE', -1 );
* No channel name provided
define('PEAR_PACKAGEUPDATE_ERROR_NOCHANNEL', -2 );
* No update information is available for the package
define('PEAR_PACKAGEUPDATE_ERROR_NOINFO', -3 );
* The package is not installed. It cannot be updated.
define('PEAR_PACKAGEUPDATE_ERROR_NOTINSTALLED', -4 );
* Preferences cannot be read from file because of access permission errors
define('PEAR_PACKAGEUPDATE_ERROR_PREFFILE_READACCESS', -5 );
* Preferences cannot be written to file because of access permission errors
define('PEAR_PACKAGEUPDATE_ERROR_PREFFILE_WRITEACCESS', -6 );
* An error occurred while trying to write the preferences to file
define('PEAR_PACKAGEUPDATE_ERROR_PREFFILE_WRITEERROR', -7 );
* Preferences file is corrupted
define('PEAR_PACKAGEUPDATE_ERROR_PREFFILE_CORRUPTED', -8 );
define('PEAR_PACKAGEUPDATE_ERROR_INVALIDTYPE', -9 );
define('PEAR_PACKAGEUPDATE_ERROR_INVALIDSTATE', -10 );
* Invalid preference identifier
define('PEAR_PACKAGEUPDATE_ERROR_INVALIDPREF', -11 );
* Driver (backend) could not be found.
define('PEAR_PACKAGEUPDATE_ERROR_NONEXISTENTDRIVER', -12 );
define('PEAR_PACKAGEUPDATE_ERROR_INVALIDINIFILE', -13 );
$GLOBALS['_PEAR_PACKAGEUPDATE_ERRORS'] =
'No package name provided',
'No channel name provided',
'No update information is available for %packagename%.',
'%packagename% is not installed. It cannot be updated.',
'Preferences cannot be read from %file%.',
'Preferences cannot be written to %file% ' .
'because of access permission errors.',
'An error occurred while trying to write the preferences to %file%.',
'Preferences file is corrupted.',
'Invalid release type: %type%',
'Invalid release state: %state%',
'Invalid preference: %preference%',
'Driver %drivername% could not be found.',
'Invalid (%layer%) INI file : %file%'
* 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
* This package keeps track of user preferences such as "don't
* 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
* - check to see if a new version is available for a given
* package on a given channel
* - 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 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
* // Check for updates...
* require_once 'PEAR/PackageUpdate.php';
* $ppu =& PEAR_PackageUpdate::factory('Gtk2', 'Goo', 'pear');
* if ($ppu->checkUpdate()) {
* // Use a dialog window to ask permission to update.
* if ($ppu->presentUpdate()) {
* // If the update succeeded, the application should
* @package PEAR_PackageUpdate
* @author Scott Mattocks <scottmattocks@php.net>
* @author Laurent Laville <pear@laurent-laville.org>
* @copyright 2006-2008 Scott Mattocks
* @license http://www.php.net/license/3_01.txt PHP License 3.01
* @version Release: 1.0.2
* @link http://pear.php.net/package/PEAR_PackageUpdate
* @since Class available since Release 0.4.0a1
* The user's update preferences.
* The file to read PPU user-defined options from
* The file to read PEAR user-defined options from
* The file to read PEAR system-wide defaults from
* The name of the package.
* The channel the package is hosted on.
* The latest version available for the given package.
* Information about the latest version of the package.
* The current installed version of the package.
* Informations about the current installed version of the package.
* @see PEAR_Registry::packageInfo()
* A collection of errors that have occurred.
* PHP 4 style constructor. Calls the PHP 5 style constructor.
* @param string $packageName The package to update.
* @param string $channel The channel the package resides on.
* @param string $user_file (optional) file to read PEAR user-defined
* @param string $system_file (optional) file to read PEAR system-wide
* @param string $pref_file (optional) file to read PPU user-defined
* @since version 0.4.0a1 (2006-03-28)
$user_file = '', $system_file = '', $pref_file = '')
$user_file, $system_file, $pref_file);
* PHP 5 style constructor. Loads the user preferences.
* @param string $packageName The package to update.
* @param string $channel The channel the package resides on.
* @param string $user_file (optional) file to read PEAR user-defined
* @param string $system_file (optional) file to read PEAR system-wide
* @param string $pref_file (optional) file to read PPU user-defined
* @since version 0.4.0a1 (2006-03-28)
* @throws PEAR_PACKAGEUPDATE_ERROR_INVALIDINIFILE
$user_file = '', $system_file = '', $pref_file = '')
// Create a pear error stack.
$this->errors->setContextCallback (array (&$this, '_getBacktrace'));
// Set the package name and channel.
array ('layer' => 'pear-user', 'file' => $user_file));
$user_file = ''; // force to use default user configuration
array ('layer' => 'pear-system', 'file' => $system_file));
$system_file = ''; // force to use default system configuration
// Set the file to read PEAR user-defined options from
// Set the file to read PEAR system-wide defaults from
// Set the file to read PPU user-defined options from
// Load the user's preferences.
* Callback that generates context information (location of error)
* for the package error stack.
* @return mixed backtrace context array or false is unavailable
* @since version 0.4.3 (2006-04-25)
$backtrace = $backtrace[count($backtrace)-1 ];
* Creates an instance of the given update class.
* @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.
* @param string $user_file (optional) file to read PEAR user-defined
* @param string $system_file (optional) file to read PEAR system-wide
* @param string $pref_file (optional) file to read PPU user-defined
* @return object An instance of type PEAR_PackageUpdate_$driver
* @since version 0.4.0a1 (2006-03-28)
* @throws PEAR_PACKAGEUPDATE_ERROR_NONEXISTENTDRIVER
function &factory($driver, $packageName, $channel,
$user_file = '', $system_file = '', $pref_file = '')
$class = 'PEAR_PackageUpdate_' . $driver;
// Attempt to include a custom version of the named class, but don't treat
// a failure as fatal. The caller may have already included their own
// version of the named class.
// Try to include the driver.
$file = 'PEAR/PackageUpdate/' . $driver . '.php';
PEAR_ErrorStack ::staticPush ('PEAR_PackageUpdate',
array ('drivername' => $driver),
$GLOBALS['_PEAR_PACKAGEUPDATE_ERRORS']
// Must assign a variable to avoid notice about references.
// See if the class exists now.
// Must assign a variable to avoid notice about references.
// Try to instantiate the class.
$instance = & new $class($packageName, $channel,
$user_file, $system_file, $pref_file);
* Returns whether or not a path is in the include path.
* @param string $path Path to filename to check if includable
* @return boolean true if the path is in the include path.
* @since version 0.4.2 (2006-04-20)
// Break up the include path and check to see if the path is readable.
// If we got down here, the path is not readable from the include path.
* 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
* @param string $pref_file (optional) file to read PPU user-defined options from
* @return boolean true on success, false on error
* @since version 0.4.0a1 (2006-03-28)
* @throws PEAR_PACKAGEUPDATE_ERROR_PREFFILE_READACCESS,
* PEAR_PACKAGEUPDATE_ERROR_PREFFILE_CORRUPTED,
* PEAR_PACKAGEUPDATE_ERROR_INVALIDINIFILE
array ('layer' => 'ppu-pref', 'file' => $pref_file));
$pref_file = ''; // force to use default PPU configuration
// Get the preferences file.
// Make sure the prefFile exists.
// Try to create it by saving the current preferences (probably
// Make sure the prefFile is readable.
null , array ('file' => $prefFile));
// Get the contents of the prefFile.
// Make sure the contents were unserialized properly.
// Borrowed from PEAR_Config.
if ($preferences === false ) {
* Returns the path to the preferences file.
* @since version 0.4.0a1 (2006-03-28)
// PEAR 1.7.0 introduced new feature like configuration directory (cfg_dir)
$cfgDir = $config->get ('cfg_dir');
// detect PEAR configuration directive (cfg_dir)
$cfgDir = PEAR_CONFIG_SYSCONFDIR;
$prefFile = $cfgDir . DIRECTORY_SEPARATOR . $cfgName;
* Checks to see if an update is available.
* Respects the user preferences when determining if an
* update is available. Returns true if an update is available
* and the user may want to update the package.
* @return boolean true if an update is available.
* @since version 0.4.0a1 (2006-03-28)
// Check to see if an update is available.
// See if the installed version is older than the current version.
// Check to see if the user's preferences allow an update.
// User doesn't want to update to the latest version.
* Returns the latest information about the given package.
* @return boolean true on success, false on error
* @since version 0.4.0a1 (2006-03-28)
* @throws PEAR_PACKAGEUPDATE_ERROR_NOPACKAGE,
* PEAR_PACKAGEUPDATE_ERROR_NOCHANNEL,
* PEAR_PACKAGEUPDATE_ERROR_NOINFO
// Make sure the channel and package are set.
// Create a config object.
// Get the config's registry object.
$reg = $config->getRegistry ($layer);
// Parse the package name.
if (PEAR ::isError ($parsed)) {
$chan = & $reg->getChannel ($this->channel);
$mirror = $config->get ('preferred_mirror');
if ($chan->supportsREST ($mirror)
&& $base = $chan->getBaseURL ('REST1.0', $mirror)) {
$rest = & $config->getREST ('1.0', array ());
$info = $rest->packageInfo ($base, $parsed['package']);
$r = & $config->getRemote ();
$info = $r->call ('package.info', $parsed['package']);
// Check to make sure the package was found.
if (PEAR ::isError ($info) || !isset ($info['name'])) {
// Get full installed data of the package.
$this->instInfo = $reg->packageInfo ($parsed['package'], null ,
if ($this->instInfo['xsdversion'] == '1.0') {
// If the package is not installed, create a dummy version.
// Pull out the latest information.
$this->info['summary'] = $info['summary'];
$this->info['description'] = $info['description'];
* Returns the preferences associated with the given package.
* The preferences returned are an array with the folling values:
* - don't ask until next version
* - bug/minor/major updates only
* @since version 0.4.0a1 (2006-03-28)
* Saves the current preferences to the RC file.
* @param string $pref_file (optional) file to save PPU user-defined options to
* @return boolean true on success, false on error
* @since version 0.4.0a1 (2006-03-28)
* @throws PEAR_PACKAGEUPDATE_ERROR_PREFFILE_WRITEACCESS,
* PEAR_PACKAGEUPDATE_ERROR_PREFFILE_WRITEERROR
* @see determinePrefFile()
// Get the file to save the preferences to.
// Open the file for writing.
$fp = fopen($prefFile, 'w');
null , array ('file' => $prefFile));
// Serialize the contents.
// Write the contents to the file.
if (fwrite($fp, $serialCont) === false ) {
null , array ('file' => $prefFile));
null , array ('file' => $prefFile));
* Returns whether or not the user's preferences will allow an update to
* The user's preferences may define restrictions such as:
* - don't ask until next version (remembers last version asked)
* - only ask for state XXXX or higher
* - minor or higher (no bug fix)
* @return boolean true if the preferences will allow an update for the
* @since version 0.4.0a1 (2006-03-28)
* @see getPackagePreferences()
// Get the preferences for the package.
// Check to see if the user wants to be notified about any updates for
// Check to see if the user has requested not to be asked until a new
// 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[$this->info['state']]
// Check to see if the user only wants to be asked about a certain
// type of release (bug|minor|major).
// Create an array for the types of releases.
// If we got down here, either there are no preferences for this
// package or everything checked out.
* Returns the type of release. (bug|minor|major);
* @since version 0.4.0a1 (2006-03-28)
// Break the two versions into pieces.
if ($latest[0 ] > $current[0 ]) {
} elseif ($latest[1 ] > $current[1 ]) {
* Returns informations about current installed version of the package
* @author Laurent Laville
* @return array|bool false on error, data array otherwise
* @since version 0.6.0 (2007-01-11)
// compatibility for package.xml version 2.0
$instInfo['packagerversion']
= $this->instInfo['attribs']['packagerversion'];
if (!isset ($instInfo['packagerversion'])) {
$instInfo['packagerversion'] = '';
'version' => $instInfo['version'],
'license' => $instInfo['release_license'],
'summary' => $this->instInfo['summary'],
'description' => $this->instInfo['description'],
'releasedate' => $instInfo['release_date'],
'releasenotes' => $instInfo['release_notes'],
'state' => $instInfo['release_state'],
'deps' => $instInfo['release_deps'],
'xsdversion' => $this->instInfo['xsdversion'],
'packagerversion' => $instInfo['packagerversion'],
* Returns information on latest version available
* @author Laurent Laville
* @return array|bool false on error, data array otherwise
* @since version 0.6.0 (2007-01-11)
* Sets the user's preference for asking about all updates for this
* @param boolean $dontAsk User's preference for asking about all updates
* @return boolean true on success, false on failure
* @since version 0.4.0a1 (2006-03-28)
// Make sure the value is a boolean.
* Sets the user's preference for asking about updates again until the next
* @param boolean $nextrelease User's preference for asking about updates again
* @return boolean true on success, false on failure
* @since version 0.4.0a1 (2006-03-28)
* @throws PEAR_PACKAGEUPDATE_ERROR_NOINFO
// If nextrelease is true, we have to swap its value out with the
// Make sure that the package info was found.
* Sets the user's preference for asking about release types.
* @param string $minType The minimum release type to allow.
* @return boolean true on success, false on failure
* @since version 0.4.0a1 (2006-03-28)
* @throws PEAR_PACKAGEUPDATE_ERROR_INVALIDTYPE
// Make sure the type is acceptable.
array ('type' => $minType));
* Sets the user's preference for asking about release states.
* @param string $minState The minimum release state to allow.
* @return boolean true on success, false on failure
* @since version 0.4.0a1 (2006-03-28)
* @throws PEAR_PACKAGEUPDATE_ERROR_INVALIDSTATE
// Make sure the type is acceptable.
array ('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.
* @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.
* @since version 0.4.0a1 (2006-03-28)
* @throws PEAR_PACKAGEUPDATE_ERROR_INVALIDPREF
// Make sure the preference is valid.
array ('preference' => $pref));
// Make sure the preferences for the channel exist.
// Make sure the preferences for the package exist.
// Set the preference value.
* Sets all preferences at once.
* @param array $preferences All user's preferences
* @return boolean true if the preferences were set and saved.
* @since version 0.4.0a1 (2006-03-28)
// Make sure preferences is an array.
// Make sure there is only valid preference information.
foreach ($preferences as $pref => $value) {
unset ($preferences[$pref]);
// Make sure that next release has the latest release.
* 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
* @return boolean true if the update was successful.
* @since version 0.4.0a1 (2006-03-28)
* @throws PEAR_PACKAGEUPDATE_ERROR_NOTINSTALLED
// Create a config object.
// 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.
include_once 'PEAR/Command.php';
$upgrade = PEAR_Command ::factory ('upgrade', $config);
// Try to upgrade the application.
$result = $upgrade->doInstall ('upgrade',
array ('onlyreqdeps' => true ),
// Reset the verbose level just to be safe.
$config->set ('verbose', $verbose);
if (PEAR ::isError ($result)) {
* Redirects or exits to force the user to restart the application.
* @since version 0.4.0a1 (2006-03-28)
$this->pushError('forceRestart is an abstract method that must be' .
' overridden in the child class.');
* Presents the user with the option to update.
* @return boolean true if the user wants to update
* @since version 0.4.0a1 (2006-03-28)
$this->pushError('presentUpdate is an abstract method that must be' .
' overridden in the child class.');
// Return false just in case something odd has happened.
* 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.
* @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
* @return PEAR_Error|array|Exception
* if compatibility mode is on, a PEAR_Error is
* also thrown. If the class Exception exists,
* @since version 0.4.0a1 (2006-03-28)
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)) {
// Check the arguments to see if just a code was submitted.
&& isset ($GLOBALS['_PEAR_PACKAGEUPDATE_ERRORS'][$code])
$msg = $GLOBALS['_PEAR_PACKAGEUPDATE_ERRORS'][$code];
// Append the error onto the stack.
return $this->errors->push ($code, $level, $params, $msg,
* Repackages PEAR_Errors for use with ErrorStack.
* @param object &$error A PEAR_Error
* @return mixed The return from PEAR::ErrorStack::push()
* @since version 0.4.0a1 (2006-03-28)
* @author Ian Eure <ieure@php.net>
E_CORE_WARNING => 'warning',
E_COMPILE_ERROR => 'exception',
E_COMPILE_WARNING => 'warning',
E_USER_WARNING => 'warning',
E_USER_NOTICE => 'notice'
// Strip this function from the trace
$error->userinfo ['backtrace'] = & $error->backtrace;
return $this->errors->push ($error->code , $map[$error->level ],
$error->userinfo , $error->message , false ,
* 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.
* @return mixed details of an error (array) or false if no errors exist.
* @since version 0.4.0a1 (2006-03-28)
* Returns whether or not errors have occurred (and been captured).
* @param string|array$level Level name. Use to determine if any errors
* of level (string), or levels (array)
* @since version 0.4.0a1 (2006-03-28)
return $this->errors->hasErrors ($level);
Documentation generated on Sun, 15 Jun 2008 10:30:06 -0400 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.
|