<?php if (!defined('PmWiki')) exit();
/*
   search extensions script version 2.0.9 
   Copyright 2004, 2005 John Rankin john.rankin@affinity.co.nz
   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 file adds action=search and action=backlinks capabilities that 
   display search results in the current page. It turns the search box 
   into a jump box and adds a SearchQuickReference page.
*/

$HTMLStylesFmt[] = "
div#wikihead { padding-top: 10px; }
dl.fplbygroup dd { margin-bottom: 0; }
form.publish, input.publish { margin-left: 40px; }";
SDV($HandleActions['search'],'HandleSearch');
SDV($HandleActions['searchinsitu'], 'HandleSearchA');
SDV($SearchInSitu,true);

if ($action=='backlinks') {
    $action = 'search';
    $SearchPatterns['normal'][] = FmtPageName('!^$FullName$!',$pagename);
    SDV($PageSearchFmt,array("<h1 class='wikiaction'>$[Pages that link to <a href='\$PageUrl' accesskey='b'>\$Group \$Name</a>]</h1>
                $HTMLVSpace",
                'markup:(:pagelist link=$FullName fmt=custom list=normal:)'));
} else
    SDV($PageSearchFmt,array("<h1 class='wikiaction'>$[Search Results]</h1>",
                         'markup:(:searchresults fmt=custom list=normal:)',
                         'wiki:$[PmWiki.SearchQuickReference]'));
$FPLFunctions['custom'] = 'FPLCustom';
SDV($NoMatchesFmt,"<div class='indent'><em>No matches.</em></div>");
$SearchTagFmt = (preg_match('/^search(insitu)?/',$action) || preg_match('/\.Search(Wiki)?$/',$pagename)) ? 
    'Searching Wiki' :
    "<form action='\$PageUrl' method='get'>". (($SearchInSitu) ?
    "<a href='\$PageUrl?action=searchinsitu'>" :
  "<a href='\$ScriptUrl/$[Site.Search]'>") . "$[Search Wiki]</a>:
  <input type='hidden' name='n' value='\$FullName' />
  <input type='hidden' name='action' value='search' />
  <input class='searchbox' type='text' name='q' value='\$Group/' />
  <input class='searchbutton' type='submit' value='$[Go]' /></form>";
/*
Markup('searchbox','>links','/\(:searchbox:\)/',
  FmtPageName("<form class='wikisearch' action='\$PageUrl' method='get'>
    <input type='hidden' name='n' value='\$FullName' />
    <input type='hidden' name='action' value='search' />
    <input class='wikisearchbox' type='text' name='q' value='' size='40' />
    <input class='wikisearchbutton' type='submit' value='$[Search]' /></form>",
    $pagename));
*/
$SearchBoxFmt = "<form class='wikisearch' action='\$PageUrl' method='get'>
    <input type='hidden' name='n' value='\$FullName' />
    <input type='hidden' name='action' value='search' />
    <input class='wikisearchbox' type='text' name='q' value='\$SearchQuery' size='40' />
    <input class='wikisearchbutton' type='submit' value='$[Search]' /></form>";

SDV($ActDescription,'last modified');
if ($action=='search') {
  $ActDescription = 'search performed';
  $text = stripmagic($_GET['q']); $jumpto='';
  if (preg_match("/^$GroupPattern([\\/.])$NamePattern$/",$text))
    $jumpto=$text;
  elseif (preg_match("/^$NamePattern$/",$text)) 
    $jumpto = (isset($_GET['group'])) ?
        stripmagic($_GET['group']).".$text" :
        FmtPageName("\$Group.$text",$pagename);
  if ($jumpto && PageExists($jumpto)) Redirect($jumpto);
}

SDV($PublishSearch,false);
SDV($PublishSearchChecked,false);
SDV($CategoryGroup,'Category');
XLSDV('en',array(
  'SearchFor' => (($action=='search' || isset($_GET['needle'])) ? 
    'Results of searching <em>$Haystack</em> for <em>$Needle</em>.' :
    'Pages in the <b class=\'selflink\'>$Name</b> $Group'),
  'SearchFound' => '$MatchCount pages found out of $MatchSearched pages searched.'));

if ($PublishSearch) {
     if (@$_GET['format']=='makepdf')  {
        $QUERY_STRING = 
            str_replace('format=makepdf','format=pdf',$_SERVER['QUERY_STRING']);
        Redirect($pagename,"\$ScriptUrl?$QUERY_STRING");
     } else {
        $checked = ($PublishSearchChecked) ? "checked='checked'" : '';
        $FPLByGroupStartFmt =
    "<form class='publish' target='_blank' action='\$ScriptUrl' method='get'>
    <input type='hidden' name='n' value='\$FullName' />
    <dl class='fplbygroup'>";
        $FPLByGroupEndFmt = "</dl>$HTMLVSpace
    <input type='hidden' name='action' value='publish' />";
        $FPLByGroupEndFmt .= (FmtPageName('$Group',$pagename)==$CategoryGroup) ?
            "<input type='hidden' name='category' value='\$FullName' />" :
            "<input type='hidden' name='needle' value='\$Needle' />
    <input type='hidden' name='haystack' value='\$Haystack' />";
        $FPLByGroupEndFmt .= "$PDFCheckboxFmt
    <input type='submit' value='$[Publish]' /></form>";
        $FPLByGroupGFmt = "<dt><a href='\$ScriptUrl/\$Group'>\$Group</a></dt>";
        $FPLByGroupIFmt = 
    "<dd><input type='checkbox' name='pagearray[]' value='\$Group/\$Name' $checked/>
     <a href='\$PageUrl'>\$Title</a></dd>";
     }
}

function Needle($incl,$excl) {
  $n = str_replace('$LQ$RQ','','$LQ'.join('$RQ and $LQ',$incl).'$RQ');
  foreach($excl as $w) { $n .= " not \$LQ$w\$RQ"; }
  if ($n) return str_replace('$LQ','&lsquo;',
                 str_replace('$RQ','&rsquo;',htmlspecialchars(trim($n))));
  else return 'all';
}

function FPLCustom($pagename,&$matches,$opt) {
  global $NoMatchesFmt,$FmtV,$FmtP;
  $FmtV['$Haystack'] = ($GLOBALS['SearchGroup']) ? 
    '&lsquo;'.$GLOBALS['SearchGroup'].'&rsquo; group' : 'all groups';
  $FmtV['$Needle'] = (@$opt['link']) ? '&lsquo;'.$opt['link'].'&rsquo; links' : 
    Needle($GLOBALS['SearchIncl'],$GLOBALS['SearchExcl']);
  $r = FPLByGroup($pagename,$matches,$opt);
  return (count($matches)==0) ? $NoMatchesFmt : $r;
}

function HandleSearch($pagename, $level='read') {
  global $HandleSearchFmt,$PageStartFmt,$PageSearchFmt,$PageEndFmt,$FmtP;
  $FmtP['/\\$LastModified/e'] = '$GLOBALS[\'CurrentTime\']';
  SDV($HandleSearchFmt,array(&$PageStartFmt, &$PageSearchFmt, &$PageEndFmt));
  PrintFmt($pagename,$HandleSearchFmt);
}

?>