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 'diogenes.spool.inc.php';
23 require_once 'diogenes.icons.inc.php';
25 require_once 'System.php';
27 /** This class handles Diogenes RCS operations.
29 class DiogenesRcs
extends DiogenesSpool
{
30 /** Absolute directory location for the barrel's RCS files. */
39 * @param login the current user's login
40 * @param init should create this module?
42 function DiogenesRcs(&$caller,$alias,$login,$init = false
) {
44 $this->DiogenesSpool($caller,$alias);
45 $this->rcsdir
= "{$globals->rcsroot}/$alias";
46 $this->login
= $login;
48 // if we were asked to, created directories
50 if (!is_dir($this->rcsdir
))
51 mkdir($this->rcsdir
, 0700);
52 if (!is_dir($this->datadir
))
53 mkdir($this->datadir
, 0700);
56 // check RCS directory
57 if (!is_dir($this->rcsdir
) ||
!is_writable($this->rcsdir
))
58 $this->kill("'{$this->rcsdir}' is not a writable directory");
60 // check spool directory
61 if (!is_dir($this->datadir
) ||
!is_writable($this->datadir
))
62 $this->kill("'{$this->datadir}' is not a writable directory");
66 /** Return the path of an RCS "item" (file or directory).
68 * @param parent parent directory (optional)
69 * @param entry the item
71 function rcsPath($parent="",$entry="") {
72 $this->checkPath($parent,$entry);
73 return $this->rcsdir
.($parent ?
"/$parent": "") . ($entry ?
"/$entry" : "");
77 /** Return the path of an RCS file (something,v).
79 * @param dir parent directory
80 * @param file the RCS entry
82 function rcsFile($dir,$file) {
83 return $this->rcsPath($dir,$file).",v";
87 /** Check whether a file is registered in RCS.
89 * @param dir parent directory
90 * @param file the RCS entry
92 function checkFile($dir, $file) {
93 return is_file($this->rcsFile($dir, $file));
97 /** Perform sanity check on an RCS directory
98 * and the corresponding checkout in the spool
102 function checkDir($dir) {
103 return is_dir($this->rcsPath($dir))
104 && is_writable($this->rcsPath($dir))
105 && is_dir($this->spoolPath($dir))
106 && is_writable($this->spoolPath($dir));
110 /** Do a checkout of an RCS item to a given location.
112 * @param dir parent directory
113 * @param file the RCS entry
114 * @param rev the revision to check out
115 * @param output the directory to which we want to perform the checkout
117 function checkout($dir,$file,$rev,$output)
119 $this->info("RCS : checkout out $file ($rev)..");
120 $rfile = $this->rcsFile($dir,$file);
121 if ($this->cmdExec("co -r".escapeshellarg($rev)." ".escapeshellarg($rfile)." ".escapeshellarg("$output/$file")))
123 $this->info("RCS : Error, checkout failed!");
124 $this->info($this->cmdStatus());
127 return "$output/$file";
131 /** Commit an RCS item. Returns true for succes, false for an error.
133 * @param dir parent directory
134 * @param file the RCS entry
135 * @param content the contents of the new revision
136 * @param message the log message for this revision
138 function commit($dir,$file,$content,$message="")
140 $this->info("RCS : checking in '$file'..");
143 if (!$this->checkDir($dir)) {
145 $this->info("RCS : Error, RCS sanity check for '$dir' failed!");
149 // log commit attempt
150 $this->log("rcs_commit","{$this->alias}:$dir/$file:$message");
152 $sfile = $this->spoolPath($dir,$file);
153 $rfile = $this->rcsFile($dir,$file);
155 // if the RCS file does not exist, create it
156 if (!file_exists($rfile)) {
157 if ($this->cmdExec("rcs -i ".escapeshellarg($rfile)))
160 $this->info("RCS : Error, could not initialise RCS file '$rfile'!");
161 $this->info($this->cmdStatus());
166 // lock the spool file
167 if ($this->cmdExec("co -l ".escapeshellarg($rfile)." ".escapeshellarg($sfile)))
170 $this->info("RCS : Error, could not get RCS lock on file '$file'!");
171 $this->info($this->cmdStatus());
174 if ($fp = fopen($sfile,"w")) {
175 fwrite($fp,$content);
178 if ($this->cmdExec("ci -w".escapeshellarg($this->login
). ($message ?
" -m".escapeshellarg($message) : "").
179 " ". escapeshellarg($sfile). " ". escapeshellarg($rfile)))
182 $this->info("RCS : Error, checkin failed!");
183 $this->info($this->cmdStatus());
187 if ($this->cmdExec("co ".escapeshellarg($rfile)." ".escapeshellarg($sfile)))
190 $this->info("RCS : Error, checkout after checkin failed!");
191 $this->info($this->cmdStatus());
200 /** Make a copy of an RCS item to a given location.
202 * @param sdir the source directory
203 * @param sfile the source RCS entry
204 * @param ddir the destination directory
205 * @param dfile the destination RCS entry
207 function copy($sdir,$sfile,$ddir,$dfile)
209 $this->info("RCS : copying '$sfile' to '$ddir/$dfile'..");
211 $spath = $this->spoolPath($sdir, $sfile);
212 if (!is_file($spath)) {
213 $this->info("Error: source file '$spath' does not exist!");
216 if (!$this->checkDir($ddir))
218 $this->info("Error: directory '$ddir' does not exist!");
221 if ($this->checkFile($ddir, $dfile))
223 $this->info("Error: file '$dfile' already exists in '$ddir'!");
226 return $this->commit($ddir,$dfile,
227 file_get_contents($spath),
228 "copied from '$ddir/$sfile'");
232 /** Delete an RCS file and its corresponding spool entry.
234 * @param dir parent directory
235 * @param file the RCS entry
237 function del($dir,$file) {
238 $this->info("RCS : deleting '$file'..");
239 $this->log("rcs_delete","{$this->alias}:$dir/$file");
240 @unlink
($this->spoolPath($dir,$file));
241 @unlink
($this->rcsFile($dir,$file));
245 /** Retrieve differences between two version of a file.
247 * @param dir parent directory
248 * @param file the RCS entry
249 * @param r1 the first revision
250 * @param r2 the second revision
252 function diff($dir,$file,$r1,$r2)
254 $rfile = $this->rcsFile($dir,$file);
255 $this->info("RCS : diffing '$file' ($r1 to $r2)..");
256 $this->cmdExec("rcsdiff -r".escapeshellarg($r1). " -r".escapeshellarg($r2)." ".escapeshellarg($rfile));
257 return $this->cmd_output
;
261 /** Converts a Word document to HTML and commits the resulting
268 function importWordFile($dir,$htmlfile,$wordfile)
272 if (!$globals->word_import
) {
273 $this->info("Error : support for word import is disabled!");
277 $func = "importWordFile_{$globals->word_import}";
279 if (!method_exists($this, $func))
281 $this->info("Error : the utility '$globals->word_import' is not supported!");
285 return $this->$func($dir, $htmlfile, $wordfile);
289 /** Converts a Word document to HTML using wvHtml and commits the resulting
296 function importWordFile_wvHtml($dir,$htmlfile,$wordfile)
298 $tmphtmlfile = "importWord.html";
299 if (($tmpdir = System
::mktemp('-d')) == false
) {
300 $this->info("Error : could not create temporary directory!");
304 if ($this->cmdExec("wvHtml --targetdir=".escapeshellarg($tmpdir).
305 " --charset=iso-8859-15 ".
306 escapeshellarg($wordfile)." ".escapeshellarg($tmphtmlfile)))
308 $this->info("Error : wvHtml returned an error!");
309 $this->info($this->cmdStatus());
313 if (!$dh = opendir($tmpdir)) {
314 $this->info("Error : could not find temporary directory '$tmpdir'!");
318 // process the files generated by wvHtml
320 while (($myentry = readdir($dh)) != false
) {
321 if (is_file($myfile = "$tmpdir/$myentry")) {
322 if ($myentry == $tmphtmlfile) {
324 $this->commit($dir,$htmlfile,
325 $this->importHtmlString(file_get_contents($myfile)),
329 $this->commit($dir,$myentry,file_get_contents($myfile),
340 /** Returns raw log entries for an RCS item.
342 * @param dir parent directory
343 * @param file the RCS entry
345 function logEntries($dir,$file) {
346 $rfile = $this->rcsFile($dir,$file);
347 $this->cmdExec("rlog ".escapeshellarg($rfile));
348 return $this->cmd_output
;
352 /** Parse the log entries for an RCS item into an array.
354 * @param dir parent directory
355 * @param file the RCS entry
357 function logParse($dir,$file)
359 // get the log, drop last 2 lines
360 $lines = $this->logEntries($dir,$file);
364 // split into revision, drop first block
365 $revs = split("----------------------------\n", implode("\n",$lines));
368 // parse info about the revisions
370 foreach ($revs as $rev) {
372 $lines = explode("\n",$rev);
373 preg_match("/^revision (.+)$/",array_shift($lines),$res);
374 $myrev['rev'] = $res[1];
375 preg_match("/^date: ([^;]+); author: ([^;]+); .*$/",array_shift($lines),$res);
376 $myrev['date'] = $res[1];
377 $myrev['author'] = $res[2];
378 $myrev['log'] = implode("\n",$lines);
379 array_push($revinfo,$myrev);
385 /** Move an RCS item to a given location.
387 * @param sdir the source directory
388 * @param sfile the source RCS entry
389 * @param ddir the destination directory
390 * @param dfile the destination RCS entry
392 function move($sdir,$sfile,$ddir,$dfile)
394 $this->info("RCS : moving '$sfile' to '$ddir/$dfile'..");
397 // check source files
398 $spath = $this->spoolPath($sdir, $sfile);
399 $srpath = $this->rcsFile($sdir, $sfile);
401 if (!is_file($spath)) {
402 $this->info("Error: source file '$spath' does not exist!");
406 if (!is_file($srpath)) {
407 $this->info("Error: source RCS file '$srpath' does not exist!");
412 $dpath = $this->spoolPath($ddir, $dfile);
413 $drpath = $this->rcsFile($ddir, $dfile);
415 if (!$this->checkDir($ddir))
417 $this->info("Error: directory '$ddir' does not exist!");
421 if (file_exists($dpath)) {
422 $this->info("Error: file '$dfile' already exists in '$ddir'!");
426 if (file_exists($drpath)) {
427 $this->info("Error: file '".basename($drpath)."' already exists in '$ddir'!");
431 if (!rename($spath, $dpath))
433 $this->info("Error: failed to move '".basename($spath)."' to '".basename($dpath)."' in '$ddir'!");
437 if (!rename($srpath, $drpath))
439 $this->info("Error: failed to move '".basename($srpath)."' to '".basename($drpath)."' in '$ddir'!");
444 //$this->log("rcs_move","{$this->alias}:$dir/$file");
450 /** Add a new RCS-managed directory.
452 * @param parent the parent directory
453 * @param dir the directory to add
455 function newdir($parent,$dir)
457 @mkdir
($this->rcsPath($parent,$dir),0700);
458 @mkdir
($this->spoolPath($parent,$dir),0700);
462 /** Retrieve differences between two version of a file and prepare for output.
464 * @param dir parent directory
465 * @param file the RCS entry
466 * @param r1 the first revision
467 * @param r2 the second revision
469 function dispDiff($dir,$file,$r1,$r2)
471 $lns = "[0-9]+|[0-9]+,[0-9]+";
473 // get diff, strip any leading comments
474 $lines = $this->diff($dir,$file,$r1,$r2);
476 while (!preg_match("/^($lns)([acd])($lns)/",$line))
477 $line = array_shift($lines);
478 array_unshift($lines,$line);
479 $raw = implode("\n",$lines);
481 $blocks = preg_split("/($lns)([acd])($lns)\n/",$raw,-1,PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY
);
484 $lold = array_shift($blocks);
487 $type = array_shift($blocks);
488 $lnew = array_shift($blocks);
489 $diff = array_shift($blocks);
493 list($a,$b) = split("---\n",$diff,2);
504 array_push($out,array($lold,$type,$lnew,$a,$b));
505 $lold = array_shift($blocks);
507 //array_push($out,array($type,$a,$b));
512 /** Return an RCS-managed directory.
514 * @param dir the directory
515 * @param loc the visible location for the directory
516 * @param canedit can we edit files in this directory?
518 function dispDir($dir,$loc,$canedit) {
521 if ($pdir = @opendir
($this->rcsPath($dir))) {
522 while ( ($file = readdir($pdir)) !== false
) {
523 if ( ($file != ".") && ($file != "..") )
525 $entry = $this->dispEntry($dir,$loc,$file,$canedit);
527 array_push($entries, $entry);
536 /** Returns an RCS "item" (file or directory).
538 * @param dir parent directory
539 * @param loc visible location for parent directory
540 * @param file the RCS "item"
541 * @param canedit can we edit files this entry?
543 function dispEntry($dir,$loc,$file,$canedit) {
546 $view = $edit = $del = $size = $rev = "";
548 $myitem = $this->rcsPath($dir,$file);
550 // check the RCS entry exists
551 if (!file_exists($myitem)) {
552 $this->info("RCS entry '$myitem' does not exist!");
558 // this is a directory, this should not happen!
559 $this->info("Unexpected directory in RCS, skipping : '$myitem'");
562 else if (substr($file,-2) == ",v")
564 // this is an RCS file
565 $file = substr($file,0,-2);
567 // check we have a working copy of this item
568 $spoolitem = $this->spoolPath($dir,$file);
569 if (!is_file($spoolitem))
571 $this->info("Could not find working copy '$spoolitem'!");
575 $size = $this->dispSize(filesize($spoolitem));
576 $icon = $globals->icons
->get_mime_icon($spoolitem);
580 $myrev = array_shift($tmparr = $this->logParse($dir,$file));
585 "rev" => array($myrev['rev'],$rev),
586 "date" => $myrev['date'],
587 "author" => $myrev['author'],
594 $this->info("Unknown RCS entry type : '$myitem'");
601 /** Format a file size for display.
603 * @param size the size, in bytes
605 function dispSize($size)
609 else if ($size < 1000000)
610 return floor($size/1000)." kB";
612 return floor($size/1000000)." MB";