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

Annotation of /FigWebServices/SearchSkeleton.cgi

Parent Directory Parent Directory | Revision Log Revision Log


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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3