| 1 | package Wiki::Toolkit::Search::DBIxFTS; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use DBIx::FullTextSearch; | ||
| 5 | use Carp "croak"; | ||
| 6 | use base 'Wiki::Toolkit::Search::Base'; | ||
| 7 | |||
| 8 | use vars qw( @ISA $VERSION ); | ||
| 9 | |||
| 10 | $VERSION = 0.05; | ||
| 11 | |||
| 12 | =head1 NAME | ||
| 13 | |||
| 14 | Wiki::Toolkit::Search::DBIxFTS - DBIx::FullTextSearch search plugin for Wiki::Toolkit. | ||
| 15 | |||
| 16 | =head1 REQUIRES | ||
| 17 | |||
| 18 | DBIx::FullTextSearch | ||
| 19 | |||
| 20 | =head1 SYNOPSIS | ||
| 21 | |||
| 22 | my $store = Wiki::Toolkit::Store::MySQL->new( | ||
| 23 | dbname => "wiki", dbpass=>"wiki" ); | ||
| 24 | my $search = Wiki::Toolkit::Search::DBIxFTS->new( dbh => $store->dbh ); | ||
| 25 | my %wombat_nodes = $search->search_nodes("wombat"); | ||
| 26 | |||
| 27 | Provides search-related methods for Wiki::Toolkit. | ||
| 28 | |||
| 29 | See also L<Wiki::Toolkit::Search::Base>, for methods not documented here. | ||
| 30 | |||
| 31 | =cut | ||
| 32 | |||
| 33 | =head1 METHODS | ||
| 34 | |||
| 35 | =over 4 | ||
| 36 | |||
| 37 | =item B<new> | ||
| 38 | |||
| 39 | my $search = Wiki::Toolkit::Search::DBIxFTS->new( dbh => $dbh ); | ||
| 40 | |||
| 41 | You must supply a handle to a database that has the | ||
| 42 | DBIx::FullTextSearch indexes already set up. (Currently though there's | ||
| 43 | no checking that what you supply is even a database handle at all, let | ||
| 44 | alone one that is compatible with DBIx::FullTextSearch.) | ||
| 45 | |||
| 46 | =cut | ||
| 47 | |||
| 48 | sub _init { | ||
| 49 | my ($self, %args) = @_; | ||
| 50 | croak "Must supply a database handle" unless $args{dbh}; | ||
| 51 | $self->{_dbh} = $args{dbh}; | ||
| 52 | return $self; | ||
| 53 | } | ||
| 54 | |||
| 55 | # We can't use the base version, since we're doing the analysis | ||
| 56 | # differently between searching and indexing | ||
| 57 | sub search_nodes { | ||
| 58 | my ($self, $termstr, $and_or) = @_; | ||
| 59 | |||
| 60 | $and_or = uc($and_or); | ||
| 61 | unless ( defined $and_or and $and_or eq "OR" ) { | ||
| 62 | $and_or = "AND"; | ||
| 63 | } | ||
| 64 | |||
| 65 | # Extract individual search terms - first phrases (between double quotes). | ||
| 66 | my @terms = ($termstr =~ m/"([^"]+)"/g); | ||
| 67 | $termstr =~ s/"[^"]*"//g; | ||
| 68 | # And now the phrases are gone, just split on whitespace. | ||
| 69 | push @terms, split(/\s+/, $termstr); | ||
| 70 | |||
| 71 | # If this is an AND search, tell DBIx::FTS we want every term. | ||
| 72 | @terms = map { "+$_" } @terms if $and_or eq "AND"; | ||
| 73 | |||
| 74 | # Open and perform the FTS. | ||
| 75 | my $dbh = $self->{_dbh}; | ||
| 76 | my $fts = DBIx::FullTextSearch->open($dbh, "_content_and_title_fts"); | ||
| 77 | my @finds = $fts->econtains(@terms); | ||
| 78 | |||
| 79 | # Well, no scoring yet, you see. | ||
| 80 | return map { $_ => 1 } @finds; | ||
| 81 | } | ||
| 82 | |||
| 83 | sub index_node { | ||
| 84 | my ($self, $node) = @_; | ||
| 85 | |||
| 86 | my $dbh = $self->{_dbh}; | ||
| 87 | my $fts_all = DBIx::FullTextSearch->open($dbh, "_content_and_title_fts"); | ||
| 88 | my $fts_titles = DBIx::FullTextSearch->open($dbh, "_title_fts"); | ||
| 89 | |||
| 90 | $fts_all->index_document($node); | ||
| 91 | $fts_titles->index_document($node); | ||
| 92 | |||
| 93 | delete $fts_all->{db_backend}; # hack around buglet in DBIx::FTS | ||
| 94 | delete $fts_titles->{db_backend}; # ditto | ||
| 95 | } | ||
| 96 | |||
| 97 | sub delete_node { | ||
| 98 | my ($self, $node) = @_; | ||
| 99 | my $dbh = $self->{_dbh}; | ||
| 100 | my $fts_all = DBIx::FullTextSearch->open($dbh, "_content_and_title_fts") | ||
| 101 | or croak "Can't open _content_and_title_fts"; | ||
| 102 | my $fts_titles = DBIx::FullTextSearch->open($dbh, "_title_fts") | ||
| 103 | or croak "Can't open _title_fts"; | ||
| 104 | eval { $fts_all->delete_document($node); }; | ||
| 105 | croak "Couldn't delete from full index: $@" if $@; | ||
| 106 | eval { $fts_titles->delete_document($node); }; | ||
| 107 | croak "Couldn't delete from title-only index: $@" if $@; | ||
| 108 | return 1; | ||
| 109 | } | ||
| 110 | |||
| 111 | sub supports_phrase_searches { return 1; } | ||
| 112 | sub supports_fuzzy_searches { return 0; } | ||
| 113 | |||
| 114 | =back | ||
| 115 | |||
| 116 | =head1 SEE ALSO | ||
| 117 | |||
| 118 | L<Wiki::Toolkit>, L<Wiki::Toolkit::Search::Base>. | ||
| 119 | |||
| 120 | =cut | ||
| 121 | |||
| 122 | 1; |