1 # BEGIN BPS TAGGED BLOCK {{{
5 # This software is Copyright (c) 1996-2009 Best Practical Solutions, LLC
6 # <jesse@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 }}}
48 # lib/RT/Interface/REST.pm
51 package RT::Interface::REST;
57 use vars qw($VERSION @ISA @EXPORT);
59 $VERSION = do { my @r = (q$Revision: 1.1.1.7 $ =~ /\d+/g); sprintf "%d."."%02d"x$#r, @r };
62 @EXPORT = qw(expand_list form_parse form_compose vpush vsplit);
65 my $field = '(?i:[a-z][a-z0-9_-]*|C(?:ustom)?F(?:ield)?-(?:[a-z0-9_ -]|\s)+)';
67 # WARN: this code is duplicated in bin/rt.in,
68 # change both functions at once
73 foreach (split /,/, $list) {
74 push @elts, /^(\d+)-(\d+)$/? ($1..$2): $_;
77 return map $_->[0], # schwartzian transform
79 defined $a->[1] && defined $b->[1]?
82 :!defined $a->[1] && !defined $b->[1]?
85 # mix, number must be first
86 :defined $a->[1]? -1: 1
88 map [ $_, (defined( /^(\d+)$/ )? $1: undef), lc($_) ],
92 # Returns a reference to an array of parsed forms.
96 my @lines = split /\n/, $_[0];
97 my ($c, $o, $k, $e) = ("", [], {}, "");
101 my $line = shift @lines;
103 next LINE if $line eq '';
106 # We reached the end of one form. We'll ignore it if it was
107 # empty, and store it otherwise, errors and all.
108 if ($e || $c || @$o) {
109 push @forms, [ $c, $o, $k, $e ];
110 $c = ""; $o = []; $k = {}; $e = "";
114 elsif ($state != -1) {
115 if ($state == 0 && $line =~ /^#/) {
116 # Read an optional block of comments (only) at the start
120 while (@lines && $lines[0] =~ /^#/) {
121 $c .= "\n".shift @lines;
125 elsif ($state <= 1 && $line =~ /^($field):(?:\s+(.*))?$/i) {
126 # Read a field: value specification.
129 $v[0] = '' unless defined $v[0];
131 # Read continuation lines, if any.
132 while (@lines && ($lines[0] eq '' || $lines[0] =~ /^\s+/)) {
133 push @v, shift @lines;
135 pop @v while (@v && $v[-1] eq '');
137 # Strip longest common leading indent from text.
138 my ($ws, $ls) = ("");
139 foreach $ls (map {/^(\s+)/} @v[1..$#v]) {
140 $ws = $ls if (!$ws || length($ls) < length($ws));
144 shift @v while (@v && $v[0] eq '');
146 push(@$o, $f) unless exists $k->{$f};
147 vpush($k, $f, join("\n", @v));
151 elsif ($line !~ /^#/) {
152 # We've found a syntax error, so we'll reconstruct the
153 # form parsed thus far, and add an error marker. (>>)
155 $e = form_compose([[ "", $o, $k, "" ]]);
156 $e.= $line =~ /^>>/ ? "$line\n" : ">> $line\n";
160 # We saw a syntax error earlier, so we'll accumulate the
161 # contents of this form until the end.
165 push(@forms, [ $c, $o, $k, $e ]) if ($e || $c || @$o);
168 foreach $l (keys %$k) {
169 $k->{$l} = vsplit($k->{$l}) if (ref $k->{$l} eq 'ARRAY');
175 # Returns text representing a set of forms.
180 foreach $form (@$forms) {
181 my ($c, $o, $k, $e) = @$form;
196 my @values = (ref $k->{$key} eq 'ARRAY') ?
200 $sp = " "x(length("$key: "));
201 $sp = " "x4 if length($sp) > 16;
203 foreach $v (@values) {
209 push @lines, "$line\n\n";
212 elsif (@lines && $lines[-1] !~ /\n\n$/) {
215 push @lines, "$key: $v\n\n";
218 length($line)+length($v)-rindex($line, "\n") >= 70)
223 $line = $line ? "$line, $v" : "$key: $v";
227 $line = "$key:" unless @values;
230 if (@lines && $lines[-1] !~ /\n\n$/) {
235 push @lines, "$line\n";
239 $text .= join "", @lines;
247 return join "\n--\n\n", @text;
250 # Add a value to a (possibly multi-valued) hash key.
252 my ($hash, $key, $val) = @_;
253 my @val = ref $val eq 'ARRAY' ? @$val : $val;
255 if (exists $hash->{$key}) {
256 unless (ref $hash->{$key} eq 'ARRAY') {
257 my @v = $hash->{$key} ne '' ? $hash->{$key} : ();
260 push @{ $hash->{$key} }, @val;
263 $hash->{$key} = $val;
267 # "Normalise" a hash key that's known to be multi-valued.
270 my ($line, $word, @words);
272 foreach $line (map {split /\n/} (ref $val eq 'ARRAY') ? @$val : $val)
274 # XXX: This should become a real parser, à la Text::ParseWords.
277 push @words, split /\s*,\s*/, $line;
287 RT::Interface::REST - helper functions for the REST interface.
291 Only the REST should use this module.