Update lib/sphinxapi.php to V2.0.1

This commit is contained in:
Craig Meyer 2011-08-04 18:41:04 +00:00
parent 99bfd4c47d
commit 116841e4e3
2 changed files with 1714 additions and 23 deletions

View File

@ -1,11 +1,13 @@
<?php <?php
// //
// $Id: sphinxapi.php 2055 2009-11-06 23:09:58Z shodan $ // $Id: sphinxapi.php 2758 2011-04-04 11:10:44Z kevg $
// //
// //
// Copyright (c) 2001-2008, Andrew Aksyonoff. All rights reserved. // Copyright (c) 2001-2011, Andrew Aksyonoff
// Copyright (c) 2008-2011, Sphinx Technologies Inc
// All rights reserved
// //
// This program is free software; you can redistribute it and/or modify // This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License. You should have // it under the terms of the GNU General Public License. You should have
@ -24,15 +26,16 @@ define ( "SEARCHD_COMMAND_UPDATE", 2 );
define ( "SEARCHD_COMMAND_KEYWORDS", 3 ); define ( "SEARCHD_COMMAND_KEYWORDS", 3 );
define ( "SEARCHD_COMMAND_PERSIST", 4 ); define ( "SEARCHD_COMMAND_PERSIST", 4 );
define ( "SEARCHD_COMMAND_STATUS", 5 ); define ( "SEARCHD_COMMAND_STATUS", 5 );
define ( "SEARCHD_COMMAND_QUERY", 6 ); define ( "SEARCHD_COMMAND_FLUSHATTRS", 7 );
/// current client-side command implementation versions /// current client-side command implementation versions
define ( "VER_COMMAND_SEARCH", 0x116 ); define ( "VER_COMMAND_SEARCH", 0x118 );
define ( "VER_COMMAND_EXCERPT", 0x100 ); define ( "VER_COMMAND_EXCERPT", 0x103 );
define ( "VER_COMMAND_UPDATE", 0x102 ); define ( "VER_COMMAND_UPDATE", 0x102 );
define ( "VER_COMMAND_KEYWORDS", 0x100 ); define ( "VER_COMMAND_KEYWORDS", 0x100 );
define ( "VER_COMMAND_STATUS", 0x100 ); define ( "VER_COMMAND_STATUS", 0x100 );
define ( "VER_COMMAND_QUERY", 0x100 ); define ( "VER_COMMAND_QUERY", 0x100 );
define ( "VER_COMMAND_FLUSHATTRS", 0x100 );
/// known searchd status codes /// known searchd status codes
define ( "SEARCHD_OK", 0 ); define ( "SEARCHD_OK", 0 );
@ -57,6 +60,8 @@ define ( "SPH_RANK_WORDCOUNT", 3 ); ///< simple word-count weighting, rank is a
define ( "SPH_RANK_PROXIMITY", 4 ); define ( "SPH_RANK_PROXIMITY", 4 );
define ( "SPH_RANK_MATCHANY", 5 ); define ( "SPH_RANK_MATCHANY", 5 );
define ( "SPH_RANK_FIELDMASK", 6 ); define ( "SPH_RANK_FIELDMASK", 6 );
define ( "SPH_RANK_SPH04", 7 );
define ( "SPH_RANK_TOTAL", 8 );
/// known sort modes /// known sort modes
define ( "SPH_SORT_RELEVANCE", 0 ); define ( "SPH_SORT_RELEVANCE", 0 );
@ -78,6 +83,7 @@ define ( "SPH_ATTR_ORDINAL", 3 );
define ( "SPH_ATTR_BOOL", 4 ); define ( "SPH_ATTR_BOOL", 4 );
define ( "SPH_ATTR_FLOAT", 5 ); define ( "SPH_ATTR_FLOAT", 5 );
define ( "SPH_ATTR_BIGINT", 6 ); define ( "SPH_ATTR_BIGINT", 6 );
define ( "SPH_ATTR_STRING", 7 );
define ( "SPH_ATTR_MULTI", 0x40000000 ); define ( "SPH_ATTR_MULTI", 0x40000000 );
/// known grouping functions /// known grouping functions
@ -631,7 +637,7 @@ class SphinxClient
$left = $len; $left = $len;
while ( $left>0 && !feof($fp) ) while ( $left>0 && !feof($fp) )
{ {
$chunk = fread ( $fp, $left ); $chunk = fread ( $fp, min ( 8192, $left ) );
if ( $chunk ) if ( $chunk )
{ {
$response .= $chunk; $response .= $chunk;
@ -731,11 +737,7 @@ class SphinxClient
/// set ranking mode /// set ranking mode
function SetRankingMode ( $ranker ) function SetRankingMode ( $ranker )
{ {
assert ( $ranker==SPH_RANK_PROXIMITY_BM25 assert ( $ranker>=0 && $ranker<SPH_RANK_TOTAL );
|| $ranker==SPH_RANK_BM25
|| $ranker==SPH_RANK_NONE
|| $ranker==SPH_RANK_WORDCOUNT
|| $ranker==SPH_RANK_PROXIMITY );
$this->_ranker = $ranker; $this->_ranker = $ranker;
} }
@ -1106,8 +1108,8 @@ class SphinxClient
// send query, get response // send query, get response
$nreqs = count($this->_reqs); $nreqs = count($this->_reqs);
$req = join ( "", $this->_reqs ); $req = join ( "", $this->_reqs );
$len = 4+strlen($req); $len = 8+strlen($req);
$req = pack ( "nnNN", SEARCHD_COMMAND_SEARCH, VER_COMMAND_SEARCH, $len, $nreqs ) . $req; // add header $req = pack ( "nnNNN", SEARCHD_COMMAND_SEARCH, VER_COMMAND_SEARCH, $len, 0, $nreqs ) . $req; // add header
if ( !( $this->_Send ( $fp, $req, $len+8 ) ) || if ( !( $this->_Send ( $fp, $req, $len+8 ) ) ||
!( $response = $this->_GetResponse ( $fp, VER_COMMAND_SEARCH ) ) ) !( $response = $this->_GetResponse ( $fp, VER_COMMAND_SEARCH ) ) )
@ -1241,6 +1243,10 @@ class SphinxClient
list(,$val) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4; list(,$val) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4;
$attrvals[$attr][] = sphFixUint($val); $attrvals[$attr][] = sphFixUint($val);
} }
} else if ( $type==SPH_ATTR_STRING )
{
$attrvals[$attr] = substr ( $response, $p, $val );
$p += $val;
} else } else
{ {
$attrvals[$attr] = sphFixUint($val); $attrvals[$attr] = sphFixUint($val);
@ -1305,22 +1311,37 @@ class SphinxClient
if ( !isset($opts["after_match"]) ) $opts["after_match"] = "</b>"; if ( !isset($opts["after_match"]) ) $opts["after_match"] = "</b>";
if ( !isset($opts["chunk_separator"]) ) $opts["chunk_separator"] = " ... "; if ( !isset($opts["chunk_separator"]) ) $opts["chunk_separator"] = " ... ";
if ( !isset($opts["limit"]) ) $opts["limit"] = 256; if ( !isset($opts["limit"]) ) $opts["limit"] = 256;
if ( !isset($opts["limit_passages"]) ) $opts["limit_passages"] = 0;
if ( !isset($opts["limit_words"]) ) $opts["limit_words"] = 0;
if ( !isset($opts["around"]) ) $opts["around"] = 5; if ( !isset($opts["around"]) ) $opts["around"] = 5;
if ( !isset($opts["exact_phrase"]) ) $opts["exact_phrase"] = false; if ( !isset($opts["exact_phrase"]) ) $opts["exact_phrase"] = false;
if ( !isset($opts["single_passage"]) ) $opts["single_passage"] = false; if ( !isset($opts["single_passage"]) ) $opts["single_passage"] = false;
if ( !isset($opts["use_boundaries"]) ) $opts["use_boundaries"] = false; if ( !isset($opts["use_boundaries"]) ) $opts["use_boundaries"] = false;
if ( !isset($opts["weight_order"]) ) $opts["weight_order"] = false; if ( !isset($opts["weight_order"]) ) $opts["weight_order"] = false;
if ( !isset($opts["query_mode"]) ) $opts["query_mode"] = false;
if ( !isset($opts["force_all_words"]) ) $opts["force_all_words"] = false;
if ( !isset($opts["start_passage_id"]) ) $opts["start_passage_id"] = 1;
if ( !isset($opts["load_files"]) ) $opts["load_files"] = false;
if ( !isset($opts["html_strip_mode"]) ) $opts["html_strip_mode"] = "index";
if ( !isset($opts["allow_empty"]) ) $opts["allow_empty"] = false;
if ( !isset($opts["passage_boundary"]) ) $opts["passage_boundary"] = "none";
if ( !isset($opts["emit_zones"]) ) $opts["emit_zones"] = false;
///////////////// /////////////////
// build request // build request
///////////////// /////////////////
// v.1.0 req // v.1.2 req
$flags = 1; // remove spaces $flags = 1; // remove spaces
if ( $opts["exact_phrase"] ) $flags |= 2; if ( $opts["exact_phrase"] ) $flags |= 2;
if ( $opts["single_passage"] ) $flags |= 4; if ( $opts["single_passage"] ) $flags |= 4;
if ( $opts["use_boundaries"] ) $flags |= 8; if ( $opts["use_boundaries"] ) $flags |= 8;
if ( $opts["weight_order"] ) $flags |= 16; if ( $opts["weight_order"] ) $flags |= 16;
if ( $opts["query_mode"] ) $flags |= 32;
if ( $opts["force_all_words"] ) $flags |= 64;
if ( $opts["load_files"] ) $flags |= 128;
if ( $opts["allow_empty"] ) $flags |= 256;
if ( $opts["emit_zones"] ) $flags |= 512;
$req = pack ( "NN", 0, $flags ); // mode=0, flags=$flags $req = pack ( "NN", 0, $flags ); // mode=0, flags=$flags
$req .= pack ( "N", strlen($index) ) . $index; // req index $req .= pack ( "N", strlen($index) ) . $index; // req index
$req .= pack ( "N", strlen($words) ) . $words; // req words $req .= pack ( "N", strlen($words) ) . $words; // req words
@ -1329,8 +1350,10 @@ class SphinxClient
$req .= pack ( "N", strlen($opts["before_match"]) ) . $opts["before_match"]; $req .= pack ( "N", strlen($opts["before_match"]) ) . $opts["before_match"];
$req .= pack ( "N", strlen($opts["after_match"]) ) . $opts["after_match"]; $req .= pack ( "N", strlen($opts["after_match"]) ) . $opts["after_match"];
$req .= pack ( "N", strlen($opts["chunk_separator"]) ) . $opts["chunk_separator"]; $req .= pack ( "N", strlen($opts["chunk_separator"]) ) . $opts["chunk_separator"];
$req .= pack ( "N", (int)$opts["limit"] ); $req .= pack ( "NN", (int)$opts["limit"], (int)$opts["around"] );
$req .= pack ( "N", (int)$opts["around"] ); $req .= pack ( "NNN", (int)$opts["limit_passages"], (int)$opts["limit_words"], (int)$opts["start_passage_id"] ); // v.1.2
$req .= pack ( "N", strlen($opts["html_strip_mode"]) ) . $opts["html_strip_mode"];
$req .= pack ( "N", strlen($opts["passage_boundary"]) ) . $opts["passage_boundary"];
// documents // documents
$req .= pack ( "N", count($docs) ); $req .= pack ( "N", count($docs) );
@ -1507,6 +1530,7 @@ class SphinxClient
} }
// build request // build request
$this->_MBPush ();
$req = pack ( "N", strlen($index) ) . $index; $req = pack ( "N", strlen($index) ) . $index;
$req .= pack ( "N", count($attrs) ); $req .= pack ( "N", count($attrs) );
@ -1531,18 +1555,28 @@ class SphinxClient
// connect, send query, get response // connect, send query, get response
if (!( $fp = $this->_Connect() )) if (!( $fp = $this->_Connect() ))
{
$this->_MBPop ();
return -1; return -1;
}
$len = strlen($req); $len = strlen($req);
$req = pack ( "nnN", SEARCHD_COMMAND_UPDATE, VER_COMMAND_UPDATE, $len ) . $req; // add header $req = pack ( "nnN", SEARCHD_COMMAND_UPDATE, VER_COMMAND_UPDATE, $len ) . $req; // add header
if ( !$this->_Send ( $fp, $req, $len+8 ) ) if ( !$this->_Send ( $fp, $req, $len+8 ) )
{
$this->_MBPop ();
return -1; return -1;
}
if (!( $response = $this->_GetResponse ( $fp, VER_COMMAND_UPDATE ) )) if (!( $response = $this->_GetResponse ( $fp, VER_COMMAND_UPDATE ) ))
{
$this->_MBPop ();
return -1; return -1;
}
// parse response // parse response
list(,$updated) = unpack ( "N*", substr ( $response, 0, 4 ) ); list(,$updated) = unpack ( "N*", substr ( $response, 0, 4 ) );
$this->_MBPop ();
return $updated; return $updated;
} }
@ -1619,8 +1653,39 @@ class SphinxClient
$this->_MBPop (); $this->_MBPop ();
return $res; return $res;
} }
//////////////////////////////////////////////////////////////////////////
// flush
//////////////////////////////////////////////////////////////////////////
function FlushAttributes ()
{
$this->_MBPush ();
if (!( $fp = $this->_Connect() ))
{
$this->_MBPop();
return -1;
}
$req = pack ( "nnN", SEARCHD_COMMAND_FLUSHATTRS, VER_COMMAND_FLUSHATTRS, 0 ); // len=0
if ( !( $this->_Send ( $fp, $req, 8 ) ) ||
!( $response = $this->_GetResponse ( $fp, VER_COMMAND_FLUSHATTRS ) ) )
{
$this->_MBPop ();
return -1;
}
$tag = -1;
if ( strlen($response)==4 )
list(,$tag) = unpack ( "N*", $response );
else
$this->_error = "unexpected response length";
$this->_MBPop ();
return $tag;
}
} }
// //
// $Id: sphinxapi.php 2055 2009-11-06 23:09:58Z shodan $ // $Id: sphinxapi.php 2758 2011-04-04 11:10:44Z kevg $
// //

File diff suppressed because it is too large Load Diff