利用php内置的svn函数实现的根据svn版本号导出相关文件的功能

本文介绍了一个使用PHP操作SVN的实际案例,展示了如何通过PHP脚本获取特定版本的文件列表并将其导出到指定目录。文章详细解释了如何连接SVN、获取文件列表及导出文件的具体步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<?php

$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 );
        }
    }	
	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值