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

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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3