[Bio] / FigKernelPackages / HTML.pm Repository:
ViewVC logotype

Annotation of /FigKernelPackages/HTML.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.68 - (view) (download) (as text)

1 : efrank 1.1 package HTML;
2 :    
3 : parrello 1.49 use Tracer;
4 : olson 1.25 use FIG;
5 : efrank 1.1 use Carp;
6 :     use Data::Dumper;
7 :     use LWP::UserAgent;
8 :     use LWP::Simple;
9 : golsen 1.41 use URI::Escape; # uri_escape()
10 : efrank 1.1 use URI::URL;
11 :     use HTTP::Request::Common;
12 : olson 1.16 use POSIX;
13 : efrank 1.1
14 : redwards 1.61
15 : overbeek 1.68 my $top_link_cache;
16 :    
17 :    
18 : olson 1.31 sub new
19 :     {
20 :     my($class) = @_;
21 :    
22 :     my $self = {};
23 :    
24 :     return bless $self, $class;
25 :     }
26 :    
27 : overbeek 1.68 sub top_link
28 :     {
29 :    
30 :     #
31 :     # Determine if this is a toplevel cgi or one in one of the subdirs (currently
32 :     # just /p2p).
33 :     #
34 :    
35 :     return $top_link_cache if ($top_link_cache);
36 :    
37 :     my @parts = split(/\//, $ENV{SCRIPT_NAME});
38 :     my $top;
39 :     if ($parts[-2] eq 'FIG')
40 :     {
41 :     $top = '.';
42 :     # warn "toplevel @parts\n";
43 :     }
44 :     elsif ($parts[-3] eq 'FIG')
45 :     {
46 :     $top = '..';
47 :     # warn "subdir @parts\n";
48 :     }
49 :     else
50 :     {
51 :     $top = $FIG_Config::cgi_base;
52 :     # warn "other @parts\n";
53 :     }
54 :    
55 :     $top_link_cache = $top;
56 :     return $top;
57 :     }
58 :    
59 : olson 1.25 sub compute_html_header
60 :     {
61 : olson 1.31 shift if UNIVERSAL::isa($_[0],__PACKAGE__);
62 : olson 1.42 my($additional_insert, $user, %options ) = @_;
63 :    
64 :     my $header_name = $options{header_name} ? $options{header_name} : "html.hdr";
65 :     my $tail_name = $options{tail_name} ? $options{tail_name} : "html.tail";
66 :    
67 :     my $html_hdr_file = "./Html/$header_name";
68 : olson 1.2 if (! -f $html_hdr_file)
69 :     {
70 : parrello 1.49 $html_hdr_file = "$FIG_Config::fig/CGI/Html/$header_name";
71 : olson 1.2 }
72 : olson 1.15 my @html_hdr = &FIG::file_read($html_hdr_file);
73 : olson 1.42
74 : olson 1.67 # for my $k (sort keys %ENV) { warn "$k = $ENV{$k}\n"; }
75 :    
76 :     #
77 :     # Determine if this is a toplevel cgi or one in one of the subdirs (currently
78 :     # just /p2p).
79 :     #
80 :    
81 :     my @parts = split(/\//, $ENV{SCRIPT_NAME});
82 :     my $top;
83 :     if ($parts[-2] eq 'FIG')
84 :     {
85 :     $top = '.';
86 :     # warn "toplevel @parts\n";
87 :     }
88 :     elsif ($parts[-3] eq 'FIG')
89 :     {
90 :     $top = '..';
91 :     # warn "subdir @parts\n";
92 :     }
93 :     else
94 :     {
95 :     $top = $FIG_Config::cgi_base;
96 :     # warn "other @parts\n";
97 :     }
98 :    
99 :     $options{no_fig_search} or push( @html_hdr, "<br><a href=\"$top/index.cgi?user=$user\">FIG search</a>\n" );
100 : overbeek 1.18
101 : olson 1.15 if (@html_hdr)
102 :     {
103 : parrello 1.49 my $insert_stuff;
104 : olson 1.42
105 : parrello 1.49 if (not $options{no_release_info})
106 :     {
107 :     my @ver = &FIG::file_head("$FIG_Config::fig_disk/CURRENT_RELEASE", 1);
108 :     my $ver = $ver[0];
109 :     chomp $ver;
110 :     if ($ver =~ /^cvs\.(\d+)$/)
111 :     {
112 :     my $d = asctime(localtime($1));
113 :     chomp($d);
114 :     $ver .= " ($d)";
115 :     }
116 :     my $host = &FIG::get_local_hostname();
117 :     $insert_stuff = "SEED version <b>$ver</b> on $host";
118 :     }
119 :    
120 :     if ($additional_insert)
121 :     {
122 :     $insert_stuff .= "<br>" . $additional_insert;
123 :     }
124 :    
125 :     for $_ (@html_hdr)
126 :     {
127 : olson 1.67 s,(href|img\s+src)="/FIG/,\1="$top/,g;
128 : redwards 1.54 s,(\?user\=)\",$1$user",;
129 : parrello 1.49 if ($_ eq "<!-- HEADER_INSERT -->\n")
130 :     {
131 :     $_ = $insert_stuff;
132 :     }
133 :     }
134 : olson 1.15 }
135 :    
136 : olson 1.25 return @html_hdr;
137 :     }
138 :    
139 :     sub show_page {
140 : efrank 1.32 #warn "SHOWPAGE: cgi=", Dumper(@_);
141 : olson 1.31 shift if UNIVERSAL::isa($_[0],__PACKAGE__);
142 : redwards 1.61 my($cgi,$html,$no_home, $alt_header, $css, $javasrc, $cookie) = @_;
143 : olson 1.25 my $i;
144 :    
145 : redwards 1.34 # ARGUMENTS:
146 :     # $cgi is the CGI method
147 :     # $html is an array with all the html in it. It is just joined by "\n" (and not <br> or <p>
148 : redwards 1.35 # $no_home eliminates ONLY the bottom FIG search link in a page
149 : parrello 1.49 # $alt_header is a reference to an array for an alternate header banner that you can replace the standard one with
150 : redwards 1.34 # $css is a reference to a hash. The key is the name of the CSS sheet and the value is the URL of that sheet. Note the usual rules about relative css urls
151 :     # the sheet named "Default" is considered to be the default style sheet, and if this is not set it points at $FIG_Config::HTML/css/default.css
152 :     # the sheet named "Sans Serif" is considered to the the first alternate, and if this is not set it points at $FIG_Config::HTML/css/sanserif.css
153 : parrello 1.49 # $javasrc is a reference to an array of URLs to javascripts to be included (e.g. "/FIG/Html/css/styleswitcher.js")
154 : redwards 1.61 # $cookie is the name and value of the cookie to set. Note that you should probably use raelib->cookie to get/set your cookies
155 : olson 1.25 #
156 :     # Find the HTML header
157 :     #
158 :    
159 : olson 1.42 my $html_tail_file = "./Html/$tail_name";
160 : olson 1.25 if (! -f $html_tail_file)
161 :     {
162 : parrello 1.49 $html_tail_file = "$FIG_Config::fig/CGI/Html/$tail_name";
163 : olson 1.25 }
164 :    
165 : overbeek 1.33 my $user = $cgi->param('user') || "";
166 : redwards 1.35 my @html_hdr;
167 : redwards 1.39 if ($alt_header && ref($alt_header) eq "ARRAY")
168 : redwards 1.35 {
169 :     @html_hdr = @$alt_header;
170 :     }
171 :     else
172 :     {
173 :     @html_hdr = compute_html_header(undef,$user);
174 :     }
175 : olson 1.25
176 : redwards 1.61 # RAE: I am offloading the handling of cookies to CGI.pm since I don't know how they are set up.
177 :     # This modification adds the cookies if necessary
178 : redwards 1.63
179 :     # Note: 3/10/05 commented this line out pending the discussion of adding cookies into the seed that we are waiting to see about
180 :     # to add cookies back in replace these two header lines with each other
181 :    
182 :     #print $cgi->header(-cookie=>$cookie);
183 :     print $cgi->header();
184 :    
185 : golsen 1.5 #
186 : golsen 1.6 # The SEED header file goes immediately after <BODY>. Figure out
187 :     # what parts of the HTML document skeleton are there, and fill in
188 :     # missing ones.
189 : golsen 1.5 #
190 : golsen 1.6 # This list should be as comprehensive as feasible:
191 : golsen 1.5 #
192 :    
193 : golsen 1.6 my %head_tag = ( base => 1,
194 :     basefont => 1,
195 :     html => 1,
196 :     isindex => 1,
197 :     link => 1,
198 :     meta => 1,
199 :     nextid => 1,
200 :     style => 1,
201 : redwards 1.61 title => 1,
202 : golsen 1.6 );
203 :    
204 :     #
205 :     # This list need not be comprehensive; it is just stopping conditions:
206 :     #
207 :    
208 :     my %body_tag = ( a => 1,
209 :     br => 1,
210 :     center => 1,
211 :     form => 1,
212 :     h1 => 1,
213 :     h2 => 1,
214 :     h3 => 1,
215 :     hr => 1,
216 :     img => 1,
217 :     p => 1,
218 :     pre => 1,
219 :     table => 1
220 :     );
221 :    
222 :     my $html_line = -1;
223 :     my $head_line = -1;
224 :     my $base_line = -1;
225 :     my $head_end_line = -1;
226 :     my $body_line = -1;
227 :     my $last_head_line = -1; # If no head tags are found, text goes at top.
228 :     my $done = 0;
229 :    
230 :     for ( $i = 0; $i < @$html; $i++ )
231 :     {
232 : parrello 1.49 # Some special cases:
233 : golsen 1.6
234 : parrello 1.49 if ( $html->[$i] =~ /\<html[^0-9a-z]/i ) { $html_line = $i }
235 :     if ( $html->[$i] =~ /\<head[^0-9a-z]/i ) { $head_line = $i }
236 :     if ( $html->[$i] =~ /\<base[^0-9a-z]/i ) { $base_line = $i }
237 :     if ( $html->[$i] =~ /\<\/head\>/i ) { $head_end_line = $i }
238 :    
239 :     # The content goes after this line:
240 :    
241 :     if ( $html->[$i] =~ /\<body[^0-9a-z]/i )
242 :     {
243 :     $body_line = $i;
244 :     $last;
245 :     }
246 :    
247 :     # Now the general case.
248 :     # Analyze all the html tags on the line:
249 :    
250 :     foreach ( $html->[$i] =~ /\<\/?([0-9a-z]+)/ig )
251 :     {
252 :     # At first body tag, we stop the search and put the text
253 :     # after the last line with a head tag:
254 :    
255 :     if ( $body_tag{ lc $_ } )
256 :     {
257 :     $done = 1;
258 :     last;
259 :     }
260 :    
261 :     # If this is a head tag, then move the marker forward
262 :    
263 :     elsif ( $head_tag{ lc $_ } )
264 :     {
265 :     $last_head_line = $i;
266 :     }
267 :     }
268 :     last if $done; # When done, break loop to avoid increment
269 : efrank 1.1 }
270 : golsen 1.6
271 :     # Some sanity checks on structure:
272 :    
273 :     if ( 1 )
274 : efrank 1.1 {
275 : parrello 1.49 if ( $html_line >= 0 )
276 :     {
277 :     if ( ( $head_line >= 0 ) && ( $html_line > $head_line ) )
278 :     {
279 :     print STDERR "<HTML> tag follows <HEAD> tag\n";
280 :     }
281 :     if ( ( $head_end_line >= 0 ) && ( $html_line > $head_end_line ) )
282 :     {
283 :     print STDERR "<HTML> tag follows </HEAD> tag\n";
284 :     }
285 :     }
286 :     if ( $head_line >= 0 )
287 :     {
288 :     if ( ( $head_end_line >= 0 ) && ( $head_line > $head_end_line ) )
289 :     {
290 :     print STDERR "<HEAD> tag follows </HEAD> tag\n";
291 :     }
292 :     }
293 : efrank 1.1 }
294 :    
295 : golsen 1.6 #
296 :     # Okay. Let's put in the html header file, and missing tags:
297 :     #
298 :     # <BODY> goes after last head line
299 :     #
300 : redwards 1.34 # RAE:
301 :     # Added the javascript for the buttons immediately after body.
302 : redwards 1.20 # Note if no buttons are added we still (at the moment) add the script,
303 :     # but it only adds a little text (495 characters) to the html and noone will notice!
304 : redwards 1.46 # RAE: This is now deprecated because everything is in an external file, FIG.js, included later
305 : golsen 1.6
306 :     if ( $body_line < 0 )
307 :     {
308 : parrello 1.49 $body_line = $last_head_line + 1;
309 :     splice( @$html, $body_line, 0, "<BODY>\n" );
310 : golsen 1.6 }
311 :    
312 :     #
313 :     # Seed page header (if it exists) goes after <BODY>
314 :     #
315 :    
316 : olson 1.15 if (@html_hdr)
317 : golsen 1.6 {
318 : parrello 1.49 splice( @$html, $body_line + 1, 0, @html_hdr );
319 : golsen 1.6 }
320 :    
321 :     #
322 :     # </HEAD> goes before <BODY>
323 :     #
324 :    
325 :     if ( $head_end_line < 0 )
326 :     {
327 : parrello 1.49 $head_end_line = $body_line;
328 :     splice( @$html, $body_line, 0, "</HEAD>\n" );
329 : golsen 1.6 }
330 :    
331 : redwards 1.34 # RAE:
332 :     # Add css here
333 : golsen 1.40 # Note that at the moment I define these two sheets here. I think this should
334 :     # be moved out, but I want to try it and see what happens. css has the format:
335 :     #
336 : redwards 1.34 # <link rel='stylesheet' title='default' href='/css/default.css' type='text/css'>
337 : golsen 1.40
338 : redwards 1.35 # convert the default key to the right case. and eliminate dups
339 :     foreach my $k (keys %$css) {if (lc($k) eq "default") {$css->{'Default'}=$css->{$k}}}
340 : golsen 1.40
341 : redwards 1.34 if (!$css || !$css->{'Default'})
342 :     {
343 : olson 1.51 $css->{'Default'} = &FIG::cgi_url() . "/Html/css/default.css";
344 : redwards 1.34 }
345 :     if (!$css->{"Sans Serif"})
346 :     {
347 : olson 1.51 $css->{'Sans Serif'} = &FIG::cgi_url() . "/Html/css/sanserif.css";
348 : redwards 1.34 }
349 : redwards 1.55
350 : redwards 1.34 my $csstext = "<link rel='stylesheet' title='default' href='".$css->{'Default'}."' type='text/css'>\n";
351 :     $csstext .= "<link rel='alternate stylesheet' title='Sans Serif' href='".$css->{'Sans Serif'}."' type='text/css'>\n";
352 :    
353 :     foreach my $k (keys %$css)
354 :     {
355 :     next if (lc($k) eq "default" || lc($k) eq "sans serif");
356 : redwards 1.38 $csstext .= "<link rel='alternate stylesheet' title='$k' href='".$css->{$k}."' type='text/css'>\n";
357 :     }
358 :    
359 : redwards 1.55 $csstext .= "<link rel='alternate' title='SEED RSS feeds' href='".&FIG::cgi_url()."/Html/rss/SEED.rss' type='application/rss+xml'>\n";
360 : redwards 1.38
361 :     # RAE: also added support for external javascripts here.
362 :     # we are cluttering the HTML code with all the javascripts when they could easily be in external files
363 :     # this solution allows us to source other files
364 :    
365 : redwards 1.46 # the file FIG.js contains most of the java script we use routinely. Every browser will just cache this and so
366 :     # it will reduce our overhead.
367 :    
368 : redwards 1.38 # $javasrc must be a ref to an array with urls (absolute or relative) to the javascripts
369 : olson 1.51 push @$javasrc, &FIG::cgi_url() . "/Html/css/FIG.js";
370 : redwards 1.46 foreach my $script (@$javasrc) {
371 : parrello 1.49 $csstext .= "<script src=\"$script\" type=\"text/javascript\"></script>\n";
372 : redwards 1.34 }
373 : redwards 1.38
374 :    
375 :    
376 : redwards 1.34 splice( @$html, $head_end_line, 1, "$csstext</HEAD>\n" ); # note here I am replacing the </head> line. Could be bad...? But it doesn't increment everything else.
377 :    
378 : golsen 1.6 #
379 :     # <BASE ...> goes before </HEAD>
380 :     #
381 :    
382 :     if ( $base_line < 0 )
383 :     {
384 : parrello 1.49 #
385 :     # Use a relative base address for pages. Also, because I am
386 :     # worried about when FIG_config.pm gets updated (clean installs
387 :     # only, or every update?), I provide an alternative derivation
388 :     # from $cgi_url. -- GJO
389 :     #
390 :     # BASE href needs to be absolute. RDO.
391 :     #
392 :     #
393 :     $base_url = &FIG::cgi_url;
394 :     # my $base_url = $FIG_Config::cgi_base;
395 :     # if ( ! $base_url ) # if cgi_base was not defined
396 :     # {
397 :     # $base_url = $FIG_Config::cgi_url; # get the full cgi url
398 :     # $base_url =~ s~^http://[^/]*~~; # remove protocol and host
399 :     # $base_url =~ m~/$~ || $base_url =~ s~$~/~; # check trailing slash
400 :     # }
401 : golsen 1.6
402 : parrello 1.49 $base_line = $head_end_line;
403 : overbeek 1.64 #
404 :     # RDO 2005-1006. Remove this so proxying works better.
405 :     #
406 :     # splice( @$html, $base_line, 0, "<BASE href=\"$base_url/\">\n" );
407 : golsen 1.6 }
408 :    
409 :     #
410 :     # <HTML> goes at the top of the output
411 :     #
412 :    
413 :     if ( $html_line < 0 )
414 :     {
415 : parrello 1.49 $html_line = 0;
416 :     splice( @$html, $html_line, 0, "<HTML>\n" );
417 : golsen 1.6 }
418 :    
419 :     #
420 :     # <HEAD> goes after <HTML>
421 :     #
422 :    
423 :     if ( $head_line < 0 )
424 :     {
425 : parrello 1.49 $head_line = $html_line + 1;
426 :     splice( @$html, $head_line, 0, "<HEAD>\n" );
427 : golsen 1.6 }
428 :    
429 :     #
430 :     # Place FIG search link at bottom of page
431 :     #
432 :    
433 :     my @tail = -f $html_tail_file ? `cat $html_tail_file` : ();
434 : efrank 1.1 if (! $no_home)
435 :     {
436 : parrello 1.49 my $user = $cgi->param('user') || "";
437 :     push( @tail, "<hr><a href=\"index.cgi?user=$user\">FIG search</a>\n" );
438 : efrank 1.1 }
439 :    
440 : golsen 1.6 #
441 : olson 1.21 # See if we have a site-specific tail (for disclaimers, etc).
442 :     #
443 :    
444 :     my $site_tail = "$FIG_Config::fig_disk/config/site_tail.html";
445 :     my $site_fh;
446 :     if (open($site_fh, "<$site_tail"))
447 :     {
448 : parrello 1.49 push(@tail, <$site_fh>);
449 :     close($site_fh);
450 : olson 1.21 }
451 :    
452 :     #
453 : golsen 1.6 # Figure out where to insert The SEED tail. Before </body>,
454 :     # or before </html>, or at end of page.
455 :     #
456 :     my @tags = ();
457 : parrello 1.49 # Check for a tracing queue.
458 :     my $traceString = QTrace("HTML");
459 :     if ($traceString) {
460 :     push @tags, $traceString;
461 :     }
462 : golsen 1.6 for ($i=0; ($i < @$html) && ($html->[$i] !~ /\<\/body\>/i); $i++) {}
463 :     if ($i >= @$html) # </body> not found; look for </html>
464 : efrank 1.1 {
465 : parrello 1.49 push @tags, "\n</BODY>\n";
466 :     # Even if tag is not found, index points to correct place for splice
467 :     for ($i=0; ($i < @$html) && ($html->[$i] !~ /\<\/html\>/i); $i++) {}
468 :     if ($i >= @$html) # </html> not found; add it
469 :     {
470 :     push @tags, "</HTML>\n";
471 :     }
472 : efrank 1.1 }
473 : golsen 1.6
474 :     if ( @tail )
475 :     {
476 : parrello 1.49 splice( @$html, $i, 0, @tail, @tags );
477 : golsen 1.6 }
478 :     elsif ( @tags )
479 :     {
480 : parrello 1.49 splice( @$html, $i, 0, @tags );
481 : golsen 1.6 }
482 :    
483 : golsen 1.40 # RAE the chomp will return any new lines at the ends of elements in the array,
484 :     # and then we can join with a "\n". This is because somethings put newlines in,
485 :     # and others don't. This should make nicer looking html
486 :     #
487 :     # chomp(@$html);
488 :     # print join "\n", @$html;
489 :     #
490 :     # Apparently the above still breaks things. This is the correct code:
491 : redwards 1.37
492 : overbeek 1.43 foreach $_ (@$html)
493 :     {
494 : parrello 1.49 print $_;
495 : overbeek 1.43 }
496 : parrello 1.49
497 : efrank 1.1 }
498 :    
499 :     sub make_table {
500 : olson 1.17 my($col_hdrs,$tab,$title, %options ) = @_;
501 : efrank 1.1 my(@tab);
502 :    
503 : olson 1.17 my $border = defined $options{border} ? "border=\"$options{border}\"" : "border";
504 : redwards 1.61 my $width = defined $options{width} ? "width=\"$options{width}\"" : undef;
505 :     push( @tab, "\n<table $border $width>\n",
506 : golsen 1.6 "\t<caption><b>$title</b></caption>\n",
507 : olson 1.27 "\t<tr>\n\t\t"
508 :     . join( "\n", map { &expand($_, "th") } @$col_hdrs )
509 :     . "\n\t</tr>\n"
510 : golsen 1.6 );
511 : overbeek 1.9 my($i);
512 : efrank 1.1
513 : overbeek 1.3 my $row;
514 :     foreach $row (@$tab)
515 : efrank 1.1 {
516 : parrello 1.49 push( @tab, "\t<tr>\n"
517 :     . join( "\n", map { &expand($_) } @$row )
518 :     . "\n\t</tr>\n"
519 :     );
520 : efrank 1.1 }
521 :     push(@tab,"</table>\n");
522 :     return join("",@tab);
523 :     }
524 :    
525 : overbeek 1.3 sub expand {
526 : olson 1.31 shift if UNIVERSAL::isa($_[0],__PACKAGE__);
527 : golsen 1.40 my( $x, $tag ) = @_;
528 :    
529 : olson 1.27 $tag = "td" unless $tag;
530 : golsen 1.40 my $endtag = $tag;
531 :    
532 :     # RAE modified this so that you can pass in a reference to an array where
533 :     # the first element is the data to display and the second element is optional
534 :     # things like colspan and align. Note that in this case you need to include the td
535 : redwards 1.30 # use something like ["some data to appear", "td colspan=4 bgcolor=gray"]
536 : overbeek 1.3
537 : redwards 1.47 # per GJO's request modified this line so it can take any tag.
538 :     if ( ref($x) eq "ARRAY" ) { ($x, $tag) = @$x; $tag =~ /^(\S+)/; $endtag = $1 }
539 : golsen 1.40
540 :     if ( $x =~ /^\@([^:]+)\:(.*)$/ )
541 : overbeek 1.3 {
542 : parrello 1.49 return "\t\t<$tag $1>$2</$endtag>";
543 : overbeek 1.3 }
544 :     else
545 :     {
546 : parrello 1.49 return "\t\t<$tag>$x</$endtag>";
547 : overbeek 1.3 }
548 :     }
549 :    
550 : redwards 1.47
551 : redwards 1.62 =head2 merge_table_rows()
552 :    
553 :     Merge table rows together. This will merge a table so that adjacent cells with the same content will only be shown once.
554 :    
555 :     Something like this:
556 :    
557 :     -----------------------
558 :     | 1 | a |
559 :     -----------------------
560 :     | 1 | b |
561 :     -----------------------
562 :     | 2 | c |
563 :     -----------------------
564 :     | 3 | d |
565 :     -----------------------
566 :     | 4 | d |
567 :     -----------------------
568 :     | 5 | d |
569 :     -----------------------
570 :    
571 :     Will become:
572 :    
573 :     -----------------------
574 :     | | a |
575 :     | 1 |-----------
576 :     | | b |
577 :     -----------------------
578 :     | 2 | c |
579 :     -----------------------
580 :     | 3 | |
581 :     ------------ |
582 :     | 4 | 5 |
583 :     ------------ |
584 :     | 5 | |
585 :     -----------------------
586 :    
587 :    
588 :     The method takes two arguments. The reference to the array that is the table ($tab). This is the standard table that is created for HTML.pm to draw, and a reference to a hash of columns that you don't want to merge together. The reference to the hash is optional, and if not included, everything will be merged.
589 :    
590 :     $tab=&HTML::merge_table_rows($tab);
591 :    
592 :     or
593 :    
594 :     $skip=(1=>1, 3=>1, 5=>1);
595 :     $tab=&HTML::merge_table_rows($tab, $skip); # will merge all columns except 1, 3 and 5. Note the first column in the table is #0
596 :    
597 :    
598 :     =cut
599 :    
600 :    
601 :    
602 :    
603 : redwards 1.47 sub merge_table_rows {
604 :     # RAE:
605 :     # Experimental piece of code. We often want to have rows or columns were cells are merged. It just looks so much nicer
606 :     # this block should merge adjacent rows that have the same text in them.
607 :     # use like this:
608 :     # $tab=&HTML::merge_table_rows($tab);
609 :     # before you do a make_table call
610 :    
611 :     my $self=shift if UNIVERSAL::isa($_[0],__PACKAGE__);
612 : redwards 1.62 my ($tab, $skip)=@_;
613 : redwards 1.47
614 :     my $newtable;
615 :     my $lastrow;
616 :     my $rowspan;
617 :     my $refs;
618 :    
619 :     for (my $y=0; $y <= $#$tab; $y++) {
620 :     #$y is the row in the table;
621 :     for (my $x=0; $x <= $#{$tab->[$y]}; $x++) {
622 : redwards 1.62 # this is the user definable columns not to merge
623 :     if ($skip->{$x})
624 :     {
625 :     $newtable->[$y]->[$x] = $tab->[$y]->[$x];
626 :     next;
627 :     }
628 :    
629 : redwards 1.47 #$x is the column in the table
630 :     # if the column in the row we are looking at is the same as the column in the previous row, we don't add
631 :     # this cell to $newtable. Instead we increment the rowspan of the previous row by one
632 :    
633 :     # handle cells that are references to arrays
634 :     if (ref($tab->[$y]->[$x]) eq "ARRAY") {$refs->[$y]->[$x]=$tab->[$y]->[$x]->[1]; $tab->[$y]->[$x]=$tab->[$y]->[$x]->[0]}
635 :    
636 :     # now we go back through the table looking where to draw the merge line:
637 :     my $lasty=$y;
638 : redwards 1.52 while ($lasty >= 0 && $tab->[$y]->[$x] eq $tab->[$lasty]->[$x]) {$lasty--}
639 : redwards 1.47 $lasty++; # this is the last identical cell. If lasty==y it is the current cell, so we just save the data. Otherwise we increment the rowspan
640 :     if ($lasty == $y) {
641 :     # we always want to have something in rows that may otherwise be empty but should be there (see below)
642 :     unless ($tab->[$y]->[$x]) {$tab->[$y]->[$x]=" &nbsp; "}
643 :     $newtable->[$y]->[$x] = $tab->[$y]->[$x];
644 :     }
645 :     else {$rowspan->[$lasty]->[$x]++}
646 :     }
647 :     }
648 :    
649 :     # now just join everything back together
650 :     for (my $y=0; $y <= $#$tab; $y++) {
651 :     for (my $x=0; $x <= $#{$tab->[$y]}; $x++) {
652 :     if ($rowspan->[$y]->[$x]) {
653 :     if ($refs->[$y]->[$x]) {$refs->[$y]->[$x] .= " rowspan=". ($rowspan->[$y]->[$x]+1)}
654 :     else {$refs->[$y]->[$x] = "td rowspan=". ($rowspan->[$y]->[$x]+1)}
655 :     $newtable->[$y]->[$x]=[$newtable->[$y]->[$x], $refs->[$y]->[$x]];
656 :     }
657 :     elsif ($newtable->[$y]->[$x] && $refs->[$y]->[$x]) {
658 :     $newtable->[$y]->[$x]=[$newtable->[$y]->[$x], $refs->[$y]->[$x]];
659 :     }
660 :     }
661 :     }
662 :    
663 :    
664 :     # finally we have to remove any completely empty cells that have been added by the array mechanism
665 :     # (e.g. if you define $a->[2] then $a->[0] and $a->[1] are now undef).
666 :     # that is why in the loop above I replace empty cells with nbsp. They are now not undef!
667 :     # I am sure that Gary can do this in one line, but I am hacking.
668 :     my @trimmed;
669 :     foreach my $a (@$newtable) {
670 :     my @row;
671 :     foreach my $b (@$a) {
672 :     push @row, $b if ($b);
673 :     }
674 :     push @trimmed, \@row;
675 :     }
676 :    
677 :     return \@trimmed;
678 :     }
679 :    
680 :    
681 :    
682 :    
683 : overbeek 1.11 sub set_ec_links {
684 : olson 1.31 shift if UNIVERSAL::isa($_[0],__PACKAGE__);
685 : overbeek 1.11 my($cgi,$x) = @_;
686 :     my($before,$match,$after);
687 :    
688 :     if ($x =~ /^(.*)(EC \d+\.\d+\.\d+\.\d+)(.*)/s)
689 :     {
690 : parrello 1.49 $before = $1;
691 :     $match = $2;
692 :     $after = $3;
693 :     return &set_ec_links($cgi,$before) . &HTML::ec_link($match) . &set_ec_links($cgi,$after);
694 : overbeek 1.11 }
695 :     return $x;
696 :     }
697 :    
698 : efrank 1.1 sub ec_link {
699 : olson 1.31 shift if UNIVERSAL::isa($_[0],__PACKAGE__);
700 : efrank 1.1 my($role) = @_;
701 :    
702 :     if ($role =~ /(\d+\.\d+\.\d+\.\d+)/)
703 :     {
704 : parrello 1.49 return "<a href=\"http://www.genome.ad.jp/dbget-bin/www_bget?ec:$1\">$role</a>";
705 : efrank 1.1 }
706 :     else
707 :     {
708 : parrello 1.49 return $role;
709 : efrank 1.1 }
710 :     }
711 :    
712 :     sub role_link {
713 : olson 1.31 shift if UNIVERSAL::isa($_[0],__PACKAGE__);
714 : efrank 1.1 my($cgi,$role) = @_;
715 :    
716 :     my $roleR = ($role =~ /^(\d+\.\d+\.\d+\.\d+)\s+-\s+/) ? $1 : $role;
717 :     my $user = $cgi->param('user');
718 :     if (! $user) { $user = "" }
719 :     my $link = $cgi->url() . "?role=$roleR&user=$user";
720 :     $link =~ s/[a-z]+\.cgi\?/pom.cgi?/;
721 :     return "<a href=$link>$role</a>";
722 :     }
723 :    
724 : olson 1.13 #
725 :     # Local means to eliminate the fig|org.peg from the
726 :     # text of the link.
727 :     #
728 : efrank 1.1 sub fid_link {
729 : olson 1.31 shift if UNIVERSAL::isa($_[0],__PACKAGE__);
730 : efrank 1.1 my($cgi,$fid,$local,$just_url) = @_;
731 :     my($n);
732 :    
733 : overbeek 1.68 my $top = top_link();
734 :    
735 : efrank 1.1 if ($fid =~ /^fig\|\d+\.\d+\.([a-z]+)\.(\d+)/)
736 :     {
737 : parrello 1.49 if ($local)
738 :     {
739 :     if ($1 eq "peg")
740 :     {
741 :     $n = $2;
742 :     }
743 :     else
744 :     {
745 :     $n = "$1.$2";
746 :     }
747 :     }
748 :     else
749 :     {
750 :     $n = $fid;
751 :     }
752 : overbeek 1.60
753 :     my $link;
754 : mkubal 1.57 #added to format prophage and path island links to feature.cgi
755 :     if ($1 ne "peg")
756 :     {
757 :     my $user = $cgi->param('user');
758 :     if (! $user) { $user = "" }
759 :     my $sprout = $cgi->param('SPROUT') ? "&SPROUT=1" : "";
760 : overbeek 1.68 $link = "$top/feature.cgi?feature=$fid&user=$user$trans$sprout";
761 : mkubal 1.57 $link =~ s/[a-z_A-Z0-9]+\.cgi\?/feature.cgi?/;
762 :     }
763 : overbeek 1.60 else
764 : mkubal 1.57 {
765 :     my $user = $cgi->param('user');
766 :     if (! $user) { $user = "" }
767 :     my $trans = $cgi->param('translate') ? "&translate=1" : "";
768 :     my $sprout = $cgi->param('SPROUT') ? "&SPROUT=1" : "";
769 : overbeek 1.56 ###a
770 :    
771 :     ### This used to be
772 :     ### my $link = &FIG::cgi_url . "/protein.cgi?prot=$fid&user=$user$trans$sprout";
773 :     ###
774 :     ### The cost became prohibitive in the subsystem spreadsheets. Hence, we cache the value
775 :     ###
776 :     ### RAO
777 :    
778 : overbeek 1.64 #if (! $cgi_url) { $cgi_url = &FIG::cgi_url }
779 :     #$link = $cgi_url . "/protein.cgi?prot=$fid&user=$user$trans$sprout";
780 : overbeek 1.68 $link = "$top/protein.cgi?prot=$fid&user=$user$trans$sprout";
781 : mkubal 1.57 $link =~ s/[a-z_A-Z0-9]+\.cgi\?/protein.cgi?/;
782 : overbeek 1.60 }
783 :     if ($just_url)
784 :     {
785 :     return $link;
786 :     }
787 :     else
788 :     {
789 :     return "<a href=$link>$n</a>";
790 : mkubal 1.59 }
791 : efrank 1.1 }
792 :     return $fid;
793 :     }
794 :    
795 :     sub family_link {
796 : olson 1.31 shift if UNIVERSAL::isa($_[0],__PACKAGE__);
797 : efrank 1.1 my($family,$user) = @_;
798 :    
799 :     return $family;
800 :     }
801 :    
802 :    
803 :     sub get_html {
804 : olson 1.31 shift if UNIVERSAL::isa($_[0],__PACKAGE__);
805 : efrank 1.1 my( $url, $type, $kv_pairs) = @_;
806 :     my( $encoded, $ua, $args, @args, $out, @output, $x );
807 :    
808 :     $ua = new LWP::UserAgent;
809 :     $ua->timeout( 900 );
810 :     if ($type =~/post/i)
811 :     {
812 : parrello 1.49 $args = [];
813 :     foreach $x (@$kv_pairs)
814 :     {
815 :     push(@$args, ( $x->[0], $x->[1]) );
816 :     }
817 :     my $request = POST $url, $args;
818 :     my $response = $ua->request($request);
819 :     $out = $response->content;
820 : efrank 1.1 }
821 :     else
822 :     {
823 : parrello 1.49 @args = ();
824 :     foreach $x (@$kv_pairs)
825 :     {
826 :     push( @args, "$x->[0]=" . uri_escape($x->[1]) );
827 :     }
828 :    
829 :     if (@args > 0)
830 :     {
831 :     $url .= "?" . join("&",@args);
832 :     }
833 :     $request = new HTTP::Request('GET', $url);
834 :     my $response = $ua->request($request);
835 :    
836 :     if ($response->is_success)
837 :     {
838 :     $out = $response->content;
839 :     }
840 :     else
841 :     {
842 :     $out = "<H1>Error: " . $response->code . "</H1>" . $response->message;
843 :     }
844 : efrank 1.1 }
845 :     # set up a document with proper eol characters
846 :     @output = split(/[\012\015]+/,$out);
847 :     foreach $out (@output) { $out .= "\n"; }
848 :    
849 :     # Now splice in a line of the form <base href=URL> to cause all relative links to work
850 :     # properly. Remove the header.
851 :    
852 :     for ($i=0; ($i < @output) && ($output[$i] !~ /^\s*\</); $i++) {}
853 :     if ($i < @output)
854 :     {
855 : parrello 1.49
856 :     splice(@output,0,$i);
857 : efrank 1.1 }
858 :    
859 :     for ($i=0; ($i < @output) && ($output[$i] !~ /\<body\>/i); $i++) {}
860 :     if ($i == @output)
861 :     {
862 : parrello 1.49 $i = -1;
863 : efrank 1.1 }
864 :     splice(@output,$i+1,0,"<base href=\"$url\">\n");
865 :     return @output;
866 :     }
867 :    
868 :     sub trim_output {
869 : olson 1.31 shift if UNIVERSAL::isa($_[0],__PACKAGE__);
870 : efrank 1.1 my($out) = @_;
871 :     my $i;
872 :    
873 :     for ($i=0; ($i < @$out) && ($out->[$i] !~ /^\</); $i++) {}
874 :     splice(@$out,0,$i);
875 :    
876 :     for ($i=0; ($i < @$out) && ($out->[$i] !~ /\<body\>/i); $i++) {}
877 :     if ($i == @$out)
878 :     {
879 : parrello 1.49 for ($i=0; ($i < @$out) && ($out->[$i] !~ /\<html\>/i); $i++) {}
880 :     if ($i == @$out)
881 :     {
882 :     $i = -1;
883 :     }
884 : efrank 1.1 }
885 :     for ($j=$i+1; ($j < @$out) && ($out->[$j] !~ /^\<hr\>$/); $j++) {}
886 :     if ($j < @$out)
887 :     {
888 : parrello 1.49 splice(@$out,$i+1,($j-$i));
889 : efrank 1.1 }
890 :    
891 :     for ($i=0; ($i < @$out) && ($out->[$i] !~ /\<\/body\>/i); $i++) {}
892 :     if ($i == @$out)
893 :     {
894 : parrello 1.49 for ($i=0; ($i < @$out) && ($out->[$i] !~ /\<\/html\>/i); $i++) {}
895 : efrank 1.1 }
896 :    
897 :     for ($j=$i-1; ($j > 0) && ($out->[$j] !~ /FIG search/); $j--) {}
898 :     if ($j > 0)
899 :     {
900 : parrello 1.49 my @tmp = `cat $html_tail_file`;
901 :     my $n = @tmp;
902 :     splice(@$out,$j-$n,$n+1);
903 : efrank 1.1 }
904 :     }
905 :    
906 :     sub set_prot_links {
907 : olson 1.31 shift if UNIVERSAL::isa($_[0],__PACKAGE__);
908 : efrank 1.1 my($cgi,$x) = @_;
909 :     my($before,$match,$after);
910 :    
911 : overbeek 1.11 if ($x =~ /^(.*)(fig\|\d+\.\d+\.peg\.\d+)(.*)/s)
912 : efrank 1.1 {
913 : parrello 1.49 $before = $1;
914 :     $match = $2;
915 :     $after = $3;
916 :     return &set_prot_links($cgi,$before) . &HTML::fid_link($cgi,$match) . &set_prot_links($cgi,$after);
917 : efrank 1.1 }
918 : overbeek 1.19 elsif ($x =~ /^(.*)\b([NXYZA]P_[0-9\.]+)\b(.*)/s)
919 :     {
920 : parrello 1.49 $before = $1;
921 :     $match = $2;
922 :     $after = $3;
923 :     return &set_prot_links($cgi,$before) . &HTML::refseq_link($cgi,$match) . &set_prot_links($cgi,$after);
924 : overbeek 1.19 }
925 : overbeek 1.11 elsif ($x =~ /^(.*)(gi\|\d+)(.*)/s)
926 : efrank 1.1 {
927 : parrello 1.49 $before = $1;
928 :     $match = $2;
929 :     $after = $3;
930 :     return &set_prot_links($cgi,$before) . &HTML::gi_link($cgi,$match) . &set_prot_links($cgi,$after);
931 : efrank 1.1 }
932 : overbeek 1.44 elsif ($x =~ /^(.*)(tigr\|[0-9a-zA-Z]+)(.*)/s)
933 :     {
934 : parrello 1.49 $before = $1;
935 :     $match = $2;
936 :     $after = $3;
937 :     return &set_prot_links($cgi,$before) . &HTML::tigr_link($cgi,$match) . &set_prot_links($cgi,$after);
938 : overbeek 1.44 }
939 : overbeek 1.14 elsif ($x =~ /^(.*)(uni\|[A-Z0-9]{6})(.*)/s)
940 :     {
941 : parrello 1.49 $before = $1;
942 :     $match = $2;
943 :     $after = $3;
944 :     return &set_prot_links($cgi,$before) . &HTML::uni_link($cgi,$match) . &set_prot_links($cgi,$after);
945 : overbeek 1.14 }
946 : overbeek 1.11 elsif ($x =~ /^(.*)(sp\|[A-Z0-9]{6})(.*)/s)
947 : efrank 1.1 {
948 : parrello 1.49 $before = $1;
949 :     $match = $2;
950 :     $after = $3;
951 :     return &set_prot_links($cgi,$before) . &HTML::sp_link($cgi,$match) . &set_prot_links($cgi,$after);
952 : efrank 1.1 }
953 : overbeek 1.11 elsif ($x =~ /^(.*)(pirnr\|NF\d+)(.*)/s)
954 : efrank 1.1 {
955 : parrello 1.49 $before = $1;
956 :     $match = $2;
957 :     $after = $3;
958 :     return &set_prot_links($cgi,$before) . &HTML::pir_link($cgi,$match) . &set_prot_links($cgi,$after);
959 : efrank 1.1 }
960 : overbeek 1.12 elsif ($x =~ /^(.*)(kegg\|[a-z]{2,4}:[a-zA-Z_0-9]+)(.*)/s)
961 :     {
962 : parrello 1.49 $before = $1;
963 :     $match = $2;
964 :     $after = $3;
965 :     return &set_prot_links($cgi,$before) . &HTML::kegg_link($cgi,$match) . &set_prot_links($cgi,$after);
966 : overbeek 1.12 }
967 : efrank 1.1 return $x;
968 :     }
969 :    
970 : overbeek 1.19 sub refseq_link {
971 : olson 1.31 shift if UNIVERSAL::isa($_[0],__PACKAGE__);
972 : overbeek 1.19 my($cgi,$id) = @_;
973 :    
974 :     if ($id =~ /^[NXYZA]P_/)
975 :     {
976 : parrello 1.49 return "<a href=http://www.ncbi.nlm.nih.gov/entrez/query.fcgi?db=protein&cmd=search&term=$id>$id</a>";
977 : overbeek 1.19 }
978 :     }
979 :    
980 : efrank 1.1 sub gi_link {
981 : olson 1.31 shift if UNIVERSAL::isa($_[0],__PACKAGE__);
982 : efrank 1.1 my($cgi,$gi) = @_;
983 :    
984 :     if ($gi =~ /^gi\|(\d+)$/)
985 :     {
986 : parrello 1.49 return "<a href=http://www.ncbi.nlm.nih.gov:80/entrez/query.fcgi?cmd=Retrieve&db=Protein&list_uids=$1&dopt=GenPept>$gi</a>";
987 : efrank 1.1 }
988 :     return $gi;
989 :     }
990 :    
991 : overbeek 1.44 sub tigr_link {
992 :     shift if UNIVERSAL::isa($_[0],__PACKAGE__);
993 :     my($cgi,$tigr) = @_;
994 :    
995 :     if ($tigr =~ /^tigr\|([0-9a-zA-Z]+)$/)
996 :     {
997 : parrello 1.49 return "<a href=\"http://pathema.tigr.org/tigr-scripts/CMR/GenePage.cgi?locus=$1\">$tigr</a>";
998 : overbeek 1.44 }
999 :     return $tigr;
1000 :     }
1001 :    
1002 : overbeek 1.14 sub uni_link {
1003 : olson 1.31 shift if UNIVERSAL::isa($_[0],__PACKAGE__);
1004 : overbeek 1.14 my($cgi,$uni) = @_;
1005 :    
1006 :     if ($uni =~ /^uni\|(\S+)$/)
1007 :     {
1008 : parrello 1.49 return "<a href=http://www.pir.uniprot.org/cgi-bin/upEntry?id=$1>$uni</a>";
1009 : overbeek 1.14 }
1010 :     return $uni;
1011 :     }
1012 :    
1013 : efrank 1.1 sub sp_link {
1014 : olson 1.31 shift if UNIVERSAL::isa($_[0],__PACKAGE__);
1015 : efrank 1.1 my($cgi,$sp) = @_;
1016 :    
1017 :     if ($sp =~ /^sp\|(\S+)$/)
1018 :     {
1019 : parrello 1.49 return "<a href=http://us.expasy.org/cgi-bin/get-sprot-entry?$1>$sp</a>";
1020 : efrank 1.1 }
1021 :     return $sp;
1022 :     }
1023 :    
1024 :     sub pir_link {
1025 : olson 1.31 shift if UNIVERSAL::isa($_[0],__PACKAGE__);
1026 : efrank 1.1 my($cgi,$pir) = @_;
1027 :    
1028 :     if ($pir =~ /^pirnr\|(NF\d+)$/)
1029 :     {
1030 : parrello 1.49 return "<a href=http://pir.georgetown.edu/cgi-bin/nfEntry.pl?id=$1>$pir</a>";
1031 : efrank 1.1 }
1032 :     return $pir;
1033 :     }
1034 :    
1035 : overbeek 1.12 sub kegg_link {
1036 : olson 1.31 shift if UNIVERSAL::isa($_[0],__PACKAGE__);
1037 : overbeek 1.12 my($cgi,$kegg) = @_;
1038 :    
1039 :     if ($kegg =~ /^kegg\|([^:]+):(\S+)$/)
1040 :     {
1041 : parrello 1.49 return "<a href=http://www.genome.ad.jp/dbget-bin/www_bget?$1+$2>$kegg</a>";
1042 : overbeek 1.12 }
1043 :     return $kegg;
1044 :     }
1045 :    
1046 : overbeek 1.11 sub set_map_links {
1047 : olson 1.31 shift if UNIVERSAL::isa($_[0],__PACKAGE__);
1048 : overbeek 1.11 my($cgi,$x) = @_;
1049 :     my($before,$match,$after);
1050 : efrank 1.1
1051 : overbeek 1.11 my $org = ($cgi->param('org') || $cgi->param('genome') || "");
1052 :    
1053 :     if ($x =~ /^(.*)(MAP\d+)(.*)/s)
1054 :     {
1055 : parrello 1.49 $before = $1;
1056 :     $match = $2;
1057 :     $after = $3;
1058 :     return &set_map_links($cgi,$before) . &map_link($cgi,$match,$org) . &set_map_links($cgi,$after);
1059 : overbeek 1.11 }
1060 :     return $x;
1061 :     }
1062 :    
1063 :     sub map_link {
1064 : olson 1.31 shift if UNIVERSAL::isa($_[0],__PACKAGE__);
1065 : overbeek 1.11 my($cgi,$map,$org) = @_;
1066 :    
1067 :     $user = $cgi->param('user');
1068 :     $user = $user ? $user : "";
1069 :     $org = $org ? $org : "";
1070 : olson 1.51
1071 : overbeek 1.64 my $url = "show_kegg_map.cgi?user=$user&map=$map&org=$org";
1072 :     #rel my $url = &FIG::cgi_url() . "/show_kegg_map.cgi?user=$user&map=$map&org=$org";
1073 : overbeek 1.11 my $link = "<a href=\"$url\">$map</a>";
1074 :     return $link;
1075 :     }
1076 : redwards 1.20
1077 :     sub java_buttons {
1078 : olson 1.31 shift if UNIVERSAL::isa($_[0],__PACKAGE__);
1079 : redwards 1.20 ## ADDED BY RAE
1080 :     # Provides code to include check all/first half/second half/none for javascrspt
1081 :     # this takes two variables - the form name provided in start_form with the
1082 :     # -name => field and the checkbox name
1083 :     my ($form, $button)=@_;
1084 :    
1085 :     $java_script="<input type=\"button\" name=\"CheckAll\" value=\"Check All\"\nonClick=\"checkAll(document.$form.$button)\">\n";
1086 :     $java_script.="<input type=\"button\" name=\"CheckFirst\" value=\"Check First Half\"\nonClick=\"checkFirst(document.$form.$button)\">\n";
1087 :     $java_script.="<input type=\"button\" name=\"CheckSecond\" value=\"Check Second Half\"\nonClick=\"checkSecond(document.$form.$button)\">\n";
1088 :     $java_script.="<input type=\"button\" name=\"UnCheckAll\" value=\"Uncheck All\"\nonClick=\"uncheckAll(document.$form.$button)\">\n";
1089 : parrello 1.49
1090 : redwards 1.20 return $java_script;
1091 :     }
1092 :    
1093 : overbeek 1.22 sub sub_link {
1094 : olson 1.31 shift if UNIVERSAL::isa($_[0],__PACKAGE__);
1095 : overbeek 1.22 my($cgi,$sub) = @_;
1096 :     my($sub_link);
1097 :    
1098 :     my $user = $cgi->param('user');
1099 :     if ($user)
1100 :     {
1101 : parrello 1.49 my $esc_sub = uri_escape( $sub );
1102 :     $sub_link = "<a href=./subsys.cgi?ssa_name=$esc_sub&request=show_ssa&user=$user>$sub</a>";
1103 : overbeek 1.22 }
1104 :     else
1105 :     {
1106 : parrello 1.49 $sub_link = $sub;
1107 : overbeek 1.22 }
1108 :     return $sub_link;
1109 :     }
1110 :    
1111 : overbeek 1.50 sub reaction_link {
1112 :     my($reaction) = @_;
1113 :    
1114 :     if ($reaction =~ /^R\d+/)
1115 :     {
1116 :     return "<a href=\"http://www.genome.ad.jp/dbget-bin/www_bget?rn+$reaction\" target=reaction$$>$reaction</a>";
1117 :     }
1118 :     return $reaction;
1119 :     }
1120 :    
1121 : overbeek 1.53 sub html_for_assignments {
1122 :     my($fig,$user,$peg_sets) = @_;
1123 :     my $i;
1124 :    
1125 :     my @vals = ();
1126 :     my $set = 1;
1127 :     foreach $peg_set (@$peg_sets)
1128 :     {
1129 :     for ($i=0; ($i < @$peg_set); $i++)
1130 :     {
1131 :     $peg = $peg_set->[$i];
1132 :     push(@vals,'show=' . join("@",($set,$i+1,$peg,&FIG::abbrev($fig->org_of($peg)),"")));
1133 :     }
1134 :     $set++;
1135 :     }
1136 :    
1137 :     $ENV{'REQUEST_METHOD'} = 'GET';
1138 :     $ENV{'QUERY_STRING'} = join('&',('request=show_commentary',"uni=1","user=$user",@vals));
1139 :     my $out = join("",`$FIG_Config::fig/CGI/chromosomal_clusters.cgi`);
1140 :     $out =~ s/^.*?<form/<form/si;
1141 :     $out =~ s/^(.*)<table.*/$1/si;
1142 :     return $out;
1143 :     }
1144 :    
1145 : redwards 1.55 =head1 rss_feed
1146 :    
1147 :     Add something to the RSS feed. The rss feeds are stored in the Html directory, and there are several RSS feeds:
1148 :     SEED.rss - everything gets written here
1149 :     SEEDgenomes.rss - whenever a genome is added to the SEED
1150 :     SEEDsubsystems.rss - whenever a subsystem is edited (or should this be added?)
1151 :    
1152 :    
1153 :     RSS feeds must contain a title, description, and link. The title is what is seen e.g. from the firefox or safari pull down menu. The description is seen from within an rss aggregator, and may be displayed on web pages and so on.
1154 :    
1155 :     The method takes a reference to an array containing the file names for the RSS feeds to add your item to, and a hash of items for the xml. Only title, description, and link are required tags in the XML.
1156 :    
1157 :     The file names are the full name of the file, eg SEEDsubsystems.rss, SEEDgenomes.rss. Be aware that this is a file name, though, so don't uses special characters. The path will be added.
1158 :    
1159 :     The has can have these keys:
1160 :    
1161 :     REQUIRED:
1162 :     title : the title. This is usually what is seen by the user in the pull down menu
1163 :     description : a more complete description that is often seen is rss viewers but not always
1164 :     link : link to the item that was added/edited
1165 : redwards 1.65 All other keys are treated as optional RSS arguments and written to the file.
1166 :    
1167 :     At most, $max_entries recent entries are stored in the rss file, and this is currently 50.
1168 : redwards 1.55
1169 :     RSS files are quite simple, and contain some standard header information, and then individual items surrounded by an <item> </item> tag. Note that there is also an initial title/description/link set that describes the file.
1170 :    
1171 :    
1172 :     =cut
1173 :    
1174 :     sub rss_feed {
1175 :     shift if UNIVERSAL::isa($_[0],__PACKAGE__);
1176 :     my ($files, $args)=@_;
1177 : redwards 1.65
1178 :     # how many entries to store in the file
1179 :     my $max_entries=50;
1180 :    
1181 : redwards 1.55 foreach my $a (keys %$args) {if ($a =~ /^-(.*)/) {my $b=$1; $args->{$b}=$args->{$a}; delete $args->{$a}}}
1182 :    
1183 :     my $filepath=$FIG_Config::fig."/CGI/Html/rss";
1184 :     # check for the directory and if not, make it
1185 :     mkdir $filepath unless (-d $filepath);
1186 :    
1187 :     # note that $info is a hash of references to hashes that are written out as headers in the file
1188 :     my $info=
1189 :     {
1190 :     "SEED.rss" =>
1191 :     {
1192 :     title => "The SEED",
1193 :     description => "Latest news from the SEED",
1194 : overbeek 1.66 link => "Html/rss/SEED.rss",
1195 : redwards 1.55 },
1196 :    
1197 :     "SEEDsubsystems.rss" =>
1198 :     {
1199 :     title => "SEED Subsystems",
1200 :     description => "Recently updated SEED subsystems",
1201 : overbeek 1.66 link => "Html/rss/SEEDsubsystems.rss",
1202 : redwards 1.55 },
1203 :    
1204 :     "SEEDsubsystems.rss" =>
1205 :     {
1206 :     title => "SEED Genomes",
1207 :     description => "Genomes recently added to the SEED",
1208 :     link => &FIG::cgi_url()."/Html/rss/SEEDsubsystems.rss",
1209 :     },
1210 :    
1211 :     };
1212 :    
1213 :    
1214 :     # build the new xml
1215 :     my $xml = "\t<item>\n";
1216 :     foreach my $qw ("title", "description", "link") {
1217 :     unless ($args->{$qw}) {
1218 :     print STDERR "You need to include a $qw tag in your RSS description\n";
1219 :     return(0);
1220 :     }
1221 :     # we need to do something a bit funky with the link. We can't have ampersands in the <link> </link> in valid html
1222 :     # so we are going to pull out the links and uri_escape just the part after the .cgi
1223 :     if ($qw eq "link")
1224 :     {
1225 :     $args->{$qw} =~ /^(.*?\.cgi.)(.*)$/;
1226 :     $args->{$qw} = $1.uri_escape($2) if ($1 && $2);
1227 :     }
1228 :    
1229 :     $xml .= "\t\t<$qw>".$args->{$qw}."</$qw>\n";
1230 :     delete $args->{$qw};
1231 :     }
1232 :    
1233 :     foreach my $tag (grep {!/type/i} keys %$args)
1234 :     {
1235 :     $xml .= "\t\t<$tag>".$args->{$tag}."</$tag>\n";
1236 :     }
1237 :    
1238 :     $xml .= "\t</item>\n";
1239 :    
1240 :    
1241 :     my @files=("SEED.rss");
1242 :     if ($args->{"type"}) {push @files, "SEED.$type.rss"}
1243 :    
1244 :     foreach my $file ("SEED.rss", @$files)
1245 :     {
1246 :     if (-e "$filepath/$file")
1247 :     {
1248 :     my @out; # the new content of the file
1249 :     my $itemcount=0; # how many <item> </item>'s are we keeping
1250 :     my $initem; # are we in an item?
1251 :     open(IN, "$filepath/$file") || die "Can't open $filepath/$file";
1252 :     while (<IN>)
1253 :     {
1254 :     if (/\<item\>/) {
1255 :     push @out, $xml, unless ($itemcount);
1256 :     $itemcount++;
1257 :     $initem=1;
1258 :     }
1259 : redwards 1.65 if (/\<\/item\>/) {$initem=0; next if ($itemcount > $max_entries)}
1260 :     next if ($initem && $itemcount > $max_entries);
1261 : redwards 1.55 push @out, $_;
1262 :     }
1263 :     close IN;
1264 :     open(OUT, ">$filepath/$file") || die "Can't open $filepath/$file for writing";
1265 :     print OUT @out;
1266 :     }
1267 :     else
1268 :     {
1269 :     open(OUT, ">$filepath/$file") || die "Can't open $filepath/$file for writing";
1270 :     print OUT "<?xml version=\"1.0\"?>\n<rss version=\"2.0\">\n<channel>\n";
1271 :     if ($info->{$file})
1272 :     {
1273 :     # we're going to sanity check each of the three options we output, just to be sure
1274 :     foreach my $qw ("title", "description", "link")
1275 :     {
1276 :     if ($info->{$file}->{$qw})
1277 :     {
1278 :     print OUT "<$qw>", $info->{$file}->{$qw}, "</$qw>\n";
1279 :     } else {
1280 :     print STDERR "Please add a $qw for $file\n"; print OUT "<$qw>$file</$qw>\n";
1281 :     }
1282 :     }
1283 :     }
1284 :     else {
1285 :     print STDERR "Please define title, link, and description information for $file\n";
1286 :     print OUT "<title>$file</title>\n<description>An RSS feed</description>\n<link>", &FIG::cgi_url, "</link>\n";
1287 :     }
1288 :     print OUT "\n", $xml;
1289 :     print OUT "\n", "</channel>\n</rss>\n"
1290 :     }
1291 :     }
1292 :     }
1293 : redwards 1.61
1294 :    
1295 :    
1296 : redwards 1.55 1;
1297 :    

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3