home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / tmp / PEAR-1.7.1 / PEAR / REST / 11.php < prev    next >
Encoding:
PHP Script  |  2008-02-15  |  11.1 KB  |  317 lines

  1. <?php
  2. /**
  3.  * PEAR_REST_11 - implement faster list-all/remote-list command
  4.  *
  5.  * PHP versions 4 and 5
  6.  *
  7.  * LICENSE: This source file is subject to version 3.0 of the PHP license
  8.  * that is available through the world-wide-web at the following URI:
  9.  * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
  10.  * the PHP License and are unable to obtain it through the web, please
  11.  * send a note to license@php.net so we can mail you a copy immediately.
  12.  *
  13.  * @category   pear
  14.  * @package    PEAR
  15.  * @author     Greg Beaver <cellog@php.net>
  16.  * @copyright  1997-2008 The PHP Group
  17.  * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
  18.  * @version    CVS: $Id: 11.php,v 1.13 2008/01/03 20:26:37 cellog Exp $
  19.  * @link       http://pear.php.net/package/PEAR
  20.  * @since      File available since Release 1.4.3
  21.  */
  22.  
  23. /**
  24.  * For downloading REST xml/txt files
  25.  */
  26. require_once 'PEAR/REST.php';
  27.  
  28. /**
  29.  * Implement REST 1.1
  30.  *
  31.  * @category   pear
  32.  * @package    PEAR
  33.  * @author     Greg Beaver <cellog@php.net>
  34.  * @copyright  1997-2008 The PHP Group
  35.  * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
  36.  * @version    Release: 1.7.1
  37.  * @link       http://pear.php.net/package/PEAR
  38.  * @since      Class available since Release 1.4.3
  39.  */
  40. class PEAR_REST_11
  41. {
  42.     /**
  43.      * @var PEAR_REST
  44.      */
  45.     var $_rest;
  46.  
  47.     function PEAR_REST_11($config, $options = array())
  48.     {
  49.         $this->_rest = &new PEAR_REST($config, $options);
  50.     }
  51.  
  52.     function listAll($base, $dostable, $basic = true)
  53.     {
  54.         $categorylist = $this->_rest->retrieveData($base . 'c/categories.xml');
  55.         if (PEAR::isError($categorylist)) {
  56.             return $categorylist;
  57.         }
  58.         $ret = array();
  59.         if (!is_array($categorylist['c']) || !isset($categorylist['c'][0])) {
  60.             $categorylist['c'] = array($categorylist['c']);
  61.         }
  62.         PEAR::pushErrorHandling(PEAR_ERROR_RETURN);
  63.  
  64.         foreach ($categorylist['c'] as $progress => $category) {
  65.             $category = $category['_content'];
  66.             $packagesinfo = $this->_rest->retrieveData($base .
  67.                 'c/' . urlencode($category) . '/packagesinfo.xml');
  68.  
  69.             if (PEAR::isError($packagesinfo)) {
  70.                 continue;
  71.             }
  72.  
  73.             if (!is_array($packagesinfo) || !isset($packagesinfo['pi'])) {
  74.                 continue;
  75.             }
  76.  
  77.             if (!is_array($packagesinfo['pi']) || !isset($packagesinfo['pi'][0])) {
  78.                 $packagesinfo['pi'] = array($packagesinfo['pi']);
  79.             }
  80.  
  81.             foreach ($packagesinfo['pi'] as $packageinfo) {
  82.                 $info = $packageinfo['p'];
  83.                 $package = $info['n'];
  84.                 $releases = isset($packageinfo['a']) ? $packageinfo['a'] : false;
  85.                 unset($latest);
  86.                 unset($unstable);
  87.                 unset($stable);
  88.                 unset($state);
  89.  
  90.                 if ($releases) {
  91.                     if (!isset($releases['r'][0])) {
  92.                         $releases['r'] = array($releases['r']);
  93.                     }
  94.                     foreach ($releases['r'] as $release) {
  95.                         if (!isset($latest)) {
  96.                             if ($dostable && $release['s'] == 'stable') {
  97.                                 $latest = $release['v'];
  98.                                 $state = 'stable';
  99.                             }
  100.                             if (!$dostable) {
  101.                                 $latest = $release['v'];
  102.                                 $state = $release['s'];
  103.                             }
  104.                         }
  105.                         if (!isset($stable) && $release['s'] == 'stable') {
  106.                             $stable = $release['v'];
  107.                             if (!isset($unstable)) {
  108.                                 $unstable = $stable;
  109.                             }
  110.                         }
  111.                         if (!isset($unstable) && $release['s'] != 'stable') {
  112.                             $unstable = $release['v'];
  113.                             $state = $release['s'];
  114.                         }
  115.                         if (isset($latest) && !isset($state)) {
  116.                             $state = $release['s'];
  117.                         }
  118.                         if (isset($latest) && isset($stable) && isset($unstable)) {
  119.                             break;
  120.                         }
  121.                     }
  122.                 }
  123.  
  124.                 if ($basic) { // remote-list command
  125.                     if (!isset($latest)) {
  126.                         $latest = false;
  127.                     }
  128.                     if ($dostable) {
  129.                         // $state is not set if there are no releases
  130.                         if (isset($state) && $state == 'stable') {
  131.                             $ret[$package] = array('stable' => $latest);
  132.                         } else {
  133.                             $ret[$package] = array('stable' => '-n/a-');
  134.                         }
  135.                     } else {
  136.                         $ret[$package] = array('stable' => $latest);
  137.                     }
  138.                     continue;
  139.                 }
  140.  
  141.                 // list-all command
  142.                 $deps = array();
  143.                 if (!isset($unstable)) {
  144.                     $unstable = false;
  145.                     $state = 'stable';
  146.                     if (isset($stable)) {
  147.                         $latest = $unstable = $stable;
  148.                     }
  149.                 } else {
  150.                     $latest = $unstable;
  151.                 }
  152.  
  153.                 if (!isset($latest)) {
  154.                     $latest = false;
  155.                 }
  156.  
  157.                 if ($latest && isset($packageinfo['deps'])) {
  158.                     if (!is_array($packageinfo['deps']) ||
  159.                           !isset($packageinfo['deps'][0])) {
  160.                         $packageinfo['deps'] = array($packageinfo['deps']);
  161.                     }
  162.                     $d = false;
  163.                     foreach ($packageinfo['deps'] as $dep) {
  164.                         if ($dep['v'] == $latest) {
  165.                             $d = unserialize($dep['d']);
  166.                         }
  167.                     }
  168.                     if ($d) {
  169.                         if (isset($d['required'])) {
  170.                             if (!class_exists('PEAR_PackageFile_v2')) {
  171.                                 require_once 'PEAR/PackageFile/v2.php';
  172.                             }
  173.                             if (!isset($pf)) {
  174.                                 $pf = new PEAR_PackageFile_v2;
  175.                             }
  176.                             $pf->setDeps($d);
  177.                             $tdeps = $pf->getDeps();
  178.                         } else {
  179.                             $tdeps = $d;
  180.                         }
  181.                         foreach ($tdeps as $dep) {
  182.                             if ($dep['type'] !== 'pkg') {
  183.                                 continue;
  184.                             }
  185.                             $deps[] = $dep;
  186.                         }
  187.                     }
  188.                 }
  189.  
  190.                 $info = array('stable' => $latest, 'summary' => $info['s'],
  191.                     'description' =>
  192.                     $info['d'], 'deps' => $deps, 'category' => $info['ca']['_content'],
  193.                     'unstable' => $unstable, 'state' => $state);
  194.                 $ret[$package] = $info;
  195.             }
  196.         }
  197.         PEAR::popErrorHandling();
  198.         return $ret;
  199.     }
  200.  
  201.     /**
  202.      * List all categories of a REST server
  203.      *
  204.      * @param string $base base URL of the server
  205.      * @return array of categorynames
  206.      */
  207.     function listCategories($base)
  208.     {
  209.         $categorylist = $this->_rest->retrieveData($base . 'c/categories.xml');
  210.         if (PEAR::isError($categorylist)) {
  211.             return $categorylist;
  212.         }
  213.         if (!is_array($categorylist) || !isset($categorylist['c'])) {
  214.             return array();
  215.         }
  216.         if (isset($categorylist['c']['_content'])) {
  217.             // only 1 category
  218.             $categorylist['c'] = array($categorylist['c']);
  219.         }
  220.         return $categorylist['c'];
  221.     }
  222.  
  223.     /**
  224.      * List packages in a category of a REST server
  225.      *
  226.      * @param string $base base URL of the server
  227.      * @param string $category name of the category
  228.      * @param boolean $info also download full package info
  229.      * @return array of packagenames
  230.      */
  231.     function listCategory($base, $category, $info=false)
  232.     {
  233.         if ($info == false) {
  234.             $url = '%s'.'c/%s/packages.xml';
  235.         } else {
  236.             $url = '%s'.'c/%s/packagesinfo.xml';
  237.         }
  238.         $url = sprintf($url,
  239.                     $base,
  240.                     urlencode($category));
  241.             
  242.         // gives '404 Not Found' error when category doesn't exist
  243.         $packagelist = $this->_rest->retrieveData($url);
  244.         if (PEAR::isError($packagelist)) {
  245.             return $packagelist;
  246.         }
  247.         if (!is_array($packagelist)) {
  248.             return array();
  249.         }
  250.  
  251.         if ($info == false) {
  252.             if (!isset($packagelist['p'])) {
  253.                 return array();
  254.             }
  255.             if (!is_array($packagelist['p']) ||
  256.                 !isset($packagelist['p'][0])) { // only 1 pkg
  257.                 $packagelist = array($packagelist['p']);
  258.             } else {
  259.                 $packagelist = $packagelist['p'];
  260.             }
  261.             return $packagelist;
  262.         } else {
  263.             // info == true
  264.             if (!isset($packagelist['pi'])) {
  265.                 return array();
  266.             }
  267.             if (!is_array($packagelist['pi']) ||
  268.                 !isset($packagelist['pi'][0])) { // only 1 pkg
  269.                 $packagelist_pre = array($packagelist['pi']);
  270.             } else {
  271.                 $packagelist_pre = $packagelist['pi'];
  272.             }
  273.  
  274.             $packagelist = array();
  275.             foreach ($packagelist_pre as $i => $item) {
  276.                 // compatibility with r/<latest.txt>.xml
  277.                 if (isset($item['a']['r'][0])) {
  278.                     // multiple releases
  279.                     $item['p']['v'] = $item['a']['r'][0]['v'];
  280.                     $item['p']['st'] = $item['a']['r'][0]['s'];
  281.                 } elseif (isset($item['a'])) {
  282.                     // first and only release
  283.                     $item['p']['v'] = $item['a']['r']['v'];
  284.                     $item['p']['st'] = $item['a']['r']['s'];
  285.                 }
  286.  
  287.                 $packagelist[$i] = array('attribs' => $item['p']['r'],
  288.                                          '_content' => $item['p']['n'],
  289.                                          'info' => $item['p']);
  290.             }
  291.         }
  292.  
  293.         return $packagelist;
  294.     }
  295.  
  296.     /**
  297.      * Return an array containing all of the states that are more stable than
  298.      * or equal to the passed in state
  299.      *
  300.      * @param string Release state
  301.      * @param boolean Determines whether to include $state in the list
  302.      * @return false|array False if $state is not a valid release state
  303.      */
  304.     function betterStates($state, $include = false)
  305.     {
  306.         static $states = array('snapshot', 'devel', 'alpha', 'beta', 'stable');
  307.         $i = array_search($state, $states);
  308.         if ($i === false) {
  309.             return false;
  310.         }
  311.         if ($include) {
  312.             $i--;
  313.         }
  314.         return array_slice($states, $i + 1);
  315.     }
  316. }
  317. ?>