simplified the rules engine
This commit is contained in:
parent
5037b16dbe
commit
394da51da7
5 changed files with 184 additions and 42 deletions
|
|
@ -1178,8 +1178,8 @@ sub manageAssetsSearch {
|
|||
return $output unless ($self->session->form->get("doit"));
|
||||
my $class = $self->session->form->get("class") eq "any" ? undef : $self->session->form->get("class");
|
||||
my $assets = WebGUI::Search->new($self->session,0)->search({
|
||||
keywords=>{terms=>[$self->session->form->get("keywords")]},
|
||||
classes=>{terms=>[$class]}
|
||||
keywords=>$self->session->form->get("keywords"),
|
||||
classes=>[$class]
|
||||
})->getAssets;
|
||||
$output .= "<script type=\"text/javascript\">
|
||||
//<![CDATA[
|
||||
|
|
|
|||
110
lib/WebGUI/Asset/Wobject/Search.pm
Normal file
110
lib/WebGUI/Asset/Wobject/Search.pm
Normal file
|
|
@ -0,0 +1,110 @@
|
|||
package WebGUI::Asset::Wobject::Search;
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
# WebGUI is Copyright 2001-2006 Plain Black Corporation.
|
||||
#-------------------------------------------------------------------
|
||||
# Please read the legal notices (docs/legal.txt) and the license
|
||||
# (docs/license.txt) that came with this distribution before using
|
||||
# this software.
|
||||
#-------------------------------------------------------------------
|
||||
# http://www.plainblack.com info@plainblack.com
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
use strict;
|
||||
use base "WebGUI::Asset::Wobject";
|
||||
use Tie::IxHash;
|
||||
use WebGUI::International;
|
||||
use WebGUI::Paginator;
|
||||
|
||||
=head1 NAME
|
||||
|
||||
Package WebGUI::Asset::Wobject::Search
|
||||
|
||||
=head1 DESCRIPTION
|
||||
|
||||
Asset used to search WebGUI content.
|
||||
|
||||
=head2 definition ( $class, $definition )
|
||||
|
||||
This method defines all properties of a Search and is used to autogenerate most methods.
|
||||
|
||||
=head3 $class
|
||||
|
||||
$class is used to make sure that inheritance works on Assets and Wobjects.
|
||||
|
||||
=head3 $definition
|
||||
|
||||
Definition hashref from subclasses.
|
||||
|
||||
=head3 Search specific properties
|
||||
|
||||
=over 4
|
||||
|
||||
=item templateId
|
||||
|
||||
ID of a tempate from the Search namespace to display the search results.
|
||||
|
||||
=back
|
||||
|
||||
=cut
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
sub definition {
|
||||
my $class = shift;
|
||||
my $session = shift;
|
||||
my $definition = shift;
|
||||
my $i18n = WebGUI::International->new($session,'Asset_Search');
|
||||
my %properties;
|
||||
tie %properties, 'Tie::IxHash';
|
||||
%properties = (
|
||||
templateId =>{
|
||||
fieldType=>"template",
|
||||
defaultValue=>'PBtmpl0000000000000200',
|
||||
tab=>"display",
|
||||
namespace=>"Search",
|
||||
hoverHelp=>$i18n->get('search template description'),
|
||||
label=>$i18n->get('search template')
|
||||
},
|
||||
searchRoot => {
|
||||
fieldType=>"asset",
|
||||
defaultValue=>$session->setting->get("defaultPage"),
|
||||
tab=>"properties",
|
||||
hoverHelp=>$i18n->get("search root description"),
|
||||
label=>$i18n->get('search root')
|
||||
}
|
||||
classLimiter => {
|
||||
fieldType => "checkList",
|
||||
defaultValue => [],
|
||||
tab=>"properties",
|
||||
hoverHelp=>$i18n->get("class limiter description"),
|
||||
label=>$i18n->get("class limiter"),
|
||||
options=>$session->db->buildHashRef("select distinct()")
|
||||
);
|
||||
push(@{$definition}, {
|
||||
assetName=>$i18n->get('assetName'),
|
||||
icon=>'search.gif',
|
||||
autoGenerateForms=>1,
|
||||
tableName=>'search',
|
||||
className=>'WebGUI::Asset::Wobject::Search',
|
||||
properties=>\%properties
|
||||
});
|
||||
return $class->SUPER::definition($session, $definition);
|
||||
}
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 view ( )
|
||||
|
||||
view defines all template variables, processes the template and
|
||||
returns the output.
|
||||
|
||||
=cut
|
||||
|
||||
sub view {
|
||||
my $self = shift;
|
||||
my %var;
|
||||
return $self->processTemplate(\%var, $templateId);
|
||||
}
|
||||
|
||||
1;
|
||||
|
||||
|
|
@ -3,7 +3,7 @@ package WebGUI::Asset::Wobject::NewWobject;
|
|||
$VERSION = "1.0.0";
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
# WebGUI is Copyright 2001-2005 Plain Black Corporation.
|
||||
# WebGUI is Copyright 2001-2006 Plain Black Corporation.
|
||||
#-------------------------------------------------------------------
|
||||
# Please read the legal notices (docs/legal.txt) and the license
|
||||
# (docs/license.txt) that came with this distribution before using
|
||||
|
|
|
|||
|
|
@ -47,9 +47,12 @@ Returns an array reference containing all the asset ids of the assets that match
|
|||
|
||||
sub getAssetIds {
|
||||
my $self = shift;
|
||||
my $query = "select assetId from assetIndex where ";
|
||||
my $query = "select assetId ";
|
||||
$query .= " , ".$self->{_score} if ($self->{_score});
|
||||
$query .= " from assetIndex where ";
|
||||
$query .= "isPublic=1 and " if ($self->{_isPublic});
|
||||
$query .= "(".$self->{_query}.")";
|
||||
$query .= "(".$self->{_where}.")";
|
||||
$query .= " order by score " if ($self->{_score});
|
||||
my $rs = $self->session->db->prepare($query);
|
||||
$rs->execute($self->{_params});
|
||||
my @ids = ();
|
||||
|
|
@ -70,9 +73,12 @@ Returns an array reference containing asset objects for those that matched.
|
|||
|
||||
sub getAssets {
|
||||
my $self = shift;
|
||||
my $query = "select assetId,className,revisionDate from assetIndex where ";
|
||||
my $query = "select assetId,className,revisionDate ";
|
||||
$query .= " , ".$self->{_score} if ($self->{_score});
|
||||
$query .= " from assetIndex where ";
|
||||
$query .= "isPublic=1 and " if ($self->{_isPublic});
|
||||
$query .= "(".$self->{_query}.")";
|
||||
$query .= "(".$self->{_where}.")";
|
||||
$query .= " order by score " if ($self->{_score});
|
||||
my $rs = $self->session->db->prepare($query);
|
||||
$rs->execute($self->{_params});
|
||||
my @assets = ();
|
||||
|
|
@ -84,6 +90,47 @@ sub getAssets {
|
|||
}
|
||||
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 getPaginatorResultSet ( )
|
||||
|
||||
Returns a paginator object containing the search result set data.
|
||||
|
||||
=head3 currentURL
|
||||
|
||||
The URL of the current page including attributes. The page number will be appended to this in all links generated by the paginator.
|
||||
|
||||
=head3 paginateAfter
|
||||
|
||||
The number of rows to display per page. If left blank it defaults to 50.
|
||||
|
||||
=head3 pageNumber
|
||||
|
||||
By default the page number will be determined by looking at $self->session->form->process("pn"). If that is empty the page number will be defaulted to "1". If you'd like to override the page number specify it here.
|
||||
|
||||
=head3 formVar
|
||||
|
||||
Specify the form variable the paginator should use in it's links. Defaults to "pn".
|
||||
|
||||
=cut
|
||||
|
||||
sub getPaginatorResultSet {
|
||||
my $self = shift;
|
||||
my $url = shift;
|
||||
my $paginate = shift;
|
||||
my $pageNumber = shift;
|
||||
my $formVar = shift;
|
||||
my $query = "select assetId, title, url, synopsis, ownerUserId, groupIdView, groupIdEdit, creationDate, revisionDate, className ";
|
||||
$query .= " , ".$self->{_score} if ($self->{_score});
|
||||
$query .= " from assetIndex where ";
|
||||
$query .= "isPublic=1 and " if ($self->{_isPublic});
|
||||
$query .= "(".$self->{_where}.")";
|
||||
$query .= " order by score " if ($self->{_score});
|
||||
my $paginator = WebGUI::Paginator->new($self->session, $url, $paginate, $pageNumber, $formVar);
|
||||
$paginator->setDataByQuery($query, undef, undef, $self->{_params});
|
||||
return $paginator;
|
||||
}
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 getResultSet ( )
|
||||
|
|
@ -94,9 +141,12 @@ Returns a WebGUI::SQL::ResultSet object containing the search results with colum
|
|||
|
||||
sub getResultSet {
|
||||
my $self = shift;
|
||||
my $query = "select assetId, title, url, synopsis, ownerUserId, groupIdView, groupIdEdit, creationDate, revisionDate, className from assetIndex where ";
|
||||
my $query = "select assetId, title, url, synopsis, ownerUserId, groupIdView, groupIdEdit, creationDate, revisionDate, className ";
|
||||
$query .= " , ".$self->{_score} if ($self->{_score});
|
||||
$query .= " from assetIndex where ";
|
||||
$query .= "isPublic=1 and " if ($self->{_isPublic});
|
||||
$query .= "(".$self->{_query}.")";
|
||||
$query .= "(".$self->{_where}.")";
|
||||
$query .= " order by score " if ($self->{_score});
|
||||
my $rs = $self->session->db->prepare($query);
|
||||
$rs->execute($self->{_params});
|
||||
return $rs;
|
||||
|
|
@ -149,7 +199,7 @@ A list of placeholder parameters to go along with the query. See WebGUI::SQL::Re
|
|||
|
||||
sub rawClause {
|
||||
my $self = shift;
|
||||
$self->{_query} = shift;
|
||||
$self->{_where} = shift;
|
||||
$self->{_params} = shift;
|
||||
return $self;
|
||||
}
|
||||
|
|
@ -166,39 +216,25 @@ A rules engine for WebGUI's search system. It also returns a reference to the se
|
|||
|
||||
A hash reference containing rules for a search. The rules will will be hash references containing the values of a rule. Here's an example rule set:
|
||||
|
||||
{
|
||||
{
|
||||
terms => [ "something to search for", "something else to search for"],
|
||||
match => "all"
|
||||
}, {
|
||||
terms => [ "000001000005", "000001000074000003" ]
|
||||
}
|
||||
}
|
||||
{ keywords => "something to search for", lineage => [ "000001000005", "000001000074000003" ] };
|
||||
|
||||
=head4 keywords
|
||||
|
||||
This rule limits the search results to assets that match keyword criteria. This rule has two properties: "terms" and "match". Terms is an array reference that contains key words and key phrases to be searched for. Match is an operator that determins whether "all" the terms must match or if "any" of the terms can match. Match defaults to "any" if not specified.
|
||||
This rule limits the search results to assets that match keyword criteria.
|
||||
|
||||
keywords => {
|
||||
terms => [ "this", "that", "foo bar" ],
|
||||
match => "all"
|
||||
}
|
||||
keywords => "foo bar"
|
||||
|
||||
=head4 lineage
|
||||
|
||||
This rule limits the search to a specific set of descendants in the asset tree. This has just one parameter, "terms", which is an array reference of asset lineages to match against.
|
||||
This rule limits the search to a specific set of descendants in the asset tree. An array reference of asset lineages to match against.
|
||||
|
||||
lineage => {
|
||||
terms => [ "000001000003", "000001000024000005" ]
|
||||
}
|
||||
lineage => [ "000001000003", "000001000024000005" ]
|
||||
|
||||
=head4 classes
|
||||
|
||||
This rule limits the search to a specific set of asset classes. It has just one parameter, "terms", which is an array reference of class names.
|
||||
This rule limits the search to a specific set of asset classes. An array reference of class names.
|
||||
|
||||
classes => {
|
||||
terms => [ "WebGUI::Asset::Wobject::Article", "WebGUI::Asset::Snippet" ]
|
||||
}
|
||||
classes => [ "WebGUI::Asset::Wobject::Article", "WebGUI::Asset::Snippet" ]
|
||||
|
||||
=head4 creationDate
|
||||
|
||||
|
|
@ -227,17 +263,13 @@ sub search {
|
|||
my $query = "";
|
||||
my @clauses = ();
|
||||
if ($rules->{keywords}) {
|
||||
push(@params,@{$rules->{keywords}{terms}});
|
||||
my $operator = ($rules->{keywords}{match} eq "all") ? " and " : " or ";
|
||||
my @phrases = ();
|
||||
foreach (1..scalar(@{$rules->{keywords}{terms}})) {
|
||||
push(@phrases, "match (keywords) against (?)");
|
||||
}
|
||||
push(@clauses, join($operator, @phrases));
|
||||
push(@params,$rules->{keywords},$rules->{keywords});
|
||||
$self->{_score} = "match (keywords) against (? in boolean mode) as score";
|
||||
push(@clauses, "match (keywords) against (? in boolean mode)");
|
||||
}
|
||||
if ($rules->{lineage}) {
|
||||
my @phrases = ();
|
||||
foreach my $lineage (@{$rules->{lineage}{terms}}) {
|
||||
foreach my $lineage (@{$rules->{lineage}}) {
|
||||
next unless defined $lineage;
|
||||
push(@params, $lineage."%");
|
||||
push(@phrases, "lineage like ?");
|
||||
|
|
@ -246,7 +278,7 @@ sub search {
|
|||
}
|
||||
if ($rules->{classes}) {
|
||||
my @phrases = ();
|
||||
foreach my $class (@{$rules->{classes}{terms}}) {
|
||||
foreach my $class (@{$rules->{classes}}) {
|
||||
next unless defined $class;
|
||||
push(@params, $class);
|
||||
push(@phrases, "className=?");
|
||||
|
|
@ -266,7 +298,7 @@ sub search {
|
|||
push(@params, $start, $end);
|
||||
}
|
||||
$self->{_params} = \@params;
|
||||
$self->{_query} = "(".join(") and (", @clauses).")";
|
||||
$self->{_where} = "(".join(") and (", @clauses).")";
|
||||
return $self;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -117,7 +117,7 @@ sub searchSite {
|
|||
my $keywords = shift;
|
||||
my $t = [Time::HiRes::gettimeofday()];
|
||||
my $search = WebGUI::Search->new($session, 0);
|
||||
$search->search({keywords=>{terms=>[$keywords]}});
|
||||
$search->search({keywords=>$keywords});
|
||||
my $rs = $search->getResultSet;
|
||||
while (my $data = $rs->hashRef) {
|
||||
print $data->{assetId}."\t".$data->{title}."\n";
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue