sphinxapi.php 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250
  1. <?php
  2. //
  3. // $Id$
  4. //
  5. //
  6. // Copyright (c) 2001-2008, Andrew Aksyonoff. All rights reserved.
  7. //
  8. // This program is free software; you can redistribute it and/or modify
  9. // it under the terms of the GNU General Public License. You should have
  10. // received a copy of the GPL license along with this program; if you
  11. // did not, you can find it at http://www.gnu.org/
  12. //
  13. /////////////////////////////////////////////////////////////////////////////
  14. // PHP version of Sphinx searchd client (PHP API)
  15. /////////////////////////////////////////////////////////////////////////////
  16. /// known searchd commands
  17. define ( "SEARCHD_COMMAND_SEARCH", 0 );
  18. define ( "SEARCHD_COMMAND_EXCERPT", 1 );
  19. define ( "SEARCHD_COMMAND_UPDATE", 2 );
  20. define ( "SEARCHD_COMMAND_KEYWORDS",3 );
  21. /// current client-side command implementation versions
  22. define ( "VER_COMMAND_SEARCH", 0x116 );
  23. define ( "VER_COMMAND_EXCERPT", 0x100 );
  24. define ( "VER_COMMAND_UPDATE", 0x102 );
  25. define ( "VER_COMMAND_KEYWORDS", 0x100 );
  26. /// known searchd status codes
  27. define ( "SEARCHD_OK", 0 );
  28. define ( "SEARCHD_ERROR", 1 );
  29. define ( "SEARCHD_RETRY", 2 );
  30. define ( "SEARCHD_WARNING", 3 );
  31. /// known match modes
  32. define ( "SPH_MATCH_ALL", 0 );
  33. define ( "SPH_MATCH_ANY", 1 );
  34. define ( "SPH_MATCH_PHRASE", 2 );
  35. define ( "SPH_MATCH_BOOLEAN", 3 );
  36. define ( "SPH_MATCH_EXTENDED", 4 );
  37. define ( "SPH_MATCH_FULLSCAN", 5 );
  38. define ( "SPH_MATCH_EXTENDED2", 6 ); // extended engine V2 (TEMPORARY, WILL BE REMOVED)
  39. /// known ranking modes (ext2 only)
  40. define ( "SPH_RANK_PROXIMITY_BM25", 0 ); ///< default mode, phrase proximity major factor and BM25 minor one
  41. define ( "SPH_RANK_BM25", 1 ); ///< statistical mode, BM25 ranking only (faster but worse quality)
  42. define ( "SPH_RANK_NONE", 2 ); ///< no ranking, all matches get a weight of 1
  43. define ( "SPH_RANK_WORDCOUNT", 3 ); ///< simple word-count weighting, rank is a weighted sum of per-field keyword occurence counts
  44. define ( "SPH_RANK_PROXIMITY", 4 );
  45. /// known sort modes
  46. define ( "SPH_SORT_RELEVANCE", 0 );
  47. define ( "SPH_SORT_ATTR_DESC", 1 );
  48. define ( "SPH_SORT_ATTR_ASC", 2 );
  49. define ( "SPH_SORT_TIME_SEGMENTS", 3 );
  50. define ( "SPH_SORT_EXTENDED", 4 );
  51. define ( "SPH_SORT_EXPR", 5 );
  52. /// known filter types
  53. define ( "SPH_FILTER_VALUES", 0 );
  54. define ( "SPH_FILTER_RANGE", 1 );
  55. define ( "SPH_FILTER_FLOATRANGE", 2 );
  56. /// known attribute types
  57. define ( "SPH_ATTR_INTEGER", 1 );
  58. define ( "SPH_ATTR_TIMESTAMP", 2 );
  59. define ( "SPH_ATTR_ORDINAL", 3 );
  60. define ( "SPH_ATTR_BOOL", 4 );
  61. define ( "SPH_ATTR_FLOAT", 5 );
  62. define ( "SPH_ATTR_BIGINT", 6 );
  63. define ( "SPH_ATTR_MULTI", 0x40000000 );
  64. /// known grouping functions
  65. define ( "SPH_GROUPBY_DAY", 0 );
  66. define ( "SPH_GROUPBY_WEEK", 1 );
  67. define ( "SPH_GROUPBY_MONTH", 2 );
  68. define ( "SPH_GROUPBY_YEAR", 3 );
  69. define ( "SPH_GROUPBY_ATTR", 4 );
  70. define ( "SPH_GROUPBY_ATTRPAIR", 5 );
  71. /// portably pack numeric to 64 unsigned bits, network order
  72. function sphPack64 ( $v )
  73. {
  74. assert ( is_numeric($v) );
  75. // x64 route
  76. if ( PHP_INT_SIZE>=8 )
  77. {
  78. $i = (int)$v;
  79. return pack ( "NN", $i>>32, $i&((1<<32)-1) );
  80. }
  81. // x32 route, bcmath
  82. $x = "4294967296";
  83. if ( function_exists("bcmul") )
  84. {
  85. $h = bcdiv ( $v, $x, 0 );
  86. $l = bcmod ( $v, $x );
  87. return pack ( "NN", (float)$h, (float)$l ); // conversion to float is intentional; int would lose 31st bit
  88. }
  89. // x32 route, 15 or less decimal digits
  90. // we can use float, because its actually double and has 52 precision bits
  91. if ( strlen($v)<=15 )
  92. {
  93. $f = (float)$v;
  94. $h = (int)($f/$x);
  95. $l = (int)($f-$x*$h);
  96. return pack ( "NN", $h, $l );
  97. }
  98. // x32 route, 16 or more decimal digits
  99. // well, let me know if you *really* need this
  100. die ( "INTERNAL ERROR: packing more than 15-digit numeric on 32-bit PHP is not implemented yet (contact support)" );
  101. }
  102. /// portably unpack 64 signed bits, network order to numeric
  103. function sphUnpack64 ( $v )
  104. {
  105. list($h,$l) = array_values ( unpack ( "N*N*", $v ) );
  106. // x64 route
  107. if ( PHP_INT_SIZE>=8 )
  108. {
  109. if ( $h<0 ) $h += (1<<32); // because php 5.2.2 to 5.2.5 is totally fucked up again
  110. if ( $l<0 ) $l += (1<<32);
  111. return ($h<<32) + $l;
  112. }
  113. // x32 route
  114. $x = "4294967296";
  115. $y = 0;
  116. $p = "";
  117. if ( $h<0 )
  118. {
  119. $h = ~$h;
  120. $l = ~$l;
  121. $y = 1;
  122. $p = "-";
  123. }
  124. $h = sprintf ( "%u", $h );
  125. $l = sprintf ( "%u", $l );
  126. // bcmath
  127. if ( function_exists("bcmul") )
  128. return $p . bcadd ( bcadd ( $l, bcmul ( $x, $h ) ), $y );
  129. // no bcmath, 15 or less decimal digits
  130. // we can use float, because its actually double and has 52 precision bits
  131. if ( $h<1048576 )
  132. {
  133. $f = ((float)$h)*$x + (float)$l + (float)$y;
  134. return $p . sprintf ( "%.0f", $f ); // builtin conversion is only about 39-40 bits precise!
  135. }
  136. // x32 route, 16 or more decimal digits
  137. // well, let me know if you *really* need this
  138. die ( "INTERNAL ERROR: unpacking more than 15-digit numeric on 32-bit PHP is not implemented yet (contact support)" );
  139. }
  140. /// sphinx searchd client class
  141. class SphinxClient
  142. {
  143. var $_host; ///< searchd host (default is "localhost")
  144. var $_port; ///< searchd port (default is 3312)
  145. var $_offset; ///< how many records to seek from result-set start (default is 0)
  146. var $_limit; ///< how many records to return from result-set starting at offset (default is 20)
  147. var $_mode; ///< query matching mode (default is SPH_MATCH_ALL)
  148. var $_weights; ///< per-field weights (default is 1 for all fields)
  149. var $_sort; ///< match sorting mode (default is SPH_SORT_RELEVANCE)
  150. var $_sortby; ///< attribute to sort by (defualt is "")
  151. var $_min_id; ///< min ID to match (default is 0, which means no limit)
  152. var $_max_id; ///< max ID to match (default is 0, which means no limit)
  153. var $_filters; ///< search filters
  154. var $_groupby; ///< group-by attribute name
  155. var $_groupfunc; ///< group-by function (to pre-process group-by attribute value with)
  156. var $_groupsort; ///< group-by sorting clause (to sort groups in result set with)
  157. var $_groupdistinct;///< group-by count-distinct attribute
  158. var $_maxmatches; ///< max matches to retrieve
  159. var $_cutoff; ///< cutoff to stop searching at (default is 0)
  160. var $_retrycount; ///< distributed retries count
  161. var $_retrydelay; ///< distributed retries delay
  162. var $_anchor; ///< geographical anchor point
  163. var $_indexweights; ///< per-index weights
  164. var $_ranker; ///< ranking mode (default is SPH_RANK_PROXIMITY_BM25)
  165. var $_maxquerytime; ///< max query time, milliseconds (default is 0, do not limit)
  166. var $_fieldweights; ///< per-field-name weights
  167. var $_overrides; ///< per-query attribute values overrides
  168. var $_select; ///< select-list (attributes or expressions, with optional aliases)
  169. var $_error; ///< last error message
  170. var $_warning; ///< last warning message
  171. var $_reqs; ///< requests array for multi-query
  172. var $_mbenc; ///< stored mbstring encoding
  173. var $_arrayresult; ///< whether $result["matches"] should be a hash or an array
  174. /////////////////////////////////////////////////////////////////////////////
  175. // common stuff
  176. /////////////////////////////////////////////////////////////////////////////
  177. /// create a new client object and fill defaults
  178. function SphinxClient ()
  179. {
  180. // per-client-object settings
  181. $this->_host = "localhost";
  182. $this->_port = 3312;
  183. // per-query settings
  184. $this->_offset = 0;
  185. $this->_limit = 20;
  186. $this->_mode = SPH_MATCH_ALL;
  187. $this->_weights = array ();
  188. $this->_sort = SPH_SORT_RELEVANCE;
  189. $this->_sortby = "";
  190. $this->_min_id = 0;
  191. $this->_max_id = 0;
  192. $this->_filters = array ();
  193. $this->_groupby = "";
  194. $this->_groupfunc = SPH_GROUPBY_DAY;
  195. $this->_groupsort = "@group desc";
  196. $this->_groupdistinct= "";
  197. $this->_maxmatches = 1000;
  198. $this->_cutoff = 0;
  199. $this->_retrycount = 0;
  200. $this->_retrydelay = 0;
  201. $this->_anchor = array ();
  202. $this->_indexweights= array ();
  203. $this->_ranker = SPH_RANK_PROXIMITY_BM25;
  204. $this->_maxquerytime= 0;
  205. $this->_fieldweights= array();
  206. $this->_overrides = array();
  207. $this->_select = "*";
  208. $this->_error = ""; // per-reply fields (for single-query case)
  209. $this->_warning = "";
  210. $this->_reqs = array (); // requests storage (for multi-query case)
  211. $this->_mbenc = "";
  212. $this->_arrayresult = false;
  213. }
  214. /// get last error message (string)
  215. function GetLastError ()
  216. {
  217. return $this->_error;
  218. }
  219. /// get last warning message (string)
  220. function GetLastWarning ()
  221. {
  222. return $this->_warning;
  223. }
  224. /// set searchd host name (string) and port (integer)
  225. function SetServer ( $host, $port )
  226. {
  227. assert ( is_string($host) );
  228. assert ( is_int($port) );
  229. $this->_host = $host;
  230. $this->_port = $port;
  231. }
  232. /////////////////////////////////////////////////////////////////////////////
  233. /// enter mbstring workaround mode
  234. function _MBPush ()
  235. {
  236. $this->_mbenc = "";
  237. if ( ini_get ( "mbstring.func_overload" ) & 2 )
  238. {
  239. $this->_mbenc = mb_internal_encoding();
  240. mb_internal_encoding ( "latin1" );
  241. }
  242. }
  243. /// leave mbstring workaround mode
  244. function _MBPop ()
  245. {
  246. if ( $this->_mbenc )
  247. mb_internal_encoding ( $this->_mbenc );
  248. }
  249. /// connect to searchd server
  250. function _Connect ()
  251. {
  252. if (!( $fp = @fsockopen ( $this->_host, $this->_port ) ) )
  253. {
  254. $this->_error = "connection to {$this->_host}:{$this->_port} failed";
  255. return false;
  256. }
  257. // check version
  258. list(,$v) = unpack ( "N*", fread ( $fp, 4 ) );
  259. $v = (int)$v;
  260. if ( $v<1 )
  261. {
  262. fclose ( $fp );
  263. $this->_error = "expected searchd protocol version 1+, got version '$v'";
  264. return false;
  265. }
  266. // all ok, send my version
  267. fwrite ( $fp, pack ( "N", 1 ) );
  268. return $fp;
  269. }
  270. /// get and check response packet from searchd server
  271. function _GetResponse ( $fp, $client_ver )
  272. {
  273. $response = "";
  274. $len = 0;
  275. $header = fread ( $fp, 8 );
  276. if ( strlen($header)==8 )
  277. {
  278. list ( $status, $ver, $len ) = array_values ( unpack ( "n2a/Nb", $header ) );
  279. $left = $len;
  280. while ( $left>0 && !feof($fp) )
  281. {
  282. $chunk = fread ( $fp, $left );
  283. if ( $chunk )
  284. {
  285. $response .= $chunk;
  286. $left -= strlen($chunk);
  287. }
  288. }
  289. }
  290. fclose ( $fp );
  291. // check response
  292. $read = strlen ( $response );
  293. if ( !$response || $read!=$len )
  294. {
  295. $this->_error = $len
  296. ? "failed to read searchd response (status=$status, ver=$ver, len=$len, read=$read)"
  297. : "received zero-sized searchd response";
  298. return false;
  299. }
  300. // check status
  301. if ( $status==SEARCHD_WARNING )
  302. {
  303. list(,$wlen) = unpack ( "N*", substr ( $response, 0, 4 ) );
  304. $this->_warning = substr ( $response, 4, $wlen );
  305. return substr ( $response, 4+$wlen );
  306. }
  307. if ( $status==SEARCHD_ERROR )
  308. {
  309. $this->_error = "searchd error: " . substr ( $response, 4 );
  310. return false;
  311. }
  312. if ( $status==SEARCHD_RETRY )
  313. {
  314. $this->_error = "temporary searchd error: " . substr ( $response, 4 );
  315. return false;
  316. }
  317. if ( $status!=SEARCHD_OK )
  318. {
  319. $this->_error = "unknown status code '$status'";
  320. return false;
  321. }
  322. // check version
  323. if ( $ver<$client_ver )
  324. {
  325. $this->_warning = sprintf ( "searchd command v.%d.%d older than client's v.%d.%d, some options might not work",
  326. $ver>>8, $ver&0xff, $client_ver>>8, $client_ver&0xff );
  327. }
  328. return $response;
  329. }
  330. /////////////////////////////////////////////////////////////////////////////
  331. // searching
  332. /////////////////////////////////////////////////////////////////////////////
  333. /// set offset and count into result set,
  334. /// and optionally set max-matches and cutoff limits
  335. function SetLimits ( $offset, $limit, $max=0, $cutoff=0 )
  336. {
  337. assert ( is_int($offset) );
  338. assert ( is_int($limit) );
  339. assert ( $offset>=0 );
  340. assert ( $limit>0 );
  341. assert ( $max>=0 );
  342. $this->_offset = $offset;
  343. $this->_limit = $limit;
  344. if ( $max>0 )
  345. $this->_maxmatches = $max;
  346. if ( $cutoff>0 )
  347. $this->_cutoff = $cutoff;
  348. }
  349. /// set maximum query time, in milliseconds, per-index
  350. /// integer, 0 means "do not limit"
  351. function SetMaxQueryTime ( $max )
  352. {
  353. assert ( is_int($max) );
  354. assert ( $max>=0 );
  355. $this->_maxquerytime = $max;
  356. }
  357. /// set matching mode
  358. function SetMatchMode ( $mode )
  359. {
  360. assert ( $mode==SPH_MATCH_ALL
  361. || $mode==SPH_MATCH_ANY
  362. || $mode==SPH_MATCH_PHRASE
  363. || $mode==SPH_MATCH_BOOLEAN
  364. || $mode==SPH_MATCH_EXTENDED
  365. || $mode==SPH_MATCH_FULLSCAN
  366. || $mode==SPH_MATCH_EXTENDED2 );
  367. $this->_mode = $mode;
  368. }
  369. /// set ranking mode
  370. function SetRankingMode ( $ranker )
  371. {
  372. assert ( $ranker==SPH_RANK_PROXIMITY_BM25
  373. || $ranker==SPH_RANK_BM25
  374. || $ranker==SPH_RANK_NONE
  375. || $ranker==SPH_RANK_WORDCOUNT
  376. || $ranker==SPH_RANK_PROXIMITY );
  377. $this->_ranker = $ranker;
  378. }
  379. /// set matches sorting mode
  380. function SetSortMode ( $mode, $sortby="" )
  381. {
  382. assert (
  383. $mode==SPH_SORT_RELEVANCE ||
  384. $mode==SPH_SORT_ATTR_DESC ||
  385. $mode==SPH_SORT_ATTR_ASC ||
  386. $mode==SPH_SORT_TIME_SEGMENTS ||
  387. $mode==SPH_SORT_EXTENDED ||
  388. $mode==SPH_SORT_EXPR );
  389. assert ( is_string($sortby) );
  390. assert ( $mode==SPH_SORT_RELEVANCE || strlen($sortby)>0 );
  391. $this->_sort = $mode;
  392. $this->_sortby = $sortby;
  393. }
  394. /// bind per-field weights by order
  395. /// DEPRECATED; use SetFieldWeights() instead
  396. function SetWeights ( $weights )
  397. {
  398. assert ( is_array($weights) );
  399. foreach ( $weights as $weight )
  400. assert ( is_int($weight) );
  401. $this->_weights = $weights;
  402. }
  403. /// bind per-field weights by name
  404. function SetFieldWeights ( $weights )
  405. {
  406. assert ( is_array($weights) );
  407. foreach ( $weights as $name=>$weight )
  408. {
  409. assert ( is_string($name) );
  410. assert ( is_int($weight) );
  411. }
  412. $this->_fieldweights = $weights;
  413. }
  414. /// bind per-index weights by name
  415. function SetIndexWeights ( $weights )
  416. {
  417. assert ( is_array($weights) );
  418. foreach ( $weights as $index=>$weight )
  419. {
  420. assert ( is_string($index) );
  421. assert ( is_int($weight) );
  422. }
  423. $this->_indexweights = $weights;
  424. }
  425. /// set IDs range to match
  426. /// only match records if document ID is beetwen $min and $max (inclusive)
  427. function SetIDRange ( $min, $max )
  428. {
  429. assert ( is_numeric($min) );
  430. assert ( is_numeric($max) );
  431. assert ( $min<=$max );
  432. $this->_min_id = $min;
  433. $this->_max_id = $max;
  434. }
  435. /// set values set filter
  436. /// only match records where $attribute value is in given set
  437. function SetFilter ( $attribute, $values, $exclude=false )
  438. {
  439. assert ( is_string($attribute) );
  440. assert ( is_array($values) );
  441. assert ( count($values) );
  442. if ( is_array($values) && count($values) )
  443. {
  444. foreach ( $values as $value )
  445. assert ( is_numeric($value) );
  446. $this->_filters[] = array ( "type"=>SPH_FILTER_VALUES, "attr"=>$attribute, "exclude"=>$exclude, "values"=>$values );
  447. }
  448. }
  449. /// set range filter
  450. /// only match records if $attribute value is beetwen $min and $max (inclusive)
  451. function SetFilterRange ( $attribute, $min, $max, $exclude=false )
  452. {
  453. assert ( is_string($attribute) );
  454. assert ( is_numeric($min) );
  455. assert ( is_numeric($max) );
  456. assert ( $min<=$max );
  457. $this->_filters[] = array ( "type"=>SPH_FILTER_RANGE, "attr"=>$attribute, "exclude"=>$exclude, "min"=>$min, "max"=>$max );
  458. }
  459. /// set float range filter
  460. /// only match records if $attribute value is beetwen $min and $max (inclusive)
  461. function SetFilterFloatRange ( $attribute, $min, $max, $exclude=false )
  462. {
  463. assert ( is_string($attribute) );
  464. assert ( is_float($min) );
  465. assert ( is_float($max) );
  466. assert ( $min<=$max );
  467. $this->_filters[] = array ( "type"=>SPH_FILTER_FLOATRANGE, "attr"=>$attribute, "exclude"=>$exclude, "min"=>$min, "max"=>$max );
  468. }
  469. /// setup anchor point for geosphere distance calculations
  470. /// required to use @geodist in filters and sorting
  471. /// latitude and longitude must be in radians
  472. function SetGeoAnchor ( $attrlat, $attrlong, $lat, $long )
  473. {
  474. assert ( is_string($attrlat) );
  475. assert ( is_string($attrlong) );
  476. assert ( is_float($lat) );
  477. assert ( is_float($long) );
  478. $this->_anchor = array ( "attrlat"=>$attrlat, "attrlong"=>$attrlong, "lat"=>$lat, "long"=>$long );
  479. }
  480. /// set grouping attribute and function
  481. function SetGroupBy ( $attribute, $func, $groupsort="@group desc" )
  482. {
  483. assert ( is_string($attribute) );
  484. assert ( is_string($groupsort) );
  485. assert ( $func==SPH_GROUPBY_DAY
  486. || $func==SPH_GROUPBY_WEEK
  487. || $func==SPH_GROUPBY_MONTH
  488. || $func==SPH_GROUPBY_YEAR
  489. || $func==SPH_GROUPBY_ATTR
  490. || $func==SPH_GROUPBY_ATTRPAIR );
  491. $this->_groupby = $attribute;
  492. $this->_groupfunc = $func;
  493. $this->_groupsort = $groupsort;
  494. }
  495. /// set count-distinct attribute for group-by queries
  496. function SetGroupDistinct ( $attribute )
  497. {
  498. assert ( is_string($attribute) );
  499. $this->_groupdistinct = $attribute;
  500. }
  501. /// set distributed retries count and delay
  502. function SetRetries ( $count, $delay=0 )
  503. {
  504. assert ( is_int($count) && $count>=0 );
  505. assert ( is_int($delay) && $delay>=0 );
  506. $this->_retrycount = $count;
  507. $this->_retrydelay = $delay;
  508. }
  509. /// set result set format (hash or array; hash by default)
  510. /// PHP specific; needed for group-by-MVA result sets that may contain duplicate IDs
  511. function SetArrayResult ( $arrayresult )
  512. {
  513. assert ( is_bool($arrayresult) );
  514. $this->_arrayresult = $arrayresult;
  515. }
  516. /// set attribute values override
  517. /// there can be only one override per attribute
  518. /// $values must be a hash that maps document IDs to attribute values
  519. function SetOverride ( $attrname, $attrtype, $values )
  520. {
  521. assert ( is_string ( $attrname ) );
  522. assert ( in_array ( $attrtype, array ( SPH_ATTR_INTEGER, SPH_ATTR_TIMESTAMP, SPH_ATTR_BOOL, SPH_ATTR_FLOAT, SPH_ATTR_BIGINT ) ) );
  523. assert ( is_array ( $values ) );
  524. $this->_overrides[$attrname] = array ( "attr"=>$attrname, "type"=>$attrtype, "values"=>$values );
  525. }
  526. /// set select-list (attributes or expressions), SQL-like syntax
  527. function SetSelect ( $select )
  528. {
  529. assert ( is_string ( $select ) );
  530. $this->_select = $select;
  531. }
  532. //////////////////////////////////////////////////////////////////////////////
  533. /// clear all filters (for multi-queries)
  534. function ResetFilters ()
  535. {
  536. $this->_filters = array();
  537. $this->_anchor = array();
  538. }
  539. /// clear groupby settings (for multi-queries)
  540. function ResetGroupBy ()
  541. {
  542. $this->_groupby = "";
  543. $this->_groupfunc = SPH_GROUPBY_DAY;
  544. $this->_groupsort = "@group desc";
  545. $this->_groupdistinct= "";
  546. }
  547. /// clear all attribute value overrides (for multi-queries)
  548. function ResetOverrides ()
  549. {
  550. $this->_overrides = array ();
  551. }
  552. //////////////////////////////////////////////////////////////////////////////
  553. /// connect to searchd server, run given search query through given indexes,
  554. /// and return the search results
  555. function Query ( $query, $index="*", $comment="" )
  556. {
  557. assert ( empty($this->_reqs) );
  558. $this->AddQuery ( $query, $index, $comment );
  559. $results = $this->RunQueries ();
  560. if ( !is_array($results) )
  561. return false; // probably network error; error message should be already filled
  562. $this->_error = $results[0]["error"];
  563. $this->_warning = $results[0]["warning"];
  564. if ( $results[0]["status"]==SEARCHD_ERROR )
  565. return false;
  566. else
  567. return $results[0];
  568. }
  569. /// helper to pack floats in network byte order
  570. function _PackFloat ( $f )
  571. {
  572. $t1 = pack ( "f", $f ); // machine order
  573. list(,$t2) = unpack ( "L*", $t1 ); // int in machine order
  574. return pack ( "N", $t2 );
  575. }
  576. /// add query to multi-query batch
  577. /// returns index into results array from RunQueries() call
  578. function AddQuery ( $query, $index="*", $comment="" )
  579. {
  580. // mbstring workaround
  581. $this->_MBPush ();
  582. // build request
  583. $req = pack ( "NNNNN", $this->_offset, $this->_limit, $this->_mode, $this->_ranker, $this->_sort ); // mode and limits
  584. $req .= pack ( "N", strlen($this->_sortby) ) . $this->_sortby;
  585. $req .= pack ( "N", strlen($query) ) . $query; // query itself
  586. $req .= pack ( "N", count($this->_weights) ); // weights
  587. foreach ( $this->_weights as $weight )
  588. $req .= pack ( "N", (int)$weight );
  589. $req .= pack ( "N", strlen($index) ) . $index; // indexes
  590. $req .= pack ( "N", 1 ); // id64 range marker
  591. $req .= sphPack64 ( $this->_min_id ) . sphPack64 ( $this->_max_id ); // id64 range
  592. // filters
  593. $req .= pack ( "N", count($this->_filters) );
  594. foreach ( $this->_filters as $filter )
  595. {
  596. $req .= pack ( "N", strlen($filter["attr"]) ) . $filter["attr"];
  597. $req .= pack ( "N", $filter["type"] );
  598. switch ( $filter["type"] )
  599. {
  600. case SPH_FILTER_VALUES:
  601. $req .= pack ( "N", count($filter["values"]) );
  602. foreach ( $filter["values"] as $value )
  603. $req .= sphPack64 ( $value );
  604. break;
  605. case SPH_FILTER_RANGE:
  606. $req .= sphPack64 ( $filter["min"] ) . sphPack64 ( $filter["max"] );
  607. break;
  608. case SPH_FILTER_FLOATRANGE:
  609. $req .= $this->_PackFloat ( $filter["min"] ) . $this->_PackFloat ( $filter["max"] );
  610. break;
  611. default:
  612. assert ( 0 && "internal error: unhandled filter type" );
  613. }
  614. $req .= pack ( "N", $filter["exclude"] );
  615. }
  616. // group-by clause, max-matches count, group-sort clause, cutoff count
  617. $req .= pack ( "NN", $this->_groupfunc, strlen($this->_groupby) ) . $this->_groupby;
  618. $req .= pack ( "N", $this->_maxmatches );
  619. $req .= pack ( "N", strlen($this->_groupsort) ) . $this->_groupsort;
  620. $req .= pack ( "NNN", $this->_cutoff, $this->_retrycount, $this->_retrydelay );
  621. $req .= pack ( "N", strlen($this->_groupdistinct) ) . $this->_groupdistinct;
  622. // anchor point
  623. if ( empty($this->_anchor) )
  624. {
  625. $req .= pack ( "N", 0 );
  626. } else
  627. {
  628. $a =& $this->_anchor;
  629. $req .= pack ( "N", 1 );
  630. $req .= pack ( "N", strlen($a["attrlat"]) ) . $a["attrlat"];
  631. $req .= pack ( "N", strlen($a["attrlong"]) ) . $a["attrlong"];
  632. $req .= $this->_PackFloat ( $a["lat"] ) . $this->_PackFloat ( $a["long"] );
  633. }
  634. // per-index weights
  635. $req .= pack ( "N", count($this->_indexweights) );
  636. foreach ( $this->_indexweights as $idx=>$weight )
  637. $req .= pack ( "N", strlen($idx) ) . $idx . pack ( "N", $weight );
  638. // max query time
  639. $req .= pack ( "N", $this->_maxquerytime );
  640. // per-field weights
  641. $req .= pack ( "N", count($this->_fieldweights) );
  642. foreach ( $this->_fieldweights as $field=>$weight )
  643. $req .= pack ( "N", strlen($field) ) . $field . pack ( "N", $weight );
  644. // comment
  645. $req .= pack ( "N", strlen($comment) ) . $comment;
  646. // attribute overrides
  647. $req .= pack ( "N", count($this->_overrides) );
  648. foreach ( $this->_overrides as $key => $entry )
  649. {
  650. $req .= pack ( "N", strlen($entry["attr"]) ) . $entry["attr"];
  651. $req .= pack ( "NN", $entry["type"], count($entry["values"]) );
  652. foreach ( $entry["values"] as $id=>$val )
  653. {
  654. assert ( is_numeric($id) );
  655. assert ( is_numeric($val) );
  656. $req .= sphPack64 ( $id );
  657. switch ( $entry["type"] )
  658. {
  659. case SPH_ATTR_FLOAT: $req .= $this->_PackFloat ( $val ); break;
  660. case SPH_ATTR_BIGINT: $req .= sphPack64 ( $val ); break;
  661. default: $req .= pack ( "N", $val ); break;
  662. }
  663. }
  664. }
  665. // select-list
  666. $req .= pack ( "N", strlen($this->_select) ) . $this->_select;
  667. // mbstring workaround
  668. $this->_MBPop ();
  669. // store request to requests array
  670. $this->_reqs[] = $req;
  671. return count($this->_reqs)-1;
  672. }
  673. /// connect to searchd, run queries batch, and return an array of result sets
  674. function RunQueries ()
  675. {
  676. if ( empty($this->_reqs) )
  677. {
  678. $this->_error = "no queries defined, issue AddQuery() first";
  679. return false;
  680. }
  681. // mbstring workaround
  682. $this->_MBPush ();
  683. if (!( $fp = $this->_Connect() ))
  684. {
  685. $this->_MBPop ();
  686. return false;
  687. }
  688. ////////////////////////////
  689. // send query, get response
  690. ////////////////////////////
  691. $nreqs = count($this->_reqs);
  692. $req = join ( "", $this->_reqs );
  693. $len = 4+strlen($req);
  694. $req = pack ( "nnNN", SEARCHD_COMMAND_SEARCH, VER_COMMAND_SEARCH, $len, $nreqs ) . $req; // add header
  695. fwrite ( $fp, $req, $len+8 );
  696. if (!( $response = $this->_GetResponse ( $fp, VER_COMMAND_SEARCH ) ))
  697. {
  698. $this->_MBPop ();
  699. return false;
  700. }
  701. $this->_reqs = array ();
  702. //////////////////
  703. // parse response
  704. //////////////////
  705. $p = 0; // current position
  706. $max = strlen($response); // max position for checks, to protect against broken responses
  707. $results = array ();
  708. for ( $ires=0; $ires<$nreqs && $p<$max; $ires++ )
  709. {
  710. $results[] = array();
  711. $result =& $results[$ires];
  712. $result["error"] = "";
  713. $result["warning"] = "";
  714. // extract status
  715. list(,$status) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4;
  716. $result["status"] = $status;
  717. if ( $status!=SEARCHD_OK )
  718. {
  719. list(,$len) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4;
  720. $message = substr ( $response, $p, $len ); $p += $len;
  721. if ( $status==SEARCHD_WARNING )
  722. {
  723. $result["warning"] = $message;
  724. } else
  725. {
  726. $result["error"] = $message;
  727. continue;
  728. }
  729. }
  730. // read schema
  731. $fields = array ();
  732. $attrs = array ();
  733. list(,$nfields) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4;
  734. while ( $nfields-->0 && $p<$max )
  735. {
  736. list(,$len) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4;
  737. $fields[] = substr ( $response, $p, $len ); $p += $len;
  738. }
  739. $result["fields"] = $fields;
  740. list(,$nattrs) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4;
  741. while ( $nattrs-->0 && $p<$max )
  742. {
  743. list(,$len) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4;
  744. $attr = substr ( $response, $p, $len ); $p += $len;
  745. list(,$type) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4;
  746. $attrs[$attr] = $type;
  747. }
  748. $result["attrs"] = $attrs;
  749. // read match count
  750. list(,$count) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4;
  751. list(,$id64) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4;
  752. // read matches
  753. $idx = -1;
  754. while ( $count-->0 && $p<$max )
  755. {
  756. // index into result array
  757. $idx++;
  758. // parse document id and weight
  759. if ( $id64 )
  760. {
  761. $doc = sphUnpack64 ( substr ( $response, $p, 8 ) ); $p += 8;
  762. list(,$weight) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4;
  763. } else
  764. {
  765. list ( $doc, $weight ) = array_values ( unpack ( "N*N*",
  766. substr ( $response, $p, 8 ) ) );
  767. $p += 8;
  768. if ( PHP_INT_SIZE>=8 )
  769. {
  770. // x64 route, workaround broken unpack() in 5.2.2+
  771. if ( $doc<0 ) $doc += (1<<32);
  772. } else
  773. {
  774. // x32 route, workaround php signed/unsigned braindamage
  775. $doc = sprintf ( "%u", $doc );
  776. }
  777. }
  778. $weight = sprintf ( "%u", $weight );
  779. // create match entry
  780. if ( $this->_arrayresult )
  781. $result["matches"][$idx] = array ( "id"=>$doc, "weight"=>$weight );
  782. else
  783. $result["matches"][$doc]["weight"] = $weight;
  784. // parse and create attributes
  785. $attrvals = array ();
  786. foreach ( $attrs as $attr=>$type )
  787. {
  788. // handle 64bit ints
  789. if ( $type==SPH_ATTR_BIGINT )
  790. {
  791. $attrvals[$attr] = sphUnpack64 ( substr ( $response, $p, 8 ) ); $p += 8;
  792. continue;
  793. }
  794. // handle floats
  795. if ( $type==SPH_ATTR_FLOAT )
  796. {
  797. list(,$uval) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4;
  798. list(,$fval) = unpack ( "f*", pack ( "L", $uval ) );
  799. $attrvals[$attr] = $fval;
  800. continue;
  801. }
  802. // handle everything else as unsigned ints
  803. list(,$val) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4;
  804. if ( $type & SPH_ATTR_MULTI )
  805. {
  806. $attrvals[$attr] = array ();
  807. $nvalues = $val;
  808. while ( $nvalues-->0 && $p<$max )
  809. {
  810. list(,$val) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4;
  811. $attrvals[$attr][] = sprintf ( "%u", $val );
  812. }
  813. } else
  814. {
  815. $attrvals[$attr] = sprintf ( "%u", $val );
  816. }
  817. }
  818. if ( $this->_arrayresult )
  819. $result["matches"][$idx]["attrs"] = $attrvals;
  820. else
  821. $result["matches"][$doc]["attrs"] = $attrvals;
  822. }
  823. list ( $total, $total_found, $msecs, $words ) =
  824. array_values ( unpack ( "N*N*N*N*", substr ( $response, $p, 16 ) ) );
  825. $result["total"] = sprintf ( "%u", $total );
  826. $result["total_found"] = sprintf ( "%u", $total_found );
  827. $result["time"] = sprintf ( "%.3f", $msecs/1000 );
  828. $p += 16;
  829. while ( $words-->0 && $p<$max )
  830. {
  831. list(,$len) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4;
  832. $word = substr ( $response, $p, $len ); $p += $len;
  833. list ( $docs, $hits ) = array_values ( unpack ( "N*N*", substr ( $response, $p, 8 ) ) ); $p += 8;
  834. $result["words"][$word] = array (
  835. "docs"=>sprintf ( "%u", $docs ),
  836. "hits"=>sprintf ( "%u", $hits ) );
  837. }
  838. }
  839. $this->_MBPop ();
  840. return $results;
  841. }
  842. /////////////////////////////////////////////////////////////////////////////
  843. // excerpts generation
  844. /////////////////////////////////////////////////////////////////////////////
  845. /// connect to searchd server, and generate exceprts (snippets)
  846. /// of given documents for given query. returns false on failure,
  847. /// an array of snippets on success
  848. function BuildExcerpts ( $docs, $index, $words, $opts=array() )
  849. {
  850. assert ( is_array($docs) );
  851. assert ( is_string($index) );
  852. assert ( is_string($words) );
  853. assert ( is_array($opts) );
  854. $this->_MBPush ();
  855. if (!( $fp = $this->_Connect() ))
  856. {
  857. $this->_MBPop();
  858. return false;
  859. }
  860. /////////////////
  861. // fixup options
  862. /////////////////
  863. if ( !isset($opts["before_match"]) ) $opts["before_match"] = "<b>";
  864. if ( !isset($opts["after_match"]) ) $opts["after_match"] = "</b>";
  865. if ( !isset($opts["chunk_separator"]) ) $opts["chunk_separator"] = " ... ";
  866. if ( !isset($opts["limit"]) ) $opts["limit"] = 256;
  867. if ( !isset($opts["around"]) ) $opts["around"] = 5;
  868. if ( !isset($opts["exact_phrase"]) ) $opts["exact_phrase"] = false;
  869. if ( !isset($opts["single_passage"]) ) $opts["single_passage"] = false;
  870. if ( !isset($opts["use_boundaries"]) ) $opts["use_boundaries"] = false;
  871. if ( !isset($opts["weight_order"]) ) $opts["weight_order"] = false;
  872. /////////////////
  873. // build request
  874. /////////////////
  875. // v.1.0 req
  876. $flags = 1; // remove spaces
  877. if ( $opts["exact_phrase"] ) $flags |= 2;
  878. if ( $opts["single_passage"] ) $flags |= 4;
  879. if ( $opts["use_boundaries"] ) $flags |= 8;
  880. if ( $opts["weight_order"] ) $flags |= 16;
  881. $req = pack ( "NN", 0, $flags ); // mode=0, flags=$flags
  882. $req .= pack ( "N", strlen($index) ) . $index; // req index
  883. $req .= pack ( "N", strlen($words) ) . $words; // req words
  884. // options
  885. $req .= pack ( "N", strlen($opts["before_match"]) ) . $opts["before_match"];
  886. $req .= pack ( "N", strlen($opts["after_match"]) ) . $opts["after_match"];
  887. $req .= pack ( "N", strlen($opts["chunk_separator"]) ) . $opts["chunk_separator"];
  888. $req .= pack ( "N", (int)$opts["limit"] );
  889. $req .= pack ( "N", (int)$opts["around"] );
  890. // documents
  891. $req .= pack ( "N", count($docs) );
  892. foreach ( $docs as $doc )
  893. {
  894. assert ( is_string($doc) );
  895. $req .= pack ( "N", strlen($doc) ) . $doc;
  896. }
  897. ////////////////////////////
  898. // send query, get response
  899. ////////////////////////////
  900. $len = strlen($req);
  901. $req = pack ( "nnN", SEARCHD_COMMAND_EXCERPT, VER_COMMAND_EXCERPT, $len ) . $req; // add header
  902. $wrote = fwrite ( $fp, $req, $len+8 );
  903. if (!( $response = $this->_GetResponse ( $fp, VER_COMMAND_EXCERPT ) ))
  904. {
  905. $this->_MBPop ();
  906. return false;
  907. }
  908. //////////////////
  909. // parse response
  910. //////////////////
  911. $pos = 0;
  912. $res = array ();
  913. $rlen = strlen($response);
  914. for ( $i=0; $i<count($docs); $i++ )
  915. {
  916. list(,$len) = unpack ( "N*", substr ( $response, $pos, 4 ) );
  917. $pos += 4;
  918. if ( $pos+$len > $rlen )
  919. {
  920. $this->_error = "incomplete reply";
  921. $this->_MBPop ();
  922. return false;
  923. }
  924. $res[] = $len ? substr ( $response, $pos, $len ) : "";
  925. $pos += $len;
  926. }
  927. $this->_MBPop ();
  928. return $res;
  929. }
  930. /////////////////////////////////////////////////////////////////////////////
  931. // keyword generation
  932. /////////////////////////////////////////////////////////////////////////////
  933. /// connect to searchd server, and generate keyword list for a given query
  934. /// returns false on failure,
  935. /// an array of words on success
  936. function BuildKeywords ( $query, $index, $hits )
  937. {
  938. assert ( is_string($query) );
  939. assert ( is_string($index) );
  940. assert ( is_bool($hits) );
  941. $this->_MBPush ();
  942. if (!( $fp = $this->_Connect() ))
  943. {
  944. $this->_MBPop();
  945. return false;
  946. }
  947. /////////////////
  948. // build request
  949. /////////////////
  950. // v.1.0 req
  951. $req = pack ( "N", strlen($query) ) . $query; // req query
  952. $req .= pack ( "N", strlen($index) ) . $index; // req index
  953. $req .= pack ( "N", (int)$hits );
  954. ////////////////////////////
  955. // send query, get response
  956. ////////////////////////////
  957. $len = strlen($req);
  958. $req = pack ( "nnN", SEARCHD_COMMAND_KEYWORDS, VER_COMMAND_KEYWORDS, $len ) . $req; // add header
  959. $wrote = fwrite ( $fp, $req, $len+8 );
  960. if (!( $response = $this->_GetResponse ( $fp, VER_COMMAND_KEYWORDS ) ))
  961. {
  962. $this->_MBPop ();
  963. return false;
  964. }
  965. //////////////////
  966. // parse response
  967. //////////////////
  968. $pos = 0;
  969. $res = array ();
  970. $rlen = strlen($response);
  971. list(,$nwords) = unpack ( "N*", substr ( $response, $pos, 4 ) );
  972. $pos += 4;
  973. for ( $i=0; $i<$nwords; $i++ )
  974. {
  975. list(,$len) = unpack ( "N*", substr ( $response, $pos, 4 ) ); $pos += 4;
  976. $tokenized = $len ? substr ( $response, $pos, $len ) : "";
  977. $pos += $len;
  978. list(,$len) = unpack ( "N*", substr ( $response, $pos, 4 ) ); $pos += 4;
  979. $normalized = $len ? substr ( $response, $pos, $len ) : "";
  980. $pos += $len;
  981. $res[] = array ( "tokenized"=>$tokenized, "normalized"=>$normalized );
  982. if ( $hits )
  983. {
  984. list($ndocs,$nhits) = array_values ( unpack ( "N*N*", substr ( $response, $pos, 8 ) ) );
  985. $pos += 8;
  986. $res [$i]["docs"] = $ndocs;
  987. $res [$i]["hits"] = $nhits;
  988. }
  989. if ( $pos > $rlen )
  990. {
  991. $this->_error = "incomplete reply";
  992. $this->_MBPop ();
  993. return false;
  994. }
  995. }
  996. $this->_MBPop ();
  997. return $res;
  998. }
  999. function EscapeString ( $string )
  1000. {
  1001. $from = array ( '(',')','|','-','!','@','~','"','&', '/' );
  1002. $to = array ( '\(','\)','\|','\-','\!','\@','\~','\"', '\&', '\/' );
  1003. return str_replace ( $from, $to, $string );
  1004. }
  1005. /////////////////////////////////////////////////////////////////////////////
  1006. // attribute updates
  1007. /////////////////////////////////////////////////////////////////////////////
  1008. /// batch update given attributes in given rows in given indexes
  1009. /// returns amount of updated documents (0 or more) on success, or -1 on failure
  1010. function UpdateAttributes ( $index, $attrs, $values, $mva=false )
  1011. {
  1012. // verify everything
  1013. assert ( is_string($index) );
  1014. assert ( is_bool($mva) );
  1015. assert ( is_array($attrs) );
  1016. foreach ( $attrs as $attr )
  1017. assert ( is_string($attr) );
  1018. assert ( is_array($values) );
  1019. foreach ( $values as $id=>$entry )
  1020. {
  1021. assert ( is_numeric($id) );
  1022. assert ( is_array($entry) );
  1023. assert ( count($entry)==count($attrs) );
  1024. foreach ( $entry as $v )
  1025. {
  1026. if ( $mva )
  1027. {
  1028. assert ( is_array($v) );
  1029. foreach ( $v as $vv )
  1030. assert ( is_int($vv) );
  1031. } else
  1032. assert ( is_int($v) );
  1033. }
  1034. }
  1035. // build request
  1036. $req = pack ( "N", strlen($index) ) . $index;
  1037. $req .= pack ( "N", count($attrs) );
  1038. foreach ( $attrs as $attr )
  1039. {
  1040. $req .= pack ( "N", strlen($attr) ) . $attr;
  1041. $req .= pack ( "N", $mva ? 1 : 0 );
  1042. }
  1043. $req .= pack ( "N", count($values) );
  1044. foreach ( $values as $id=>$entry )
  1045. {
  1046. $req .= sphPack64 ( $id );
  1047. foreach ( $entry as $v )
  1048. {
  1049. $req .= pack ( "N", $mva ? count($v) : $v );
  1050. if ( $mva )
  1051. foreach ( $v as $vv )
  1052. $req .= pack ( "N", $vv );
  1053. }
  1054. }
  1055. // connect, send query, get response
  1056. if (!( $fp = $this->_Connect() ))
  1057. return -1;
  1058. $len = strlen($req);
  1059. $req = pack ( "nnN", SEARCHD_COMMAND_UPDATE, VER_COMMAND_UPDATE, $len ) . $req; // add header
  1060. fwrite ( $fp, $req, $len+8 );
  1061. if (!( $response = $this->_GetResponse ( $fp, VER_COMMAND_UPDATE ) ))
  1062. return -1;
  1063. // parse response
  1064. list(,$updated) = unpack ( "N*", substr ( $response, 0, 4 ) );
  1065. return $updated;
  1066. }
  1067. }
  1068. //
  1069. // $Id$
  1070. //
  1071. ?>