Location: PHPKode > projects > SvnDashboard > 1.1.3.140/phpLib/functions/svn.funcs.php
<?php

require_once( 'file.funcs.php' );
require_once( 'console.funcs.php' );

function isRepo( $path )
{
	// TODO: this function is deprecated, use isLocalRepo() or isRemoteRepo()
	// trigger_error( 'isRepo() has been replaced by isLocalRepo() and isRemoteRepo()' , E_DEPRECATED );
	trigger_error( 'isRepo() has been replaced by isLocalRepo() and isRemoteRepo()' , E_USER_WARNING );

	// ==================== Ensure Trailing / on Path
	$path = trailingSlash( $path );

	$repo = true;
	if (is_dir( $path )) // -- path is valid
	{
		$requiredDirs = array('conf', 'db', 'hooks', 'locks');
		foreach ($requiredDirs as $rdir)
		{
			if (!is_dir( $path.$rdir ))
			{
				$repo = false;
				break;
			}
		}
	}
	else // -- path does not exist
	{
		$repo = false;
	}

	return $repo;
}

function isLocalRepo( $repoPath, $svnPath )
{
	// deprecated -- replaced with SvnServer object method(s)
	
	// ==================== Initialize Variables
	$errors = array( );
	$svnPath = trailingSlash( $svnPath );

	// ==================== Ensure Trailing / on Path
	$repoPath = trailingSlash( $repoPath );

	// ==================== Determine if repoPath is a Repo
	$isRepo = console( sprintf( '%ssvnlook author %s', trailingSlash( $svnPath ), $repoPath ), $errors );

	return $isRepo;
}

function isRemoteRepo( $host, $hostUsername, $repoPath, $svnPath )
{
	// deprecated -- replaced with SvnServer object method(s)
	
	// ==================== Initialize Variables
	$errors = array( );
	$svnPath = trailingSlash( $svnPath );
	$timeout = 5;
	$command = sprintf( 'ssh -T -o ConnectTimeout=%d %s@%s', $timeout, $hostUsername, $host );
	$subCommands = array( );

	// ==================== Ensure Trailing / on Path
	$repoPath = trailingSlash( $repoPath );

	// ==================== Determine if Path is a Repo
	$subCommands[] = sprintf( '%ssvnlook author %s', trailingSlash( $svnPath ), $repoPath );
	$string = $command.' "'.implode( '; ', $subCommands ).'"';
	$isRepo = console( $string, $errors );

	return $isRepo;
}

function findLocalRepos( $baseDir, $svnPath )
{
	// deprecated -- replaced with SvnServer object method(s)
	
	$svnPath = trailingSlash( $svnPath );
	$baseDir = trailingSlash( $baseDir );

	$foundRepos = array();

	if (is_dir( $baseDir )) // -- dir exists
	{
		if (isLocalRepo( $baseDir, $svnPath )) // -- this is a repo
		{
			$foundRepos[] = $baseDir;
		}
		else // -- not a repo
		{
			$dir = opendir( $baseDir );
			if ($dir) // -- dir is open
			{
				while ($file = readdir( $dir )) // -- each file in dir
				{
					if ($file{0} != '.' && is_dir( $baseDir.$file )) // -- this file is a dir
					{
						$subRepos = findLocalRepos( $baseDir.$file.'/', $svnPath ); // -- check for repos within this dir
						$foundRepos = array_merge( $foundRepos, $subRepos );
					}
				}
			}
		}
	}

	return $foundRepos;
}

function createLocalRepo( $path, $svnPath, &$errors = array( ))
{
	// deprecated -- replaced with SvnServer object method(s)
	
	$svnPath = trailingSlash( $svnPath );
	
	// ==================== Ensure $errors Argument is an Array
	if (!is_array( $errors ))
	{
		$errors = array( $errors );
	}

	$commands = array( );

	// ==================== Ensure Trailing / on Path
	$path = trailingSlash( $path );

	// ==================== Create New Repository
	exec( 'whoami', $username );
	$username = reset( $username );

	$commands[] = sprintf( 'HOME=/home/%s;export HOME;%ssvnadmin create %s', $username, trailingSlash( $svnPath ), $path );
	$commands[] = sprintf( 'chgrp svn %s', $path );

	$success = console( $commands, $errors );

	return $success;
}

function createRemoteRepo( $username, $host, $path, &$errors = array( ))
{
	// deprecated -- replaced with SvnServer object method(s)
	
	// ==================== Ensure $errors Argument is an Array
	if (!is_array( $errors ))
	{
		$errors = array( $errors );
	}

	// ==================== Initialize Variables
	$timeout = 5;
	$command = sprintf( 'ssh -T -o ConnectTimeout=%d %s@%s', $timeout, $username, $host );
	$subCommands = array( );

	// ==================== Ensure Trailing / on Path
	$path = trailingSlash( $path );

	// ==================== Create New Repository
	$subCommands[] = sprintf( 'svnadmin create %s', $path );
	$string = $command.' "'.implode( '; ', $subCommands ).'"';

	$success = console( $string, $errors );

	return $success;
}

function svn_logStat( $path, $username, $password )
{
	// deprecated -- replaced with SvnServer object method(s)
	
	$authors = array( );
	$days = array( );
	$xml = svn_logXML( $path, $username, $password );
	
	$xmlDoc = new DOMDocument( );
	$xmlDoc->loadXML( $xml );
	
	$logEntries = $xmlDoc->getElementsByTagName( 'logentry' );
	
	// ==================== Commits By Author
	foreach ( $logEntries as $logEntry )
	{
		$authorNode = $logEntry->getElementsByTagName( 'author' )->item( 0 );
		
		$author = $authorNode->nodeValue;
		
		if (!isset( $authors[$author] ))
		{
			$authors[$author] = array( );
			
			$authors[$author]['commits'] = 0;
		}
		
		$authors[$author]['commits']++;		
	}
	
	// ==================== Commits By Day
	foreach ( $logEntries as $logEntry )
	{
		$dateNode = $logEntry->getElementsByTagName( 'date' )->item( 0 );
		
		$date = $dateNode->nodeValue;
		$index = strtotime( date('m/d/Y', strtotime( $date )));
		
		if (!isset( $days[$index] ))
		{
			$days[$index] = array( );
			$days[$index]['commits'] = 0;
		}
		
		$days[$index]['commits']++;
	}
	
	$return = array( 'days' => $days, 'authors' => $authors );
	return $return;
}

function svn_logXML( $path, $username, $password )
{
	// deprecated -- replaced with SvnServer object method(s)
	
	exec( 'whoami', $localUsername );
	$localUsername = reset( $localUsername );
	
	$command = sprintf( 'HOME=/home/%s;export HOME;svn log --xml -v %s --username %s --password %s 2>&1', $localUsername, $path, $username, $password );	
	exec( $command, $output, $success );
	
	if ($success == 0)
	{
		return implode( "\n", arrayTrim( $output ));
	}
	else
	{
		throw new Exception( 'svn_logXML: failed: '.implode( '--', $output ));
	}
}

function svn_logRaw( $path, $username, $password )
{
	// deprecated -- replaced with SvnServer object method(s)
	
	exec( 'whoami', $localUsername );
	$localUsername = reset( $localUsername );
	
	$command = sprintf( 'HOME=/home/%s;export HOME;svn log -v %s --username %s --password %s 2>&1', $localUsername, $path, $username, $password );	
	exec( $command, $output, $success );
	
	if ($success == 0)
	{
		return implode( "\n", arrayTrim( $output ));
	}
	else
	{
		throw new Exception( 'svn_logRaw: failed: '.implode( '--', $output ));
	}
}

function svn_lastTimestamp( $path, $username, $password )
{
	// deprecated -- replaced with SvnServer object method(s)
	
	$date = svn_lastDate( $path, $username, $password );
	$date = preg_replace( '/^(\d{4}-\d{2}-\d{2}\s*\d{2}:\d{2}:\d{2}\s+[\-0-9]{4,5}).*?$/', '\\1', $date );
	
	return strtotime( $date );
}

function svn_lastDate( $path, $username, $password )
{
	// deprecated -- replaced with SvnServer object method(s)
	
	$last = svn_last( $path, $username, $password );
	
	if (isset( $last['date'] ))
	{
		return $last['date'];
	}
	else
	{
		return NULL;
	}
}

function svn_lastMessage( $path, $username, $password )
{
	// deprecated -- replaced with SvnServer object method(s)
	
	return svn_lastMsg( $path, $username, $password );
}

function svn_lastMsg( $path, $username, $password )
{
	// deprecated -- replaced with SvnServer object method(s)
	
	$last = svn_last( $path, $username, $password );
	
	if (isset( $last['msg'] ))
	{
		return $last['msg'];
	}
	else
	{
		return NULL;
	}
}

function svn_lastRevision( $path, $username, $password )
{
	// deprecated -- replaced with SvnServer object method(s)
	
	return svn_lastRev( $path, $username, $password );
}

function svn_lastRev( $path, $username, $password )
{
	// deprecated -- replaced with SvnServer object method(s)
	
	$last = svn_last( $path, $username, $password );
	
	if (isset( $last['rev'] ))
	{
		return $last['rev'];
	}
	else
	{
		return NULL;
	}
}

function svn_lastAuthor( $path, $username, $password )
{
	// deprecated -- replaced with SvnServer object method(s)
	
	$last = svn_last( $path, $username, $password );
	
	if (isset( $last['author'] ))
	{
		return $last['author'];
	}
	else
	{
		return NULL;
	}
}

function svn_last( $path, $username, $password )
{
	// deprecated -- replaced with SvnServer object method(s)
	
	$last = array( );
	
	$svnInfo = svn_info( $path, $username, $password );
	$logXml = svn_logXML( $path, $username, $password );
	
	if ($svnInfo !== FALSE)
	{
		foreach ($svnInfo as $line)
		{
			if (preg_match( '/^last\s+changed\s+\b([^:]+)\b\s*:\s*(.*?)$/i', $line, $matches ))
			{
				$last[strtolower( $matches[1] )] = $matches[2];
			}
		}
	}
	
	if ($log !== FALSE)
	{
		$xmlDoc = new DOMDocument( );
		$xmlDoc->loadXML( $logXml );
		
		$logEntries = $xmlDoc->getElementsByTagName( 'logentry' );
		
		if ($logEntry = $logEntries->item( 0 ))
		{
			$msgNode = $logEntry->getElementsByTagName( 'msg' )->item( 0 );		
			$msg = $msgNode->nodeValue;
			
			$last['msg'] = $msg;
		}
	}
	
	if (sizeof( $last ))
	{
		return $last;
	}
	else
	{
		return FALSE;
	}
}

function svn_info( $repoPath, $svnUsername, $svnPassword, $svnPath )
{
	// deprecated -- replaced with SvnServer object method(s)
	
	exec( 'whoami', $localUsername );
	$localUsername = reset( $localUsername );
	
	$command = sprintf( 'HOME=/home/%s;export HOME;%ssvn info %s --username %s --password %s 2>&1', $localUsername, trailingSlash( $svnPath ), $repoPath, $svnUsername, $svnPassword );	
	exec( $command, $output, $success );
	
	if ($success == 0)
	{
		return arrayTrim( $output );
	}
	else
	{
		throw new Exception( 'svn_info: failed: '.implode( '--', $output ));
	}
}

?>
Return current item: SvnDashboard