/
BruteForce.pm
309 lines (264 loc) · 10.9 KB
/
BruteForce.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# See the bottom of this file for license and copyright information
=begin TML
---+ package Foswiki::Store::QueryAlgorithms::BruteForce
Default brute-force query algorithm
Has some basic optimisation: it hoists regular expressions out of the
query to use with grep, so we can narrow down the set of topics that we
have to evaluate the query on.
Not sure exactly where the breakpoint is between the
costs of hoisting and the advantages of hoisting. Benchmarks suggest
that it's around 6 topics, though this may vary depending on disk
speed and memory size. It also depends on the complexity of the query.
=cut
package Foswiki::Store::QueryAlgorithms::BruteForce;
use strict;
#@ISA = ( 'Foswiki::Query::QueryAlgorithms' ); # interface
use Foswiki::Search::Node ();
use Foswiki::Meta ();
use Foswiki::Search::InfoCache ();
use Foswiki::Search::ResultSet ();
use Foswiki::MetaCache ();
# See Foswiki::Query::QueryAlgorithms.pm for details
sub query {
my ( $query, $inputTopicSet, $session, $options ) = @_;
#TODO: th 1==2 and other false optimisations..
#if ( !defined($query->{tokens}) or
# (@{ $query->{tokens} } ) == 0) {
# return new Foswiki::Search::InfoCache($session, '');
#}
# Eliminate static expressions
my $context = Foswiki::Meta->new( $session, $session->{webName} );
$query->simplify(tom => $context, data => $context );
my $webNames = $options->{web} || '';
my $recurse = $options->{'recurse'} || '';
my $isAdmin = $session->{users}->isAdmin( $session->{user} );
my $searchAllFlag = ( $webNames =~ /(^|[\,\s])(all|on)([\,\s]|$)/i );
my @webs = Foswiki::Search::InfoCache::_getListOfWebs( $webNames, $recurse, $searchAllFlag );
my @resultCacheList;
foreach my $web (@webs) {
# can't process what ain't thar
next unless $session->webExists($web);
my $webObject = Foswiki::Meta->new( $session, $web );
my $thisWebNoSearchAll = $webObject->getPreference('NOSEARCHALL') || '';
# make sure we can report this web on an 'all' search
# DON'T filter out unless it's part of an 'all' search.
next
if ( $searchAllFlag
&& !$isAdmin
&& ( $thisWebNoSearchAll =~ /on/i || $web =~ /^[\.\_]/ )
&& $web ne $session->{webName} );
#TODO: combine these into one great ResultSet
my $infoCache = _webQuery($query, $web, $inputTopicSet, $session, $options);
push(@resultCacheList, $infoCache);
}
my $resultset = new Foswiki::Search::ResultSet(\@resultCacheList, $options->{groupby}, $options->{order}, Foswiki::isTrue( $options->{reverse} ));
#TODO: $options should become redundant
$resultset->sortResults( $options );
return $resultset;
}
#ok, for initial validation, naively call the code with a web.
sub _webQuery {
my ( $query, $web, $inputTopicSet, $session, $options ) = @_;
require Foswiki::Query::HoistREs;
my $hoistedREs = Foswiki::Query::HoistREs::collatedHoist($query);
if ((!defined($options->{topic})) and
($hoistedREs->{name}) and
(scalar(@{$hoistedREs->{name}}) == 1) #only do this if the 'name' query is simple (ie, has only one element)
) {
my @filter = @{$hoistedREs->{name_source}};
#set the 'includetopic' matcher..
$options->{topic} = $filter[0];
}
my $topicSet = $inputTopicSet;
if (!defined($topicSet)) {
#then we start with the whole web?
#TODO: i'm sure that is a flawed assumption
my $webObject = Foswiki::Meta->new( $session, $web );
$topicSet = Foswiki::Search::InfoCache::getTopicListIterator( $webObject, $options );
}
#TODO: howto ask iterator for list length?
#TODO: once the inputTopicSet isa ResultSet we might have an idea
# if ( scalar(@$topics) > 6 ) {
if ( defined($hoistedREs->{text}) ) {
my $searchOptions = {
type => 'regex',
casesensitive => 1,
files_without_match => 1,
};
my @filter = @{$hoistedREs->{text}};
my $searchQuery =
new Foswiki::Search::Node( $query->toString(), \@filter,
$searchOptions );
$topicSet->reset();
$topicSet =
$session->{store}->searchInWebMetaData(
$searchQuery, $web, $topicSet, $session, $searchOptions );
}
else {
#TODO: clearly _this_ can be re-written as a FilterIterator, and if we are able to use the sorting hints (ie DB Store) can propogate all the way to FORMAT
#print STDERR "WARNING: couldn't hoistREs on ".$query->toString();
}
my $resultTopicSet =
new Foswiki::Search::InfoCache( $Foswiki::Plugins::SESSION, $web);
local $/;
$topicSet->reset();
while ( $topicSet->hasNext() ) {
my $webtopic = $topicSet->next();
my ($Iweb, $topic) = Foswiki::Func::normalizeWebTopicName($web, $webtopic);
my $cache = $Foswiki::Plugins::SESSION->search->metacache->get($Iweb, $topic);
my $meta = $cache->{tom};
# this 'lazy load' will become useful when @$topics becomes
# an infoCache
$meta->reload() unless ( $meta->getLoadedRev() );
next unless ( $meta->getLoadedRev() );
print STDERR "Processing $topic\n" if Foswiki::Query::Node::MONITOR_EVAL();
my $match = $query->evaluate( tom => $meta, data => $meta );
if ($match) {
$resultTopicSet->addTopic($meta);
}
}
return $resultTopicSet;
}
# The getField function is here to allow for Store specific optimisations
# such as direct database lookups.
sub getField {
my ( $this, $node, $data, $field ) = @_;
my $result;
if ( UNIVERSAL::isa( $data, 'Foswiki::Meta' ) ) {
# The object being indexed is a Foswiki::Meta object, so
# we have to use a different approach to treating it
# as an associative array. The first thing to do is to
# apply our "alias" shortcuts.
my $realField = $field;
if ( $Foswiki::Query::Node::aliases{$field} ) {
$realField = $Foswiki::Query::Node::aliases{$field};
}
if ( $realField =~ s/^META:// ) {
if ( $Foswiki::Query::Node::isArrayType{$realField} ) {
# Array type, have to use find
my @e = $data->find($realField);
$result = \@e;
}
else {
$result = $data->get($realField);
}
}
elsif ( $realField eq 'name' ) {
# Special accessor to compensate for lack of a topic
# name anywhere in the saved fields of meta
return $data->topic();
}
elsif ( $realField eq 'text' ) {
# Special accessor to compensate for lack of the topic text
# name anywhere in the saved fields of meta
return $data->text();
}
elsif ( $realField eq 'web' ) {
# Special accessor to compensate for lack of a web
# name anywhere in the saved fields of meta
return $data->web();
}
else {
# The field name isn't an alias, check to see if it's
# the form name
my $form = $data->get('FORM');
if ( $form && $field eq $form->{name} ) {
# SHORTCUT;it's the form name, so give me the fields
# as if the 'field' keyword had been used.
# TODO: This is where multiple form support needs to reside.
# Return the array of FIELD for further indexing.
my @e = $data->find('FIELD');
return \@e;
}
else {
# SHORTCUT; not a predefined name; assume it's a field
# 'name' instead.
# SMELL: Needs to error out if there are multiple forms -
# or perhaps have a heuristic that gives access to the
# uniquely named field.
$result = $data->get( 'FIELD', $field );
$result = $result->{value} if $result;
}
}
}
elsif ( ref($data) eq 'ARRAY' ) {
# Array objects are returned during evaluation, e.g. when
# a subset of an array is matched for further processing.
# Indexing an array object. The index will be one of:
# 1. An integer, which is an implicit index='x' query
# 2. A name, which is an implicit name='x' query
if ( $field =~ /^\d+$/ ) {
# Integer index
$result = $data->[$field];
}
else {
# String index
my @res;
# Get all array entries that match the field
foreach my $f (@$data) {
my $val = getField( undef, $node, $f, $field );
push( @res, $val ) if defined($val);
}
if ( scalar(@res) ) {
$result = \@res;
}
else {
# The field name wasn't explicitly seen in any of the records.
# Try again, this time matching 'name' and returning 'value'
foreach my $f (@$data) {
next unless ref($f) eq 'HASH';
if ( $f->{name}
&& $f->{name} eq $field
&& defined $f->{value} )
{
push( @res, $f->{value} );
}
}
if ( scalar(@res) ) {
$result = \@res;
}
}
}
}
elsif ( ref($data) eq 'HASH' ) {
# A hash object may be returned when a sub-object of a Foswiki::Meta
# object has been matched.
$result = $data->{ $node->{params}[0] };
}
else {
$result = $node->{params}[0];
}
return $result;
}
# Get a referenced topic
# See Foswiki::Store::QueryAlgorithms.pm for details
sub getRefTopic {
my ($this, $relativeTo, $w, $t) = @_;
return Foswiki::Meta->load( $relativeTo->session, $w, $t );
}
1;
__END__
# Module of Foswiki - The Free and Open Source Wiki, http://foswiki.org/
#
# Copyright (C) 2008-2010 Foswiki Contributors. All Rights Reserved.
# Foswiki Contributors are listed in the AUTHORS file in the root
# of this distribution. NOTE: Please extend that file, not this notice.
#
# Additional copyrights apply to some or all of the code in this
# file as follows:
#
# Copyright (C) 2007 TWiki Contributors. All Rights Reserved.
#
# 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.
#
# Author: Crawford Currie