Changeset 1066

Show
Ignore:
Timestamp:
06/11/07 04:06:21 (1 year ago)
Author:
kake
Message:

Refactor redirect detection into OpenGuides::Utils->detect_redirect

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/lib/OpenGuides.pm

    r1064 r1066  
    269269                                . "?id="; 
    270270    } 
    271      
    272     if ( $node_data{content} && $node_data{content} =~ /^#REDIRECT\s+(.+?)\s*$/ ) { 
    273         my $redirect = $1; 
    274         # Strip off enclosing [[ ]] in case this is an extended link. 
    275         $redirect =~ s/^\[\[//; 
    276         $redirect =~ s/\]\]\s*$//; 
    277  
     271 
     272    my $redirect = OpenGuides::Utils->detect_redirect( 
     273                                              content => $node_data{content} ); 
     274    if ( $redirect ) { 
    278275        # Don't redirect if the parameter "redirect" is given as 0. 
    279276        if ($do_redirect == 0) { 
  • trunk/lib/OpenGuides/RDF.pm

    r1060 r1066  
    22 
    33use strict; 
     4 
     5use OpenGuides::Utils; 
    46 
    57use vars qw( $VERSION ); 
     
    140142                                                        $tt_vars{version} ); 
    141143 
    142     # Should probably be moved into OpenGuides::Utils. 
    143     if ($node_data{content} =~ /^\#REDIRECT \[\[(.*?)]\]$/) { 
    144         my $redirect = $1; 
     144    my $redirect = OpenGuides::Utils->detect_redirect( content => 
     145                                                         $node_data{content} ); 
     146    if ( $redirect ) { 
    145147        $tt_vars{redirect} = $config->script_url . $config->script_name 
    146148                             . "?id=" 
  • trunk/lib/OpenGuides/Utils.pm

    r1032 r1066  
    329329} 
    330330 
     331=item B<detect_redirect> 
     332 
     333    $redir = OpenGuides::Utils->detect_redirect( content => "foo" ); 
     334 
     335Checks the content of a node to see if the node is a redirect to another 
     336node.  If so, returns the name of the node that this one redirects to.  If 
     337not, returns false. 
     338 
     339(Also returns false if no content is provided.) 
     340 
     341=cut 
     342 
     343sub detect_redirect { 
     344    my ( $self, %args ) = @_; 
     345    return unless $args{content}; 
     346 
     347    if ( $args{content} =~ /^#REDIRECT\s+(.+?)\s*$/ ) { 
     348        my $redirect = $1; 
     349 
     350        # Strip off enclosing [[ ]] in case this is an extended link. 
     351        $redirect =~ s/^\[\[//; 
     352        $redirect =~ s/\]\]\s*$//; 
     353 
     354        return $redirect; 
     355    } 
     356} 
     357 
    331358=back 
    332359 
  • trunk/t/11_utils.t

    r958 r1066  
    33use OpenGuides::Config; 
    44use OpenGuides::Utils; 
    5 use Test::More tests => 8
     5use Test::More tests => 10
    66 
    77eval { my $wiki = OpenGuides::Utils->make_wiki_object; }; 
     
    2929 
    3030SKIP: { 
    31     skip "DBD::SQLite could not be used - no database to test with. ($sqlite_error)", 5 
     31    skip "DBD::SQLite could not be used - no database to test with. " 
     32         . "($sqlite_error)", 7 
    3233      unless $have_sqlite; 
    3334 
     
    6061    ok( $wiki->search_obj, "...and search defined" ); 
    6162    ok( $wiki->formatter,  "...and formatter defined" ); 
     63 
     64    # Now test ->detect_redirect 
     65    is( OpenGuides::Utils->detect_redirect( content => "#REDIRECT [[Foo]]" ), 
     66        "Foo", 
     67        "->detect_redirect successfully detects redirect content" ); 
     68    ok( !OpenGuides::Utils->detect_redirect( content => "Mmmm, tea." ), 
     69        "...and successfully detects non-redirect content" ); 
    6270}