Remote.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. <?php
  2. // /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 5 |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2004 The PHP Group |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 3.0 of the PHP license, |
  9. // | that is bundled with this package in the file LICENSE, and is |
  10. // | available through the world-wide-web at the following url: |
  11. // | http://www.php.net/license/3_0.txt. |
  12. // | If you did not receive a copy of the PHP license and are unable to |
  13. // | obtain it through the world-wide-web, please send a note to |
  14. // | [email protected] so we can mail you a copy immediately. |
  15. // +----------------------------------------------------------------------+
  16. // | Author: Stig Bakken <[email protected]> |
  17. // | |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // $Id: Remote.php,v 1.39 2004/04/03 15:56:00 cellog Exp $
  21. require_once 'PEAR/Command/Common.php';
  22. require_once 'PEAR/Common.php';
  23. require_once 'PEAR/Remote.php';
  24. require_once 'PEAR/Registry.php';
  25. class PEAR_Command_Remote extends PEAR_Command_Common
  26. {
  27. // {{{ command definitions
  28. var $commands = array(
  29. 'remote-info' => array(
  30. 'summary' => 'Information About Remote Packages',
  31. 'function' => 'doRemoteInfo',
  32. 'shortcut' => 'ri',
  33. 'options' => array(),
  34. 'doc' => '<package>
  35. Get details on a package from the server.',
  36. ),
  37. 'list-upgrades' => array(
  38. 'summary' => 'List Available Upgrades',
  39. 'function' => 'doListUpgrades',
  40. 'shortcut' => 'lu',
  41. 'options' => array(),
  42. 'doc' => '
  43. List releases on the server of packages you have installed where
  44. a newer version is available with the same release state (stable etc.).'
  45. ),
  46. 'remote-list' => array(
  47. 'summary' => 'List Remote Packages',
  48. 'function' => 'doRemoteList',
  49. 'shortcut' => 'rl',
  50. 'options' => array(),
  51. 'doc' => '
  52. Lists the packages available on the configured server along with the
  53. latest stable release of each package.',
  54. ),
  55. 'search' => array(
  56. 'summary' => 'Search remote package database',
  57. 'function' => 'doSearch',
  58. 'shortcut' => 'sp',
  59. 'options' => array(),
  60. 'doc' => '
  61. Lists all packages which match the search parameters (first param
  62. is package name, second package info)',
  63. ),
  64. 'list-all' => array(
  65. 'summary' => 'List All Packages',
  66. 'function' => 'doListAll',
  67. 'shortcut' => 'la',
  68. 'options' => array(),
  69. 'doc' => '
  70. Lists the packages available on the configured server along with the
  71. latest stable release of each package.',
  72. ),
  73. 'download' => array(
  74. 'summary' => 'Download Package',
  75. 'function' => 'doDownload',
  76. 'shortcut' => 'd',
  77. 'options' => array(
  78. 'nocompress' => array(
  79. 'shortopt' => 'Z',
  80. 'doc' => 'download an uncompressed (.tar) file',
  81. ),
  82. ),
  83. 'doc' => '{package|package-version}
  84. Download a package tarball. The file will be named as suggested by the
  85. server, for example if you download the DB package and the latest stable
  86. version of DB is 1.2, the downloaded file will be DB-1.2.tgz.',
  87. ),
  88. 'clear-cache' => array(
  89. 'summary' => 'Clear XML-RPC Cache',
  90. 'function' => 'doClearCache',
  91. 'shortcut' => 'cc',
  92. 'options' => array(),
  93. 'doc' => '
  94. Clear the XML-RPC cache. See also the cache_ttl configuration
  95. parameter.
  96. ',
  97. ),
  98. );
  99. // }}}
  100. // {{{ constructor
  101. /**
  102. * PEAR_Command_Remote constructor.
  103. *
  104. * @access public
  105. */
  106. function PEAR_Command_Remote(&$ui, &$config)
  107. {
  108. parent::PEAR_Command_Common($ui, $config);
  109. }
  110. // }}}
  111. // {{{ doRemoteInfo()
  112. function doRemoteInfo($command, $options, $params)
  113. {
  114. if (sizeof($params) != 1) {
  115. return $this->raiseError("$command expects one param: the remote package name");
  116. }
  117. $r = new PEAR_Remote($this->config);
  118. $info = $r->call('package.info', $params[0]);
  119. if (PEAR::isError($info)) {
  120. return $this->raiseError($info);
  121. }
  122. $reg = new PEAR_Registry($this->config->get('php_dir'));
  123. $installed = $reg->packageInfo($info['name']);
  124. $info['installed'] = $installed['version'] ? $installed['version'] : '- no -';
  125. $this->ui->outputData($info, $command);
  126. return true;
  127. }
  128. // }}}
  129. // {{{ doRemoteList()
  130. function doRemoteList($command, $options, $params)
  131. {
  132. $r = new PEAR_Remote($this->config);
  133. $list_options = false;
  134. if ($this->config->get('preferred_state') == 'stable')
  135. $list_options = true;
  136. $available = $r->call('package.listAll', $list_options);
  137. if (PEAR::isError($available)) {
  138. return $this->raiseError($available);
  139. }
  140. $i = $j = 0;
  141. $data = array(
  142. 'caption' => 'Available packages:',
  143. 'border' => true,
  144. 'headline' => array('Package', 'Version'),
  145. );
  146. foreach ($available as $name => $info) {
  147. $data['data'][] = array($name, isset($info['stable']) ? $info['stable'] : '-n/a-');
  148. }
  149. if (count($available)==0) {
  150. $data = '(no packages installed yet)';
  151. }
  152. $this->ui->outputData($data, $command);
  153. return true;
  154. }
  155. // }}}
  156. // {{{ doListAll()
  157. function doListAll($command, $options, $params)
  158. {
  159. $r = new PEAR_Remote($this->config);
  160. $reg = new PEAR_Registry($this->config->get('php_dir'));
  161. $list_options = false;
  162. if ($this->config->get('preferred_state') == 'stable')
  163. $list_options = true;
  164. $available = $r->call('package.listAll', $list_options);
  165. if (PEAR::isError($available)) {
  166. return $this->raiseError($available);
  167. }
  168. if (!is_array($available)) {
  169. return $this->raiseError('The package list could not be fetched from the remote server. Please try again. (Debug info: "'.$available.'")');
  170. }
  171. $data = array(
  172. 'caption' => 'All packages:',
  173. 'border' => true,
  174. 'headline' => array('Package', 'Latest', 'Local'),
  175. );
  176. $local_pkgs = $reg->listPackages();
  177. foreach ($available as $name => $info) {
  178. $installed = $reg->packageInfo($name);
  179. $desc = $info['summary'];
  180. if (isset($params[$name]))
  181. $desc .= "\n\n".$info['description'];
  182. if (isset($options['mode']))
  183. {
  184. if ($options['mode'] == 'installed' && !isset($installed['version']))
  185. continue;
  186. if ($options['mode'] == 'notinstalled' && isset($installed['version']))
  187. continue;
  188. if ($options['mode'] == 'upgrades'
  189. && (!isset($installed['version']) || $installed['version'] == $info['stable']))
  190. {
  191. continue;
  192. }
  193. }
  194. $pos = array_search(strtolower($name), $local_pkgs);
  195. if ($pos !== false) {
  196. unset($local_pkgs[$pos]);
  197. }
  198. $data['data'][$info['category']][] = array(
  199. $name,
  200. @$info['stable'],
  201. @$installed['version'],
  202. @$desc,
  203. @$info['deps'],
  204. );
  205. }
  206. foreach ($local_pkgs as $name) {
  207. $info = $reg->packageInfo($name);
  208. $data['data']['Local'][] = array(
  209. $info['package'],
  210. '',
  211. $info['version'],
  212. $info['summary'],
  213. @$info['release_deps']
  214. );
  215. }
  216. $this->ui->outputData($data, $command);
  217. return true;
  218. }
  219. // }}}
  220. // {{{ doSearch()
  221. function doSearch($command, $options, $params)
  222. {
  223. if ((!isset($params[0]) || empty($params[0]))
  224. && (!isset($params[1]) || empty($params[1])))
  225. {
  226. return $this->raiseError('no valid search string supplied');
  227. };
  228. $r = new PEAR_Remote($this->config);
  229. $reg = new PEAR_Registry($this->config->get('php_dir'));
  230. $available = $r->call('package.listAll', true, false);
  231. if (PEAR::isError($available)) {
  232. return $this->raiseError($available);
  233. }
  234. $data = array(
  235. 'caption' => 'Matched packages:',
  236. 'border' => true,
  237. 'headline' => array('Package', 'Stable/(Latest)', 'Local'),
  238. );
  239. foreach ($available as $name => $info) {
  240. $found = (!empty($params[0]) && stristr($name, $params[0]) !== false);
  241. if (!$found && !(isset($params[1]) && !empty($params[1])
  242. && (stristr($info['summary'], $params[1]) !== false
  243. || stristr($info['description'], $params[1]) !== false)))
  244. {
  245. continue;
  246. };
  247. $installed = $reg->packageInfo($name);
  248. $desc = $info['summary'];
  249. if (isset($params[$name]))
  250. $desc .= "\n\n".$info['description'];
  251. $unstable = '';
  252. if ($info['unstable']) {
  253. $unstable = '/(' . $info['unstable'] . $info['state'] . ')';
  254. }
  255. if (!isset($info['stable']) || !$info['stable']) {
  256. $info['stable'] = 'none';
  257. }
  258. $data['data'][$info['category']][] = array(
  259. $name,
  260. $info['stable'] . $unstable,
  261. $installed['version'],
  262. $desc,
  263. );
  264. }
  265. if (!isset($data['data'])) {
  266. return $this->raiseError('no packages found');
  267. }
  268. $this->ui->outputData($data, $command);
  269. return true;
  270. }
  271. // }}}
  272. // {{{ doDownload()
  273. function doDownload($command, $options, $params)
  274. {
  275. //$params[0] -> The package to download
  276. if (count($params) != 1) {
  277. return PEAR::raiseError("download expects one argument: the package to download");
  278. }
  279. $server = $this->config->get('master_server');
  280. if (!ereg('^http://', $params[0])) {
  281. $getoption = isset($options['nocompress'])&&$options['nocompress']==1?'?uncompress=on':'';
  282. $pkgfile = "http://$server/get/$params[0]".$getoption;
  283. } else {
  284. $pkgfile = $params[0];
  285. }
  286. $this->bytes_downloaded = 0;
  287. $saved = PEAR_Common::downloadHttp($pkgfile, $this->ui, '.',
  288. array(&$this, 'downloadCallback'));
  289. if (PEAR::isError($saved)) {
  290. return $this->raiseError($saved);
  291. }
  292. $fname = basename($saved);
  293. $this->ui->outputData("File $fname downloaded ($this->bytes_downloaded bytes)", $command);
  294. return true;
  295. }
  296. function downloadCallback($msg, $params = null)
  297. {
  298. if ($msg == 'done') {
  299. $this->bytes_downloaded = $params;
  300. }
  301. }
  302. // }}}
  303. // {{{ doListUpgrades()
  304. function doListUpgrades($command, $options, $params)
  305. {
  306. include_once "PEAR/Registry.php";
  307. $remote = new PEAR_Remote($this->config);
  308. if (empty($params[0])) {
  309. $state = $this->config->get('preferred_state');
  310. } else {
  311. $state = $params[0];
  312. }
  313. $caption = 'Available Upgrades';
  314. if (empty($state) || $state == 'any') {
  315. $latest = $remote->call("package.listLatestReleases");
  316. } else {
  317. $latest = $remote->call("package.listLatestReleases", $state);
  318. $caption .= ' (' . implode(', ', PEAR_Common::betterStates($state, true)) . ')';
  319. }
  320. $caption .= ':';
  321. if (PEAR::isError($latest)) {
  322. return $latest;
  323. }
  324. $reg = new PEAR_Registry($this->config->get('php_dir'));
  325. $inst = array_flip($reg->listPackages());
  326. $data = array(
  327. 'caption' => $caption,
  328. 'border' => 1,
  329. 'headline' => array('Package', 'Local', 'Remote', 'Size'),
  330. );
  331. foreach ((array)$latest as $pkg => $info) {
  332. $package = strtolower($pkg);
  333. if (!isset($inst[$package])) {
  334. // skip packages we don't have installed
  335. continue;
  336. }
  337. extract($info);
  338. $pkginfo = $reg->packageInfo($package);
  339. $inst_version = $pkginfo['version'];
  340. $inst_state = $pkginfo['release_state'];
  341. if (version_compare("$version", "$inst_version", "le")) {
  342. // installed version is up-to-date
  343. continue;
  344. }
  345. if ($filesize >= 20480) {
  346. $filesize += 1024 - ($filesize % 1024);
  347. $fs = sprintf("%dkB", $filesize / 1024);
  348. } elseif ($filesize > 0) {
  349. $filesize += 103 - ($filesize % 103);
  350. $fs = sprintf("%.1fkB", $filesize / 1024.0);
  351. } else {
  352. $fs = " -"; // XXX center instead
  353. }
  354. $data['data'][] = array($pkg, "$inst_version ($inst_state)", "$version ($state)", $fs);
  355. }
  356. if (empty($data['data'])) {
  357. $this->ui->outputData('No upgrades available');
  358. } else {
  359. $this->ui->outputData($data, $command);
  360. }
  361. return true;
  362. }
  363. // }}}
  364. // {{{ doClearCache()
  365. function doClearCache($command, $options, $params)
  366. {
  367. $cache_dir = $this->config->get('cache_dir');
  368. $verbose = $this->config->get('verbose');
  369. $output = '';
  370. if (!($dp = @opendir($cache_dir))) {
  371. return $this->raiseError("opendir($cache_dir) failed: $php_errormsg");
  372. }
  373. if ($verbose >= 1) {
  374. $output .= "reading directory $cache_dir\n";
  375. }
  376. $num = 0;
  377. while ($ent = readdir($dp)) {
  378. if (preg_match('/^xmlrpc_cache_[a-z0-9]{32}$/', $ent)) {
  379. $path = $cache_dir . DIRECTORY_SEPARATOR . $ent;
  380. $ok = @unlink($path);
  381. if ($ok) {
  382. if ($verbose >= 2) {
  383. $output .= "deleted $path\n";
  384. }
  385. $num++;
  386. } elseif ($verbose >= 1) {
  387. $output .= "failed to delete $path\n";
  388. }
  389. }
  390. }
  391. closedir($dp);
  392. if ($verbose >= 1) {
  393. $output .= "$num cache entries cleared\n";
  394. }
  395. $this->ui->outputData(rtrim($output), $command);
  396. return $num;
  397. }
  398. // }}}
  399. }
  400. ?>