|
 |
Perl 5 Sample Coding/Documentation Style |
|
Sample of couple functions for managing files in categories to be searched. |
#==========================================================================
# $Id: app-search.lib,v 1.20 2001/11/05 18:17:42 BSAC Exp $
#
# Brent S.A. Cowgill
#
# common application level functions for Search Engine admin and search CGI
#==========================================================================
# ------------------------------------------------------------------------
# Function : category_find_files
# Description : find all searchable files or all files in a category
# Usage : &category_find_files($rhCat, $trace, $rCode);
# &category_find_files($rhCat, $trace,
# sub {
# my $rhFileInfo = shift;
# # keys: action, full_name, path, file, name, ext
# })
# Arguments : $rhCat the category record from the category table
# $trace this flag, if true will find all files under
# the category root direcotory and the 'action'
# key will indicate how the file would be handled
# otherwise, only searchable files will be found
# $rCode subroutine reference called for each file found
# a hash reference is passed to the subroutine
# containing keys:
# action the action normally taken for the file
# this may be: dir, file, ignore_too_small_NNNN Kb,
# ignore_too_big_NNNN Kb, ignore_file, ignore_dir,
# ignore_ext
# full_name the full name and path of the file/dir
# relative_name the name and path of the file, relative to the
# root directory being scanned
# path the full path to the file/dir
# file the file name and extension without full path
# name the file name without extension
# ext the extension of the file
# ------------------------------------------------------------------------
sub category_find_files {
my ($rhCat, $trace, $rCode) = @_;
# TODO parameter validation
# First, prepare the list of file extensions based on
# checkboxes, setup options and custom file extensions
my ($raExt) = &category_prepare_extension_list($rhCat);
my $raPrefix = &category_prepare_prefix_list($rhCat);
my $raScan = &category_prepare_scan_list($rhCat);
my $raIgnore = &category_prepare_ignore_list($rhCat);
my $shallow = ! $rhCat->{'recursive'};
my ($min_size, $max_size) = &setup_min_max_size();
my $relative_regex = '^(' . join('|', map { my $temp = $_; $temp =~ s/([^a-z0-9])/\\$1/ig; $temp;} @$raScan) . '/?)';
# error checking, make sure root directories all exist
map { -d $_ || die("baddir: '$_'") } @$raScan;
my $rhFinder = {
'scan' => $raScan,
'skip' => $raIgnore,
'ext' => $raExt,
'prefix' => $raPrefix,
'shallow' => $shallow,
'trace' => $trace,
};
#-- COMPRESSOR IGNORE --#
# &log_error('trace', "category find files $rhCat->{'num'}", "about to FindFiles");
#-- /COMPRESSOR IGNORE --#
&FindFiles($rhFinder,
sub {
my ($full_name, $action) = @_;
#-- COMPRESSOR IGNORE --#
# &log_error('find', "$action\: $full_name\n");
#-- /COMPRESSOR IGNORE --#
# ignore files too small or too big
if ($action eq 'file') {
$min_size && (-s _ < $min_size) && ($action = 'ignore_too_small_' . &kb(-s _));
$max_size && (-s _ > $max_size) && ($action = 'ignore_too_big_' . &kb(-s _));
}
# Split up the file name into parts
my ($ext, $path, $file, $name, $relative) = ('', '', '', '', '');
$ext = $1 if $full_name =~ /\.([^.]*|)$/;
$path = $1 if $full_name =~ m#^(.*|)/[^/]*#;
$file = substr($full_name, 1 + length($path));
$path .= '/' if length($path);
$name = $1 if $file =~ /^(.*|)\.([^.]*)$/;
# determine filename relative to root directory being scanned
$relative = $full_name;
$relative =~ s/$relative_regex//;
# Call the provided subroutine
my $file_info =
{
'full_name' => $full_name,
'path' => $path,
'file' => $file,
'name' => $name,
'ext' => $ext,
'action' => $action,
'relative_name' => $relative,
};
$rCode->($file_info) if $trace || $action eq 'file';
});
} # category_find_files()
# ------------------------------------------------------------------------
# Function : category_purge_summaries
# Description : purge all searchable summary files for the category
# ------------------------------------------------------------------------
sub category_purge_summaries {
# $no_update_last_purge will prevent recording the last purge date
# in the category table, for when the category is being removed
my ($rhCat, $no_update_last_purge) = @_;
my $locked;
eval {
# temporarily cancel the auto die handler cause we need to check for a die
local $SIG{'__DIE__'} = undef;
# create directory if it doesn't exist yet
my ($searchable, $searchable_dir) = &category_searchable_file($rhCat, '');
unless (-d $searchable_dir)
{
&make_directory($searchable_dir, 0777);
die "STOP\n";
}
#-- COMPRESSOR IGNORE --#
# &log_error('trace', 'category purge', "purging search files...");
#-- /COMPRESSOR IGNORE --#
&lock_category($rhCat);
++$locked;
&category_record_purge($rhCat) unless $no_update_last_purge;
my $no_errors = 1;
# Change $no_errors to !$no_errors if you want to debug and examine the admin.log
my ($count, $raErrors) = &rmtree($searchable_dir, $no_errors);
if ($raErrors->[0]) {
# errors during removal of files, log to admin log
if ($raErrors->[0] =~ /^\d+$/) {
&log_error('warning', 'category purge', "$raErrors->[0] errors during file removal in $searchable_dir");
} # if
else {
&log_error('warning', 'category purge', @{$raErrors});
} # else
} # if
};
if ($@) {
# error locking or deleting, record to log file
&log_error('error', 'category purge', $@) unless $@ =~ /^STOP\n/;
} # if
&unlock_category($rhCat) if $locked;
} # category_purge_summaries()
|