Show
Ignore:
Timestamp:
02/06/07 14:53:36 (2 years ago)
Author:
moseley
Message:

More updates getting ready to move to the new host
Combined the two search scripts into one.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • swish_website/src/search/index.html

    r1611 r1889  
    1 #![%- META type = 'text' -%] 
    2 [% locate.in_path( 'speedy') || locate.in_path( 'perl' ) %] 
     1#![% locate.in_path( 'speedy') || locate.in_path( 'perl' ) %] 
     2 
     3[% META type = 'text' %] 
    34 
    45package SwishAPISearch; 
    56use strict; 
    67use warnings; 
     8use DateTime; 
     9 
     10 
    711 
    812###################################################################### 
    913# Skeleton CGI script for searching a Swsih-e index with SWISH::API. 
    10 # see below for documenation or run "perldoc search.cgi" 
     14# see below for documentation or run "perldoc search.cgi" 
    1115# 
    1216# Copyright 2003, 2004 Bill Moseley - All rights reserved. 
     
    1519# 
    1620####################################################################### 
     21 
     22 
     23# Lookup hash for which metanames are defined 
     24our %METANAMES = ( 
     25    title       => 'swishtitle', 
     26    name        => 'name', 
     27    email       => 'email', 
     28); 
     29 
     30our %PROPERTIES = ( 
     31    name        => 'name', 
     32    email       => 'email', 
     33    title       => 'swishtitle', 
     34    rank        => 'swishrank', 
     35    date        => 'swishlastmodified', 
     36); 
     37 
     38our %REVERSE_SORT = ( 
     39    swishrank           => 1, 
     40    swishlastmodified   => 1, 
     41); 
     42 
     43# Valid section metaname values -- to limit to sections of the site. 
     44our %SECTIONS = ( 
     45    website     => 1, 
     46    devel       => 1, 
     47    docs        => 1, 
     48    archive     => 1, 
     49); 
     50 
     51 
    1752 
    1853use vars '$VERSION'; 
     
    163198    $instance->{request} = $request; 
    164199 
     200    $instance->{cur_year} = (gmtime(time))[5] + 1900; 
     201 
    165202    $instance->{rooturl} = '[% site.url.root %]'; 
    166203 
     
    169206    if ( $request->{query} ) { 
    170207 
    171         #  Limit by metaname 
    172         $request->{swish_query} = $request->{metaname} 
    173             ? "$request->{metaname}=( $request->{query} )" 
    174             : $request->{query}; 
    175  
    176         $instance->{result} = run_query( $instance ); 
     208        # Update to handle a bit more complex queries 
     209        generate_query( $request ); 
     210 
     211 
     212        $instance->{result} = $request->{message} 
     213            ? { message => $request->{message } } 
     214            : run_query( $instance ); 
    177215    } 
    178216 
     
    188226    print $fill_in_object->fill( scalarref => $output, fobject => $cgi ); 
    189227 
     228    warn sprintf("Query=[%s] Hits=[%d]\n", 
     229        $instance->{request}->{swish_query} || '',  
     230        $instance->{result}->{hits} || 0 ); 
     231 
    190232    delete $instance->{request};  # clean up the request 
    191233    delete $instance->{result}; 
     
    195237 
    196238 
     239 
     240 
     241 
     242 
     243 
     244 
     245=item generate_query 
     246 
     247Looks at request and build up query, sorts, and limits 
     248 
     249=cut 
     250 
     251sub generate_query { 
     252    my $request = shift; 
     253 
     254    my $query       = $request->{query}; 
     255    my $cgi         = $request->{cgi}; 
     256 
     257 
     258    # Limit query to a specific metaname? 
     259    if ( my $meta = $cgi->param('meta') ) { 
     260 
     261        $query = "$METANAMES{$meta}=($query)" if $METANAMES{$meta}; 
     262    } 
     263 
     264 
     265    # Limit the query to sections of the site 
     266 
     267    my $sections = join " or ", grep { $SECTIONS{$_} } $cgi->param('section'); 
     268    $query .= " section=$sections" if $sections; 
     269 
     270 
     271    $request->{swish_query} = $query; 
     272 
     273 
     274 
     275 
     276    # Set sort 
     277    if ( $cgi->param('sort') && (my $sort = $PROPERTIES{ $cgi->param('sort')}) ) { 
     278 
     279        my $reverse = $cgi->param('reverse') ? 1 : 0; 
     280        $reverse = ! $reverse if $REVERSE_SORT{ $sort }; 
     281 
     282        $sort .= ' desc' if $reverse; 
     283 
     284        $request->{sort} = $sort; 
     285    } 
     286 
     287 
     288    # Limit by dates 
     289    my $limit = $cgi->param('date') || return; 
     290 
     291 
     292    my $low     = DateTime->now; 
     293    my $high    = DateTime->now; 
     294 
     295    my @limit_range; 
     296 
     297    if ( $limit =~ /^(day|month|year|week)$/ ) { 
     298        @limit_range = ( 
     299            DateTime->now->truncate( to => $1 )->epoch, 
     300            time, 
     301        ); 
     302    } 
     303 
     304 
     305    if ( $limit =~ /^\d+$/ && $limit < 5000 ) { 
     306        @limit_range = ( 
     307            DateTime->now->truncate( to => $1 )->subtract( days => $limit ), 
     308            time, 
     309        ); 
     310    } 
     311 
     312    if ( $limit eq 'select' ) { 
     313 
     314        my %params; 
     315        for my $t ( 's', 'e' ) { 
     316            for my $x ( qw/ year month day / ) { 
     317                my $value = $cgi->param( $t . $x ); 
     318                next unless $value; 
     319                $params{$t}{$x} = $value; 
     320            } 
     321        } 
     322        unless ( $params{s}{year} && $params{e}{year} ) { 
     323            $request->{message} = 'Incomplete date submitted'; 
     324            return; 
     325        } 
     326 
     327 
     328        eval { 
     329            @limit_range = ( 
     330                DateTime->new( %{$params{s}} )->epoch, 
     331                DateTime->new( %{$params{e}} 
     332                    )->add( days => 1 )->subtract( seconds => 1 )->epoch, 
     333            ); 
     334        }; 
     335 
     336        if ( $@ ) { 
     337            warn "Bad date submitted $@\n"; 
     338            $request->{message} = "Invalid date submitted"; 
     339            @limit_range = (); 
     340        } else { 
     341            if ( $limit_range[0] > $limit_range[1] ) { 
     342                $request->{message} = "Start date must be before end date"; 
     343                @limit_range = (); 
     344            } 
     345        } 
     346    } 
     347 
     348 
     349    $request->{limit_search} = [ 'swishlastmodified' , @limit_range ] if @limit_range; 
     350} 
     351 
     352 
     353 
     354 
     355 
     356 
    197357# Subroutine to run the Swish query.  Returns a hash reference. 
    198358# A better design might be to return an object with methods for accessing the data. 
     
    218378 
    219379    # $$$ TODO need to test for index file being modified 
    220     # one trick is to touch public_html/seach/index.html and then speedy will reload. 
     380    # one trick is to touch this script and then speedy will reload. 
    221381 
    222382    if ( ! $swish ) { 
     
    240400 
    241401 
    242     # Run the search.  See SWISH::API for more options (like sorting) 
    243  
    244     my $results = $swish->Query( $request->{swish_query} ); 
     402    # Run the search 
     403    my $search = $swish->New_Search_Object; 
     404 
     405 
     406    $search->SetSort( $request->{sort} ) if $request->{sort}; 
     407    $search->SetSearchLimit( @{ $request->{limit_search} } ) if $request->{limit_search}; 
     408 
     409    my $results = $search->Execute( $request->{swish_query} ); 
    245410 
    246411    return { message => check_swish_error( $swish ) } if $swish->Error;