<html>
<head>
<title>Тестируем поиск</title>
</head>
<body>
<form action="http://rock.ru/forum/sphinx/index.php" method="get">
<input type="text" name="s" value="<?=$_GET['s'] ? htmlentities($_GET['s'],ENT_COMPAT,'UTF-8') : ''?>" />
<input type="submit" />
</form>
<?php
define('SMF', 'search');
if (isset ($_GET['s'])) {
require_once('/var/www/rock/forum/Settings.php');

require_once($sourcedir . '/QueryString.php');
require_once($sourcedir . '/Subs.php');
require_once($sourcedir . '/Errors.php');
require_once($sourcedir . '/Load.php');
require_once($sourcedir . '/Security.php');

  include('/usr/share/sphinx/api/sphinxapi.php');

  $cl = new SphinxClient();
  $cl->SetServer( "localhost", 9312 );
  $cl->SetMatchMode( SPH_MATCH_EXTENDED2 );
#  $cl->SetSortMode( SPH_SORT_RELEVANCE  );
  //$cl->SetGroupBy ( "ID_TOPIC", SPH_GROUPBY_ATTR, '@weight DESC');
//  $cl->SetFieldWeights(array('subject'=>10, 'body'=>1));
#  $cl->SetSelect( 'id AS ID_MSG');
  $cl->SetLimits(0, 20);
#  $cl->SetFilter( 'model', array( 3 ) );
  $cl->SetArrayResult( true );

  $q = $cl->EscapeString ($_GET['s']);
  $result = $cl->Query( "\"$q\"", 'rt_posts' );

  $msgs = array();
  $subjects = array();
  $bodies = array();

  if ( $result === false ) {
      echo "Query failed: " . $cl->GetLastError() . ".\n";
  }
  else {
      if ( $cl->GetLastWarning() ) {
          echo "WARNING: " . $cl->GetLastWarning() . "
";
      }

      if ( ! empty($result["matches"]) ) {
          foreach ( $result["matches"] as $doc => $docinfo ) {
		$msgs[] = $docinfo['id'];
          }
	  mysql_connect("localhost","root","Id0nTkN0w");
          mysql_select_db("rock");
	  $res = mysql_query("SELECT ID_TOPIC, subject, body, ID_MSG from smf_messages WHERE ID_MSG IN (".implode(',',$msgs).");");
	  $msgs = array();
	  while ($msg = mysql_fetch_object($res)) {
		if (! $msg) continue;
		$subjects[] = $msg->subject;
		parsecode($msg->body);
		$bodies[] = $msg->body;
		$topics[] = $msg->ID_TOPIC;
		$msgs[] = $msg->ID_MSG;
	  }
	  $res2 = $cl->BuildExcerpts($subjects,'topics',$q);
	  $res3 = $cl->BuildExcerpts($bodies,'topics',$q, array("html_strip_mode" => "strip"));
	  foreach($topics as $key=>$topic) {
		  echo '<a href="http://rock.ru/forum/index.php?topic=', $topic, '.msg'.$msgs[$key].'#msg'.$msgs[$key].'">',
		       $res2[$key],"</a><br />\n";
		  echo '<i>', $res3[$key],"</i>\n<br />\n<hr />\n";
	  }		

      }
  }
}
?>
</body>
</html>
