Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/* vim: set expandtab tabstop=4 shiftwidth=4: */
3
// +----------------------------------------------------------------------+
4
// | PHP version 5                                                        |
5
// +----------------------------------------------------------------------+
6
// | Copyright (c) 2004-2007, Clay Loveless                               |
7
// | All rights reserved.                                                 |
8
// +----------------------------------------------------------------------+
9
// | This LICENSE is in the BSD license style.                            |
10
// | http://www.opensource.org/licenses/bsd-license.php                   |
11
// |                                                                      |
12
// | Redistribution and use in source and binary forms, with or without   |
13
// | modification, are permitted provided that the following conditions   |
14
// | are met:                                                             |
15
// |                                                                      |
16
// |  * Redistributions of source code must retain the above copyright    |
17
// |    notice, this list of conditions and the following disclaimer.     |
18
// |                                                                      |
19
// |  * Redistributions in binary form must reproduce the above           |
20
// |    copyright notice, this list of conditions and the following       |
21
// |    disclaimer in the documentation and/or other materials provided   |
22
// |    with the distribution.                                            |
23
// |                                                                      |
24
// |  * Neither the name of Clay Loveless nor the names of contributors   |
25
// |    may be used to endorse or promote products derived from this      |
26
// |    software without specific prior written permission.               |
27
// |                                                                      |
28
// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS  |
29
// | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT    |
30
// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS    |
31
// | FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE      |
32
// | COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,  |
33
// | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
34
// | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;     |
35
// | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER     |
36
// | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT   |
37
// | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN    |
38
// | ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE      |
39
// | POSSIBILITY OF SUCH DAMAGE.                                          |
40
// +----------------------------------------------------------------------+
41
// | Author: Clay Loveless <clay@killersoft.com>                          |
42
// +----------------------------------------------------------------------+
43
//
44
// $Id: List.php 286753 2009-08-03 19:37:03Z mrook $
45
//
46
 
47
/**
48
 * @package     VersionControl_SVN
49
 * @category    VersionControl
50
 * @author      Clay Loveless <clay@killersoft.com>
51
 */
52
 
53
/**
54
 * Subversion List command manager class
55
 *
56
 * List directory entries in the repository
57
 *
58
 * If the 'target' option is omitted, '.' is assumed, meaning the
59
 * repository URL of the current working directory.
60
 *
61
 * $switches is an array containing one or more command line options
62
 * defined by the following associative keys:
63
 *
64
 * <code>
65
 *
66
 * array(
67
 *  'r [revision]'  =>  'ARG (some commands also take ARG1:ARG2 range)
68
 *                        A revision argument can be one of:
69
 *                           NUMBER       revision number
70
 *                           "{" DATE "}" revision at start of the date
71
 *                           "HEAD"       latest in repository
72
 *                           "BASE"       base rev of item's working copy
73
 *                           "COMMITTED"  last commit at or before BASE
74
 *                           "PREV"       revision just before COMMITTED',
75
 *                      // either 'r' or 'revision' may be used
76
 *  'v [verbose]'   =>  true|false,
77
 *                      // prints extra information
78
 *  'R'             =>  true|false,
79
 *                      // descend recursively
80
 *  'recursive'     =>  true|false,
81
 *                      // descend recursively
82
 *  'username'      =>  'Subversion repository login',
83
 *  'password'      =>  'Subversion repository password',
84
 *  'no-auth-cache' =>  true|false,
85
 *                      // Do not cache authentication tokens
86
 *  'config-dir'    =>  'Path to a Subversion configuration directory'
87
 * );
88
 *
89
 * </code>
90
 *
91
 * With the 'verbose' option set to true, the following fields show the
92
 * status of the item:
93
 *
94
 *     Revision number of the last commit
95
 *     Author of the last commit
96
 *     Size (in bytes)
97
 *     Date and time of the last commit
98
 *
99
 *
100
 * Usage example:
101
 * <code>
102
 * <?php
103
 * require_once 'VersionControl/SVN.php';
104
 *
105
 * // Setup error handling -- always a good idea!
106
 * $svnstack = &PEAR_ErrorStack::singleton('VersionControl_SVN');
107
 *
108
 * // Set up runtime options. Will be passed to all
109
 * // subclasses.
110
 * $options = array('fetchmode' => VERSIONCONTROL_SVN_FETCHMODE_RAW);
111
 *
112
 * // Pass array of subcommands we need to factory
113
 * $svn = VersionControl_SVN::factory(array('list'), $options);
114
 *
115
 * // Define any switches and aguments we may need
116
 * $switches = array('R' => true, 'username' => 'user', 'password' => 'pass');
117
 * $args = array('svn://svn.example.com/repos/TestProject');
118
 *
119
 * // Run command
120
 * if ($output = $svn->list->run($args, $switches)) {
121
 *     print_r($output);
122
 * } else {
123
 *     if (count($errs = $svnstack->getErrors())) {
124
 *         foreach ($errs as $err) {
125
 *             echo '<br />'.$err['message']."<br />\n";
126
 *             echo "Command used: " . $err['params']['cmd'];
127
 *         }
128
 *     }
129
 * }
130
 * ?>
131
 * </code>
132
 *
133
 * Note: Subversion does not offer an XML output option for this subcommand
134
 *
135
 * @package  VersionControl_SVN
136
 * @version  0.3.4
137
 * @category SCM
138
 * @author   Clay Loveless <clay@killersoft.com>
139
 */
140
class VersionControl_SVN_List extends VersionControl_SVN
141
{
142
    /**
143
     * Valid switches for svn list
144
     *
145
     * @var     array
146
     * @access  public
147
     */
148
    var $valid_switches = array('r',
149
                                'revision',
150
                                'v',
151
                                'verbose',
152
                                'R',
153
                                'recursive',
154
                                'username',
155
                                'password',
156
                                'no-auth-cache',
157
                                'no_auth_cache',
158
                                'non-interactive',
159
                                'non_interactive',
160
                                'config-dir',
161
                                'config_dir'
162
                                );
163
 
164
    /**
165
     * Command-line arguments that should be passed
166
     * <b>outside</b> of those specified in {@link switches}.
167
     *
168
     * @var     array
169
     * @access  public
170
     */
171
    var $args = array();
172
 
173
    /**
174
     * Minimum number of args required by this subcommand.
175
     * See {@link http://svnbook.red-bean.com/svnbook/ Version Control with Subversion},
176
     * Subversion Complete Reference for details on arguments for this subcommand.
177
     * @var     int
178
     * @access  public
179
     */
180
    var $min_args = 0;
181
 
182
    /**
183
     * Switches required by this subcommand.
184
     * See {@link http://svnbook.red-bean.com/svnbook/ Version Control with Subversion},
185
     * Subversion Complete Reference for details on arguments for this subcommand.
186
     * @var     array
187
     * @access  public
188
     */
189
    var $required_switches = array();
190
 
191
    /**
192
     * Use exec or passthru to get results from command.
193
     * @var     bool
194
     * @access  public
195
     */
196
    var $passthru = false;
197
 
198
    /**
199
     * Prepare the svn subcommand switches.
200
     *
201
     * Defaults to non-interactive mode, and will auto-set the
202
     * --xml switch (if available) if $fetchmode is set to VERSIONCONTROL_SVN_FETCHMODE_XML,
203
     * VERSIONCONTROL_SVN_FETCHMODE_ASSOC or VERSIONCONTROL_SVN_FETCHMODE_OBJECT
204
     *
205
     * @param   void
206
     * @return  int    true on success, false on failure. Check PEAR_ErrorStack
207
     *                 for error details, if any.
208
     */
209
    function prepare()
210
    {
211
        $meets_requirements = $this->checkCommandRequirements();
212
        if (!$meets_requirements) {
213
            return false;
214
        }
215
 
216
        $valid_switches     = $this->valid_switches;
217
        $switches           = $this->switches;
218
        $args               = $this->args;
219
        $fetchmode          = $this->fetchmode;
220
        $invalid_switches   = array();
221
        $_switches          = '';
222
 
223
        foreach ($switches as $switch => $val) {
224
            if (in_array($switch, $valid_switches)) {
225
                $switch = str_replace('_', '-', $switch);
226
                switch ($switch) {
227
                    case 'revision':
228
                    case 'username':
229
                    case 'password':
230
                    case 'encoding':
231
                    case 'config-dir':
232
                        $_switches .= "--$switch $val ";
233
                        break;
234
                    case 'recursive':
235
                    case 'verbose':
236
                    case 'no-auth-cache':
237
                    case 'non-interactive':
238
                        if ($val === true) {
239
                            $_switches .= "--$switch ";
240
                        }
241
                        break;
242
                    case 'r':
243
                        $_switches .= "-$switch $val ";
244
                        break;
245
                    case 'R':
246
                    case 'v':
247
                        if ($val === true) {
248
                            $_switches .= "-$switch ";
249
                        }
250
                        break;
251
                    default:
252
                        // that's all, folks!
253
                        break;
254
                }
255
            } else {
256
                $invalid_switches[] = $switch;
257
            }
258
        }
259
        // We don't want interactive mode
260
        if (strpos($_switches, 'non-interactive') === false) {
261
            $_switches .= '--non-interactive ';
262
        }
263
 
264
        $_switches = trim($_switches);
265
        $this->_switches = $_switches;
266
 
267
        $cmd = "$this->svn_path $this->_svn_cmd $_switches";
268
        if (!empty($args)) {
269
            $cmd .= ' '. join(' ', $args);
270
        }
271
 
272
        $this->_prepped_cmd = $cmd;
273
        $this->_prepared = true;
274
 
275
        $invalid = count($invalid_switches);
276
        if ($invalid > 0) {
277
            $params['was'] = 'was';
278
            $params['is_invalid_switch'] = 'is an invalid switch';
279
            if ($invalid > 1) {
280
                $params['was'] = 'were';
281
                $params['is_invalid_switch'] = 'are invalid switches';
282
            }
283
            $params['list'] = $invalid_switches;
284
            $params['switches'] = $switches;
285
            $params['_svn_cmd'] = ucfirst($this->_svn_cmd);
286
            $this->_stack->push(VERSIONCONTROL_SVN_NOTICE_INVALID_SWITCH, 'notice', $params);
287
        }
288
        return true;
289
    }
290
 
291
    // }}}
292
    // {{{ parseOutput()
293
 
294
    /**
295
     * Handles output parsing of standard and verbose output of command.
296
     *
297
     * @param   array   $out    Array of output captured by exec command in {@link run}.
298
     * @return  mixed   Returns output requested by fetchmode (if available), or raw output
299
     *                  if desired fetchmode is not available.
300
     * @access  public
301
     */
302
    function parseOutput($out)
303
    {
304
        $fetchmode = $this->fetchmode;
305
        switch($fetchmode) {
306
            case VERSIONCONTROL_SVN_FETCHMODE_RAW:
307
                return join("\n", $out);
308
                break;
309
            case VERSIONCONTROL_SVN_FETCHMODE_ARRAY:
310
            case VERSIONCONTROL_SVN_FETCHMODE_ASSOC:
311
                return $this->parseOutputArray($out);
312
                break;
313
            case VERSIONCONTROL_SVN_FETCHMODE_OBJECT:
314
                return (object) $this->parseOutputArray($out);
315
                break;
316
            case VERSIONCONTROL_SVN_FETCHMODE_XML:
317
                // Temporary, will eventually build an XML string
318
                // with XML_Util or XML_Tree
319
                return join("\n", $out);
320
                break;
321
            default:
322
                // What you get with VERSIONCONTROL_SVN_FETCHMODE_DEFAULT
323
                return join("\n", $out);
324
                break;
325
        }
326
    }
327
 
328
    /**
329
     * Helper method for parseOutput that parses output into an associative or numbered array
330
     *
331
     * @param   array   $items  Item list from the svn list command (already split into an array
332
     *                          by exec)
333
     * @return  array
334
     * @access  public
335
     */
336
    function parseOutputArray($items)
337
    {
338
        $parsed = array();
339
 
340
        // check switches for verbose output
341
        $verbose = false;
342
        if ((isset($this->switches['v']) && $this->switches['v'] === true) ||
343
            (isset($this->switches['verbose']) && $this->switches['verbose'] === true)) {
344
            $verbose = true;
345
        }
346
 
347
 
348
        if ($verbose) {
349
            // Must trim off verbose information PRIOR to natcasesort
350
            $path_items = array();
351
            $item_vdata = array();
352
            foreach ($items as $item) {
353
                // Regex should work with svn list's "%b %d %H:%M" and "%b %d  %Y" date formats
354
                preg_match("/\s*(\d+) \s?(\S+)\s+(\d+)? (\w{3} +\d{2} +\d{2}:?\d{2}) (.*)/", $item, $matches);
355
                $path_items[] = $matches[5];
356
                $item_vdata[] = array(
357
                                'revision'  => $matches[1],
358
                                'author'    => $matches[2],
359
                                'size'      => $matches[3],
360
                                'date'      => $matches[4]
361
                              );
362
            }
363
            $items = $path_items;
364
        }
365
        natcasesort($items);
366
 
367
        if ($this->fetchmode == VERSIONCONTROL_SVN_FETCHMODE_ASSOC) {
368
            $branch_arrays = array();
369
            foreach ($items as $key => $path) {
370
                $dir = dirname($path);
371
                $branches = explode('/', $path);
372
                $branch = array();
373
                $last = end($branches);
374
                $type = 'F';
375
                if ($last == '') {
376
                    // Directories have an empty item in the last array slot
377
                    $type = 'D';
378
                    $name = prev($branches);
379
                }
380
                foreach ($branches as $leaf) {
381
                    if ($leaf == $last) {
382
                        if ($type == 'D') {
383
                            $branch[$dir] = array('name' => array($name), 'type' => array($type));
384
                        } else {
385
                            $branch[$dir] = array('name' => array($leaf), 'type' => array($type));
386
                        }
387
                    } else {
388
                        $branch[$dir] = array('name' => array($leaf), 'type' => array('D'));
389
                    }
390
                    if ($verbose) {
391
                        $branch[$dir] = array_merge($branch[$dir], $item_vdata[$key]);
392
                    }
393
                }
394
                $branch_arrays[] = $branch;
395
            }
396
 
397
            foreach ($branch_arrays as $branch) {
398
                $parsed = array_merge_recursive($parsed, $branch);
399
            }
400
        } else {
401
            foreach ($items as $key => $path) {
402
                $item = array();
403
                if (substr($path, -1) == '/') {
404
                    $item['type'] = 'D';
405
                    $path = substr($path, 0, -1);
406
                } else {
407
                    $item['type'] = 'F';
408
                }
409
                $item['name'] = $path;
410
                if ($verbose) {
411
                    $item = array_merge($item, $item_vdata[$key]);
412
                }
413
                $parsed[] = $item;
414
            }
415
        }
416
 
417
        return $parsed;
418
    }
419
 
420
}
421
 
422
// }}
423
?>