0
-// $Id: sphinxapi.php 11
03 2008-01-24 18:42:57Z shodan $
0
+// $Id: sphinxapi.php 11
63 2008-02-19 21:00:40Z glook $
0
define ( "SEARCHD_COMMAND_SEARCH", 0 );
0
define ( "SEARCHD_COMMAND_EXCERPT", 1 );
0
define ( "SEARCHD_COMMAND_UPDATE", 2 );
0
+define ( "SEARCHD_COMMAND_KEYWORDS",3 );
0
/// current client-side command implementation versions
0
-define ( "VER_COMMAND_SEARCH", 0x11
2 );
0
+define ( "VER_COMMAND_SEARCH", 0x11
3 );
0
define ( "VER_COMMAND_EXCERPT", 0x100 );
0
define ( "VER_COMMAND_UPDATE", 0x101 );
0
+define ( "VER_COMMAND_KEYWORDS", 0x100 );
0
/// known searchd status codes
0
define ( "SEARCHD_OK", 0 );
0
@@ -591,11 +593,11 @@ class SphinxClient
0
/// connect to searchd server, run given search query through given indexes,
0
/// and return the search results
0
- function Query ( $query, $index="*"
)
0
+ function Query ( $query, $index="*"
, $comment="" )
0
assert ( empty($this->_reqs) );
0
- $this->AddQuery ( $query, $index
);
0
+ $this->AddQuery ( $query, $index
, $comment );
0
$results = $this->RunQueries ();
0
if ( !is_array($results) )
0
@@ -619,7 +621,7 @@ class SphinxClient
0
/// add query to multi-query batch
0
/// returns index into results array from RunQueries() call
0
- function AddQuery ( $query, $index="*"
)
0
+ function AddQuery ( $query, $index="*"
, $comment="" )
0
@@ -696,6 +698,9 @@ class SphinxClient
0
foreach ( $this->_fieldweights as $field=>$weight )
0
$req .= pack ( "N", strlen($field) ) . $field . pack ( "N", $weight );
0
+ $req .= pack ( "N", strlen($comment) ) . $comment;
0
@@ -817,7 +822,16 @@ class SphinxClient
0
list ( $doc, $weight ) = array_values ( unpack ( "N*N*",
0
substr ( $response, $p, 8 ) ) );
0
- $doc = sprintf ( "%u", $doc ); // workaround for php signed/unsigned braindamage
0
+ if ( PHP_INT_SIZE>=8 )
0
+ // x64 route, workaround broken unpack() in 5.2.2+
0
+ if ( $doc<0 ) $doc += (1<<32);
0
+ // x32 route, workaround php signed/unsigned braindamage
0
+ $doc = sprintf ( "%u", $doc );
0
$weight = sprintf ( "%u", $weight );
0
@@ -989,6 +1003,103 @@ class SphinxClient
0
+ /////////////////////////////////////////////////////////////////////////////
0
+ /////////////////////////////////////////////////////////////////////////////
0
+ /// connect to searchd server, and generate keyword list for a given query
0
+ /// returns false on failure,
0
+ /// an array of words on success
0
+ function BuildKeywords ( $query, $index, $hits )
0
+ assert ( is_string($query) );
0
+ assert ( is_string($index) );
0
+ assert ( is_bool($hits) );
0
+ // Commented out for testing Riddle
0
+ // if (!( $fp = $this->_Connect() ))
0
+ $req = pack ( "N", strlen($query) ) . $query; // req query
0
+ $req .= pack ( "N", strlen($index) ) . $index; // req index
0
+ $req .= pack ( "N", (int)$hits );
0
+ // Line for testing Riddle:
0
+ ////////////////////////////
0
+ // send query, get response
0
+ ////////////////////////////
0
+ $req = pack ( "nnN", SEARCHD_COMMAND_KEYWORDS, VER_COMMAND_KEYWORDS, $len ) . $req; // add header
0
+ $wrote = fwrite ( $fp, $req, $len+8 );
0
+ if (!( $response = $this->_GetResponse ( $fp, VER_COMMAND_KEYWORDS ) ))
0
+ $rlen = strlen($response);
0
+ list(,$nwords) = unpack ( "N*", substr ( $response, $pos, 4 ) );
0
+ for ( $i=0; $i<$nwords; $i++ )
0
+ list(,$len) = unpack ( "N*", substr ( $response, $pos, 4 ) ); $pos += 4;
0
+ $tokenized = $len ? substr ( $response, $pos, $len ) : "";
0
+ list(,$len) = unpack ( "N*", substr ( $response, $pos, 4 ) ); $pos += 4;
0
+ $normalized = $len ? substr ( $response, $pos, $len ) : "";
0
+ $res[] = array ( "tokenized"=>$tokenized, "normalized"=>$normalized );
0
+ list($ndocs,$nhits) = array_values ( unpack ( "N*N*", substr ( $response, $pos, 8 ) ) );
0
+ $res [$i]["docs"] = $ndocs;
0
+ $res [$i]["hits"] = $nhits;
0
+ $this->_error = "incomplete reply";
0
+ function EscapeString ( $string )
0
+ $from = array ( '(',')','|','-','!','@','~','\"','&' );
0
+ $to = array ( '\\(','\\)','\\|','\\-','\\!','\\@','\\~','\\\"', '\\&' );
0
+ return str_replace ( $from, $to, $string );
0
/////////////////////////////////////////////////////////////////////////////
0
/////////////////////////////////////////////////////////////////////////////
0
@@ -1060,7 +1171,7 @@ class SphinxClient
0
-// $Id: sphinxapi.php 11
03 2008-01-24 18:42:57Z shodan $
0
+// $Id: sphinxapi.php 11
63 2008-02-19 21:00:40Z glook $
0
\ No newline at end of file
Comments
No one has commented yet.