Clause.pm 3.49 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

package Bugzilla::Search::Clause;
9 10

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

use Bugzilla::Error;
15
use Bugzilla::Search::Condition qw(condition);
16
use Bugzilla::Util qw(trick_taint);
17 18

sub new {
19 20 21 22 23 24 25 26
  my ($class, $joiner) = @_;
  if ($joiner and $joiner ne 'OR' and $joiner ne 'AND') {
    ThrowCodeError('search_invalid_joiner', {joiner => $joiner});
  }

  # This will go into SQL directly so needs to be untainted.
  trick_taint($joiner) if $joiner;
  bless {joiner => $joiner || 'AND'}, $class;
27 28 29
}

sub children {
30 31 32
  my ($self) = @_;
  $self->{children} ||= [];
  return $self->{children};
33 34
}

35
sub update_search_args {
36 37 38
  my ($self, $search_args) = @_;

  # abstract
39 40
}

41 42
sub joiner { return $_[0]->{joiner} }

43
sub has_translated_conditions {
44 45 46 47 48 49 50 51 52 53
  my ($self) = @_;
  my $children = $self->children;
  return 1
    if grep { $_->isa('Bugzilla::Search::Condition') && $_->translated }
    @$children;
  foreach my $child (@$children) {
    next if $child->isa('Bugzilla::Search::Condition');
    return 1 if $child->has_translated_conditions;
  }
  return 0;
54 55 56
}

sub add {
57 58 59 60 61 62 63 64 65 66 67 68 69
  my $self     = shift;
  my $children = $self->children;
  if (@_ == 3) {
    push(@$children, condition(@_));
    return;
  }

  my ($child) = @_;
  return if !defined $child;
  $child->isa(__PACKAGE__)
    || $child->isa('Bugzilla::Search::Condition')
    || die 'child not the right type: ' . $child;
  push(@{$self->children}, $child);
70 71 72
}

sub negate {
73 74 75 76 77
  my ($self, $value) = @_;
  if (@_ == 2) {
    $self->{negate} = $value ? 1 : 0;
  }
  return $self->{negate};
78 79 80
}

sub walk_conditions {
81 82 83 84 85 86 87
  my ($self, $callback) = @_;
  foreach my $child (@{$self->children}) {
    if ($child->isa('Bugzilla::Search::Condition')) {
      $callback->($self, $child);
    }
    else {
      $child->walk_conditions($callback);
88
    }
89
  }
90 91 92
}

sub as_string {
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
  my ($self) = @_;
  if (!$self->{sql}) {
    my @strings;
    foreach my $child (@{$self->children}) {
      next if $child->isa(__PACKAGE__) && !$child->has_translated_conditions;
      next if $child->isa('Bugzilla::Search::Condition') && !$child->translated;

      my $string = $child->as_string;
      next unless $string;
      if ($self->joiner eq 'AND') {
        $string = "( $string )" if $string =~ /OR/;
      }
      else {
        $string = "( $string )" if $string =~ /AND/;
      }
      push(@strings, $string);
109
    }
110 111 112 113 114 115

    my $sql = join(' ' . $self->joiner . ' ', @strings);
    $sql = "NOT( $sql )" if $sql && $self->negate;
    $self->{sql} = $sql;
  }
  return $self->{sql};
116 117
}

118 119 120
# Search.pm converts URL parameters to Clause objects. This helps do the
# reverse.
sub as_params {
121 122 123 124 125 126 127 128 129 130 131 132
  my ($self) = @_;
  my @params;
  foreach my $child (@{$self->children}) {
    if ($child->isa(__PACKAGE__)) {
      my %open_paren = (f => 'OP', n => scalar $child->negate, j => $child->joiner);
      push(@params, \%open_paren);
      push(@params, $child->as_params);
      my %close_paren = (f => 'CP');
      push(@params, \%close_paren);
    }
    else {
      push(@params, $child->as_params);
133
    }
134 135
  }
  return @params;
136
}
137

138
1;
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162

=head1 B<Methods in need of POD>

=over

=item has_translated_conditions

=item as_string

=item add

=item children

=item negate

=item update_search_args

=item walk_conditions

=item joiner

=item as_params

=back