Menu

#2 Add a custom comment on update

open
nobody
None
5
2004-03-16
2004-03-16
Tempus
No

Hello !

Nice application ! But I've change some part of code
- Change the code to add custom comment on file
update (instead of "foo revision").
- Convert each String to Base64 in Client <-> Server
messages (to avoid XML Parsing error if characters like,
ë, ä, ...)
- Write the revision comments when display the history
of a file.

Hope that will help !

My changes:
On server.php:
New update, history and _getInodeHistory functions:
$update_sig = array(array($XML_RPC_Boolean,
$XML_RPC_Base64, $XML_RPC_Int, $XML_RPC_Base64));
$update_doc = 'Create a new version of an element;
accepts as input a struct with the following
format: "bytes" => "content of the file","inode" => "id of
the inode to update","comment" => "comment of the
revision"';

/**
* update()
*
* @param $params
* @return
*/
function update ($params)
{
global $XML_RPC_Boolean, $XML_RPC_erruser,
$log; // import user errcode value
global $XML_RPC_Boolean, $XML_RPC_Base64,
$XML_RPC_Int, $XML_RPC_String;

if (ENABLE_LOG) $log->log("method " .
__FUNCTION__ . " called", LOG_INFO);

$fcontent = $params->getParam(0);
$finode = $params->getParam(1);
$fcomment = $params->getParam(2);
// set a default comment if none given
$fcommentValue = Base64_Decode($fcomment-
>scalarval());
if ($fcommentValue == '')
$fcommentValue = 'foo revision';

if (ENABLE_LOG) $log->log("finode:" . $finode-
>scalarval(), LOG_INFO);
// check if the inode exists & is a file
if (!_inodeIsFile($finode->scalarval())) {
return new XML_RPC_Response(0,
$XML_RPC_erruser + 1, // user error 1
"File #" . $finode->scalarval() . " is not
versioned!");
} else {
// save file content on fs
$tmpfname = tempnam (STORAGE_DIR, "inode_");
$fp = fopen($tmpfname, "wb");
fwrite($fp, $fcontent->scalarval());
fclose($fp);
// retrieve info for the latest version of file
$currentVer = _getInodeInfo($finode->scalarval());
// if (ENABLE_LOG) $log->log("_getInodeInfo:" .
print_r($inodeObj, true), LOG_INFO);
// avoid the creation of multiple versions of the
same file
if (file_get_contents($currentVer[4]) == $fcontent-
>scalarval())
return new XML_RPC_Response(0,
$XML_RPC_erruser + 1, // user error 1
"The proposed file is identical to version " .
$currentVer[3] . " yet stored into repository");

$nextVer = $currentVer[3] + 0.01;
// create a new version for the file
//
($inode,$ver,$storagename,$filesize,$owner,$changelog)
$sql = _versionCreate($finode->scalarval(),
$nextVer, $tmpfname, filesize($tmpfname), $_SERVER
['PHP_AUTH_USER'], $fcommentValue);
if (ENABLE_LOG) $log->log("_versionCreate:" .
$sql , LOG_INFO);
// and update the inode table to reflect the actual
version
$sql = _updateInode($finode->scalarval(),
$nextVer);
if (ENABLE_LOG) $log->log("_updateInode:" . $sql ,
LOG_INFO);

return new XML_RPC_Response(new
XML_RPC_Value(true, $XML_RPC_Boolean));
}
}

$history_sig = array(array($XML_RPC_Array,
$XML_RPC_Int));
$history_doc = 'Return a list of the avaible versions for
the given inode ID';
function history ($params)
{
global $XML_RPC_erruser, $log; // import user errcode
value
global $XML_RPC_DateTime, $XML_RPC_Struct,
$XML_RPC_Array, $XML_RPC_Int, $XML_RPC_Double,
$XML_RPC_String; // used RPC types

if (ENABLE_LOG) $log->log("method " .
__FUNCTION__ . " called", LOG_INFO);

$inode = $params->getParam(0);
$inodeID = $inode->scalarval();

if (ENABLE_LOG) $log->log("inode:" . $inodeID,
LOG_INFO);

if (!_inodeIsFile($inodeID)) {
return new XML_RPC_Response(0,
$XML_RPC_erruser + 1, // user error 1
"File #" . $inodeID . " does not exists!");
}

$ret = _getInodeHistory($inodeID);

$out = array();

foreach ($ret as $ver => $verdata) {
// if (ENABLE_LOG) $log->log(print_r
($inodeObj,true), LOG_INFO);
$out[] = &new XML_RPC_Value(array("ver" => new
XML_RPC_Value($ver, $XML_RPC_Double),
"owner" => new XML_RPC_Value
(base64_encode($verdata[0]), $XML_RPC_String),
"changetime" => new XML_RPC_Value
($verdata[1], $XML_RPC_DateTime),
"size" => new XML_RPC_Value($verdata[2],
$XML_RPC_Int),
"changelog" => new XML_RPC_Value
(base64_encode($verdata[3]), $XML_RPC_Base64)),
$XML_RPC_Struct);
}

$response = new XML_RPC_Response(new
XML_RPC_Value($out, $XML_RPC_Array));
//if (ENABLE_LOG) $log->log(print_r($response, true),
LOG_INFO);
return $response;
}

function _getInodeHistory($id)
{
global $db;
$sql = "SELECT ver, owner, changetime, filesize,
changelog ";
$sql .= "FROM versions ";
$sql .= "WHERE inode = " . $id;
$sql .= " ORDER BY ver DESC";

return $db->getAssoc($sql);
}

On libphpvcsclient.php:
New update function:
function update($inode, $fname, $fcomment)
{
global $XML_RPC_Boolean , $XML_RPC_Int,
$XML_RPC_Base64;

if (!is_numeric($inode)) {
$this->_faultCode = 902 ;
$this->_faultString = "Invalid inode id(" .
$inode . ")";
return false;
}

if (!file_exists($fname)) {
$this->_faultCode = 901 ;
$this->_faultString = "File " . $fname . " does
not exists";
return false;
}

$content = file_get_contents($fname);

$input = array(new XML_RPC_Value($content,
$XML_RPC_Base64),
new XML_RPC_Value($inode, $XML_RPC_Int),
new XML_RPC_Value(Base64_Encode
($fcomment), $XML_RPC_Base64));

$f = new XML_RPC_Message('phpVcs.update',
$input);
$r = $this->_XML_RPC_Client->send($f);
if (!is_object($r)) {
$this->_faultCode = 900;
$this->_faultString = "XML-RPC server is not
avaible";
return false;
}
$v = $r->value();
if (!$r->faultCode()) {
return true;
} else {
$this->_faultCode = $r->faultCode() ;
$this->_faultString = $r->faultString();
return false;
}
}

On vcs.php (cli, not gtk !) :
New update and history functions:
function update($cmd)
{
global $client;
$cmd_list = explode (" ", $cmd);
$fpath = $cmd_list[1];
$fpath = intval($fpath);

// Build comment
$fcomment_count = strlen($cmd) - strlen($cmd_list
[0] . ' ') - strlen($cmd_list[1] . ' ');
if ($fcomment_count > 0) {
$fcomment = substr($cmd, strlen($cmd) -
$fcomment_count, $fcomment_count);
$destName = _getNameByID($fpath);

if ($destName === false) {
print "Unknown file with id #" . $fpath;
} else {

$res = $client->update
($fpath,$destName,$fcomment);

if ($res === false)
print("Fault code: " . $client->faultCode() . "
Reason '" . $client->faultString() . "'");
else
print "Element " . $destName . " succesfully
updated.\n";
}
}
else
print "Cannot update file, no comment given!";
}

function history($cmd)
{
global $XML_RPC_Int; // used RPC types

list ($foo, $fpath) = explode (" ", $cmd);
$id = (int) ($fpath);

$input = array(new XML_RPC_Value($id,
$XML_RPC_Int));

$f = new XML_RPC_Message('phpVcs.history', $input);
$c = new XML_RPC_Client($GLOBALS['vcs_url'],
$GLOBALS['vcs_host'], $GLOBALS['vcs_port'], $GLOBALS
['proxy_host'], $GLOBALS['proxy_port']);
$c->setCredentials($GLOBALS['vcs_login'], $GLOBALS
['vcs_pass']);

$r = $c->send($f);
$v = $r->value();
if (!$r->faultCode()) {
$format = "%.2f %s %s %s %s\n";

foreach ($v->scalarval() as $verdata) {
$iver = $verdata->structmem("ver");
$iowner = $verdata->structmem("owner");
$ichtime = $verdata->structmem("changetime");
$isize = $verdata->structmem("size");
$ichlog = $verdata->structmem("changelog");

printf ($format, $iver->scalarval(),
base64_decode($iowner->scalarval()),
_fe_dbdate_to_human($ichtime->scalarval()), $isize-
>scalarval(), base64_decode($ichlog->scalarval()));
}
} else {
print "Fault: ";
print "Code: " . $r->faultCode() . " Reason '" . $r-
>faultString();
}
}

Discussion


Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.