Source for file swish-defs.php

Documentation is available at swish-defs.php

  1. <?php
  2. /* ******************************************************************** */
  3. /* CATALYST PHP Source Code */
  4. /* -------------------------------------------------------------------- */
  5. /* This program is free software; you can redistribute it and/or modify */
  6. /* it under the terms of the GNU General Public License as published by */
  7. /* the Free Software Foundation; either version 2 of the License, or */
  8. /* (at your option) any later version. */
  9. /* */
  10. /* This program is distributed in the hope that it will be useful, */
  11. /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
  12. /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
  13. /* GNU General Public License for more details. */
  14. /* */
  15. /* You should have received a copy of the GNU General Public License */
  16. /* along with this program; if not, write to: */
  17. /* The Free Software Foundation, Inc., 59 Temple Place, Suite 330, */
  18. /* Boston, MA 02111-1307 USA */
  19. /* -------------------------------------------------------------------- */
  20. /* */
  21. /* Filename: swish-defs.php */
  22. /* Author: Paul Waite */
  23. /* Description: Definitions for interfacing to the SWISH++ search */
  24. /* engine system. Swish++ is a system which is optimised */
  25. /* for indexing and searching a file hierarchy of text */
  26. /* files of some kind. In particular it is very good at */
  27. /* doing this for HTML files, and will index the content */
  28. /* of META tags in the header of such files. This allows */
  29. /* us to essentially cross-index these files by key data. */
  30. /* */
  31. /* ******************************************************************** */
  32. /** @package search */
  33. include_once("search-defs.php");
  34.  
  35. // ----------------------------------------------------------------------
  36. /**
  37. * The swish search class
  38. * This class inherits the functionality of the generic 'search'
  39. * class. It extends it to implement a swish++ search. To use this
  40. * you must install the swish++ system on the web server running
  41. * this code. There is a Debian package to do this for you or else
  42. * retrieve the tarball from:
  43. * http://homepage.mac.com/pauljlucas/software/swish/
  44. *
  45. * NB: After installing make sure that there are links to the main
  46. * executables: search++, index++, extract++ etc. in a directory
  47. * on the path of the executing script, eg: /usr/bin
  48. * @package search
  49. */
  50. class swish_search extends search {
  51. /** Directory to do the search from */
  52.  
  53. var $searchdir = "";
  54. /** Name of index file to use (defaults to 'swish++.index') */
  55.  
  56. var $searchindex = "";
  57. // .....................................................................
  58. /**
  59. * Constructor
  60. * Create a new swish++ search
  61. * @param string $title Title describing this search
  62. * @param string $searchdir Directory root in which to perform the search
  63. */
  64. function swish_search($title="Search Results", $searchdir="") {
  65. $this->search($title);
  66. $this->max_results = 25;
  67. $this->skip_results = 0;
  68. $this->searchdir = $searchdir;
  69. $this->initialise();
  70. }
  71. // .....................................................................
  72. /**
  73. * Set search directory
  74. * Set the root directory to go to and search from.
  75. * @param string $dir Directory root in which to perform the search
  76. */
  77. function set_searchdir($dir="") {
  78. $this->searchdir = $dir;
  79. return $this;
  80. }
  81. // .....................................................................
  82. /**
  83. * Set search index filename
  84. * Defines the index filename to use for searching. If no filename is
  85. * specified in the constructor (@see swish_search()) or via this
  86. * method, then the default is used: 'swish++.index'.
  87. * NB: This should be the basename only, not a path to a file.
  88. * @param string $fname Alternative name of index file to use in search
  89. */
  90. function set_searchindex($fname="") {
  91. $this->searchindex = $fname;
  92. return $this;
  93. }
  94. // .....................................................................
  95. /**
  96. * Execute the swish++ search
  97. * Here we execute a swish++ search, overriding the method
  98. * in the parent class. This involves piping an OS command
  99. * containing the search query terms. The default is to execute
  100. * 'search++' without any path to the executable, relying on
  101. * webserver PATH settings. You can over-ride this by specifying
  102. * a path to the executable here if required.
  103. * @param string $executable Alternative executable to use to search
  104. */
  105. function execute($executable="") {
  106. if ($executable == "") {
  107. $executable = "search++";
  108. }
  109. debug_trace($this);
  110. if ($this->queryvalid()) {
  111. // Assemble the commandline for the query..
  112. if ($this->searchdir != "") {
  113. $cmdline = "cd $this->searchdir; ";
  114. }
  115. $cmdline .= "$executable ";
  116. if ($this->skip_results > 0) {
  117. $cmdline .= "-r $this->skip_results ";
  118. }
  119. //if ($this->max_results > 0) {
  120. //$cmdline .= "-m 10000 ";
  121. //}
  122. if ($this->searchindex != "") {
  123. $cmdline .= "-i $this->searchindex ";
  124. }
  125. $cmdline .= escapeshellcmd($this->query);
  126. debugbr("swish++ module: query: '$cmdline'");
  127.  
  128. $fp = popen( $cmdline, "r" );
  129. $linecnt = 0;
  130. while ( !feof($fp) ) {
  131. $line = fgets($fp, 256);
  132. $fields = explode( " ", $line );
  133. $linecnt += 1;
  134. if ($fields[0] != "#" && $line != "") {
  135. $title = "";
  136. for( $j = 3; $j < count($fields); $j++) {
  137. if ($title != "") $title .= " ";
  138. $title .= $fields[$j];
  139. }
  140. $title = trim($title);
  141. if ($title == "") $title = "(No title)";
  142. $this->hit[] = new hit($fields[0], $fields[1], $fields[2], $title);
  143. }
  144. elseif ($linecnt == 1) {
  145. $swish_hits = trim($fields[2]);
  146. }
  147. } // while
  148.  
  149. // Flag that we did it..
  150. $this->executed = true;
  151. debugbr("swish++ module: execution ok: returning " . $this->hitcount() . " hits");
  152. }
  153. else {
  154. debugbr("swish++ module: invalid query: '$this->query'");
  155. }
  156. debug_trace();
  157. } // execute
  158.  
  159.  
  160.  
  161. } // swish_search class
  162. // ----------------------------------------------------------------------
  163.  
  164. ?>

Documentation generated by phpDocumentor 1.3.0RC3