<?php  if (!defined('PmWiki')) exit();
/**
  Enhance search at pmwiki.org
  Written by (2018-2021) Petko Yotov www.pmwiki.org/petko

  This text is written for PmWiki; 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. See pmwiki.php for full details
  and lack of warranty.

  Copyright 2018-2021 Petko Yotov http://5ko.fr
 */


##  make a custom 'pmwiki' search order
if ($action == 'search') $MakePageListOpt['order'] = 'pmwiki';
$PageListSortCmp['pmwiki'] = 'PmWikiGroupSort';
$PageListSortRead['pmwiki'] = 0;

$PmWikiGroupPriority = explode(" ", "PmWiki News Cookbook Skins PITS Site Category Profiles");
foreach($PmWikiGroupPriority as $k=>$g) $PmWikiGroupOrder[$g] = -100+$k;

function PmWikiGroupSort($x, $y) {
  global $PmWikiGroupOrder;
  $xgroup = @$PmWikiGroupOrder[PageVar($x, '$Group')]+0;
  $ygroup = @$PmWikiGroupOrder[PageVar($y, '$Group')]+0;
  
  if ($xgroup != $ygroup) return $xgroup - $ygroup;
  return strcasecmp($x, $y);
} 
  
$SearchPatterns['default']['archive'] = '!^(Cookbook-V1|Archive)\.!';
$SearchPatterns['recipes'] = array(
  'cookbook' => '/^(Cookbook|Skins)\./',
  'extra' => '!-(Talk|Sandbox|Comments|Archive|Users)$!',
  'junk' => '!\.(RecentChanges|Group(Header|Footer|Attributes))$!');

$SearchPatterns['main'] = array(
  'groups' => '/^(Skins|Cookbook|PmWiki|Site|SiteAdmin)\./',
  'extra' => '!-(Talk|Sandbox|Comments|Archive|Users)$!',
  'junk' => '!\.(RecentChanges|Group(Header|Footer|Attributes))$!',
);

#if($action == 'search') {
  $FPLTemplateFunctions = array(
    'FPLTemplatePmWikiOrg' => 350,
    'FPLTemplatePmWikiOrgPostFormat' => 550,
  );
#}

function FPLTemplatePmWikiOrg($pagename, $matches, &$opt, $tparts, &$output, $postformat=false) {
  global $OOut, $action;
  $OOut = "";
  $perpage = 100;
  $cntmatches = count($matches);
  $groups = array();

  $namematches = $groupmatches = array();
  
  $rxwords = '';
  $words = array();
  if(count($opt[''])) {
    foreach($opt[''] as $word) {
      if(strlen($word)<3) continue;
      $words[] = preg_quote($word, '!');
    }
    $rxwords = "!(".implode('|', $words).")!i";
  }
  $m1 = Keep('<mark>');
  $m2 = Keep('</mark>');
  foreach($matches as $pn) {
    list($g, $n) = explode('.', $pn);
    @$groups[$g]++;
    if($rxwords) {
      if(preg_match_all($rxwords, $n, $m)) $namematches["[[$pn|".preg_replace($rxwords, "$m1$1$m2", $pn)."]]"] = levenshtein(implode('', $m[0]), $n);
      if(preg_match_all($rxwords, $g, $m)) $groupmatches["[[$g/|".preg_replace($rxwords, "$m1$1$m2", $g)."/]]"] = levenshtein(implode('', $m[0]), $g);
    }
  }
  $out = $out0 = '';
  if(count($groupmatches)) {
    asort($groupmatches);
    $out0 .= "\n>>searchgroupnames<<\n$[Group names:]&nbsp; " .implode(", ", array_keys($groupmatches));
  }
  if(count($namematches)) {
    asort($namematches);
    $out0 .= "\n>>searchpagenames<<\n$[Page names:]&nbsp; " .implode(", ", array_keys($namematches));
  }
  
  if($perpage<$cntmatches && preg_match('/^(#?default)$/', $opt['fmt'])) {
    $a = ($action=='browse')? "" : "?action=$action";
    if(!$opt['count']) $opt['count'] = "1..$perpage";
    $q = trim(preg_replace('! *count=\\S+!', '', $opt['q']));
    $out .= "\n>>searchpages<<\n$[Go to page:]&nbsp; ";
    for($i=0; $i<$cntmatches/$perpage; $i++) {
      $ii = $i+1;
      $j = $i*$perpage+1;
      $k = $ii*$perpage;
      $jk = "$j..$k";
      if($jk != $opt['count'])
        $out .= "[[$pagename$a?q=".urlencode("$q count=$jk")."|$ii]]&nbsp; ";
      else $out .= "'''$ii'''&nbsp; ";
    }
    if(count($groups)>1) {
      $out .= "\n>>searchgroups<<\n$[Only search in group:]&nbsp; ";
      foreach($groups as $g=>$cnt) {
        $out .= " [[$pagename$a?q=" .urlencode("$q group=$g") . " |$g]] %gray%($cnt)%%,";
      }
      $out = preg_replace('/,$/', '', $out);
    }
    $out .= "\n>><<\n";
  }
  $OOut = MarkupToHTML($pagename, $out);
  if($out0) $output .=  MarkupToHTML($pagename, $out0);
}
function FPLTemplatePmWikiOrgPostFormat($pagename, $matches, &$opt, $tparts, &$output) {
  global $OOut;
  $output .= $OOut;
}