Commit 85bb266e authored by bbaetz%acm.org's avatar bbaetz%acm.org

Bug 191863 - Clean up Bugzilla.pm

r=gerv, justdave a=justdave
parent 7407cb30
...@@ -29,103 +29,71 @@ use Bugzilla::Config; ...@@ -29,103 +29,71 @@ use Bugzilla::Config;
use Bugzilla::DB; use Bugzilla::DB;
use Bugzilla::Template; use Bugzilla::Template;
sub create { my $_template;
sub template {
my $class = shift; my $class = shift;
my $B = $class->instance; $_template ||= Bugzilla::Template->create();
return $_template;
# And set up the vars for this request }
$B->_init_transient;
return $B; my $_cgi;
sub cgi {
my $class = shift;
$_cgi ||= new Bugzilla::CGI();
return $_cgi;
} }
# We don't use Class::Singleton, because theres no need. However, I'm keeping my $_dbh;
# the same interface in case we do change in the future my $_dbh_main;
my $_dbh_shadow;
my $_instance; sub dbh {
sub instance {
my $class = shift; my $class = shift;
$_instance = $class->_new_instance unless ($_instance); # If we're not connected, then we must want the main db
if (!$_dbh) {
$_dbh = $_dbh_main = Bugzilla::DB::connect_main();
}
return $_instance; return $_dbh;
} }
sub template { return $_[0]->{_template}; }
sub cgi { return $_[0]->{_cgi}; }
sub dbh { return $_[0]->{_dbh}; }
sub switch_to_shadow_db { sub switch_to_shadow_db {
my $self = shift; my $class = shift;
if (!$self->{_dbh_shadow}) { if (!$_dbh_shadow) {
if (Param('shadowdb')) { if (Param('shadowdb')) {
$self->{_dbh_shadow} = Bugzilla::DB::connect_shadow(); $_dbh_shadow = Bugzilla::DB::connect_shadow();
} else { } else {
$self->{_dbh_shadow} = $self->{_dbh_main}; $_dbh_shadow = $_dbh_main;
} }
} }
$self->{_dbh} = $self->{_dbh_shadow}; $_dbh = $_dbh_shadow;
} }
sub switch_to_main_db { sub switch_to_main_db {
my $self = shift;
$self->{_dbh} = $self->{_dbh_main};
}
# PRIVATE methods below here
# Called from instance
sub _new_instance {
my $class = shift; my $class = shift;
my $self = { }; $_dbh = $_dbh_main;
bless($self, $class);
$self->_init_persistent;
return $self;
}
# Initialise persistent items
sub _init_persistent {
my $self = shift;
# We're always going to use the main db, so connect now
$self->{_dbh} = $self->{_dbh_main} = Bugzilla::DB::connect_main();
# Set up the template
$self->{_template} = Bugzilla::Template->create();
} }
# Initialise transient (per-request) items # Private methods
sub _init_transient {
my $self = shift;
$self->{_cgi} = new Bugzilla::CGI if exists $::ENV{'GATEWAY_INTERFACE'}; # Per process cleanup
}
# Clean up transient items such as database handles
sub _cleanup { sub _cleanup {
my $self = shift; undef $_cgi;
delete $self->{_cgi}; # When we support transactions, need to ->rollback here
$_dbh_main->disconnect if $_dbh_main;
$_dbh_shadow->disconnect if $_dbh_shadow and Param("shadowdb");
undef $_dbh_main;
undef $_dbh_shadow;
undef $_dbh;
} }
sub DESTROY { sub END {
my $self = shift; _cleanup();
# Clean up transient items. We can't just let perl handle removing
# stuff from the $self hash because some stuff (eg database handles)
# may need special casing
# under a persistent environment (ie mod_perl)
$self->_cleanup;
# Now clean up the persistent items
$self->{_dbh_main}->disconnect if $self->{_dbh_main};
$self->{_dbh_shadow}->disconnect if
$self->{_dbh_shadow} and Param("shadowdb")
} }
1; 1;
...@@ -141,11 +109,9 @@ and modules ...@@ -141,11 +109,9 @@ and modules
use Bugzilla; use Bugzilla;
Bugzilla->create;
sub someModulesSub { sub someModulesSub {
my $B = Bugzilla->instance; Bugzilla->dbh->prepare(...);
$B->template->process(...); Bugzilla->template->process(...);
} }
=head1 DESCRIPTION =head1 DESCRIPTION
...@@ -180,32 +146,18 @@ templates), whilst destroying those which are only valid for a single request ...@@ -180,32 +146,18 @@ templates), whilst destroying those which are only valid for a single request
=back =back
Note that items accessible via this object may be loaded when the Bugzilla Note that items accessible via this object are demand-loaded when requested.
object is created, or may be demand-loaded when requested.
For something to be added to this object, it should either be able to benefit For something to be added to this object, it should either be able to benefit
from persistence when run under mod_perl (such as the a C<template> object), from persistence when run under mod_perl (such as the a C<template> object),
or should be something which is globally required by a large ammount of code or should be something which is globally required by a large ammount of code
(such as the current C<user> object). (such as the current C<user> object).
=head1 CREATION =head1 METHODS
=over 4
=item C<create>
Creates the C<Bugzilla> object, and initialises any per-request data
=item C<instance>
Returns the current C<Bugzilla> instance. If one doesn't exist, then it will
be created, but no per-request data will be set. The only use this method has
for creating the object is from a mod_perl init script. (Its also what
L<Class::Singleton> does, and I'm trying to keep that interface for this)
=back
=head1 FUNCTIONS Note that all C<Bugzilla> functionailty is method based; use C<Bugzilla->dbh>
rather than C<Bugzilla::dbh>. Nothing cares about this now, but don't rely on
that.
=over 4 =over 4
......
...@@ -60,7 +60,7 @@ sub SendSQL { ...@@ -60,7 +60,7 @@ sub SendSQL {
require Bugzilla; require Bugzilla;
$_current_sth = Bugzilla->instance->dbh->prepare($str); $_current_sth = Bugzilla->dbh->prepare($str);
return $_current_sth->execute; return $_current_sth->execute;
} }
...@@ -73,7 +73,7 @@ sub SqlQuote { ...@@ -73,7 +73,7 @@ sub SqlQuote {
require Bugzilla; require Bugzilla;
my $res = Bugzilla->instance->dbh->quote($str); my $res = Bugzilla->dbh->quote($str);
trick_taint($res); trick_taint($res);
......
...@@ -31,7 +31,18 @@ use Bugzilla; ...@@ -31,7 +31,18 @@ use Bugzilla;
sub new { sub new {
my ($class, $context) = @_; my ($class, $context) = @_;
return Bugzilla->instance; return bless {}, $class;
}
sub AUTOLOAD {
my $class = shift;
our $AUTOLOAD;
$AUTOLOAD =~ s/^.*:://;
return if $AUTOLOAD eq 'DESTROY';
return Bugzilla->$AUTOLOAD(@_);
} }
1; 1;
......
...@@ -896,7 +896,7 @@ sub GetBugActivity { ...@@ -896,7 +896,7 @@ sub GetBugActivity {
use Bugzilla; use Bugzilla;
# XXX - mod_perl - reset this between runs # XXX - mod_perl - reset this between runs
$::cgi = Bugzilla->instance->cgi; $::cgi = Bugzilla->cgi;
# Set up stuff for compatibility with the old CGI.pl code # Set up stuff for compatibility with the old CGI.pl code
# This code will be removed as soon as possible, in favour of # This code will be removed as soon as possible, in favour of
......
...@@ -629,7 +629,7 @@ if ($serverpush) { ...@@ -629,7 +629,7 @@ if ($serverpush) {
# Connect to the shadow database if this installation is using one to improve # Connect to the shadow database if this installation is using one to improve
# query performance. # query performance.
Bugzilla->instance->switch_to_shadow_db(); Bugzilla->switch_to_shadow_db();
# Normally, we ignore SIGTERM and SIGPIPE (see globals.pl) but we need to # Normally, we ignore SIGTERM and SIGPIPE (see globals.pl) but we need to
# respond to them here to prevent someone DOSing us by reloading a query # respond to them here to prevent someone DOSing us by reloading a query
......
...@@ -43,7 +43,7 @@ if (chdir("graphs")) { ...@@ -43,7 +43,7 @@ if (chdir("graphs")) {
ConnectToDatabase(); ConnectToDatabase();
GetVersionTable(); GetVersionTable();
Bugzilla->instance->switch_to_shadow_db(); Bugzilla->switch_to_shadow_db();
my @myproducts; my @myproducts;
push( @myproducts, "-All-", @::legal_product ); push( @myproducts, "-All-", @::legal_product );
......
...@@ -56,7 +56,7 @@ GetVersionTable(); ...@@ -56,7 +56,7 @@ GetVersionTable();
quietly_check_login(); quietly_check_login();
Bugzilla->instance->switch_to_shadow_db(); Bugzilla->switch_to_shadow_db();
use vars qw (%FORM $userid @legal_product); use vars qw (%FORM $userid @legal_product);
......
...@@ -1517,9 +1517,7 @@ sub GetFormat { ...@@ -1517,9 +1517,7 @@ sub GetFormat {
use Bugzilla; use Bugzilla;
$::BZ = Bugzilla->create(); $::template = Bugzilla->template();
$::template = $::BZ->template();
$::vars = {}; $::vars = {};
......
...@@ -46,7 +46,7 @@ GetVersionTable(); ...@@ -46,7 +46,7 @@ GetVersionTable();
confirm_login(); confirm_login();
Bugzilla->instance->switch_to_shadow_db(); Bugzilla->switch_to_shadow_db();
my $action = $cgi->param('action') || 'menu'; my $action = $cgi->param('action') || 'menu';
......
...@@ -60,7 +60,7 @@ quietly_check_login(); ...@@ -60,7 +60,7 @@ quietly_check_login();
GetVersionTable(); GetVersionTable();
Bugzilla->instance->switch_to_shadow_db(); Bugzilla->switch_to_shadow_db();
# We only want those products that the user has permissions for. # We only want those products that the user has permissions for.
my @myproducts; my @myproducts;
......
...@@ -33,7 +33,7 @@ quietly_check_login(); ...@@ -33,7 +33,7 @@ quietly_check_login();
# Connect to the shadow database if this installation is using one to improve # Connect to the shadow database if this installation is using one to improve
# performance. # performance.
Bugzilla->instance->switch_to_shadow_db(); Bugzilla->switch_to_shadow_db();
use vars qw($template $vars $userid); use vars qw($template $vars $userid);
......
...@@ -39,7 +39,7 @@ quietly_check_login(); ...@@ -39,7 +39,7 @@ quietly_check_login();
# Connect to the shadow database if this installation is using one to improve # Connect to the shadow database if this installation is using one to improve
# performance. # performance.
Bugzilla->instance->switch_to_shadow_db(); Bugzilla->switch_to_shadow_db();
# More warning suppression silliness. # More warning suppression silliness.
$::userid = $::userid; $::userid = $::userid;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment