1 # BEGIN BPS TAGGED BLOCK {{{
5 # This software is Copyright (c) 1996-2013 Best Practical Solutions, LLC
6 # <sales@bestpractical.com>
8 # (Except where explicitly superseded by other copyright notices)
13 # This work is made available to you under the terms of Version 2 of
14 # the GNU General Public License. A copy of that license should have
15 # been provided with this software, but in any event can be snarfed
18 # This work is distributed in the hope that it will be useful, but
19 # WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 # General Public License for more details.
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 # 02110-1301 or visit their web page on the internet at
27 # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
30 # CONTRIBUTION SUBMISSION POLICY:
32 # (The following paragraph is not intended to limit the rights granted
33 # to you to modify and distribute this software under the terms of
34 # the GNU General Public License and is only of importance to you if
35 # you choose to contribute your changes and enhancements to the
36 # community by submitting them to Best Practical Solutions, LLC.)
38 # By intentionally submitting any modifications, corrections or
39 # derivatives to this work, or any other work intended for use with
40 # Request Tracker, to Best Practical Solutions, LLC, you confirm that
41 # you are the copyright holder for those contributions and you grant
42 # Best Practical Solutions, LLC a nonexclusive, worldwide, irrevocable,
43 # royalty-free, perpetual, license to use, copy, create derivative
44 # works based on those contributions, and sublicense and distribute
45 # those contributions and any derivatives thereof.
47 # END BPS TAGGED BLOCK }}}
49 package RT::Interface::REST;
55 our @EXPORT = qw(expand_list form_parse form_compose vpush vsplit);
57 sub custom_field_spec {
61 my $CF_name = '[^,]+';
62 $CF_name = '(' . $CF_name . ')' if $capture;
64 my $new_style = 'CF\.\{'.$CF_name.'\}';
65 my $old_style = 'C(?:ustom)?F(?:ield)?-'.$CF_name;
67 return '(?i:' . join('|', $new_style, $old_style) . ')';
74 my $field = '[a-z][a-z0-9_-]*';
75 $field = '(' . $field . ')' if $capture;
77 my $custom_field = __PACKAGE__->custom_field_spec($capture);
79 return '(?i:' . join('|', $field, $custom_field) . ')';
82 # WARN: this code is duplicated in bin/rt.in,
83 # change both functions at once
88 foreach (split /\s*,\s*/, $list) {
89 push @elts, /^(\d+)-(\d+)$/? ($1..$2): $_;
92 return map $_->[0], # schwartzian transform
94 defined $a->[1] && defined $b->[1]?
97 :!defined $a->[1] && !defined $b->[1]?
100 # mix, number must be first
101 :defined $a->[1]? -1: 1
103 map [ $_, (defined( /^(\d+)$/ )? $1: undef), lc($_) ],
107 # Returns a reference to an array of parsed forms.
111 my @lines = split /\n/, $_[0];
112 my ($c, $o, $k, $e) = ("", [], {}, "");
113 my $field = __PACKAGE__->field_spec;
117 my $line = shift @lines;
119 next LINE if $line eq '';
122 # We reached the end of one form. We'll ignore it if it was
123 # empty, and store it otherwise, errors and all.
124 if ($e || $c || @$o) {
125 push @forms, [ $c, $o, $k, $e ];
126 $c = ""; $o = []; $k = {}; $e = "";
130 elsif ($state != -1) {
131 if ($state == 0 && $line =~ /^#/) {
132 # Read an optional block of comments (only) at the start
136 while (@lines && $lines[0] =~ /^#/) {
137 $c .= "\n".shift @lines;
141 elsif ($state <= 1 && $line =~ /^($field):(?:\s+(.*))?$/i) {
142 # Read a field: value specification.
145 $v[0] = '' unless defined $v[0];
147 # Read continuation lines, if any.
148 while (@lines && ($lines[0] eq '' || $lines[0] =~ /^\s+/)) {
149 push @v, shift @lines;
151 pop @v while (@v && $v[-1] eq '');
153 # Strip longest common leading indent from text.
155 foreach my $ls (map {/^(\s+)/} @v[1..$#v]) {
156 $ws = $ls if (!$ws || length($ls) < length($ws));
160 shift @v while (@v && $v[0] eq '');
162 push(@$o, $f) unless exists $k->{$f};
163 vpush($k, $f, join("\n", @v));
167 elsif ($line =~ /^#/) {
168 # We've found a syntax error, so we'll reconstruct the
169 # form parsed thus far, and add an error marker. (>>)
171 $e = form_compose([[ "", $o, $k, "" ]]);
172 $e.= $line =~ /^>>/ ? "$line\n" : ">> $line\n";
176 # We saw a syntax error earlier, so we'll accumulate the
177 # contents of this form until the end.
181 push(@forms, [ $c, $o, $k, $e ]) if ($e || $c || @$o);
183 foreach my $l (keys %$k) {
184 $k->{$l} = vsplit($k->{$l}) if (ref $k->{$l} eq 'ARRAY');
190 # Returns text representing a set of forms.
195 foreach my $form (@$forms) {
196 my ($c, $o, $k, $e) = @$form;
209 foreach my $key (@$o) {
211 my @values = (ref $k->{$key} eq 'ARRAY') ?
215 $sp = " "x(length("$key: "));
216 $sp = " "x4 if length($sp) > 16;
218 foreach my $v (@values) {
219 $v = '' unless defined $v;
225 push @lines, "$line\n\n";
228 elsif (@lines && $lines[-1] !~ /\n\n$/) {
231 push @lines, "$key: $v\n\n";
234 length($line)+length($v)-rindex($line, "\n") >= 70)
239 $line = $line ? "$line, $v" : "$key: $v";
243 $line = "$key:" unless @values;
246 if (@lines && $lines[-1] !~ /\n\n$/) {
251 push @lines, "$line\n";
255 $text .= join "", @lines;
263 return join "\n--\n\n", @text;
266 # Add a value to a (possibly multi-valued) hash key.
268 my ($hash, $key, $val) = @_;
269 my @val = ref $val eq 'ARRAY' ? @$val : $val;
271 if (exists $hash->{$key}) {
272 unless (ref $hash->{$key} eq 'ARRAY') {
273 my @v = $hash->{$key} ne '' ? $hash->{$key} : ();
276 push @{ $hash->{$key} }, @val;
279 $hash->{$key} = $val;
283 # "Normalise" a hash key that's known to be multi-valued.
288 foreach my $line (map {split /\n/} (ref $val eq 'ARRAY') ? @$val : ($val||''))
290 # XXX: This should become a real parser, ? la Text::ParseWords.
293 push @words, split /\s*,\s*/, $line;
299 RT::Base->_ImportOverlays();
305 RT::Interface::REST - helper functions for the REST interface.
309 Only the REST should use this module.