Saved.pm 10.3 KB
Newer Older
1 2 3
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
#
5 6
# This Source Code Form is "Incompatible With Secondary Licenses", as
# defined by the Mozilla Public License, v. 2.0.
7 8 9

package Bugzilla::Search::Saved;

10 11
use 5.10.1;
use strict;
12
use warnings;
13

14
use parent qw(Bugzilla::Object);
15 16 17 18

use Bugzilla::CGI;
use Bugzilla::Constants;
use Bugzilla::Group;
19
use Bugzilla::Error;
20 21
use Bugzilla::Search qw(IsValidQueryType);
use Bugzilla::User;
22
use Bugzilla::Util;
23

24 25
use Scalar::Util qw(blessed);

26 27 28 29 30
#############
# Constants #
#############

use constant DB_TABLE => 'namedqueries';
31

32 33 34 35
# Do not track buglists saved by users.
use constant AUDIT_CREATES => 0;
use constant AUDIT_UPDATES => 0;
use constant AUDIT_REMOVES => 0;
36 37

use constant DB_COLUMNS => qw(
38 39 40 41
  id
  userid
  name
  query
42 43
);

44
use constant VALIDATORS => {
45 46 47
  name           => \&_check_name,
  query          => \&_check_query,
  link_in_footer => \&_check_link_in_footer,
48 49
};

50
use constant UPDATE_COLUMNS => qw(name query);
51

52 53 54 55 56
###############
# Constructor #
###############

sub new {
57 58 59 60 61 62 63 64 65 66
  my $class = shift;
  my $param = shift;
  my $dbh   = Bugzilla->dbh;

  my $user;
  if (ref $param) {
    $user = $param->{user} || Bugzilla->user;
    my $name = $param->{name};
    if (!defined $name) {
      ThrowCodeError('bad_arg', {argument => 'name', function => "${class}::new"});
67
    }
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
    my $condition = 'userid = ? AND name = ?';
    my $user_id = blessed $user ? $user->id : $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) {
    $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
    # it correctly.
    utf8::downgrade($self->{query}) if utf8::is_utf8($self->{query});
  }
  return $self;
89 90 91
}

sub check {
92 93 94 95 96 97 98 99 100 101 102 103
  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))
  {
    ThrowUserError('missing_query',
      {name => $search->name, sharer_id => $search->user->id});
  }

  return $search;
104 105
}

106 107 108 109 110 111 112
##############
# Validators #
##############

sub _check_link_in_footer { return $_[1] ? 1 : 0; }

sub _check_name {
113 114 115 116 117 118 119 120
  my ($invocant, $name) = @_;
  $name = trim($name);
  $name || ThrowUserError("query_name_missing");
  $name !~ /[<>&]/ || ThrowUserError("illegal_query_name");
  if (length($name) > MAX_LEN_QUERY_NAME) {
    ThrowUserError("query_name_too_long");
  }
  return $name;
121 122 123
}

sub _check_query {
124 125 126 127 128 129 130 131
  my ($invocant, $query) = @_;
  $query || ThrowUserError("buglist_parameters_required");
  my $cgi = new Bugzilla::CGI($query);
  $cgi->clean_search_url;

  # Don't store the query name as a parameter.
  $cgi->delete('known_name');
  return $cgi->query_string;
132 133 134 135 136 137 138
}

#########################
# Database Manipulation #
#########################

sub create {
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
  my $class = shift;
  Bugzilla->login(LOGIN_REQUIRED);
  my $dbh = Bugzilla->dbh;
  $class->check_required_create_fields(@_);
  $dbh->bz_start_transaction();
  my $params = $class->run_create_validators(@_);

  # Right now you can only create a Saved Search for the current user.
  $params->{userid} = Bugzilla->user->id;

  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
    );
  }
  $dbh->bz_commit_transaction();

  return $obj;
161 162
}

163
sub rename_field_value {
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
  my ($class, $field, $old_value, $new_value) = @_;

  my $old     = url_quote($old_value);
  my $new     = url_quote($new_value);
  my $old_sql = $old;
  $old_sql =~ s/([_\%])/\\$1/g;

  my $table    = $class->DB_TABLE;
  my $id_field = $class->ID_FIELD;

  my $dbh = Bugzilla->dbh;
  $dbh->bz_start_transaction();

  my %queries = @{
    $dbh->selectcol_arrayref(
      "SELECT $id_field, query FROM $table WHERE query LIKE ?",
      {Columns => [1, 2]},
      "\%$old_sql\%"
    )
  };
  foreach my $id (keys %queries) {
    my $query = $queries{$id};
    $query =~ s/\b$field=\Q$old\E\b/$field=$new/gi;

    # Fix boolean charts.
    while ($query =~ /\bfield(\d+-\d+-\d+)=\Q$field\E\b/gi) {
      my $chart_id = $1;

      # Note that this won't handle lists or substrings inside of
      # boolean charts. Users will have to fix those themselves.
      $query =~ s/\bvalue\Q$chart_id\E=\Q$old\E\b/value$chart_id=$new/i;
195
    }
196 197 198
    $dbh->do("UPDATE $table SET query = ? WHERE $id_field = ?", undef, $query, $id);
    Bugzilla->memcached->clear({table => $table, id => $id});
  }
199

200
  $dbh->bz_commit_transaction();
201 202
}

203
sub preload {
204 205
  my ($searches) = @_;
  my $dbh = Bugzilla->dbh;
206

207
  return unless scalar @$searches;
208

209 210 211
  my @query_ids = map { $_->id } @$searches;
  my $queries_in_footer = $dbh->selectcol_arrayref(
    'SELECT namedquery_id
212 213
           FROM namedqueries_link_in_footer
          WHERE ' . $dbh->sql_in('namedquery_id', \@query_ids) . ' AND user_id = ?',
214 215
    undef, Bugzilla->user->id
  );
216

217 218 219 220
  my %links_in_footer = map { $_ => 1 } @$queries_in_footer;
  foreach my $query (@$searches) {
    $query->{link_in_footer} = ($links_in_footer{$query->id}) ? 1 : 0;
  }
221
}
222 223 224 225 226
#####################
# Complex Accessors #
#####################

sub edit_link {
227 228 229 230 231 232 233 234 235
  my ($self) = @_;
  return $self->{edit_link} if defined $self->{edit_link};
  my $cgi = new Bugzilla::CGI($self->url);
  if (!$cgi->param('query_type') || !IsValidQueryType($cgi->param('query_type')))
  {
    $cgi->param('query_type', 'advanced');
  }
  $self->{edit_link} = $cgi->canonicalise_query;
  return $self->{edit_link};
236 237 238
}

sub used_in_whine {
239 240 241 242
  my ($self) = @_;
  return $self->{used_in_whine} if exists $self->{used_in_whine};
  ($self->{used_in_whine}) = Bugzilla->dbh->selectrow_array(
    'SELECT 1 FROM whine_events INNER JOIN whine_queries
243
                       ON whine_events.id = whine_queries.eventid
244 245 246 247
          WHERE whine_events.owner_userid = ? AND query_name = ?', undef,
    $self->{userid}, $self->name
  ) || 0;
  return $self->{used_in_whine};
248 249 250
}

sub link_in_footer {
251 252 253 254 255 256 257 258 259 260 261
  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;
  $self->{link_in_footer} = $link_in_footer if !$user;
  return $link_in_footer;
262 263 264
}

sub shared_with_group {
265 266 267 268 269 270 271 272 273 274 275
  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;
  return $self->{shared_with_group};
276 277
}

278
sub shared_with_users {
279 280
  my $self = shift;
  my $dbh  = Bugzilla->dbh;
281

282 283 284
  if (!exists $self->{shared_with_users}) {
    $self->{shared_with_users} = $dbh->selectrow_array(
      'SELECT COUNT(*)
285 286 287 288
                                   FROM namedqueries_link_in_footer
                             INNER JOIN namedqueries
                                     ON namedquery_id = id
                                  WHERE namedquery_id = ?
289 290 291 292
                                    AND user_id != userid', undef, $self->id
    );
  }
  return $self->{shared_with_users};
293 294
}

295 296 297 298
####################
# Simple Accessors #
####################

299
sub url { return $_[0]->{'query'}; }
300 301

sub user {
302 303 304
  my ($self) = @_;
  return $self->{user}
    ||= Bugzilla::User->new({id => $self->{userid}, cache => 1});
305 306
}

307 308 309 310
############
# Mutators #
############

311 312
sub set_name { $_[0]->set('name',  $_[1]); }
sub set_url  { $_[0]->set('query', $_[1]); }
313

314 315 316 317 318 319
1;

__END__

=head1 NAME

320
Bugzilla::Search::Saved - A saved search
321 322 323 324 325 326 327 328 329 330

=head1 SYNOPSIS

 use Bugzilla::Search::Saved;

 my $query = new Bugzilla::Search::Saved($query_id);

 my $edit_link  = $query->edit_link;
 my $search_url = $query->url;
 my $owner      = $query->user;
331
 my $num_subscribers = $query->shared_with_users;
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349

=head1 DESCRIPTION

This module exists to represent a L<Bugzilla::Search> that has been
saved to the database.

This is an implementation of L<Bugzilla::Object>, and so has all the
same methods available as L<Bugzilla::Object>, in addition to what is
documented below.

=head1 METHODS

=head2 Constructors and Database Manipulation

=over

=item C<new>

350 351
Takes either an id, or the named parameters C<user> and C<name>.
C<user> can be either a L<Bugzilla::User> object or a numeric user id.
352 353 354

See also: L<Bugzilla::Object/new>.

355 356 357 358 359 360
=item C<preload>

Sets C<link_in_footer> for all given saved searches at once, for the
currently logged in user. This is much faster than calling this method
for each saved search individually.

361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
=back


=head2 Accessors

These return data about the object, without modifying the object.

=over

=item C<edit_link>

A url with which you can edit the search.

=item C<url>

The CGI parameters for the search, as a string.

=item C<link_in_footer>

Whether or not this search should be displayed in the footer for the
I<current user> (not the owner of the search, but the person actually
using Bugzilla right now).

384
=item C<type>
385

386
The numeric id of the type of search this is (from L<Bugzilla::Constants>).
387 388 389 390 391 392

=item C<shared_with_group>

The L<Bugzilla::Group> that this search is shared with. C<undef> if
this search isn't shared.

393 394 395 396 397
=item C<shared_with_users>

Returns how many users (besides the author of the saved search) are
using the saved search, i.e. have it displayed in their footer.

398
=back
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416

=head1 B<Methods in need of POD>

=over

=item create

=item set_name

=item set_url

=item rename_field_value

=item user

=item used_in_whine

=back