Commit | Line | Data |
---|---|---|
14d630dd JL |
1 | #!/usr/bin/perl -w |
2 | # | |
3 | # $Id$ | |
4 | ||
0a7f073d | 5 | use DBI; |
90bd1770 JL |
6 | use strict; |
7 | ||
8 | my $dsn = "DBI:mysql:database=deptrack;host=localhost"; | |
9 | my $dbh = DBI->connect($dsn,"deptrack","phptrax"); | |
10 | ||
0a7f073d | 11 | |
14d630dd JL |
12 | # return program syntax |
13 | sub syntax { | |
14 | print "Syntax:\n\tdeptrack directory\n"; | |
15 | die; | |
16 | } | |
17 | ||
18 | # worker function called by tagextract and tagstrip | |
19 | sub tagcrunch { | |
20 | my($tag1,$tag2,$intag,@lines) = @_; | |
90bd1770 | 21 | my($line,$pos,@out,$add); |
14d630dd JL |
22 | |
23 | while ($line = shift(@lines)) { | |
24 | if ($intag) { | |
25 | # we are inside the block | |
26 | if (($pos=index($line, $tag2)) > -1) { | |
27 | $add = substr($line,0,$pos); | |
28 | $line = substr($line,$pos+length($tag2)); | |
29 | $intag = 0; | |
30 | } else { | |
31 | $add = $line; | |
32 | $line = ""; | |
33 | } | |
34 | $add =~ s/^\s*//; | |
35 | $add =~ s/\s*$//; | |
0a7f073d | 36 | if (length($add)>0) { push @out, "$add\n"; } |
14d630dd JL |
37 | } |
38 | ||
39 | if (!$intag) { | |
40 | # we are outside the block | |
41 | while(($pos = index($line, $tag1)) > -1) { | |
42 | $line = substr($line,$pos+length($tag1)); | |
43 | if (($pos=index($line, $tag2)) > -1) { | |
44 | $add = substr($line,0,$pos); | |
45 | $line = substr($line,$pos+length($tag2)); | |
46 | } else { | |
47 | $add = $line; | |
48 | $line=""; | |
49 | $intag=1; | |
50 | } | |
51 | $add =~ s/^\s*//; | |
52 | $add =~ s/\s*$//; | |
0a7f073d | 53 | if (length($add)>0) { push @out, "$add\n"; } |
14d630dd JL |
54 | } |
55 | } | |
56 | } | |
0a7f073d | 57 | return @out; |
14d630dd JL |
58 | } |
59 | ||
60 | # return only blocks of @lines between tag1 and tag2 | |
61 | sub tagextract { | |
62 | my($tag1,$tag2,@lines) = @_; | |
63 | return tagcrunch($tag1,$tag2,0,@lines); | |
64 | } | |
65 | ||
66 | # return @lines minus blocks between tag1 and tag2 | |
67 | sub tagstrip { | |
68 | my($tag1,$tag2,@lines) = @_; | |
69 | return tagcrunch($tag2,$tag1,1,@lines); | |
70 | } | |
71 | ||
0a7f073d JL |
72 | # for each line, remove whatever is after the comment marker |
73 | sub commentstrip { | |
74 | my($tag,@lines) = @_; | |
75 | my @out; | |
76 | ||
77 | foreach my $line (@lines) { | |
78 | $line =~ s/$tag.*//; | |
79 | $line =~ s/\s*$//; | |
80 | if (length($line)>0) { push @out, "$line\n"; } | |
81 | } | |
82 | return @out; | |
83 | } | |
84 | ||
14d630dd JL |
85 | # parse a directory |
86 | sub parsedir { | |
90bd1770 | 87 | my($dir,$vdir) = @_; |
0a7f073d JL |
88 | |
89 | # build virtual path | |
90 | if (length($vdir)>0) { | |
91 | $vdir="$vdir/" | |
92 | } | |
93 | ||
90bd1770 JL |
94 | opendir(DIRHANDLE, $dir); |
95 | my @list = grep /^[^\.]/, readdir(DIRHANDLE); | |
96 | closedir(DIRHANDLE); | |
0a7f073d | 97 | |
90bd1770 | 98 | my @phpfiles=grep /\.(php|inc)$/,@list; |
0a7f073d JL |
99 | foreach my $file (@phpfiles) { |
100 | parsefile("$dir/$file","$vdir$file"); | |
101 | } | |
102 | ||
103 | foreach my $entry (@list) { | |
104 | if (-d $entry) { | |
105 | if ($entry !~ /^CVS$/) { | |
90bd1770 | 106 | &parsedir("$dir/$entry", "$vdir$entry", "$vdir"); |
0a7f073d JL |
107 | } |
108 | } | |
14d630dd JL |
109 | } |
110 | } | |
111 | ||
112 | # parse a file | |
113 | sub parsefile { | |
90bd1770 JL |
114 | my($file,$vfile,$vdir) = @_; |
115 | my(@lines,@phplines,@includes); | |
0a7f073d | 116 | |
90bd1770 JL |
117 | open(FHANDLE,$file); |
118 | @lines = <FHANDLE>; | |
119 | close(FHANDLE); | |
0a7f073d | 120 | |
90bd1770 | 121 | # strip out commented code |
14d630dd | 122 | @lines = tagstrip("<!--","-->",@lines); |
90bd1770 JL |
123 | |
124 | # deal with PHP lines | |
125 | @phplines = tagextract("<?php","?>",@lines); | |
126 | @phplines = tagstrip("/*","*/",@phplines); | |
127 | @phplines = commentstrip("\/\/",@phplines); | |
128 | @phplines = commentstrip("#",@phplines); | |
129 | @includes = grep /(require|include)\s*\(/, @phplines; | |
130 | foreach my $include (@includes) { | |
131 | if ($include =~ /\.inc(\.php)?$/) { | |
132 | ||
133 | } else { | |
134 | ||
135 | } | |
136 | } | |
14d630dd JL |
137 | } |
138 | ||
0a7f073d JL |
139 | # create DB entries for a directory |
140 | sub listdir { | |
90bd1770 JL |
141 | my($dir,$vdir) = @_; |
142 | my($sth); | |
0a7f073d JL |
143 | |
144 | # build virtual path | |
145 | if (length($vdir)>0) { | |
90bd1770 | 146 | $vdir="$vdir/"; |
0a7f073d JL |
147 | } |
148 | ||
90bd1770 JL |
149 | opendir(DIRHANDLE, $dir); |
150 | my @list = grep /^[^\.]/, readdir(DIRHANDLE); | |
151 | closedir(DIRHANDLE); | |
0a7f073d | 152 | |
90bd1770 | 153 | my @phpfiles=grep /\.(php|inc)/,@list; |
0a7f073d | 154 | foreach my $file (@phpfiles) { |
90bd1770 JL |
155 | if ($file =~ /\.inc(\.php)?$/) { |
156 | $sth = $dbh->prepare("insert into file set path='$vdir$file',type='include'"); | |
157 | } else { | |
158 | $sth = $dbh->prepare("insert into file set path='$vdir$file',type='page'"); | |
159 | } | |
0a7f073d JL |
160 | $sth->execute; |
161 | } | |
162 | ||
163 | foreach my $entry (@list) { | |
164 | if (-d $entry) { | |
165 | if ($entry !~ /^CVS$/) { | |
166 | &listdir("$dir/$entry", "$vdir$entry"); | |
167 | } | |
168 | } | |
169 | } | |
170 | } | |
171 | ||
172 | ## do the work | |
14d630dd JL |
173 | my $nargs = @ARGV; |
174 | $nargs || syntax(); | |
175 | ||
0a7f073d JL |
176 | my $topdir = $ARGV[0]; |
177 | $topdir =~ s/\/$//; | |
178 | ||
179 | listdir($topdir,""); | |
180 | parsedir($topdir,""); |