search.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php if (!defined('PmWiki')) exit();
  2. /* Copyright 2002-2004 Patrick R. Michaud (pmichaud@pobox.com)
  3. This file is part of PmWiki; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published
  5. by the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version. See pmwiki.php for full details.
  7. This script provides search capabilities for PmWiki. It is included
  8. by default from the stdconfig.php script unless disabled by
  9. $EnableSearch=0; in config.php.
  10. */
  11. XLSDV('en',array(
  12. 'SearchFor' => 'Results of searching for <em>$Needle</em>:',
  13. 'SearchFound' =>
  14. '$MatchCount pages found out of $MatchSearched pages searched.'
  15. ));
  16. SDV($HandleActions['search'],'HandleSearch');
  17. if (isset($EnablePathInfo) && !$EnablePathInfo)
  18. SDV($SearchTagFmt,"<form class='wikisearch' action='\$ScriptUrl'
  19. method='get'><input type='hidden' name='pagename'
  20. value='$[Main/SearchWiki]'><input class='wikisearchbox' type='text'
  21. name='text' value='\$Needle' size='40' /><input class='wikisearchbutton'
  22. type='submit' value='$[Search]' /></form>");
  23. SDV($SearchTagFmt,"<form class='wikisearch'
  24. action='\$ScriptUrl/$[Main/SearchWiki]' method='get'><input
  25. class='wikisearchbox' type='text' name='text' value='\$Needle'
  26. size='40' /><input class='wikisearchbutton' type='submit'
  27. value='$[Search]' /></form>");
  28. SDV($SearchResultsFmt,"$[SearchFor]
  29. $HTMLVSpace<dl class='wikisearch'>\$MatchList</dl>
  30. $HTMLVSpace$[SearchFound]$HTMLVSpace");
  31. SDV($PageSearchFmt,
  32. "<h1 class='wikiaction'>$[Search Results]</h1>\$SearchResults");
  33. SDV($SearchListItemFmt,'<dd><a href="$PageUrl">$Title</a></dd>');
  34. SDV($SearchListGroupFmt,'<dt><a href="$ScriptUrl/$Group">$Group</a> /</dt>');
  35. SDV($InlineReplacements['/\\[\\[\\$Search\\]\\]/e'],
  36. "FmtPageName(\$GLOBALS['SearchTagFmt'],\$pagename)");
  37. SDV($InlineReplacements['/\\[\\[\\$Searchresults\\]\\]/e'],
  38. "Keep(SearchResults(\$pagename))");
  39. foreach(array('group'=>'SearchGroup','text'=>'Needle') as $k=>$v) {
  40. $Search[$k]='';
  41. if (isset($_POST[$k])) $Search[$k]=stripmagic($_POST[$k]);
  42. if (isset($_GET[$k])) $Search[$k]=stripmagic($_GET[$k]);
  43. }
  44. if (preg_match("!^($GroupNamePattern)?/!i",$Search['text'],$match))
  45. $Search['group'] = $match[1];
  46. if ($action!='edit') {
  47. $Needle = str_replace('$','&#036;',htmlentities($Search['text'],ENT_QUOTES));
  48. $SearchGroup = htmlentities($Search['group'],ENT_QUOTES);
  49. }
  50. function SearchResults($pagename) {
  51. global $Search,$WikiLibDirs,$GroupNamePattern,$SearchExcludePatterns,
  52. $SearchListItemFmt,$SearchListGroupFmt,$SearchResultsFmt,
  53. $GroupFreeLinkPattern,$PageNameSpace;
  54. if (!$Search['text']) return "";
  55. $matches = array(); $matchsearched = 0;
  56. $group = $Search['group'];
  57. $terms = preg_replace("!^($GroupNamePattern)?/!i","",$Search['text']);
  58. $terms = preg_split('/((?<!\\S)[-+]?[\'"].*?[\'"](?!\\S)|\\S+)/',$terms,-1,
  59. PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
  60. $excl = array(); $incl = array();
  61. foreach($terms as $t) {
  62. if (trim($t)=="") continue;
  63. preg_match('/^([-+]?)([\'"]?)(.+?)\\2$/',$t,$match);
  64. if ($match[1]=='-') $excl[] = $match[3];
  65. else $incl[] = $match[3];
  66. }
  67. $dirlist = (array)$WikiLibDirs;
  68. while (count($dirlist)>0) {
  69. $dir = array_shift($dirlist);
  70. $dfp = opendir($dir); if (!$dfp) continue;
  71. while (($pagefile=readdir($dfp))!=false) {
  72. if (substr($pagefile,0,1)=='.') continue;
  73. if (is_dir("$dir/$pagefile"))
  74. { array_push($dirlist,"$dir/$pagefile"); continue; }
  75. if (@$seen[$pagefile]++) continue;
  76. if ($group && strcasecmp(FmtPageName('$Group',$pagefile),$group)!=0)
  77. continue;
  78. if (isset($SearchExcludePatterns))
  79. foreach((array)$SearchExcludePatterns as $t)
  80. if (preg_match($t,$pagefile)) continue 2;
  81. $page = ReadPage($pagefile); Lock(0); if (!$page) continue;
  82. $matchsearched++;
  83. $text = $pagefile."\n".preg_replace("/$GroupFreeLinkPattern/e","'$0'.preg_replace('/\\s+/','$PageNameSpace',ucwords('$3'.'$4')).' '.'$3'.'$5'",$page['text']);
  84. foreach($excl as $t) if (stristr($text,$t)) continue 2;
  85. foreach($incl as $t) if (!stristr($text,$t)) continue 2;
  86. $matches[] = $pagefile;
  87. }
  88. closedir($dfp);
  89. }
  90. sort($matches); reset($matches);
  91. $MatchList = array();
  92. foreach($matches as $pagefile) {
  93. $pgroup = FmtPageName($SearchListGroupFmt,$pagefile);
  94. if ($pgroup!=@$lgroup) {
  95. $MatchList[] = $pgroup;
  96. $lgroup = $pgroup;
  97. }
  98. $MatchList[] = FmtPageName($SearchListItemFmt,$pagefile);
  99. }
  100. $GLOBALS['MatchList'] = join('',$MatchList);
  101. $GLOBALS['MatchCount'] = count($matches);
  102. $GLOBALS['MatchSearched'] = $matchsearched;
  103. $GLOBALS['SearchIncl'] = $incl;
  104. $GLOBALS['SearchExcl'] = $excl;
  105. $GLOBALS['GCount'] = 0;
  106. return FmtPageName($SearchResultsFmt,$pagename);
  107. }
  108. function HandleSearch($pagename) {
  109. global $HandleSearchFmt,$SearchResults,$CurrentTime;
  110. global $PageStartFmt,$PageSearchFmt,$PageEndFmt;
  111. $SearchResults = SearchResults($pagename);
  112. $GLOBALS['LastModified'] = $CurrentTime;
  113. $GLOBALS['LastModifiedBy'] = @$Author;
  114. $GLOBALS['HTMLTitle'] = 'Search Results';
  115. $GLOBALS['GCount'] = 0;
  116. SDV($HandleSearchFmt,array(&$PageStartFmt, &$PageSearchFmt, &$PageEndFmt));
  117. PrintFmt($pagename,$HandleSearchFmt);
  118. }
  119. ?>