[Bio] / FigWebServices / SearchSkeleton.cgi Repository:
ViewVC logotype

Annotation of /FigWebServices/SearchSkeleton.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.8 - (view) (download)

1 : parrello 1.1 #!/usr/bin/perl -w
2 :    
3 :     BEGIN {
4 :     # Print the HTML header.
5 :     print "CONTENT-TYPE: text/html\n\n";
6 :     }
7 :    
8 :     =head1 NMPDR Search Skeleton
9 :    
10 :     This script executes a search and displays the results. If, on entry,
11 :     it sees a session ID, then it will assume search results have been
12 :     cached and the cached results are to be displayed. Otherwise, it
13 :     will perform the search, cache the results, and display the first
14 :     page. The search itself is performed by an object that subclasses
15 :     B<SearchHelper>. To allow for additional search types, you need
16 :     merely implement a new subclass of B<SearchHelper> and add it
17 :     to the C<use> list below. By convention, all search helper
18 :     subclasses begin with the letters C<SH>. This is not consistent
19 :     with normal PERL practice, but it fits better into the way we
20 :     do builds.
21 :    
22 :     =head2 Session Data
23 :    
24 :     The following parameters are expected from the CGI query object.
25 :     Additional parameters may be required by whichever B<SearchHelper>
26 :     subclass is selected. By convention, the parameters required by
27 :     the subclasses will be lower-case and the parameters used by this
28 :     script are capital-case. Note that some parameters are only required
29 :     by old sessions, that is, sessions which are established with
30 :     existing search result cache files.
31 :    
32 :     =over 4
33 :    
34 :     =item Trace
35 :    
36 :     Trace level and list of trace modules to turn on, space-delimited.
37 :    
38 :     =item NoForm
39 :    
40 :     If specified, then no search form will be generated.
41 :    
42 :     =item SessionID
43 :    
44 :     Unique session ID for this user. This is used to generate the name of the user's
45 :     cache file in the temporary directory. The actual filename will be
46 :     C<tmp_>I<SessionID>C<.cache>.
47 :    
48 :     =item Page (old only)
49 :    
50 :     Number of the current page to display.
51 :    
52 :     =item PageSize
53 :    
54 :     Number of items per page.
55 :    
56 :     =item ResultCount (old only)
57 :    
58 :     Total number of search result lines.
59 :    
60 :     =item Class
61 :    
62 :     Name of the B<SearchHelper> subclass for this type of search. The name does not include
63 :     the C<SH> prefix. So, to specify a B<SHFidSearch> type of
64 :     search, you would specify a class of C<FidSearch>. If this parameter is omitted,
65 :     then all of the advanced search forms will be displayed.
66 :    
67 : parrello 1.3 =item ShowURL
68 :    
69 :     If specified, then a URL for repeating the search will be shown as a hyperlink on the
70 :     results page.
71 :    
72 :     =item ShowAliases
73 :    
74 :     If specified, then hyperlinked aliases will be shown for each feature.
75 :    
76 : parrello 1.1 =back
77 :    
78 :     =head2 The Cache File
79 :    
80 :     The cache file is a tab-delimited file. The first line of the file contains the
81 :     column names and the remaining lines contain the data for each result item.
82 :    
83 :     The column contents may contain HTML tags, including hyperlinks and buttons. For best
84 :     results, all links should be relative.
85 :    
86 : parrello 1.3 Some columns will consist of a doubled percent sign followed by a name, an equal sign,
87 :     and some text. This tells the display code to call the B<RunTimeColumns> method of
88 :     the B<SearchHelper> object to compute the column value. This facility is designed for
89 :     columns that require a lot of time to calculate, so we don't want to calculate them
90 :     until we absolutely have to display them.
91 :    
92 : parrello 1.1 It is presumed that the cache file is small, containing no more than a few thousand
93 :     lines of data. If this is not the case, an entirely different strategy will be
94 :     needed for displaying search results.
95 :    
96 :     If the cache file is empty or has only a single line, a stock "No Search Results"
97 :     message will be displayed.
98 :    
99 :     =cut
100 :    
101 :     use strict;
102 :     use Tracer;
103 :     use CGI;
104 :     use Sprout;
105 :     use SearchHelper;
106 :     use SHFidSearch;
107 :     use SHBlastSearch;
108 :     use SHSigGenes;
109 : parrello 1.8 use SHWordSearch;
110 : parrello 1.1 use POSIX qw(ceil);
111 : parrello 1.6 use File::stat;
112 : parrello 1.1
113 :    
114 :     my ($cgi, $varHash) = ScriptSetup();
115 :     eval {
116 :     # Get the search class.
117 :     my $class = $cgi->param("Class");
118 :     if (! $class) {
119 : parrello 1.2 Trace("Producing index of search tools.") if T(3);
120 :     # No class specified, so we simply generate an index of the
121 :     # searches. First, make sure the template knows there are no search results.
122 : parrello 1.1 $varHash->{result_count} = 0;
123 : parrello 1.2 Trace("Building URL.") if T(3);
124 :     # Get a copy of our URL. Note we include the query fields so that any
125 :     # tracing parameters are preserved.
126 :     my $selfURL = $cgi->url(-relative => 1, -query => 1);
127 : parrello 1.4 # Append a question mark or semicolon to the URL, depending on whether or not
128 : parrello 1.2 # there's already a question mark present.
129 : parrello 1.4 $selfURL .= ($selfURL =~ /\?/ ? ';' : '?');
130 : parrello 1.2 # Loop through the search classes building a table of contents.
131 : parrello 1.1 my @contents = ();
132 : parrello 1.5 for my $className (SearchHelper::AdvancedClassList()) {
133 : parrello 1.2 Trace("Processing $className") if T(3);
134 : parrello 1.1 my $shelp = GetHelper($cgi, $className);
135 : parrello 1.2 push @contents, "<a href=\"${selfURL}Class=$className\">$className</a>: " . $shelp->Description();
136 : parrello 1.1 }
137 :     # Create the table of contents.
138 : parrello 1.2 Trace("Building index.") if T(3);
139 :     my $index = $cgi->h3("Index of Search Tools") .
140 : parrello 1.1 $cgi->ul($cgi->li(\@contents));
141 : parrello 1.2 # Store it as the form.
142 :     $varHash->{form} = $index;
143 :     # Tell the template we don't have a class.
144 :     $varHash->{class} = "";
145 :     Trace("Index built.") if T(3);
146 : parrello 1.1 } else {
147 : parrello 1.2 Trace("Class $class detected.") if T(3);
148 : parrello 1.1 # Here we have a class, so we're working with a single type of search.
149 :     my $shelp = GetHelper($cgi, $class);
150 : parrello 1.2 # Tell the template what the class is.
151 :     $varHash->{class} = $class;
152 : parrello 1.3 # Insure we have a page size.
153 :     if (! $cgi->param("PageSize")) {
154 :     $cgi->param(-name => 'PageSize', -value => $FIG_Config::results_per_page);
155 :     }
156 : parrello 1.1 # Display the form, if desired.
157 : parrello 1.3 my $formShown = ! $cgi->param("NoForm");
158 :     if ($formShown) {
159 : parrello 1.1 $varHash->{form} = $shelp->Form();
160 :     }
161 : parrello 1.3 # Declare the result count variable.
162 :     my $result_count = 0;
163 : parrello 1.1 # Now there are three different directions we can go. If a
164 :     # "Search" button has been pressed, then we need to perform a
165 :     # search. If this is a new session and the button has not
166 :     # been pressed, we do nothing. If this is an old session
167 :     # and the button has not been pressed, we display results. Note
168 :     # that we allow for regular buttons (Search) or image buttons
169 :     # (Search.x).
170 :     if (!$cgi->param("Search") && !$cgi->param("Search.x")) {
171 :     # No button, so check for results. If this is a new session, we do
172 :     # nothing. The form is displayed and nothing else need be done.
173 : parrello 1.7 # Otherwise, we go into display-results mode.
174 : parrello 1.1 Trace("No search requested.") if T(3);
175 :     if (! $shelp->IsNew()) {
176 :     $varHash->{results} = DisplayResults($shelp, $cgi);
177 :     }
178 :     } else {
179 :     # Here we have a button press, so we need to find stuff and
180 : parrello 1.3 # then display it..
181 : parrello 1.1 Trace("Performing the search.") if T(3);
182 : parrello 1.3 # Clear the result area.
183 :     $varHash->{results} = "";
184 :     # Now we process the showURL thing. If showURL is checked, then
185 : parrello 1.4 # we will display a hyperlink to this search. Note that we only
186 :     # do it if the form was shown. If the form was not shown, the
187 :     # code for computing the URL won't work.
188 :     if ($cgi->param('ShowURL') && $formShown) {
189 : parrello 1.3 my $searchURL = $shelp->ComputeSearchURL();
190 :     $varHash->{results} .= $cgi->p("<a href=\"$searchURL\">Right-click to save a URL for this search</a>");
191 :     }
192 : parrello 1.4 # Perform the search.
193 : parrello 1.3 $result_count = $shelp->Find();
194 : parrello 1.1 # Check to see what kind of results we got.
195 : parrello 1.2 if (! defined($result_count)) {
196 : parrello 1.1 # Here an error occurred, so we display the error message.
197 : parrello 1.3 $varHash->{results} .= $cgi->h3("ERROR: " . $shelp->Message());
198 :     $result_count = 0;
199 : parrello 1.1 } elsif ($result_count == 0) {
200 :     # Here nothing matched.
201 : parrello 1.3 $varHash->{results} .= $cgi->h3("No match found.");
202 : parrello 1.1 } else {
203 :     # Here we have results. Save the result count and set up to display
204 :     # the first page of results.
205 :     $cgi->param(-name => "ResultCount", -value => $result_count);
206 :     $cgi->param(-name => "Page", -value => 1);
207 :     # Start with a message about how many matches we found.
208 :     my $countText = ($result_count == 1 ? "One match" : "$result_count matches");
209 : parrello 1.3 $varHash->{results} .= $cgi->h3("$countText found.");
210 : parrello 1.1 # Append the page display.
211 :     $varHash->{results} .= DisplayResults($shelp, $cgi);
212 :     }
213 :     }
214 :     # Save the result count so that the results helper text appears if it
215 :     # is needed. This text is in the template, but it's protected by a TMPL_IF
216 :     # on "result_count".
217 : parrello 1.3 $varHash->{result_count} = $result_count;
218 :     # If there are no results and the form was shown, add the help text. We are
219 :     # assuming that if the user got the search to work, he doesn't need help.
220 :     # In addition, if the form was not shown, a description of how to use it
221 :     # makes no sense.
222 :     if (! $result_count && $formShown) {
223 :     $varHash->{helptext} = $shelp->GetHelpText();
224 :     }
225 : parrello 1.1 }
226 :     };
227 :     if ($@) {
228 :     my $errorMessage = $@;
229 :     Trace("Script Error: $errorMessage") if T(0);
230 :     $varHash->{results} = $cgi->h3("Script Error: $errorMessage");
231 :     }
232 :     ScriptFinish("SproutSearch_tmpl.php", $varHash);
233 :    
234 :     =head3 DisplayResults
235 :    
236 :     C<< my $htmlText = DisplayResults($shelp, $cgi); >>
237 :    
238 :     Display the results of a search. A page of results will be displayed, along with links to get to
239 :     other pages. The HTML for the results display is returned.
240 :    
241 :     =over 4
242 :    
243 :     =item shelp
244 :    
245 :     Search helper object representing the search. The column headers and search rows will be
246 :     stored in the session file attached to it.
247 :    
248 :     =item cgi
249 :    
250 :     CGI query object for the current session. This includes the page number, size, and result
251 :     counts.
252 :    
253 :     =item RETURN
254 :    
255 :     Returns the HTML text for displaying the current page of search results.
256 :    
257 :     =back
258 :    
259 :     =cut
260 :    
261 :     sub DisplayResults {
262 :     # Get the parameters.
263 :     my ($shelp, $cgi) = @_;
264 :     # Declare the return variable.
265 :     my $retVal;
266 :     # Extract the result parameters.
267 :     my ($pageSize, $pageNum, $resultCount) = ($cgi->param('PageSize'),
268 :     $cgi->param('Page'),
269 :     $cgi->param('ResultCount'));
270 :     # Only proceed if there are actual results.
271 :     if ($resultCount <= 0) {
272 :     $retVal = $cgi->h3("No matches found.");
273 :     } else {
274 :     # Check the state of the session file.
275 :     my $fileName = $shelp->GetCacheFileName();
276 :     if (! -e $fileName) {
277 :     $retVal = $cgi->h3("Search session has expired. Please resubmit your query.");
278 :     } else {
279 :     # The file is here, so we can open it.
280 :     my $sessionH = Open(undef, "<$fileName");
281 : parrello 1.6 if (T(3)) {
282 :     my $fileData = stat($sessionH);
283 :     Trace($fileData->size . " bytes in $fileName.");
284 :     }
285 : parrello 1.1 # Read the column headers.
286 :     my @colHdrs = Tracer::GetLine($sessionH);
287 :     # Compute the page navigation string.
288 :     my $pageNavigator = PageNavigator($cgi);
289 :     # Now we need to find our page. The line number we compute will be
290 :     # zero-based. We'll read from the session file until it drops to zero.
291 :     # This may push us past end-of-file, but it won't cause an exception, and
292 :     # it's something that should only happen very rarely in any case.
293 : parrello 1.4 my $linesToSkip = ($pageNum - 1) * $pageSize;
294 :     Trace("Skipping $linesToSkip lines in session file $fileName.") if T(3);
295 :     for (my $lines = $linesToSkip; $lines > 0; $lines--) {
296 : parrello 1.1 Tracer::GetLine($sessionH);
297 :     }
298 :     # The session file is now positioned at the beginning of our line.
299 :     # We build the table rows one line at a time until we run out of data
300 :     # or exceed the page size.
301 :     my @tableRows = ();
302 :     my $linesLeft = $pageSize;
303 : parrello 1.5 Trace("$linesLeft lines to read from session file.") if T(3);
304 :     while ($linesLeft-- > 0) {
305 :     Trace("Reading line from session file.") if T(3);
306 :     my @cols = Tracer::GetLine($sessionH);
307 :     if (! @cols) {
308 :     Trace("End of file read.") if T(3);
309 :     $linesLeft = 0;
310 :     } else {
311 :     Trace("Line has " . scalar(@cols) . " columns. $linesLeft lines left.") if T(3);
312 :     # Check the columns for run-time generation.
313 :     my @actual = map { substr($_,0,2) eq "%%" ? $shelp->GetRunTimeValue($_) : $_ } @cols;
314 :     # Put the actual data into the table list.
315 :     push @tableRows, \@actual;
316 :     }
317 : parrello 1.1 }
318 :     # Now we're ready. We do a page navigator, a spacer, the table, a spacer,
319 :     # and another page navigator.
320 :     $retVal = join("\n", $pageNavigator,
321 :     $cgi->p("&nbsp;"),
322 :     HTML::make_table(\@colHdrs, \@tableRows, "Search Results Page $pageNum"),
323 :     $cgi->p("&nbsp;"),
324 :     $pageNavigator,
325 :     "");
326 :     }
327 :     }
328 :     # Return the result.
329 :     return $retVal;
330 :     }
331 :    
332 :     =head3 PageNavigator
333 :    
334 :     C<< my $htmlText = PageNavigator($cgi); >>
335 :    
336 :     Return a page navigation string for the specified query.
337 :    
338 :     =over 4
339 :    
340 :     =item cgi
341 :    
342 :     CGI query object for the current session. The key values are described in the
343 :     introduction to this document.
344 :    
345 :     =back
346 :    
347 :     =cut
348 :    
349 :     sub PageNavigator {
350 :     # Get the parameters.
351 :     my ($cgi) = @_;
352 :     # Extract the result parameters.
353 :     my ($pageSize, $pageNum, $resultCount) = ($cgi->param('PageSize'),
354 :     $cgi->param('Page'),
355 :     $cgi->param('ResultCount'));
356 : parrello 1.4 # Declare the return variable.
357 :     my $retVal = "";
358 : parrello 1.1 # Compute the number of the last page.
359 :     my $lastPage = ceil($resultCount / $pageSize);
360 : parrello 1.4 # Only proceed if there's more than one page.
361 :     if ($lastPage > 1) {
362 :     # Create a URL without a page number. All the other URLs will be generated
363 :     # from this one by appending the new page number.
364 :     my $url = StatusURL($cgi, Page => undef);
365 :     # Now compute the start and end pages for the display. We display ten pages,
366 :     # with the current one more or less centered.
367 :     my $startPage = $pageNum - 4;
368 :     if ($startPage < 1) { $startPage = 1; }
369 :     my $endPage = $startPage + 9;
370 :     if ($endPage > $lastPage) { $endPage = $lastPage; }
371 :     # Create a list of URL/page-number combinations.
372 :     my @pageThings = ();
373 :     for (my $linkPage = $startPage; $linkPage <= $endPage; $linkPage++) {
374 :     # Check for the current page. It gets a page number with no link.
375 :     if ($linkPage == $pageNum) {
376 :     push @pageThings, $linkPage;
377 :     } else {
378 :     # This is not the current page, so it gets the full treatment.
379 :     push @pageThings, PageThing($cgi, $linkPage, $linkPage, $url);
380 :     }
381 : parrello 1.1 }
382 : parrello 1.4 # Now add some jump links at the end.
383 :     my @forePointers = ();
384 :     if ($endPage < $lastPage) {
385 :     for (my $pg = $endPage + 5; $pg < $lastPage; $pg += 15) {
386 :     push @forePointers, PageThing($cgi, $pg, $pg, $url);
387 :     }
388 :     push @forePointers, PageThing($cgi, ">>", $lastPage, $url);
389 : parrello 1.3 }
390 : parrello 1.4 # Finally, add some jump links at the front.
391 :     my @backPointers = ();
392 :     if ($startPage > 1) {
393 :     for (my $pg = $startPage - 5; $pg > 1; $pg -= 15) {
394 : parrello 1.6 unshift @backPointers, PageThing($cgi, $pg, $pg, $url);
395 : parrello 1.4 }
396 : parrello 1.6 unshift @backPointers, PageThing($cgi, "<<", 1, $url);
397 : parrello 1.3 }
398 : parrello 1.4 # Put it all together.
399 :     my $middle = join(" ", @pageThings);
400 :     $retVal = join " ... ", @backPointers, $middle, @forePointers;
401 : parrello 1.3 }
402 : parrello 1.1 # Return the result.
403 :     return $retVal;
404 :     }
405 :    
406 :     =head3 PageThing
407 :    
408 :     C<< my $htmlText = PageThing($cgi, $pageLabel, $pageNumber, $url); >>
409 :    
410 :     Create an entry for the page navigator. The entry consists of a label that
411 :     is hyperlinked to the specified page number of the search results.
412 :    
413 :     =over 4
414 :    
415 :     =item cgi
416 :    
417 :     CGI object, used to access the CGI HTML-building methods.
418 :    
419 :     =item pageLabel
420 :    
421 :     Text to be hyperlinked. This is usually the page number, but sometimes it will be
422 :     arrows.
423 :    
424 :     =item pageNumber
425 :    
426 :     Number of the page to be presented when the link is followed.
427 :    
428 :     =item url
429 :    
430 :     Base URL for viewing a page.
431 :    
432 :     =item RETURN
433 :    
434 :     Returns HTML for the specified label, hyperlinked to the desired page.
435 :    
436 :     =back
437 :    
438 :     =cut
439 :    
440 :     sub PageThing {
441 :     # Get the parameters.
442 :     my ($cgi, $pageLabel, $pageNumber, $url) = @_;
443 :     # Compute the full URL.
444 :     my $purl = "$url&Page=$pageNumber";
445 :     # Form it into a hyperlink.
446 :     my $retVal = "<a href=\"$purl\" title=\"Results page $pageNumber\">$pageLabel</a>";
447 :     # Return the result.
448 :     return $retVal;
449 :     }
450 :    
451 :     =head3 StatusURL
452 :    
453 :     C<< my $queryUrl = StatusURL($cgi, %overrides); >>
454 :    
455 :     Create a URL for the current script containing status information for the search in progress.
456 :     The values in the incoming CGI object will be used for all parameters except the ones
457 :     specified as overrides. So, for example
458 :    
459 :     StatusURL($cgi, PageNum => 3)
460 :    
461 :     would specify a page number of 3, but all the other parameters will be taken as is from
462 :     the CGI object. The complete list of session variables is given in the L</Session Data>
463 :     section.
464 :    
465 :     =over 4
466 :    
467 :     =item cgi
468 :    
469 :     CGI query object containing the session variables.
470 :    
471 :     =item overrides
472 :    
473 :     A hash mapping key names to override values. These are used to override values in the
474 :     I<$cgi> parameter.
475 :    
476 :     =item RETURN
477 :    
478 :     Returns a relative URL for the current page with GET-style values for all the session
479 :     variables.
480 :    
481 :     =back
482 :    
483 :     =cut
484 :    
485 :     sub StatusURL {
486 :     # Get the parameters.
487 :     my ($cgi, %overrides) = @_;
488 :     # Create a hash of the session variables we want to keep.
489 :     my %varHash;
490 :     for my $varKey (qw(SessionID Trace NoForm ResultCount Page PageSize Class SPROUT)) {
491 :     # Check for an override.
492 :     if (exists $overrides{$varKey}) {
493 :     my $override = $overrides{$varKey};
494 :     # Use the override if it is not null or undefined.
495 :     if (defined($override) && $override ne "") {
496 :     $varHash{$varKey} = $override;
497 :     }
498 :     } else {
499 :     # Check for a CGI value.
500 :     my $normal = $cgi->param($varKey);
501 :     # Use it if it exists.
502 :     if (defined($normal)) {
503 :     $varHash{$varKey} = $normal;
504 :     }
505 :     }
506 :     }
507 :     # Compute the full URL.
508 :     my $retVal = Tracer::GenerateURL($cgi->url(-relative => 1), %varHash);
509 :     # Return the result.
510 :     return $retVal;
511 :     }
512 :    
513 :     =head3 GetHelper
514 :    
515 :     C<< my $shelp = GetHelper($className); >>
516 :    
517 :     Return a helper object with the given class name. If no such class exists, an
518 :     error will be thrown.
519 :    
520 :     =over 4
521 :    
522 :     =item cgi
523 :    
524 :     Active CGI query object.
525 :    
526 :     =item className
527 :    
528 :     Class name for the search helper object, without the preceding C<SH>. This is
529 :     identical to what the script expects for the C<Class> parameter.
530 :    
531 :     =item RETURN
532 :    
533 :     Returns a search helper object for the specified class.
534 :    
535 :     =back
536 :    
537 :     =cut
538 :    
539 :     sub GetHelper {
540 :     # Get the parameters.
541 :     my ($cgi, $className) = @_;
542 :     # Try to create the search helper.
543 :     my $retVal = eval("SH$className->new(\$cgi)");
544 :     if (! defined $retVal) {
545 :     Confess("Could not find a search handler of type $className.");
546 :     }
547 :     # Return the result.
548 :     return $retVal;
549 :     }
550 :    
551 :     1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3