Skip to content

Commit

Permalink
Item1339: continue to extract formating from searching
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.foswiki.org/trunk@3968 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
  • Loading branch information
SvenDowideit authored and SvenDowideit committed May 24, 2009
1 parent be35258 commit cac4c52
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 115 deletions.
217 changes: 115 additions & 102 deletions core/lib/Foswiki/Search.pm
Expand Up @@ -320,7 +320,6 @@ sub searchWeb {
my $nonoise = Foswiki::isTrue( $params{nonoise} );
my $noEmpty = Foswiki::isTrue( $params{noempty}, $nonoise );

my $noSearch = Foswiki::isTrue( $params{nosearch}, $nonoise );
my $noSummary = Foswiki::isTrue( $params{nosummary}, $nonoise );
my $zeroResults =
1 - Foswiki::isTrue( ( $params{zeroresults} || 'on' ), $nonoise );
Expand All @@ -331,7 +330,6 @@ sub searchWeb {
my $scope = $params{scope} || '';
my $searchString = defined $params{search} ? $params{search} : '';
my $separator = $params{separator};
my $template = $params{template} || '';
my $topic = $params{topic} || '';
my $type = $params{type} || '';

Expand Down Expand Up @@ -366,9 +364,6 @@ sub searchWeb {
}

my $searchResult = '';
my $homeWeb = $session->{webName};
my $homeTopic = $Foswiki::cfg{HomeTopicName};
my $baseWebObject = Foswiki::Meta->new( $session, $session->{webName} );

# A value of 'all' or 'on' by itself gets all webs,
# otherwise ignored (unless there is a web called 'All'.)
Expand All @@ -380,90 +375,7 @@ sub searchWeb {
$params{numberOfWebs} = scalar(@webs);

my $output = '';
my $tmpl = '';

my $originalSearch = $searchString;
my $spacedTopic;

if ($format) {
$template = 'searchformat';
}
elsif ($template) {

# template definition overrides book and rename views
}
elsif ($doBookView) {
$template = 'searchbookview';
}
else {
$template = 'search';
}
$tmpl = $session->templates->readTemplate($template);

# SMELL: the only META tags in a template will be METASEARCH
# Why the heck are they being filtered????
$tmpl =~ s/\%META{.*?}\%//go; # remove %META{'parent'}%

# Split template into 5 sections
my ( $tmplHead, $tmplSearch, $tmplTable, $tmplNumber, $tmplTail ) =
split( /%SPLIT%/, $tmpl );

# Invalid template?
if ( !$tmplTail ) {
my $mess =
CGI::h1('Foswiki Installation Error')
. 'Incorrect format of '
. $template
. ' template (missing sections? There should be 4 %SPLIT% tags)';
if ( defined $callback ) {
&$callback( $cbdata, $mess );
return;
}
else {
return $mess;
}
}

# Expand tags in template sections
$tmplSearch = $baseWebObject->expandMacros($tmplSearch);
$tmplNumber = $baseWebObject->expandMacros($tmplNumber);

# If not inline search, also expand tags in head and tail sections
unless ($inline) {
$tmplHead = $baseWebObject->expandMacros($tmplHead);

if ( defined $callback ) {
$tmplHead = $baseWebObject->renderTML($tmplHead);
$tmplHead =~ s|</*nop/*>||goi; # remove <nop> tags
&$callback( $cbdata, $tmplHead );
}
else {

# don't render; this will be done by a single
# call at the end.
$searchResult .= $tmplHead;
}
}

# Generate 'Search:' part showing actual search string used
unless ($noSearch) {
my $searchStr = $searchString;
$searchStr =~ s/&/&amp;/go;
$searchStr =~ s/</&lt;/go;
$searchStr =~ s/>/&gt;/go;
$searchStr =~ s/^\.\*$/Index/go;
$tmplSearch =~ s/%SEARCHSTRING%/$searchStr/go;
if ( defined $callback ) {
$tmplSearch = $baseWebObject->renderTML($tmplSearch);
$tmplSearch =~ s|</*nop/*>||goi; # remove <nop> tag
&$callback( $cbdata, $tmplSearch );
}
else {

# don't render; will be done later
$searchResult .= $tmplSearch;
}
}

# Write log entry
# FIXME: Move log entry further down to log actual webs searched
Expand Down Expand Up @@ -528,6 +440,9 @@ sub searchWeb {
@webs = () unless scalar( @{ $query->{tokens} } ); #default
}

#TODO: work out how to remove this formatting value..
my $tmplTail;

# Loop through webs
my $isAdmin = $session->{users}->isAdmin( $session->{user} );
my $ttopics = 0;
Expand Down Expand Up @@ -560,9 +475,9 @@ sub searchWeb {

my $infoCache = $webObject->query( $query, $inputTopicSet, $options );
$this->sortResults( $web, $infoCache, %params );
my ( $web_ttopics, $web_searchResult ) = $this->formatResults(
$tmplTable, $tmplNumber, $webObject,
$query, $infoCache, %params
my ($web_ttopics, $web_searchResult);
( $web_ttopics, $web_searchResult, $tmplTail) = $this->formatResults(
$webObject, $query, $searchString, $infoCache, %params
);
$ttopics += $web_ttopics;
$searchResult .= $web_searchResult;
Expand All @@ -579,6 +494,9 @@ sub searchWeb {
}
}

#this should really be the object used to render things. (ie, move to format)
my $baseWebObject = Foswiki::Meta->new( $session, $session->{webName} );

unless ($inline) {
$tmplTail = $baseWebObject->expandMacros($tmplTail);

Expand Down Expand Up @@ -709,11 +627,18 @@ sub sortResults {
the implementation of %FORMAT{}%
TODO: rewrite to take a resultset, a set of params? and a hash of sub's to
enable evaluations of things like '$include(blah)' in format strings.
have a default set of replacements like $lt, $nop, $percnt, $dollar etc, and then
the hash of subs can take care of %MACRO{}% specific complex to evaluate replacements..
(that way we don't pre-evaluate and then subst)
=cut

sub formatResults {
my ( $this, $tmplTable, $tmplNumber, $webObject, $query, $infoCache,
%params )
my ( $this, $webObject, $query, $searchString, $infoCache, %params )
= @_;
my $session = $this->{session};
my $users = $session->{users};
Expand All @@ -726,14 +651,106 @@ sub formatResults {
my $baseWeb = $params{baseweb} || $session->{webName};
my $doBookView = Foswiki::isTrue( $params{bookview} );
my $caseSensitive = Foswiki::isTrue( $params{casesensitive} );
my $excludeTopic = $params{excludetopic} || '';
my $doExpandVars = Foswiki::isTrue( $params{expandvariables} );
my $nonoise = Foswiki::isTrue( $params{nonoise} );
my $noSearch = Foswiki::isTrue( $params{nosearch}, $nonoise );
my $format = $params{format} || '';
my $header = $params{header};
my $footer = $params{footer};
my $inline = $params{inline};
my $limit = $params{limit} || '';

my $searchResult = '';


#tmpl loading code.
my $tmpl = '';

my $originalSearch = $searchString;
my $spacedTopic;

my $template = $params{template} || '';
if ($format) {
$template = 'searchformat';
}
elsif ($template) {

# template definition overrides book and rename views
}
elsif ($doBookView) {
$template = 'searchbookview';
}
else {
$template = 'search';
}
$tmpl = $session->templates->readTemplate($template);

# SMELL: the only META tags in a template will be METASEARCH
# Why the heck are they being filtered????
$tmpl =~ s/\%META{.*?}\%//go; # remove %META{'parent'}%

# Split template into 5 sections
my ( $tmplHead, $tmplSearch, $tmplTable, $tmplNumber, $tmplTail ) =
split( /%SPLIT%/, $tmpl );

# Invalid template?
if ( !$tmplTail ) {
my $mess =
CGI::h1('Foswiki Installation Error')
. 'Incorrect format of '
. $template
. ' template (missing sections? There should be 4 %SPLIT% tags)';
if ( defined $callback ) {
&$callback( $cbdata, $mess );
return;
}
else {
return $mess;
}
}

# Expand tags in template sections
my $baseWebObject = Foswiki::Meta->new( $session, $session->{webName} );
$tmplSearch = $baseWebObject->expandMacros($tmplSearch);
$tmplNumber = $baseWebObject->expandMacros($tmplNumber);

# If not inline search, also expand tags in head and tail sections
unless ($inline) {
$tmplHead = $baseWebObject->expandMacros($tmplHead);

if ( defined $callback ) {
$tmplHead = $baseWebObject->renderTML($tmplHead);
$tmplHead =~ s|</*nop/*>||goi; # remove <nop> tags
&$callback( $cbdata, $tmplHead );
}
else {

# don't render; this will be done by a single
# call at the end.
$searchResult .= $tmplHead;
}
}

# Generate 'Search:' part showing actual search string used
unless ($noSearch) {
my $searchStr = $searchString;
$searchStr =~ s/&/&amp;/go;
$searchStr =~ s/</&lt;/go;
$searchStr =~ s/>/&gt;/go;
$searchStr =~ s/^\.\*$/Index/go;
$tmplSearch =~ s/%SEARCHSTRING%/$searchStr/go;
if ( defined $callback ) {
$tmplSearch = $baseWebObject->renderTML($tmplSearch);
$tmplSearch =~ s|</*nop/*>||goi; # remove <nop> tag
&$callback( $cbdata, $tmplSearch );
}
else {

# don't render; will be done later
$searchResult .= $tmplSearch;
}
}

# Limit search results
if ( $limit =~ /(^\d+$)/o ) {

Expand All @@ -748,9 +765,9 @@ sub formatResults {
}
$limit = 32000 unless ($limit);

my $doMultiple = Foswiki::isTrue( $params{multiple} );
my $nonoise = Foswiki::isTrue( $params{nonoise} );
my $noEmpty = Foswiki::isTrue( $params{noempty}, $nonoise );
#TODO: multiple is an attribute of the ResultSet
my $doMultiple = Foswiki::isTrue( $params{multiple} );
my $noEmpty = Foswiki::isTrue( $params{noempty}, $nonoise );

# Note: a defined header/footer overrides noheader/nofooter
# To maintain Cairo compatibility we ommit default header/footer if the
Expand All @@ -763,7 +780,6 @@ sub formatResults {
!defined($footer) && Foswiki::isTrue( $params{nofooter}, $nonoise )
|| ( !$footer && $format && $inline );

my $noSearch = Foswiki::isTrue( $params{nosearch}, $nonoise );
my $noSummary = Foswiki::isTrue( $params{nosummary}, $nonoise );
my $zeroResults =
1 - Foswiki::isTrue( ( $params{zeroresults} || 'on' ), $nonoise );
Expand All @@ -772,14 +788,11 @@ sub formatResults {
my $sortOrder = $params{order} || '';
my $revSort = Foswiki::isTrue( $params{reverse} );
my $scope = $params{scope} || '';
my $searchString = $params{search} || '';
my $separator = $params{separator};
my $template = $params{template} || '';
my $topic = $params{topic} || '';
my $type = $params{type} || '';

my $ttopics = 0;
my $searchResult = '';

# header and footer of $web
my ( $beforeText, $repeatText, $afterText ) =
Expand Down Expand Up @@ -1070,7 +1083,7 @@ sub formatResults {
}
}
}
return ( $ttopics, $searchResult );
return ( $ttopics, $searchResult, $tmplTail );
}

=begin TML
Expand Down
19 changes: 6 additions & 13 deletions core/lib/Foswiki/Search/Node.pm
Expand Up @@ -39,19 +39,12 @@ sub new {
1;
__DATA__
Module of Foswiki - The Free and Open Source Wiki, http://foswiki.org/, http://Foswiki.org/
# Copyright (C) 2008-2009 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) 2005-2007 TWiki Contributors. All Rights Reserved.
# TWiki Contributors are listed in the AUTHORS file in the root
# of this distribution. NOTE: Please extend that file, not this notice.
#
Module of Foswiki - The Free and Open Source Wiki, http://foswiki.org/
Copyright (C) 2008-2009 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.
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
Expand Down

0 comments on commit cac4c52

Please sign in to comment.