Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * PEAR_Command_Categories (list-packages, list-categories, list-category
4
 * commands)
5
 *
6
 * PHP versions 4 and 5
7
 *
8
 * LICENSE: This source file is subject to version 3.0 of the PHP license
9
 * that is available through the world-wide-web at the following URI:
10
 * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
11
 * the PHP License and are unable to obtain it through the web, please
12
 * send a note to license@php.net so we can mail you a copy immediately.
13
 *
14
 * @category   pear
15
 * @package    PEAR_Frontend_Web
16
 * @author     Tias Guns <tias@ulyssis.org>
17
 * @copyright  1997-2007 The PHP Group
18
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
19
 * @version    CVS: $Id: Categories.php 237916 2007-06-17 14:33:53Z tias $
20
 * @link       http://pear.php.net/package/PEAR_Frontend_Web
21
 * @since      File available since Release 1.0
22
 */
23
 
24
/**
25
 * base class
26
 */
27
require_once 'PEAR/Command/Common.php';
28
require_once 'PEAR/REST.php';
29
 
30
/**
31
 * PEAR_Frontend_Web command for listing individual category information
32
 *
33
 * @category   pear
34
 * @package    PEAR_Frontend_Web
35
 * @author     Tias Guns <tias@ulyssis.org>
36
 * @copyright  1997-2007 The PHP Group
37
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
38
 * @version    Release: TODO
39
 * @link       http://pear.php.net/package/PEAR_Frontend_Web
40
 * @since      File available since Release 0.6.0
41
 */
42
class PEAR_Command_Categories extends PEAR_Command_Common
43
{
44
    // {{{ properties
45
 
46
    var $commands = array(
47
        'list-packages' => array(
48
            'summary' => 'List All Packages of a Channel',
49
            'function' => 'doListPackages',
50
            'shortcut' => 'lp',
51
            'options' => array(
52
                'channel' => array(
53
                    'shortopt' => 'c',
54
                    'doc' => 'specify a channel other than the default channel',
55
                    'arg' => 'CHAN',
56
                    ),
57
                'allchannels' => array(
58
                    'shortopt' => 'a',
59
                    'doc' => 'list available packages from all channels',
60
                    ),
61
                ),
62
            'doc' => '
63
Lists all the packages of a channel. For each channel it displays the
64
channel and package name.',
65
            ),
66
        'list-categories' => array(
67
            'summary' => 'List All Categories',
68
            'function' => 'doListCategories',
69
            'shortcut' => 'cats',
70
            'options' => array(
71
                'channel' => array(
72
                    'shortopt' => 'c',
73
                    'doc' => 'specify a channel other than the default channel',
74
                    'arg' => 'CHAN',
75
                    ),
76
                'allchannels' => array(
77
                    'shortopt' => 'a',
78
                    'doc' => 'list available categories from all channels',
79
                    ),
80
                'packages' => array(
81
                    'shortopt' => 'p',
82
                    'doc' => 'list the packagenames of the categories too',
83
                    ),
84
                ),
85
            'doc' => '
86
Lists the categories available on the channel server. For each channel
87
it displays the channel and categorie name, and optionally the all the
88
names of the packages in the categories.',
89
            ),
90
        'list-category' => array(
91
            'summary' => 'List All Packages of a Category',
92
            'function' => 'doListCategory',
93
            'shortcut' => 'cat',
94
            'options' => array(
95
                'channel' => array(
96
                    'shortopt' => 'c',
97
                    'doc' => 'specify a channel other than the default channel',
98
                    'arg' => 'CHAN',
99
                    )
100
                ),
101
            'doc' => '<category> [<category>...]
102
Lists all the packages of a category of a channel. For each category
103
it displays the channel and package name, with local and remote version
104
information, and the summary.',
105
            ),
106
        );
107
 
108
    // }}}
109
    // {{{ constructor
110
 
111
    /**
112
     * PEAR_Command_Registry constructor.
113
     *
114
     * @access public
115
     */
116
    function PEAR_Command_Categories(&$ui, &$config)
117
    {
118
        parent::PEAR_Command_Common($ui, $config);
119
    }
120
 
121
    // }}}
122
    // {{{ doListPackages()
123
 
124
    function doListPackages($command, $options, $params)
125
    {
126
        $reg = &$this->config->getRegistry();
127
        if (isset($options['allchannels']) && $options['allchannels'] == true) {
128
            // over all channels
129
            unset($options['allchannels']);
130
            $channels = $reg->getChannels();
131
            $errors = array();
132
            foreach ($channels as $channel) {
133
                if ($channel->getName() != '__uri') {
134
                    $options['channel'] = $channel->getName();
135
                    $ret = $this->doListPackages($command, $options, $params);
136
                    if ($ret !== true) {
137
                        $errors[] = $ret;
138
                    }
139
                }
140
            }
141
            if (count($errors) !== 0) {
142
                // for now, only give first error
143
                return $errors[0];
144
            }
145
            return true;
146
        }
147
 
148
        $savechannel = $channel = $this->config->get('default_channel');
149
        if (isset($options['channel'])) {
150
            $channel = $options['channel'];
151
            if ($reg->channelExists($channel)) {
152
                $this->config->set('default_channel', $channel);
153
            } else {
154
                return $this->raiseError("Channel \"$channel\" does not exist");
155
            }
156
        }
157
        $chan = $reg->getChannel($channel);
158
        // we need Remote::_checkChannelForStatus()
159
        require_once 'PEAR/Command/Remote.php';
160
        //$cmd = new PEAR_Command_Remote($this->ui, $this->config);
161
        //if (PEAR::isError($e = $cmd->_checkChannelForStatus($channel, $chan))) {
162
        if (PEAR::isError($e = PEAR_Command_Remote::_checkChannelForStatus($channel, $chan))) {
163
            return $e;
164
        }
165
        if ($chan->supportsREST($this->config->get('preferred_mirror')) &&
166
              $base = $chan->getBaseURL('REST1.0', $this->config->get('preferred_mirror'))) {
167
            $rest = &$this->config->getREST('1.0', array());
168
            $packages = $rest->listPackages($base);
169
        } else {
170
            return PEAR::raiseError($command.' only works for REST servers');
171
        }
172
        if (PEAR::isError($packages)) {
173
            $this->config->set('default_channel', $savechannel);
174
            return $this->raiseError('The package list could not be fetched from the remote server. Please try again. (Debug info: "' . $packages->getMessage() . '")');
175
        }
176
 
177
        $data = array(
178
            'caption' => 'Channel ' . $channel . ' All packages:',
179
            'border' => true,
180
            'headline' => array('Channel', 'Package'),
181
            'channel' => $channel,
182
            );
183
 
184
        if (count($packages) === 0) {
185
            unset($data['headline']);
186
            $data['data'] = 'No packages registered';
187
        } else {
188
            $data['data'] = array();
189
            foreach($packages as $item) {
190
                $array = array(
191
                        $channel,
192
                        $item,
193
                            );
194
                $data['data'][] = $array;
195
            }
196
        }
197
 
198
        $this->config->set('default_channel', $savechannel);
199
        $this->ui->outputData($data, $command);
200
        return true;
201
    }
202
 
203
    // }}}
204
    // {{{ doListCategories()
205
 
206
    function doListCategories($command, $options, $params)
207
    {
208
        $reg = &$this->config->getRegistry();
209
        if (isset($options['allchannels']) && $options['allchannels'] == true) {
210
            // over all channels
211
            unset($options['allchannels']);
212
            $channels = $reg->getChannels();
213
            $errors = array();
214
            foreach ($channels as $channel) {
215
                if ($channel->getName() != '__uri') {
216
                    $options['channel'] = $channel->getName();
217
                    $ret = $this->doListCategories($command, $options, $params);
218
                    if ($ret !== true) {
219
                        $errors[] = $ret;
220
                    }
221
                }
222
            }
223
            if (count($errors) !== 0) {
224
                // for now, only give first error
225
                return $errors[0];
226
            }
227
            return true;
228
        }
229
 
230
        $savechannel = $channel = $this->config->get('default_channel');
231
        if (isset($options['channel'])) {
232
            $channel = $options['channel'];
233
            if ($reg->channelExists($channel)) {
234
                $this->config->set('default_channel', $channel);
235
            } else {
236
                return $this->raiseError("Channel \"$channel\" does not exist");
237
            }
238
        }
239
        $chan = $reg->getChannel($channel);
240
        PEAR::staticPushErrorHandling(PEAR_ERROR_RETURN);
241
        // we need Remote::_checkChannelForStatus()
242
        require_once 'PEAR/Command/Remote.php';
243
        //$cmd = new PEAR_Command_Remote($this->ui, $this->config);
244
        //if (PEAR::isError($e = $cmd->_checkChannelForStatus($channel, $chan))) {
245
        if (PEAR::isError($e = PEAR_Command_Remote::_checkChannelForStatus($channel, $chan))) {
246
            return $e;
247
        }
248
        if ($chan->supportsREST($this->config->get('preferred_mirror')) &&
249
              $base = $chan->getBaseURL('REST1.1', $this->config->get('preferred_mirror'))) {
250
            $rest = &$this->config->getREST('1.1', array());
251
        } elseif ($chan->supportsREST($this->config->get('preferred_mirror')) &&
252
              $base = $chan->getBaseURL('REST1.0', $this->config->get('preferred_mirror'))) {
253
            $rest = &$this->config->getREST('1.0', array());
254
        } else {
255
            return PEAR::raiseError($command.' only works for REST servers');
256
        }
257
        $categories = $rest->listCategories($base);
258
 
259
        $data = array(
260
            'caption' => 'Channel ' . $channel . ' All categories:',
261
            'border' => true,
262
            'headline' => array('Channel', 'Category'),
263
            'channel' => $channel,
264
            );
265
        if (isset($options['packages']) && $options['packages']) {
266
            $data['headline'][] = 'Packages';
267
        }
268
 
269
        if (PEAR::isError($categories)) {
270
            unset($data['headline']);
271
            $data['data'] = 'The category list could not be fetched from the remote server. Please try again. (Debug info: "' . $categories->getMessage() . '")';
272
        } elseif (count($categories) === 0) {
273
            unset($data['headline']);
274
            $data['data'] = 'No categories registered';
275
        } else {
276
            $data['data'] = array();
277
 
278
            foreach($categories as $item) {
279
                $category = $item['_content'];
280
                $array = array(
281
                        $channel,
282
                        $category);
283
 
284
                if (isset($options['packages']) && $options['packages']) {
285
                    // get packagenames
286
                    $cat_pkgs = $rest->listCategory($base, $category);
287
                    if (!PEAR::isError($cat_pkgs)) {
288
                        $packages = array();
289
                        foreach($cat_pkgs as $cat_pkg) {
290
                            $packages[] = $cat_pkg['_content'];
291
                        }
292
                        $array[] = $packages;
293
                    }
294
                }
295
                $data['data'][] = $array;
296
            }
297
        }
298
        PEAR::staticPopErrorHandling();
299
 
300
        $this->config->set('default_channel', $savechannel);
301
        $this->ui->outputData($data, $command);
302
        return true;
303
    }
304
 
305
    // }}}
306
    // {{{ doListCategory()
307
 
308
    function doListCategory($command, $options, $params)
309
    {
310
        if (count($params) < 1) {
311
            return PEAR::raiseError('Not enough parameters, use: '.$command.' <category> [<category>...]');
312
        }
313
        if (count($params) > 1) {
314
            $errors = array();
315
            foreach($params as $pkg) {
316
                $ret = $this->doListCategory($command, $options, array($pkg));
317
                if ($ret !== true) {
318
                    $errors[] = $ret;
319
                    return $ret;
320
                }
321
            }
322
            if (count($errors) !== 0) {
323
                // for now, only give first error
324
                return $errors[0];
325
            }
326
            return true;
327
        }
328
        $category = $params[0];
329
 
330
        $savechannel = $channel = $this->config->get('default_channel');
331
        $reg = &$this->config->getRegistry();
332
        if (isset($options['channel'])) {
333
            $channel = $options['channel'];
334
            if ($reg->channelExists($channel)) {
335
                $this->config->set('default_channel', $channel);
336
            } else {
337
                return $this->raiseError("Channel \"$channel\" does not exist");
338
            }
339
        }
340
        $chan = $reg->getChannel($channel);
341
        // we need Remote::_checkChannelForStatus()
342
        require_once 'PEAR/Command/Remote.php';
343
        //$cmd = new PEAR_Command_Remote($this->ui, $this->config);
344
        //if (PEAR::isError($e = $cmd->_checkChannelForStatus($channel, $chan))) {
345
        if (PEAR::isError($e = PEAR_Command_Remote::_checkChannelForStatus($channel, $chan))) {
346
            return $e;
347
        }
348
        if ($chan->supportsREST($this->config->get('preferred_mirror')) &&
349
              $base = $chan->getBaseURL('REST1.1', $this->config->get('preferred_mirror'))) {
350
            $rest = &$this->config->getREST('1.1', array());
351
        } elseif ($chan->supportsREST($this->config->get('preferred_mirror')) &&
352
              $base = $chan->getBaseURL('REST1.0', $this->config->get('preferred_mirror'))) {
353
            $rest = &$this->config->getREST('1.0', array());
354
        } else {
355
            return PEAR::raiseError($command.' only works for REST servers');
356
        }
357
        $packages = $rest->listCategory($base, $category, true);
358
        if (PEAR::isError($packages)) {
359
            $this->config->set('default_channel', $savechannel);
360
            return $this->raiseError('The package list could not be fetched from the remote server. Please try again. (Debug info: "' . $packages->getMessage() . '")');
361
        }
362
 
363
        $data = array(
364
            'caption' => 'Channel '.$channel.' Category '.$category.' All packages:',
365
            'border' => true,
366
            'headline' => array('Channel', 'Package', 'Local', 'Remote', 'Summary'),
367
            'channel' => $channel,
368
            );
369
        if (count($packages) === 0) {
370
            unset($data['headline']);
371
            $data['data'] = 'No packages registered';
372
        } else {
373
            $data['data'] = array();
374
            foreach ($packages as $package_data) {
375
                $package = $package_data['_content'];
376
                $info = $package_data['info'];
377
                if (!isset($info['v'])) {
378
                    $remote = '-';
379
                } else {
380
                    $remote = $info['v'].' ('.$info['st'].')';
381
                }
382
                $summary = $info['s'];
383
                if ($reg->packageExists($package, $channel)) {
384
                    $local = sprintf('%s (%s)',
385
                        $reg->packageInfo($package, 'version', $channel),
386
                        $reg->packageInfo($package, 'release_state', $channel));
387
                } else {
388
                    $local = '-';
389
                }
390
                $data['data'][] = array($channel, $package, $local, $remote, $summary);
391
            }
392
        }
393
 
394
        $this->config->set('default_channel', $savechannel);
395
        $this->ui->outputData($data, $command);
396
        return true;
397
    }
398
 
399
}
400
 
401
?>