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

Diff of /FigKernelPackages/HTML.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.6, Sun Mar 21 02:20:55 2004 UTC revision 1.74, Tue Dec 20 17:59:09 2005 UTC
# Line 1  Line 1 
1    #
2    # Copyright (c) 2003-2006 University of Chicago and Fellowship
3    # for Interpretations of Genomes. All Rights Reserved.
4    #
5    # This file is part of the SEED Toolkit.
6    #
7    # The SEED Toolkit is free software. You can redistribute
8    # it and/or modify it under the terms of the SEED Toolkit
9    # Public License.
10    #
11    # You should have received a copy of the SEED Toolkit Public License
12    # along with this program; if not write to the University of Chicago
13    # at info@ci.uchicago.edu or the Fellowship for Interpretation of
14    # Genomes at veronika@thefig.info or download a copy from
15    # http://www.theseed.org/LICENSE.TXT.
16    #
17    
18  package HTML;  package HTML;
19    
20    use Tracer;
21    use FIG;
22  use Carp;  use Carp;
23  use Data::Dumper;  use Data::Dumper;
24  use LWP::UserAgent;  use LWP::UserAgent;
25  use LWP::Simple;  use LWP::Simple;
26    use URI::Escape;  # uri_escape()
27  use URI::URL;  use URI::URL;
28  use HTTP::Request::Common;  use HTTP::Request::Common;
29    use POSIX;
30    
31    
32    my $top_link_cache;
33    
34    
35    sub new
36    {
37        my($class) = @_;
38    
39        my $self = {};
40    
41        return bless $self, $class;
42    }
43    
44    sub top_link
45    {
46    
47        #
48        # Determine if this is a toplevel cgi or one in one of the subdirs (currently
49        # just /p2p).
50        #
51    
52        return $top_link_cache if ($top_link_cache);
53    
54        my @parts = split(/\//, $ENV{SCRIPT_NAME});
55        my $top;
56        if ($parts[-2] eq 'FIG')
57        {
58            $top = '.';
59    #       warn "toplevel @parts\n";
60        }
61        elsif ($parts[-3] eq 'FIG')
62        {
63            $top = '..';
64    #       warn "subdir @parts\n";
65        }
66        else
67        {
68            $top = $FIG_Config::cgi_base;
69    #       warn "other @parts\n";
70        }
71    
72        $top_link_cache = $top;
73        return $top;
74    }
75    
76    sub compute_html_header
77    {
78        shift if UNIVERSAL::isa($_[0],__PACKAGE__);
79        my($additional_insert, $user, %options ) = @_;
80    
81        my $header_name = $options{header_name} ? $options{header_name} : "html.hdr";
82        my $tail_name = $options{tail_name} ? $options{tail_name} : "html.tail";
83    
84        my $html_hdr_file = "./Html/$header_name";
85        if (! -f $html_hdr_file)
86        {
87            $html_hdr_file = "$FIG_Config::fig/CGI/Html/$header_name";
88        }
89        my @html_hdr = &FIG::file_read($html_hdr_file);
90    
91        # for my $k (sort keys %ENV) { warn "$k = $ENV{$k}\n"; }
92    
93        #
94        # Determine if this is a toplevel cgi or one in one of the subdirs (currently
95        # just /p2p).
96        #
97    
98        my @parts = split(/\//, $ENV{SCRIPT_NAME});
99        my $top;
100        if ($parts[-2] eq 'FIG')
101        {
102            $top = '.';
103    #       warn "toplevel @parts\n";
104        }
105        elsif ($parts[-3] eq 'FIG')
106        {
107            $top = '..';
108    #       warn "subdir @parts\n";
109        }
110        else
111        {
112            $top = $FIG_Config::cgi_base;
113    #       warn "other @parts\n";
114        }
115    
116        $options{no_fig_search} or push( @html_hdr, "<br><a href=\"$top/index.cgi?user=$user\">FIG search</a>\n" );
117    
118        if (@html_hdr)
119        {
120            my $insert_stuff;
121    
122            if (not $options{no_release_info})
123            {
124                my @ver = &FIG::file_head("$FIG_Config::fig_disk/CURRENT_RELEASE", 1);
125                my $ver = $ver[0];
126                chomp $ver;
127                if ($ver =~ /^cvs\.(\d+)$/)
128                {
129                    my $d = asctime(localtime($1));
130                    chomp($d);
131                    $ver .=  " ($d)";
132                }
133                my $host = &FIG::get_local_hostname();
134                $insert_stuff = "SEED version <b>$ver</b> on $host";
135            }
136    
137            if ($additional_insert)
138            {
139                $insert_stuff .= "<br>" . $additional_insert;
140            }
141    
142            for $_ (@html_hdr)
143            {
144                s,(href|img\s+src)="/FIG/,$1="$top/,g;
145                    s,(\?user\=)\",$1$user",;
146                if ($_ eq "<!-- HEADER_INSERT -->\n")
147                {
148                    $_ = $insert_stuff;
149                }
150            }
151        }
152    
153        return @html_hdr;
154    }
155    
156  sub show_page {  sub show_page {
157      my($cgi,$html,$no_home) = @_;      #warn "SHOWPAGE: cgi=", Dumper(@_);
158        shift if UNIVERSAL::isa($_[0],__PACKAGE__);
159        my($cgi,$html,$no_home, $alt_header, $css, $javasrc, $cookie) = @_;
160      my $i;      my $i;
161    
162        my $top = top_link();
163    
164        # ARGUMENTS:
165        #     $cgi is the CGI method
166        #     $html is an array with all the html in it. It is just joined by "\n" (and not <br> or <p>
167        #     $no_home eliminates ONLY the bottom FIG search link in a page
168        #     $alt_header is a reference to an array for an alternate header banner that you can replace the standard one with
169        #     $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
170        #               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
171        #               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
172        #     $javasrc is a reference to an array of URLs to javascripts to be included (e.g. "/FIG/Html/css/styleswitcher.js")
173        #     $cookie is the name and value of the cookie to set. Note that you should probably use raelib->cookie to get/set your cookies
174      #      #
175      # Find the HTML header      # Find the HTML header
176      #      #
177    
178      my $html_hdr_file = "./Html/html.hdr";      my $html_tail_file = "./Html/$tail_name";
179      if (! -f $html_hdr_file)      if (! -f $html_tail_file)
180      {      {
181          $html_hdr_file = "$FIG_Config::fig/CGI/Html/html.hdr";          $html_tail_file = "$FIG_Config::fig/CGI/Html/$tail_name";
182      }      }
183    
184      my $html_tail_file = "./Html/html.tail";      my $user = $cgi->param('user') || "";
185      if (! -f $html_tail_file)      my @html_hdr;
186        if ($alt_header && ref($alt_header) eq "ARRAY")
187      {      {
188          $html_tail_file = "$FIG_Config::fig/CGI/Html/html.tail";         @html_hdr = @$alt_header;
189      }      }
190        else
191        {
192            @html_hdr = compute_html_header(undef,$user);
193        }
194    
195        # RAE: I am offloading the handling of cookies to CGI.pm since I don't know how they are set up.
196        # This modification adds the cookies if necessary
197    
198        # Note: 3/10/05 commented this line out pending the discussion of adding cookies into the seed that we are waiting to see about
199        # to add cookies back in replace these two header lines with each other
200    
201      print $cgi->header;      #print $cgi->header(-cookie=>$cookie);
202        print $cgi->header();
203    
204      #      #
205      #  The SEED header file goes immediately after <BODY>.  Figure out      #  The SEED header file goes immediately after <BODY>.  Figure out
# Line 46  Line 217 
217                       meta     => 1,                       meta     => 1,
218                       nextid   => 1,                       nextid   => 1,
219                       style    => 1,                       style    => 1,
220                       title    => 1                       title    => 1,
221                     );                     );
222    
223      #      #
# Line 89  Line 260 
260          if ( $html->[$i] =~ /\<body[^0-9a-z]/i )          if ( $html->[$i] =~ /\<body[^0-9a-z]/i )
261          {          {
262              $body_line = $i;              $body_line = $i;
263              $last;              last;
264          }          }
265    
266          #  Now the general case.          #  Now the general case.
# Line 145  Line 316 
316      #      #
317      #  <BODY> goes after last head line      #  <BODY> goes after last head line
318      #      #
319        #  RAE:
320        #  Added the javascript for the buttons immediately after body.
321        #  Note if no buttons are added we still (at the moment) add the script,
322        #  but it only adds a little text (495 characters) to the html and noone will notice!
323        #  RAE: This is now deprecated because everything is in an external file, FIG.js, included later
324    
325      if ( $body_line < 0 )      if ( $body_line < 0 )
326      {      {
# Line 156  Line 332 
332      #  Seed page header (if it exists) goes after <BODY>      #  Seed page header (if it exists) goes after <BODY>
333      #      #
334    
335      if ( -f $html_hdr_file )      if (@html_hdr)
336      {      {
337          splice( @$html, $body_line + 1, 0, `cat $html_hdr_file` );          splice( @$html, $body_line + 1, 0, @html_hdr );
338      }      }
339    
340      #      #
# Line 171  Line 347 
347          splice( @$html, $body_line, 0, "</HEAD>\n" );          splice( @$html, $body_line, 0, "</HEAD>\n" );
348      }      }
349    
350        # RAE:
351        # Add css here
352        # Note that at the moment I define these two sheets here. I think this should
353        # be moved out, but I want to try it and see what happens.  css has the format:
354        #
355        # <link rel='stylesheet' title='default' href='/css/default.css' type='text/css'>
356    
357        # convert the default key to the right case. and eliminate dups
358        foreach my $k (keys %$css) {if (lc($k) eq "default") {$css->{'Default'}=$css->{$k}}}
359    
360        if (!$css || !$css->{'Default'})
361        {
362           $css->{'Default'} = "$top/Html/css/default.css";
363        }
364        if (!$css->{"Sans Serif"})
365        {
366           $css->{'Sans Serif'} = "$top/Html/css/sanserif.css";
367        }
368    
369        my $csstext = "<link rel='stylesheet' title='default' href='".$css->{'Default'}."' type='text/css'>\n";
370        $csstext   .= "<link rel='alternate stylesheet' title='Sans Serif' href='".$css->{'Sans Serif'}."' type='text/css'>\n";
371    
372        foreach my $k (keys %$css)
373        {
374           next if (lc($k) eq "default" || lc($k) eq "sans serif");
375           $csstext .= "<link rel='alternate stylesheet' title='$k' href='".$css->{$k}."' type='text/css'>\n";
376        }
377    
378        $csstext   .= "<link rel='alternate'  title='SEED RSS feeds' href='$top/Html/rss/SEED.rss' type='application/rss+xml'>\n";
379    
380        # RAE: also added support for external javascripts here.
381        # we are cluttering the HTML code with all the javascripts when they could easily be in external files
382        # this solution allows us to source other files
383    
384        # the file FIG.js contains most of the java script we use routinely. Every browser will just cache this and so
385        # it will reduce our overhead.
386    
387        # $javasrc must be a ref to an array with urls (absolute or relative) to the javascripts
388        push @$javasrc, "$top/Html/css/FIG.js";
389        foreach my $script (@$javasrc) {
390            $csstext .= "<script src=\"$script\" type=\"text/javascript\"></script>\n";
391        }
392    
393    
394    
395        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.
396    
397      #      #
398      #  <BASE ...> goes before </HEAD>      #  <BASE ...> goes before </HEAD>
399      #      #
# Line 183  Line 406 
406          #  only, or every update?), I provide an alternative derivation          #  only, or every update?), I provide an alternative derivation
407          #  from $cgi_url. -- GJO          #  from $cgi_url. -- GJO
408          #          #
409            # BASE href needs to be absolute. RDO.
410          my $base_url = $FIG_Config::cgi_base;          #
411          if ( ! $base_url )                      # if cgi_base was not defined          #
412          {          $base_url = &FIG::cgi_url;
413              $base_url = $FIG_Config::cgi_url;   # get the full cgi url  #       my $base_url = $FIG_Config::cgi_base;
414              $base_url =~ s~^http://[^/]*~~;     # remove protocol and host  #       if ( ! $base_url )                      # if cgi_base was not defined
415              $base_url =~ m~/$~ || $base_url =~ s~$~/~; # check trailing slash  #       {
416          }  #           $base_url = $FIG_Config::cgi_url;   # get the full cgi url
417    #           $base_url =~ s~^http://[^/]*~~;     # remove protocol and host
418    #           $base_url =~ m~/$~ || $base_url =~ s~$~/~; # check trailing slash
419    #       }
420    
421          $base_line = $head_end_line;          $base_line = $head_end_line;
422          splice( @$html, $base_line, 0, "<BASE href=\"$base_url\">\n" );          #
423            # RDO 2005-1006. Remove this so proxying works better.
424            #
425    #        splice( @$html, $base_line, 0, "<BASE href=\"$base_url/\">\n" );
426      }      }
427    
428      #      #
# Line 228  Line 457 
457      }      }
458    
459      #      #
460        # See if we have a site-specific tail (for disclaimers, etc).
461        #
462    
463        my $site_tail = "$FIG_Config::fig_disk/config/site_tail.html";
464        my $site_fh;
465        if (open($site_fh, "<$site_tail"))
466        {
467            push(@tail, <$site_fh>);
468            close($site_fh);
469        }
470    
471        #
472      #  Figure out where to insert The SEED tail.  Before </body>,      #  Figure out where to insert The SEED tail.  Before </body>,
473      #  or before </html>, or at end of page.      #  or before </html>, or at end of page.
474      #      #
   
475      my @tags = ();      my @tags = ();
476        # Check for a tracing queue.
477        my $traceString = QTrace("HTML");
478        if ($traceString) {
479            push @tags, $traceString;
480        }
481      for ($i=0; ($i < @$html) && ($html->[$i] !~ /\<\/body\>/i); $i++) {}      for ($i=0; ($i < @$html) && ($html->[$i] !~ /\<\/body\>/i); $i++) {}
482      if ($i >= @$html)        # </body> not found; look for </html>      if ($i >= @$html)        # </body> not found; look for </html>
483      {      {
# Line 255  Line 499 
499          splice( @$html, $i, 0, @tags );          splice( @$html, $i, 0, @tags );
500      }      }
501    
502      print @$html;      # RAE the chomp will return any new lines at the ends of elements in the array,
503        # and then we can join  with a "\n". This is because somethings put newlines in,
504        # and others don't. This should make nicer looking html
505        #
506        # chomp(@$html);
507        # print join "\n", @$html;
508        #
509        # Apparently the above still breaks things. This is the correct code:
510    
511        foreach $_ (@$html)
512        {
513            print $_;
514        }
515    
516  }  }
517    
518  sub make_table {  sub make_table {
519      my($col_hdrs,$tab,$title,$instr) = @_;      my($col_hdrs,$tab,$title, %options ) = @_;
520      my(@tab);      my(@tab);
521    
522      push( @tab, "\n<table border>\n",      my $border = defined $options{border} ? "border=\"$options{border}\"" : "border";
523        my $width = defined $options{width} ? "width=\"$options{width}\"" : undef;
524        my $class = defined $options{class} ? "class=\"$options{class}\"" : undef;
525        push( @tab, "\n<table $border $width $class>\n",
526                  "\t<caption><b>$title</b></caption>\n",                  "\t<caption><b>$title</b></caption>\n",
527                  "\t<tr>\n\t\t<th>"                  "\t<tr>\n\t\t"
528                . join( "</th>\n\t\t<th>", @$col_hdrs )                . join( "\n", map { &expand($_, "th") } @$col_hdrs )
529                . "</th>\n\t</tr>\n"                . "\n\t</tr>\n"
530          );          );
531      my($i,$nowrap);      my($i);
   
     for ($i=0; ($i < @$instr) && ($instr->[$i] !~ /nowrap/); $i++) {}  
     $nowrap = ($i == @$instr) ? "" : " nowrap";  
532    
533      my $row;      my $row;
534      foreach $row (@$tab)      foreach $row (@$tab)
535      {      {
536          push( @tab, "\t<tr>\n"          push( @tab, "\t<tr>\n"
537                    . join( "\n", map { &expand($_,$nowrap) } @$row )                    . join( "\n", map { &expand($_) } @$row )
538                    . "\n\t</tr>\n"                    . "\n\t</tr>\n"
539              );              );
540      }      }
# Line 285  Line 542 
542      return join("",@tab);      return join("",@tab);
543  }  }
544    
545    sub abstract_coupling_table {
546        my($cgi,$coupling) = @_;
547    
548        my $col_hdrs = ["coupled to","Score","Type of Coupling", "Type-specific Data"];
549        my $tab = [];
550        foreach my $x (@$coupling)
551        {
552            my($peg2,$psc,$type,$extra) = @$x;
553            push(@$tab,[&fid_link($cgi,$peg2,1),$psc,$type,&set_prot_links($cgi,join(", ",@$extra))]);
554        }
555        return &make_table($col_hdrs,$tab,"Abstract Coupling Data");
556    }
557    
558  sub expand {  sub expand {
559      my($x,$nowrap) = @_;      shift if UNIVERSAL::isa($_[0],__PACKAGE__);
560        my( $x, $tag ) = @_;
561    
562        $tag = "td" unless $tag;
563        my $endtag = $tag;
564    
565        # RAE modified this so that you can pass in a reference to an array where
566        # the first element is the data to display and the second element is optional
567        # things like colspan and align. Note that in this case you need to include the td
568        # use something like ["some data to appear", "td colspan=4 bgcolor=gray"]
569    
570      if ($x =~ /^\@([^:]+)\:([^:]+)\:(.*)$/)      # per GJO's request modified this line so it can take any tag.
571        if ( ref($x) eq "ARRAY" ) { ($x, $tag) = @$x; $tag =~ /^(\S+)/; $endtag = $1 }
572    
573        if ( $x =~ /^\@([^:]+)\:(.*)$/ )
574      {      {
575          return "\t\t<td$nowrap $1=\"$2\">$3</td>";          return "\t\t<$tag $1>$2</$endtag>";
576      }      }
577      else      else
578      {      {
579          return "\t\t<td$nowrap>$x</td>";          return "\t\t<$tag>$x</$endtag>";
580        }
581    }
582    
583    
584    =head2 merge_table_rows()
585    
586    Merge table rows together. This will merge a table so that adjacent cells with the same content will only be shown once.
587    
588    Something like this:
589    
590        -----------------------
591        |    1     |    a     |
592        -----------------------
593        |    1     |    b     |
594        -----------------------
595        |    2     |    c     |
596        -----------------------
597        |    3     |    d     |
598        -----------------------
599        |    4     |    d     |
600        -----------------------
601        |    5     |    d     |
602        -----------------------
603    
604    Will become:
605    
606        -----------------------
607        |          |    a     |
608        |    1     |-----------
609        |          |    b     |
610        -----------------------
611        |    2     |    c     |
612        -----------------------
613        |    3     |          |
614        ------------          |
615        |    4     |    5     |
616        ------------          |
617        |    5     |          |
618        -----------------------
619    
620    
621    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.
622    
623     $tab=&HTML::merge_table_rows($tab);
624    
625     or
626    
627     $skip=(1=>1, 3=>1, 5=>1);
628     $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
629    
630    
631    =cut
632    
633    
634    
635    
636    sub merge_table_rows {
637     # RAE:
638     # Experimental piece of code. We often want to have rows or columns were cells are merged. It just looks so much nicer
639     # this block should merge adjacent rows that have the same text in them.
640     # use like this:
641     #      $tab=&HTML::merge_table_rows($tab);
642     # before you do a make_table call
643    
644     my $self=shift if UNIVERSAL::isa($_[0],__PACKAGE__);
645     my ($tab, $skip)=@_;
646    
647     my $newtable;
648     my $lastrow;
649     my $rowspan;
650     my $refs;
651    
652     for (my $y=0; $y <= $#$tab; $y++) {
653     #$y is the row in the table;
654      for (my $x=0; $x <= $#{$tab->[$y]}; $x++) {
655       # this is the user definable columns not to merge
656       if ($skip->{$x})
657       {
658        $newtable->[$y]->[$x] = $tab->[$y]->[$x];
659        next;
660       }
661    
662       #$x is the column in the table
663       # if the column in the row we are looking at is the same as the column in the previous row, we don't add
664       # this cell to $newtable. Instead we increment the rowspan of the previous row by one
665    
666       # handle cells that are references to arrays
667       if (ref($tab->[$y]->[$x]) eq "ARRAY") {$refs->[$y]->[$x]=$tab->[$y]->[$x]->[1]; $tab->[$y]->[$x]=$tab->[$y]->[$x]->[0]}
668    
669       # now we go back through the table looking where to draw the merge line:
670       my $lasty=$y;
671       while ($lasty >= 0 && $tab->[$y]->[$x] eq $tab->[$lasty]->[$x]) {$lasty--}
672       $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
673       if ($lasty == $y) {
674        # we always want to have something in rows that may otherwise be empty but should be there (see below)
675        unless ($tab->[$y]->[$x]) {$tab->[$y]->[$x]=" &nbsp; "}
676        $newtable->[$y]->[$x] = $tab->[$y]->[$x];
677       }
678       else {$rowspan->[$lasty]->[$x]++}
679      }
680     }
681    
682     # now just join everything back together
683     for (my $y=0; $y <= $#$tab; $y++) {
684      for (my $x=0; $x <= $#{$tab->[$y]}; $x++) {
685       if ($rowspan->[$y]->[$x]) {
686        if ($refs->[$y]->[$x]) {$refs->[$y]->[$x] .= " rowspan=". ($rowspan->[$y]->[$x]+1)}
687        else {$refs->[$y]->[$x] = "td rowspan=". ($rowspan->[$y]->[$x]+1)}
688        $newtable->[$y]->[$x]=[$newtable->[$y]->[$x], $refs->[$y]->[$x]];
689       }
690       elsif ($newtable->[$y]->[$x] && $refs->[$y]->[$x]) {
691        $newtable->[$y]->[$x]=[$newtable->[$y]->[$x], $refs->[$y]->[$x]];
692       }
693      }
694     }
695    
696    
697     # finally we have to remove any completely empty cells that have been added by the array mechanism
698     # (e.g. if you define $a->[2] then $a->[0] and $a->[1] are now undef).
699     # that is why in the loop above I replace empty cells with nbsp. They are now not undef!
700     # I am sure that Gary can do this in one line, but I am hacking.
701     my @trimmed;
702     foreach my $a (@$newtable) {
703      my @row;
704      foreach my $b (@$a) {
705       push @row, $b if ($b);
706      }
707      push @trimmed, \@row;
708     }
709    
710     return \@trimmed;
711    }
712    
713    
714    
715    
716    sub set_ec_links {
717        shift if UNIVERSAL::isa($_[0],__PACKAGE__);
718        my($cgi,$x) = @_;
719        my($before,$match,$after);
720    
721        if ($x =~ /^(.*)(EC \d+\.\d+\.\d+\.\d+)(.*)/s)
722        {
723            $before = $1;
724            $match = $2;
725            $after = $3;
726            return &set_ec_links($cgi,$before) . &HTML::ec_link($match) . &set_ec_links($cgi,$after);
727      }      }
728        return $x;
729  }  }
730    
731  sub ec_link {  sub ec_link {
732        shift if UNIVERSAL::isa($_[0],__PACKAGE__);
733      my($role) = @_;      my($role) = @_;
734    
735      if ($role =~ /(\d+\.\d+\.\d+\.\d+)/)      if ($role =~ /(\d+\.\d+\.\d+\.\d+)/)
# Line 312  Line 743 
743  }  }
744    
745  sub role_link {  sub role_link {
746        shift if UNIVERSAL::isa($_[0],__PACKAGE__);
747      my($cgi,$role) = @_;      my($cgi,$role) = @_;
748    
749      my $roleR = ($role =~ /^(\d+\.\d+\.\d+\.\d+)\s+-\s+/) ? $1 : $role;      my $roleR = ($role =~ /^(\d+\.\d+\.\d+\.\d+)\s+-\s+/) ? $1 : $role;
# Line 322  Line 754 
754      return "<a href=$link>$role</a>";      return "<a href=$link>$role</a>";
755  }  }
756    
757    #
758    # Local means to eliminate the fig|org.peg from the
759    # text of the link.
760    #
761  sub fid_link {  sub fid_link {
762        shift if UNIVERSAL::isa($_[0],__PACKAGE__);
763      my($cgi,$fid,$local,$just_url) = @_;      my($cgi,$fid,$local,$just_url) = @_;
764      my($n);      my($n);
765    
766        my $top = top_link();
767    
768      if ($fid =~ /^fig\|\d+\.\d+\.([a-z]+)\.(\d+)/)      if ($fid =~ /^fig\|\d+\.\d+\.([a-z]+)\.(\d+)/)
769      {      {
770          if ($local)          if ($local)
# Line 343  Line 782 
782          {          {
783              $n = $fid;              $n = $fid;
784          }          }
785          if ($1 ne "peg") { return $n }  
786            my $link;
787            #added to format prophage and path island links to feature.cgi
788            if ($1 ne "peg")
789            {
790               my $user = $cgi->param('user');
791               if (! $user) { $user = "" }
792               my $sprout = $cgi->param('SPROUT') ? "&SPROUT=1" : "";
793               $link = "$top/feature.cgi?feature=$fid&user=$user$trans$sprout";
794               $link =~ s/[a-z_A-Z0-9]+\.cgi\?/feature.cgi?/;
795            }
796            else
797            {
798          my $user = $cgi->param('user');          my $user = $cgi->param('user');
799          if (! $user) { $user = "" }          if (! $user) { $user = "" }
800          my $trans = $cgi->param('translate') ? "&translate=1" : "";          my $trans = $cgi->param('translate') ? "&translate=1" : "";
801          my $link = $cgi->url() . "?prot=$fid&user=$user$trans";              my $sprout = $cgi->param('SPROUT') ? "&SPROUT=1" : "";
802          $link =~ s/[a-z_A-Z]+\.cgi\?/protein.cgi?/;  ###a
803    
804    ### This used to be
805    ###     my $link = &FIG::cgi_url . "/protein.cgi?prot=$fid&user=$user$trans$sprout";
806    ###
807    ### The cost became prohibitive in the subsystem spreadsheets.  Hence, we cache the value
808    ###
809    ### RAO
810    
811                #if (! $cgi_url) { $cgi_url = &FIG::cgi_url }
812                #$link = $cgi_url . "/protein.cgi?prot=$fid&user=$user$trans$sprout";
813                $link = "$top/protein.cgi?prot=$fid&user=$user$trans$sprout";
814                $link =~ s/[a-z_A-Z0-9]+\.cgi\?/protein.cgi?/;
815            }
816          if ($just_url)          if ($just_url)
817          {          {
818              return $link;              return $link;
# Line 362  Line 826 
826  }  }
827    
828  sub family_link {  sub family_link {
829        shift if UNIVERSAL::isa($_[0],__PACKAGE__);
830      my($family,$user) = @_;      my($family,$user) = @_;
831    
832      return $family;      return $family;
833  }  }
834    
 use URI::Escape;  
835    
836  sub get_html {  sub get_html {
837        shift if UNIVERSAL::isa($_[0],__PACKAGE__);
838      my( $url, $type, $kv_pairs) = @_;      my( $url, $type, $kv_pairs) = @_;
839      my( $encoded, $ua, $args, @args, $out, @output, $x );      my( $encoded, $ua, $args, @args, $out, @output, $x );
840    
841      $ua = new LWP::UserAgent;      $ua = new LWP::UserAgent;
842      $ua->timeout( 900 );      $ua->timeout( 900 );
   
843      if ($type =~/post/i)      if ($type =~/post/i)
844      {      {
845          $args = [];          $args = [];
# Line 435  Line 899 
899  }  }
900    
901  sub trim_output {  sub trim_output {
902        shift if UNIVERSAL::isa($_[0],__PACKAGE__);
903      my($out) = @_;      my($out) = @_;
904      my $i;      my $i;
905    
# Line 472  Line 937 
937  }  }
938    
939  sub set_prot_links {  sub set_prot_links {
940        shift if UNIVERSAL::isa($_[0],__PACKAGE__);
941      my($cgi,$x) = @_;      my($cgi,$x) = @_;
942      my($before,$match,$after);      my($before,$match,$after);
943    
944      if ($x =~ /^(.*)(fig\|\d+\.\d+\.peg\.\d+)(.*)$/)      if ($x =~ /^(.*)(fig\|\d+\.\d+\.peg\.\d+)(.*)/s)
945        {
946            $before = $1;
947            $match = $2;
948            $after = $3;
949            return &set_prot_links($cgi,$before) . &HTML::fid_link($cgi,$match) . &set_prot_links($cgi,$after);
950        }
951        elsif ($x =~ /^(.*)\b([NXYZA][PM]_[0-9\.]+)\b(.*)/s)
952      {      {
953          $before = $1;          $before = $1;
954          $match = $2;          $match = $2;
955          $after = $3;          $after = $3;
956          return &set_prot_links($cgi,$before) . &HTML::fid_link($cgi,$match) . &set_prot_links($cgi,$after) . "\n";          return &set_prot_links($cgi,$before) . &HTML::refseq_link($cgi,$match) . &set_prot_links($cgi,$after);
957      }      }
958      elsif ($x =~ /^(.*)(gi\|\d+)(.*)$/)      elsif ($x =~ /^(.*)(gi\|\d+)(.*)/s)
959      {      {
960          $before = $1;          $before = $1;
961          $match = $2;          $match = $2;
962          $after = $3;          $after = $3;
963          return &set_prot_links($cgi,$before) . &HTML::gi_link($cgi,$match) . &set_prot_links($cgi,$after) . "\n";          return &set_prot_links($cgi,$before) . &HTML::gi_link($cgi,$match) . &set_prot_links($cgi,$after);
964      }      }
965      elsif ($x =~ /^(.*)(sp\|[A-Z0-9]{6})(.*)$/)      elsif ($x =~ /^(.*)(tigr\|[0-9a-zA-Z]+)(.*)/s)
966      {      {
967          $before = $1;          $before = $1;
968          $match = $2;          $match = $2;
969          $after = $3;          $after = $3;
970          return &set_prot_links($cgi,$before) . &HTML::sp_link($cgi,$match) . &set_prot_links($cgi,$after) . "\n";          return &set_prot_links($cgi,$before) . &HTML::tigr_link($cgi,$match) . &set_prot_links($cgi,$after);
971      }      }
972      elsif ($x =~ /^(.*)(pirnr\|NF\d+)(.*)$/)      elsif ($x =~  /^(.*)(uni\|[A-Z0-9]{6})(.*)/s)
973      {      {
974          $before = $1;          $before = $1;
975          $match = $2;          $match = $2;
976          $after = $3;          $after = $3;
977          return &set_prot_links($cgi,$before) . &HTML::pir_link($cgi,$match) . &set_prot_links($cgi,$after) . "\n";          return &set_prot_links($cgi,$before) . &HTML::uni_link($cgi,$match) . &set_prot_links($cgi,$after);
978        }
979        elsif ($x =~ /^(.*)(sp\|[A-Z0-9]{6})(.*)/s)
980        {
981            $before = $1;
982            $match = $2;
983            $after = $3;
984            return &set_prot_links($cgi,$before) . &HTML::sp_link($cgi,$match) . &set_prot_links($cgi,$after);
985        }
986        elsif ($x =~ /^(.*)(pirnr\|NF\d+)(.*)/s)
987        {
988            $before = $1;
989            $match = $2;
990            $after = $3;
991            return &set_prot_links($cgi,$before) . &HTML::pir_link($cgi,$match) . &set_prot_links($cgi,$after);
992        }
993        elsif ($x =~ /^(.*)(kegg\|[a-z]{2,4}:[a-zA-Z_0-9]+)(.*)/s)
994        {
995            $before = $1;
996            $match = $2;
997            $after = $3;
998            return &set_prot_links($cgi,$before) . &HTML::kegg_link($cgi,$match) . &set_prot_links($cgi,$after);
999        }
1000        elsif ($x =~ /^(.*)(Ensembl[a-zA-Z]+:[a-zA-Z_0-9\.]+)(.*)/s)
1001        {
1002            $before = $1;
1003            $match = $2;
1004            $after = $3;
1005            return &set_prot_links($cgi,$before) . &HTML::ensembl_link($cgi,$match) . &set_prot_links($cgi,$after);
1006        }
1007        elsif ($x =~ /^(.*)(EntrezGene:[a-zA-Z_0-9\.]+)(.*)/s)
1008        {
1009            $before = $1;
1010            $match = $2;
1011            $after = $3;
1012            return &set_prot_links($cgi,$before) . &HTML::entrezgene_link($cgi,$match) . &set_prot_links($cgi,$after);
1013        }
1014        elsif ($x =~ /^(.*)(MIM:[a-zA-Z_0-9\.]+)(.*)/s)
1015        {
1016            $before = $1;
1017            $match = $2;
1018            $after = $3;
1019            return &set_prot_links($cgi,$before) . &HTML::mim_link($cgi,$match) . &set_prot_links($cgi,$after);
1020        }
1021        elsif ($x =~ /^(.*)(UniGene:[a-zA-Z_0-9\.]+)(.*)/s)
1022        {
1023            $before = $1;
1024            $match = $2;
1025            $after = $3;
1026            return &set_prot_links($cgi,$before) . &HTML::unigene_link($cgi,$match) . &set_prot_links($cgi,$after);
1027        }
1028        elsif ($x =~ /^(.*)(IPI:[a-zA-Z_0-9\.]+)(.*)/s)
1029        {
1030            $before = $1;
1031            $match = $2;
1032            $after = $3;
1033            return &set_prot_links($cgi,$before) . &HTML::ipi_link($cgi,$match) . &set_prot_links($cgi,$after);
1034        }
1035        elsif ($x =~ /^(.*)(WP:[a-zA-Z_0-9\.]+)(.*)/s)
1036        {
1037            #wormbase
1038    
1039            $before = $1;
1040            $match = $2;
1041            $after = $3;
1042            return &set_prot_links($cgi,$before) . &HTML::wp_link($cgi,$match) . &set_prot_links($cgi,$after);
1043        }
1044        elsif ($x =~ /^(.*)(FB:[a-zA-Z_0-9\.]+)(.*)/s)
1045        {
1046            #flybase
1047    
1048            $before = $1;
1049            $match = $2;
1050            $after = $3;
1051            return &set_prot_links($cgi,$before) . &HTML::fb_link($cgi,$match) . &set_prot_links($cgi,$after);
1052        }
1053        elsif ($x =~ /^(.*)(FlyBaseORFNames:[a-zA-Z_0-9\.]+)(.*)/s)
1054        {
1055            #flybase
1056    
1057            $before = $1;
1058            $match = $2;
1059            $after = $3;
1060            return &set_prot_links($cgi,$before) . &HTML::fborf_link($cgi,$match) . &set_prot_links($cgi,$after);
1061        }
1062        elsif ($x =~ /^(.*)(SGD_LOCUS:[a-zA-Z_0-9\.]+)(.*)/s)
1063        {
1064            #flybase
1065    
1066            $before = $1;
1067            $match = $2;
1068            $after = $3;
1069            return &set_prot_links($cgi,$before) . &HTML::sgd_link($cgi,$match) . &set_prot_links($cgi,$after);
1070      }      }
1071      return $x;      return $x;
1072  }  }
1073    
1074    sub refseq_link {
1075        shift if UNIVERSAL::isa($_[0],__PACKAGE__);
1076        my($cgi,$id) = @_;
1077    
1078        if ($id =~ /^[NXYZA]P_/)
1079        {
1080            return "<a href=http://www.ncbi.nlm.nih.gov/entrez/query.fcgi?db=protein&cmd=search&term=$id>$id</a>";
1081        }
1082        elsif ($id =~ /^[NXYZA]M_/)
1083        {
1084            return "<a href=http://www.ncbi.nlm.nih.gov/entrez/query.fcgi?db=nuccore&cmd=search&term=$id>$id</a>";
1085        }
1086    }
1087    
1088  sub gi_link {  sub gi_link {
1089        shift if UNIVERSAL::isa($_[0],__PACKAGE__);
1090      my($cgi,$gi) = @_;      my($cgi,$gi) = @_;
1091    
1092      if ($gi =~ /^gi\|(\d+)$/)      if ($gi =~ /^gi\|(\d+)$/)
# Line 516  Line 1096 
1096      return $gi;      return $gi;
1097  }  }
1098    
1099    sub tigr_link {
1100        shift if UNIVERSAL::isa($_[0],__PACKAGE__);
1101        my($cgi,$tigr) = @_;
1102    
1103        if ($tigr =~ /^tigr\|([0-9a-zA-Z]+)$/)
1104        {
1105            return "<a href=\"http://pathema.tigr.org/tigr-scripts/CMR/GenePage.cgi?locus=$1\">$tigr</a>";
1106        }
1107        return $tigr;
1108    }
1109    
1110    sub uni_link {
1111        shift if UNIVERSAL::isa($_[0],__PACKAGE__);
1112        my($cgi,$uni) = @_;
1113    
1114        if ($uni =~ /^uni\|(\S+)$/)
1115        {
1116            return "<a href=http://www.pir.uniprot.org/cgi-bin/upEntry?id=$1>$uni</a>";
1117        }
1118        return $uni;
1119    }
1120    
1121  sub sp_link {  sub sp_link {
1122        shift if UNIVERSAL::isa($_[0],__PACKAGE__);
1123      my($cgi,$sp) = @_;      my($cgi,$sp) = @_;
1124    
1125      if ($sp =~ /^sp\|(\S+)$/)      if ($sp =~ /^sp\|(\S+)$/)
# Line 527  Line 1130 
1130  }  }
1131    
1132  sub pir_link {  sub pir_link {
1133        shift if UNIVERSAL::isa($_[0],__PACKAGE__);
1134      my($cgi,$pir) = @_;      my($cgi,$pir) = @_;
1135    
1136      if ($pir =~ /^pirnr\|(NF\d+)$/)      if ($pir =~ /^pirnr\|(NF\d+)$/)
# Line 536  Line 1140 
1140      return $pir;      return $pir;
1141  }  }
1142    
1143    sub kegg_link {
1144        shift if UNIVERSAL::isa($_[0],__PACKAGE__);
1145        my($cgi,$kegg) = @_;
1146    
1147        if ($kegg =~ /^kegg\|([^:]+):(\S+)$/)
1148        {
1149            return "<a href=http://www.genome.ad.jp/dbget-bin/www_bget?$1+$2>$kegg</a>";
1150        }
1151        return $kegg;
1152    }
1153    
1154    sub ensembl_link {
1155        shift if UNIVERSAL::isa($_[0],__PACKAGE__);
1156        my($cgi,$ensembl) = @_;
1157    
1158        if ($ensembl =~ /^(\S+):(\S+)$/)
1159        {
1160            my $what=$1;
1161            my $key=$2;
1162            my $idx="all";
1163            if ($what eq "EnsemblGene") { $idx = "Gene" }
1164            if ($what eq "EnsemblTranscript") { $idx = "all" }
1165            if ($what eq "EnsemblProtein") { $idx = "all" }
1166    
1167            #I really want to get right to the transcript and peptide pages, but
1168            #can't see how to do that without knowing the org name too, which
1169            #I don't know at this point. (ensembl org name, not real org name)
1170    
1171            return "<a href=http://www.ensembl.org/Homo_sapiens/textview?species=all&idx=$idx&q=$key>$ensembl</a>";
1172        }
1173        return $ensembl;
1174    }
1175    
1176    sub entrezgene_link {
1177        shift if UNIVERSAL::isa($_[0],__PACKAGE__);
1178        my($cgi,$entrezgene) = @_;
1179    
1180        if ($entrezgene =~ /^EntrezGene:(\S+)$/)
1181        {
1182            return "<a href=http://www.ncbi.nlm.nih.gov/entrez/query.fcgi?db=gene&cmd=Retrieve&dopt=full_report&list_uids=$1>$entrezgene</a>";
1183        }
1184        return $entrezgene;
1185    }
1186    
1187    sub mim_link {
1188        shift if UNIVERSAL::isa($_[0],__PACKAGE__);
1189        my($cgi,$mim) = @_;
1190    
1191        if ($mim =~ /^MIM:(\S+)$/)
1192        {
1193            return "<a href=http://www3.ncbi.nlm.nih.gov/entrez/dispomim.cgi?id=$1>$mim</a>";
1194        }
1195        return $mim;
1196    }
1197    
1198    sub unigene_link {
1199        shift if UNIVERSAL::isa($_[0],__PACKAGE__);
1200        my($cgi,$unigene) = @_;
1201    
1202        if ($unigene =~ /^UniGene:(\S+)$/)
1203        {
1204            return "<a href=http://www.ncbi.nlm.nih.gov/entrez/query.fcgi?db=unigene&cmd=search&term=$1>$unigene</a>";
1205        }
1206        return $unigene;
1207    }
1208    
1209    sub ipi_link {
1210        shift if UNIVERSAL::isa($_[0],__PACKAGE__);
1211        my($cgi,$ipi) = @_;
1212    
1213        if ($ipi =~ /^IPI:(\S+)$/)
1214        {
1215            return "<a href=http://srs.ebi.ac.uk/srsbin/cgi-bin/wgetz?-id+AEoS1R8Jnn+-e+[IPI:\'$1\']+-qnum+1+-enum+1>$ipi</a>";
1216        }
1217        return $ipi;
1218    }
1219    
1220    sub wp_link {
1221        shift if UNIVERSAL::isa($_[0],__PACKAGE__);
1222        my($cgi,$wp) = @_;
1223    
1224        #wormbase
1225    
1226        if ($wp =~ /^WP:(\S+)$/)
1227        {
1228            return "<a href=http://www.wormbase.org/db/searches/basic?class=Any&query=$1&Search=Search>$wp</a>";
1229        }
1230        return $wp;
1231    }
1232    
1233    sub fb_link {
1234        shift if UNIVERSAL::isa($_[0],__PACKAGE__);
1235        my($cgi,$fb) = @_;
1236    
1237        #flybase
1238    
1239        if ($fb =~ /^FB:(\S+)$/)
1240        {
1241            return "<a href=http://flybase.bio.indiana.edu/.bin/fbidq.html?$1>$fb</a>";
1242        }
1243        return $fb;
1244    }
1245    
1246    sub fborf_link {
1247        shift if UNIVERSAL::isa($_[0],__PACKAGE__);
1248        my($cgi,$fb) = @_;
1249    
1250        #flybase
1251    
1252        if ($fb =~ /^FlyBaseORFNames:(\S+)$/)
1253        {
1254            return "<a href=http://flybase.bio.indiana.edu/.bin/fbidq.html?$1>$fb</a>";
1255        }
1256        return $fb;
1257    }
1258    
1259    sub sgd_link {
1260        shift if UNIVERSAL::isa($_[0],__PACKAGE__);
1261        my($cgi,$sgd) = @_;
1262    
1263        #yeast
1264    
1265        if ($sgd =~ /^SGD_LOCUS:(\S+)$/)
1266        {
1267            return "<a href=http://db.yeastgenome.org/cgi-bin/locus.pl?locus=$1>$sgd</a>";
1268        }
1269        return $sgd;
1270    }
1271    
1272    
1273    
1274    
1275    sub set_map_links {
1276        shift if UNIVERSAL::isa($_[0],__PACKAGE__);
1277        my($cgi,$x) = @_;
1278        my($before,$match,$after);
1279    
1280        my $org = ($cgi->param('org') || $cgi->param('genome') || "");
1281    
1282        if ($x =~ /^(.*)(MAP\d+)(.*)/s)
1283        {
1284            $before = $1;
1285            $match = $2;
1286            $after = $3;
1287            return &set_map_links($cgi,$before) . &map_link($cgi,$match,$org) . &set_map_links($cgi,$after);
1288        }
1289        return $x;
1290    }
1291    
1292    
1293    
1294    sub map_link {
1295        shift if UNIVERSAL::isa($_[0],__PACKAGE__);
1296        my($cgi,$map,$org) = @_;
1297    
1298        $user = $cgi->param('user');
1299        $user = $user ? $user : "";
1300        $org = $org ? $org : "";
1301    
1302        my $url = "show_kegg_map.cgi?user=$user&map=$map&org=$org";
1303    #rel    my $url = &FIG::cgi_url() . "/show_kegg_map.cgi?user=$user&map=$map&org=$org";
1304        my $link = "<a href=\"$url\">$map</a>";
1305        return $link;
1306    }
1307    
1308    sub java_buttons {
1309        shift if UNIVERSAL::isa($_[0],__PACKAGE__);
1310      ## ADDED BY RAE
1311      # Provides code to include check all/first half/second half/none for javascrspt
1312      # this takes two variables - the form name provided in start_form with the
1313      # -name => field and the checkbox name
1314      my ($form, $button)=@_;
1315    
1316      $java_script="<input type=\"button\" name=\"CheckAll\" value=\"Check All\"\nonClick=\"checkAll(document.$form.$button)\">\n";
1317      $java_script.="<input type=\"button\" name=\"CheckFirst\" value=\"Check First Half\"\nonClick=\"checkFirst(document.$form.$button)\">\n";
1318      $java_script.="<input type=\"button\" name=\"CheckSecond\" value=\"Check Second Half\"\nonClick=\"checkSecond(document.$form.$button)\">\n";
1319      $java_script.="<input type=\"button\" name=\"UnCheckAll\" value=\"Uncheck All\"\nonClick=\"uncheckAll(document.$form.$button)\">\n";
1320    
1321      return $java_script;
1322    }
1323    
1324    sub sub_link {
1325        shift if UNIVERSAL::isa($_[0],__PACKAGE__);
1326        my($cgi,$sub) = @_;
1327        my($sub_link);
1328    
1329        my $user = $cgi->param('user');
1330        if ($user)
1331        {
1332            my $esc_sub = uri_escape( $sub );
1333            $sub_link = "<a href=./subsys.cgi?ssa_name=$esc_sub&request=show_ssa&user=$user>$sub</a>";
1334        }
1335        else
1336        {
1337            $sub_link = $sub;
1338        }
1339        return $sub_link;
1340    }
1341    
1342    sub reaction_link {
1343        my($reaction) = @_;
1344    
1345        if ($reaction =~ /^R\d+/)
1346        {
1347            return "<a href=\"http://www.genome.ad.jp/dbget-bin/www_bget?rn+$reaction\" target=reaction$$>$reaction</a>";
1348        }
1349        return $reaction;
1350    }
1351    
1352    sub html_for_assignments {
1353        my($fig,$user,$peg_sets) = @_;
1354        my $i;
1355    
1356        my @vals = ();
1357        my $set = 1;
1358        foreach $peg_set (@$peg_sets)
1359        {
1360            for ($i=0; ($i < @$peg_set); $i++)
1361            {
1362                $peg = $peg_set->[$i];
1363                push(@vals,'show=' . join("@",($set,$i+1,$peg,&FIG::abbrev($fig->org_of($peg)),"")));
1364            }
1365            $set++;
1366        }
1367    
1368        $ENV{'REQUEST_METHOD'} = 'GET';
1369        $ENV{'QUERY_STRING'} = join('&',('request=show_commentary',"uni=1","user=$user",@vals));
1370        my $out = join("",`$FIG_Config::fig/CGI/chromosomal_clusters.cgi`);
1371        $out =~ s/^.*?<form/<form/si;
1372        $out =~ s/^(.*)<table.*/$1/si;
1373        return $out;
1374    }
1375    
1376    =head1 rss_feed
1377    
1378    Add something to the RSS feed. The rss feeds are stored in the Html directory, and there are several RSS feeds:
1379            SEED.rss                - everything gets written here
1380            SEEDgenomes.rss                 - whenever a genome is added to the SEED
1381            SEEDsubsystems.rss      - whenever a subsystem is edited (or should this be added?)
1382    
1383    
1384    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.
1385    
1386    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.
1387    
1388    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.
1389    
1390    The has can have these keys:
1391    
1392    REQUIRED:
1393    title       : the title. This is usually what is seen by the user in the pull down menu
1394    description : a more complete description that is often seen is rss viewers but not always
1395    link        : link to the item that was added/edited
1396    All other keys are treated as optional RSS arguments and written to the file.
1397    
1398    At most, $max_entries recent entries are stored in the rss file, and this is currently 50.
1399    
1400    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.
1401    
1402    
1403    =cut
1404    
1405    sub rss_feed {
1406     shift if UNIVERSAL::isa($_[0],__PACKAGE__);
1407     my ($files, $args)=@_;
1408    
1409     # how many entries to store in the file
1410     my $max_entries=50;
1411    
1412     foreach my $a (keys %$args) {if ($a =~ /^-(.*)/) {my $b=$1; $args->{$b}=$args->{$a}; delete $args->{$a}}}
1413    
1414     my $filepath=$FIG_Config::fig."/CGI/Html/rss";
1415     # check for the directory and if not, make it
1416     mkdir $filepath unless (-d $filepath);
1417    
1418     # note that $info is a hash of references to hashes that are written out as headers in the file
1419     my $info=
1420     {
1421      "SEED.rss" =>
1422       {
1423            title           => "The SEED",
1424            description     => "Latest news from the SEED",
1425            link            => "Html/rss/SEED.rss",
1426       },
1427    
1428      "SEEDsubsystems.rss" =>
1429      {
1430            title           => "SEED Subsystems",
1431            description     => "Recently updated SEED subsystems",
1432            link            => "Html/rss/SEEDsubsystems.rss",
1433      },
1434    
1435      "SEEDsubsystems.rss" =>
1436      {
1437            title           => "SEED Genomes",
1438            description     => "Genomes recently added to the SEED",
1439            link            => &FIG::cgi_url()."/Html/rss/SEEDsubsystems.rss",
1440      },
1441    
1442     };
1443    
1444    
1445     # build the new xml
1446     my $xml = "\t<item>\n";
1447     foreach my $qw ("title", "description", "link") {
1448      unless ($args->{$qw}) {
1449       print STDERR "You need to include a $qw tag in your RSS description\n";
1450       return(0);
1451      }
1452      # we need to do something a bit funky with the link. We can't have ampersands in the <link> </link> in valid html
1453      # so we are going to pull out the links and uri_escape just the part after the .cgi
1454      if ($qw eq "link")
1455      {
1456       $args->{$qw} =~ /^(.*?\.cgi.)(.*)$/;
1457       $args->{$qw} = $1.uri_escape($2) if ($1 && $2);
1458      }
1459    
1460      $xml .= "\t\t<$qw>".$args->{$qw}."</$qw>\n";
1461      delete $args->{$qw};
1462     }
1463    
1464     foreach my $tag (grep {!/type/i} keys %$args)
1465     {
1466      $xml .= "\t\t<$tag>".$args->{$tag}."</$tag>\n";
1467     }
1468    
1469     $xml .= "\t</item>\n";
1470    
1471    
1472     my @files=("SEED.rss");
1473     if ($args->{"type"}) {push @files, "SEED.$type.rss"}
1474    
1475     foreach my $file ("SEED.rss", @$files)
1476     {
1477      if (-e "$filepath/$file")
1478      {
1479       my @out; # the new content of the file
1480       my $itemcount=0; # how many <item> </item>'s are we keeping
1481       my $initem; # are we in an item?
1482       open(IN, "$filepath/$file") || die "Can't open $filepath/$file";
1483       while (<IN>)
1484       {
1485        if (/\<item\>/) {
1486         push @out, $xml, unless ($itemcount);
1487         $itemcount++;
1488         $initem=1;
1489        }
1490        if (/\<\/item\>/) {$initem=0; next if ($itemcount > $max_entries)}
1491        next if ($initem && $itemcount > $max_entries);
1492        push @out, $_;
1493       }
1494       close IN;
1495       open(OUT, ">$filepath/$file") || die "Can't open $filepath/$file for writing";
1496       print OUT @out;
1497      }
1498      else
1499      {
1500       open(OUT, ">$filepath/$file") || die "Can't open $filepath/$file for writing";
1501       print OUT "<?xml version=\"1.0\"?>\n<rss version=\"2.0\">\n<channel>\n";
1502       if ($info->{$file})
1503       {
1504         # we're going to sanity check each of the three options we output, just to be sure
1505         foreach my $qw ("title", "description", "link")
1506         {
1507           if ($info->{$file}->{$qw})
1508           {
1509              print OUT "<$qw>", $info->{$file}->{$qw}, "</$qw>\n";
1510           } else {
1511              print STDERR "Please add a $qw for $file\n"; print OUT "<$qw>$file</$qw>\n";
1512           }
1513         }
1514       }
1515       else {
1516        print STDERR "Please define title, link, and description information for $file\n";
1517        print OUT "<title>$file</title>\n<description>An RSS feed</description>\n<link>", &FIG::cgi_url, "</link>\n";
1518       }
1519       print OUT "\n", $xml;
1520       print OUT "\n", "</channel>\n</rss>\n"
1521      }
1522     }
1523    }
1524    
1525    
1526    
1527    1;
1528    
 1  

Legend:
Removed from v.1.6  
changed lines
  Added in v.1.74

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3