~ubuntu-branches/ubuntu/hardy/php5/hardy-updates

« back to all changes in this revision

Viewing changes to pear/PEAR/Command/Registry.php

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-10-09 03:14:32 UTC
  • Revision ID: james.westby@ubuntu.com-20051009031432-kspik3lobxstafv9
Tags: upstream-5.0.5
ImportĀ upstreamĀ versionĀ 5.0.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
// /* vim: set expandtab tabstop=4 shiftwidth=4: */
 
3
// +----------------------------------------------------------------------+
 
4
// | PHP Version 5                                                        |
 
5
// +----------------------------------------------------------------------+
 
6
// | Copyright (c) 1997-2004 The PHP Group                                |
 
7
// +----------------------------------------------------------------------+
 
8
// | This source file is subject to version 3.0 of the PHP license,       |
 
9
// | that is bundled with this package in the file LICENSE, and is        |
 
10
// | available through the world-wide-web at the following url:           |
 
11
// | http://www.php.net/license/3_0.txt.                                  |
 
12
// | If you did not receive a copy of the PHP license and are unable to   |
 
13
// | obtain it through the world-wide-web, please send a note to          |
 
14
// | license@php.net so we can mail you a copy immediately.               |
 
15
// +----------------------------------------------------------------------+
 
16
// | Author: Stig Bakken <ssb@php.net>                                    |
 
17
// |                                                                      |
 
18
// +----------------------------------------------------------------------+
 
19
//
 
20
// $Id: Registry.php,v 1.36 2004/01/08 17:33:13 sniper Exp $
 
21
 
 
22
require_once 'PEAR/Command/Common.php';
 
23
require_once 'PEAR/Registry.php';
 
24
require_once 'PEAR/Config.php';
 
25
 
 
26
class PEAR_Command_Registry extends PEAR_Command_Common
 
27
{
 
28
    // {{{ properties
 
29
 
 
30
    var $commands = array(
 
31
        'list' => array(
 
32
            'summary' => 'List Installed Packages',
 
33
            'function' => 'doList',
 
34
            'shortcut' => 'l',
 
35
            'options' => array(),
 
36
            'doc' => '[package]
 
37
If invoked without parameters, this command lists the PEAR packages
 
38
installed in your php_dir ({config php_dir)).  With a parameter, it
 
39
lists the files in that package.
 
40
',
 
41
            ),
 
42
        'shell-test' => array(
 
43
            'summary' => 'Shell Script Test',
 
44
            'function' => 'doShellTest',
 
45
            'shortcut' => 'st',
 
46
            'options' => array(),
 
47
            'doc' => '<package> [[relation] version]
 
48
Tests if a package is installed in the system. Will exit(1) if it is not.
 
49
   <relation>   The version comparison operator. One of:
 
50
                <, lt, <=, le, >, gt, >=, ge, ==, =, eq, !=, <>, ne
 
51
   <version>    The version to compare with
 
52
'),
 
53
        'info' => array(
 
54
            'summary'  => 'Display information about a package',
 
55
            'function' => 'doInfo',
 
56
            'shortcut' => 'in',
 
57
            'options'  => array(),
 
58
            'doc'      => '<package>
 
59
Displays information about a package. The package argument may be a
 
60
local package file, an URL to a package file, or the name of an
 
61
installed package.'
 
62
            )
 
63
        );
 
64
 
 
65
    // }}}
 
66
    // {{{ constructor
 
67
 
 
68
    /**
 
69
     * PEAR_Command_Registry constructor.
 
70
     *
 
71
     * @access public
 
72
     */
 
73
    function PEAR_Command_Registry(&$ui, &$config)
 
74
    {
 
75
        parent::PEAR_Command_Common($ui, $config);
 
76
    }
 
77
 
 
78
    // }}}
 
79
 
 
80
    // {{{ doList()
 
81
 
 
82
    function _sortinfo($a, $b)
 
83
    {
 
84
        return strcmp($a['package'], $b['package']);
 
85
    }
 
86
 
 
87
    function doList($command, $options, $params)
 
88
    {
 
89
        $reg = new PEAR_Registry($this->config->get('php_dir'));
 
90
        if (sizeof($params) == 0) {
 
91
            $installed = $reg->packageInfo();
 
92
            usort($installed, array(&$this, '_sortinfo'));
 
93
            $i = $j = 0;
 
94
            $data = array(
 
95
                'caption' => 'Installed packages:',
 
96
                'border' => true,
 
97
                'headline' => array('Package', 'Version', 'State')
 
98
                );
 
99
            foreach ($installed as $package) {
 
100
                $data['data'][] = array($package['package'],
 
101
                                          $package['version'],
 
102
                                          @$package['release_state']);
 
103
            }
 
104
            if (count($installed)==0) {
 
105
                $data = '(no packages installed)';
 
106
            }
 
107
            $this->ui->outputData($data, $command);
 
108
        } else {
 
109
            if (file_exists($params[0]) && !is_dir($params[0])) {
 
110
                include_once "PEAR/Common.php";
 
111
                $obj = &new PEAR_Common;
 
112
                $info = $obj->infoFromAny($params[0]);
 
113
                $headings = array('Package File', 'Install Path');
 
114
                $installed = false;
 
115
            } else {
 
116
                $info = $reg->packageInfo($params[0]);
 
117
                $headings = array('Type', 'Install Path');
 
118
                $installed = true;
 
119
            }
 
120
            if (PEAR::isError($info)) {
 
121
                return $this->raiseError($info);
 
122
            }
 
123
            if ($info === null) {
 
124
                return $this->raiseError("`$params[0]' not installed");
 
125
            }
 
126
            $list = $info['filelist'];
 
127
            if ($installed) {
 
128
                $caption = 'Installed Files For ' . $params[0];
 
129
            } else {
 
130
                $caption = 'Contents of ' . basename($params[0]);
 
131
            }
 
132
            $data = array(
 
133
                'caption' => $caption,
 
134
                'border' => true,
 
135
                'headline' => $headings);
 
136
            foreach ($list as $file => $att) {
 
137
                if ($installed) {
 
138
                    if (empty($att['installed_as'])) {
 
139
                        continue;
 
140
                    }
 
141
                    $data['data'][] = array($att['role'], $att['installed_as']);
 
142
                } else {
 
143
                    if (isset($att['baseinstalldir'])) {
 
144
                        $dest = $att['baseinstalldir'] . DIRECTORY_SEPARATOR .
 
145
                            $file;
 
146
                    } else {
 
147
                        $dest = $file;
 
148
                    }
 
149
                    switch ($att['role']) {
 
150
                        case 'test':
 
151
                        case 'data':
 
152
                            if ($installed) {
 
153
                                break 2;
 
154
                            }
 
155
                            $dest = '-- will not be installed --';
 
156
                            break;
 
157
                        case 'doc':
 
158
                            $dest = $this->config->get('doc_dir') . DIRECTORY_SEPARATOR .
 
159
                                $dest;
 
160
                            break;
 
161
                        case 'php':
 
162
                        default:
 
163
                            $dest = $this->config->get('php_dir') . DIRECTORY_SEPARATOR .
 
164
                                $dest;
 
165
                    }
 
166
                    $dest = preg_replace('!/+!', '/', $dest);
 
167
                    $file = preg_replace('!/+!', '/', $file);
 
168
                    $data['data'][] = array($file, $dest);
 
169
                }
 
170
            }
 
171
            $this->ui->outputData($data, $command);
 
172
 
 
173
 
 
174
        }
 
175
        return true;
 
176
    }
 
177
 
 
178
    // }}}
 
179
    // {{{ doShellTest()
 
180
 
 
181
    function doShellTest($command, $options, $params)
 
182
    {
 
183
        $this->pushErrorHandling(PEAR_ERROR_RETURN);
 
184
        $reg = &new PEAR_Registry($this->config->get('php_dir'));
 
185
        // "pear shell-test Foo"
 
186
        if (sizeof($params) == 1) {
 
187
            if (!$reg->packageExists($params[0])) {
 
188
                exit(1);
 
189
            }
 
190
            // "pear shell-test Foo 1.0"
 
191
        } elseif (sizeof($params) == 2) {
 
192
            $v = $reg->packageInfo($params[0], 'version');
 
193
            if (!$v || !version_compare("$v", "{$params[1]}", "ge")) {
 
194
                exit(1);
 
195
            }
 
196
            // "pear shell-test Foo ge 1.0"
 
197
        } elseif (sizeof($params) == 3) {
 
198
            $v = $reg->packageInfo($params[0], 'version');
 
199
            if (!$v || !version_compare("$v", "{$params[2]}", $params[1])) {
 
200
                exit(1);
 
201
            }
 
202
        } else {
 
203
            $this->popErrorHandling();
 
204
            $this->raiseError("$command: expects 1 to 3 parameters");
 
205
            exit(1);
 
206
        }
 
207
    }
 
208
 
 
209
    // }}}
 
210
    // {{{ doInfo
 
211
 
 
212
    function doInfo($command, $options, $params)
 
213
    {
 
214
        // $params[0] The package for showing info
 
215
        if (sizeof($params) != 1) {
 
216
            return $this->raiseError("This command only accepts one param: ".
 
217
                                     "the package you want information");
 
218
        }
 
219
        if (@is_file($params[0])) {
 
220
            $obj  = &new PEAR_Common();
 
221
            $info = $obj->infoFromAny($params[0]);
 
222
        } else {
 
223
            $reg = &new PEAR_Registry($this->config->get('php_dir'));
 
224
            $info = $reg->packageInfo($params[0]);
 
225
        }
 
226
        if (PEAR::isError($info)) {
 
227
            return $info;
 
228
        }
 
229
        if (empty($info)) {
 
230
            $this->raiseError("Nothing found for `$params[0]'");
 
231
            return;
 
232
        }
 
233
        unset($info['filelist']);
 
234
        unset($info['changelog']);
 
235
        $keys = array_keys($info);
 
236
        $longtext = array('description', 'summary');
 
237
        foreach ($keys as $key) {
 
238
            if (is_array($info[$key])) {
 
239
                switch ($key) {
 
240
                    case 'maintainers': {
 
241
                        $i = 0;
 
242
                        $mstr = '';
 
243
                        foreach ($info[$key] as $m) {
 
244
                            if ($i++ > 0) {
 
245
                                $mstr .= "\n";
 
246
                            }
 
247
                            $mstr .= $m['name'] . " <";
 
248
                            if (isset($m['email'])) {
 
249
                                $mstr .= $m['email'];
 
250
                            } else {
 
251
                                $mstr .= $m['handle'] . '@php.net';
 
252
                            }
 
253
                            $mstr .= "> ($m[role])";
 
254
                        }
 
255
                        $info[$key] = $mstr;
 
256
                        break;
 
257
                    }
 
258
                    case 'release_deps': {
 
259
                        $i = 0;
 
260
                        $dstr = '';
 
261
                        foreach ($info[$key] as $d) {
 
262
                            if (isset($this->_deps_rel_trans[$d['rel']])) {
 
263
                                $rel = $this->_deps_rel_trans[$d['rel']];
 
264
                            } else {
 
265
                                $rel = $d['rel'];
 
266
                            }
 
267
                            if (isset($this->_deps_type_trans[$d['type']])) {
 
268
                                $type = ucfirst($this->_deps_type_trans[$d['type']]);
 
269
                            } else {
 
270
                                $type = $d['type'];
 
271
                            }
 
272
                            if (isset($d['name'])) {
 
273
                                $name = $d['name'] . ' ';
 
274
                            } else {
 
275
                                $name = '';
 
276
                            }
 
277
                            if (isset($d['version'])) {
 
278
                                $version = $d['version'] . ' ';
 
279
                            } else {
 
280
                                $version = '';
 
281
                            }
 
282
                            $dstr .= "$type $name$rel $version\n";
 
283
                        }
 
284
                        $info[$key] = $dstr;
 
285
                        break;
 
286
                    }
 
287
                    case 'provides' : {
 
288
                        $debug = $this->config->get('verbose');
 
289
                        if ($debug < 2) {
 
290
                            $pstr = 'Classes: ';
 
291
                        } else {
 
292
                            $pstr = '';
 
293
                        }
 
294
                        $i = 0;
 
295
                        foreach ($info[$key] as $p) {
 
296
                            if ($debug < 2 && $p['type'] != "class") {
 
297
                                continue;
 
298
                            }
 
299
                            // Only print classes when verbosity mode is < 2
 
300
                            if ($debug < 2) {
 
301
                                if ($i++ > 0) {
 
302
                                    $pstr .= ", ";
 
303
                                }
 
304
                                $pstr .= $p['name'];
 
305
                            } else {
 
306
                                if ($i++ > 0) {
 
307
                                    $pstr .= "\n";
 
308
                                }
 
309
                                $pstr .= ucfirst($p['type']) . " " . $p['name'];
 
310
                                if (isset($p['explicit']) && $p['explicit'] == 1) {
 
311
                                    $pstr .= " (explicit)";
 
312
                                }
 
313
                            }
 
314
                        }
 
315
                        $info[$key] = $pstr;
 
316
                        break;
 
317
                    }
 
318
                    default: {
 
319
                        $info[$key] = implode(", ", $info[$key]);
 
320
                        break;
 
321
                    }
 
322
                }
 
323
            }
 
324
            if ($key == '_lastmodified') {
 
325
                $hdate = date('Y-m-d', $info[$key]);
 
326
                unset($info[$key]);
 
327
                $info['Last Modified'] = $hdate;
 
328
            } else {
 
329
                $info[$key] = trim($info[$key]);
 
330
                if (in_array($key, $longtext)) {
 
331
                    $info[$key] = preg_replace('/  +/', ' ', $info[$key]);
 
332
                }
 
333
            }
 
334
        }
 
335
        $caption = 'About ' . $info['package'] . '-' . $info['version'];
 
336
        $data = array(
 
337
            'caption' => $caption,
 
338
            'border' => true);
 
339
        foreach ($info as $key => $value) {
 
340
            $key = ucwords(trim(str_replace('_', ' ', $key)));
 
341
            $data['data'][] = array($key, $value);
 
342
        }
 
343
        $data['raw'] = $info;
 
344
 
 
345
        $this->ui->outputData($data, 'package-info');
 
346
    }
 
347
 
 
348
    // }}}
 
349
}
 
350
 
 
351
?>