sub get_facets { my $array_ref = shift; my %facet; my $i = 0; foreach ( @$array_ref ) { my $k = $array_ref->[ $i ]; $i++; my $v = $array_ref->[ $i ]; $i++; next if ( ! $v ); $facet{ $k } = $v; } return %facet; } sub escape_entities { # get the input my $s = shift; # escape $s =~ s/&/&/g; $s =~ s//>/g; $s =~ s/"/"/g; $s =~ s/'/'/g; # done return $s; } sub normalize { my $s = shift; $s =~ s/<.*?>/ /g; while ( $s =~ /^\s/ ) { $s =~ s/^\s// } while ( $s =~ /\s$/ ) { $s =~ s/\s$// } $s =~ s/\t/ /g; $s =~ s/\n/ /g; $s =~ s/\r/ /g; while ( $s =~ / / ) { $s =~ s/ / / } return $s; } sub remove_duplicates { my @array = @_; my %seen = (); return grep { ! $seen{ $_ }++ } @array; } sub guess_mime { # guess a mime-type based on a file's extension my $s = shift; my $extension = substr $s, (( rindex $s, '.' ) + 1 ); my $mime = ''; if ( $extension =~ /[Tt][Xx][Tt]/ ) { $mime = 'text/plain' } if ( $extension =~ /[Hh][Tt][Mm][Ll]/ ) { $mime = 'text/html' } if ( $extension =~ /[Hh][Tt][Mm]/ ) { $mime = 'text/html' } if ( $extension =~ /[Pp][Dd][Ff]/ ) { $mime = 'application/pdf' } return $mime; } sub make_or_get_facet_id { # get the input my $name = shift; my $note = shift; # initialize my $facet = MyLibrary::Facet->new; # check for the name if (! MyLibrary::Facet->new( name => $name )) { # create it $facet->facet_name( $name ); $facet->facet_note( $note ); $facet->commit; print "The facet $name ($note) has been created.\n"; } else { # already exists $facet = MyLibrary::Facet->new( name => $name ); print "The facet $name already exists.\n"; } # done return $facet->facet_id; } sub make_or_get_term_id { # get the input my $name = shift; my $note = shift; my $facet_id = shift; # initialize my $term = MyLibrary::Term->new; # check for the name if (! MyLibrary::Term->new( name => $name )) { # create it $term->term_name( $name ); $term->term_note( $note ); $term->facet_id( $facet_id ); $term->commit; print "The term $name ($note) has been created.\n"; } else { # already exists $term = MyLibrary::Term->new( name => $name ); print "The term $name already exists.\n"; } # done return $term->term_id; } sub make_or_get_location_type { # get input my $name = shift; my $note = shift; # initialize my $location_type; # see if it exists foreach ( MyLibrary::Resource::Location::Type->all_types ) { my $type = MyLibrary::Resource::Location::Type->new( id => $_ ); if ( $type->name eq $name ) { $location_type = $type->location_type_id; print "Location type $name exists\n"; last; } } # if not, then create it if ( ! $location_type ) { my $type = MyLibrary::Resource::Location::Type->new; $type->name( $name ); $type->description( $note ); $type->commit; $location_type = $type->location_type_id; print "Location type $name was created\n"; } # done return $location_type; } sub error { # get the error message my $msg = shift; $msg = $cgi->p({ style => 'text-align: center; color: red' }, $msg ); # create the html my $html = &slurp( TEMPLATE ); $html =~ s/##CONTENT##/$msg/e; $html =~ s/##QUERY##//ge; # done &gracefulExit ( $html ); } sub slurp { # open a file named by the input and return its contents my $f = @_[0]; my $r; open (F, "< $f"); while () { $r .= $_ } close F; return $r; } 1;