This commit was generated by cvs2svn to compensate for changes in r10640,
[freeside.git] / rt / lib / RT / Interface / REST.pm
1 # BEGIN BPS TAGGED BLOCK {{{
2 #
3 # COPYRIGHT:
4 #
5 # This software is Copyright (c) 1996-2011 Best Practical Solutions, LLC
6 #                                          <sales@bestpractical.com>
7 #
8 # (Except where explicitly superseded by other copyright notices)
9 #
10 #
11 # LICENSE:
12 #
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
16 # from www.gnu.org.
17 #
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.
22 #
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.
28 #
29 #
30 # CONTRIBUTION SUBMISSION POLICY:
31 #
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.)
37 #
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.
46 #
47 # END BPS TAGGED BLOCK }}}
48
49 # lib/RT/Interface/REST.pm
50 #
51
52 package RT::Interface::REST;
53 use strict;
54 use RT;
55
56 BEGIN {
57     use base 'Exporter';
58     use vars qw($VERSION @EXPORT);
59
60     $VERSION = do { my @r = (q$Revision: 1.1.1.9 $ =~ /\d+/g); sprintf "%d."."%02d"x$#r, @r };
61
62     @EXPORT = qw(expand_list form_parse form_compose vpush vsplit);
63 }
64
65 sub custom_field_spec {
66     my $self    = shift;
67     my $capture = shift;
68
69     my $CF_char = '[\sa-z0-9_ :()/-]';
70     my $CF_name = $CF_char . '+';
71     $CF_name = '(' . $CF_name . ')' if $capture;
72
73     my $new_style = 'CF\.\{'.$CF_name.'\}';
74     my $old_style = 'C(?:ustom)?F(?:ield)?-'.$CF_name;
75
76     return '(?i:' . join('|', $new_style, $old_style) . ')';
77 }
78
79 sub field_spec {
80     my $self    = shift;
81     my $capture = shift;
82
83     my $field = '[a-z][a-z0-9_-]*';
84     $field = '(' . $field . ')' if $capture;
85
86     my $custom_field = __PACKAGE__->custom_field_spec($capture);
87
88     return '(?i:' . join('|', $field, $custom_field) . ')';
89 }
90
91 # WARN: this code is duplicated in bin/rt.in,
92 # change both functions at once
93 sub expand_list {
94     my ($list) = @_;
95
96     my @elts;
97     foreach (split /\s*,\s*/, $list) {
98         push @elts, /^(\d+)-(\d+)$/? ($1..$2): $_;
99     }
100
101     return map $_->[0], # schwartzian transform
102         sort {
103             defined $a->[1] && defined $b->[1]?
104                 # both numbers
105                 $a->[1] <=> $b->[1]
106                 :!defined $a->[1] && !defined $b->[1]?
107                     # both letters
108                     $a->[2] cmp $b->[2]
109                     # mix, number must be first
110                     :defined $a->[1]? -1: 1
111         }
112         map [ $_, (defined( /^(\d+)$/ )? $1: undef), lc($_) ],
113         @elts;
114 }
115
116 # Returns a reference to an array of parsed forms.
117 sub form_parse {
118     my $state = 0;
119     my @forms = ();
120     my @lines = split /\n/, $_[0];
121     my ($c, $o, $k, $e) = ("", [], {}, "");
122     my $field = __PACKAGE__->field_spec;
123
124     LINE:
125     while (@lines) {
126         my $line = shift @lines;
127
128         next LINE if $line eq '';
129
130         if ($line eq '--') {
131             # We reached the end of one form. We'll ignore it if it was
132             # empty, and store it otherwise, errors and all.
133             if ($e || $c || @$o) {
134                 push @forms, [ $c, $o, $k, $e ];
135                 $c = ""; $o = []; $k = {}; $e = "";
136             }
137             $state = 0;
138         }
139         elsif ($state != -1) {
140             if ($state == 0 && $line =~ /^#/) {
141                 # Read an optional block of comments (only) at the start
142                 # of the form.
143                 $state = 1;
144                 $c = $line;
145                 while (@lines && $lines[0] =~ /^#/) {
146                     $c .= "\n".shift @lines;
147                 }
148                 $c .= "\n";
149             }
150             elsif ($state <= 1 && $line =~ /^($field):(?:\s+(.*))?$/i) {
151                 # Read a field: value specification.
152                 my $f  = $1;
153                 my @v  = ($2);
154                 $v[0] = '' unless defined $v[0];
155
156                 # Read continuation lines, if any.
157                 while (@lines && ($lines[0] eq '' || $lines[0] =~ /^\s+/)) {
158                     push @v, shift @lines;
159                 }
160                 pop @v while (@v && $v[-1] eq '');
161
162                 # Strip longest common leading indent from text.
163                 my ($ws, $ls) = ("");
164                 foreach $ls (map {/^(\s+)/} @v[1..$#v]) {
165                     $ws = $ls if (!$ws || length($ls) < length($ws));
166                 }
167                 s/^$ws// foreach @v;
168
169                 shift @v while (@v && $v[0] eq '');
170
171                 push(@$o, $f) unless exists $k->{$f};
172                 vpush($k, $f, join("\n", @v));
173
174                 $state = 1;
175             }
176             elsif ($line =~ /^#/) {
177                 # We've found a syntax error, so we'll reconstruct the
178                 # form parsed thus far, and add an error marker. (>>)
179                 $state = -1;
180                 $e = form_compose([[ "", $o, $k, "" ]]);
181                 $e.= $line =~ /^>>/ ? "$line\n" : ">> $line\n";
182             }
183         }
184         else {
185             # We saw a syntax error earlier, so we'll accumulate the
186             # contents of this form until the end.
187             $e .= "$line\n";
188         }
189     }
190     push(@forms, [ $c, $o, $k, $e ]) if ($e || $c || @$o);
191
192     my $l;
193     foreach $l (keys %$k) {
194         $k->{$l} = vsplit($k->{$l}) if (ref $k->{$l} eq 'ARRAY');
195     }
196
197     return \@forms;
198 }
199
200 # Returns text representing a set of forms.
201 sub form_compose {
202     my ($forms) = @_;
203     my (@text, $form);
204
205     foreach $form (@$forms) {
206         my ($c, $o, $k, $e) = @$form;
207         my $text = "";
208
209         if ($c) {
210             $c =~ s/\n*$/\n/;
211             $text = "$c\n";
212         }
213         if ($e) {
214             $text .= $e;
215         }
216         elsif ($o) {
217             my (@lines, $key);
218
219             foreach $key (@$o) {
220                 my ($line, $sp, $v);
221                 my @values = (ref $k->{$key} eq 'ARRAY') ?
222                                @{ $k->{$key} } :
223                                   $k->{$key};
224
225                 $sp = " "x(length("$key: "));
226                 $sp = " "x4 if length($sp) > 16;
227
228                 foreach $v (@values) {
229                     $v = '' unless defined $v;
230                     if ( $v =~ /\n/) {
231                         $v =~ s/^/$sp/gm;
232                         $v =~ s/^$sp//;
233
234                         if ($line) {
235                             push @lines, "$line\n\n";
236                             $line = "";
237                         }
238                         elsif (@lines && $lines[-1] !~ /\n\n$/) {
239                             $lines[-1] .= "\n";
240                         }
241                         push @lines, "$key: $v\n\n";
242                     }
243                     elsif ($line &&
244                            length($line)+length($v)-rindex($line, "\n") >= 70)
245                     {
246                         $line .= ",\n$sp$v";
247                     }
248                     else {
249                         $line = $line ? "$line, $v" : "$key: $v";
250                     }
251                 }
252
253                 $line = "$key:" unless @values;
254                 if ($line) {
255                     if ($line =~ /\n/) {
256                         if (@lines && $lines[-1] !~ /\n\n$/) {
257                             $lines[-1] .= "\n";
258                         }
259                         $line .= "\n";
260                     }
261                     push @lines, "$line\n";
262                 }
263             }
264
265             $text .= join "", @lines;
266         }
267         else {
268             chomp $text;
269         }
270         push @text, $text;
271     }
272
273     return join "\n--\n\n", @text;
274 }
275
276 # Add a value to a (possibly multi-valued) hash key.
277 sub vpush {
278     my ($hash, $key, $val) = @_;
279     my @val = ref $val eq 'ARRAY' ? @$val : $val;
280
281     if (exists $hash->{$key}) {
282         unless (ref $hash->{$key} eq 'ARRAY') {
283             my @v = $hash->{$key} ne '' ? $hash->{$key} : ();
284             $hash->{$key} = \@v;
285         }
286         push @{ $hash->{$key} }, @val;
287     }
288     else {
289         $hash->{$key} = $val;
290     }
291 }
292
293 # "Normalise" a hash key that's known to be multi-valued.
294 sub vsplit {
295     my ($val) = @_;
296     my ($line, $word, @words);
297
298     foreach $line (map {split /\n/} (ref $val eq 'ARRAY') ? @$val : ($val||''))
299     {
300         # XXX: This should become a real parser, ? la Text::ParseWords.
301         $line =~ s/^\s+//;
302         $line =~ s/\s+$//;
303         push @words, split /\s*,\s*/, $line;
304     }
305
306     return \@words;
307 }
308
309 eval "require RT::Interface::REST_Vendor";
310 if ($@ && $@ !~ qr{^Can't locate RT/Interface/REST_Vendor.pm}) {
311     die $@;
312 };
313
314 eval "require RT::Interface::REST_Local";
315 if ($@ && $@ !~ qr{^Can't locate RT/Interface/REST_Local.pm}) {
316     die $@;
317 };
318
319 1;
320
321 =head1 NAME
322
323   RT::Interface::REST - helper functions for the REST interface.
324
325 =head1 SYNOPSIS
326
327   Only the REST should use this module.