10.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
  1. <?php
  2. /**
  3. * PEAR_REST_10
  4. *
  5. * PHP versions 4 and 5
  6. *
  7. * @category pear
  8. * @package PEAR
  9. * @author Greg Beaver <cellog@php.net>
  10. * @copyright 1997-2009 The Authors
  11. * @license http://opensource.org/licenses/bsd-license.php New BSD License
  12. * @version CVS: $Id: 10.php 313023 2011-07-06 19:17:11Z dufuz $
  13. * @link http://pear.php.net/package/PEAR
  14. * @since File available since Release 1.4.0a12
  15. */
  16. /**
  17. * For downloading REST xml/txt files
  18. */
  19. require_once 'PEAR/REST.php';
  20. /**
  21. * Implement REST 1.0
  22. *
  23. * @category pear
  24. * @package PEAR
  25. * @author Greg Beaver <cellog@php.net>
  26. * @copyright 1997-2009 The Authors
  27. * @license http://opensource.org/licenses/bsd-license.php New BSD License
  28. * @version Release: 1.9.4
  29. * @link http://pear.php.net/package/PEAR
  30. * @since Class available since Release 1.4.0a12
  31. */
  32. class PEAR_REST_10
  33. {
  34. /**
  35. * @var PEAR_REST
  36. */
  37. var $_rest;
  38. function PEAR_REST_10($config, $options = array())
  39. {
  40. $this->_rest = &new PEAR_REST($config, $options);
  41. }
  42. /**
  43. * Retrieve information about a remote package to be downloaded from a REST server
  44. *
  45. * @param string $base The uri to prepend to all REST calls
  46. * @param array $packageinfo an array of format:
  47. * <pre>
  48. * array(
  49. * 'package' => 'packagename',
  50. * 'channel' => 'channelname',
  51. * ['state' => 'alpha' (or valid state),]
  52. * -or-
  53. * ['version' => '1.whatever']
  54. * </pre>
  55. * @param string $prefstate Current preferred_state config variable value
  56. * @param bool $installed the installed version of this package to compare against
  57. * @return array|false|PEAR_Error see {@link _returnDownloadURL()}
  58. */
  59. function getDownloadURL($base, $packageinfo, $prefstate, $installed, $channel = false)
  60. {
  61. $states = $this->betterStates($prefstate, true);
  62. if (!$states) {
  63. return PEAR::raiseError('"' . $prefstate . '" is not a valid state');
  64. }
  65. $channel = $packageinfo['channel'];
  66. $package = $packageinfo['package'];
  67. $state = isset($packageinfo['state']) ? $packageinfo['state'] : null;
  68. $version = isset($packageinfo['version']) ? $packageinfo['version'] : null;
  69. $restFile = $base . 'r/' . strtolower($package) . '/allreleases.xml';
  70. $info = $this->_rest->retrieveData($restFile, false, false, $channel);
  71. if (PEAR::isError($info)) {
  72. return PEAR::raiseError('No releases available for package "' .
  73. $channel . '/' . $package . '"');
  74. }
  75. if (!isset($info['r'])) {
  76. return false;
  77. }
  78. $release = $found = false;
  79. if (!is_array($info['r']) || !isset($info['r'][0])) {
  80. $info['r'] = array($info['r']);
  81. }
  82. foreach ($info['r'] as $release) {
  83. if (!isset($this->_rest->_options['force']) && ($installed &&
  84. version_compare($release['v'], $installed, '<'))) {
  85. continue;
  86. }
  87. if (isset($state)) {
  88. // try our preferred state first
  89. if ($release['s'] == $state) {
  90. $found = true;
  91. break;
  92. }
  93. // see if there is something newer and more stable
  94. // bug #7221
  95. if (in_array($release['s'], $this->betterStates($state), true)) {
  96. $found = true;
  97. break;
  98. }
  99. } elseif (isset($version)) {
  100. if ($release['v'] == $version) {
  101. $found = true;
  102. break;
  103. }
  104. } else {
  105. if (in_array($release['s'], $states)) {
  106. $found = true;
  107. break;
  108. }
  109. }
  110. }
  111. return $this->_returnDownloadURL($base, $package, $release, $info, $found, false, $channel);
  112. }
  113. function getDepDownloadURL($base, $xsdversion, $dependency, $deppackage,
  114. $prefstate = 'stable', $installed = false, $channel = false)
  115. {
  116. $states = $this->betterStates($prefstate, true);
  117. if (!$states) {
  118. return PEAR::raiseError('"' . $prefstate . '" is not a valid state');
  119. }
  120. $channel = $dependency['channel'];
  121. $package = $dependency['name'];
  122. $state = isset($dependency['state']) ? $dependency['state'] : null;
  123. $version = isset($dependency['version']) ? $dependency['version'] : null;
  124. $restFile = $base . 'r/' . strtolower($package) . '/allreleases.xml';
  125. $info = $this->_rest->retrieveData($restFile, false, false, $channel);
  126. if (PEAR::isError($info)) {
  127. return PEAR::raiseError('Package "' . $deppackage['channel'] . '/' . $deppackage['package']
  128. . '" dependency "' . $channel . '/' . $package . '" has no releases');
  129. }
  130. if (!is_array($info) || !isset($info['r'])) {
  131. return false;
  132. }
  133. $exclude = array();
  134. $min = $max = $recommended = false;
  135. if ($xsdversion == '1.0') {
  136. switch ($dependency['rel']) {
  137. case 'ge' :
  138. $min = $dependency['version'];
  139. break;
  140. case 'gt' :
  141. $min = $dependency['version'];
  142. $exclude = array($dependency['version']);
  143. break;
  144. case 'eq' :
  145. $recommended = $dependency['version'];
  146. break;
  147. case 'lt' :
  148. $max = $dependency['version'];
  149. $exclude = array($dependency['version']);
  150. break;
  151. case 'le' :
  152. $max = $dependency['version'];
  153. break;
  154. case 'ne' :
  155. $exclude = array($dependency['version']);
  156. break;
  157. }
  158. } else {
  159. $min = isset($dependency['min']) ? $dependency['min'] : false;
  160. $max = isset($dependency['max']) ? $dependency['max'] : false;
  161. $recommended = isset($dependency['recommended']) ?
  162. $dependency['recommended'] : false;
  163. if (isset($dependency['exclude'])) {
  164. if (!isset($dependency['exclude'][0])) {
  165. $exclude = array($dependency['exclude']);
  166. }
  167. }
  168. }
  169. $release = $found = false;
  170. if (!is_array($info['r']) || !isset($info['r'][0])) {
  171. $info['r'] = array($info['r']);
  172. }
  173. foreach ($info['r'] as $release) {
  174. if (!isset($this->_rest->_options['force']) && ($installed &&
  175. version_compare($release['v'], $installed, '<'))) {
  176. continue;
  177. }
  178. if (in_array($release['v'], $exclude)) { // skip excluded versions
  179. continue;
  180. }
  181. // allow newer releases to say "I'm OK with the dependent package"
  182. if ($xsdversion == '2.0' && isset($release['co'])) {
  183. if (!is_array($release['co']) || !isset($release['co'][0])) {
  184. $release['co'] = array($release['co']);
  185. }
  186. foreach ($release['co'] as $entry) {
  187. if (isset($entry['x']) && !is_array($entry['x'])) {
  188. $entry['x'] = array($entry['x']);
  189. } elseif (!isset($entry['x'])) {
  190. $entry['x'] = array();
  191. }
  192. if ($entry['c'] == $deppackage['channel'] &&
  193. strtolower($entry['p']) == strtolower($deppackage['package']) &&
  194. version_compare($deppackage['version'], $entry['min'], '>=') &&
  195. version_compare($deppackage['version'], $entry['max'], '<=') &&
  196. !in_array($release['v'], $entry['x'])) {
  197. $recommended = $release['v'];
  198. break;
  199. }
  200. }
  201. }
  202. if ($recommended) {
  203. if ($release['v'] != $recommended) { // if we want a specific
  204. // version, then skip all others
  205. continue;
  206. } else {
  207. if (!in_array($release['s'], $states)) {
  208. // the stability is too low, but we must return the
  209. // recommended version if possible
  210. return $this->_returnDownloadURL($base, $package, $release, $info, true, false, $channel);
  211. }
  212. }
  213. }
  214. if ($min && version_compare($release['v'], $min, 'lt')) { // skip too old versions
  215. continue;
  216. }
  217. if ($max && version_compare($release['v'], $max, 'gt')) { // skip too new versions
  218. continue;
  219. }
  220. if ($installed && version_compare($release['v'], $installed, '<')) {
  221. continue;
  222. }
  223. if (in_array($release['s'], $states)) { // if in the preferred state...
  224. $found = true; // ... then use it
  225. break;
  226. }
  227. }
  228. return $this->_returnDownloadURL($base, $package, $release, $info, $found, false, $channel);
  229. }
  230. /**
  231. * Take raw data and return the array needed for processing a download URL
  232. *
  233. * @param string $base REST base uri
  234. * @param string $package Package name
  235. * @param array $release an array of format array('v' => version, 's' => state)
  236. * describing the release to download
  237. * @param array $info list of all releases as defined by allreleases.xml
  238. * @param bool|null $found determines whether the release was found or this is the next
  239. * best alternative. If null, then versions were skipped because
  240. * of PHP dependency
  241. * @return array|PEAR_Error
  242. * @access private
  243. */
  244. function _returnDownloadURL($base, $package, $release, $info, $found, $phpversion = false, $channel = false)
  245. {
  246. if (!$found) {
  247. $release = $info['r'][0];
  248. }
  249. $packageLower = strtolower($package);
  250. $pinfo = $this->_rest->retrieveCacheFirst($base . 'p/' . $packageLower . '/' .
  251. 'info.xml', false, false, $channel);
  252. if (PEAR::isError($pinfo)) {
  253. return PEAR::raiseError('Package "' . $package .
  254. '" does not have REST info xml available');
  255. }
  256. $releaseinfo = $this->_rest->retrieveCacheFirst($base . 'r/' . $packageLower . '/' .
  257. $release['v'] . '.xml', false, false, $channel);
  258. if (PEAR::isError($releaseinfo)) {
  259. return PEAR::raiseError('Package "' . $package . '" Version "' . $release['v'] .
  260. '" does not have REST xml available');
  261. }
  262. $packagexml = $this->_rest->retrieveCacheFirst($base . 'r/' . $packageLower . '/' .
  263. 'deps.' . $release['v'] . '.txt', false, true, $channel);
  264. if (PEAR::isError($packagexml)) {
  265. return PEAR::raiseError('Package "' . $package . '" Version "' . $release['v'] .
  266. '" does not have REST dependency information available');
  267. }
  268. $packagexml = unserialize($packagexml);
  269. if (!$packagexml) {
  270. $packagexml = array();
  271. }
  272. $allinfo = $this->_rest->retrieveData($base . 'r/' . $packageLower .
  273. '/allreleases.xml', false, false, $channel);
  274. if (PEAR::isError($allinfo)) {
  275. return $allinfo;
  276. }
  277. if (!is_array($allinfo['r']) || !isset($allinfo['r'][0])) {
  278. $allinfo['r'] = array($allinfo['r']);
  279. }
  280. $compatible = false;
  281. foreach ($allinfo['r'] as $release) {
  282. if ($release['v'] != $releaseinfo['v']) {
  283. continue;
  284. }
  285. if (!isset($release['co'])) {
  286. break;
  287. }
  288. $compatible = array();
  289. if (!is_array($release['co']) || !isset($release['co'][0])) {
  290. $release['co'] = array($release['co']);
  291. }
  292. foreach ($release['co'] as $entry) {
  293. $comp = array();
  294. $comp['name'] = $entry['p'];
  295. $comp['channel'] = $entry['c'];
  296. $comp['min'] = $entry['min'];
  297. $comp['max'] = $entry['max'];
  298. if (isset($entry['x']) && !is_array($entry['x'])) {
  299. $comp['exclude'] = $entry['x'];
  300. }
  301. $compatible[] = $comp;
  302. }
  303. if (count($compatible) == 1) {
  304. $compatible = $compatible[0];
  305. }
  306. break;
  307. }
  308. $deprecated = false;
  309. if (isset($pinfo['dc']) && isset($pinfo['dp'])) {
  310. if (is_array($pinfo['dp'])) {
  311. $deprecated = array('channel' => (string) $pinfo['dc'],
  312. 'package' => trim($pinfo['dp']['_content']));
  313. } else {
  314. $deprecated = array('channel' => (string) $pinfo['dc'],
  315. 'package' => trim($pinfo['dp']));
  316. }
  317. }
  318. $return = array(
  319. 'version' => $releaseinfo['v'],
  320. 'info' => $packagexml,
  321. 'package' => $releaseinfo['p']['_content'],
  322. 'stability' => $releaseinfo['st'],
  323. 'compatible' => $compatible,
  324. 'deprecated' => $deprecated,
  325. );
  326. if ($found) {
  327. $return['url'] = $releaseinfo['g'];
  328. return $return;
  329. }
  330. $return['php'] = $phpversion;
  331. return $return;
  332. }
  333. function listPackages($base, $channel = false)
  334. {
  335. $packagelist = $this->_rest->retrieveData($base . 'p/packages.xml', false, false, $channel);
  336. if (PEAR::isError($packagelist)) {
  337. return $packagelist;
  338. }
  339. if (!is_array($packagelist) || !isset($packagelist['p'])) {
  340. return array();
  341. }
  342. if (!is_array($packagelist['p'])) {
  343. $packagelist['p'] = array($packagelist['p']);
  344. }
  345. return $packagelist['p'];
  346. }
  347. /**
  348. * List all categories of a REST server
  349. *
  350. * @param string $base base URL of the server
  351. * @return array of categorynames
  352. */
  353. function listCategories($base, $channel = false)
  354. {
  355. $categories = array();
  356. // c/categories.xml does not exist;
  357. // check for every package its category manually
  358. // This is SLOOOWWWW : ///
  359. $packagelist = $this->_rest->retrieveData($base . 'p/packages.xml', false, false, $channel);
  360. if (PEAR::isError($packagelist)) {
  361. return $packagelist;
  362. }
  363. if (!is_array($packagelist) || !isset($packagelist['p'])) {
  364. $ret = array();
  365. return $ret;
  366. }
  367. if (!is_array($packagelist['p'])) {
  368. $packagelist['p'] = array($packagelist['p']);
  369. }
  370. PEAR::pushErrorHandling(PEAR_ERROR_RETURN);
  371. foreach ($packagelist['p'] as $package) {
  372. $inf = $this->_rest->retrieveData($base . 'p/' . strtolower($package) . '/info.xml', false, false, $channel);
  373. if (PEAR::isError($inf)) {
  374. PEAR::popErrorHandling();
  375. return $inf;
  376. }
  377. $cat = $inf['ca']['_content'];
  378. if (!isset($categories[$cat])) {
  379. $categories[$cat] = $inf['ca'];
  380. }
  381. }
  382. return array_values($categories);
  383. }
  384. /**
  385. * List a category of a REST server
  386. *
  387. * @param string $base base URL of the server
  388. * @param string $category name of the category
  389. * @param boolean $info also download full package info
  390. * @return array of packagenames
  391. */
  392. function listCategory($base, $category, $info = false, $channel = false)
  393. {
  394. // gives '404 Not Found' error when category doesn't exist
  395. $packagelist = $this->_rest->retrieveData($base.'c/'.urlencode($category).'/packages.xml', false, false, $channel);
  396. if (PEAR::isError($packagelist)) {
  397. return $packagelist;
  398. }
  399. if (!is_array($packagelist) || !isset($packagelist['p'])) {
  400. return array();
  401. }
  402. if (!is_array($packagelist['p']) ||
  403. !isset($packagelist['p'][0])) { // only 1 pkg
  404. $packagelist = array($packagelist['p']);
  405. } else {
  406. $packagelist = $packagelist['p'];
  407. }
  408. if ($info == true) {
  409. // get individual package info
  410. PEAR::pushErrorHandling(PEAR_ERROR_RETURN);
  411. foreach ($packagelist as $i => $packageitem) {
  412. $url = sprintf('%s'.'r/%s/latest.txt',
  413. $base,
  414. strtolower($packageitem['_content']));
  415. $version = $this->_rest->retrieveData($url, false, false, $channel);
  416. if (PEAR::isError($version)) {
  417. break; // skipit
  418. }
  419. $url = sprintf('%s'.'r/%s/%s.xml',
  420. $base,
  421. strtolower($packageitem['_content']),
  422. $version);
  423. $info = $this->_rest->retrieveData($url, false, false, $channel);
  424. if (PEAR::isError($info)) {
  425. break; // skipit
  426. }
  427. $packagelist[$i]['info'] = $info;
  428. }
  429. PEAR::popErrorHandling();
  430. }
  431. return $packagelist;
  432. }
  433. function listAll($base, $dostable, $basic = true, $searchpackage = false, $searchsummary = false, $channel = false)
  434. {
  435. $packagelist = $this->_rest->retrieveData($base . 'p/packages.xml', false, false, $channel);
  436. if (PEAR::isError($packagelist)) {
  437. return $packagelist;
  438. }
  439. if ($this->_rest->config->get('verbose') > 0) {
  440. $ui = &PEAR_Frontend::singleton();
  441. $ui->log('Retrieving data...0%', true);
  442. }
  443. $ret = array();
  444. if (!is_array($packagelist) || !isset($packagelist['p'])) {
  445. return $ret;
  446. }
  447. if (!is_array($packagelist['p'])) {
  448. $packagelist['p'] = array($packagelist['p']);
  449. }
  450. // only search-packagename = quicksearch !
  451. if ($searchpackage && (!$searchsummary || empty($searchpackage))) {
  452. $newpackagelist = array();
  453. foreach ($packagelist['p'] as $package) {
  454. if (!empty($searchpackage) && stristr($package, $searchpackage) !== false) {
  455. $newpackagelist[] = $package;
  456. }
  457. }
  458. $packagelist['p'] = $newpackagelist;
  459. }
  460. PEAR::pushErrorHandling(PEAR_ERROR_RETURN);
  461. $next = .1;
  462. foreach ($packagelist['p'] as $progress => $package) {
  463. if ($this->_rest->config->get('verbose') > 0) {
  464. if ($progress / count($packagelist['p']) >= $next) {
  465. if ($next == .5) {
  466. $ui->log('50%', false);
  467. } else {
  468. $ui->log('.', false);
  469. }
  470. $next += .1;
  471. }
  472. }
  473. if ($basic) { // remote-list command
  474. if ($dostable) {
  475. $latest = $this->_rest->retrieveData($base . 'r/' . strtolower($package) .
  476. '/stable.txt', false, false, $channel);
  477. } else {
  478. $latest = $this->_rest->retrieveData($base . 'r/' . strtolower($package) .
  479. '/latest.txt', false, false, $channel);
  480. }
  481. if (PEAR::isError($latest)) {
  482. $latest = false;
  483. }
  484. $info = array('stable' => $latest);
  485. } else { // list-all command
  486. $inf = $this->_rest->retrieveData($base . 'p/' . strtolower($package) . '/info.xml', false, false, $channel);
  487. if (PEAR::isError($inf)) {
  488. PEAR::popErrorHandling();
  489. return $inf;
  490. }
  491. if ($searchpackage) {
  492. $found = (!empty($searchpackage) && stristr($package, $searchpackage) !== false);
  493. if (!$found && !(isset($searchsummary) && !empty($searchsummary)
  494. && (stristr($inf['s'], $searchsummary) !== false
  495. || stristr($inf['d'], $searchsummary) !== false)))
  496. {
  497. continue;
  498. };
  499. }
  500. $releases = $this->_rest->retrieveData($base . 'r/' . strtolower($package) .
  501. '/allreleases.xml', false, false, $channel);
  502. if (PEAR::isError($releases)) {
  503. continue;
  504. }
  505. if (!isset($releases['r'][0])) {
  506. $releases['r'] = array($releases['r']);
  507. }
  508. unset($latest);
  509. unset($unstable);
  510. unset($stable);
  511. unset($state);
  512. foreach ($releases['r'] as $release) {
  513. if (!isset($latest)) {
  514. if ($dostable && $release['s'] == 'stable') {
  515. $latest = $release['v'];
  516. $state = 'stable';
  517. }
  518. if (!$dostable) {
  519. $latest = $release['v'];
  520. $state = $release['s'];
  521. }
  522. }
  523. if (!isset($stable) && $release['s'] == 'stable') {
  524. $stable = $release['v'];
  525. if (!isset($unstable)) {
  526. $unstable = $stable;
  527. }
  528. }
  529. if (!isset($unstable) && $release['s'] != 'stable') {
  530. $latest = $unstable = $release['v'];
  531. $state = $release['s'];
  532. }
  533. if (isset($latest) && !isset($state)) {
  534. $state = $release['s'];
  535. }
  536. if (isset($latest) && isset($stable) && isset($unstable)) {
  537. break;
  538. }
  539. }
  540. $deps = array();
  541. if (!isset($unstable)) {
  542. $unstable = false;
  543. $state = 'stable';
  544. if (isset($stable)) {
  545. $latest = $unstable = $stable;
  546. }
  547. } else {
  548. $latest = $unstable;
  549. }
  550. if (!isset($latest)) {
  551. $latest = false;
  552. }
  553. if ($latest) {
  554. $d = $this->_rest->retrieveCacheFirst($base . 'r/' . strtolower($package) . '/deps.' .
  555. $latest . '.txt', false, false, $channel);
  556. if (!PEAR::isError($d)) {
  557. $d = unserialize($d);
  558. if ($d) {
  559. if (isset($d['required'])) {
  560. if (!class_exists('PEAR_PackageFile_v2')) {
  561. require_once 'PEAR/PackageFile/v2.php';
  562. }
  563. if (!isset($pf)) {
  564. $pf = new PEAR_PackageFile_v2;
  565. }
  566. $pf->setDeps($d);
  567. $tdeps = $pf->getDeps();
  568. } else {
  569. $tdeps = $d;
  570. }
  571. foreach ($tdeps as $dep) {
  572. if ($dep['type'] !== 'pkg') {
  573. continue;
  574. }
  575. $deps[] = $dep;
  576. }
  577. }
  578. }
  579. }
  580. if (!isset($stable)) {
  581. $stable = '-n/a-';
  582. }
  583. if (!$searchpackage) {
  584. $info = array('stable' => $latest, 'summary' => $inf['s'], 'description' =>
  585. $inf['d'], 'deps' => $deps, 'category' => $inf['ca']['_content'],
  586. 'unstable' => $unstable, 'state' => $state);
  587. } else {
  588. $info = array('stable' => $stable, 'summary' => $inf['s'], 'description' =>
  589. $inf['d'], 'deps' => $deps, 'category' => $inf['ca']['_content'],
  590. 'unstable' => $unstable, 'state' => $state);
  591. }
  592. }
  593. $ret[$package] = $info;
  594. }
  595. PEAR::popErrorHandling();
  596. return $ret;
  597. }
  598. function listLatestUpgrades($base, $pref_state, $installed, $channel, &$reg)
  599. {
  600. $packagelist = $this->_rest->retrieveData($base . 'p/packages.xml', false, false, $channel);
  601. if (PEAR::isError($packagelist)) {
  602. return $packagelist;
  603. }
  604. $ret = array();
  605. if (!is_array($packagelist) || !isset($packagelist['p'])) {
  606. return $ret;
  607. }
  608. if (!is_array($packagelist['p'])) {
  609. $packagelist['p'] = array($packagelist['p']);
  610. }
  611. foreach ($packagelist['p'] as $package) {
  612. if (!isset($installed[strtolower($package)])) {
  613. continue;
  614. }
  615. $inst_version = $reg->packageInfo($package, 'version', $channel);
  616. $inst_state = $reg->packageInfo($package, 'release_state', $channel);
  617. PEAR::pushErrorHandling(PEAR_ERROR_RETURN);
  618. $info = $this->_rest->retrieveData($base . 'r/' . strtolower($package) .
  619. '/allreleases.xml', false, false, $channel);
  620. PEAR::popErrorHandling();
  621. if (PEAR::isError($info)) {
  622. continue; // no remote releases
  623. }
  624. if (!isset($info['r'])) {
  625. continue;
  626. }
  627. $release = $found = false;
  628. if (!is_array($info['r']) || !isset($info['r'][0])) {
  629. $info['r'] = array($info['r']);
  630. }
  631. // $info['r'] is sorted by version number
  632. usort($info['r'], array($this, '_sortReleasesByVersionNumber'));
  633. foreach ($info['r'] as $release) {
  634. if ($inst_version && version_compare($release['v'], $inst_version, '<=')) {
  635. // not newer than the one installed
  636. break;
  637. }
  638. // new version > installed version
  639. if (!$pref_state) {
  640. // every state is a good state
  641. $found = true;
  642. break;
  643. } else {
  644. $new_state = $release['s'];
  645. // if new state >= installed state: go
  646. if (in_array($new_state, $this->betterStates($inst_state, true))) {
  647. $found = true;
  648. break;
  649. } else {
  650. // only allow to lower the state of package,
  651. // if new state >= preferred state: go
  652. if (in_array($new_state, $this->betterStates($pref_state, true))) {
  653. $found = true;
  654. break;
  655. }
  656. }
  657. }
  658. }
  659. if (!$found) {
  660. continue;
  661. }
  662. $relinfo = $this->_rest->retrieveCacheFirst($base . 'r/' . strtolower($package) . '/' .
  663. $release['v'] . '.xml', false, false, $channel);
  664. if (PEAR::isError($relinfo)) {
  665. return $relinfo;
  666. }
  667. $ret[$package] = array(
  668. 'version' => $release['v'],
  669. 'state' => $release['s'],
  670. 'filesize' => $relinfo['f'],
  671. );
  672. }
  673. return $ret;
  674. }
  675. function packageInfo($base, $package, $channel = false)
  676. {
  677. PEAR::pushErrorHandling(PEAR_ERROR_RETURN);
  678. $pinfo = $this->_rest->retrieveData($base . 'p/' . strtolower($package) . '/info.xml', false, false, $channel);
  679. if (PEAR::isError($pinfo)) {
  680. PEAR::popErrorHandling();
  681. return PEAR::raiseError('Unknown package: "' . $package . '" in channel "' . $channel . '"' . "\n". 'Debug: ' .
  682. $pinfo->getMessage());
  683. }
  684. $releases = array();
  685. $allreleases = $this->_rest->retrieveData($base . 'r/' . strtolower($package) .
  686. '/allreleases.xml', false, false, $channel);
  687. if (!PEAR::isError($allreleases)) {
  688. if (!class_exists('PEAR_PackageFile_v2')) {
  689. require_once 'PEAR/PackageFile/v2.php';
  690. }
  691. if (!is_array($allreleases['r']) || !isset($allreleases['r'][0])) {
  692. $allreleases['r'] = array($allreleases['r']);
  693. }
  694. $pf = new PEAR_PackageFile_v2;
  695. foreach ($allreleases['r'] as $release) {
  696. $ds = $this->_rest->retrieveCacheFirst($base . 'r/' . strtolower($package) . '/deps.' .
  697. $release['v'] . '.txt', false, false, $channel);
  698. if (PEAR::isError($ds)) {
  699. continue;
  700. }
  701. if (!isset($latest)) {
  702. $latest = $release['v'];
  703. }
  704. $pf->setDeps(unserialize($ds));
  705. $ds = $pf->getDeps();
  706. $info = $this->_rest->retrieveCacheFirst($base . 'r/' . strtolower($package)
  707. . '/' . $release['v'] . '.xml', false, false, $channel);
  708. if (PEAR::isError($info)) {
  709. continue;
  710. }
  711. $releases[$release['v']] = array(
  712. 'doneby' => $info['m'],
  713. 'license' => $info['l'],
  714. 'summary' => $info['s'],
  715. 'description' => $info['d'],
  716. 'releasedate' => $info['da'],
  717. 'releasenotes' => $info['n'],
  718. 'state' => $release['s'],
  719. 'deps' => $ds ? $ds : array(),
  720. );
  721. }
  722. } else {
  723. $latest = '';
  724. }
  725. PEAR::popErrorHandling();
  726. if (isset($pinfo['dc']) && isset($pinfo['dp'])) {
  727. if (is_array($pinfo['dp'])) {
  728. $deprecated = array('channel' => (string) $pinfo['dc'],
  729. 'package' => trim($pinfo['dp']['_content']));
  730. } else {
  731. $deprecated = array('channel' => (string) $pinfo['dc'],
  732. 'package' => trim($pinfo['dp']));
  733. }
  734. } else {
  735. $deprecated = false;
  736. }
  737. if (!isset($latest)) {
  738. $latest = '';
  739. }
  740. return array(
  741. 'name' => $pinfo['n'],
  742. 'channel' => $pinfo['c'],
  743. 'category' => $pinfo['ca']['_content'],
  744. 'stable' => $latest,
  745. 'license' => $pinfo['l'],
  746. 'summary' => $pinfo['s'],
  747. 'description' => $pinfo['d'],
  748. 'releases' => $releases,
  749. 'deprecated' => $deprecated,
  750. );
  751. }
  752. /**
  753. * Return an array containing all of the states that are more stable than
  754. * or equal to the passed in state
  755. *
  756. * @param string Release state
  757. * @param boolean Determines whether to include $state in the list
  758. * @return false|array False if $state is not a valid release state
  759. */
  760. function betterStates($state, $include = false)
  761. {
  762. static $states = array('snapshot', 'devel', 'alpha', 'beta', 'stable');
  763. $i = array_search($state, $states);
  764. if ($i === false) {
  765. return false;
  766. }
  767. if ($include) {
  768. $i--;
  769. }
  770. return array_slice($states, $i + 1);
  771. }
  772. /**
  773. * Sort releases by version number
  774. *
  775. * @access private
  776. */
  777. function _sortReleasesByVersionNumber($a, $b)
  778. {
  779. if (version_compare($a['v'], $b['v'], '=')) {
  780. return 0;
  781. }
  782. if (version_compare($a['v'], $b['v'], '>')) {
  783. return -1;
  784. }
  785. if (version_compare($a['v'], $b['v'], '<')) {
  786. return 1;
  787. }
  788. }
  789. }