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: Cat.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 Cat command manager class
55
 *
56
 * Outputs the content of specified files or URLs without performing
57
 * a checkout operation.
58
 *
59
 * $switches is an array containing one or more command line options
60
 * defined by the following associative keys:
61
 *
62
 * <code>
63
 *
64
 * $switches = array(
65
 *  'r [revision]'  =>  'ARG (some commands also take ARG1:ARG2 range)
66
 *                        A revision argument can be one of:
67
 *                           NUMBER       revision number
68
 *                           "{" DATE "}" revision at start of the date
69
 *                           "HEAD"       latest in repository
70
 *                           "BASE"       base rev of item's working copy
71
 *                           "COMMITTED"  last commit at or before BASE
72
 *                           "PREV"       revision just before COMMITTED',
73
 *                      // either 'r' or 'revision' may be used
74
 *  'username'      =>  'Subversion repository login',
75
 *  'password'      =>  'Subversion repository password',
76
 *  'no-auth-cache' =>  true|false,
77
 *                      // Do not cache authentication tokens
78
 *  'config-dir'    =>  'Path to a Subversion configuration directory'
79
 * );
80
 *
81
 * </code>
82
 *
83
 * Note: Subversion does not offer an XML output option for this subcommand
84
 *
85
 * The non-interactive option available on the command-line
86
 * svn client may also be set (true|false), but it is set to true by default.
87
 *
88
 * Usage example:
89
 * <code>
90
 * <?php
91
 * require_once 'VersionControl/SVN.php';
92
 *
93
 * // Setup error handling -- always a good idea!
94
 * $svnstack = &PEAR_ErrorStack::singleton('VersionControl_SVN');
95
 *
96
 * // Set up runtime options. Will be passed to all
97
 * // subclasses.
98
 * $options = array('fetchmode' => VERSIONCONTROL_SVN_FETCHMODE_RAW);
99
 *
100
 * // Pass array of subcommands we need to factory
101
 * $svn = VersionControl_SVN::factory(array('cat'), $options);
102
 *
103
 * // Define any switches and aguments we may need
104
 * $switches = array('username' => 'user', 'password' => 'pass');
105
 * $args = array('svn://svn.example.com/repos/TestProject/testfile.php');
106
 *
107
 * // Run command
108
 * if ($output = $svn->cat->run($args, $switches)) {
109
 *     print_r($output);
110
 * } else {
111
 *     if (count($errs = $svnstack->getErrors())) {
112
 *         foreach ($errs as $err) {
113
 *             echo '<br />'.$err['message']."<br />\n";
114
 *             echo "Command used: " . $err['params']['cmd'];
115
 *         }
116
 *     }
117
 * }
118
 * ?>
119
 * </code>
120
 *
121
 * @package  VersionControl_SVN
122
 * @version  0.3.4
123
 * @category SCM
124
 * @author   Clay Loveless <clay@killersoft.com>
125
 */
126
class VersionControl_SVN_Cat extends VersionControl_SVN
127
{
128
    /**
129
     * Valid switches for svn cat
130
     *
131
     * @var     array
132
     * @access  public
133
     */
134
    var $valid_switches = array('r',
135
                                'revision',
136
                                'username',
137
                                'password',
138
                                'no-auth-cache',
139
                                'no_auth_cache',
140
                                'non-interactive',
141
                                'non_interactive',
142
                                'config-dir',
143
                                'config_dir'
144
                                );
145
 
146
 
147
    /**
148
     * Command-line arguments that should be passed
149
     * <b>outside</b> of those specified in {@link switches}.
150
     *
151
     * @var     array
152
     * @access  public
153
     */
154
    var $args = array();
155
 
156
    /**
157
     * Minimum number of args required by this subcommand.
158
     * See {@link http://svnbook.red-bean.com/svnbook/ Version Control with Subversion},
159
     * Subversion Complete Reference for details on arguments for this subcommand.
160
     * @var     int
161
     * @access  public
162
     */
163
    var $min_args = 1;
164
 
165
    /**
166
     * Switches required by this subcommand.
167
     * See {@link http://svnbook.red-bean.com/svnbook/ Version Control with Subversion},
168
     * Subversion Complete Reference for details on arguments for this subcommand.
169
     * @var     array
170
     * @access  public
171
     */
172
    var $required_switches = array();
173
 
174
    /**
175
     * Use exec or passthru to get results from command.
176
     * Defaults to true here so that Cat just dumps out whatever's in the repository without
177
     * attempting to alter it. Good for pulling binary data from the repository.
178
     * @var     bool
179
     * @access  public
180
     */
181
    var $passthru = true;
182
 
183
    /**
184
     * Prepare the svn subcommand switches.
185
     *
186
     * Defaults to non-interactive mode, and will auto-set the
187
     * --xml switch if $fetchmode is set to VERSIONCONTROL_SVN_FETCHMODE_XML,
188
     * VERSIONCONTROL_SVN_FETCHMODE_ASSOC or VERSIONCONTROL_SVN_FETCHMODE_OBJECT
189
     *
190
     * @param   void
191
     * @return  int    true on success, false on failure. Check PEAR_ErrorStack
192
     *                 for error details, if any.
193
     */
194
    function prepare()
195
    {
196
        $meets_requirements = $this->checkCommandRequirements();
197
        if (!$meets_requirements) {
198
            return false;
199
        }
200
 
201
        $valid_switches     = $this->valid_switches;
202
        $switches           = $this->switches;
203
        $args               = $this->args;
204
        $fetchmode          = $this->fetchmode;
205
        $invalid_switches   = array();
206
        $_switches          = '';
207
 
208
        foreach ($switches as $switch => $val) {
209
            if (in_array($switch, $valid_switches)) {
210
                $switch = str_replace('_', '-', $switch);
211
                switch ($switch) {
212
                    case 'revision':
213
                    case 'username':
214
                    case 'password':
215
                    case 'config-dir':
216
                        $_switches .= "--$switch $val ";
217
                        break;
218
                    case 'r':
219
                        $_switches .= "-$switch $val ";
220
                        break;
221
                    case 'no-auth-cache':
222
                    case 'non-interactive':
223
                        if ($val === true) {
224
                            $_switches .= "--$switch ";
225
                        }
226
                        break;
227
                   default:
228
                        // that's all, folks!
229
                        break;
230
                }
231
            } else {
232
                $invalid_switches[] = $switch;
233
            }
234
        }
235
 
236
        // We don't want interactive mode
237
        if (strpos($_switches, 'non-interactive') === false) {
238
            $_switches .= '--non-interactive ';
239
        }
240
 
241
        $_switches = trim($_switches);
242
        $this->_switches = $_switches;
243
 
244
        $cmd = "$this->svn_path $this->_svn_cmd $_switches";
245
        if (!empty($args)) {
246
            $cmd .= ' '. join(' ', $args);
247
        }
248
        $this->_prepped_cmd = $cmd;
249
        $this->prepared = true;
250
 
251
        $invalid = count($invalid_switches);
252
        if ($invalid > 0) {
253
            $params['was'] = 'was';
254
            $params['is_invalid_switch'] = 'is an invalid switch';
255
            if ($invalid > 1) {
256
                $params['was'] = 'were';
257
                $params['is_invalid_switch'] = 'are invalid switches';
258
            }
259
            $params['list'] = $invalid_switches;
260
            $params['switches'] = $switches;
261
            $params['_svn_cmd'] = ucfirst($this->_svn_cmd);
262
            $this->_stack->push(VERSIONCONTROL_SVN_NOTICE_INVALID_SWITCH, 'notice', $params);
263
        }
264
        return true;
265
    }
266
 
267
    // }}}
268
    // {{{ parseOutput()
269
 
270
    /**
271
     * Handles output parsing of standard and verbose output of command.
272
     *
273
     * @param   array   $out    Array of output captured by exec command in {@link run}.
274
     * @return  mixed   Returns output requested by fetchmode (if available), or raw output
275
     *                  if desired fetchmode is not available.
276
     * @access  public
277
     */
278
    function parseOutput($out)
279
    {
280
        $fetchmode = $this->fetchmode;
281
        switch($fetchmode) {
282
            case VERSIONCONTROL_SVN_FETCHMODE_RAW:
283
                return join("\n", $out);
284
                break;
285
            case VERSIONCONTROL_SVN_FETCHMODE_ASSOC:
286
                // Temporary, see parseOutputArray below
287
                return join("\n", $out);
288
                break;
289
            case VERSIONCONTROL_SVN_FETCHMODE_OBJECT:
290
                // Temporary, will return object-ified array from
291
                // parseOutputArray
292
                return join("\n", $out);
293
                break;
294
            case VERSIONCONTROL_SVN_FETCHMODE_XML:
295
                // Temporary, will eventually build an XML string
296
                // with XML_Util or XML_Tree
297
                return join("\n", $out);
298
                break;
299
            default:
300
                // What you get with VERSIONCONTROL_SVN_FETCHMODE_DEFAULT
301
                return join("\n", $out);
302
                break;
303
        }
304
    }
305
 
306
    /**
307
     * Helper method for parseOutput that parses output into an associative array
308
     *
309
     * @todo Finish this method! : )
310
     */
311
    function parseOutputArray($out)
312
    {
313
        $parsed = array();
314
    }
315
}
316
 
317
/// }}}
318
?>