reorganisation, le parse se fait en un coup
authorJeremy Laine <jeremy.laine@m4x.org>
Sat, 23 Nov 2002 03:33:19 +0000 (03:33 +0000)
committerJeremy Laine <jeremy.laine@m4x.org>
Sat, 23 Nov 2002 03:33:19 +0000 (03:33 +0000)
deptrack/deptrack.pl

index 10afc3e..a0e3589 100755 (executable)
@@ -2,12 +2,15 @@
 #
 # $Id$
 
-use DBI;
 use strict;
+use DBI();
 
 my $dsn = "DBI:mysql:database=deptrack;host=localhost";
 my $dbh = DBI->connect($dsn,"deptrack","phptrax");
 
+my $mask_inc = "\\.inc(\\.php)?\$";
+my $mask_php = "(?<!\\.inc)\\.php\$";
+my $mask_img = "\\.(png|gif|jpg)\$"; 
 
 # return program syntax 
 sub syntax {
@@ -15,6 +18,13 @@ sub syntax {
   exit 0;
 }
 
+
+###############################################################################
+#                                                                             #
+#                              text operations                                #
+#                                                                             #
+###############################################################################
+
 # worker function called by tagextract and tagstrip
 sub tagcrunch {
   my($tag1,$tag2,$intag,@lines) = @_;
@@ -82,6 +92,61 @@ sub commentstrip {
   return @out;
 }
 
+
+###############################################################################
+#                                                                             #
+#                                  DB access                                  #
+#                                                                             #
+###############################################################################
+
+sub cleartable {
+  my($table) = @_;
+  my $sth = $dbh->do("delete from $table");
+}
+
+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);
+    $fid=&getfileid($file);
+  }
+  return $fid;  
+}
+
+sub getfileid {
+  my($file) = @_;
+  my($sth);
+  
+  $sth = $dbh->prepare("select id from file where path='$file'");
+  $sth->execute();
+  if (my $ref = $sth->fetchrow_hashref()) {
+    return $ref->{"id"};
+  } else {
+   return 0;
+  }
+}
+
+
+###############################################################################
+#                                                                             #
+#                              file processing                                #
+#                                                                             #
+###############################################################################
+
 # parse a directory
 sub parsedir {
   my($dir,$vdir) = @_;
@@ -93,9 +158,16 @@ sub parsedir {
   my @list = grep /^[^\.]/, readdir(DIRHANDLE);
   closedir(DIRHANDLE);
   
-  my @phpfiles=grep /\.(php|inc)$/,@list;
-  map (&parsefile("$dir/$_", "${vdir}$_"), @phpfiles);
+  print "$dir\n";
+  my @incfiles=grep /$mask_inc/,@list;
+  map (&putfile("${vdir}$_", 1), @incfiles);
 
+  my @imgfiles=grep /$mask_img/,@list;
+  map (&putfile("${vdir}$_", 1), @imgfiles);
+  
+  my @phpfiles=grep /$mask_php/,@list;
+  map (&parsefile("$dir/$_", "${vdir}$_", $vdir), @phpfiles);
+  
   foreach my $entry (@list) {
     if ((-d $entry) && ($entry !~ /^CVS$/)) {
       &parsedir("$dir/$entry", "${vdir}${entry}", "$vdir");
@@ -106,8 +178,11 @@ sub parsedir {
 # parse a file
 sub parsefile {
   my($file,$vfile,$vdir) = @_;
-  my(@lines,@phplines,@includes);
+  my(@lines,@phplines,@includes,$fid);
  
+  print "parsefile($file,$vfile,$vdir)\n";
+  $fid=&putfile($vfile,1);
+
   open(FHANDLE,$file);
   @lines = <FHANDLE>;
   close(FHANDLE);
@@ -130,40 +205,10 @@ sub parsefile {
   }
 }
 
-# create DB entries for a directory
-sub listdir {
-  my($dir,$vdir) = @_;
-  my($sth);
-  
-  # build virtual path
-  $vdir="$vdir/" if (length($vdir)>0);
-
-  opendir(DIRHANDLE, $dir);
-  my @list = grep /^[^\.]/, readdir(DIRHANDLE);
-  closedir(DIRHANDLE);
-  
-  my @phpfiles=grep /\.(php|inc)/,@list;
-  foreach my $file (@phpfiles) {
-    if ($file =~ /\.inc(\.php)?$/) {
-      $sth = $dbh->do("insert into file set path='$vdir$file',type='include'");
-    } else {
-      $sth = $dbh->do("insert into file set path='$vdir$file',type='page'");
-    }
-  }
-  
-  foreach my $entry (@list) {
-    if ((-d $entry) && ($entry !~ /^CVS$/)) {
-      &listdir("$dir/$entry", "$vdir$entry");
-    }
-  }
-}
-
 ## do the work
 my $nargs = @ARGV;
 $nargs || &syntax();
 
 my $topdir = $ARGV[0];
 $topdir =~ s/\/$//;
-
-&listdir($topdir,"");
 &parsedir($topdir,"");