8 my $dsn = "DBI:mysql:database=deptrack;host=localhost";
9 my $dbh = DBI
->connect($dsn,"deptrack","phptrax");
11 my @incpath = split /:/, "./:../";
13 my $mask_inc = "\\.inc(\\.php)?\$";
14 my $mask_php = "(?<!\\.inc)\\.php\$";
15 my $mask_img = "\\.(png|gif|jpg)\$";
17 my $rootdir = $ARGV[0];
21 # return program syntax
23 print "Syntax:\n\tdeptrack root_directory\n\n";
29 print($text) if ($debug);
32 ###############################################################################
36 ###############################################################################
38 # worker function called by tagextract and tagstrip
40 my($tag1,$tag2,$intag,@lines) = @_;
41 my($line,$pos,@out,$add);
43 while ($line = shift(@lines)) {
45 # we are inside the block
46 if (($pos=index($line, $tag2)) > -1) {
47 $add = substr($line,0,$pos);
48 $line = substr($line,$pos+length($tag2));
56 push @out, "$add\n" if (length($add)>0);
60 # we are outside the block
61 while(($pos = index($line, $tag1)) > -1) {
62 $line = substr($line,$pos+length($tag1));
63 if (($pos=index($line, $tag2)) > -1) {
64 $add = substr($line,0,$pos);
65 $line = substr($line,$pos+length($tag2));
73 push @out, "$add\n" if (length($add)>0);
80 # return only blocks of @lines between tag1 and tag2
82 my($tag1,$tag2,@lines) = @_;
83 return &tagcrunch
($tag1,$tag2,0,@lines);
86 # return @lines minus blocks between tag1 and tag2
88 my($tag1,$tag2,@lines) = @_;
89 return &tagcrunch
($tag2,$tag1,1,@lines);
92 # for each line, remove whatever is after the comment marker
97 foreach my $line (@lines) {
100 push @out, "$line\n" if (length($line)>0);
106 ###############################################################################
110 ###############################################################################
112 # clear a database table
115 my $sth = $dbh->do("delete from $table");
118 # add a dependency into DB
120 my($pageid,$depid,$did,$type) = @_;
123 $sth = $dbh->prepare("select id from dep where page=$pageid and dep=$depid and dir=$did and type='$type'");
126 $dbh->do("insert into dep set page=$pageid,dep=$depid,dir=$did,type='$type'");
130 # put a directory into DB
134 &dprint
("putdir($dir)\n");
135 my $did=&getdirid
($dir);
137 $dbh->do("insert into dir set path='$dir'");
138 $did=&getdirid
($dir);
145 my($file,$exists) = @_;
148 $exists=0 if (!$exists);
149 &dprint
("putfile($file,$exists)\n");
151 $type = "page" if ($file =~ /$mask_php/);
152 $type = "include" if ($file =~ /$mask_inc/);
153 $type = "image" if ($file =~ /$mask_img/);
158 $fid=&getfileid
($file);
160 $dbh->do("update file set type='$type' where id=$fid") if ($exists);
162 $dbh->do("insert into file set path='$file',type='$type'");
163 $fid=&getfileid
($file);
168 # return the ID corresponding to a directory name
173 $sth = $dbh->prepare("select id from dir where path='$dir'");
175 if (my $ref = $sth->fetchrow_hashref()) {
182 # return the ID corresponding to a file name
187 $sth = $dbh->prepare("select id from file where path='$file'");
189 if (my $ref = $sth->fetchrow_hashref()) {
197 ###############################################################################
201 ###############################################################################
203 # from a path in the server tree, return the path in the filesystem
207 return $rootdir.$vpath;
210 # given a working directory and a relative path, return full path
211 # ( in the server tree )
213 my($curdir,$relpath) = @_;
214 my(@path,@comps,$ret);
216 # current directory not absolute
217 if ($curdir !~ /^\//) {
218 print "abspath($curdir,$relpath) : $curdir is not an absolute path!\n";
222 # relpath is already absolute
223 return $relpath if ($relpath =~ /^\//);
225 # is there a final slash?
227 $finalslash="/" if ($relpath =~ /\/$/);
229 @path = split /\//,substr($curdir,1);
230 @comps = split /\//,$relpath;
231 foreach my $comp (@comps) {
233 if ($comp =~ /^\.$/) { last SWITCH; }
234 if ($comp =~ /^\.\.$/) { pop @path; last SWITCH; }
238 $ret = "/". join("/",@path) .$finalslash;
251 opendir(DIRHANDLE, &realpath($vdir));
252 my @list = grep /^[^\.]/, readdir(DIRHANDLE);
255 my @incfiles=grep /$mask_inc/,@list;
256 map (&putfile("${vdir
}$_", 1), @incfiles);
258 my @imgfiles=grep /$mask_img/,@list;
259 map (&putfile("${vdir
}$_", 1), @imgfiles);
261 my @phpfiles=grep /$mask_php/,@list;
262 foreach my $phpfile (@phpfiles) {
263 if (-f realpath("${vdir
}${phpfile
}")) {
264 $fid=&putfile("${vdir
}${phpfile
}", 1);
265 &parsefile($fid, $did, "${vdir
}${phpfile
}", $vdir);
267 &putfile("${vdir
}$phpfile", 0)
271 foreach my $entry (@list) {
272 if ((-d realpath("${vdir
}${entry
}")) && ($entry !~ /^CVS$/)) {
273 &parsedir("${vdir
}${entry
}/");
280 my($fid,$did,$vfile,$vdir) = @_;
281 my(@lines,@phplines,@includes,$depid,$sth,$ifile,$iexists);
283 &dprint("parsefile
($fid,$did,$vfile,$vdir)\n");
285 # we return if we already have dependecies in the same scope
286 $sth = $dbh->prepare("select id from dep where page
=$fid and dir
=$did");
288 return if (my $ref = $sth->fetchrow_hashref());
290 open(FHANDLE,realpath($vfile));
294 # strip out commented code
295 @lines = &tagstrip("<!--","-->",@lines);
297 # deal with PHP lines
298 @phplines = &tagextract("<?php
","?
>",@lines);
299 @phplines = &tagstrip("/*","*/",@phplines);
300 @phplines = &commentstrip("\
/\/",@phplines);
301 @phplines = &commentstrip("#",@phplines);
302 @includes = grep s/.*(require|include)\s*\(\"(.*)\"\).*\n$/$2/, @phplines;
303 foreach my $include (@includes) {
304 &dprint
("REQ:$include\n");
305 if ($include =~ /$mask_php/) {
306 print "Warning : $include gets included by $vfile!\n";
309 if ($include =~ /^[\.\/]/) {
310 # the directory is specified
311 $ifile = &abspath
($vdir,$include);
312 $iexists=1 if (-f
&realpath
($ifile));
314 # directory not specified, go through the include path
315 foreach my $incp (@incpath) {
316 $ifile = &abspath
(&abspath
($vdir,$incp),$include);
317 &dprint
("trying $ifile\n");
318 if (-f
&realpath
($ifile)) {
323 # did not find file to be included, treat it as if it were
324 # in the first segment of include path
325 $ifile = &abspath
(&abspath
($vdir,$incpath[0]),$include) if (!$iexists);
327 $depid=&putfile
($ifile,$iexists);
328 &putdep
($fid,$depid,$did,"include");
329 &parsefile
($depid,$did,$ifile,$vdir) if ($iexists);
340 print "root directory $rootdir not found!\n";