sphinxapi.php 35 KB

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