Commit b62885e4 authored by lpsolit%gmail.com's avatar lpsolit%gmail.com

Bug 388251: Implement 'new Bugzilla::Attachment' - Patch by Fré©ric Buclin…

Bug 388251: Implement 'new Bugzilla::Attachment' - Patch by Fré©ric Buclin <LpSolit@gmail.com> a=LpSolit
parent 4dd427ea
...@@ -28,23 +28,26 @@ package Bugzilla::Attachment; ...@@ -28,23 +28,26 @@ package Bugzilla::Attachment;
=head1 NAME =head1 NAME
Bugzilla::Attachment - a file related to a bug that a user has uploaded Bugzilla::Attachment - Bugzilla attachment class.
to the Bugzilla server
=head1 SYNOPSIS =head1 SYNOPSIS
use Bugzilla::Attachment; use Bugzilla::Attachment;
# Get the attachment with the given ID. # Get the attachment with the given ID.
my $attachment = Bugzilla::Attachment->get($attach_id); my $attachment = new Bugzilla::Attachment($attach_id);
# Get the attachments with the given IDs. # Get the attachments with the given IDs.
my $attachments = Bugzilla::Attachment->get_list($attach_ids); my $attachments = Bugzilla::Attachment->new_from_list($attach_ids);
=head1 DESCRIPTION =head1 DESCRIPTION
This module defines attachment objects, which represent files related to bugs Attachment.pm represents an attachment object. It is an implementation
that users upload to the Bugzilla server. of L<Bugzilla::Object>, and thus provides all methods that
L<Bugzilla::Object> provides.
The methods that are specific to C<Bugzilla::Attachment> are listed
below.
=cut =cut
...@@ -55,60 +58,37 @@ use Bugzilla::User; ...@@ -55,60 +58,37 @@ use Bugzilla::User;
use Bugzilla::Util; use Bugzilla::Util;
use Bugzilla::Field; use Bugzilla::Field;
sub get { use base qw(Bugzilla::Object);
my $invocant = shift;
my $id = shift;
my $attachments = _retrieve([$id]);
my $self = $attachments->[0];
bless($self, ref($invocant) || $invocant) if $self;
return $self;
}
sub get_list {
my $invocant = shift;
my $ids = shift;
my $attachments = _retrieve($ids); ###############################
foreach my $attachment (@$attachments) { #### Initialization ####
bless($attachment, ref($invocant) || $invocant); ###############################
}
return $attachments; use constant DB_TABLE => 'attachments';
} use constant ID_FIELD => 'attach_id';
use constant LIST_ORDER => ID_FIELD;
sub _retrieve { sub DB_COLUMNS {
my ($ids) = @_;
return [] if scalar(@$ids) == 0;
my @columns = (
'attachments.attach_id AS id',
'attachments.bug_id AS bug_id',
'attachments.description AS description',
'attachments.mimetype AS contenttype',
'attachments.submitter_id AS attacher_id',
Bugzilla->dbh->sql_date_format('attachments.creation_ts',
'%Y.%m.%d %H:%i') . " AS attached",
'attachments.modification_time',
'attachments.filename AS filename',
'attachments.ispatch AS ispatch',
'attachments.isurl AS isurl',
'attachments.isobsolete AS isobsolete',
'attachments.isprivate AS isprivate'
);
my $columns = join(", ", @columns);
my $dbh = Bugzilla->dbh; my $dbh = Bugzilla->dbh;
my $records = $dbh->selectall_arrayref(
"SELECT $columns return qw(
FROM attachments attach_id
WHERE " bug_id
. Bugzilla->dbh->sql_in('attach_id', $ids) description
. " ORDER BY attach_id", filename
{ Slice => {} }); isobsolete
return $records; ispatch
} isprivate
isurl
mimetype
modification_time
submitter_id),
$dbh->sql_date_format('attachments.creation_ts', '%Y.%m.%d %H:%i') . ' AS creation_ts';
}
###############################
#### Accessors ######
###############################
=pod =pod
...@@ -116,21 +96,6 @@ sub _retrieve { ...@@ -116,21 +96,6 @@ sub _retrieve {
=over =over
=item C<id>
the unique identifier for the attachment
=back
=cut
sub id {
my $self = shift;
return $self->{id};
}
=over
=item C<bug_id> =item C<bug_id>
the ID of the bug to which the attachment is attached the ID of the bug to which the attachment is attached
...@@ -189,7 +154,7 @@ the attachment's MIME media type ...@@ -189,7 +154,7 @@ the attachment's MIME media type
sub contenttype { sub contenttype {
my $self = shift; my $self = shift;
return $self->{contenttype}; return $self->{mimetype};
} }
=over =over
...@@ -205,7 +170,7 @@ the user who attached the attachment ...@@ -205,7 +170,7 @@ the user who attached the attachment
sub attacher { sub attacher {
my $self = shift; my $self = shift;
return $self->{attacher} if exists $self->{attacher}; return $self->{attacher} if exists $self->{attacher};
$self->{attacher} = new Bugzilla::User($self->{attacher_id}); $self->{attacher} = new Bugzilla::User($self->{submitter_id});
return $self->{attacher}; return $self->{attacher};
} }
...@@ -221,7 +186,7 @@ the date and time on which the attacher attached the attachment ...@@ -221,7 +186,7 @@ the date and time on which the attacher attached the attachment
sub attached { sub attached {
my $self = shift; my $self = shift;
return $self->{attached}; return $self->{creation_ts};
} }
=over =over
...@@ -367,7 +332,7 @@ sub data { ...@@ -367,7 +332,7 @@ sub data {
FROM attach_data FROM attach_data
WHERE id = ?", WHERE id = ?",
undef, undef,
$self->{id}); $self->id);
# If there's no attachment data in the database, the attachment is stored # If there's no attachment data in the database, the attachment is stored
# in a local file, so retrieve it from there. # in a local file, so retrieve it from there.
...@@ -412,7 +377,7 @@ sub datasize { ...@@ -412,7 +377,7 @@ sub datasize {
Bugzilla->dbh->selectrow_array("SELECT LENGTH(thedata) Bugzilla->dbh->selectrow_array("SELECT LENGTH(thedata)
FROM attach_data FROM attach_data
WHERE id = ?", WHERE id = ?",
undef, $self->{id}) || 0; undef, $self->id) || 0;
# If there's no attachment data in the database, either the attachment # If there's no attachment data in the database, either the attachment
# is stored in a local file, and so retrieve its size from the file, # is stored in a local file, and so retrieve its size from the file,
...@@ -470,6 +435,10 @@ sub flag_types { ...@@ -470,6 +435,10 @@ sub flag_types {
return $self->{flag_types}; return $self->{flag_types};
} }
###############################
#### Validators ######
###############################
# Instance methods; no POD documentation here yet because the only ones so far # Instance methods; no POD documentation here yet because the only ones so far
# are private. # are private.
...@@ -595,7 +564,8 @@ sub get_attachments_by_bug { ...@@ -595,7 +564,8 @@ sub get_attachments_by_bug {
my $attach_ids = $dbh->selectcol_arrayref("SELECT attach_id FROM attachments my $attach_ids = $dbh->selectcol_arrayref("SELECT attach_id FROM attachments
WHERE bug_id = ? $and_restriction", WHERE bug_id = ? $and_restriction",
undef, @values); undef, @values);
my $attachments = Bugzilla::Attachment->get_list($attach_ids);
my $attachments = Bugzilla::Attachment->new_from_list($attach_ids);
# To avoid $attachment->flags to run SQL queries itself for each # To avoid $attachment->flags to run SQL queries itself for each
# attachment listed here, we collect all the data at once and # attachment listed here, we collect all the data at once and
...@@ -769,10 +739,9 @@ sub validate_obsolete { ...@@ -769,10 +739,9 @@ sub validate_obsolete {
detaint_natural($attachid) detaint_natural($attachid)
|| ThrowCodeError('invalid_attach_id_to_obsolete', $vars); || ThrowCodeError('invalid_attach_id_to_obsolete', $vars);
my $attachment = Bugzilla::Attachment->get($attachid);
# Make sure the attachment exists in the database. # Make sure the attachment exists in the database.
ThrowUserError('invalid_attach_id', $vars) unless $attachment; my $attachment = new Bugzilla::Attachment($attachid)
|| ThrowUserError('invalid_attach_id', $vars);
# Check that the user can view and edit this attachment. # Check that the user can view and edit this attachment.
$attachment->validate_can_edit($bug->product_id); $attachment->validate_can_edit($bug->product_id);
...@@ -794,10 +763,13 @@ sub validate_obsolete { ...@@ -794,10 +763,13 @@ sub validate_obsolete {
return @obsolete_attachments; return @obsolete_attachments;
} }
###############################
#### Constructors #####
###############################
=pod =pod
=item C<insert_attachment_for_bug($throw_error, $bug, $user, $timestamp, $hr_vars)> =item C<create($throw_error, $bug, $user, $timestamp, $hr_vars)>
Description: inserts an attachment from CGI input for the given bug. Description: inserts an attachment from CGI input for the given bug.
...@@ -814,7 +786,8 @@ Returns: the ID of the new attachment. ...@@ -814,7 +786,8 @@ Returns: the ID of the new attachment.
=cut =cut
sub insert_attachment_for_bug { # FIXME: needs to follow the way Object->create() works.
sub create {
my ($class, $throw_error, $bug, $user, $timestamp, $hr_vars) = @_; my ($class, $throw_error, $bug, $user, $timestamp, $hr_vars) = @_;
my $cgi = Bugzilla->cgi; my $cgi = Bugzilla->cgi;
...@@ -957,7 +930,7 @@ sub insert_attachment_for_bug { ...@@ -957,7 +930,7 @@ sub insert_attachment_for_bug {
$timestamp, $fieldid, 0, 1)); $timestamp, $fieldid, 0, 1));
} }
my $attachment = Bugzilla::Attachment->get($attachid); my $attachment = new Bugzilla::Attachment($attachid);
# 1. Add flags, if any. To avoid dying if something goes wrong # 1. Add flags, if any. To avoid dying if something goes wrong
# while processing flags, we will eval() flag validation. # while processing flags, we will eval() flag validation.
......
...@@ -180,7 +180,7 @@ sub attachment { ...@@ -180,7 +180,7 @@ sub attachment {
return undef unless $self->attach_id; return undef unless $self->attach_id;
require Bugzilla::Attachment; require Bugzilla::Attachment;
$self->{'attachment'} ||= Bugzilla::Attachment->get($self->attach_id); $self->{'attachment'} ||= new Bugzilla::Attachment($self->attach_id);
return $self->{'attachment'}; return $self->{'attachment'};
} }
......
...@@ -161,7 +161,7 @@ sub validateID { ...@@ -161,7 +161,7 @@ sub validateID {
|| ThrowUserError("invalid_attach_id", { attach_id => $cgi->param($param) }); || ThrowUserError("invalid_attach_id", { attach_id => $cgi->param($param) });
# Make sure the attachment exists in the database. # Make sure the attachment exists in the database.
my $attachment = Bugzilla::Attachment->get($attach_id) my $attachment = new Bugzilla::Attachment($attach_id)
|| ThrowUserError("invalid_attach_id", { attach_id => $attach_id }); || ThrowUserError("invalid_attach_id", { attach_id => $attach_id });
# Make sure the user is authorized to access this attachment's bug. # Make sure the user is authorized to access this attachment's bug.
...@@ -320,7 +320,7 @@ sub enter { ...@@ -320,7 +320,7 @@ sub enter {
# Define the variables and functions that will be passed to the UI template. # Define the variables and functions that will be passed to the UI template.
$vars->{'bug'} = $bug; $vars->{'bug'} = $bug;
$vars->{'attachments'} = Bugzilla::Attachment->get_list($attach_ids); $vars->{'attachments'} = Bugzilla::Attachment->new_from_list($attach_ids);
my $flag_types = Bugzilla::FlagType::match({'target_type' => 'attachment', my $flag_types = Bugzilla::FlagType::match({'target_type' => 'attachment',
'product_id' => $bug->product_id, 'product_id' => $bug->product_id,
...@@ -374,8 +374,7 @@ sub insert { ...@@ -374,8 +374,7 @@ sub insert {
} }
my $attachment = my $attachment =
Bugzilla::Attachment->insert_attachment_for_bug(THROW_ERROR, $bug, $user, Bugzilla::Attachment->create(THROW_ERROR, $bug, $user, $timestamp, $vars);
$timestamp, $vars);
# Insert a comment about the new attachment into the database. # Insert a comment about the new attachment into the database.
my $comment = "Created an attachment (id=" . $attachment->id . ")\n" . my $comment = "Created an attachment (id=" . $attachment->id . ")\n" .
...@@ -558,7 +557,7 @@ sub update { ...@@ -558,7 +557,7 @@ sub update {
$cgi->param('ispatch'), $cgi->param('isobsolete'), $cgi->param('ispatch'), $cgi->param('isobsolete'),
$cgi->param('isprivate'), $timestamp, $attachment->id)); $cgi->param('isprivate'), $timestamp, $attachment->id));
my $updated_attachment = Bugzilla::Attachment->get($attachment->id); my $updated_attachment = new Bugzilla::Attachment($attachment->id);
# Record changes in the activity table. # Record changes in the activity table.
my $sth = $dbh->prepare('INSERT INTO bugs_activity (bug_id, attach_id, who, bug_when, my $sth = $dbh->prepare('INSERT INTO bugs_activity (bug_id, attach_id, who, bug_when,
fieldid, removed, added) fieldid, removed, added)
......
...@@ -194,7 +194,7 @@ if (defined $cgi->param('version')) { ...@@ -194,7 +194,7 @@ if (defined $cgi->param('version')) {
# Add an attachment if requested. # Add an attachment if requested.
if (defined($cgi->upload('data')) || $cgi->param('attachurl')) { if (defined($cgi->upload('data')) || $cgi->param('attachurl')) {
$cgi->param('isprivate', $cgi->param('commentprivacy')); $cgi->param('isprivate', $cgi->param('commentprivacy'));
my $attachment = Bugzilla::Attachment->insert_attachment_for_bug(!THROW_ERROR, my $attachment = Bugzilla::Attachment->create(!THROW_ERROR,
$bug, $user, $timestamp, $vars); $bug, $user, $timestamp, $vars);
if ($attachment) { if ($attachment) {
......
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