]> git.ipfire.org Git - thirdparty/bugzilla.git/commitdiff
Bug 191863 - Clean up Bugzilla.pm
authorbbaetz%acm.org <>
Fri, 7 Feb 2003 15:19:01 +0000 (15:19 +0000)
committerbbaetz%acm.org <>
Fri, 7 Feb 2003 15:19:01 +0000 (15:19 +0000)
r=gerv, justdave
a=justdave

12 files changed:
Bugzilla.pm
Bugzilla/DB.pm
Bugzilla/Template/Plugin/Bugzilla.pm
CGI.pl
buglist.cgi
collectstats.pl
duplicates.cgi
globals.pl
report.cgi
reports.cgi
showdependencygraph.cgi
showdependencytree.cgi

index 66831046d9ec392d43963fdeb606d76b8c51aab6..a45c5ca0fd40bd6e714b5523cfb57e940069e766 100644 (file)
@@ -29,103 +29,71 @@ use Bugzilla::Config;
 use Bugzilla::DB;
 use Bugzilla::Template;
 
-sub create {
+my $_template;
+sub template {
     my $class = shift;
-    my $B = $class->instance;
-
-    # And set up the vars for this request
-    $B->_init_transient;
+    $_template ||= Bugzilla::Template->create();
+    return $_template;
+}
 
-    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
-# the same interface in case we do change in the future
+my $_dbh;
+my $_dbh_main;
+my $_dbh_shadow;
 
-my $_instance;
-sub instance {
+sub dbh {
     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 {
-    my $self = shift;
+    my $class = shift;
 
-    if (!$self->{_dbh_shadow}) {
+    if (!$_dbh_shadow) {
         if (Param('shadowdb')) {
-            $self->{_dbh_shadow} = Bugzilla::DB::connect_shadow();
+            $_dbh_shadow = Bugzilla::DB::connect_shadow();
         } else {
-            $self->{_dbh_shadow} = $self->{_dbh_main};
+            $_dbh_shadow = $_dbh_main;
         }
     }
 
-    $self->{_dbh} = $self->{_dbh_shadow};
+    $_dbh = $_dbh_shadow;
 }
 
 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 $self = { };
-    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();
+    $_dbh = $_dbh_main;
 }
 
-# Initialise transient (per-request) items
-sub _init_transient {
-    my $self = shift;
+# Private methods
 
-    $self->{_cgi} = new Bugzilla::CGI if exists $::ENV{'GATEWAY_INTERFACE'};
-}
-
-# Clean up transient items such as database handles
+# Per process cleanup
 sub _cleanup {
-    my $self = shift;
-
-    delete $self->{_cgi};
+    undef $_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 {
-    my $self = shift;
-
-    # 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")
+sub END {
+    _cleanup();
 }
 
 1;
@@ -141,11 +109,9 @@ and modules
 
   use Bugzilla;
 
-  Bugzilla->create;
-
   sub someModulesSub {
-    my $B = Bugzilla->instance;
-    $B->template->process(...);
+    Bugzilla->dbh->prepare(...);
+    Bugzilla->template->process(...);
   }
 
 =head1 DESCRIPTION
@@ -180,32 +146,18 @@ templates), whilst destroying those which are only valid for a single request
 
 =back
 
-Note that items accessible via this object may be loaded when the Bugzilla
-object is created, or may be demand-loaded when requested.
+Note that items accessible via this object are demand-loaded when requested.
 
 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),
 or should be something which is globally required by a large ammount of code
 (such as the current C<user> object).
 
-=head1 CREATION
-
-=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 METHODS
 
-=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
 
index 681b0597aeaad6e5bc1bfe0aef44604cbd35e990..23078369adeea93dc233f87137923b2559fa0529 100644 (file)
@@ -60,7 +60,7 @@ sub SendSQL {
 
     require Bugzilla;
 
-    $_current_sth = Bugzilla->instance->dbh->prepare($str);
+    $_current_sth = Bugzilla->dbh->prepare($str);
     return $_current_sth->execute;
 }
 
@@ -73,7 +73,7 @@ sub SqlQuote {
 
     require Bugzilla;
 
-    my $res = Bugzilla->instance->dbh->quote($str);
+    my $res = Bugzilla->dbh->quote($str);
 
     trick_taint($res);
 
index 285553b7e32ee3ae2f5ae62a4dc4cb9828776d53..101bd06ee7dc9280da493a518e86f177e567673b 100644 (file)
@@ -31,7 +31,18 @@ use Bugzilla;
 sub new {
     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;
diff --git a/CGI.pl b/CGI.pl
index 6b9751df6ee6a9dee8bcac7bc0e606e7c004395c..61797ac44b5cb42f29078ad592aca76cd57608c2 100644 (file)
--- a/CGI.pl
+++ b/CGI.pl
@@ -896,7 +896,7 @@ sub GetBugActivity {
 use Bugzilla;
 
 # 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
 # This code will be removed as soon as possible, in favour of
index 680b836cf3b978dd16bbadd2d1e66bf65d3cd00a..e8a21d76e477c4a9fe7779d3718282dc55d16ec2 100755 (executable)
@@ -629,7 +629,7 @@ if ($serverpush) {
 
 # Connect to the shadow database if this installation is using one to improve
 # 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
 # respond to them here to prevent someone DOSing us by reloading a query
index 27a6e1840ae994af7e48516586d380efebac3ec1..bdc027a689cbe4a29e5e6068d22ae9229a223f67 100755 (executable)
@@ -43,7 +43,7 @@ if (chdir("graphs")) {
 ConnectToDatabase();
 GetVersionTable();
 
-Bugzilla->instance->switch_to_shadow_db();
+Bugzilla->switch_to_shadow_db();
 
 my @myproducts;
 push( @myproducts, "-All-", @::legal_product );
index 45eb219ff34316b9a6099b5d8bf5ff614156ab85..175fb649a77f16e08d5fc31493fcbc527fb291f5 100755 (executable)
@@ -56,7 +56,7 @@ GetVersionTable();
 
 quietly_check_login();
 
-Bugzilla->instance->switch_to_shadow_db();
+Bugzilla->switch_to_shadow_db();
 
 use vars qw (%FORM $userid @legal_product);
 
index 8162b9d21f2248aad5d7bc67032a9740a3f50f1d..1aee0bd02f40ba11ed9e8f26bfcf381f72bd29e8 100644 (file)
@@ -1517,9 +1517,7 @@ sub GetFormat {
 
 use Bugzilla;
 
-$::BZ = Bugzilla->create();
-
-$::template = $::BZ->template();
+$::template = Bugzilla->template();
 
 $::vars = {};
 
index d113e6d898c7f5ee0fc54e31bea91b00cfcd9306..99566d67cad1531aa3104ca36fd261189ddded20 100755 (executable)
@@ -46,7 +46,7 @@ GetVersionTable();
 
 confirm_login();
 
-Bugzilla->instance->switch_to_shadow_db();
+Bugzilla->switch_to_shadow_db();
 
 my $action = $cgi->param('action') || 'menu';
 
index 230fe32db8ca89b88cd4dc8aed77d992dbe645e8..5c802ccfee410fe1ccc82cc197649d8068eaf888 100755 (executable)
@@ -60,7 +60,7 @@ quietly_check_login();
 
 GetVersionTable();
 
-Bugzilla->instance->switch_to_shadow_db();
+Bugzilla->switch_to_shadow_db();
 
 # We only want those products that the user has permissions for.
 my @myproducts;
index 24d0cdfa573a5fc6e5b976ed56b340ae25eef838..77a1d4dc020b93d81e0c354159d7a69794e65fb6 100755 (executable)
@@ -33,7 +33,7 @@ quietly_check_login();
 
 # Connect to the shadow database if this installation is using one to improve
 # performance.
-Bugzilla->instance->switch_to_shadow_db();
+Bugzilla->switch_to_shadow_db();
 
 use vars qw($template $vars $userid);
 
index e246d5c8d7d38f6eca1f8288c583275c8dbc292b..4ee9e4cc83a8f38aa4ea55c0b0895e8a80782e35 100755 (executable)
@@ -39,7 +39,7 @@ quietly_check_login();
 
 # Connect to the shadow database if this installation is using one to improve
 # performance.
-Bugzilla->instance->switch_to_shadow_db();
+Bugzilla->switch_to_shadow_db();
 
 # More warning suppression silliness.
 $::userid = $::userid;