=head1 NAME
-Bugzilla::Attachment - a file related to a bug that a user has uploaded
- to the Bugzilla server
+Bugzilla::Attachment - Bugzilla attachment class.
=head1 SYNOPSIS
use Bugzilla::Attachment;
# 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.
- my $attachments = Bugzilla::Attachment->get_list($attach_ids);
+ my $attachments = Bugzilla::Attachment->new_from_list($attach_ids);
=head1 DESCRIPTION
-This module defines attachment objects, which represent files related to bugs
-that users upload to the Bugzilla server.
+Attachment.pm represents an attachment object. It is an implementation
+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
use Bugzilla::Util;
use Bugzilla::Field;
-sub get {
- 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;
+use base qw(Bugzilla::Object);
- my $attachments = _retrieve($ids);
- foreach my $attachment (@$attachments) {
- bless($attachment, ref($invocant) || $invocant);
- }
+###############################
+#### Initialization ####
+###############################
- return $attachments;
-}
+use constant DB_TABLE => 'attachments';
+use constant ID_FIELD => 'attach_id';
+use constant LIST_ORDER => ID_FIELD;
-sub _retrieve {
- 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);
+sub DB_COLUMNS {
my $dbh = Bugzilla->dbh;
- my $records = $dbh->selectall_arrayref(
- "SELECT $columns
- FROM attachments
- WHERE "
- . Bugzilla->dbh->sql_in('attach_id', $ids)
- . " ORDER BY attach_id",
- { Slice => {} });
- return $records;
-}
+
+ return qw(
+ attach_id
+ bug_id
+ description
+ filename
+ isobsolete
+ 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
=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>
the ID of the bug to which the attachment is attached
sub contenttype {
my $self = shift;
- return $self->{contenttype};
+ return $self->{mimetype};
}
=over
sub attacher {
my $self = shift;
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};
}
sub attached {
my $self = shift;
- return $self->{attached};
+ return $self->{creation_ts};
}
=over
FROM attach_data
WHERE id = ?",
undef,
- $self->{id});
+ $self->id);
# If there's no attachment data in the database, the attachment is stored
# in a local file, so retrieve it from there.
Bugzilla->dbh->selectrow_array("SELECT LENGTH(thedata)
FROM attach_data
WHERE id = ?",
- undef, $self->{id}) || 0;
+ undef, $self->id) || 0;
# 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,
return $self->{flag_types};
}
+###############################
+#### Validators ######
+###############################
+
# Instance methods; no POD documentation here yet because the only ones so far
# are private.
my $attach_ids = $dbh->selectcol_arrayref("SELECT attach_id FROM attachments
WHERE bug_id = ? $and_restriction",
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
# attachment listed here, we collect all the data at once and
detaint_natural($attachid)
|| ThrowCodeError('invalid_attach_id_to_obsolete', $vars);
- my $attachment = Bugzilla::Attachment->get($attachid);
-
# 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.
$attachment->validate_can_edit($bug->product_id);
return @obsolete_attachments;
}
+###############################
+#### Constructors #####
+###############################
=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.
=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 $cgi = Bugzilla->cgi;
$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
# while processing flags, we will eval() flag validation.
|| ThrowUserError("invalid_attach_id", { attach_id => $cgi->param($param) });
# 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 });
# Make sure the user is authorized to access this attachment's bug.
# Define the variables and functions that will be passed to the UI template.
$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',
'product_id' => $bug->product_id,
}
my $attachment =
- Bugzilla::Attachment->insert_attachment_for_bug(THROW_ERROR, $bug, $user,
- $timestamp, $vars);
+ Bugzilla::Attachment->create(THROW_ERROR, $bug, $user, $timestamp, $vars);
# Insert a comment about the new attachment into the database.
my $comment = "Created an attachment (id=" . $attachment->id . ")\n" .
$cgi->param('ispatch'), $cgi->param('isobsolete'),
$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.
my $sth = $dbh->prepare('INSERT INTO bugs_activity (bug_id, attach_id, who, bug_when,
fieldid, removed, added)