<?php
/**
* SVN external Command class
*
* @author Rubekid
*
* @todo comment need addslashes for SVN commit
*
*/
Class Svnutils {
/**
*
* SVN account
*/
Const SVN_USERNAME = "Robot";
/**
* SVN password
*/
Const Svn_password = "robot2013";
/**
* Configuration file directory (optionally specify a temporary directory to resolve Svn:warning:Can ' t open file '/root/.subversion/servers ': Permission denied)
*/
Const SVN_CONFIG_DIR = "/var/tmp/";
/**
* SVN list
*
* @param $repository string
* @return Boolean
*
*/
public static function ls ($repository) {
$command = "sudo svn ls". $repository;
$output = Self::runcmd ($command);
$output = Implode ("<br/>", $output);
if (Strpos ($output, ' non-existent in that revision ')) {
return false;
}
Return "<br/>". $command. "<br/>". $output;
}
/**
* SVN copy
*
* @param $src string
* @param $DST string
* @param $comment string
* @return Boolean
*
*/
public static function copy ($SRC, $DST, $comment) {
$command = "sudo svn cp $src $DST-M ' $comment";
$output = Self::runcmd ($command);
$output = Implode ("<br/>", $output);
if (Strpos ($output, ' committed revision ')) {
return true;
}
Return "<br/>". $command. "<br/>". $output;
}
/**
* SVN Delete
*
* @param $url string
* @param $comment string
* @return Boolean
*
*/
public static function Delete ($url, $comment) {
$command = "sudo svn del $url-M ' $comment '";
$output = Self::runcmd ($command);
$output = Implode (' <br/> ', $output);
if (Strpos ($output, ' committed revision ')) {
return true;
}
Return "<br/>". $command. "<br/>". $output;
}
/**
* SVN move
*
* @param $src string
* @param $DST string
* @param $comment string
* @return Boolean
*/
public static function Move ($SRC, $DST, $comment) {
$command = "sudo svn mv $src $dst-M ' $comment";
$output = Self::runcmd ($command);
$output = Implode (' <br/> ', $output);
if (Strpos ($output, ' committed revision ')) {
return true;
}
Return "<br/>". $command. "<br/>". $output;
}
/**
* SVN mkdir
*
* @param $url string
* @param $comment string
* @return Boolean
*/
public static function mkdir ($url, $comment) {
$command = "sudo svn mkdir $url-M ' $comment '";
$output = Self::runcmd ($command);
$output = Implode (' <br/> ', $output);
if (Strpos ($output, ' committed revision ')) {
return true;
}
Return "<br/>". $command. "<br/>". $output;
}
/**
* SVN diff
* @param $pathA string
* @param $pathB string
* @return String
*/
public static function diff ($pathA, $pathB) {
$output = Self::runcmd ("Sudo svn diff $pathA $pathB");
Return implode (' <br/> ', $output);
}
/**
* SVN Checkout
* @param $url string
* @param $dir string
* @return Boolean
*/
public static function Checkout ($url, $dir) {
$command = "CD $dir && sudo svn co $url";
$output = Self::runcmd ($command);
$output = Implode (' <br/> ', $output);
if (Strstr ($output, ' Checked out Revision ')) {
return true;
}
Return "<br/>". $command. "<br/>". $output;
}
/**
* SVN update
* @param $path string
*/
public static function Update ($path) {
$command = "CD $path && sudo svn up";
$output = Self::runcmd ($command);
$output = Implode (' <br/> ', $output);
Preg_match_all ("/[0-9]+/", $output, $ret);
if (! $ret [0] [0]) {
Return "<br/>". $command. "<br/>". $output;
}
return $ret [0] [0];
}
/**
* SVN merge
*
* @param $revision string
* @param $url string
* @param $dir string
*
* @return Boolean
*/
public static function merge ($revision, $url, $dir) {
$command = "CD $dir && sudo svn merge-r1: $revision $url";
$output = Implode (' <br/> ', Self::runcmd ($command));
if (Strstr ($output, ' Text conflicts ')) {
Return ' Command: '. $command. ' <br/> '. $output;
}
return true;
}
/**
* SVN commit
*
* @param $dir string
* @param $comment string
*
* @return Boolean
*/
public static function Commit ($dir, $comment) {
$command = "CD $dir && sudo svn commit-m ' $comment '";
$output = Implode (' <br/> ', Self::runcmd ($command));
if (Strpos ($output, ' committed revision ') | | empty ($output)) {
return true;
}
return $output;
}
/**
* SVN status (output state of files and directories in WC)
*
* @param $dir string
*/
public static function GetStatus ($dir) {
$command = "CD $dir && sudo svn st";
Return Self::runcmd ($command);
}
/**
* SVN conflict
*
* @param $dir string
* @return Boolean
*/
public static function Hasconflict ($dir) {
$output = Self::getstatus ($dir);
foreach ($output as $line) {
if (substr ($line), 0, 1) = = ' C ' | | (Substr (Trim ($line), 0, 1) = = '! ')) {
return true;
}
}
return false;
}
/**
* SVN log
*
* @param $path string
* @return String
*
*/
public static function GetLog ($path) {
$command = "sudo svn log $path--xml";
$output = Self::runcmd ($command);
Return implode (", $output);
}
/**
* SVN info
* @param $path string
*/
public static function Getpathrevision ($path) {
$command = "sudo svn info $path--xml";
$output = Self::runcmd ($command);
$string = Implode (", $output);
$xml = new SimpleXMLElement ($string);
foreach ($xml->entry [0]->attributes () as $key => $value) {
if ($key = = ' revision ') {
return $value;
}
}
}
/**
* Get the latest version number
* @param $path string
*/
public static function Getheadrevision ($path) {
$command = "CD $path && sudo svn up";
$output = Self::runcmd ($command);
$output = Implode (' <br/> ', $output);
Preg_match_all ("/[0-9]+/", $output, $ret);
if (! $ret [0] [0]) {
Return "<br/>". $command. "<br/>". $output;
}
return $ret [0] [0];
}
/**
* Get the earliest version number of a file
*
* @param $filePath string
*
*/
public static function Getfilefirstversion ($filePath) {
$command = "sudo svn log {$filePath}";
$output = Self::runcmd ($command, "|grep-i ^r[0-9]* |awk ' {print} '");
if (empty ($output)) {
return false;
}
Return Str_replace ("R", ", $output [Count ($output)-1]);
}
/**
* Get a list of file information modified between two versions
*
* @param $fromVersion int
* @param $headRevision int
* @param $ $path string
*
* @return Array
*/
public static function Getchangedfiles ($path, $fromVersion, $headRevision) {
$files = Array ();
$pipe = "|grep-i ^index:|awk-f: ' {print $} '";
$command = "svn diff-r {$fromVersion}:{$headRevision} $path";
$output = Self::runcmd ($command, $pipe);
$files = Array_merge ($files, $output);
$command = "svn diff-r {$headRevision}:{$fromVersion} $path"; File deletion available in reverse contrast
$output = Self::runcmd ($command, $pipe);
$files = Array_merge ($files, $output);
Return Array_unique ($files);
}
/**
* Get the contents of a file modified between two versions
*
* @param $filePath string
* @param $fromVersion int
* @param $headRevision int
*
* @return Array
*/
public static function Getchangedinfo ($filePath, $fromVersion, $headRevision) {
$command = "sudo svn diff-r {$fromVersion}:{$headRevision} $filePath";
$output = Self::runcmd ($command);
return $output;
}
/**
* View the contents of the file
*
* @param $filePath string
* @param $version int
*
* @return Array
*/
public static function Getfilecontent ($filePath, $version) {
$command = "sudo svn cat-r {$version} $filePath";
$output = Self::runcmd ($command);
return $output;
}
/**
* Run a cmd and return result
* @param $command string
* @param $pipe string (you can increase the pipeline to filter the returned data)
* @return Array
*/
protected static function Runcmd ($command, $pipe = "") {
$authCommand = '--username '. Self::svn_username. '--password '. Self::svn_password. '--no-auth-cache--non-interactive--config-dir '. Self::svn_config_dir. '. Subversion ';
EXEC ($command. $authCommand. "2>&1". $pipe, $output);
return $output;
}
}