#!/usr/bin/perl
#
# TWiki WikiClone (see wiki.pm for $wikiversion and other info)
#
# Copyright (C) 1999 Peter Thoeny, peter@thoeny.com
#
# 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.
#
# 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.  See the
# GNU General Public License for more details, published at 
# http://www.gnu.org/copyleft/gpl.html
#
# This is a wrapper script called by a generic "search" box, that can 
# perform different functions based on the "name" value
# searched text is in var "search"
#  all - Search in all webs
#  webs - search in a group of webs, space-separated list in var web
#  web - search only one web, name in var web (and current web in URL)
#  topicname - search for a topic name in current web
#  goto - go to topic of name
#  create - create topic of name
#  forcecreate - create topic of name, even if not in WikiName notation

# debug
# test by something like:
#  curl -i -S -d 'Search+By=Main+webs&search=foo&Go=Search' http://koala.ilog.fr/wiki/bin/searchmulti/Main

use CGI::Carp qw(fatalsToBrowser);
use CGI;
use lib ( '.' );
use lib ( '../lib' );
use TWiki;

use strict;

use vars qw($debug);

$debug = "0";

&main();

sub main
{
    my $query = new CGI;
    
    my $thePathInfo = $query->path_info(); 
    my $theRemoteUser = $query->remote_user();
    my $theTopic = $query->param( 'topic' );
    my $theUrl = $query->url;
    my $newurl;

    my( $topic, $webName ) = 
	&TWiki::initialize( $thePathInfo, $theRemoteUser, $theTopic, $theUrl, $query );
    
    if( ! &TWiki::Store::webExists( $webName ) ) {
        my $url = &TWiki::getOopsUrl( $webName, $topic, "oopsnoweb" );
        TWiki::redirect( $query, $url );
        return;
    }
    # type = all|group|web|topicname|goto|create|forcecreate
    my $opname        = $query->param( "type" ) || "";
    # web name and blank-separated list of webs in group, starts with leader
    my $web           = $query->param( "web" ) || "";
    my $group         = $query->param( "group" ) || "";
    my $leader        = $query->param( "groupleader" ) || "";
    my $topicparent   = $query->param( "topicparent" ) || "";
    my $topicparentParam = "";
    my $templatetopic = $query->param( "templatetopic" ) || "";
    my $formtemplate = $query->param( "formtemplate" ) || "";
    my $websStr       = $web;
    # string to search
    my $search        = $query->param( "search" ) || "";

    my $scope         = "text"; 
    my $order         = "topic";
    my $revSort       = "";
    my $caseSensitive = "";
    my $regex         = "";
    my $limit         = "";
    my $nosummary     = "";
    my $nosearch      = "";
    my $noheader      = "";
    my $nototal       = "";
    my $bookView      = "";
    my $renameView    = "";
    my $showlock      = "";
    my $noempty       = "";
    my $oldsearch     = ($TWiki::wikiversion eq "01 Feb 2003");
    my $searchType = "keyword";

    $group =~ s/\|/ /go;
    if ( $topicparent ) { $topicparentParam = "?topicparent=$topicparent"; }
    if ( $templatetopic ) { 
      if ( $topicparent ) { 
	$topicparentParam = "?topicparent=$topicparent&templatetopic=$templatetopic"; 
      } else {
	$topicparentParam = "?templatetopic=$templatetopic"; 
      }
    }
    if ( $formtemplate ) { 
      if ( $topicparentParam ) { 
	$topicparentParam = "$topicparentParam&formtemplate=$formtemplate"; 
      } else {
	$topicparentParam = "?formtemplate=$formtemplate"; 
      }
    }

    if ( $opname eq "all" ) { 
      $websStr = "All";
      &TWiki::writeHeader( $query );
      if ($oldsearch) {
	&TWiki::Search::searchWeb( "0", $websStr, $search, $scope, $order, 
         $regex, $limit, $revSort, $caseSensitive, $nosummary, $nosearch, 
         $noheader, $nototal, $bookView, $renameView, $showlock, $noempty, "");
      } else {
	&TWiki::Search::searchWeb( inline => 0, web => $websStr, search => $search, scope => $scope, order => $order, type => $searchType);
      }
    } elsif ( $opname eq "group" ) {
      $websStr = $group;
      &TWiki::writeDebug("$opname: searching $search in $websStr");
      &TWiki::writeHeader( $query );
      if ($oldsearch) {
        &TWiki::Search::searchWeb( "0", $websStr, $search, $scope, $order, 
          $regex, $limit, $revSort, $caseSensitive, $nosummary, $nosearch, 
          $noheader, $nototal, $bookView, $renameView, $showlock, $noempty, ""
        );
      } else {
	&TWiki::Search::searchWeb( inline => 0, web => $websStr, search => $search, scope => $scope, order => $order, type => $searchType);
      }
    } elsif ( $opname eq "web" ) {
      &TWiki::writeDebug("$opname: searching $search in $websStr");
      &TWiki::writeHeader( $query );
      if ($oldsearch) {
        &TWiki::Search::searchWeb( "0", $websStr, $search, $scope, $order, 
          $regex, $limit, $revSort, $caseSensitive, $nosummary, $nosearch, 
          $noheader, $nototal, $bookView, $renameView, $showlock, $noempty, ""
        );
      } else {
	&TWiki::Search::searchWeb( inline => 0, web => $websStr, search => $search, scope => $scope, order => $order, type => $searchType);
      }
    } elsif ( $opname eq "topicname" ) { 
      $scope = "topic";
      if ( $web eq $leader ) {
	$websStr = $group;
      } else {
	$websStr = $web;
      }
      &TWiki::writeDebug("$opname: searching $search in $websStr");
      &TWiki::writeHeader( $query );
      if ($oldsearch) {
        &TWiki::Search::searchWeb( "0", $websStr, $search, $scope, $order, 
          $regex, $limit, $revSort, $caseSensitive, $nosummary, $nosearch, 
          $noheader, $nototal, $bookView, $renameView, $showlock, $noempty, ""
        );
      } else {
	&TWiki::Search::searchWeb( inline => 0, web => $websStr, search => $search, scope => $scope, order => $order, type => $searchType);
      }
    } elsif ( $opname eq "goto" ) {
      $newurl = "$TWiki::urlHost$TWiki::scriptUrlPath/view$TWiki::scriptSuffix/$webName/$search";
      TWiki::redirect( $query, $newurl );
    } elsif ( $opname eq "create" ) {
      $search =~ s/\s//go;
      if ( TWiki::isWikiName($search)) {
	$newurl = "$TWiki::urlHost$TWiki::scriptUrlPath/edit$TWiki::scriptSuffix/$webName/$search$topicparentParam";
      } else {
	if ( ! $topicparent ) { $topicparent = $TWiki::mainTopicname; }
	$newurl = "$TWiki::urlHost$TWiki::scriptUrlPath/oops$TWiki::scriptSuffix/$webName/$topicparent?template=oopscreate&param1=$search&param2=warn";
      }
      TWiki::redirect( $query, $newurl );
    } elsif ( $opname eq "forcecreate" ) {
      $newurl = "$TWiki::urlHost$TWiki::scriptUrlPath/edit$TWiki::scriptSuffix/$webName/$search$topicparentParam";
      TWiki::redirect( $query, $newurl );
    } else { 
      # unknown op? redirect to search page
      TWiki::writeWarning "searchmulti: unknown op '$opname'";
      TWiki::redirect( $query, "$TWiki::urlHost$TWiki::scriptUrlPath/view$TWiki::scriptSuffix/$webName/WebSearch" );
    }
}
