PageRenderTime 44ms CodeModel.GetById 1ms app.highlight 36ms RepoModel.GetById 1ms app.codeStats 0ms

/gespac/config/PEAR/PEAR/REST/13.php

http://gespac.googlecode.com/
PHP | 299 lines | 205 code | 32 blank | 62 comment | 63 complexity | 58734baedca9a15f3013a8e5e21db229 MD5 | raw file
  1<?php
  2/**
  3 * PEAR_REST_13
  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: 13.php 287110 2009-08-11 18:51:15Z dufuz $
 13 * @link       http://pear.php.net/package/PEAR
 14 * @since      File available since Release 1.4.0a12
 15 */
 16
 17/**
 18 * For downloading REST xml/txt files
 19 */
 20require_once 'PEAR/REST.php';
 21require_once 'PEAR/REST/10.php';
 22
 23/**
 24 * Implement REST 1.3
 25 *
 26 * @category   pear
 27 * @package    PEAR
 28 * @author     Greg Beaver <cellog@php.net>
 29 * @copyright  1997-2009 The Authors
 30 * @license    http://opensource.org/licenses/bsd-license.php New BSD License
 31 * @version    Release: 1.9.1
 32 * @link       http://pear.php.net/package/PEAR
 33 * @since      Class available since Release 1.4.0a12
 34 */
 35class PEAR_REST_13 extends PEAR_REST_10
 36{
 37    /**
 38     * Retrieve information about a remote package to be downloaded from a REST server
 39     *
 40     * This is smart enough to resolve the minimum PHP version dependency prior to download
 41     * @param string $base The uri to prepend to all REST calls
 42     * @param array $packageinfo an array of format:
 43     * <pre>
 44     *  array(
 45     *   'package' => 'packagename',
 46     *   'channel' => 'channelname',
 47     *  ['state' => 'alpha' (or valid state),]
 48     *  -or-
 49     *  ['version' => '1.whatever']
 50     * </pre>
 51     * @param string $prefstate Current preferred_state config variable value
 52     * @param bool $installed the installed version of this package to compare against
 53     * @return array|false|PEAR_Error see {@link _returnDownloadURL()}
 54     */
 55    function getDownloadURL($base, $packageinfo, $prefstate, $installed, $channel = false)
 56    {
 57        $states = $this->betterStates($prefstate, true);
 58        if (!$states) {
 59            return PEAR::raiseError('"' . $prefstate . '" is not a valid state');
 60        }
 61
 62        $channel  = $packageinfo['channel'];
 63        $package  = $packageinfo['package'];
 64        $state    = isset($packageinfo['state'])   ? $packageinfo['state']   : null;
 65        $version  = isset($packageinfo['version']) ? $packageinfo['version'] : null;
 66        $restFile = $base . 'r/' . strtolower($package) . '/allreleases2.xml';
 67
 68        $info = $this->_rest->retrieveData($restFile, false, false, $channel);
 69        if (PEAR::isError($info)) {
 70            return PEAR::raiseError('No releases available for package "' .
 71                $channel . '/' . $package . '"');
 72        }
 73
 74        if (!isset($info['r'])) {
 75            return false;
 76        }
 77
 78        $release = $found = false;
 79        if (!is_array($info['r']) || !isset($info['r'][0])) {
 80            $info['r'] = array($info['r']);
 81        }
 82
 83        $skippedphp = false;
 84        foreach ($info['r'] as $release) {
 85            if (!isset($this->_rest->_options['force']) && ($installed &&
 86                  version_compare($release['v'], $installed, '<'))) {
 87                continue;
 88            }
 89
 90            if (isset($state)) {
 91                // try our preferred state first
 92                if ($release['s'] == $state) {
 93                    if (!isset($version) && version_compare($release['m'], phpversion(), '>')) {
 94                        // skip releases that require a PHP version newer than our PHP version
 95                        $skippedphp = $release;
 96                        continue;
 97                    }
 98                    $found = true;
 99                    break;
100                }
101
102                // see if there is something newer and more stable
103                // bug #7221
104                if (in_array($release['s'], $this->betterStates($state), true)) {
105                    if (!isset($version) && version_compare($release['m'], phpversion(), '>')) {
106                        // skip releases that require a PHP version newer than our PHP version
107                        $skippedphp = $release;
108                        continue;
109                    }
110                    $found = true;
111                    break;
112                }
113            } elseif (isset($version)) {
114                if ($release['v'] == $version) {
115                    if (!isset($this->_rest->_options['force']) &&
116                          !isset($version) &&
117                          version_compare($release['m'], phpversion(), '>')) {
118                        // skip releases that require a PHP version newer than our PHP version
119                        $skippedphp = $release;
120                        continue;
121                    }
122                    $found = true;
123                    break;
124                }
125            } else {
126                if (in_array($release['s'], $states)) {
127                    if (version_compare($release['m'], phpversion(), '>')) {
128                        // skip releases that require a PHP version newer than our PHP version
129                        $skippedphp = $release;
130                        continue;
131                    }
132                    $found = true;
133                    break;
134                }
135            }
136        }
137
138        if (!$found && $skippedphp) {
139            $found = null;
140        }
141
142        return $this->_returnDownloadURL($base, $package, $release, $info, $found, $skippedphp, $channel);
143    }
144
145    function getDepDownloadURL($base, $xsdversion, $dependency, $deppackage,
146                               $prefstate = 'stable', $installed = false, $channel = false)
147    {
148        $states = $this->betterStates($prefstate, true);
149        if (!$states) {
150            return PEAR::raiseError('"' . $prefstate . '" is not a valid state');
151        }
152
153        $channel  = $dependency['channel'];
154        $package  = $dependency['name'];
155        $state    = isset($dependency['state'])   ? $dependency['state']   : null;
156        $version  = isset($dependency['version']) ? $dependency['version'] : null;
157        $restFile = $base . 'r/' . strtolower($package) .'/allreleases2.xml';
158
159        $info = $this->_rest->retrieveData($restFile, false, false, $channel);
160        if (PEAR::isError($info)) {
161            return PEAR::raiseError('Package "' . $deppackage['channel'] . '/' . $deppackage['package']
162                . '" dependency "' . $channel . '/' . $package . '" has no releases');
163        }
164
165        if (!is_array($info) || !isset($info['r'])) {
166            return false;
167        }
168
169        $exclude = array();
170        $min = $max = $recommended = false;
171        if ($xsdversion == '1.0') {
172            $pinfo['package'] = $dependency['name'];
173            $pinfo['channel'] = 'pear.php.net'; // this is always true - don't change this
174            switch ($dependency['rel']) {
175                case 'ge' :
176                    $min = $dependency['version'];
177                break;
178                case 'gt' :
179                    $min = $dependency['version'];
180                    $exclude = array($dependency['version']);
181                break;
182                case 'eq' :
183                    $recommended = $dependency['version'];
184                break;
185                case 'lt' :
186                    $max = $dependency['version'];
187                    $exclude = array($dependency['version']);
188                break;
189                case 'le' :
190                    $max = $dependency['version'];
191                break;
192                case 'ne' :
193                    $exclude = array($dependency['version']);
194                break;
195            }
196        } else {
197            $pinfo['package'] = $dependency['name'];
198            $min = isset($dependency['min']) ? $dependency['min'] : false;
199            $max = isset($dependency['max']) ? $dependency['max'] : false;
200            $recommended = isset($dependency['recommended']) ?
201                $dependency['recommended'] : false;
202            if (isset($dependency['exclude'])) {
203                if (!isset($dependency['exclude'][0])) {
204                    $exclude = array($dependency['exclude']);
205                }
206            }
207        }
208
209        $skippedphp = $found = $release = false;
210        if (!is_array($info['r']) || !isset($info['r'][0])) {
211            $info['r'] = array($info['r']);
212        }
213
214        foreach ($info['r'] as $release) {
215            if (!isset($this->_rest->_options['force']) && ($installed &&
216                  version_compare($release['v'], $installed, '<'))) {
217                continue;
218            }
219
220            if (in_array($release['v'], $exclude)) { // skip excluded versions
221                continue;
222            }
223
224            // allow newer releases to say "I'm OK with the dependent package"
225            if ($xsdversion == '2.0' && isset($release['co'])) {
226                if (!is_array($release['co']) || !isset($release['co'][0])) {
227                    $release['co'] = array($release['co']);
228                }
229
230                foreach ($release['co'] as $entry) {
231                    if (isset($entry['x']) && !is_array($entry['x'])) {
232                        $entry['x'] = array($entry['x']);
233                    } elseif (!isset($entry['x'])) {
234                        $entry['x'] = array();
235                    }
236
237                    if ($entry['c'] == $deppackage['channel'] &&
238                          strtolower($entry['p']) == strtolower($deppackage['package']) &&
239                          version_compare($deppackage['version'], $entry['min'], '>=') &&
240                          version_compare($deppackage['version'], $entry['max'], '<=') &&
241                          !in_array($release['v'], $entry['x'])) {
242                        if (version_compare($release['m'], phpversion(), '>')) {
243                            // skip dependency releases that require a PHP version
244                            // newer than our PHP version
245                            $skippedphp = $release;
246                            continue;
247                        }
248
249                        $recommended = $release['v'];
250                        break;
251                    }
252                }
253            }
254
255            if ($recommended) {
256                if ($release['v'] != $recommended) { // if we want a specific
257                    // version, then skip all others
258                    continue;
259                }
260
261                if (!in_array($release['s'], $states)) {
262                    // the stability is too low, but we must return the
263                    // recommended version if possible
264                    return $this->_returnDownloadURL($base, $package, $release, $info, true, false, $channel);
265                }
266            }
267
268            if ($min && version_compare($release['v'], $min, 'lt')) { // skip too old versions
269                continue;
270            }
271
272            if ($max && version_compare($release['v'], $max, 'gt')) { // skip too new versions
273                continue;
274            }
275
276            if ($installed && version_compare($release['v'], $installed, '<')) {
277                continue;
278            }
279
280            if (in_array($release['s'], $states)) { // if in the preferred state...
281                if (version_compare($release['m'], phpversion(), '>')) {
282                    // skip dependency releases that require a PHP version
283                    // newer than our PHP version
284                    $skippedphp = $release;
285                    continue;
286                }
287
288                $found = true; // ... then use it
289                break;
290            }
291        }
292
293        if (!$found && $skippedphp) {
294            $found = null;
295        }
296
297        return $this->_returnDownloadURL($base, $package, $release, $info, $found, $skippedphp, $channel);
298    }
299}