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

Annotation of /FigKernelPackages/HTML.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3