AcePerl
view release on metacpan or search on metacpan
Ace/Browser/AceSubs.pm view on Meta::CPAN
PrintBottom();
=head1 DESCRIPTION
Ace::Browser::AceSubs exports a set of routines that are useful for
creating search pages and displays for AceBrowser CGI pages. See
http://stein.cshl.org/AcePerl/AceBrowser.
The following subroutines are exported by default:
AceError
AceMissing
AceNotFound
Configuration
DoRedirect
GetAceObject
Object2URL
ObjectLink
OpenDatabase
PrintTop
PrintBottom
Url
The following subroutines are exported if explicitly requested:
AceAddCookie
AceInit
AceHeader
AceMultipleChoices
AceRedirect
DB_Name
Footer
Header
ResolveUrl
Style
Toggle
TypeSelector
To load the default subroutines load the module with:
use Ace::Browser::AceSubs;
To bring in a set of optionally routines, load the module with:
use Ace::Browser::AceSubs qw(AceInit AceRedirect);
To bring in all the default subroutines, plus some of the optional
ones:
use Ace::Browser::AceSubs qw(:DEFAULT AceInit AceRedirect);
There are two main types of AceBrowser scripts:
=over 4
=item display scripts
These are called with the CGI parameters b<name> and b<class>,
corresponding to the name and class of an AceDB object to display.
The subroutine GetAceObject() will return the requested object, or
undef if the object does not exist.
To retrieve the parameters, use the CGI.pm param() method:
$name = param('name');
$class = param('class');
=item search scripts
These are not called with any CGI parameters on their first
invocation, but can define their own parameter lists by creating
fill-out forms. The AceBrowser system remembers the last search
performed by a search script in a cookie and regenerates the CGI
parameters the next time the user selects that search script.
=back
=head1 SUBROUTINES
The following sections describe the exported subroutines.
=over 4
=cut
use strict;
use Ace::Browser::SiteDefs;
use Ace 1.76;
use CGI qw(:standard escape);
use CGI::Cookie;
use File::Path 'mkpath';
use vars qw/@ISA @EXPORT @EXPORT_OK $VERSION %EXPORT_TAGS
%DB %OPEN $HEADER $TOP @COOKIES
$APACHE_CONF/;
require Exporter;
@ISA = qw(Exporter);
$VERSION = 1.21;
######################### This is the list of exported subroutines #######################
@EXPORT = qw(
GetAceObject AceError AceNotFound AceMissing DoRedirect
OpenDatabase Object2URL Url
ObjectLink Configuration PrintTop PrintBottom);
@EXPORT_OK = qw(AceRedirect Toggle ResolveUrl AceInit AceAddCookie
AceHeader TypeSelector Style AcePicRoot
Header Footer DB_Name AceMultipleChoices);
%EXPORT_TAGS = ( );
use constant DEFAULT_DATABASE => 'default';
use constant PRIVACY => 'misc/privacy'; # privacy/cookie statement
use constant SEARCH_BROWSE => 'search'; # a fallback search script
my %VALID; # cache for get_symbolic() lookups
=item AceError($message)
This subroutine will print out an error message and exit the script.
The text of the message is taken from $message.
=cut
sub AceError {
my $msg = shift;
PrintTop(undef,undef,'Error');
print CGI::font({-color=>'red'},$msg);
PrintBottom();
Apache->exit(0) if defined &Apache::exit;
exit(0);
}
=item AceHeader()
This function prints the HTTP header and issues a number of cookies
used for maintaining AceBrowser state. It is not exported by default.
=cut
=item AceAddCookie(@cookies)
This subroutine, which must be called b<after> OpenDatabase() and/or
GetAceObject() and b<before> PrintTop(), will add one or more cookies
to the outgoing HTTP headers that are emitted by AceHeader().
Cookies must be CGI::Cookie objects.
=cut
sub AceAddCookie {
push @COOKIES,@_; # add caller's to our globals
}
################## canned header ############
sub AceHeader {
my %searches = map {$_=>1} Configuration()->searches;
my $quovadis = url(-relative=>1);
my $db = get_symbolic();
my $referer = referer();
$referer =~ s!^http://[^/]+!! if defined $referer;
my $home = Configuration()->Home->[0] if Configuration()->Home;
if ($referer && $home && index($referer,$home) >= 0) {
my $bookmark = cookie(
-name=>"HOME_${db}",
-value=>$referer,
-path=>'/');
push(@COOKIES,$bookmark);
}
if ($searches{$quovadis}) {
Delete('Go');
my $search_name = "SEARCH_${db}_${quovadis}";
my $search_data = cookie(-name => $search_name,
-value => query_string(),
-path=>'/',
);
my $last_search = cookie(-name=>"ACEDB_$db",
-value=>$quovadis,
-path=>'/');
push(@COOKIES,$search_data,$last_search);
}
print @COOKIES ? header(-cookie=>\@COOKIES,@_) : header(@_);
@COOKIES = ();
$HEADER++;
}
=item AceInit()
This subroutine initializes the AcePerl connection to the configured
database. If the database cannot be opened, it generates an error
message and exits. This subroutine is not exported by default, but is
called by PrintTop() and Header() internally.
=cut
# Subroutines used by all scripts.
# Will generate an HTTP 'document not found' error if you try to get an
# undefined database name. Check the return code from this function and
# return immediately if not true (actually, not needed because we exit).
sub AceInit {
$HEADER = 0;
$TOP = 0;
@COOKIES = ();
# keeps track of what sections should be open
%OPEN = param('open') ? map {$_ => 1} split(' ',param('open')) : () ;
return 1 if Configuration();
# if we get here, it is a big NOT FOUND error
print header(-status=>'404 Not Found',-type=>'text/html');
$HEADER++;
print start_html(-title => 'Database Not Found',
-style => Ace::Browser::SiteDefs->getConfig(DEFAULT_DATABASE)->Style,
),
h1('Database not found'),
p('The requested database',i(get_symbolic()),'is not recognized',
'by this server.');
print p('Please return to the',a({-href=>referer()},'referring page.')) if referer();
print end_html;
Apache::exit(0) if defined &Apache::exit; # bug out of here!
exit(0);
}
=item AceMissing([$class,$name])
This subroutine will print out an error message indicating that an
object is present in AceDB, but that the information the user
requested is absent. It will then exit the script. This is
infrequently encountered when following XREFed objects. If the class
and name of the object are not provided as arguments, they are taken
from CGI's param() function.
=cut
sub AceMissing {
my ($class,$name) = @_;
$class ||= param('class');
$name ||= param('name');
PrintTop(undef,undef,$name);
print strong('There is no further information about this object in the database.');
PrintBottom();
Apache->exit(0) if defined &Apache::exit;
exit(0);
}
=item AceMultipleChoices($symbol,$report,$objects)
This function is called when a search has recovered multiple objects
and the user must make a choice among them. The user is presented
with an ordered list of the objects, and asked to click on one of
them.
The three arguements are:
$symbol The keyword or query string the user was searching
on, undef if none.
$report The symbolic name of the current display, or undef
if none.
$objects An array reference containing the Ace objects in
question.
This subroutine is not exported by default.
=cut
sub AceMultipleChoices {
my ($symbol,$report,$objects) = @_;
if ($objects && @$objects == 1) {
my $destination = Object2URL($objects->[0]);
AceHeader(-Refresh => "1; URL=$destination");
print start_html (
'-Title' => 'Redirect',
'-Style' => Style(),
),
h1('Redirect'),
p("Automatically transforming this query into a request for corresponding object",
ObjectLink($objects->[0],$objects->[0]->class.':'.$objects->[0])),
p("Please wait..."),
Footer(),
end_html();
return;
}
PrintTop(undef,undef,'Multiple Choices');
print
p("Multiple $report objects correspond to $symbol.",
"Please choose one:"),
ol(
li([
map {ObjectLink($_,font({-color=>'red'},$_->class).': '.$_)} @$objects
])
);
PrintBottom();
}
=item AceNotFound([$class,$name])
This subroutine will print out an error message indicating that the
requested object is not present in AceDB, even as a name. It will then
exit the script. If the class and name of the object are not provided
as arguments, they are taken from CGI's param() function.
=cut
sub AceNotFound {
my $class = shift || param('class');
my $name = shift || param('name');
PrintTop(undef,undef,"$class: $name not found");
print p(font({-color => 'red'},
strong("The $class named \"$name\" is not found in the database.")));
PrintBottom();
Apache->exit(0) if defined &Apache::exit;
exit(0);
}
=item ($uri,$physical_path) = AcePicRoot($directory)
This function returns the physical and URL paths of a temporary
directory in which the pic script can write pictures. Not exported by
default. Returns a two-element list containing the URL and physical
path.
=cut
sub AcePicRoot {
my $path = shift;
my $umask = umask();
umask 002; # want this writable by group
my ($picroot,$uri);
if ($ENV{MOD_PERL} && Apache->can('request')) { # we have apache, so no reason not to take advantage of it
my $r = Apache->request;
$uri = join('/',Configuration()->Pictures->[0],"/",$path);
my $subr = $r->lookup_uri($uri);
$picroot = $subr->filename if $subr;
} else {
($uri,$picroot) = @{Configuration()->Pictures} if Configuration()->Pictures;
$uri .= "/$path";
$picroot .= "/$path";
}
mkpath ($picroot,0,0777) || AceError("Can't create directory to store image in") unless -d $picroot;
umask $umask;
return ($uri,$picroot);
}
=item AceRedirect($report,$object)
This function redirects the user to a named display script for viewing
an Ace object. It is used, for example, to convert a request for a
sequence into a request for a protein:
$obj = GetAceObject();
if ($obj->CDS) {
my $protein = $obj->Corresponding_protein;
AceRedirect('protein',$protein);
}
AceRedirect must be called b<before> PrintTop() or AceHeader(). It
invokes exit(), so it will not return.
This subroutine is not exported by default. It differs from
DoRedirect() in that it displays a message to the user for two seconds
before it generates the new page. It also allows the display to be set
explicitly, rather than determined automatically by the AceBrowser
system.
=cut
Ace/Browser/AceSubs.pm view on Meta::CPAN
# Contents of the HTML footer. It gets printed immediately before the </BODY> tag.
# The one given here generates a link to the "feedback" page, as well as to the
# privacy statement. You may or may not want these features.
sub Footer {
if (my $footer = Configuration()->Footer) {
return $footer;
}
my $webmaster = $ENV{SERVER_ADMIN} || 'webmaster@sanger.ac.uk';
my $obj_name = escape(param('name'));
my $obj_class = escape(param('class')) || ucfirst url(-relative=>1);
my $referer = escape(self_url());
my $name = get_symbolic();
# set up the feedback link
my $feedback_link = Configuration()->Feedback_recipients &&
$obj_name &&
(url(-relative=>1) ne 'feedback') ?
a({-href=>ResolveUrl("misc/feedback/$name","name=$obj_name;class=$obj_class;referer=$referer")},
"Click here to send data or comments to the maintainers")
: '';
# set up the privacy statement link
my $privacy_link = ( Configuration()->Print_privacy_statement &&
url(-relative=>1) ne PRIVACY())
?
a({ -href=>ResolveUrl(PRIVACY."/$name") },'Privacy Statement')
: '';
my ($home,$label) = @{Configuration()->Home};
my $hlink = $home ? a({-href=>$home},$label) : '';
# Either generate a pointer to ACeDB home page, or the copyright statement.
my $clink = Configuration()->Copyright ? a({-href=>Configuration()->Copyright,-target=>"_new"},'Copyright Statement')
: qq(<A HREF="http://stein.cshl.org/AcePerl">AcePerl Home Page</A>);
return <<END;
<TABLE WIDTH="100%" BORDER=0 CELLPADDING=0 CELLSPACING=0>
<TR CLASS="technicalinfo">
<TD CLASS="small" VALIGN="TOP">
$hlink<br>$clink
</TD>
<TD CLASS="small" ALIGN=RIGHT VALIGN=TOP><p><strong>$feedback_link</strong><br>
$privacy_link<br>
<A HREF="mailto:$webmaster"><address>$webmaster</address></A><br>
</TD>
</TR>
</TABLE>
END
}
=item $object = GetAceObject()
This function is called by display scripts to return the
Ace::Object.that the user wishes to view. It automatically opens or
refreshes the database, and performs the request using the values of the
"name" and "class" CGI variables.
If a single object is found, the function returns it as the function
result. If no objects are found, it returns undef. If more than one
object is found, the function invokes AceMultipleChoices() and exits
the script.
=cut
# open database, return object requested by CGI parameters
sub GetAceObject {
my $db = OpenDatabase() || AceError("Couldn't open database."); # exits
my $name = param('name') or return;
my $class = param('class') or return;
my @objs = $db->fetch($class => $name);
if (@objs > 1) {
AceMultipleChoices($name,'',\@objs);
Apache->exit(0) if defined &Apache::exit;
exit(0);
}
return $objs[0];
}
=item $html = Header()
This subroutine returns the boilerplate at the top of the HTML page as
a string, but does not print it out. It is not exported by default.
=cut
sub Header {
my $config = Configuration();
my $dbname = get_symbolic();
return unless my $searches = $config->Searches;
my $banner = $config->Banner;
# next select the correct search script
my @searches = @{$searches};
my $self = url(-relative=>1);
my $modperl = $ENV{MOD_PERL} && Apache->can('request') && eval {Apache->request->dir_config('AceBrowserConf')};
my @row;
foreach (@searches) {
my ($name,$url,$on,$off,$size) = @{$config->searches($_)}{qw/name url onimage
offimage size/};
my $active = $url =~ /\b$self\b/;
my $image = $active ? $on : $off;
# replace the url with a cookie, if one is defined
my $cookie_name = "SEARCH_${dbname}_${_}";
my $query_string = cookie($cookie_name) unless /blast/;
$url .= "/$dbname" unless $url =~ /\b$dbname\b/ or $modperl;
$url .= "?$query_string" if $query_string;
if ($image) {
push @row,a({-href=>$url},img({-src=>$image,-border=>0,
-width=>$size->[0],-height=>$size->[1],
-alt=>$name}));
} else {
push @row,$active ? font({-color=>'black'},$name) : a({-href=>$url,-class=>'searchbanner'},$name);
}
}
Ace/Browser/AceSubs.pm view on Meta::CPAN
sub Url {
my ($display,$parameters) = @_;
my $url = Configuration()->display($display,'url');
return ResolveUrl($url,$parameters);
}
sub Open_table{
print '<table width=660>
<tr>
<td>';
}
sub Close_table{
print '</tr>
</td>
</table>';
}
# return host and port for symbolic database name
sub getDatabasePorts {
my $name = shift;
my $config = Ace::Browser::SiteDefs->getConfig($name);
return ($config->Host,$config->Port,
$config->Username,$config->Password,
$config->Cacheroot,$config->Cachesize,$config->Cacheexpires,$config->Cachepurge,
) if $config;
# If we get here, then try getservbynam()
# I think this is a bit of legacy code.
my @s = getservbyname($name,'tcp');
return unless @s;
return unless $s[2]>1024; # don't allow connections to reserved ports
return ('localhost',$s[2]);
}
sub get_symbolic {
if (exists $ENV{MOD_PERL} && Apache->can('request')) { # the easy way
if (my $r = Apache->request) {
if (my $conf = $r->dir_config('AceBrowserConf')) {
my ($name) = $conf =~ m!([^/]+)\.(?:pm|conf)$!;
return $name if $name;
}
}
}
# otherwise, the hard way
(my $name = path_info())=~s!^/!!;
return $name if defined $name && $name ne ''; # get from additional path info
my $path = url(-absolute=>1);
return $VALID{$path} if exists $VALID{$path};
my @path = split '/',$path;
pop @path;
for my $name ((reverse @path),'default') {
next unless $name;
return $VALID{$path} if exists $VALID{$name};
return $VALID{$path} = $name if Ace::Browser::SiteDefs->getConfig($name);
$VALID{$path} = undef;
}
return;
}
1;
__END__
=back
=head1 BUGS
Please report them.
=head1 SEE ALSO
L<Ace::Object>, L<Ace::Browser::SiteDefs>, L<Ace::Browsr::SearchSubs>,
the README.ACEBROWSER file.
=head1 AUTHOR
Lincoln Stein <lstein@cshl.org>.
Copyright (c) 2001 Cold Spring Harbor Laboratory
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself. See DISCLAIMER.txt for
disclaimers of warranty.
=cut
( run in 1.322 second using v1.01-cache-2.11-cpan-d80b1682f3f )