Subversion Repositories Applications.gtt

Rev

Rev 187 | Details | Compare with Previous | Last modification | View Log | RSS feed

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