Author: kake Date: 2012-04-16 17:40:58 +0100 (Mon, 16 Apr 2012) New Revision: 1342
Added: trunk/t/95_multiple_index.t Modified: trunk/Changes trunk/MANIFEST trunk/lib/OpenGuides.pm trunk/templates/map_index_leaflet.tt trunk/templates/rdf_index.tt trunk/templates/site_index.tt Log: Added support for indexing on category and locale at the same time.
Modified: trunk/Changes =================================================================== --- trunk/Changes 2012-04-15 10:25:44 UTC (rev 1341) +++ trunk/Changes 2012-04-16 16:40:58 UTC (rev 1342) @@ -4,10 +4,14 @@ http://dev.openguides.org/log/trunk.
0.67 ? - action=index now uses a new form of parameters - instead of e.g. + Added support for indexing on category and locale at the same time. + As part of this, action=index now uses a new form of parameters, so + instead of e.g. action=index;index_type=locale;index_value=holborn you should use action=index;loc=holborn + or indeed + action=index;cat=pubs;loc=holborn Format params such as format=map work as before. Old-style URLs will automatically redirect to new-style ones. Added project metadata to Build.PL (#285).
Modified: trunk/MANIFEST =================================================================== --- trunk/MANIFEST 2012-04-15 10:25:44 UTC (rev 1341) +++ trunk/MANIFEST 2012-04-16 16:40:58 UTC (rev 1342) @@ -162,6 +162,7 @@ t/92_node_name_from_cgi_obj.t t/93_redirect_without_spaces.t t/94_leaflet.t +t/95_multiple_index.t t/templates/15_test.tt wiki.cgi META.json
Modified: trunk/lib/OpenGuides.pm =================================================================== --- trunk/lib/OpenGuides.pm 2012-04-15 10:25:44 UTC (rev 1341) +++ trunk/lib/OpenGuides.pm 2012-04-16 16:40:58 UTC (rev 1342) @@ -851,6 +851,12 @@ cat => "pubs", );
+ # Show all pubs in Holborn. + $guide->show_index( + cat => "pubs", + loc => "holborn", + ); + # RDF version of things in Locale: Holborn. $guide->show_index( loc => "Holborn", @@ -917,41 +923,55 @@ # OK, we either show everything, or do a new-style cat/loc search. my $cat = $args{cat} || ""; my $loc = $args{loc} || ""; - my ( $type, $value ); + my ( $type, $value, @names, @criteria ); if ( !$cat && !$loc ) { @selnodes = $wiki->list_all_nodes(); } else { + my ( @catnodes, @locnodes ); if ( $cat ) { - @selnodes = $wiki->list_nodes_by_metadata( + @catnodes = $wiki->list_nodes_by_metadata( metadata_type => "category", metadata_value => $cat, ignore_case => 1 ); - $type = "category"; - $value = $cat; - } else { - @selnodes = $wiki->list_nodes_by_metadata( + my $name = "Category " . ucfirst( $cat ); + push @criteria, { + type => "category", + value => $cat, + name => $name, + param => $formatter->node_name_to_node_param( $name ), + }; + push @names, $name; + } + if ( $loc ) { + @locnodes = $wiki->list_nodes_by_metadata( metadata_type => "locale", metadata_value => $loc, ignore_case => 1 ); - $type = "locale"; - $value = $loc; + my $name = "Locale " . ucfirst( $loc ); + push @criteria, { + type => "locale", + value => $loc, + name => $name, + param => $formatter->node_name_to_node_param( $name ), + }; + push @names, $name; } - my $name = ucfirst($type) . " $value"; - my $url = $self->config->script_name - . "?" - . ucfirst( $type ) - . "_" - . uri_escape( - $formatter->node_name_to_node_param($value) - ); - $tt_vars{criterion} = { - type => $type, - value => $value, # for RDF version - name => CGI->escapeHTML( $name ), - url => $url - }; + if ( $cat && !$loc ) { + @selnodes = @catnodes; + } elsif ( $loc && !$cat ) { + @selnodes = @locnodes; + } else { + # Intersect the category and locale results. + my %count = (); + foreach my $node ( @catnodes, @locnodes ) { $count{$node}++; } + foreach my $node ( keys %count ) { + push @selnodes, $node if $count{$node} > 1; + } + } + $tt_vars{criteria_title} = join( " and ", @names ); + $tt_vars{criteria} = @criteria; $tt_vars{not_editable} = 1; } }
Added: trunk/t/95_multiple_index.t =================================================================== --- trunk/t/95_multiple_index.t (rev 0) +++ trunk/t/95_multiple_index.t 2012-04-16 16:40:58 UTC (rev 1342) @@ -0,0 +1,107 @@ +use strict; +use OpenGuides; +use OpenGuides::CGI; +use OpenGuides::Test; +use Test::More; + +eval { require DBD::SQLite; }; +if ( $@ ) { + my ($error) = $@ =~ /^(.*?)\n/; + plan skip_all => + "DBD::SQLite could not be used - no database to test with. ($error)"; +} + +eval { require Test::HTML::Content; }; +if ( $@ ) { + plan skip_all => "Test::HTML::Content not available."; +} + +plan tests => 15; + +my $config = OpenGuides::Test->make_basic_config; +my $guide = OpenGuides->new( config => $config ); +my $wiki = $guide->wiki; + +# Clear out the database from any previous runs. +OpenGuides::Test::refresh_db(); + +# Write some nodes. +OpenGuides::Test->write_data( + guide => $guide, + node => "Red Lion", + locales => "Croydon", + categories => "Pubs", + return_output => 1, + ); + +OpenGuides::Test->write_data( + guide => $guide, + node => "Blue Lion", + locales => "Waddon", + categories => "Pubs", + return_output => 1, + ); + +OpenGuides::Test->write_data( + guide => $guide, + node => "Blue Ribbon", + locales => "Waddon", + categories => "Shops", + return_output => 1, + ); + +my %tt_vars = $guide->show_index( cat => "pubs", return_tt_vars => 1 ); +is( scalar @{$tt_vars{nodes}}, 2, + "Right number of nodes returned in pure category search" ); +my $output = $guide->show_index( cat => "pubs", return_output => 1 ); +Test::HTML::Content::title_ok( $output, "Index of Category Pubs - Test", + "...and page title is correct" ); +Test::HTML::Content::link_ok( $output, $config->script_name . "?Category_Pubs", + "...and we link to the category page." ); + +%tt_vars = $guide->show_index( loc => "waddon", return_tt_vars => 1 ); +is( scalar @{$tt_vars{nodes}}, 2, + "Right number of nodes returned in pure locale search" ); +$output = $guide->show_index( loc => "waddon", return_output => 1 ); +Test::HTML::Content::title_ok( $output, "Index of Locale Waddon - Test", + "...and page title is correct" ); +Test::HTML::Content::link_ok( $output, $config->script_name . "?Locale_Waddon", + "...and we link to the locale page." ); + +%tt_vars = $guide->show_index( cat => "pubs", loc => "waddon", + return_tt_vars => 1 ); +is( scalar @{$tt_vars{nodes}}, 1, + "Right number of nodes returned in category+locale search" ); +$output = $guide->show_index( cat => "pubs", loc => "waddon", + return_output => 1 ); +Test::HTML::Content::title_ok( $output, + "Index of Category Pubs and Locale Waddon - Test", + "...and page title is correct" ); +Test::HTML::Content::link_ok( $output, $config->script_name . "?Category_Pubs", + "...and we link to the category page." ); +Test::HTML::Content::link_ok( $output, $config->script_name . "?Locale_Waddon", + "...and we link to the locale page." ); + +# Test the map version. +$config->use_leaflet( 1 ); +%tt_vars = $guide->show_index( cat => "pubs", loc => "waddon", format => "map", + return_tt_vars => 1 ); +is( scalar @{$tt_vars{nodes}}, 1, + "Right number of nodes returned in category+locale search with map" ); +$output = $guide->show_index( cat => "pubs", loc => "waddon", format => "map", + return_output => 1 ); +Test::HTML::Content::title_ok( $output, + "Map of Category Pubs and Locale Waddon - Test", + "...and page title is correct" ); +Test::HTML::Content::link_ok( $output, $config->script_name . "?Category_Pubs", + "...and we link to the category page." ); +Test::HTML::Content::link_ok( $output, $config->script_name . "?Locale_Waddon", + "...and we link to the locale page." ); + +# Test the RDF version. +$output = $guide->show_index( cat => "pubs", loc => "waddon", format => "rdf", + return_output => 1 ); +like( $output, + qr|dc:titleCategory Pubs and Locale Waddon</dc:title>|, + "Page title is correct on RDF version." ); +
Modified: trunk/templates/map_index_leaflet.tt =================================================================== --- trunk/templates/map_index_leaflet.tt 2012-04-15 10:25:44 UTC (rev 1341) +++ trunk/templates/map_index_leaflet.tt 2012-04-16 16:40:58 UTC (rev 1342) @@ -1,9 +1,22 @@ -[% INCLUDE header.tt page_title = criterion.name || "Map of all nodes" %] +[% page_title = "Map of $criteria_title - $site_name" %] +[% INCLUDE header.tt %] [% INCLUDE banner.tt %] <div id="content">
<div id="maincontent">
+ <h2>Map of + [% IF criteria %] + [% links = [] %] + [% FOREACH criterion = criteria %] + [% links.push( '<a href="' _ cgi_url _ '?' _ criterion.param _ '">' _ criterion.name _ '</a>' ) %] + [% END %] + [% links.join( ' and ' ) %] + [% ELSE %] + all nodes + [% END %] + </h2> + <div id="map_index_node_list" style="height:400px; overflow:auto;float:right; width:30%"> <ul> [% i = 0 %]
Modified: trunk/templates/rdf_index.tt =================================================================== --- trunk/templates/rdf_index.tt 2012-04-15 10:25:44 UTC (rev 1341) +++ trunk/templates/rdf_index.tt 2012-04-16 16:40:58 UTC (rev 1342) @@ -10,8 +10,8 @@
<rdf:Description rdf:about=""> -[% IF criterion %] - dc:title[% CGI.escapeHTML(criterion.name) %]</dc:title> +[% IF criteria_title %] + dc:title[% CGI.escapeHTML( criteria_title ) %]</dc:title> [% ELSE %] dc:titleRDF index</dc:title> [% END %]
Modified: trunk/templates/site_index.tt =================================================================== --- trunk/templates/site_index.tt 2012-04-15 10:25:44 UTC (rev 1341) +++ trunk/templates/site_index.tt 2012-04-16 16:40:58 UTC (rev 1342) @@ -1,13 +1,17 @@ -[% IF criterion %] +[% my_feed_base = cgi_url _ '?action=index' %] + +[% FOREACH criterion = criteria %] [% IF criterion.type == 'category' %] - [% my_params = ';cat=' _ criterion.value.lower %] + [% my_feed_base = my_feed_base _ ';cat=' _ criterion.value.lower %] [% ELSIF criterion.type == 'locale' %] - [% my_params = ';loc=' _ criterion.value.lower %] + [% my_feed_base = my_feed_base _ ';loc=' _ criterion.value.lower %] [% END %] +[% END %]
+[% IF criteria %] [% INCLUDE header.tt - page_title = "Index of $criterion.name - $site_name" - feed_base = "$cgi_url?action=index" _ my_params + page_title = "Index of $criteria_title - $site_name" + feed_base = my_feed_base %] [% ELSE %] [% INCLUDE header.tt page_title = "Things within $limit of $origin - $site_name" %] @@ -17,13 +21,13 @@ [% INCLUDE navbar.tt %] <div id="maincontent"> <h2>Node List - [%- IF criterion -%] - [%- IF criterion.url -%] - - <a href="[% criterion.url %]">[%- criterion.name -%]</a> - [%- ELSE -%] - - [%- criterion.name -%] - [%- END -%] - [%- END -%] + [% IF criteria %] + [% links = [] %] + [% FOREACH criterion = criteria %] + [% links.push( '<a href="' _ cgi_url _ '?' _ criterion.param _ '">' _ criterion.name _ '</a>' ) %] + [% END %] + - [% links.join( ' and ' ) %] + [% END %] [%- IF origin -%] - Things within [%- limit -%] of <a href="[% cgi_url %]?[%- origin_param -%]">[%- origin -%]</a> [%- END -%] @@ -37,15 +41,14 @@ </ol> </div>
-[%# my_params is set at the top of this file %] +[%# my_feed_base is set at the top of this file %] <p> This list is available as - <a href="[% cgi_url %]?action=index[% my_params %];format=rdf">rdf</a>, + <a href="[% my_feed_base %];format=rdf">rdf</a>, or as a - <a href="[% cgi_url %]?action=index[% my_params %];format=rss">rss feed</a>, + <a href="[% my_feed_base %];format=rss">rss feed</a>, or as an - <a href="[% cgi_url %]?action=index[% my_params %];format=atom">atom - feed</a>. + <a href="[% my_feed_base %];format=atom">atom feed</a>. </p>
[% INCLUDE footer.tt %]
openguides-commits@lists.openguides.org