php-src/pear/PEAR/Command/Remote.php

359 lines
12 KiB
PHP
Raw Normal View History

<?php
2002-04-16 08:38:21 +08:00
// /* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// | PHP Version 4 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2002 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.02 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Author: Stig Bakken <ssb@fast.no> |
// | |
// +----------------------------------------------------------------------+
//
// $Id$
require_once 'PEAR/Command/Common.php';
2002-04-23 15:01:45 +08:00
require_once 'PEAR/Common.php';
require_once 'PEAR/Remote.php';
2002-06-02 21:07:19 +08:00
require_once 'PEAR/Registry.php';
class PEAR_Command_Remote extends PEAR_Command_Common
{
2002-05-20 19:37:33 +08:00
// {{{ command definitions
var $commands = array(
2002-05-21 09:38:50 +08:00
'remote-info' => array(
2002-05-20 19:37:33 +08:00
'summary' => 'Information About Remote Packages',
2002-05-21 09:38:50 +08:00
'function' => 'doRemoteInfo',
'shortcut' => 'ri',
2002-05-20 19:37:33 +08:00
'options' => array(),
'doc' => '<package>
Get details on a package from the server.',
2002-05-20 19:37:33 +08:00
),
'list-upgrades' => array(
'summary' => 'List Available Upgrades',
'function' => 'doListUpgrades',
2002-05-21 09:38:50 +08:00
'shortcut' => 'lu',
2002-05-20 19:37:33 +08:00
'options' => array(),
'doc' => '
List releases on the server of packages you have installed where
a newer version is available with the same release state (stable etc.).'
2002-05-20 19:37:33 +08:00
),
2002-05-21 09:38:50 +08:00
'remote-list' => array(
2002-05-20 19:37:33 +08:00
'summary' => 'List Remote Packages',
2002-05-21 09:38:50 +08:00
'function' => 'doRemoteList',
'shortcut' => 'rl',
2002-05-20 19:37:33 +08:00
'options' => array(),
'doc' => '
Lists the packages available on the configured server along with the
2002-06-02 21:07:19 +08:00
latest stable release of each package.',
),
'search' => array(
2002-06-25 00:03:45 +08:00
'summary' => 'Search remote package database',
2002-06-02 21:07:19 +08:00
'function' => 'doSearch',
'shortcut' => 'sp',
'options' => array(),
'doc' => '
2002-06-07 23:06:25 +08:00
Lists all packages which match the search paramteres (first param
2002-06-02 21:07:19 +08:00
is package name, second package info)',
),
'list-all' => array(
'summary' => 'List All Packages',
'function' => 'doListAll',
'shortcut' => 'la',
'options' => array(),
'doc' => '
Lists the packages available on the configured server along with the
latest stable release of each package.',
2002-05-20 19:37:33 +08:00
),
'download' => array(
'summary' => 'Download Package',
'function' => 'doDownload',
2002-05-21 09:38:50 +08:00
'shortcut' => 'd',
'options' => array(
'nocompress' => array(
'shortopt' => 'Z',
'doc' => 'download an uncompressed (.tar) file',
),
),
'doc' => '{package|package-version}
Download a package tarball. The file will be named as suggested by the
server, for example if you download the DB package and the latest stable
version of DB is 1.2, the downloaded file will be DB-1.2.tgz.',
2002-05-20 19:37:33 +08:00
),
);
// }}}
// {{{ constructor
/**
* PEAR_Command_Remote constructor.
*
* @access public
*/
function PEAR_Command_Remote(&$ui, &$config)
{
parent::PEAR_Command_Common($ui, $config);
}
// }}}
2002-06-02 21:07:19 +08:00
// {{{ remote-info
2002-06-02 21:07:19 +08:00
function doRemoteInfo($command, $options, $params)
{
2002-06-07 23:06:25 +08:00
if (sizeof($params) != 1) {
return $this->raiseError("$command expects one param: the remote package name");
2002-06-02 21:07:19 +08:00
}
$r = new PEAR_Remote($this->config);
$available = $r->call('package.listAll', true);
if (PEAR::isError($available)) {
return $this->raiseError($available);
}
$info = $available[$params[0]];
$info["name"] = $params[0];
$reg = new PEAR_Registry($this->config->get('php_dir'));
$installed = $reg->packageInfo($info['name']);
2002-06-07 23:06:25 +08:00
$info['installed'] = $installed['version'] ? $installed['version'] : '- no -';
2002-06-02 21:07:19 +08:00
$this->ui->outputData($info, $command);
2002-06-09 02:44:34 +08:00
return true;
}
// }}}
// {{{ list-remote
2002-05-21 10:28:08 +08:00
function doRemoteList($command, $options, $params)
{
2002-05-20 19:37:33 +08:00
$r = new PEAR_Remote($this->config);
$list_options = false;
if ($this->config->get('preferred_state') == 'stable')
$list_options = true;
$available = $r->call('package.listAll', $list_options);
2002-05-20 19:37:33 +08:00
if (PEAR::isError($available)) {
return $this->raiseError($available);
}
$i = $j = 0;
2002-06-02 21:07:19 +08:00
$data = array(
'caption' => 'Available packages:',
'border' => true,
'headline' => array('Package', 'Version'),
);
2002-05-20 19:37:33 +08:00
foreach ($available as $name => $info) {
2002-06-02 21:07:19 +08:00
$data['data'][] = array($name, $info['stable']);
}
if (count($available)==0) {
$data = '(no packages installed yet)';
}
2002-06-02 21:07:19 +08:00
$this->ui->outputData($data, $command);
return true;
}
// }}}
// {{{ list-all
function doListAll($command, $options, $params)
{
$r = new PEAR_Remote($this->config);
$reg = new PEAR_Registry($this->config->get('php_dir'));
$list_options = false;
if ($this->config->get('preferred_state') == 'stable')
$list_options = true;
$available = $r->call('package.listAll', $list_options);
2002-06-02 21:07:19 +08:00
if (PEAR::isError($available)) {
return $this->raiseError($available);
}
$data = array(
'caption' => 'All packages:',
'border' => true,
'headline' => array('Package', 'Latest', 'Local'),
);
2002-06-07 23:06:25 +08:00
2002-06-02 21:07:19 +08:00
foreach ($available as $name => $info) {
$installed = $reg->packageInfo($name);
$desc = $info['summary'];
if (isset($params[$name]))
$desc .= "\n\n".$info['description'];
2002-06-07 23:06:25 +08:00
2002-06-09 02:44:34 +08:00
if (isset($options['mode']))
{
if ($options['mode'] == 'installed' && !isset($installed['version']))
continue;
if ($options['mode'] == 'notinstalled' && isset($installed['version']))
continue;
if ($options['mode'] == 'upgrades'
2002-06-09 02:44:34 +08:00
&& (!isset($installed['version']) || $installed['version'] == $info['stable']))
{
continue;
};
};
2002-06-02 21:07:19 +08:00
$data['data'][$info['category']][] = array(
2002-06-07 23:06:25 +08:00
$name,
$info['stable'],
2002-06-02 21:07:19 +08:00
$installed['version'],
$desc,
);
}
$this->ui->outputData($data, $command);
return true;
}
// }}}
// {{{ search
function doSearch($command, $options, $params)
{
if ((!isset($params[0]) || empty($params[0]))
&& (!isset($params[1]) || empty($params[1])))
{
2002-06-07 23:06:25 +08:00
return $this->raiseError('no valid search string supplied');
2002-06-02 21:07:19 +08:00
};
2002-06-07 23:06:25 +08:00
2002-06-02 21:07:19 +08:00
$r = new PEAR_Remote($this->config);
$reg = new PEAR_Registry($this->config->get('php_dir'));
$available = $r->call('package.listAll', true);
if (PEAR::isError($available)) {
return $this->raiseError($available);
}
$data = array(
'caption' => 'Matched packages:',
'border' => true,
'headline' => array('Package', 'Latest', 'Local'),
);
2002-06-07 23:06:25 +08:00
2002-06-02 21:07:19 +08:00
foreach ($available as $name => $info) {
$found = (!empty($params[0]) && stristr($name, $params[0]) !== false);
if (!$found && !(isset($params[1]) && !empty($params[1])
&& (stristr($info['summary'], $params[1]) !== false
|| stristr($info['description'], $params[1]) !== false)))
2002-06-07 23:06:25 +08:00
{
2002-06-02 21:07:19 +08:00
continue;
};
2002-06-07 23:06:25 +08:00
2002-06-02 21:07:19 +08:00
$installed = $reg->packageInfo($name);
$desc = $info['summary'];
if (isset($params[$name]))
$desc .= "\n\n".$info['description'];
2002-06-07 23:06:25 +08:00
2002-06-02 21:07:19 +08:00
$data['data'][$info['category']][] = array(
2002-06-07 23:06:25 +08:00
$name,
$info['stable'],
2002-06-02 21:07:19 +08:00
$installed['version'],
$desc,
);
}
2002-06-02 21:07:19 +08:00
if (!isset($data['data'])) {
return $this->raiseError('no packages found');
};
$this->ui->outputData($data, $command);
return true;
}
// }}}
2002-05-20 19:37:33 +08:00
// {{{ download
function doDownload($command, $options, $params)
{
//$params[0] -> The package to download
if (count($params) != 1) {
return PEAR::raiseError("download expects one argument: the package to download");
}
$server = $this->config->get('master_server');
if (!ereg('^http://', $params[0])) {
$pkgfile = "http://$server/get/$params[0]";
} else {
$pkgfile = $params[0];
}
$this->bytes_downloaded = 0;
$saved = PEAR_Common::downloadHttp($pkgfile, $this->ui, '.',
array(&$this, 'downloadCallback'));
if (PEAR::isError($saved)) {
return $this->raiseError($saved);
}
$fname = basename($saved);
2002-06-02 21:07:19 +08:00
$this->ui->outputData("File $fname downloaded ($this->bytes_downloaded bytes)", $command);
2002-05-20 19:37:33 +08:00
return true;
}
2002-04-23 15:01:45 +08:00
function downloadCallback($msg, $params = null)
{
if ($msg == 'done') {
$this->bytes_downloaded = $params;
}
}
2002-05-20 19:37:33 +08:00
// }}}
// {{{ list-upgrades
function doListUpgrades($command, $options, $params)
{
include_once "PEAR/Registry.php";
$remote = new PEAR_Remote($this->config);
if (empty($params[0])) {
$state = $this->config->get('preferred_state');
} else {
$state = $params[0];
}
$caption = 'Available Upgrades';
if (empty($state) || $state == 'any') {
$latest = $remote->call("package.listLatestReleases");
} else {
$latest = $remote->call("package.listLatestReleases", $state);
$caption .= ' (' . $state . ')';
}
$caption .= ':';
if (PEAR::isError($latest)) {
return $latest;
}
$reg = new PEAR_Registry($this->config->get('php_dir'));
$inst = array_flip($reg->listPackages());
2002-06-02 21:07:19 +08:00
$data = array(
'caption' => $caption,
'border' => 1,
'headline' => array('Package', 'Version', 'Size'),
);
2002-05-20 19:37:33 +08:00
foreach ($latest as $package => $info) {
if (!isset($inst[$package])) {
// skip packages we don't have installed
continue;
}
extract($info);
$inst_version = $reg->packageInfo($package, 'version');
if (version_compare("$version", "$inst_version", "le")) {
2002-05-20 19:37:33 +08:00
// installed version is up-to-date
continue;
}
if ($filesize >= 20480) {
$filesize += 1024 - ($filesize % 1024);
$fs = sprintf("%dkB", $filesize / 1024);
} elseif ($filesize > 0) {
$filesize += 103 - ($filesize % 103);
$fs = sprintf("%.1fkB", $filesize / 1024.0);
} else {
$fs = " -"; // XXX center instead
}
2002-06-02 21:07:19 +08:00
$data['data'][] = array($package, $version, $fs);
2002-05-20 19:37:33 +08:00
}
if (empty($data['data'])) {
2002-06-21 21:11:04 +08:00
$this->ui->outputData('No upgrades available');
} else {
$this->ui->outputData($data, $command);
}
2002-05-20 19:37:33 +08:00
return true;
}
// }}}
}
?>