1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
<?php
/*-----8<--------------------------------------------------------------------
*
* BEdita - a semantic content management framework
*
* Copyright 2008-2014 ChannelWeb Srl, Chialab Srl
*
* This file is part of BEdita: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* BEdita is distributed WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with BEdita (see LICENSE.LGPL).
* If not, see <http://gnu.org/licenses/lgpl-3.0.html>.
*
*------------------------------------------------------------------->8-----
*/
/**
* Revision Control System Model for SVN
*/
if (interface_exists('RCS') != true) {
APP::import('Vendor', 'rcs');
}
class Svn implements RCS {
public $path = null;
public $username = '';
public $passwd = '';
public $lastCommandCode = null;
public $lastCommand = null;
public $lastError = null;
/**
* Get the repository type for this model
*
* @return string: the repository type
*/
public function type() {
return 'svn';
}
/**
* Startup the model initializing a path.
*
* @param string $path: the repository path
* @return void
*/
public function startup($path) {
$this->path = $path;
}
/**
* Set authorization params for the model
*
* @param string $user: the SVN username
* @param string $passwd: the SVN user password
* @return void
*/
public function authorize($user, $passwd) {
$this->username = $user;
$this->passwd = $passwd;
}
/**
* Checkout a repository
*
* @param string $url: the repository remote url
* @param string $path: where the repository should be checkouted
* @param null $branch: SVN does not use branches
* @return array: the checkout command results
*/
public function cloneRemote($url, $path = null, $branch = null) {
if (empty($path)) {
$path = $this->path;
}
$cmd = "svn checkout $url -b $path";
$res = $this->exec($cmd);
return $res;
}
/**
* Update a repository
*
* @param string $path: optional. Can be a repository path to update
* @return array: the update command results
*/
public function up($path = null) {
if (empty($path)) {
$path = $this->path;
}
$cmd = 'cd ' . $path . '; ';
$cmd .= 'svn up --username ' . $this->username . ' --password ' . $this->passwd;
$res = $this->command($cmd);
return $res;
}
/**
* Get the current status of the repository
*
* @param string $path: optional. Can be a repository path
* @return array: the status command results
*/
public function status($path = null) {
if (empty($path)) {
$path = $this->path;
}
if (empty($path)) {
$path = $this->path;
}
$cmd = 'cd ' . $path . '; ';
$cmd .= 'svn status ' . $path;
$status = $this->command($cmd);
$status = (!empty($status))? implode("\n", $status) : null;
return $status;
}
/**
* SVN does not use branches
*
* @return boolean: false
*/
public function branch($path = null) {
return false;
}
/**
* SVN does not use branches
*
* @return boolean: false
*/
public function branches($path = null, $remote = false) {
return false;
}
/**
* Check if the current or given path is a valid SVN repository
*
* @param string $path: optional. Can be a repository path
* @return boolean: is a valid repository or not
*/
public function valid($path = null) {
$status = $this->status($path);
if ($this->lastCommandCode == 0 && strpos($status, 'is not a working copy') === false) {
return true;
}
return false;
}
/**
* Get last commit data
*
* @param string $path: optional. Can be a repository path
* @return array: a list of properties for the last commit
*/
public function lastCommit($path = null) {
if (empty($path)) {
$path = $this->path;
}
$cmd = 'cd ' . $path . '; ';
$cmd .= 'svn log -l 1 --username ' . $this->username . ' --password ' . $this->passwd;
$output = $this->command($cmd);
if (!empty($output)) {
$data = explode(' | ', $output[1]);
if (!empty($data)) {
return array(
'hash' => $data[0],
'date' => $data[2],
'author' => $data[1],
'message' => $output[3]
);
}
}
return null;
}
/**
* Exec a given SVN command
* fill `lastCommand` and eventually `lastCommandCode` and `lastError` attributes.
*
* @param string $cmd: the command to exec
* @return array: the result of the exec
*/
public function command($cmd) {
$cmd = $cmd . ' 2>&1';
$this->lastCommand = $cmd;
exec($cmd, $res, $this->lastCommandCode);
if ($this->lastCommandCode != 0) {
$this->lastError = $res;
}
return $res;
}
}
?>