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";
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);
# #
###############################################################################
+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);
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);
@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");
}
}
}
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("/");