/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Copyright ©2006 by sTEFANs
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* A collection of functions that encapsulates SVN commands.
* @copyright Copyright ©2006 by sTEFANs
* @license http://opensource.org/licenses/lgpl-license.php GNU General Public License
require_once('var.inc.php');
/** Project configuration interface */
require_once ('config.inc.php');
* Returns the help output of <i>svn</i>.
* @return array <i>svn help</i> output in
* form of a result array as produced
* Returns the help output of <i>svnlook</i>.
* @return array <i>svnlook help</i> output in
* form of a result array as produced
* Returns the help output of <i>svnadmin</i>.
* @return array <i>svnadmin</i> help output in
* form of a result array as produced
* Returns the help output of <i>svnversion</i>.
* @return array <i>svnversion</i> help output in
* form of a result array as produced
* Returns the version of SVN.
* @return string Version of Subversion
$result =
ExecSvnCmd($command, '', $switches);
* Returns the output of <i>svn info</i> for
* the repository contained in a result
* array as produced by <b>ExecSvnCmd</b>.
* @param SwcConfig $config SWC config for the operation.
* @return array Output line by line.
$arg =
$config->GetRepositoryRoot();
$result =
ExecSvnCmd($command, $arg, $switches);
* Returns the number of the youngest SVN revision (HEAD)
* available in the repository.
* @param SwcConfig $config SWC config for the operation.
* @return int Number of HEAD revision
* Returns the formated date of the latest changes
* @param SwcConfig $config SWC config for the operation.
* @return string Date of HEAD revision.
* Returns the output of <i>svn info</i>
* contained in a result array as produced
* @param SwcConfig $config SWC config for the operation.
* @param boolean $xml_output Whether or not output shall
* be printed in xml format (default).
* @return array Output line by line.
$arg =
$config->GetWebspaceRootDir();
static $result_plain =
NULL;
static $result_xml =
NULL;
if (!$xml_output &&
empty($result_plain)){
$result_plain =
ExecSvnCmd($command, $arg, $switches);
}else if ($xml_output &&
empty($result_xml)){
$result_xml =
ExecSvnCmd($command, $arg, $switches);
$res =
($xml_output ?
$result_xml :
$result_plain);
* Returns the number of the SVN revision
* @param SwcConfig $config SWC config for the operation.
* @return int Webspace revision.
* Returns the path of the repository
* where the webspace referes to with
* repository root directory.
* @param SwcConfig $config SWC config for the operation.
* @return string Repository path the webspace points to.
* Returns the formated date of the
* revision of the workspace.
* @param SwcConfig $config SWC config for the operation.
* @return string Date of webspace revision.
* Returns the output of <i>svn status</i>
* contained in a result array as produced
* @param SwcConfig $config SWC config for the operation.
* @return array Result of <i>svn status</i> execution.
$switches =
'--verbose --show-updates --non-interactive';
$arg =
$config->GetWebspaceRootDir();
$result =
ExecSvnCmd($command, $arg, $switches);
* Returns the output of <i>svn log</i>
* contained in a result array as produced
* @param SwcConfig $config SWC config for the operation.
* @return array Result of <i>svn log</i> execution.
$switches =
'--verbose --non-interactive';
$arg =
$config->GetWebspaceRootDir();
* Returns the output of <i>svn list</i>
* contained in a result array as produced
* @param SwcConfig $config SWC config for the operation.
* @return array Result of <i>svn list</i> execution.
$rep_root =
$config->GetRepositoryRoot();
$switches =
'--recursive';
* Returns the array of tags available
* @param SwcConfig $config SWC config for the operation.
* @return array Version tags.
$tag_dirs =
$config->GetTagDirs();
$depth =
$config->GetMaxTagDirDepth();
* Returns the array of branches available
* @param SwcConfig $config SWC config for the operation.
* @return array Version branches.
$branch_dirs =
$config->GetBranchDirs();
$depth =
$config->GetMaxBranchDirDepth();
* Returns an array of repository paths starting
* at $root with maximum directory count given in $level.
* @param SwcConfig $config SWC config for the operation.
* @param string or array $roots (Array of) repository directories to start from
* @param int $depth Maximum directory depth.
* @return array Array of paths of repository.
$rep_root =
$config->GetRepositoryRoot();
$switches =
'--recursive';
foreach($root_dirs as $dir){
$parent_dir =
trim($dir);
$arg =
trim($rep_root).
$parent_dir;
* Builds SVN directory tree.
* @param array $paths Reference to SVN path array
* @param array $tree Reference to 'svnlook tree' stdout that
* should be transformed into SVN paths.
* @param int $max_level Mamimum level for paths.
* @param string $parent_dir Parent directory in path.
* @return array Paths already found.
if (! empty($parent_dir)){
while ($offset !==
false &&
($level <
$max_level)){
$offset =
strpos($path, '/', $offset);
$path =
substr($path, 0, $offset);
if (!empty($parent_dir)){
$path =
$parent_dir.
'/'.
$path;
* Performs webspace update to head revision
* and returns the output of 'svn update'
* contained in a result array as produced
* @param SwcConfig $config SWC config for the operation.
* @return array Result of <i>svn update</i> execution.
$switches =
'--non-interactive';
$arg =
$config->GetWebspaceRootDir();
$result =
ExecSvnCmd($command, $arg, $switches);
* Performs workspace (webspace) checkout
* and returns the output of 'svn checkout'
* contained in a result array as produced
* @param SwcConfig $config SWC config for the operation.
* @return array Result of <i>svn checkout</i> execution.
$trunk_path =
$config->GetTrunkDir();
$command =
'svn checkout';
$switches =
'--revision HEAD --non-interactive';
$arg =
$config->GetRepositoryRoot().
'/'.
$trunk_path;
$arg =
$arg.
' '.
$config->GetWebspaceRootDir();
* Performs webspace cleanup
* and returns the output of <i>svn cleanup</i>
* contained in a result array as produced
* @param SwcConfig $config SWC config for the operation.
* @return array Result of <i>svn cleanup</i> execution.
$command =
'svn cleanup';
$switches =
'--non-interactive';
$arg =
$config->GetWebspaceRootDir();
* Switches webspace to given repository path.
* @param SwcConfig $config SWC config for the operation.
* @param string $path Repository path to switch to
* A path without repository root should be applied.
* @return array Result of <i>svn switch</i> execution.
$path =
$config->GetTrunkDir();
$switches =
'--non-interactive';
$arg =
$config->GetRepositoryRoot();
$arg .=
' '.
$config->GetWebspaceRootDir();
* Returns the SVN User switch for the given config.
* @param SwcConfig $config Config for retrieving user.
* @return string svn switch for user (empty string if no user
$user =
$config->GetSvnUser();
if ($user !=
NULL &&
$user !=
''){
$switch =
' --username '.
$user;
* Returns the SVN password switch for the given config.
* @param SwcConfig $config Config for retrieving user.
* @return string svn switch for password (empty string if no password
$pw =
$config->GetSvnPassword();
if ($pw !=
NULL &&
$pw !=
''){
$switch =
' --password '.
$pw;
* Executes an SVN command.
* @param string $cmd Command to execute
* @param string $switch Switches to be applied for the given command
* @param string $arg Arguments of the command
* @return array Result array containing commandline (idx = IDX_CMDLINE),
* standard out array (idx = IDX_STDOUT), error array (idx = IDX_ERROUT),
* and return code of the command (idx = IDX_CMD_RC).
function &ExecSvnCmd($cmd, $arg =
'', $switches =
''){
0 =>
array("pipe", "r"), // stdin is a pipe that the child will read from
1 =>
array("pipe", "w"), // stdout is a pipe that the child will write to
2 =>
array("pipe", "w") // stderr is a pipe to write to
$cmdline =
$cmd.
" ".
$switches.
" ".
$arg;
$process =
proc_open($cmdline, $descriptorspec, $pipes, $cwd);
// $pipes now looks like this:
// 0 => writeable handle connected to child stdin
// 1 => readable handle connected to child stdout
// 1 => readable handle connected to child errout
// It is important that you close any pipes before calling
// proc_close in order to avoid a deadlock
// echo "CMD: $cmdline<br/>";
// PrintDebugArray($result, "Result");
* Reads data from a resource (e.g. pipe) and
* returns an array that contains each line for a
* @param resource $handle Resource handle
* @param int $length Maximum line length used if a line is not delimited.
* @return array Array containing a delimited line per index.
$line =
fgets($handle, $length);
$line =
fgets($handle, $length);
* Returns a string of the given array that is
* encapsulated within $startstr and $endstr.
* @param array $array Line array as provided by 'GetLineArray' to
* search the string within.
* @param string $startstr String to search.
* @param string $endstr String to delimit search. If NULL, the returned string
* @return string String within a 'line' of $array that starts with '$startstr'
* and ends with '$endstr'. If $startstr is not found, '?' is returned. $startstr
function ParseArray (&$array, $startstr, $endstr =
NULL){
while ($idx <
count($array) &&
strpos($array[$idx], $startstr) ===
false){
if ($idx <
count($array)){
$start =
strpos($array[$idx], $startstr) +
strlen($startstr);
$len =
strpos($array[$idx], $endstr) -
$start;
$val =
substr($array[$idx], $start, $len);
$val =
substr($array[$idx], $start);
* HTML and user friendly output of an array.
* @param array $array Array to print
* @param string $name Name of the array, also printed.
* @param boolean $encode_html_chars Whether or not to
* encode special characters to HTML equivalents (default).
function PrintArray($array, $name =
'', $encode_html_chars =
true){
echo
'<span class="text_low_bold">'.
$name.
'</span><br/>';
foreach($array as $line){