#!/usr/local/bin/perl -w # # xhibition index_gallery_text.pl (c) Marc Majcher 2002 # # Indexing functionality for gallery text search. use strict; use XML::Twig; use DB_File; my $conf_dir = "config"; my $image_dir = "images"; my $xhconf_file = "$conf_dir/xhconf.xml"; my $index_db = "$conf_dir/search_index.db"; my %config = read_config($xhconf_file); my @gallery_files = @{$config{'galleries'}}; my $tpg = $config{'thumbs_per_gallery'}; my $base_url = $config{'base_url'}; my $page_ext = $config{'page_extension'}; my %index; my $debug; print "Starting index...\n" if $debug; for my $file (@gallery_files) { print " ...$file\n" if $debug; my $twig = XML::Twig->new; $twig->parsefile("$conf_dir/$file"); my $root = $twig->root; my $url = "$base_url/$image_dir/$file"; $url =~ s/\.xml$/\//; for my $title_word (split /\W+/,$root->first_child_text('gallery_title')) { push(@{$index{lc($title_word)}},$url); } for my $gallery_word (split /\W+/, $root->first_child_text('gallery_text')) { push(@{$index{lc($gallery_word)}},$url); } for my $image ($root->children('image')) { my $img_url = "$base_url/".$image->att('src').".$page_ext"; for my $ititle_word (split /\W+/,$image->first_child_text('title')) { push(@{$index{lc($ititle_word)}},$img_url); } for my $caption_word (split /\W+/,$image->first_child_text('caption')) { push(@{$index{lc($caption_word)}},$img_url); } } } print "Writing DB...\n" if $debug; my %DB; dbmopen(%DB,$index_db,0755) || die "can't open $index_db: $!"; for my $key (keys %index) { $DB{$key} = join("\t",@{$index{$key}}); } dbmclose(%DB); print "done.\n" if $debug; ## Ohhh, look at the False Laziness! AGAIN! Refactor, lazy! sub read_config { # Reads an XML configuration file, and returns a hash of values my $file = shift || return; my %config; my $content; open(CONF,$file) || die "can't open config file $file: $!"; { local $/ = undef; $content = ; } close(CONF); my $t = XML::Twig->new(PrettyPrint => 'indented'); $t->parse($content); my $r = $t->root; $config{'base_url'} = $r->first_child('base_url')->text; $config{'root_dir'} = $r->first_child('root_directory')->text; $config{'page_extension'} = $r->first_child('page_extension')->text; $config{'base_url'} =~ s/\/$//; my @galleries; for my $gtwig ($r->first_child('galleries')->children('gallery_file')) { push @galleries, $gtwig->text; } $config{'galleries'} = \@galleries; return %config; }