Changeset 1023

Show
Ignore:
Timestamp:
04/21/07 14:07:37 (19 months ago)
Author:
kake
Message:

Added format => "raw" option to OpenGuides::Search->run to let you get your results back as a hash.

Location:
trunk
Files:
1 added
3 modified

Legend:

Unmodified
Added
Removed
  • trunk/Changes

    r1022 r1023  
    1515          the whole form. 
    1616        Added links to the "revision N" and "Last edited" text in the navbar. 
     17        Added format => "raw" option to OpenGuides::Search->run to let you get 
     18          your results back as a hash. 
    1719        Add JavaScript to the Create New Page page so the prefilled 
    1820          "New page name" disappears when you click in the field (it doesn't 
  • trunk/MANIFEST

    r1020 r1023  
    9393t/27_geo_data_edit_form.t 
    9494t/28_wgs84_coords.t 
     95t/30_search_raw.t 
    9596t/31_search.t 
    9697t/32_search_simple_metadata.t 
  • trunk/lib/OpenGuides/Search.pm

    r992 r1023  
    119119want to use them in production. 
    120120 
     121You can also request just the raw search results: 
     122 
     123  my %results = $search->run( 
     124                              os_x    => 528864, 
     125                              os_y    => 180797, 
     126                              os_dist => 750, 
     127                              format  => "raw", 
     128                            ); 
     129 
     130Results are returned as a hash, keyed on the page name.  All results 
     131are returned, not just the first C<page>.  The values in the hash are 
     132hashes themselves, with the following key/value pairs: 
     133 
     134=over 4 
     135 
     136=item * name 
     137 
     138=item * wgs84_lat - WGS-84 latitude 
     139 
     140=item * wgs84_long - WGS-84 longitude 
     141 
     142=item * summary 
     143 
     144=item * distance - distance (in metres) from origin, if origin exists 
     145 
     146=item * score - relevance to search string, if search string exists; higher score means more relevance 
     147 
     148=back 
    121149 
    122150In case you're struggling to follow the code, it does the following: 
     
    140168    $self->{return_output}  = $args{return_output}  || 0; 
    141169    $self->{return_tt_vars} = $args{return_tt_vars} || 0; 
     170 
     171    my $want_raw; 
     172    if ( $args{format} && $args{format} eq "raw" ) { 
     173        $want_raw = 1; 
     174    } 
    142175 
    143176    $self->process_params( $args{vars} ); 
     
    185218    } 
    186219 
    187     # If we're not doing a search then just print the search form. 
    188     unless ( $doing_search ) { 
    189         return $self->process_template( tt_vars => \%tt_vars ); 
     220    # If we're not doing a search then just print the search form (or return 
     221    # an empty hash if we were asked for raw results). 
     222    if ( !$doing_search ) { 
     223        if ( $want_raw ) { 
     224            return ( ); 
     225        } else { 
     226            return $self->process_template( tt_vars => \%tt_vars ); 
     227        } 
    190228    } 
    191229 
     
    208246 
    209247    my %results_hash = %{ $self->{results} || [] }; 
     248 
     249    # If we were asked for just the raw results, return them now, after 
     250    # grabbing additional info. 
     251    if ( $want_raw ) { 
     252        foreach my $node ( keys %results_hash ) { 
     253            my %data = $self->wiki->retrieve_node( $node ); 
     254            $results_hash{$node}{summary} = $data{metadata}{summary}[0]; 
     255            my $lat  = $data{metadata}{latitude}[0]; 
     256            my $long = $data{metadata}{longitude}[0]; 
     257            my ( $wgs84_lat, $wgs84_long ) = OpenGuides::Utils->get_wgs84_coords( latitude => $lat, longitude => $long, config => $self->config ); 
     258            $results_hash{$node}{wgs84_lat} = $wgs84_lat; 
     259            $results_hash{$node}{wgs84_long} = $wgs84_long; 
     260        } 
     261        return %results_hash; 
     262    } 
     263 
    210264    my @results = values %results_hash; 
    211265    my $numres = scalar @results; 
     
    605659    delete $self->{distance_in_metres}; 
    606660    delete $self->{search_string}; 
     661    delete $self->{results}; 
    607662 
    608663    # Strip out any non-digits from distance and OS co-ords.