Author: kake Date: 2007-06-11 04:06:21 +0100 (Mon, 11 Jun 2007) New Revision: 1066
Modified: trunk/lib/OpenGuides.pm trunk/lib/OpenGuides/RDF.pm trunk/lib/OpenGuides/Utils.pm trunk/t/11_utils.t Log: Refactor redirect detection into OpenGuides::Utils->detect_redirect
Modified: trunk/lib/OpenGuides/RDF.pm =================================================================== --- trunk/lib/OpenGuides/RDF.pm 2007-06-11 02:26:08 UTC (rev 1065) +++ trunk/lib/OpenGuides/RDF.pm 2007-06-11 03:06:21 UTC (rev 1066) @@ -2,6 +2,8 @@
use strict;
+use OpenGuides::Utils; + use vars qw( $VERSION ); $VERSION = '0.09';
@@ -139,9 +141,9 @@ = $self->{make_node_url}->( $node_name, $tt_vars{version} );
- # Should probably be moved into OpenGuides::Utils. - if ($node_data{content} =~ /^#REDIRECT [[(.*?)]]$/) { - my $redirect = $1; + my $redirect = OpenGuides::Utils->detect_redirect( content => + $node_data{content} ); + if ( $redirect ) { $tt_vars{redirect} = $config->script_url . $config->script_name . "?id=" . $formatter->node_name_to_node_param( $redirect )
Modified: trunk/lib/OpenGuides/Utils.pm =================================================================== --- trunk/lib/OpenGuides/Utils.pm 2007-06-11 02:26:08 UTC (rev 1065) +++ trunk/lib/OpenGuides/Utils.pm 2007-06-11 03:06:21 UTC (rev 1066) @@ -328,6 +328,33 @@ } }
+=item B<detect_redirect> + + $redir = OpenGuides::Utils->detect_redirect( content => "foo" ); + +Checks the content of a node to see if the node is a redirect to another +node. If so, returns the name of the node that this one redirects to. If +not, returns false. + +(Also returns false if no content is provided.) + +=cut + +sub detect_redirect { + my ( $self, %args ) = @_; + return unless $args{content}; + + if ( $args{content} =~ /^#REDIRECT\s+(.+?)\s*$/ ) { + my $redirect = $1; + + # Strip off enclosing [[ ]] in case this is an extended link. + $redirect =~ s/^[[//; + $redirect =~ s/]]\s*$//; + + return $redirect; + } +} + =back
=head1 AUTHOR
Modified: trunk/lib/OpenGuides.pm =================================================================== --- trunk/lib/OpenGuides.pm 2007-06-11 02:26:08 UTC (rev 1065) +++ trunk/lib/OpenGuides.pm 2007-06-11 03:06:21 UTC (rev 1066) @@ -268,13 +268,10 @@ $tt_vars{catloc_link} = $config->script_url . $config->script_name . "?id="; } - - if ( $node_data{content} && $node_data{content} =~ /^#REDIRECT\s+(.+?)\s*$/ ) { - my $redirect = $1; - # Strip off enclosing [[ ]] in case this is an extended link. - $redirect =~ s/^[[//; - $redirect =~ s/]]\s*$//;
+ my $redirect = OpenGuides::Utils->detect_redirect( + content => $node_data{content} ); + if ( $redirect ) { # Don't redirect if the parameter "redirect" is given as 0. if ($do_redirect == 0) { return %tt_vars if $args{return_tt_vars};
Modified: trunk/t/11_utils.t =================================================================== --- trunk/t/11_utils.t 2007-06-11 02:26:08 UTC (rev 1065) +++ trunk/t/11_utils.t 2007-06-11 03:06:21 UTC (rev 1066) @@ -2,7 +2,7 @@ use Wiki::Toolkit::Setup::SQLite; use OpenGuides::Config; use OpenGuides::Utils; -use Test::More tests => 8; +use Test::More tests => 10;
eval { my $wiki = OpenGuides::Utils->make_wiki_object; }; ok( $@, "->make_wiki_object croaks if no config param supplied" ); @@ -28,7 +28,8 @@ }
SKIP: { - skip "DBD::SQLite could not be used - no database to test with. ($sqlite_error)", 5 + skip "DBD::SQLite could not be used - no database to test with. " + . "($sqlite_error)", 7 unless $have_sqlite;
# Clear out the database from any previous runs. @@ -59,4 +60,11 @@ ok( $wiki->store, "...and store defined" ); ok( $wiki->search_obj, "...and search defined" ); ok( $wiki->formatter, "...and formatter defined" ); + + # Now test ->detect_redirect + is( OpenGuides::Utils->detect_redirect( content => "#REDIRECT [[Foo]]" ), + "Foo", + "->detect_redirect successfully detects redirect content" ); + ok( !OpenGuides::Utils->detect_redirect( content => "Mmmm, tea." ), + "...and successfully detects non-redirect content" ); }