The svn class encapsulated by PHP uses the built in svn function to implement the example of exporting related files according to the svn version number

  • 2021-10-24 19:05:50
  • OfStack

In this paper, the svn class encapsulated by PHP uses the built-in svn function to export related files according to svn version number. Share it for your reference, as follows:


<?php
$revision_array = array(3099, 3339, 2573,3351); /* svn Version number of  */
$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 ' Export succeeded ';
  }
  else
  {
    echo ' Export failed ';
  }
}
else
{
  echo ' Failed to get file list ';
}
/**
 * php Operation svn Class, making full use of php Built-in svn Function 
 *
 * @author wengxianhu
 * @date 2013-08-05
 */
class svnPeer
{
  /* svn User name  */
  public $svn_user = 'wengxianhu';
  /* svn Password  */
  public $svn_password = 'wxh025';
  /*  Source path  */
  public $source_path = '/var/www/trunk/';
  /*  Target path  */
  public $dest_path = '/var/www/';
  /**
   *  Constructor 
   *
   * @author wengxianhu
   * @date 2013-08-05
   * @return void
   */
  public function __construct ()
  {
    $this->_svn_connect();
  }
  /**
   *  Configure SVN Use the default username and password 
   *
   * @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);
  }
  /**
   *  According to svn Version number gets all file paths 
   *
   * @author wengxianhu
   * @date 2013-08-05
   * @param array $revision_array  Version number list 
   * @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;
    }
  }
  /**
   *  Search for multidimensional arrays 
   *
   * @author wengxianhu
   * @date 2013-08-05
   * @param array $parents  Searched array 
   * @param array $searched  Search array 
   * @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;
  }
  /**
   *  According to svn Export the corresponding file with the version number 
   *
   * @author wengxianhu
   * @date 2013-08-05
   * @param array $file_array  File pathname 
   * @param string $package_name  Package name 
   * @return boolean  Success is true The failure is 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;
  }
  /**
   *  Create a folder 
   *
   * @author wengxianhu
   * @date 2013-08-05
   * string $path  File path (excluding file name) 
   * return void
   */
  public function _mkdirm ($path)
  {
    if (!file_exists($path))
    {
      $this->_mkdirm(dirname($path));
      mkdir($path, 0755);
    }
  }
  /**
   *  Loop to delete directories and files function 
   *
   * @author wengxianhu
   * @date 2013-08-15
   * @param string $dirName  Directory path 
   * 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 );
    }
  }
}

More readers interested in PHP can check the topics of this site: "Summary of PHP Directory Operation Skills", "Summary of php File Operation", "Summary of PHP Common Traversal Algorithms and Skills", "Tutorial on PHP Data Structure and Algorithms", "Summary of php Programming Algorithms", "Encyclopedia of PHP Array (Array) Operation Skills", "Summary of php String (string) Usage" and "Summary of php Common Database Operation Skills"

I hope this article is helpful to everyone's PHP programming.


Related articles: