lib/MojoMojo/Model/Search.pm

filter: Statementstmt 98.8% 87 / 88 Branchbran 59.0% 13 / 22 Conditioncond 50.0% 7 / 14 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 100% 15 / 15 Podpod 100% 6 / 6 totaltotal 84.3% 129 / 153 CC 15 SCAR 11.5
CC
15
Cov
86%
index_page
6.3
_get_language
4.1
prepare_search_index
4.0
CRAP 3.2
« lib/MojoMojo/Formatter/SyntaxHighlight.pm ↑ Summary lib/MojoMojo/Formatter/WikipediaLink.pm »
1 package MojoMojo::Model::Search;
2  
3 35 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 35 ▶use parent 'Catalyst::Model';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 35 ▶use KinoSearch1::InvIndexer;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 35 ▶use KinoSearch1::Searcher;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 35 ▶use KinoSearch1::Analysis::PolyAnalyzer;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 35 ▶use KinoSearch1::Index::Term;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 35 ▶use KinoSearch1::Search::Query;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 35 ▶use KinoSearch1::QueryParser::QueryParser;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 35 ▶__PACKAGE__->config->{index_dir} ||= MojoMojo->config->{index_dir};
Condition33%1/3
__PACKAGE__->config->{"index_dir"} ||= "MojoMojo"->config->{"index_dir"}
l!l&&r!l&&!r
0035
MC/DC0%0/2
__PACKAGE__->config->{"index_dir"} ||= "MojoMojo"->config->{"index_dir"}
__PACKAGE__->config->{"index_dir"} "MojoMojo"->config->{"index_dir"}
Truth table33%1/3
A ||= B
A= __PACKAGE__->config->{"index_dir"}B= "MojoMojo"->config->{"index_dir"}
ABresult
1X1
011
000
15 # Fall back just in case MojoMojo->config->{index_dir} doesn't exist
16 # but it should. See MojoMojo.pm to verify that we will short circuit
17 # on this next line.
18 35 ▶__PACKAGE__->config->{index_dir} ||= MojoMojo->path_to('/index');
Condition33%1/3
__PACKAGE__->config->{"index_dir"} ||= "MojoMojo"->path_to("/index")
l!l&&r!l&&!r
0350
MC/DC0%0/2
__PACKAGE__->config->{"index_dir"} ||= "MojoMojo"->path_to("/index")
__PACKAGE__->config->{"index_dir"} "MojoMojo"->path_to("/index")
Truth table33%1/3
A ||= B
A= __PACKAGE__->config->{"index_dir"}B= "MojoMojo"->path_to("/index")
ABresult
1X1
011
000
19  
20 =head1 NAME
21  
22 MojoMojo::Model::Search - support for searching pages
23  
24 =head1 METHODS
25  
26 =cut
27  
28 35 my $invindexer;
29 35 my $analyzer = KinoSearch1::Analysis::PolyAnalyzer->new( language => _get_language() );
30  
31 =head2 indexer
32  
33 Index the search data
34  
35 =cut
36  
37 9 ▶sub indexer {
Subroutine indexer: called CC=2 SCAR=6.9
Pod documented
38 9 my $self = shift;
39 my $invindexer = KinoSearch1::InvIndexer->new(
40 invindex => __PACKAGE__->config->{index_dir},
41 create =>
42 9 ▶ ( -f __PACKAGE__->config->{index_dir} . '/segments' ? 0 : 1 ),
Branch: 1 branch
BranchTrueFalseTotal
-f __PACKAGE__->config->{'index_dir'} . '/segments' ? : 8 1 9
43 analyzer => $analyzer,
44 );
45 9 $invindexer->spec_field( name => 'path', analyzed => 0 );
46 9 $invindexer->spec_field( name => 'text' );
47 9 $invindexer->spec_field( name => 'author' );
48 9 $invindexer->spec_field( name => 'date', analyzed => 0 );
49 9 $invindexer->spec_field( name => 'tags' );
50 9 return $invindexer;
51 }
52  
53 =head2 searcher
54  
55 Used by search() to do the grunt work.
56  
57 =cut
58  
59 3 ▶sub searcher {
Subroutine searcher: called CC=2 SCAR=7.1
Pod documented
60 3 my $self = shift;
61 $self->prepare_search_index
62 3 ▶ unless -f __PACKAGE__->config->{index_dir} . '/segments';
Branch: 1 branch
BranchTrueFalseTotal
unless -f __PACKAGE__->config->{"index_dir"} . '/segments' 0 3 3
63 return KinoSearch1::Searcher->new(
64 invindex => __PACKAGE__->config->{index_dir},
65 3 analyzer => $analyzer,
66 );
67 }
68  
69 =head2 prepare_search_index
70  
71 Create a new search index from all pages in the database.
72 Will do nothing if the index already exists.
73  
74 =cut
75  
76 1 ▶sub prepare_search_index {
Subroutine prepare_search_index: called CC=4 SCAR=14.0
Pod documented
77 1 my $self = shift;
78  
79 1 ▶ MojoMojo->log->info("Initializing search index...")
Branch: 1 branch
BranchTrueFalseTotal
if "MojoMojo"->debug 0 1 1
80 if MojoMojo->debug;
81  
82 # loop through all latest-version pages
83 1 my $count = 0;
84 1 my $it = MojoMojo->model('DBIC::Page')->search;
85 1 ▶ while ( my $page = $it->next ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $page = $it->next) 3 1 4
86 3 $page->result_source->resultset->set_paths($page);
87 3 $self->index_page($page);
88 3 $count++;
89 }
90  
91 1 ▶ MojoMojo->log->info("Indexed $count pages") if MojoMojo->debug;
Branch: 1 branch
BranchTrueFalseTotal
if "MojoMojo"->debug 0 1 1
92 }
93  
94 =head2 index_page <page>
95  
96 Create/update the search index with data from a MojoMojo page when it changes.
97  
98 =cut
99  
100 8 ▶sub index_page {
Subroutine index_page: called CC=6 SCAR=18.5
Pod documented
101 8 my ( $self, $page ) = @_;
102 8 my $index = $self->indexer;
103 8 $page->discard_changes();
104 8 ▶ return unless ( $page && $page->content );
Branch: 1 branch
BranchTrueFalseTotal
unless $page and $page->content 0 8 8
Condition33%1/3
$page and $page->content
!ll&&!rl&&r
008
MC/DC0%0/2
$page and $page->content
$page $page->content
Truth table33%1/3
A and B
A= $pageB= $page->content
ABresult
0X0
100
111
105  
106 8 my $content = $page->content;
107 8 my $key = $page->path;
108  
109 8 my $text = $content->body;
110 8 ▶ $text .= " " . $content->abstract if ( $content->abstract );
Branch: 1 branch
BranchTrueFalseTotal
if $content->abstract 0 8 8
111 8 ▶ $text .= " " . $content->comments if ( $content->comments );
Branch: 1 branch
BranchTrueFalseTotal
if $content->comments 0 8 8
112  
113 # translate the path into plain text so we can use it in the search query later
114 8 my $fixed_path = $key;
115 8 $fixed_path =~ s{/}{X}g;
116  
117 8 my $term = KinoSearch1::Index::Term->new( path => $fixed_path );
118 8 $index->delete_docs_by_term($term);
119 8 my $doc = $index->new_doc();
120 8 $doc->set_value( author => $content->creator->login );
121 8 $doc->set_value( path => $fixed_path );
122 8 ▶ $doc->set_value(
Branch: 1 branch
BranchTrueFalseTotal
$content->created ? : 8 0 8
123 date => ( $content->created ) ? $content->created->ymd : '' );
124 0 $doc->set_value( tags => join( ' ', map { $_->tag } $page->tags ) );
125 8 $doc->set_value( text => $text );
126 8 $index->add_doc($doc);
127 8 $index->finish( optimize => 1 );
128 }
129  
130 =head2 search
131  
132 Search for a term or phrase.
133  
134 =cut
135  
136 3 ▶sub search {
Subroutine search: called CC=1 SCAR=0.0
Pod documented
137 3 my ( $self, $q ) = @_;
138 3 my $qp = KinoSearch1::QueryParser::QueryParser->new(
139 analyzer => $analyzer,
140 fields => [ 'text', 'tags' ],
141 default_boolop => 'AND'
142 );
143 3 my $query = $qp->parse($q);
144 3 my $hits = $self->searcher->search( query => $query );
145  
146 3 return $hits;
147 }
148  
149 =head2 delete_page <page>
150  
151 Removes a page from the search index.
152  
153 =cut
154  
155 1 ▶sub delete_page {
Subroutine delete_page: called CC=2 SCAR=7.0
Pod documented
156 1 my ( $self, $page ) = @_;
157  
158 1 ▶ return unless $page;
Branch: 1 branch
BranchTrueFalseTotal
unless $page 0 1 1
159  
160 1 my $index = $self->indexer;
161 1 my $path = $page->path;
162 1 $path =~ s{/}{X}g;
163  
164 1 my $term = KinoSearch1::Index::Term->new( path => $path );
165 1 $index->delete_docs_by_term($term);
166 1 $index->finish( optimize => 1 );
167 }
168  
169 35 ▶sub _get_language {
Subroutine _get_language: called CC=4 SCAR=14.1
170 35 my %supported_lang = map { $_ => 1 } qw( en da de es fi fr it nl no pt ru sv );
171 35 ▶ my $default_lang = __PACKAGE__->config->{default_lang} || MojoMojo->config->{default_lang} || 'en';
Condition80%4/5
__PACKAGE__->config->{'default_lang'} || 'MojoMojo'->config->{'default_lang'} || 'en'
l!l
332
__PACKAGE__->config->{"default_lang"} or "MojoMojo"->config->{"default_lang"}
l!l&&r!l&&!r
0332
MC/DC50%1/2
__PACKAGE__->config->{'default_lang'} || 'MojoMojo'->config->{'default_lang'} || 'en'
__PACKAGE__->config->{"default_lang"} "MojoMojo"->config->{"default_lang"}
Truth table66%2/3
A or B
A= __PACKAGE__->config->{"default_lang"}B= "MojoMojo"->config->{"default_lang"}
ABresult
1X1
011
000
172  
173 35 ▶ return exists $supported_lang{$default_lang} ? $default_lang : 'en';
Branch: 1 branch
BranchTrueFalseTotal
exists $supported_lang{$default_lang} ? : 35 0 35
174 }
175  
176 =head1 AUTHOR
177  
178 Marcus Ramberg <mramberg@cpan.org>
179  
180 =head1 LICENSE
181  
182 This library is free software. You can redistribute it and/or modify
183 it under the same terms as Perl itself.
184  
185 =cut
186  
187 35 1;
« lib/MojoMojo/Formatter/SyntaxHighlight.pm ↑ Summary lib/MojoMojo/Formatter/WikipediaLink.pm »