Toolkit/Search/Base.pm

filter: Statementstmt 19.3% 11 / 57 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 15.0% 3 / 20 Podpod 100% 10 / 10 totaltotal 22.4% 24 / 107 CC 8 SCAR 16.1
CC
8
Cov
15%
search_nodes
12.0
index_node
12.0
analyze
6.0
CRAP 5.0
« Toolkit/Formatter/WikiLinkFormatterParent.pm ↑ Summary Toolkit/Search/SII.pm »
1 package Wiki::Toolkit::Search::Base;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Carp "croak";
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use vars qw( @ISA $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 0 ▶sub _abstract {
Subroutine _abstract: not called CC=1 SCAR=6.9
9 0 my $who = (caller(1))[3];
10 0 croak "$who is an abstract method which the ".(ref shift).
11 " class has not provided";
12 }
13  
14 1 $VERSION = 0.01;
15  
16 =head1 NAME
17  
18 Wiki::Toolkit::Search::Base - Base class for Wiki::Toolkit search plugins.
19  
20 =head1 SYNOPSIS
21  
22 my $search = Wiki::Toolkit::Search::XXX->new( @args );
23 my %wombat_nodes = $search->search_nodes("wombat");
24  
25 This class details the methods that need to be overridden by search plugins.
26  
27 =cut
28  
29 =head1 METHODS
30  
31 =head2 C<new>
32  
33 my $search = Wiki::Toolkit::Search::XXX->new( @args );
34  
35 Creates a new searcher. By default the arguments are just passed to
36 C<_init>, so you may wish to override that instead.
37  
38 =cut
39  
40 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
41 0 my ($class, @args) = @_;
42 0 my $self = {};
43 0 bless $self, $class;
44 0 return $self->_init(@args);
45 }
46  
47 0 ▶sub _init {
Subroutine _init: not called CC=1 SCAR=6.9
48 0 my ($self, %args) = @_;
49 0 @{$self}{keys %args} = values %args;
50 0 return $self;
51 }
52  
53 =head2 C<search_nodes>
54  
55 # Find all the nodes which contain the word 'expert'.
56 my %results = $search->search_nodes('expert');
57  
58 Returns a (possibly empty) hash whose keys are the node names and
59 whose values are the scores in some kind of relevance-scoring system I
60 haven't entirely come up with yet. For OR searches, this could
61 initially be the number of terms that appear in the node, perhaps.
62  
63 Defaults to AND searches (if $and_or is not supplied, or is anything
64 other than C<OR> or C<or>).
65  
66 Searches are case-insensitive.
67  
68 =cut
69  
70 0 ▶sub search_nodes {
Subroutine search_nodes: not called CC=3 SCAR=24.8
Pod documented
71 0 my ($self, $termstr, $and_or) = @_;
72  
73 0 $and_or = lc($and_or);
74 0 ▶ unless ( defined $and_or and $and_or eq "or" ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $and_or and $and_or eq "or") 0 0 0
Condition0%0/3
defined $and_or and $and_or eq "or"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $and_or and $and_or eq "or"
defined $and_or $and_or eq "or"
Truth table0%0/3
A and B
A= defined $and_orB= $and_or eq "or"
ABresult
0X0
100
111
75 0 $and_or = "and";
76 }
77  
78 # Extract individual search terms.
79 0 my @terms = $self->analyze($termstr);
80  
81 0 return $self->_do_search($and_or, \@terms);
82 }
83  
84 0 ▶sub _do_search { shift->_abstract };
Subroutine _do_search: not called CC=1 SCAR=6.9
85  
86 =head2 C<analyze>
87  
88 @terms = $self->analyze($string)
89  
90 Splits a string into a set of terms for indexing and searching. Typically
91 this is done case-insensitively, splitting at word boundaries, and extracting
92 words that contain at least 1 word characters.
93  
94 =cut
95  
96 0 ▶sub analyze {
Subroutine analyze: not called CC=2 SCAR=17.9
Pod documented
97 0 my ($self, $string) = @_;
98 0 ▶ return grep { length > 1 # ignore single characters
Condition0%0/3
length $_ > 1 && !/^\W*$/
!ll&&!rl&&r
000
MC/DC0%0/2
length $_ > 1 && !/^\W*$/
length $_ > 1 !/^\W*$/
Truth table0%0/3
A && not(B)
A= length $_ > 1B= !/^\W*$/
ABresult
0X0
100
111
99 and ! /^\W*$/ } # and things composed entirely
100 # of non-word characters
101 split( /\b/, # split at word boundaries
102 lc($string) # be case-insensitive
103 );
104 }
105  
106 =head2 C<fuzzy_title_match>
107  
108 $wiki->write_node( "King's Cross St Pancras", "A station." );
109 my %matches = $search->fuzzy_title_match( "Kings Cross St. Pancras" );
110  
111 Returns a (possibly empty) hash whose keys are the node names and
112 whose values are the scores in some kind of relevance-scoring system I
113 haven't entirely come up with yet.
114  
115 Note that even if an exact match is found, any other similar enough
116 matches will also be returned. However, any exact match is guaranteed
117 to have the highest relevance score.
118  
119 The matching is done against "canonicalised" forms of the search
120 string and the node titles in the database: stripping vowels, repeated
121 letters and non-word characters, and lowercasing.
122  
123 =cut
124  
125 0 ▶sub fuzzy_title_match {
Subroutine fuzzy_title_match: not called CC=1 SCAR=6.9
Pod documented
126 0 my ($self, $string) = @_;
127 0 my $canonical = $self->canonicalise_title( $string );
128 0 $self->_fuzzy_match($string, $canonical);
129 }
130  
131 0 ▶sub _fuzzy_match { shift->_abstract };
Subroutine _fuzzy_match: not called CC=1 SCAR=6.9
132  
133 =head2 C<index_node>
134  
135 $search->index_node( $node, $content, $metadata );
136  
137 Indexes or reindexes the given node in the search engine indexes.
138 You must supply both the node name and its content, but metadata is
139 optional.
140  
141 If you do supply metadata, it will be used if and only if your chosen
142 search backend supports metadata indexing (see
143 C<supports_metadata_indexing>). It should be a reference to a hash
144 where the keys are the names of the metadata fields and the values are
145 either scalars or references to arrays of scalars. For example:
146  
147 $search->index_node( "Calthorpe Arms", "Nice pub in Bloomsbury.",
148 { category => [ "Pubs", "Bloomsbury" ],
149 postcode => "WC1X 8JR" } );
150  
151 =cut
152  
153 0 ▶sub index_node {
Subroutine index_node: not called CC=3 SCAR=24.8
Pod documented
154 0 my ($self, $node, $content) = @_;
155 0 ▶ croak "Must supply a node name" unless $node;
Branch: 1 branch
BranchTrueFalseTotal
unless $node 0 0 0
156 0 ▶ croak "Must supply node content" unless defined $content;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $content 0 0 0
157  
158 # Index the individual words in the node content and title.
159 0 my @keys = $self->analyze("$content $node");
160 0 $self->_index_node($node, $content, \@keys);
161 0 $self->_index_fuzzy($node, $self->canonicalise_title( $node ));
162 }
163  
164 0 ▶sub _index_node { shift->_abstract };
Subroutine _index_node: not called CC=1 SCAR=6.9
165 0 ▶sub _index_fuzzy { shift->_abstract };
Subroutine _index_fuzzy: not called CC=1 SCAR=6.9
166  
167 =head2 B<canonicalise_title>
168  
169 $fuzzy = $self->canonicalise_title( $ node);
170  
171 Returns the node title as suitable for fuzzy searching: with punctuation
172 and spaces removes, vowels removed, and double letters squashed.
173  
174 =cut
175  
176 0 ▶sub canonicalise_title {
Subroutine canonicalise_title: not called CC=2 SCAR=17.9
Pod documented
177 0 my ($self, $title) = @_;
178 0 ▶ return "" unless $title;
Branch: 1 branch
BranchTrueFalseTotal
unless $title 0 0 0
179 0 my $canonical = lc($title);
180 0 $canonical =~ s/\W//g; # remove non-word characters
181 0 $canonical =~ s/[aeiouy]//g; # remove vowels and 'y'
182 0 $canonical =~ tr/a-z//s; # collapse doubled (or tripled, etc) letters
183 0 return $canonical;
184 }
185  
186 =head2 C<delete_node>
187  
188 $search->delete_node($node);
189  
190 Removes the given node from the search indexes. NOTE: It's up to you to
191 make sure the node is removed from the backend store. Croaks on error.
192  
193 =cut
194  
195 0 ▶sub delete_node {
Subroutine delete_node: not called CC=2 SCAR=17.9
Pod documented
196 0 my ($self, $node) = @_;
197 0 ▶ croak "Must supply a node name" unless $node;
Branch: 1 branch
BranchTrueFalseTotal
unless $node 0 0 0
198 0 $self->_delete_node($node);
199 }
200  
201 0 ▶sub _delete_node { shift->_abstract };
Subroutine _delete_node: not called CC=1 SCAR=6.9
202  
203 =head2 C<supports_phrase_searches>
204  
205 if ( $search->supports_phrase_searches ) {
206 return $search->search_nodes( '"fox in socks"' );
207 }
208  
209 Returns true if this search backend supports phrase searching, and
210 false otherwise.
211  
212 =cut
213  
214 0 ▶sub supports_phrase_searches { shift->_abstract };
Subroutine supports_phrase_searches: not called CC=1 SCAR=6.9
Pod documented
215  
216 =head2 C<supports_fuzzy_searches>
217  
218 if ( $search->supports_fuzzy_searches ) {
219 return $search->fuzzy_title_match("Kings Cross St Pancreas");
220 }
221  
222 Returns true if this search backend supports fuzzy title matching, and
223 false otherwise.
224  
225 =cut
226  
227 0 ▶sub supports_fuzzy_searches { shift->_abstract };
Subroutine supports_fuzzy_searches: not called CC=1 SCAR=6.9
Pod documented
228  
229 =head2 C<supports_metadata_indexing>
230  
231 if ( $search->supports_metadata_indexing ) {
232 print "This search backend indexes metadata as well as content.";
233 }
234  
235 Returns true if this search backend supports metadata indexing, and
236 false otherwise.
237  
238 =cut
239  
240 0 ▶sub supports_metadata_indexing { 0; };
Subroutine supports_metadata_indexing: not called CC=1 SCAR=6.9
Pod documented
241  
242 =head1 SEE ALSO
243  
244 L<Wiki::Toolkit>
245  
246 =cut
247  
248 1 1;
« Toolkit/Formatter/WikiLinkFormatterParent.pm ↑ Summary Toolkit/Search/SII.pm »