fonctionne, mais ne tiens pas compte du include path
authorJeremy Laine <jeremy.laine@m4x.org>
Sat, 23 Nov 2002 06:35:07 +0000 (06:35 +0000)
committerJeremy Laine <jeremy.laine@m4x.org>
Sat, 23 Nov 2002 06:35:07 +0000 (06:35 +0000)
deptrack/deptrack.pl
deptrack/deptrack.sql

index a0e3589..024cd81 100755 (executable)
@@ -8,10 +8,14 @@ use DBI();
 my $dsn = "DBI:mysql:database=deptrack;host=localhost";
 my $dbh = DBI->connect($dsn,"deptrack","phptrax");
 
+my $incpath = "./:../";
+
 my $mask_inc = "\\.inc(\\.php)?\$";
 my $mask_php = "(?<!\\.inc)\\.php\$";
 my $mask_img = "\\.(png|gif|jpg)\$"; 
 
+my $rootdir = $ARGV[0];
+
 # return program syntax 
 sub syntax {
   print "Syntax:\n\tdeptrack directory\n\n";
@@ -104,29 +108,65 @@ sub cleartable {
   my $sth = $dbh->do("delete from $table");
 }
 
+sub putdep {
+  my($pageid,$depid,$did,$type) = @_;
+  my($sth);
+  
+  $sth = $dbh->prepare("select id from dep where page=$pageid and dep=$depid and dir=$did and type='$type'");
+  $sth->execute();
+  if (!$sth->rows) {
+    $dbh->do("insert into dep set page=$pageid,dep=$depid,dir=$did,type='$type'");
+  }
+}
+
+sub putdir {
+  my($dir) = @_;
+
+  print "putdir($dir)\n";
+  my $did=&getdirid($dir);
+  if (!$did) {
+    $dbh->do("insert into dir set path='$dir'");
+    $did=&getdirid($dir);
+  }
+  return $did;
+}
+
 sub putfile {
   my($file,$exists) = @_;
   my($fid,$type);
   
   print "putfile($file,$exists)\n";
-  $fid=&getfileid($file);
   if ($exists) {
     $type = "page" if ($file =~ /$mask_php/);
     $type = "include" if ($file =~ /$mask_inc/);
     $type = "image" if ($file =~ /$mask_img/);
-    if ($fid) {
-      $dbh->do("update file set path='$file' where id=$fid");
-    } else {
-      $dbh->do("insert into file set path='$file',type='$type'");
-      $fid=&getfileid($file);
-    }
   } else {
-    $dbh->do("insert into file set path='$file',type='dead'") if (!$fid);
+    $type = "dead";
+  }
+
+  $fid=&getfileid($file);
+  if ($fid) {
+    $dbh->do("update file set type='$type' where id=$fid") if ($exists);
+  } else {
+    $dbh->do("insert into file set path='$file',type='$type'");
     $fid=&getfileid($file);
   }
   return $fid;  
 }
 
+sub getdirid {
+  my($dir) = @_;
+  my($sth);
+  
+  $sth = $dbh->prepare("select id from dir where path='$dir'");
+  $sth->execute();
+  if (my $ref = $sth->fetchrow_hashref()) {
+    return $ref->{"id"};
+  } else {
+   return 0;
+  }
+}
+
 sub getfileid {
   my($file) = @_;
   my($sth);
@@ -147,18 +187,56 @@ sub getfileid {
 #                                                                             #
 ###############################################################################
 
+sub realpath {
+  my($vpath) = @_;
+  
+  return $rootdir.$vpath;
+}
+
+sub abspath {
+  my($curdir,$relpath) = @_;
+  my(@path,@comps,$ret);
+  # current directory not absolute
+  if ($curdir !~ /^\//) {
+    print "abspath($curdir,$relpath) : $curdir is not an absolute path!\n";
+    die;
+  }
+
+  # relpath is already absolute
+  return $relpath if ($relpath =~ /^\//);
+  
+  # is there a final slash?
+  my $finalslash="";
+  $finalslash="/" if ($relpath =~ /\/$/);
+  
+  @path = split /\//,substr($curdir,1);
+  @comps = split /\//,$relpath;
+  foreach my $comp (@comps) {
+    SWITCH: {
+      if ($comp =~ /^\.$/) { last SWITCH; }
+      if ($comp =~ /^\.\.$/) { pop @path; last SWITCH; }
+      push @path, $comp;
+    }
+  }
+  $ret = "/". join("/",@path) .$finalslash;
+  $ret =~ s/\/\//\//;
+  return $ret;
+}
+
 # parse a directory
 sub parsedir {
-  my($dir,$vdir) = @_;
+  my($vdir) = @_;
+  my($fid,$did);
 
   # build virtual path
-  $vdir="$vdir/" if (length($vdir)>0);
+  $did=&putdir($vdir);
 
-  opendir(DIRHANDLE, $dir);
+  opendir(DIRHANDLE, &realpath($vdir));
   my @list = grep /^[^\.]/, readdir(DIRHANDLE);
   closedir(DIRHANDLE);
   
-  print "$dir\n";
+  print "$vdir\n";
   my @incfiles=grep /$mask_inc/,@list;
   map (&putfile("${vdir}$_", 1), @incfiles);
 
@@ -166,24 +244,33 @@ sub parsedir {
   map (&putfile("${vdir}$_", 1), @imgfiles);
   
   my @phpfiles=grep /$mask_php/,@list;
-  map (&parsefile("$dir/$_", "${vdir}$_", $vdir), @phpfiles);
-  
+  foreach my $phpfile (@phpfiles) {
+    if (-f realpath("${vdir}${phpfile}")) {
+      $fid=&putfile("${vdir}${phpfile}", 1);
+      &parsefile($fid, $did, "${vdir}${phpfile}", $vdir);
+    } else {
+      &putfile("${vdir}$phpfile", 0)
+    }
+  } 
+
   foreach my $entry (@list) {
-    if ((-d $entry) && ($entry !~ /^CVS$/)) {
-      &parsedir("$dir/$entry", "${vdir}${entry}", "$vdir");
+    if ((-d realpath("${vdir}${entry}")) && ($entry !~ /^CVS$/)) {
+      &parsedir("${vdir}${entry}/");
     }
   }
 }
 
 # parse a file
 sub parsefile {
-  my($file,$vfile,$vdir) = @_;
-  my(@lines,@phplines,@includes,$fid);
-  print "parsefile($file,$vfile,$vdir)\n";
-  $fid=&putfile($vfile,1);
-
-  open(FHANDLE,$file);
+  my($fid,$did,$vfile,$vdir) = @_;
+  my(@lines,@phplines,@includes,$depid,$sth);
+  
+  print "parsefile($fid,$did,$vfile,$vdir)\n";
+  $sth = $dbh->prepare("select id from dep where page=$fid and dir=$did");
+  $sth->execute();
+  return if (my $ref = $sth->fetchrow_hashref());
+  
+  open(FHANDLE,realpath($vfile));
   @lines = <FHANDLE>;
   close(FHANDLE);
 
@@ -195,12 +282,16 @@ sub parsefile {
   @phplines = &tagstrip("/*","*/",@phplines);
   @phplines = &commentstrip("\/\/",@phplines);
   @phplines = &commentstrip("#",@phplines);
-  @includes = grep /(require|include)\s*\(/, @phplines;
+  @includes = grep s/.*(require|include)\s*\(\"(.*)\"\).*\n$/$2/, @phplines;
   foreach my $include (@includes) {
-    if ($include =~ /\.inc(\.php)?$/) {
-
-    } else {
-
+    print "REQ:$include\n";
+    if ($include =~ /$mask_php/) {
+      print "ERROR : a .php file gets included by $vfile!\n";
+      die;
+    }
+    if ($include =~ /$mask_inc/) {
+      $depid=&putfile(&abspath($vdir,$include),0);
+      &putdep($fid,$depid,$did,"include");
     }
   }
 }
@@ -209,6 +300,14 @@ sub parsefile {
 my $nargs = @ARGV;
 $nargs || &syntax();
 
-my $topdir = $ARGV[0];
-$topdir =~ s/\/$//;
-&parsedir($topdir,"");
+$rootdir =~ s/\/$//;
+
+if (!-d $rootdir) {
+ print "root directory $rootdir not found!\n";
+ die;
+}
+
+&cleartable("dir");
+&cleartable("file");
+&cleartable("dep");
+&parsedir("/");
index 7cb646b..40fa443 100644 (file)
@@ -4,6 +4,7 @@
 
 CREATE TABLE dep (
   id int(8) NOT NULL auto_increment,
+  dir int(6) NOT NULL default '0',
   page int(6) NOT NULL default '0',
   dep int(6) NOT NULL default '0',
   type enum('include','image','link') NOT NULL default 'include',
@@ -12,6 +13,18 @@ CREATE TABLE dep (
 # --------------------------------------------------------
 
 #
+# Table structure for table `dir`
+#
+
+CREATE TABLE dir (
+  id int(6) NOT NULL auto_increment,
+  path varchar(255) NOT NULL default '',
+  PRIMARY KEY  (id),
+  UNIQUE KEY path (path)
+) TYPE=MyISAM;
+# --------------------------------------------------------
+
+#
 # Table structure for table `file`
 #