vendor/riddle/spec/fixtures/sphinxapi.php in ultrasphinx-1.9 vs vendor/riddle/spec/fixtures/sphinxapi.php in ultrasphinx-1.11

- old
+ new

@@ -1,9 +1,9 @@ <?php // -// $Id: sphinxapi.php 1103 2008-01-24 18:42:57Z shodan $ +// $Id: sphinxapi.php 1163 2008-02-19 21:00:40Z glook $ // // // Copyright (c) 2001-2008, Andrew Aksyonoff. All rights reserved. // @@ -19,15 +19,17 @@ /// known searchd commands define ( "SEARCHD_COMMAND_SEARCH", 0 ); define ( "SEARCHD_COMMAND_EXCERPT", 1 ); define ( "SEARCHD_COMMAND_UPDATE", 2 ); +define ( "SEARCHD_COMMAND_KEYWORDS",3 ); /// current client-side command implementation versions -define ( "VER_COMMAND_SEARCH", 0x112 ); +define ( "VER_COMMAND_SEARCH", 0x113 ); define ( "VER_COMMAND_EXCERPT", 0x100 ); define ( "VER_COMMAND_UPDATE", 0x101 ); +define ( "VER_COMMAND_KEYWORDS", 0x100 ); /// known searchd status codes define ( "SEARCHD_OK", 0 ); define ( "SEARCHD_ERROR", 1 ); define ( "SEARCHD_RETRY", 2 ); @@ -589,15 +591,15 @@ ////////////////////////////////////////////////////////////////////////////// /// connect to searchd server, run given search query through given indexes, /// and return the search results - function Query ( $query, $index="*" ) + function Query ( $query, $index="*", $comment="" ) { assert ( empty($this->_reqs) ); - $this->AddQuery ( $query, $index ); + $this->AddQuery ( $query, $index, $comment ); $results = $this->RunQueries (); if ( !is_array($results) ) return false; // probably network error; error message should be already filled @@ -617,11 +619,11 @@ return pack ( "N", $t2 ); } /// add query to multi-query batch /// returns index into results array from RunQueries() call - function AddQuery ( $query, $index="*" ) + function AddQuery ( $query, $index="*", $comment="" ) { // mbstring workaround $this->_MBPush (); // build request @@ -694,10 +696,13 @@ // per-field weights $req .= pack ( "N", count($this->_fieldweights) ); foreach ( $this->_fieldweights as $field=>$weight ) $req .= pack ( "N", strlen($field) ) . $field . pack ( "N", $weight ); + // comment + $req .= pack ( "N", strlen($comment) ) . $comment; + // mbstring workaround $this->_MBPop (); // store request to requests array $this->_reqs[] = $req; @@ -815,11 +820,20 @@ } else { list ( $doc, $weight ) = array_values ( unpack ( "N*N*", substr ( $response, $p, 8 ) ) ); $p += 8; - $doc = sprintf ( "%u", $doc ); // workaround for php signed/unsigned braindamage + + if ( PHP_INT_SIZE>=8 ) + { + // x64 route, workaround broken unpack() in 5.2.2+ + if ( $doc<0 ) $doc += (1<<32); + } else + { + // x32 route, workaround php signed/unsigned braindamage + $doc = sprintf ( "%u", $doc ); + } } $weight = sprintf ( "%u", $weight ); // create match entry if ( $this->_arrayresult ) @@ -987,11 +1001,108 @@ $this->_MBPop (); return $res; } + ///////////////////////////////////////////////////////////////////////////// + // keyword generation + ///////////////////////////////////////////////////////////////////////////// + + /// connect to searchd server, and generate keyword list for a given query + /// returns false on failure, + /// an array of words on success + function BuildKeywords ( $query, $index, $hits ) + { + assert ( is_string($query) ); + assert ( is_string($index) ); + assert ( is_bool($hits) ); + + // Commented out for testing Riddle + // $this->_MBPush (); + // + // if (!( $fp = $this->_Connect() )) + // { + // $this->_MBPop(); + // return false; + // } + + ///////////////// + // build request + ///////////////// + + // v.1.0 req + $req = pack ( "N", strlen($query) ) . $query; // req query + $req .= pack ( "N", strlen($index) ) . $index; // req index + $req .= pack ( "N", (int)$hits ); + + // Line for testing Riddle: + return $req; + + //////////////////////////// + // send query, get response + //////////////////////////// + + $len = strlen($req); + $req = pack ( "nnN", SEARCHD_COMMAND_KEYWORDS, VER_COMMAND_KEYWORDS, $len ) . $req; // add header + $wrote = fwrite ( $fp, $req, $len+8 ); + if (!( $response = $this->_GetResponse ( $fp, VER_COMMAND_KEYWORDS ) )) + { + $this->_MBPop (); + return false; + } + + ////////////////// + // parse response + ////////////////// + + $pos = 0; + $res = array (); + $rlen = strlen($response); + list(,$nwords) = unpack ( "N*", substr ( $response, $pos, 4 ) ); + $pos += 4; + for ( $i=0; $i<$nwords; $i++ ) + { + list(,$len) = unpack ( "N*", substr ( $response, $pos, 4 ) ); $pos += 4; + $tokenized = $len ? substr ( $response, $pos, $len ) : ""; + $pos += $len; + + list(,$len) = unpack ( "N*", substr ( $response, $pos, 4 ) ); $pos += 4; + $normalized = $len ? substr ( $response, $pos, $len ) : ""; + $pos += $len; + + $res[] = array ( "tokenized"=>$tokenized, "normalized"=>$normalized ); + + if ( $hits ) + { + list($ndocs,$nhits) = array_values ( unpack ( "N*N*", substr ( $response, $pos, 8 ) ) ); + $pos += 8; + $res [$i]["docs"] = $ndocs; + $res [$i]["hits"] = $nhits; + } + + if ( $pos > $rlen ) + { + $this->_error = "incomplete reply"; + $this->_MBPop (); + return false; + } + } + + $this->_MBPop (); + return $res; + } + + function EscapeString ( $string ) + { + $from = array ( '(',')','|','-','!','@','~','\"','&' ); + $to = array ( '\\(','\\)','\\|','\\-','\\!','\\@','\\~','\\\"', '\\&' ); + + return str_replace ( $from, $to, $string ); + } + + ///////////////////////////////////////////////////////////////////////////// // attribute updates ///////////////////////////////////////////////////////////////////////////// /// update given attribute values on given documents in given indexes /// returns amount of updated documents (0 or more) on success, or -1 on failure @@ -1058,9 +1169,9 @@ return $updated; } } // -// $Id: sphinxapi.php 1103 2008-01-24 18:42:57Z shodan $ +// $Id: sphinxapi.php 1163 2008-02-19 21:00:40Z glook $ // ?> \ No newline at end of file