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

Annotation of /FigKernelPackages/HTML.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3