Author: kake Date: 2012-04-14 16:30:05 +0100 (Sat, 14 Apr 2012) New Revision: 1339
Modified: trunk/Changes trunk/lib/OpenGuides.pm trunk/lib/OpenGuides/Utils.pm trunk/t/12_macros.t trunk/t/53_show_index.t trunk/templates/node.tt trunk/templates/site_index.tt trunk/wiki.cgi Log: Switched to new form of parameters for action=index - this will let us extend the code to let people index on category and locale at the same time.
Modified: trunk/Changes =================================================================== --- trunk/Changes 2012-04-14 15:03:13 UTC (rev 1338) +++ trunk/Changes 2012-04-14 15:30:05 UTC (rev 1339) @@ -3,6 +3,14 @@ More detailed changelogs can be found at http://dev.openguides.org/log/trunk.
+0.67 ? + action=index now uses a new form of parameters - instead of e.g. + action=index;index_type=locale;index_value=holborn + you should use + action=index;loc=holborn + Format params such as format=map work as before. Old-style URLs + will automatically redirect to new-style ones. + 0.66 12 April 2012 All templates now have access to the "username" TT variable. Node name parameters in URLs are now accepted with spaces instead of
Modified: trunk/lib/OpenGuides/Utils.pm =================================================================== --- trunk/lib/OpenGuides/Utils.pm 2012-04-14 15:03:13 UTC (rev 1338) +++ trunk/lib/OpenGuides/Utils.pm 2012-04-14 15:30:05 UTC (rev 1339) @@ -130,8 +130,9 @@ if ( UNIVERSAL::isa( $_[0], "Wiki::Toolkit" ) ) { shift; # just throw it away } + my $type = ( lc( $_[0] ) eq "category" ) ? "cat" : "loc"; my $link_title = $_[2] || "View all pages in $_[0] $_[1]"; - return qq(<a href="$script_name?action=index;index_type=) . uri_escape(lc($_[0])) . qq(;index_value=) . uri_escape($_[1]) . qq(">$link_title</a>); + return qq(<a href="$script_name?action=index;$type=) . uri_escape( lc( $_[1] ) ) . qq(">$link_title</a>); }, qr/@INDEX_LIST\s+[[(Category|Locale)\s+([^]]+)]]/ => sub { @@ -168,9 +169,11 @@ if ( UNIVERSAL::isa( $_[0], "Wiki::Toolkit" ) ) { shift; # don't need $wiki } + + my $type = ( lc( $_[0] ) eq "category" ) ? "cat" : "loc"; my $link_title = $_[2] || "View map of pages in $_[0] $_[1]"; - return qq(<a href="$script_name?action=index;format=map;index_type=) . uri_escape(lc($_[0])) . qq(;index_value=) . uri_escape($_[1]) . qq(">$link_title</a>); + return qq(<a href="$script_name?action=index;format=map;$type=) . uri_escape( lc( $_[1] ) ) . qq(">$link_title</a>); }, qr/@RANDOM_PAGE_LINK(?:\s+[[(Category|Locale)\s+([^]|]+)|?([^]]+)?]])?/ => sub {
Modified: trunk/lib/OpenGuides.pm =================================================================== --- trunk/lib/OpenGuides.pm 2012-04-14 15:03:13 UTC (rev 1338) +++ trunk/lib/OpenGuides.pm 2012-04-14 15:30:05 UTC (rev 1339) @@ -846,43 +846,50 @@
=item B<show_index>
+ # Show everything in Category: Pubs. $guide->show_index( - type => "category", - value => "pubs", + cat => "pubs", );
- # RDF version. + # RDF version of things in Locale: Holborn. $guide->show_index( - type => "locale", - value => "Holborn", + loc => "Holborn", format => "rdf", );
# RSS / Atom version (recent changes style). $guide->show_index( - type => "locale", - value => "Holborn", + loc => "Holborn", format => "rss", );
# Or return output as a string (useful for writing tests). $guide->show_index( - type => "category", - value => "pubs", + cat => "pubs", return_output => 1, );
# Or return the template variables (again, useful for writing tests). $guide->show_index( - type => "category", - value => "pubs", + cat => "pubs", format => "map" return_tt_vars => 1, );
-If either the C<type> or the C<value> parameter is omitted, then all pages -will be returned. +If neither C<cat> or C<loc> is supplied, then all pages will be returned.
+The recommended format of parameters to this method changed to the +above in version 0.67 of OpenGuides, though older invocations are +still supported and will redirect to the new URL format. + +If you pass the C<return_output> or C<return_tt_vars> parameters, and a +redirect is required, this method will fake the redirect and return the +output/variables that will actually end up being viewed by the user. If +instead you want to see the HTTP headers that will be printed in order to +perform the redirect, pass the C<intercept_redirect> parameter as well. The +C<intercept_redirect> parameter has no effect if no redirect is required, or +if the C<return_output>/C<return_tt_vars> parameter is omitted. + =cut
sub show_index { @@ -904,29 +911,49 @@ }; $tt_vars{not_editable} = 1; } else { - @selnodes = $wiki->list_nodes_by_metadata( - metadata_type => $args{type}, - metadata_value => $args{value}, - ignore_case => 1 - ); - my $name = ucfirst($args{type}) . " $args{value}"; + return $self->_do_old_style_index_search( %args ); + } + } else { + # OK, we either show everything, or do a new-style cat/loc search. + my $cat = $args{cat} || ""; + my $loc = $args{loc} || ""; + my ( $type, $value ); + if ( !$cat && !$loc ) { + @selnodes = $wiki->list_all_nodes(); + } else { + if ( $cat ) { + @selnodes = $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( + metadata_type => "locale", + metadata_value => $loc, + ignore_case => 1 + ); + $type = "locale"; + $value = $loc; + } + my $name = ucfirst($type) . " $value"; my $url = $self->config->script_name . "?" - . ucfirst( $args{type} ) + . ucfirst( $type ) . "_" . uri_escape( - $formatter->node_name_to_node_param($args{value}) + $formatter->node_name_to_node_param($value) ); $tt_vars{criterion} = { - type => $args{type}, - value => $args{value}, # for RDF version + type => $type, + value => $value, # for RDF version name => CGI->escapeHTML( $name ), url => $url }; $tt_vars{not_editable} = 1; } - } else { - @selnodes = $wiki->list_all_nodes(); }
my @nodes = map { @@ -1024,10 +1051,15 @@ # They really wanted a recent changes style rss/atom feed my $feed_type = $args{format}; my ($feed,$content_type) = $self->get_feed_and_content_type($feed_type); - $feed->set_feed_name_and_url_params( - "Index of $args{type} $args{value}", - "action=index;index_type=$args{type};index_value=$args{value}" - ); + my ($name, $params ); + if ( $args{cat} ) { + $name = "Index of Category $args{cat}"; + $params = "action=index;cat=$args{cat}"; + } else { + $name = "Index of Locale $args{loc}"; + $params = "action=index;loc=$args{loc}"; + } + $feed->set_feed_name_and_url_params( $name, $params );
# Grab the actual node data out of @nodes my @node_data; @@ -1060,6 +1092,28 @@ print $output; }
+# Deal with legacy URLs/tests. +sub _do_old_style_index_search { + my ( $self, %args ) = @_; + if ( ( $args{return_output} || $args{return_tt_vars} ) ) { + if ( $args{intercept_redirect} ) { + return $self->redirect_index_search( %args ); + } else { + my $type = delete $args{type}; + my $value = delete $args{value}; + if ( $type eq "category" ) { + return $self->show_index( %args, cat => $value ); + } elsif ( $type eq "locale" ) { + return $self->show_index( %args, loc => $value ); + } else { + return $self->show_index( %args ); + } + } + } else { + print $self->redirect_index_search( %args ); + } +} + =item B<show_metadata>
$guide->show_metadata(); @@ -2324,6 +2378,29 @@ return OpenGuides::Template->output( %output_conf ); }
+# Redirection for legacy URLs. +sub redirect_index_search { + my ( $self, %args ) = @_; + my $type = lc( $args{type} ) || ""; + my $value = lc( $args{value} ) || ""; + my $format = lc( $args{format} ) || ""; + + my $script_url = $self->config->script_url; + my $script_name = $self->config->script_name; + + my $url = "$script_url$script_name?action=index"; + + if ( $type eq "category" ) { + $url .= ";cat=$value"; + } elsif ( $type eq "locale" ) { + $url .= ";loc=$value"; + } + if ( $format ) { + $url .= ";format=$format"; + } + return CGI->redirect( -uri => $url, -status => 301 ); +} + sub redirect_to_node { my ($self, $node, $redirected_from) = @_;
Modified: trunk/t/12_macros.t =================================================================== --- trunk/t/12_macros.t 2012-04-14 15:03:13 UTC (rev 1338) +++ trunk/t/12_macros.t 2012-04-14 15:30:05 UTC (rev 1339) @@ -11,7 +11,7 @@ plan skip_all => "DBD::SQLite could not be used - no database to test with. ($error)"; }
-plan tests => 15; +plan tests => 19;
SKIP: { # Clear out the database from any previous runs. @@ -34,11 +34,14 @@ ); like( $output, qr/View all pages in Category Foo/, "@INDEX_LINK has right default link text" ); + like( $output, qr/action=index;cat=foo/, "...and URL looks right" ); + $output = $guide->display_node( return_output => 1, id => "Test 2", ); - like( $output, qr/>Bars</a>/, "...and can be overridden" ); + like( $output, qr/>Bars</a>/, "Default link text can be overridden" ); + like( $output, qr/action=index;cat=bar/, "...and URL looks right" );
# Test @INDEX_LIST $wiki->write_node( "Test 3", "@INDEX_LIST [[Category Foo]]" ) @@ -100,11 +103,14 @@ ); like( $output, qr/View map of pages in Category Foo/, "@MAP_LINK has right default link text" ); + like( $output, qr/\bcat=foo\b/, "...and URL looks right" ); + $output = $guide->display_node( return_output => 1, id => "Test 2", ); - like( $output, qr/>Map</a>/, "...and can be overridden" ); + like( $output, qr/>Map</a>/, "Default link text can be overridden" ); + like( $output, qr/\bcat=foo\b/, "...and URL looks right" );
# Test @RANDOM_PAGE_LINK OpenGuides::Test->write_data(
Modified: trunk/t/53_show_index.t =================================================================== --- trunk/t/53_show_index.t 2012-04-14 15:03:13 UTC (rev 1338) +++ trunk/t/53_show_index.t 2012-04-14 15:30:05 UTC (rev 1339) @@ -11,7 +11,7 @@ plan skip_all => "DBD::SQLite could not be used - no database to test with ($error)"; }
-plan tests => 27; # 29 when all enabled +plan tests => 40;
# Clear out the database from any previous runs. OpenGuides::Test::refresh_db(); @@ -26,29 +26,64 @@
$wiki->write_node( "Test Page", "foo", undef, - { category => "Alpha", latitude=>51.754349, longitude=>-1.258200 } ) + { category => "Alpha", locale => "Assam", + latitude => 51.754349, longitude => -1.258200 } ) or die "Couldn't write node"; $wiki->write_node( "Test Page 2", "foo", undef, - { category => "Alpha" } ) + { category => "Alpha", locale => "Assam" } ) or die "Couldn't write node"; +$wiki->write_node( "Test Page 3", "foo", undef, + { category => "Beta", locale => "Bangalore", + latitude => 51.8, longitude => -0.8 } ) + or die "Couldn't write node";
+# Make sure that old-style invocations redirect to new. +my $output = $guide->show_index( type => "category", value => "Alpha", + return_output => 1, intercept_redirect => 1 ); +like( $output, qr/Status: 301/, + "Old-style category index search prints a redirect" ); +like( $output, qr/cat=alpha/, "...and includes the correct param/value pair" ); + +$output = $guide->show_index( type => "locale", value => "Assam", + return_output => 1, intercept_redirect => 1, + format => "map" ); +like( $output, qr/Status: 301/, + "Old-style locale index search prints a redirect" ); +like( $output, qr/loc=assam/, "...and includes the correct param/value pair" ); +like( $output, qr/format=map/, "...format parameter included too" ); + # Test the normal, HTML version -my $output = eval { +$output = eval { $guide->show_index( - type => "category", - value => "Alpha", + cat => "Alpha", return_output => 1, ); }; is( $@, "", "->show_index doesn't die" ); -like( $output, qr|wiki.cgi?Test_Page|, - "...and includes correct links" ); +like( $output, qr|wiki.cgi?Test_Page|, "...and includes correct links" ); +unlike( $output, qr|wiki.cgi?Test_Page_3|, "...but not incorrect ones" ); unlike( $output, qr|<title>\s*-|, "...sets <title> correctly" );
+# Test links in the header. +like( $output, qr|<link rel="alternate[^>]*action=index;cat=alpha;format=rss|, + "RSS link correct in header" ); +like( $output, qr|<link rel="alternate[^>]*action=index;cat=alpha;format=atom|, + "Atom link correct in header" ); + +# Test links in the footer. +my $footer = $output; +$footer =~ s/^.*This list is available as//s; +$footer =~ s|</p>.*$||s; +like( $footer, qr|action=index;cat=alpha;format=rdf|, + "RDF link correct in footer" ); +like( $footer, qr|action=index;cat=alpha;format=rss|, + "RSS link correct in footer" ); +like( $footer, qr|action=index;cat=alpha;format=atom|, + "Atom link correct in footer" ); + # Test the RDF version $output = $guide->show_index( - type => "category", - value => "Alpha", + cat => "Alpha", return_output => 1, format => "rdf" ); @@ -62,8 +97,7 @@ # Test the RSS version $output = eval { $guide->show_index( - type => "category", - value => "Alpha", + cat => "Alpha", return_output => 1, format => "rss", ); @@ -72,15 +106,15 @@ like( $output, qr|Content-Type: application/rdf+xml|, "RSS output gets content-type of application/rdf+xml" ); like( $output, "/<rdf:RDF.*?http://purl.org/rss//s", "Really is rss" ); -#like( $output, qr|<title>Category Alpha</title>|, "Right rss title" ); +like( $output, qr|<title>Test - Index of Category Alpha</title>|, + "Right rss title" ); @entries = ($output =~ /(</item>)/g); is( 2, scalar @entries, "Right number of nodes included in rss" );
# Test the Atom version $output = eval { $guide->show_index( - type => "category", - value => "Alpha", + cat => "Alpha", return_output => 1, format => "atom", ); @@ -89,7 +123,8 @@ like( $output, qr|Content-Type: application/atom+xml|, "Atom output gets content-type of application/atom+xml" ); like( $output, qr|<feed|, "Really is atom" ); -#like( $output, qr|<title>Category Alpha</title>|, "Right atom title" ); +like( $output, qr|<title>Test - Index of Category Alpha</title>|, + "Right atom title" ); @entries = ($output =~ /(<entry>)/g); is( 2, scalar @entries, "Right number of nodes included in atom" );
@@ -116,6 +151,7 @@ $output = eval { $guide->show_index( return_output => 1, + loc => "assam", format => "map", ); };
Modified: trunk/templates/node.tt =================================================================== --- trunk/templates/node.tt 2012-04-14 15:03:13 UTC (rev 1338) +++ trunk/templates/node.tt 2012-04-14 15:30:05 UTC (rev 1339) @@ -72,7 +72,12 @@ <div id="node_title"> [% IF current %] [% IF config.backlinks_in_title AND is_indexable_node %] - <h2 class="node_name"><a href="[% cgi_url %]?action=index;index_type=[% CGI.escape(index_type) %];index_value=[% CGI.escape(index_value) %]">[% node_name %]</a></h2> + [% IF index_type == 'category' %] + [% params = ';cat=' _ index_value.lower | html %] + [% ELSIF index_type == 'locale' %] + [% params = ';loc=' _ index_value.lower | html %] + [% END %] + <h2 class="node_name"><a href="[% cgi_url %]?action=index[% params %]">[% node_name %]</a></h2> [% ELSIF config.backlinks_in_title %] <h2 class="node_name"><a href="[% cgi_url %]?action=show_backlinks;id=[% CGI.escape(node_name) %]">[% node_name %]</a></h2> [% ELSE %]
Modified: trunk/templates/site_index.tt =================================================================== --- trunk/templates/site_index.tt 2012-04-14 15:03:13 UTC (rev 1338) +++ trunk/templates/site_index.tt 2012-04-14 15:30:05 UTC (rev 1339) @@ -1,7 +1,13 @@ [% IF criterion %] + [% IF criterion.type == 'category' %] + [% my_params = ';cat=' _ criterion.value.lower %] + [% ELSIF criterion.type == 'locale' %] + [% my_params = ';loc=' _ criterion.value.lower %] + [% END %] + [% INCLUDE header.tt page_title = "Index of $criterion.name - $site_name" - feed_base = "$cgi_url?action=index;index_type=$criterion.type;index_value=$criterion.value" + feed_base = "$cgi_url?action=index" _ my_params %] [% ELSE %] [% INCLUDE header.tt page_title = "Things within $limit of $origin - $site_name" %] @@ -31,13 +37,15 @@ </ol> </div>
+[%# my_params is set at the top of this file %] <p> This list is available as - <a href="[% cgi_url %]?action=index;index_type=[% criterion.type %];index_value=[% criterion.value %];format=rdf">rdf</a>, + <a href="[% cgi_url %]?action=index[% my_params %];format=rdf">rdf</a>, or as a - <a href="[% cgi_url %]?action=index;index_type=[% criterion.type %];index_value=[% criterion.value %];format=rss">rss feed</a>, + <a href="[% cgi_url %]?action=index[% my_params %];format=rss">rss feed</a>, or as an - <a href="[% cgi_url %]?action=index;index_type=[% criterion.type %];index_value=[% criterion.value %];format=atom">atom feed</a>. + <a href="[% cgi_url %]?action=index[% my_params %];format=atom">atom + feed</a>. </p>
[% INCLUDE footer.tt %]
Modified: trunk/wiki.cgi =================================================================== --- trunk/wiki.cgi 2012-04-14 15:03:13 UTC (rev 1338) +++ trunk/wiki.cgi 2012-04-14 15:30:05 UTC (rev 1339) @@ -85,9 +85,12 @@ show_needing_moderation(); } elsif ($action eq 'index') { $guide->show_index( + cat => $q->param( "cat" ) || "", + loc => $q->param( "loc" ) || "", + format => $format, + # Next two for backwards compatibility (deprecated) type => $q->param("index_type") || "Full", value => $q->param("index_value") || "", - format => $format, ); } elsif ($action eq 'random') { print $guide->display_random_page(