Debian Codegraph @

examples/odffilesearch

raw
1 #!/usr/bin/perl
2 #-----------------------------------------------------------------------------
3 # $Id: odffilesearch 0.007 2008-05-04$
4 #-----------------------------------------------------------------------------
5
6 =head1 NAME
7
8 odffilesearch - File selection by keywords
9
10 =head1 SYNOPSIS
11
12 odffilesearch -R "D:\Documents\*.odt" openoffice desktop XML
13
14 produces the list of the ODF Text documents present in the given
15 directory and its subdirectories, and containing the words
16 "openoffice", "desktop" AND "XML"
17
18 odffilesearch -command "rm -f %f" "*.ods *.odt" lost dismiss cancel
19
20 executes the "rm -f filename" (i.e. deletes the file in a Unix system)
21 for each ODT or ODS file present in the current directory
22 and containing the words "lost", "dismiss" AND "cancel"
23
24 =head1 USAGE
25
26 odffilesearch [-options] <file filter> <keyword list>
27
28 =head1 DESCRIPTION
29
30 This utility allows the user to retrieve a list of files matching
31 a given set of keywords or regular expressions. A file is selected
32 when it contains, in its text and/or in its metadata (title, subject,
33 keywords or description), all the given search strings.
34
35 The selected files are echoed to the standard output (one file per
36 line), so this utility can be used as a filter piping its results
37 to another program. Alternatively, a given shell command can be
38 launched by the script each time a file matches, allowing on-the-fly
39 processing of the selected documents.
40
41 The files filter may content one or more space-separated paths.
42 Each path may content jokers. So it's possible to explore several
43 directories and/or several filename patterns.
44
45 All the arguments after the file filter are processed as search
46 criteria.
47
48 =head1 OPTIONS
49
50 -R -recursive
51 include the subdirectories of each given search directory
52 -verbose -trace -debug
53 echo some processing comments
54 -warnings
55 activate the warning messages of the OpenOffice::OODoc API
56 -log <file>
57 like -verbose, but then messages are sent to the given file
58 and don't pollute the standard output
59 -result <file>
60 -output <file>
61 send the list of matching files to the given file and not
62 to the standard output
63 -criteria <file>
64 get search criteria from a file (one per line); the loaded
65 search keywords may be combined with additional criteria
66 passed with the command line, if any.
67 -command <command> -exec <command>
68 execute a shell command for each matching file ; if the
69 command string contains "%f", this substring is replaced
70 with the name of the selected file ; if this option is
71 provided, the selection list is not echoed to the standard
72 output ; if -verbose is on, the value returned by the
73 command is echoed
74 -encoding <encoding>
75 selects the user's character set ; this option is mandatory
76 if one or more search criteria contain characters not
77 belonging to the default character set
78
79 =cut
80
81 #=============================================================================
82
83 use OpenOffice::OODoc 2.101;
84 use Getopt::Long;
85
86 our $VERSION = 0.007;
87
88 #=============================================================================
89
90
91 my $recursive = undef;
92 my $verbose = undef;
93 my $warnings = undef;
94 my $command = undef;
95 my $result = undef;
96 my $log = undef;
97 my $list = undef;
98 my $character_set = undef;
99 my $RESULT = *STDOUT;
100 my $LOG = *STDOUT;
101
102 GetOptions
103 (
104 'R|recursive' => \$recursive,
105 'verbose|trace|debug' => \$verbose,
106 'warnings' => \$warnings,
107 'log=s' => \$log,
108 'result|output=s' => \$result,
109 'command|exec=s' => \$command,
110 'criteria=s' => \$list,
111 'encoding=s' => \$character_set
112 );
113
114 #=============================================================================
115
116 my @keywords = ();
117 my $count = 0;
118
119 #=============================================================================
120
121 sub horodate
122 {
123 my @d = localtime();
124 return sprintf
125 (
126 "[%02d/%02d/%04d %02d:%02d:%02d] ",
127 $d[3], $d[4] + 1, $d[5] + 1900, $d[2], $d[1], $d[0]
128 );
129 }
130
131 sub message
132 {
133 my $text = shift;
134 return unless ($verbose);
135 print $LOG horodate() . "$text\n";
136 print $LOG "\t$_\n" for @_;
137 }
138
139 #-----------------------------------------------------------------------------
140
141 sub matching_file
142 {
143 my $file = shift;
144 my @words = @_;
145 my $n = scalar @words;
146 my $text = "";
147
148 my $oof = ooFile($file);
149 unless ($oof)
150 {
151 message "$file doesn't look like an ODF file";
152 return undef;
153 }
154 my $meta = odfMeta(container => $oof) or message
155 "$file doesn't contain metadata";
156 if ($meta)
157 {
158 my $title = $meta->title;
159 if ($title)
160 {
161 message "Title: \"$title\"";
162 $text .= $title;
163 }
164 else
165 {
166 message "Title: <UNTITLED>";
167 }
168 $text .= ($meta->keywords || "");
169 $text .= ($meta->subject || "");
170 $text .= ($meta->description || "");
171
172 $meta->dispose;
173 }
174 my $content = odfText(container => $oof) or message
175 "$file doesn't have a regular content";
176 if ($content)
177 {
178 $text .= ($content->getTextContent || "");
179
180 $content->dispose;
181 }
182
183 return undef unless $text;
184 while (@words)
185 {
186 my $word = shift @words or next;
187 return undef unless $text =~ /$word/i;
188 }
189 return 1;
190 }
191
192 #-----------------------------------------------------------------------------
193
194 sub file_selection
195 {
196 my @list = @_;
197 my $number = scalar @list;
198
199 message "$number file(s) in the search list";
200 FILE: foreach my $file (@list)
201 {
202 unless (-r $file)
203 {
204 message "$file : unreadable";
205 next FILE;
206 }
207 if (-l $file)
208 {
209 message "$file : symbolic link, ignored";
210 next FILE;
211 }
212 if ((-d $file) && $recursive)
213 {
214 message "Searching in $file";
215 file_selection(glob("$file/*"));
216 next FILE;
217 }
218 unless (-s $file)
219 {
220 message "$file : empty";
221 next FILE;
222 }
223 unless (-f $file)
224 {
225 message "$file is not a regular file";
226 next FILE;
227 }
228 message "Processing $file";
229 if (matching_file($file, @keywords))
230 {
231 message "OK! $file matches all the criteria";
232 if ($command)
233 {
234 my $cmd = $command;
235 $cmd =~ s/\%f/$file/g;
236 message "Executing command: $cmd";
237 my $r = system $cmd;
238 message "Command result is $r";
239 }
240 else
241 {
242 print $RESULT "$file\n";
243 }
244 $count++;
245 }
246 else
247 {
248 message "file $file doesn't match";
249 }
250 }
251 }
252
253 #=============================================================================
254 # main program
255
256 if ($result)
257 {
258 open RESULT, ">", $result
259 or die "output file $result is unwritable\n";
260 $RESULT = *RESULT;
261 }
262
263 if ($log)
264 {
265 open OUTPUT, ">>", $log
266 or die "log file $log is unwritable\n";
267 $LOG = *OUTPUT;
268 $verbose = 1;
269 }
270
271 if ($list)
272 {
273 message "Loading a keyword list from $list";
274 my $m;
275 open LIST, "<", $list or warn "file $list is unreadable\n";
276 while ($m = <LIST>)
277 {
278 chomp $m;
279 push @keywords, $m;
280 }
281 close LIST;
282 }
283
284 localEncoding($character_set) if $character_set;
285
286 die "Usage: odffilesearch [-options] <filefilter> [keywords]\n"
287 unless $ARGV[0];
288
289 message "Starting the search...";
290
291 my $filter = shift @ARGV;
292 push @keywords, @ARGV;
293 die "Empty keword list.\n" unless @keywords;
294 message "Keyword list:", @keywords;
295 unless ($warnings)
296 {
297 $SIG{'__WARN__'} = sub {};
298 }
299 file_selection(glob($filter));
300
301 message "Finished - $count file(s) selected";
302 exit;
303
304 #=============================================================================
305