<?php if (!defined('PmWiki')) exit();

define(SEARCHINDEX_VERSION, '1.0beta001');

/*  Copyright 2005 Siegfried Seibert (s.seibert@fh-darmstadt.de)
    This file is searchindex.php; 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 script's goal is to make PmWiki's search tool faster by 
		generating and using a search index file.

    To use this script you have to do the following:
		
		1) First copy this script into the cookbook/ directory
    and add the following line to config.php (or a per-page/per-group
    customization file):

        include_once('cookbook/searchindex.php');
				
    With this script you'll be given a special, new action 'makeindex',
    which can be invoked by appending '?action=makeindex' to page's
    address in your browser's address bar. This action will build (or 
		renew) a file 'searchindex.txt' in your 'local'-directory. Please 
		use this action periodically to update the searchindex of your
		PmWiki installation.

		2) Copy the script pagelist from the script directory to your cookbook
		directory. In the new 'cookbook/pagelist.php' file include the following 
		line after  line 160 (PmWiki version 2.02: " $FmtV['$MatchSearched'] = count($list); " ) 
		
				if ($searchterms) $list = SearchIndexList($inclp, $exclp, $list);
								
		This change will integrate the searchindex into PmWikis Standard, searchbox,
		searchresults and pagelist functions.								
		Please note that with every new PmWiki version, in which 'scrip/pagelist.php'
		is changed, you have to repeat this step 2.
								
		3) Add the following 2 lines to config.php (or a per-page/per-group
    customization file):

        $EnablePageList = 0;  
				include_once('cookbook/pagelist.php');
				
		This command will activate the use of the searchindex script in  your PmWiki.
				
    For more details, and some notes, visit:
    http://pmwiki.org/wiki/Cookbook/SearchIndex

*/

## Following Variables could be customized in 'config.php' before searchindex.php is included
SDV($SearchIndexFile, "local/searchindex.txt"); 
SDV($PagesNotIndexed, "(RecentChanges|GeänderteSeiten|Administration|Template|Group)");
SDV($ExpressionsRemovedFormIndex, "(nogroupheader|noleft|noright|notitle|wikilist|form|title|comment|align|left|fmt|simple|pagelist|group|count|rfloat|lfloat|Attach| \.|\{.*\}|\.\.\.|\.\.|----)");
SDV($SymbolsRemovedFromIndex, "\\²*!#%[]|_\':/<=>@~,()+^?\"·");	

## Don't Change the Following Code

if ($action=='makeindex') {
  Lock(2);
  $page = RetrieveAuthPage($pagename, "admin");
  if (!$page) { Abort("?admin password required"); }
  $pagelist = $WikiDir->ls(); 
	$i = 0;
	echo "<b>List of Indexed PmWiki Pages: </b>";
  foreach($pagelist as $pagename) {
		if (ereg($PagesNotIndexed, $pagename)) continue; // Wiki Pages excluded from the index file
    echo "$pagename - ";
		$page = ReadPage($pagename);
		$file = $page['text'];	
    	$file = strip_tags($file);   // remove php/html tags
      $file = ereg_replace($ExpressionsRemovedFormIndex, "" , $file); 
    	$file = strtr($file, $SymbolsRemovedFromIndex, str_pad("", strlen($SymbolsRemovedFromIndex))); 
      $file = trim(ereg_replace("[[:space:]]+", " " , $file)); // reduce white space to single spaces
		$list .= $pagename.":".$file."\n";	 $i++;			
	}
	$h = fopen( $SearchIndexFile, 'w' );
	if( !$h ) Abort( "Couldn't open $fn for writing!" );
	$list = substr ($list, 0, -1); // remove last '\n'
	fwrite( $h, $list );
	fclose( $h );
	echo "<b>".$i." Pages Indexed.</b>";
  exit;
}
	
function SearchIndexList($inclp, $exclp, $pats) {
	global $SearchIndexFile;
	$index = file( $SearchIndexFile );
	foreach( $index as $t ) {
		$t = rtrim( $t );
		if (!$t) continue;
    foreach($inclp as $i) if (!preg_match($i, $t)) continue 2;
    foreach($exclp as $i) if (preg_match($i, $t)) continue 2;
		list($pagename,$rest) = explode(":",$t,2);
		$list .= $pagename.',';		
	}
	$list = substr($list,0,-1); //remove last ',';
  $list = explode( ',', $list );
	$list = array_intersect ($list, $pats);
	return $list;	
}