lib/TWiki/Store/SearchAlgorithms/PurePerl.pm
author Colas Nahaboo <colas@nahaboo.net>
Sat, 26 Jan 2008 15:50:53 +0100
changeset 0 414e01d06fd5
permissions -rw-r--r--
RELEASE 4.2.0 freetown
colas@0
     1
#
colas@0
     2
# Copyright (C) 2007 TWiki Contributors. All Rights Reserved.
colas@0
     3
# TWiki Contributors are listed in the AUTHORS file in the root
colas@0
     4
# of this distribution. NOTE: Please extend that file, not this notice.
colas@0
     5
#
colas@0
     6
# This program is free software; you can redistribute it and/or
colas@0
     7
# modify it under the terms of the GNU General Public License
colas@0
     8
# as published by the Free Software Foundation; either version 2
colas@0
     9
# of the License, or (at your option) any later version. For
colas@0
    10
# more details read LICENSE in the root of this distribution.
colas@0
    11
#
colas@0
    12
# This program is distributed in the hope that it will be useful,
colas@0
    13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
colas@0
    14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
colas@0
    15
#
colas@0
    16
# As per the GPL, removal of this notice is prohibited.
colas@0
    17
#
colas@0
    18
colas@0
    19
package TWiki::Store::SearchAlgorithms::PurePerl;
colas@0
    20
colas@0
    21
=pod
colas@0
    22
colas@0
    23
---+ package TWiki::Store::SearchAlgorithms::PurePerl
colas@0
    24
colas@0
    25
Pure perl implementation of the RCS cache search.
colas@0
    26
colas@0
    27
---++ search($searchString, $topics, $options, $sDir) -> \%seen
colas@0
    28
Search .txt files in $dir for $string. See RcsFile::searchInWebContent
colas@0
    29
for details.
colas@0
    30
colas@0
    31
=cut
colas@0
    32
colas@0
    33
sub search {
colas@0
    34
    my ($searchString, $topics, $options, $sDir) = @_;
colas@0
    35
colas@0
    36
    local $/ = "\n";
colas@0
    37
    my %seen;
colas@0
    38
    if ($options->{type} && $options->{type} eq 'regex') {
colas@0
    39
        # Escape /, used as delimiter. This also blocks any attempt to use
colas@0
    40
        # the search string to execute programs on the server.
colas@0
    41
        $searchString =~ s!/!\\/!g;
colas@0
    42
    } else {
colas@0
    43
        # Escape non-word chars in search string for plain text search
colas@0
    44
        $searchString =~ s/(\W)/\\$1/g;
colas@0
    45
    }
colas@0
    46
    # Convert GNU grep \< \> syntax to \b
colas@0
    47
    $searchString =~ s/(?<!\\)\\[<>]/\\b/g;
colas@0
    48
    $searchString =~ s/^(.*)$/\\b$1\\b/go if $options->{'wordboundaries'};
colas@0
    49
    my $match_code = "return \$_[0] =~ m/$searchString/o";
colas@0
    50
    $match_code .= 'i' unless ($options->{casesensitive});
colas@0
    51
    my $doMatch = eval "sub { $match_code }";
colas@0
    52
  FILE:
colas@0
    53
    foreach my $file ( @$topics ) {
colas@0
    54
        next unless open(FILE, "<$sDir/$file.txt");
colas@0
    55
        while (my $line = <FILE>) {
colas@0
    56
            if (&$doMatch($line)) {
colas@0
    57
                chomp($line);
colas@0
    58
                push( @{$seen{$file}}, $line );
colas@0
    59
                if ($options->{files_without_match}) {
colas@0
    60
                    close(FILE);
colas@0
    61
                    next FILE;
colas@0
    62
                }
colas@0
    63
            }
colas@0
    64
        }
colas@0
    65
        close(FILE);
colas@0
    66
    }
colas@0
    67
    return \%seen;
colas@0
    68
}
colas@0
    69
colas@0
    70
1;