From 2721de5aa6b81e069a44b05e8b8e44c904928301 Mon Sep 17 00:00:00 2001 From: Byron Jones Date: Mon, 7 Jul 2014 16:01:17 +0800 Subject: Bug 1033955: backport bug 1028795 to bmo (pre-load all related bugs during show_bug initialisation) --- Bugzilla/Bug.pm | 67 +++++++++++++++++++++++++++++++++++++++++++++++---- Bugzilla/Object.pm | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++ Bugzilla/Template.pm | 9 +++---- 3 files changed, 136 insertions(+), 8 deletions(-) diff --git a/Bugzilla/Bug.pm b/Bugzilla/Bug.pm index 217253b02..e8f0fd44a 100644 --- a/Bugzilla/Bug.pm +++ b/Bugzilla/Bug.pm @@ -514,6 +514,52 @@ sub preload { foreach my $bug (@$bugs) { $bug->comments(); } + + foreach my $bug (@$bugs) { + $bug->_preload_referenced_bugs(); + } +} + +# Helps load up bugs referenced in comments by retrieving them with a single +# query from the database and injecting bug objects into the object-cache. +sub _preload_referenced_bugs { + my $self = shift; + my @referenced_bug_ids; + + # inject current duplicates into the object-cache first + foreach my $bug (@{ $self->duplicates }) { + $bug->object_cache_set() + unless Bugzilla::Bug->object_cache_get($bug->id); + } + + # preload bugs from comments + require Bugzilla::Template; + foreach my $comment (@{ $self->comments }) { + if ($comment->type == CMT_HAS_DUPE || $comment->type == CMT_DUPE_OF) { + # duplicate bugs that aren't currently in $self->duplicates + push @referenced_bug_ids, $comment->extra_data + unless Bugzilla::Bug->object_cache_get($comment->extra_data); + } + else { + # bugs referenced in comments + Bugzilla::Template::quoteUrls($comment->body, undef, undef, undef, + sub { + my $bug_id = $_[0]; + push @referenced_bug_ids, $bug_id + unless Bugzilla::Bug->object_cache_get($bug_id); + }); + } + } + + # inject into object-cache + my $referenced_bugs = Bugzilla::Bug->new_from_list( + [ uniq @referenced_bug_ids ]); + foreach my $bug (@$referenced_bugs) { + $bug->object_cache_set(); + } + + # preload bug visibility + Bugzilla->user->visible_bugs(\@referenced_bug_ids); } sub possible_duplicates { @@ -3908,10 +3954,23 @@ sub EmitDependList { # Creates a lot of bug objects in the same order as the input array. sub _bugs_in_order { my ($self, $bug_ids) = @_; - my $bugs = $self->new_from_list($bug_ids); - my %bug_map = map { $_->id => $_ } @$bugs; - my @result = map { $bug_map{$_} } @$bug_ids; - return \@result; + my %bug_map; + # there's no need to load bugs from the database if they are already in the + # object-cache + my @missing_ids; + foreach my $bug_id (@$bug_ids) { + if (my $bug = Bugzilla::Bug->object_cache_get($bug_id)) { + $bug_map{$bug_id} = $bug; + } + else { + push @missing_ids, $bug_id; + } + } + my $bugs = $self->new_from_list(\@missing_ids); + foreach my $bug (@$bugs) { + $bug_map{$bug->id} = $bug; + } + return [ map { $bug_map{$_} } @$bug_ids ]; } # Get the activity of a bug, starting from $starttime (if given). diff --git a/Bugzilla/Object.pm b/Bugzilla/Object.pm index e3e4ca229..8a7bba1c5 100644 --- a/Bugzilla/Object.pm +++ b/Bugzilla/Object.pm @@ -224,6 +224,22 @@ sub initialize { # Provides a mechanism for objects to be cached in the request_cahce +sub object_cache_get { + my ($class, $id) = @_; + return $class->_object_cache_get( + { id => $id, cache => 1}, + $class + ); +} + +sub object_cache_set { + my $self = shift; + return $self->_object_cache_set( + { id => $self->id, cache => 1 }, + $self + ); +} + sub _object_cache_get { my $class = shift; my ($param) = @_; @@ -1456,6 +1472,58 @@ Returns C<1> if the passed-in value is true, C<0> otherwise. =back +=head2 CACHE FUNCTIONS + +=over + +=item C + +=over + +=item B + +Class function which returns an object from the object-cache for the provided +C<$id>. + +=item B + +Takes an integer C<$id> of the object to retrieve. + +=item B + +Returns the object from the cache if found, otherwise returns C. + +=item B + +my $bug_from_cache = Bugzilla::Bug->object_cache_get(35); + +=back + +=item C + +=over + +=item B + +Object function which injects the object into the object-cache, using the +object's C as the key. + +=item B + +(none) + +=item B + +(nothing) + +=item B + +$bug->object_cache_set(); + +=back + +=back + =head1 CLASS FUNCTIONS =over diff --git a/Bugzilla/Template.pm b/Bugzilla/Template.pm index b805a5a2c..057298b06 100644 --- a/Bugzilla/Template.pm +++ b/Bugzilla/Template.pm @@ -156,9 +156,10 @@ sub get_format { # If you want to modify this routine, read the comments carefully sub quoteUrls { - my ($text, $bug, $comment, $user) = @_; + my ($text, $bug, $comment, $user, $bug_link_func) = @_; return $text unless $text; $user ||= Bugzilla->user; + $bug_link_func ||= \&get_bug_link; # We use /g for speed, but uris can have other things inside them # (http://foo/bug#3 for example). Filtering that out filters valid @@ -212,7 +213,7 @@ sub quoteUrls { map { qr/$_/ } grep($_, Bugzilla->params->{'urlbase'}, Bugzilla->params->{'sslbase'})) . ')'; $text =~ s~\b(${urlbase_re}\Qshow_bug.cgi?id=\E([0-9]+)(\#c([0-9]+))?)\b - ~($things[$count++] = get_bug_link($3, $1, { comment_num => $5, user => $user })) && + ~($things[$count++] = $bug_link_func->($3, $1, { comment_num => $5, user => $user })) && ("\x{FDD2}" . ($count-1) . "\x{FDD3}") ~egox; @@ -259,7 +260,7 @@ sub quoteUrls { $text =~ s~\b($bug_re(?:$s*,?$s*$comment_re)?|$comment_re) ~ # We have several choices. $1 here is the link, and $2-4 are set # depending on which part matched - (defined($2) ? get_bug_link($2, $1, { comment_num => $3, user => $user }) : + (defined($2) ? $bug_link_func->($2, $1, { comment_num => $3, user => $user }) : "$1") ~egx; @@ -268,7 +269,7 @@ sub quoteUrls { $text =~ s~(?<=^\*\*\*\ This\ bug\ has\ been\ marked\ as\ a\ duplicate\ of\ ) (\d+) (?=\ \*\*\*\Z) - ~get_bug_link($1, $1, { user => $user }) + ~$bug_link_func->($1, $1, { user => $user }) ~egmx; # Now remove the encoding hacks in reverse order -- cgit v1.2.3-24-g4f1b