Changeset 1889 for swish_website/src/search
- Timestamp:
- 02/06/07 14:53:36 (2 years ago)
- Files:
-
- swish_website/src/search/.htaccess (deleted)
- swish_website/src/search/index.html (modified) (8 diffs)
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' %] 3 4 4 5 package SwishAPISearch; 5 6 use strict; 6 7 use warnings; 8 use DateTime; 9 10 7 11 8 12 ###################################################################### 9 13 # Skeleton CGI script for searching a Swsih-e index with SWISH::API. 10 # see below for documen ation or run "perldoc search.cgi"14 # see below for documentation or run "perldoc search.cgi" 11 15 # 12 16 # Copyright 2003, 2004 Bill Moseley - All rights reserved. … … 15 19 # 16 20 ####################################################################### 21 22 23 # Lookup hash for which metanames are defined 24 our %METANAMES = ( 25 title => 'swishtitle', 26 name => 'name', 27 email => 'email', 28 ); 29 30 our %PROPERTIES = ( 31 name => 'name', 32 email => 'email', 33 title => 'swishtitle', 34 rank => 'swishrank', 35 date => 'swishlastmodified', 36 ); 37 38 our %REVERSE_SORT = ( 39 swishrank => 1, 40 swishlastmodified => 1, 41 ); 42 43 # Valid section metaname values -- to limit to sections of the site. 44 our %SECTIONS = ( 45 website => 1, 46 devel => 1, 47 docs => 1, 48 archive => 1, 49 ); 50 51 17 52 18 53 use vars '$VERSION'; … … 163 198 $instance->{request} = $request; 164 199 200 $instance->{cur_year} = (gmtime(time))[5] + 1900; 201 165 202 $instance->{rooturl} = '[% site.url.root %]'; 166 203 … … 169 206 if ( $request->{query} ) { 170 207 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 ); 177 215 } 178 216 … … 188 226 print $fill_in_object->fill( scalarref => $output, fobject => $cgi ); 189 227 228 warn sprintf("Query=[%s] Hits=[%d]\n", 229 $instance->{request}->{swish_query} || '', 230 $instance->{result}->{hits} || 0 ); 231 190 232 delete $instance->{request}; # clean up the request 191 233 delete $instance->{result}; … … 195 237 196 238 239 240 241 242 243 244 245 =item generate_query 246 247 Looks at request and build up query, sorts, and limits 248 249 =cut 250 251 sub 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 197 357 # Subroutine to run the Swish query. Returns a hash reference. 198 358 # A better design might be to return an object with methods for accessing the data. … … 218 378 219 379 # $$$ TODO need to test for index file being modified 220 # one trick is to touch public_html/seach/index.htmland then speedy will reload.380 # one trick is to touch this script and then speedy will reload. 221 381 222 382 if ( ! $swish ) { … … 240 400 241 401 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} ); 245 410 246 411 return { message => check_swish_error( $swish ) } if $swish->Error;
