File Coverage

blib/lib/CGI/Application/Search.pm
Criterion Covered Total %
statement 51 53 96.2
branch 1 2 50.0
condition n/a
subroutine 18 18 100.0
pod n/a
total 70 73 95.8


line stmt bran cond sub pod time code
1             package CGI::Application::Search;
2 6     6   343179 use base 'CGI::Application';
  6         14  
  6         7456  
3 6     6   56903 use strict;
  6         18  
  6         210  
4 6     6   31 use warnings;
  6         15  
  6         165  
5 6     6   33 use Carp;
  6         12  
  6         449  
6 6     6   8367 use CGI::Application::Plugin::AnyTemplate;
  6         44168  
  6         415  
7 6     6   6771 use Data::Page;
  6         398172  
  6         98  
8 6     6   6692 use File::Spec::Functions qw(catfile splitpath catdir);
  6         6340  
  6         628  
9 6     6   6302 use Number::Format qw(format_bytes format_number);
  6         119683  
  6         1011  
10 6     6   5996 use HTML::FillInForm;
  6         21657  
  6         1029  
11 6     6   6424 use Time::HiRes;
  6         12050  
  6         32  
12 6     6   9623 use Time::Piece;
  6         101339  
  6         34  
13 6     6   547 use POSIX qw(ceil);
  6         15  
  6         61  
14 6     6   10197 use Text::Context;
  6         27706  
  6         83  
15 6     6   26226 use Unicode::Normalize;
  6         17684  
  6         602  
16 6     6   6722 use Encode qw(decode_utf8 encode_utf8);
  6         96238  
  6         763  
17 6     6   6094 use File::Slurp qw(read_file);
  6         46934  
  6         1487  
18              
19             our $VERSION = '1.14';
20             our (
21             $DEBUG, # a debug flag
22             @SUGGEST_CACHE, # cached suggestions
23             $SUGGEST_CACHE_TIME # time of the last cache
24             );
25             $SUGGEST_CACHE_TIME = 0;
26              
27             # some thing that will never appear in any doc that let's us return everything
28             our $BLANK_SEARCH = 'not asdfqweasdfasdflqkqoiqnwlekjiquwoienlkqw';
29              
30             # load SWISH::API and complain if not available. This is done here
31             # and not in Makefile.PL because SWISH::API is not on CPAN. It's part
32             # of the Swish-e distribution.
33             BEGIN {
34 6     6   409 eval "use SWISH::API";
  6     6   2578  
  0            
  0            
35 6 50       212103 croak(<
36              
37             Unable to load SWISH::API. This module is included in the Swish-e
38             distribution, inside the perl/ directory. Please see the
39             CGI::Application::Search documentation for more details.
40              
41             Error returned was: $@
42              
43             END
44             }
45              
46             __PACKAGE__->add_callback(
47             init => sub {
48             my $self = shift;
49             return unless $self->param('UTF8');
50             my $q = $self->query;
51             $q->charset('UTF-8');
52              
53             # mark the strings as UTF8 so other stuff doesn't have to worry about it
54             my @names = $q->param();
55             foreach my $name (@names) {
56             my @values = $q->param($name);
57             foreach my $i (0 .. $#values) {
58             # don't do filehandles
59             next if lc(ref $values[$i]) eq 'fh';
60             $values[$i] = Unicode::Normalize::NFC(decode_utf8($values[$i]));
61             }
62              
63             $q->param($name => @values);
64             }
65             }
66             );
67              
68             =head1 NAME
69              
70             CGI::Application::Search - Base class for CGI::App Swish-e site engines
71              
72             =head1 SYNOPSIS
73              
74             package My::Search;
75             use base 'CGI::Application::Search';
76              
77             sub cgiapp_init {
78             my $self = shift;
79             $self->param(
80             'SWISHE_INDEX' => 'my-swishe.index',
81             'TEMPLATE' => 'search_results.tmpl',
82             );
83             }
84              
85             sub cgiapp_prerun {
86             my $self = shift;
87             my $query = $self->query;
88              
89             # let the user turn context highlighting off
90             $self->param('HIGHLIGHT' => 0) if $query->param('highlight_off');
91              
92             # let the user change which property is used in the sort
93             my $sort_by = $query->param('sort_by');
94             $self->param('SORT_BY' => $sort_by) if $sort_by;
95             }
96              
97             1;
98              
99             =head1 DESCRIPTION
100              
101             A L based control module that uses Swish-e API in
102             perl (L) to to perform searches on a swish-e index
103             of documents.
104              
105             =head2 Features
106              
107             =over
108              
109             =item * Sub-Classable. Unlike the Perl examples that come with swish-e,
110             this is not a script, and can be customized without modifiying the
111             original so that several sites may share the same underlying code.
112              
113             =item * Uses L to allow flexibility
114             in template engine choice (L, L
115             or L).
116              
117             =item * Built-in templates to use out of box or as examples for your
118             own templates
119              
120             =item * HiLighted search results
121              
122             =item * HiLighted pages linked from search results
123              
124             =item * AJAX results sent to page without need of a page reload
125              
126             =item * AJAX powered 'auto-suggest' to give the user list of choices
127             available for search
128              
129             =back
130              
131             =head1 TUTORIAL
132              
133             If this is your first time using Swish-e (or you think you need
134             a refresher) or if you want step-by-step instructions on how to
135             use the AJAX capabilities of this module, then please see the
136             L<"Tutorial"|CGI::Application::Search::Tutorial>.
137              
138             =head1 RUN_MODES
139              
140             The start_mode is B.
141              
142             =head2 show_search()
143              
144             This method will load the template pointed to by the B