3 * Copyright (C) 2003-2004 Polytechnique.org
4 * http://opensource.polytechnique.org/
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 require_once 'VCS/Spool.php';
24 require_once 'System.php';
26 /** This class handles Diogenes RCS operations.
28 class Diogenes_VCS_RCS
extends Diogenes_VCS_Spool
{
29 /** Absolute directory location for the barrel's RCS files. */
38 * @param login the current user's login
39 * @param init should create this module?
41 function Diogenes_VCS_RCS(&$caller,$alias,$login,$init = false
) {
43 $this->Diogenes_VCS_Spool($caller,$alias);
44 $this->rcsdir
= "{$globals->rcsroot}/$alias";
45 $this->login
= $login;
47 // if we were asked to, created directories
49 if (!is_dir($this->rcsdir
))
50 mkdir($this->rcsdir
, 0700);
51 if (!is_dir($this->datadir
))
52 mkdir($this->datadir
, 0700);
55 // check RCS directory
56 if (!is_dir($this->rcsdir
) ||
!is_writable($this->rcsdir
))
57 $this->kill("'{$this->rcsdir}' is not a writable directory");
59 // check spool directory
60 if (!is_dir($this->datadir
) ||
!is_writable($this->datadir
))
61 $this->kill("'{$this->datadir}' is not a writable directory");
65 /** Return the path of an RCS "item" (file or directory).
67 * @param parent parent directory (optional)
68 * @param entry the item
70 function rcsPath($parent="",$entry="") {
71 $this->checkPath($parent,$entry);
72 return $this->rcsdir
.($parent ?
"/$parent": "") . ($entry ?
"/$entry" : "");
76 /** Return the path of an RCS file (something,v).
78 * @param dir parent directory
79 * @param file the RCS entry
81 function rcsFile($dir,$file) {
82 return $this->rcsPath($dir,$file).",v";
86 /** Check whether a file is registered in RCS.
88 * @param dir parent directory
89 * @param file the RCS entry
91 function checkFile($dir, $file) {
92 return is_file($this->rcsFile($dir, $file));
96 /** Perform sanity check on an RCS directory
97 * and the corresponding checkout in the spool
101 function checkDir($dir) {
102 return is_dir($this->rcsPath($dir))
103 && is_writable($this->rcsPath($dir))
104 && is_dir($this->spoolPath($dir))
105 && is_writable($this->spoolPath($dir));
109 /** Do a checkout of an RCS item to a given location.
111 * @param dir parent directory
112 * @param file the RCS entry
113 * @param rev the revision to check out
114 * @param output the directory to which we want to perform the checkout
116 function checkout($dir,$file,$rev,$output)
118 $this->info("RCS : checkout out $file ($rev)..");
119 $rfile = $this->rcsFile($dir,$file);
120 if ($this->cmdExec("co -q -r".escapeshellarg($rev)." ".escapeshellarg($rfile)." ".escapeshellarg("$output/$file")))
122 $this->info("RCS : Error, checkout failed!");
123 $this->info($this->cmdStatus());
126 return "$output/$file";
130 /** Commit an RCS item. Returns true for succes, false for an error.
132 * @param dir parent directory
133 * @param file the RCS entry
134 * @param content the contents of the new revision
135 * @param message the log message for this revision
137 function commit($dir,$file,$content,$message="")
139 $this->info("RCS : checking in '$file'..");
142 if (!$this->checkDir($dir)) {
144 $this->info("RCS : Error, RCS sanity check for '$dir' failed!");
148 // log commit attempt
149 $this->log("rcs_commit","{$this->alias}:$dir/$file:$message");
151 $sfile = $this->spoolPath($dir,$file);
152 $rfile = $this->rcsFile($dir,$file);
154 // if the RCS file does not exist, create it
155 if (!file_exists($rfile)) {
156 if ($this->cmdExec("echo '' | rcs -q -i ".escapeshellarg($rfile)))
159 $this->info("RCS : Error, could not initialise RCS file '$rfile'!");
160 $this->info($this->cmdStatus());
165 // lock the spool file
166 if ($this->cmdExec("co -q -l ".escapeshellarg($rfile)." ".escapeshellarg($sfile)))
169 $this->info("RCS : Error, could not get RCS lock on file '$file'!");
170 $this->info($this->cmdStatus());
173 if ($fp = fopen($sfile,"w")) {
174 fwrite($fp,$content);
177 if ($this->cmdExec("ci -q -w".escapeshellarg($this->login
). ($message ?
" -m".escapeshellarg($message) : "").
178 " ". escapeshellarg($sfile). " ". escapeshellarg($rfile)))
181 $this->info("RCS : Error, checkin failed!");
182 $this->info($this->cmdStatus());
186 if ($this->cmdExec("co -q ".escapeshellarg($rfile)." ".escapeshellarg($sfile)))
189 $this->info("RCS : Error, checkout after checkin failed!");
190 $this->info($this->cmdStatus());
199 /** Make a copy of an RCS item to a given location.
201 * @param sdir the source directory
202 * @param sfile the source RCS entry
203 * @param ddir the destination directory
204 * @param dfile the destination RCS entry
206 function copy($sdir,$sfile,$ddir,$dfile)
208 $this->info("RCS : copying '$sfile' to '$ddir/$dfile'..");
210 $spath = $this->spoolPath($sdir, $sfile);
211 if (!is_file($spath)) {
212 $this->info("Error: source file '$spath' does not exist!");
215 if (!$this->checkDir($ddir))
217 $this->info("Error: directory '$ddir' does not exist!");
220 if ($this->checkFile($ddir, $dfile))
222 $this->info("Error: file '$dfile' already exists in '$ddir'!");
225 return $this->commit($ddir,$dfile,
226 file_get_contents($spath),
227 "copied from '$ddir/$sfile'");
231 /** Delete an RCS file and its corresponding spool entry.
233 * @param dir parent directory
234 * @param file the RCS entry
236 function del($dir,$file) {
237 $this->info("RCS : deleting '$file'..");
238 $this->log("rcs_delete","{$this->alias}:$dir/$file");
239 @unlink
($this->spoolPath($dir,$file));
240 @unlink
($this->rcsFile($dir,$file));
244 /** Retrieve differences between two version of a file.
246 * @param dir parent directory
247 * @param file the RCS entry
248 * @param r1 the first revision
249 * @param r2 the second revision
251 function diff($dir,$file,$r1,$r2)
253 $rfile = $this->rcsFile($dir,$file);
254 $this->info("RCS : diffing '$file' ($r1 to $r2)..");
255 $this->cmdExec("rcsdiff -r".escapeshellarg($r1). " -r".escapeshellarg($r2)." ".escapeshellarg($rfile));
256 return $this->cmd_output
;
260 /** Converts a Word document to HTML and commits the resulting
267 function importWordFile($dir,$htmlfile,$wordfile)
271 if (!$globals->word_import
) {
272 $this->info("Error : support for word import is disabled!");
276 $func = "importWordFile_{$globals->word_import}";
278 if (!method_exists($this, $func))
280 $this->info("Error : the utility '$globals->word_import' is not supported!");
284 return $this->$func($dir, $htmlfile, $wordfile);
288 /** Converts a Word document to HTML using wvHtml and commits the resulting
295 function importWordFile_wvHtml($dir,$htmlfile,$wordfile)
297 $tmphtmlfile = "importWord.html";
298 if (($tmpdir = System
::mktemp('-d')) == false
) {
299 $this->info("Error : could not create temporary directory!");
303 if ($this->cmdExec("wvHtml --targetdir=".escapeshellarg($tmpdir).
304 " --charset=iso-8859-15 ".
305 escapeshellarg($wordfile)." ".escapeshellarg($tmphtmlfile)))
307 $this->info("Error : wvHtml returned an error!");
308 $this->info($this->cmdStatus());
312 if (!$dh = opendir($tmpdir)) {
313 $this->info("Error : could not find temporary directory '$tmpdir'!");
317 // process the files generated by wvHtml
319 while (($myentry = readdir($dh)) != false
) {
320 if (is_file($myfile = "$tmpdir/$myentry")) {
321 if ($myentry == $tmphtmlfile) {
323 $this->commit($dir,$htmlfile,
324 $this->importHtmlString(file_get_contents($myfile)),
328 $this->commit($dir,$myentry,file_get_contents($myfile),
339 /** Returns raw log entries for an RCS item.
341 * @param dir parent directory
342 * @param file the RCS entry
344 function logEntries($dir,$file) {
345 $rfile = $this->rcsFile($dir,$file);
346 $this->cmdExec("rlog ".escapeshellarg($rfile));
347 return $this->cmd_output
;
351 /** Parse the log entries for an RCS item into an array.
353 * @param dir parent directory
354 * @param file the RCS entry
356 function logParse($dir,$file)
358 // get the log, drop last 2 lines
359 $lines = $this->logEntries($dir,$file);
363 // split into revision, drop first block
364 $revs = split("----------------------------\n", implode("\n",$lines));
367 // parse info about the revisions
369 foreach ($revs as $rev) {
371 $lines = explode("\n",$rev);
372 preg_match("/^revision (.+)$/",array_shift($lines),$res);
373 $myrev['rev'] = $res[1];
374 preg_match("/^date: ([^;]+); author: ([^;]+); .*$/",array_shift($lines),$res);
375 $myrev['date'] = $res[1];
376 $myrev['author'] = $res[2];
377 $myrev['log'] = implode("\n",$lines);
378 array_push($revinfo,$myrev);
384 /** Move an RCS item to a given location.
386 * @param sdir the source directory
387 * @param sfile the source RCS entry
388 * @param ddir the destination directory
389 * @param dfile the destination RCS entry
391 function move($sdir,$sfile,$ddir,$dfile)
393 $this->info("RCS : moving '$sfile' to '$ddir/$dfile'..");
396 // check source files
397 $spath = $this->spoolPath($sdir, $sfile);
398 $srpath = $this->rcsFile($sdir, $sfile);
400 if (!is_file($spath)) {
401 $this->info("Error: source file '$spath' does not exist!");
405 if (!is_file($srpath)) {
406 $this->info("Error: source RCS file '$srpath' does not exist!");
411 $dpath = $this->spoolPath($ddir, $dfile);
412 $drpath = $this->rcsFile($ddir, $dfile);
414 if (!$this->checkDir($ddir))
416 $this->info("Error: directory '$ddir' does not exist!");
420 if (file_exists($dpath)) {
421 $this->info("Error: file '$dfile' already exists in '$ddir'!");
425 if (file_exists($drpath)) {
426 $this->info("Error: file '".basename($drpath)."' already exists in '$ddir'!");
430 if (!rename($spath, $dpath))
432 $this->info("Error: failed to move '".basename($spath)."' to '".basename($dpath)."' in '$ddir'!");
436 if (!rename($srpath, $drpath))
438 $this->info("Error: failed to move '".basename($srpath)."' to '".basename($drpath)."' in '$ddir'!");
443 //$this->log("rcs_move","{$this->alias}:$dir/$file");
449 /** Add a new RCS-managed directory.
451 * @param parent the parent directory
452 * @param dir the directory to add
454 function newdir($parent,$dir)
456 @mkdir
($this->rcsPath($parent,$dir),0700);
457 @mkdir
($this->spoolPath($parent,$dir),0700);
461 /** Retrieve differences between two version of a file and prepare for output.
463 * @param dir parent directory
464 * @param file the RCS entry
465 * @param r1 the first revision
466 * @param r2 the second revision
468 function dispDiff($dir,$file,$r1,$r2)
470 $lns = "[0-9]+|[0-9]+,[0-9]+";
472 // get diff, strip any leading comments
473 $lines = $this->diff($dir,$file,$r1,$r2);
475 while (!preg_match("/^($lns)([acd])($lns)/",$line))
476 $line = array_shift($lines);
477 array_unshift($lines,$line);
478 $raw = implode("\n",$lines);
480 $blocks = preg_split("/($lns)([acd])($lns)\n/",$raw,-1,PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY
);
483 $lold = array_shift($blocks);
486 $type = array_shift($blocks);
487 $lnew = array_shift($blocks);
488 $diff = array_shift($blocks);
492 list($a,$b) = split("---\n",$diff,2);
503 array_push($out,array($lold,$type,$lnew,$a,$b));
504 $lold = array_shift($blocks);
506 //array_push($out,array($type,$a,$b));
511 /** Return an RCS-managed directory.
513 * @param dir the directory
514 * @param loc the visible location for the directory
515 * @param canedit can we edit files in this directory?
517 function dispDir($dir,$loc,$canedit) {
520 if ($pdir = @opendir
($this->rcsPath($dir))) {
521 while ( ($file = readdir($pdir)) !== false
) {
522 if ( ($file != ".") && ($file != "..") )
524 $entry = $this->dispEntry($dir,$loc,$file,$canedit);
526 array_push($entries, $entry);
535 /** Returns an RCS "item" (file or directory).
537 * @param dir parent directory
538 * @param loc visible location for parent directory
539 * @param file the RCS "item"
540 * @param canedit can we edit files this entry?
542 function dispEntry($dir,$loc,$file,$canedit) {
545 $view = $edit = $del = $size = $rev = "";
547 $myitem = $this->rcsPath($dir,$file);
549 // check the RCS entry exists
550 if (!file_exists($myitem)) {
551 $this->info("RCS entry '$myitem' does not exist!");
557 // this is a directory, this should not happen!
558 $this->info("Unexpected directory in RCS, skipping : '$myitem'");
561 else if (substr($file,-2) == ",v")
563 // this is an RCS file
564 $file = substr($file,0,-2);
566 // check we have a working copy of this item
567 $spoolitem = $this->spoolPath($dir,$file);
568 if (!is_file($spoolitem))
570 $this->info("Could not find working copy '$spoolitem'!");
574 $size = $this->dispSize(filesize($spoolitem));
575 $icon = $globals->icons
->get_mime_icon($spoolitem);
579 $myrev = array_shift($tmparr = $this->logParse($dir,$file));
584 "rev" => array($myrev['rev'],$rev),
585 "date" => $myrev['date'],
586 "author" => $myrev['author'],
593 $this->info("Unknown RCS entry type : '$myitem'");
600 /** Format a file size for display.
602 * @param size the size, in bytes
604 function dispSize($size)
608 else if ($size < 1000000)
609 return floor($size/1000)." kB";
611 return floor($size/1000000)." MB";