From e9f07c7e2bc8dcf66381ed588ddd955d77b7d26f Mon Sep 17 00:00:00 2001 From: Byron Jones Date: Wed, 31 Jul 2013 13:20:07 +0800 Subject: Bug 877078: shift bugmail generation to the jobqueue r=sgreen, a=sgreen --- Bugzilla/Object.pm | 50 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) (limited to 'Bugzilla/Object.pm') diff --git a/Bugzilla/Object.pm b/Bugzilla/Object.pm index 0cecb8eca..9f305e929 100644 --- a/Bugzilla/Object.pm +++ b/Bugzilla/Object.pm @@ -113,7 +113,10 @@ sub _init { "SELECT $columns FROM $table WHERE $condition", undef, @values); } - $class->_cache_set($param, $object) if $object; + if ($object) { + $class->_serialisation_keys($object); + $class->_cache_set($param, $object); + } return $object; } @@ -144,6 +147,15 @@ sub cache_key { } } +# To support serialisation, we need to capture the keys in an object's default +# hashref. +sub _serialisation_keys { + my ($class, $object) = @_; + my $cache = Bugzilla->request_cache->{serialisation_keys} ||= {}; + $cache->{$class} = [ keys %$object ] if $object; + return @{ $cache->{$class} }; +} + sub check { my ($invocant, $param) = @_; my $class = ref($invocant) || $invocant; @@ -199,6 +211,14 @@ sub new_from_list { return match($invocant, { $id_field => \@detainted_ids }); } +sub new_from_hash { + my $invocant = shift; + my $class = ref($invocant) || $invocant; + my $object = shift; + bless($object, $class); + return $object; +} + # Note: Future extensions to this could be: # * Add a MATCH_JOIN constant so that we can join against # certain other tables for the WHERE criteria. @@ -297,7 +317,8 @@ sub _do_list_select { my @untainted = @{ $values || [] }; trick_taint($_) foreach @untainted; my $objects = $dbh->selectall_arrayref($sql, {Slice=>{}}, @untainted); - bless ($_, $class) foreach @$objects; + $class->_serialisation_keys($objects->[0]) if @$objects; + bless($_, $class) foreach @$objects; return $objects } @@ -474,6 +495,13 @@ sub audit_log { } } +sub flatten_to_hash { + my $self = shift; + my $class = blessed($self); + my %hash = map { $_ => $self->{$_} } $class->_serialisation_keys; + return \%hash; +} + ############################### #### Subroutines ###### ############################### @@ -1040,6 +1068,13 @@ template. Returns: A reference to an array of objects. +=item C + + Description: Create an object from the given hash. + + Params: $hashref - A reference to a hash which was created by + flatten_to_hash. + =item C =over @@ -1277,6 +1312,17 @@ that should be passed to the C function that is called. =back +=head2 Simple Methods + +=over + +=item C + +Returns a hashref suitable for serialisation and re-inflation with C. + +=back + + =head2 Simple Validators You can use these in your subclass L or L. -- cgit v1.2.3-24-g4f1b