-
Notifications
You must be signed in to change notification settings - Fork 38
/
QUERY.pm
112 lines (94 loc) · 3.18 KB
/
QUERY.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# See bottom of file for license and copyright information
package Foswiki::Macros::QUERY;
use v5.14;
use Foswiki::Serialise ();
use Try::Tiny;
use Moo;
use namespace::clean;
extends qw(Foswiki::Object);
with qw(Foswiki::AppObject);
with qw(Foswiki::Macro);
BEGIN {
if ( $Foswiki::cfg{UseLocale} ) {
require locale;
import locale();
}
}
our $evalParser; # could share $ifParser from IF.pm
has evaluatingEval => (
is => 'rw',
lazy => 1,
default => sub { {} },
);
sub expand {
my ( $this, $params, $topicObject ) = @_;
my $result;
my $expr = $params->{_DEFAULT};
$expr = '' unless defined $expr;
my $style = ucfirst( lc( $params->{style} || 'default' ) );
if ( $style =~ m/[^a-zA-Z0-9_]/ ) {
return "%RED%QUERY: invalid 'style' parameter passed%ENDCOLOR%";
}
$style = Foswiki::Sandbox::untaintUnchecked($style);
my $rev = $params->{rev};
# FORMFIELD does its own caching.
# Either the home-made cache there should go into Meta so that both
# FORMFIELD and QUERY benefit, or the store should be made a lot smarter.
if ( defined $rev ) {
my $crev = $topicObject->getLoadedRev();
if ( defined $crev && $crev != $rev ) {
$topicObject =
Foswiki::Meta->load( $topicObject->app, $topicObject->web,
$topicObject->topic, $rev );
}
}
elsif ( !$topicObject->latestIsLoaded() ) {
# load latest rev
$topicObject = $topicObject->load();
}
# Block after 5 levels.
if ( $this->evaluatingEval->{$expr}
&& $this->evaluatingEval->{$expr} > 5 )
{
delete $this->evaluatingEval->{$expr};
return '';
}
unless ($evalParser) {
$evalParser = $this->app->create('Foswiki::Query::Parser');
}
$this->evaluatingEval->{$expr}++;
try {
my $node = $evalParser->parse($expr);
$result = $node->evaluate( tom => $topicObject, data => $topicObject );
$result = Foswiki::Serialise::serialise( $result, $style );
}
catch {
if ( $_->isa('Foswiki::Infix::Error') ) {
$result =
$this->app->inlineAlert( 'alerts', 'generic', 'QUERY{',
$params->stringify(), '}:', $_->text );
}
else {
Foswiki::Exception->rethrow($_);
}
}
finally {
delete $this->evaluatingEval->{$expr};
};
return $result;
}
1;
__END__
Foswiki - The Free and Open Source Wiki, http://foswiki.org/
Copyright (C) 2010 Foswiki Contributors. Foswiki Contributors
are listed in the AUTHORS file in the root of this distribution.
NOTE: Please extend that file, not this notice.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version. For
more details read LICENSE in the root of this distribution.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
As per the GPL, removal of this notice is prohibited.