$revision_array = array(3099, 3339, 2573,3351); /* svn的版本号 */
$svnPeer = new svnPeer();
$filelist = $svnPeer->_get_file_list($revision_array);
if (!empty($filelist))
{
$lbv_export = $svnPeer->_svn_export_list($filelist, 'trunk889');
if (true === $lbv_export)
{
echo '导出成功';
}
else
{
echo '导出失败';
}
}
else
{
echo '获取文件列表失败';
}
/**
* php操作svn类,全部利用php内置的svn函数
*
* @author wengxianhu
* @date 2013-08-05
*/
class svnPeer
{
/* svn用户名 */
public $svn_user = 'wengxianhu';
/* svn密码 */
public $svn_password = 'wxh025';
/* 来源路径 */
public $source_path = '/var/www/trunk/';
/* 目标路径 */
public $dest_path = '/var/www/';
/**
* 构造函数
*
* @author wengxianhu
* @date 2013-08-05
* @return void
*/
public function __construct ()
{
$this->_svn_connect();
}
/**
* 配置SVN使用默认的用户名和密码
*
* @author wengxianhu
* @date 2013-08-05
* @return void
*/
public function _svn_connect ()
{
svn_auth_set_parameter(SVN_AUTH_PARAM_DEFAULT_USERNAME, $this->svn_user);
svn_auth_set_parameter(SVN_AUTH_PARAM_DEFAULT_PASSWORD, $this->svn_password);
}
/**
* 根据svn版本号获取所有的文件路径
*
* @author wengxianhu
* @date 2013-08-05
* @param array $revision_array 版本号列表
* @return array
*/
public function _get_file_list ($revision_array = array())
{
if (!empty($revision_array))
{
$filelist = array();
$log_list = array();
rsort($revision_array, SORT_NUMERIC);
foreach ($revision_array as $_k=>$_v)
{
$log_list = @svn_log($this->source_path, $_v, $_v);
if (false === $log_list)
{
return false;
}
else
{
$log_list = current($log_list);
foreach ($log_list['paths'] as $s_k=>$s_v)
{
$s_v['path'] = preg_replace('/^\/[^\/]+\/(.*)$/i', '$1', $s_v['path']);
$filetmp = $s_v['path'];
if (is_file($this->source_path . $s_v['path']))
{
if (false === $this->multidimensional_search($filelist, array('filepath'=>$s_v['path'])))
{
$filelist[] = array(
'revision_no'=> $log_list['rev'],
'filepath'=> $s_v['path']
);
}
}
}
}
}
return $filelist;
}
}
/**
* 对多维数组进行搜索
*
* @author wengxianhu
* @date 2013-08-05
* @param array $parents 被搜索数组
* @param array $searched 搜索数组
* @return boolean
*/
public function multidimensional_search ($parents = array(), $searched = array())
{
if (empty($searched) || empty($parents))
{
return false;
}
foreach ($parents as $key => $value)
{
$exists = true;
foreach ($searched as $skey => $svalue) {
$exists = ($exists && IsSet($parents[$key][$skey]) && $parents[$key][$skey] == $svalue);
}
if ($exists)
{
return $key;
}
}
return false;
}
/**
* 根据svn版本号导出相应的文件
*
* @author wengxianhu
* @date 2013-08-05
* @param array $file_array 文件路径名
* @param string $package_name 包名
* @return boolean 成功为true,失败为false
*/
public function _svn_export_list ($file_array = array(), $package_name = '')
{
$info = true;
$this->dest_path = $this->dest_path . $package_name;
if (file_exists($this->dest_path))
{
$this->delDirAndFile($this->dest_path);
}
foreach ($file_array as $_k=>$_v)
{
$source_files = $this->source_path . $_v['filepath'];
$dest_files = $this->dest_path . '/' . $_v['filepath'];
$revision_no = (int)$_v['revision_no'];
$this->_mkdirm(dirname($dest_files));
$lbv_export = @svn_export($source_files, $dest_files, false, $revision_no);
if (false === $lbv_export)
{
$info = false;
break;
}
}
return $info;
}
/**
* 创建文件夹
*
* @author wengxianhu
* @date 2013-08-05
* string $path 文件路径(不包括文件名)
* return void
*/
public function _mkdirm ($path)
{
if (!file_exists($path))
{
$this->_mkdirm(dirname($path));
mkdir($path, 0755);
}
}
/**
* 循环删除目录和文件函数
*
* @author wengxianhu
* @date 2013-08-15
* @param string $dirName 目录路径
* return array
*/
public function delDirAndFile($dirName)
{
if ( $handle = opendir( "$dirName" ) )
{
while ( false !== ( $item = readdir( $handle ) ) )
{
if ( $item != "." && $item != ".." )
{
if ( is_dir( "$dirName/$item" ) )
{
$this->delDirAndFile( "$dirName/$item" );
}
else
{
unlink( "$dirName/$item" );
}
}
}
closedir( $handle );
rmdir( $dirName );
}
}
}