Code style

hinted-selects
Vitaliy Filippov 2014-08-14 20:20:32 +04:00
parent 14b1019de4
commit b2cf9833dc
4 changed files with 182 additions and 158 deletions

View File

@ -1,5 +1,3 @@
# -*- Mode: perl; indent-tabs-mode: nil -*-
#
# The contents of this file are subject to the Mozilla Public
# License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
@ -98,37 +96,39 @@ sub bug_count { 0 }
#### Validators ####
###############################
sub _check_name {
sub _check_name
{
my ($invocant, $name) = @_;
$name = trim($name);
$name || ThrowUserError('classification_not_specified');
if (length($name) > MAX_FIELD_VALUE_SIZE) {
ThrowUserError('classification_name_too_long', {'name' => $name});
if (length($name) > MAX_FIELD_VALUE_SIZE)
{
ThrowUserError('classification_name_too_long', { name => $name });
}
my $classification = new Bugzilla::Classification({name => $name});
if ($classification && (!ref $invocant || $classification->id != $invocant->id)) {
my $classification = new Bugzilla::Classification({ name => $name });
if ($classification && (!ref $invocant || $classification->id != $invocant->id))
{
ThrowUserError("classification_already_exists", { name => $classification->name });
}
return $name;
}
sub _check_description {
sub _check_description
{
my ($invocant, $description) = @_;
$description = trim($description || '');
return $description;
}
sub _check_sortkey {
sub _check_sortkey
{
my ($invocant, $sortkey) = @_;
$sortkey ||= 0;
my $stored_sortkey = $sortkey;
if (!detaint_natural($sortkey) || $sortkey > MAX_SMALLINT) {
ThrowUserError('classification_invalid_sortkey', { 'sortkey' => $stored_sortkey });
if (!detaint_natural($sortkey) || $sortkey > MAX_SMALLINT)
{
ThrowUserError('classification_invalid_sortkey', { sortkey => $sortkey });
}
return $sortkey;
}
@ -141,39 +141,36 @@ sub set_name { $_[0]->set('name', $_[1]); }
sub set_description { $_[0]->set('description', $_[1]); }
sub set_sortkey { $_[0]->set('sortkey', $_[1]); }
sub product_count {
sub product_count
{
my $self = shift;
my $dbh = Bugzilla->dbh;
if (!defined $self->{'product_count'}) {
$self->{'product_count'} = $dbh->selectrow_array(q{
SELECT COUNT(*) FROM products
WHERE classification_id = ?}, undef, $self->id) || 0;
if (!defined $self->{product_count})
{
$self->{product_count} = $dbh->selectrow_array(
'SELECT COUNT(*) FROM products WHERE classification_id = ?', undef, $self->id
) || 0;
}
return $self->{'product_count'};
return $self->{product_count};
}
sub products {
sub products
{
my $self = shift;
my $dbh = Bugzilla->dbh;
if (!$self->{'products'}) {
my $product_ids = $dbh->selectcol_arrayref(q{
SELECT id FROM products
WHERE classification_id = ?
ORDER BY name}, undef, $self->id);
$self->{'products'} = Bugzilla::Product->new_from_list($product_ids);
if (!$self->{products})
{
$self->{products} = Bugzilla::Product->_do_list_select('classification_id=?', [ $self->id ]);
}
return $self->{'products'};
return $self->{products};
}
###############################
#### Accessors ####
###############################
sub description { return $_[0]->{'description'}; }
sub sortkey { return $_[0]->{'sortkey'}; }
sub description { $_[0]->{description} }
sub sortkey { $_[0]->{sortkey} }
# Return all visible classifications.
sub get_all { @{ Bugzilla->user->get_selectable_classifications } }
@ -215,7 +212,7 @@ A Classification is a higher-level grouping of Products.
=over
=item C<product_count()>
=item B<product_count()>
Description: Returns the total number of products that belong to
the classification.
@ -224,7 +221,7 @@ A Classification is a higher-level grouping of Products.
Returns: Integer - The total of products inside the classification.
=item C<products>
=item B<products>
Description: Returns all products of the classification.

View File

@ -1,5 +1,3 @@
# -*- Mode: perl; indent-tabs-mode: nil -*-
#
# The contents of this file are subject to the Mozilla Public
# License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
@ -77,12 +75,14 @@ sub update {
# Speeds up displays of comment lists by loading all ->author objects
# at once for a whole list.
sub preload {
sub preload
{
my ($class, $comments) = @_;
my %user_ids = map { $_->{who} => 1 } @$comments;
my $users = Bugzilla::User->new_from_list([keys %user_ids]);
my %user_map = map { $_->id => $_ } @$users;
foreach my $comment (@$comments) {
foreach my $comment (@$comments)
{
$comment->{author} = $user_map{$comment->{who}};
}
}
@ -91,14 +91,14 @@ sub preload {
#### Accessors ######
###############################
sub already_wrapped { return $_[0]->{'already_wrapped'}; }
sub bug_id { return $_[0]->{'bug_id'}; }
sub creation_ts { return $_[0]->{'bug_when'}; }
sub is_private { return $_[0]->{'isprivate'}; }
sub work_time { return $_[0]->{'work_time'}; }
sub type { return $_[0]->{'type'}; }
sub extra_data { return $_[0]->{'extra_data'} }
sub who { return $_[0]->{'who'}; }
sub already_wrapped { $_[0]->{already_wrapped} }
sub bug_id { $_[0]->{bug_id} }
sub creation_ts { $_[0]->{bug_when} }
sub is_private { $_[0]->{isprivate} }
sub work_time { $_[0]->{work_time} }
sub type { $_[0]->{type} }
sub extra_data { $_[0]->{extra_data} }
sub who { $_[0]->{who} }
sub body
{
@ -108,45 +108,49 @@ sub body
{
my $max_lines = Bugzilla->params->{preview_comment_lines} - 1;
my $line_length = Bugzilla->params->{comment_line_length} - 1;
my $result = $self->{'thetext'};
my $result = $self->{thetext};
$result =~ s/(>[^\n]*?\n)+/>...\n/g;
$result =~ s/^((?>[^\n]{0,$line_length}.){0,$max_lines}(?>[^\n]{0,$line_length}\s)).*$/$1.../s if !$self->check_length($result);
return $result;
}
return $_[0]->{'thetext'};
return $_[0]->{thetext};
}
sub bug {
sub bug
{
my $self = shift;
require Bugzilla::Bug;
$self->{bug} ||= new Bugzilla::Bug($self->bug_id);
return $self->{bug};
}
sub is_about_attachment {
sub is_about_attachment
{
my ($self) = @_;
return 1 if ($self->type == CMT_ATTACHMENT_CREATED
or $self->type == CMT_ATTACHMENT_UPDATED);
return 1 if ($self->type == CMT_ATTACHMENT_CREATED || $self->type == CMT_ATTACHMENT_UPDATED);
return 0;
}
sub attachment {
sub attachment
{
my ($self) = @_;
return undef if not $self->is_about_attachment;
$self->{attachment} ||= new Bugzilla::Attachment($self->extra_data);
return $self->{attachment};
}
sub author {
sub author
{
my $self = shift;
$self->{'author'} ||= new Bugzilla::User($self->{'who'});
return $self->{'author'};
$self->{author} ||= new Bugzilla::User($self->{'who'});
return $self->{author};
}
# %$params:
# is_bugmail => format as plaintext (TODO rename to 'plaintext')
# wrap => wrap or not
sub body_full {
sub body_full
{
my ($self, $params) = @_;
$params ||= {};
my $preview = $params->{preview} ? $params->{preview} : 0;
@ -155,28 +159,33 @@ sub body_full {
my $template = Bugzilla->template_inner;
my $body;
my $t = $self->type;
if ($t && $t != CMT_BACKDATED_WORKTIME &&
$t != CMT_WORKTIME)
if ($t && $t != CMT_BACKDATED_WORKTIME && $t != CMT_WORKTIME)
{
$template->process("bug/format_comment.txt.tmpl",
{ comment => $self, %$params }, \$body)
$template->process("bug/format_comment.txt.tmpl", { comment => $self, %$params }, \$body)
|| ThrowTemplateError($template->error());
$body =~ s/^X//;
}
else {
else
{
$body = $self->body($preview);
}
if (!$params->{is_bugmail}) {
if (!$params->{is_bugmail})
{
$body = Bugzilla::Template::quoteUrls($body, $self->bug_id, $self);
}
if ($params->{wrap}) {
if ($params->{wrap})
{
$body = wrap_comment($body);
if (!$preview && !($self->check_length) && !$wo_preview)
{
$params->{preview} = 1;
my $new_body;
$template->process("bug/comment-preview-text.html.tmpl",
{ preview => $self->body_full($params), body => $body, id => $self->id }, \$new_body)
my $vars = {
preview => $self->body_full($params),
body => $body,
id => $self->id,
};
$template->process("bug/comment-preview-text.html.tmpl", $vars, \$new_body)
|| ThrowTemplateError($template->error());
$body = $new_body;
}
@ -187,7 +196,7 @@ sub body_full {
sub check_length
{
my ($self, $test) = @_;
$test ||= $self->{'thetext'};
$test ||= $self->{thetext};
my $line_length = Bugzilla->params->{comment_line_length};
my $length = $test =~ s/([^\n]{$line_length}|\n)/$1/g;
$length = 0 if !$length;
@ -200,7 +209,8 @@ sub check_length
sub set_extra_data { $_[0]->set('extra_data', $_[1]); }
sub set_type {
sub set_type
{
my ($self, $type, $extra_data) = @_;
$self->set('type', $type);
$self->set_extra_data($extra_data);
@ -210,46 +220,49 @@ sub set_type {
# Validators #
##############
sub _check_extra_data {
sub _check_extra_data
{
my ($invocant, $extra_data, $type) = @_;
$type = $invocant->type if ref $invocant;
if ($type == CMT_NORMAL or $type == CMT_POPULAR_VOTES) {
if (defined $extra_data) {
ThrowCodeError('comment_extra_data_not_allowed',
{ type => $type, extra_data => $extra_data });
if ($type == CMT_NORMAL or $type == CMT_POPULAR_VOTES)
{
if (defined $extra_data)
{
ThrowCodeError('comment_extra_data_not_allowed', { type => $type, extra_data => $extra_data });
}
}
else {
if (!defined $extra_data) {
else
{
if (!defined $extra_data)
{
ThrowCodeError('comment_extra_data_required', { type => $type });
}
if ($type == CMT_MOVED_TO) {
if ($type == CMT_MOVED_TO)
{
$extra_data = Bugzilla::User->check($extra_data)->login;
}
elsif ($type == CMT_ATTACHMENT_CREATED
or $type == CMT_ATTACHMENT_UPDATED)
elsif ($type == CMT_ATTACHMENT_CREATED || $type == CMT_ATTACHMENT_UPDATED)
{
my $attachment = Bugzilla::Attachment->check({
id => $extra_data });
my $attachment = Bugzilla::Attachment->check({ id => $extra_data });
$extra_data = $attachment->id;
}
else {
else
{
my $original = $extra_data;
detaint_natural($extra_data)
or ThrowCodeError('comment_extra_data_not_numeric',
{ type => $type, extra_data => $original });
detaint_natural($extra_data) || ThrowCodeError('comment_extra_data_not_numeric',
{ type => $type, extra_data => $original });
}
}
return $extra_data;
}
sub _check_type {
sub _check_type
{
my ($invocant, $type) = @_;
$type ||= CMT_NORMAL;
my $original = $type;
detaint_natural($type)
or ThrowCodeError('comment_type_invalid', { type => $original });
detaint_natural($type) || ThrowCodeError('comment_type_invalid', { type => $original });
return $type;
}
@ -338,8 +351,6 @@ A string, the full text of the comment as it would be displayed to an end-user.
=back
=back
=cut

View File

@ -1,5 +1,4 @@
#!/usr/bin/perl
# Text difference engine
# License: Dual-license GPL 3.0+ or MPL 1.1+
# Contributor(s): Vladimir Koptev <vladimir.koptev@gmail.com>, Vitaliy Filippov <vitalif@mail.ru>

View File

@ -1,5 +1,3 @@
# -*- Mode: perl; indent-tabs-mode: nil -*-
#
# The contents of this file are subject to the Mozilla Public
# License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
@ -53,8 +51,8 @@ use constant DB_COLUMNS => qw(
use constant REQUIRED_CREATE_FIELDS => qw(name query);
use constant VALIDATORS => {
name => \&_check_name,
query => \&_check_query,
name => \&_check_name,
query => \&_check_query,
link_in_footer => \&_check_link_in_footer,
};
@ -64,34 +62,34 @@ use constant UPDATE_COLUMNS => qw(name query);
# Constructor #
###############
sub new {
sub new
{
my $class = shift;
my $param = shift;
my $dbh = Bugzilla->dbh;
my $user;
if (ref $param && !$param->{id}) {
if (ref $param && !$param->{id})
{
$user = $param->{user} || Bugzilla->user;
my $name = $param->{name};
if (!defined $name) {
ThrowCodeError('bad_arg',
{argument => 'name',
function => "${class}::new"});
if (!defined $name)
{
ThrowCodeError('bad_arg', { argument => 'name', function => "${class}::new" });
}
my $condition = 'userid = ? AND '.$dbh->sql_istrcmp('name', '?');
my $user_id = blessed $user ? $user->id : $user;
detaint_natural($user_id)
|| ThrowCodeError('param_must_be_numeric',
{function => $class . '::_init', param => 'user'});
detaint_natural($user_id) || ThrowCodeError('param_must_be_numeric',
{ function => $class . '::_init', param => 'user' });
my @values = ($user_id, $name);
$param = { condition => $condition, values => \@values };
}
unshift @_, $param;
my $self = $class->SUPER::new(@_);
if ($self) {
if ($self)
{
$self->{user} = $user if blessed $user;
# Some DBs (read: Oracle) incorrectly mark the query string as UTF-8
# when it's coming out of the database, even though it has no UTF-8
# characters in it, which prevents Bugzilla::CGI from later reading
@ -101,19 +99,19 @@ sub new {
return $self;
}
sub check {
sub check
{
my $class = shift;
my $search = $class->SUPER::check(@_);
my $user = Bugzilla->user;
return $search if $search->user->id == $user->id;
if (!$search->shared_with_group
or !$user->in_group($search->shared_with_group))
if (!$search->shared_with_group || !$user->in_group($search->shared_with_group))
{
ThrowUserError('missing_query', { queryname => $search->name,
sharer_id => $search->user->id });
ThrowUserError('missing_query', {
queryname => $search->name,
sharer_id => $search->user->id,
});
}
return $search;
}
@ -123,18 +121,21 @@ sub check {
sub _check_link_in_footer { return $_[1] ? 1 : 0; }
sub _check_name {
sub _check_name
{
my ($invocant, $name) = @_;
$name = trim($name);
$name || ThrowUserError("query_name_missing");
$name !~ /[<>&]/ || ThrowUserError("illegal_query_name");
if (length($name) > MAX_FIELD_VALUE_SIZE) {
if (length($name) > MAX_FIELD_VALUE_SIZE)
{
ThrowUserError("query_name_too_long");
}
return $name;
}
sub _check_query {
sub _check_query
{
my ($invocant, $query) = @_;
$query || ThrowUserError("buglist_parameters_required");
my $params = http_decode_query($query);
@ -147,7 +148,8 @@ sub _check_query {
# Database Manipulation #
#########################
sub create {
sub create
{
my $class = shift;
Bugzilla->login(LOGIN_REQUIRED);
my $dbh = Bugzilla->dbh;
@ -160,17 +162,20 @@ sub create {
my $lif = delete $params->{link_in_footer};
my $obj = $class->insert_create_data($params);
if ($lif) {
$dbh->do('INSERT INTO namedqueries_link_in_footer
(user_id, namedquery_id) VALUES (?,?)',
undef, $params->{userid}, $obj->id);
if ($lif)
{
$dbh->do(
'INSERT INTO namedqueries_link_in_footer (user_id, namedquery_id) VALUES (?,?)',
undef, $params->{userid}, $obj->id
);
}
$dbh->bz_commit_transaction();
return $obj;
}
sub preload {
sub preload
{
my ($searches) = @_;
my $dbh = Bugzilla->dbh;
@ -178,23 +183,28 @@ sub preload {
my @query_ids = map { $_->id } @$searches;
my $queries_in_footer = $dbh->selectcol_arrayref(
'SELECT namedquery_id
FROM namedqueries_link_in_footer
WHERE ' . $dbh->sql_in('namedquery_id', \@query_ids) . ' AND user_id = ?',
undef, Bugzilla->user->id);
'SELECT namedquery_id FROM namedqueries_link_in_footer' .
' WHERE ' . $dbh->sql_in('namedquery_id', \@query_ids) . ' AND user_id = ?',
undef, Bugzilla->user->id
);
my %links_in_footer = map { $_ => 1 } @$queries_in_footer;
foreach my $query (@$searches) {
foreach my $query (@$searches)
{
$query->{link_in_footer} = ($links_in_footer{$query->id}) ? 1 : 0;
}
}
sub update {
sub update
{
my $self = shift;
my @r;
if (wantarray) {
if (wantarray)
{
@r = $self->SUPER::update(@_);
} else {
}
else
{
@r = scalar $self->SUPER::update(@_);
}
Bugzilla::Hook::process('savedsearch-post-update', { search => $self });
@ -273,45 +283,46 @@ sub used_in_checkers
return $self->{used_in_checkers};
}
sub link_in_footer {
sub link_in_footer
{
my ($self, $user) = @_;
# We only cache link_in_footer for the current Bugzilla->user.
return $self->{link_in_footer} if exists $self->{link_in_footer} && !$user;
my $user_id = $user ? $user->id : Bugzilla->user->id;
my $link_in_footer = Bugzilla->dbh->selectrow_array(
'SELECT 1 FROM namedqueries_link_in_footer
WHERE namedquery_id = ? AND user_id = ?',
undef, $self->id, $user_id) || 0;
'SELECT 1 FROM namedqueries_link_in_footer WHERE namedquery_id = ? AND user_id = ?',
undef, $self->id, $user_id
) || 0;
$self->{link_in_footer} = $link_in_footer if !$user;
return $link_in_footer;
}
sub shared_with_group {
sub shared_with_group
{
my ($self) = @_;
return $self->{shared_with_group} if exists $self->{shared_with_group};
# Bugzilla only currently supports sharing with one group, even
# though the database backend allows for an infinite number.
my ($group_id) = Bugzilla->dbh->selectrow_array(
'SELECT group_id FROM namedquery_group_map WHERE namedquery_id = ?',
undef, $self->id);
$self->{shared_with_group} = $group_id ? new Bugzilla::Group($group_id)
: undef;
undef, $self->id
);
$self->{shared_with_group} = $group_id ? new Bugzilla::Group($group_id) : undef;
return $self->{shared_with_group};
}
sub shared_with_users {
sub shared_with_users
{
my $self = shift;
my $dbh = Bugzilla->dbh;
if (!exists $self->{shared_with_users}) {
$self->{shared_with_users} =
$dbh->selectrow_array('SELECT COUNT(*)
FROM namedqueries_link_in_footer
INNER JOIN namedqueries
ON namedquery_id = id
WHERE namedquery_id = ?
AND user_id != userid',
undef, $self->id);
if (!exists $self->{shared_with_users})
{
$self->{shared_with_users} = $dbh->selectrow_array(
'SELECT COUNT(*) FROM namedqueries_link_in_footer'.
' INNER JOIN namedqueries ON namedquery_id = id'.
' WHERE namedquery_id = ? AND user_id != userid',
undef, $self->id
);
}
return $self->{shared_with_users};
}
@ -320,10 +331,11 @@ sub shared_with_users {
# Simple Accessors #
####################
sub query { $_[0]->{query}; }
sub query { $_[0]->{query} }
sub userid { $_[0]->{userid} }
sub user {
sub user
{
my ($self) = @_;
return $self->{user} if defined $self->{user};
return Bugzilla->user if Bugzilla->user->id == $self->{userid};
@ -335,8 +347,8 @@ sub user {
# Mutators #
############
sub set_name { $_[0]->set('name', $_[1]); }
sub set_query { $_[0]->set('query', $_[1]); }
sub set_name { $_[0]->set('name', $_[1]); }
sub set_query { $_[0]->set('query', $_[1]); }
sub set_link_in_footer
{
@ -344,11 +356,17 @@ sub set_link_in_footer
my ($link) = @_;
if ($link && !$self->link_in_footer)
{
Bugzilla->dbh->do('INSERT INTO namedqueries_link_in_footer (namedquery_id, user_id) VALUES (?, ?)', undef, $self->id, Bugzilla->user->id);
Bugzilla->dbh->do(
'INSERT INTO namedqueries_link_in_footer (namedquery_id, user_id) VALUES (?, ?)',
undef, $self->id, Bugzilla->user->id
);
}
elsif (!$link && $self->link_in_footer)
{
Bugzilla->dbh->do('DELETE FROM namedqueries_link_in_footer WHERE namedquery_id=? AND user_id=?', undef, $self->id, Bugzilla->user->id);
Bugzilla->dbh->do(
'DELETE FROM namedqueries_link_in_footer WHERE namedquery_id=? AND user_id=?',
undef, $self->id, Bugzilla->user->id
);
}
delete $self->{link_in_footer};
}
@ -358,8 +376,7 @@ sub set_shared_with_group
my $self = shift;
my ($group, $force_link_in_footer) = @_;
my $user = Bugzilla->user;
# Don't allow the user to share queries with groups he's not
# allowed to.
# Don't allow the user to share queries with groups he's not allowed to.
$group = undef if $group && !grep { $_ eq $group->id } @{$user->queryshare_groups};
# Don't remove namedqueries_link_in_footer entries for users
# subscribing to the shared query. The idea is that they will