sphinxapi.php 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691
  1. <?php
  2. //
  3. // $Id: sphinxapi.php 2758 2011-04-04 11:10:44Z kevg $
  4. //
  5. //
  6. // Copyright (c) 2001-2011, Andrew Aksyonoff
  7. // Copyright (c) 2008-2011, Sphinx Technologies Inc
  8. // All rights reserved
  9. //
  10. // This program is free software; you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License. You should have
  12. // received a copy of the GPL license along with this program; if you
  13. // did not, you can find it at http://www.gnu.org/
  14. //
  15. /////////////////////////////////////////////////////////////////////////////
  16. // PHP version of Sphinx searchd client (PHP API)
  17. /////////////////////////////////////////////////////////////////////////////
  18. /// known searchd commands
  19. define ( "SEARCHD_COMMAND_SEARCH", 0 );
  20. define ( "SEARCHD_COMMAND_EXCERPT", 1 );
  21. define ( "SEARCHD_COMMAND_UPDATE", 2 );
  22. define ( "SEARCHD_COMMAND_KEYWORDS", 3 );
  23. define ( "SEARCHD_COMMAND_PERSIST", 4 );
  24. define ( "SEARCHD_COMMAND_STATUS", 5 );
  25. define ( "SEARCHD_COMMAND_FLUSHATTRS", 7 );
  26. /// current client-side command implementation versions
  27. define ( "VER_COMMAND_SEARCH", 0x118 );
  28. define ( "VER_COMMAND_EXCERPT", 0x103 );
  29. define ( "VER_COMMAND_UPDATE", 0x102 );
  30. define ( "VER_COMMAND_KEYWORDS", 0x100 );
  31. define ( "VER_COMMAND_STATUS", 0x100 );
  32. define ( "VER_COMMAND_QUERY", 0x100 );
  33. define ( "VER_COMMAND_FLUSHATTRS", 0x100 );
  34. /// known searchd status codes
  35. define ( "SEARCHD_OK", 0 );
  36. define ( "SEARCHD_ERROR", 1 );
  37. define ( "SEARCHD_RETRY", 2 );
  38. define ( "SEARCHD_WARNING", 3 );
  39. /// known match modes
  40. define ( "SPH_MATCH_ALL", 0 );
  41. define ( "SPH_MATCH_ANY", 1 );
  42. define ( "SPH_MATCH_PHRASE", 2 );
  43. define ( "SPH_MATCH_BOOLEAN", 3 );
  44. define ( "SPH_MATCH_EXTENDED", 4 );
  45. define ( "SPH_MATCH_FULLSCAN", 5 );
  46. define ( "SPH_MATCH_EXTENDED2", 6 ); // extended engine V2 (TEMPORARY, WILL BE REMOVED)
  47. /// known ranking modes (ext2 only)
  48. define ( "SPH_RANK_PROXIMITY_BM25", 0 ); ///< default mode, phrase proximity major factor and BM25 minor one
  49. define ( "SPH_RANK_BM25", 1 ); ///< statistical mode, BM25 ranking only (faster but worse quality)
  50. define ( "SPH_RANK_NONE", 2 ); ///< no ranking, all matches get a weight of 1
  51. define ( "SPH_RANK_WORDCOUNT", 3 ); ///< simple word-count weighting, rank is a weighted sum of per-field keyword occurence counts
  52. define ( "SPH_RANK_PROXIMITY", 4 );
  53. define ( "SPH_RANK_MATCHANY", 5 );
  54. define ( "SPH_RANK_FIELDMASK", 6 );
  55. define ( "SPH_RANK_SPH04", 7 );
  56. define ( "SPH_RANK_TOTAL", 8 );
  57. /// known sort modes
  58. define ( "SPH_SORT_RELEVANCE", 0 );
  59. define ( "SPH_SORT_ATTR_DESC", 1 );
  60. define ( "SPH_SORT_ATTR_ASC", 2 );
  61. define ( "SPH_SORT_TIME_SEGMENTS", 3 );
  62. define ( "SPH_SORT_EXTENDED", 4 );
  63. define ( "SPH_SORT_EXPR", 5 );
  64. /// known filter types
  65. define ( "SPH_FILTER_VALUES", 0 );
  66. define ( "SPH_FILTER_RANGE", 1 );
  67. define ( "SPH_FILTER_FLOATRANGE", 2 );
  68. /// known attribute types
  69. define ( "SPH_ATTR_INTEGER", 1 );
  70. define ( "SPH_ATTR_TIMESTAMP", 2 );
  71. define ( "SPH_ATTR_ORDINAL", 3 );
  72. define ( "SPH_ATTR_BOOL", 4 );
  73. define ( "SPH_ATTR_FLOAT", 5 );
  74. define ( "SPH_ATTR_BIGINT", 6 );
  75. define ( "SPH_ATTR_STRING", 7 );
  76. define ( "SPH_ATTR_MULTI", 0x40000000 );
  77. /// known grouping functions
  78. define ( "SPH_GROUPBY_DAY", 0 );
  79. define ( "SPH_GROUPBY_WEEK", 1 );
  80. define ( "SPH_GROUPBY_MONTH", 2 );
  81. define ( "SPH_GROUPBY_YEAR", 3 );
  82. define ( "SPH_GROUPBY_ATTR", 4 );
  83. define ( "SPH_GROUPBY_ATTRPAIR", 5 );
  84. // important properties of PHP's integers:
  85. // - always signed (one bit short of PHP_INT_SIZE)
  86. // - conversion from string to int is saturated
  87. // - float is double
  88. // - div converts arguments to floats
  89. // - mod converts arguments to ints
  90. // the packing code below works as follows:
  91. // - when we got an int, just pack it
  92. // if performance is a problem, this is the branch users should aim for
  93. //
  94. // - otherwise, we got a number in string form
  95. // this might be due to different reasons, but we assume that this is
  96. // because it didn't fit into PHP int
  97. //
  98. // - factor the string into high and low ints for packing
  99. // - if we have bcmath, then it is used
  100. // - if we don't, we have to do it manually (this is the fun part)
  101. //
  102. // - x64 branch does factoring using ints
  103. // - x32 (ab)uses floats, since we can't fit unsigned 32-bit number into an int
  104. //
  105. // unpacking routines are pretty much the same.
  106. // - return ints if we can
  107. // - otherwise format number into a string
  108. /// pack 64-bit signed
  109. function sphPackI64 ( $v )
  110. {
  111. assert ( is_numeric($v) );
  112. // x64
  113. if ( PHP_INT_SIZE>=8 )
  114. {
  115. $v = (int)$v;
  116. return pack ( "NN", $v>>32, $v&0xFFFFFFFF );
  117. }
  118. // x32, int
  119. if ( is_int($v) )
  120. return pack ( "NN", $v < 0 ? -1 : 0, $v );
  121. // x32, bcmath
  122. if ( function_exists("bcmul") )
  123. {
  124. if ( bccomp ( $v, 0 ) == -1 )
  125. $v = bcadd ( "18446744073709551616", $v );
  126. $h = bcdiv ( $v, "4294967296", 0 );
  127. $l = bcmod ( $v, "4294967296" );
  128. return pack ( "NN", (float)$h, (float)$l ); // conversion to float is intentional; int would lose 31st bit
  129. }
  130. // x32, no-bcmath
  131. $p = max(0, strlen($v) - 13);
  132. $lo = abs((float)substr($v, $p));
  133. $hi = abs((float)substr($v, 0, $p));
  134. $m = $lo + $hi*1316134912.0; // (10 ^ 13) % (1 << 32) = 1316134912
  135. $q = floor($m/4294967296.0);
  136. $l = $m - ($q*4294967296.0);
  137. $h = $hi*2328.0 + $q; // (10 ^ 13) / (1 << 32) = 2328
  138. if ( $v<0 )
  139. {
  140. if ( $l==0 )
  141. $h = 4294967296.0 - $h;
  142. else
  143. {
  144. $h = 4294967295.0 - $h;
  145. $l = 4294967296.0 - $l;
  146. }
  147. }
  148. return pack ( "NN", $h, $l );
  149. }
  150. /// pack 64-bit unsigned
  151. function sphPackU64 ( $v )
  152. {
  153. assert ( is_numeric($v) );
  154. // x64
  155. if ( PHP_INT_SIZE>=8 )
  156. {
  157. assert ( $v>=0 );
  158. // x64, int
  159. if ( is_int($v) )
  160. return pack ( "NN", $v>>32, $v&0xFFFFFFFF );
  161. // x64, bcmath
  162. if ( function_exists("bcmul") )
  163. {
  164. $h = bcdiv ( $v, 4294967296, 0 );
  165. $l = bcmod ( $v, 4294967296 );
  166. return pack ( "NN", $h, $l );
  167. }
  168. // x64, no-bcmath
  169. $p = max ( 0, strlen($v) - 13 );
  170. $lo = (int)substr ( $v, $p );
  171. $hi = (int)substr ( $v, 0, $p );
  172. $m = $lo + $hi*1316134912;
  173. $l = $m % 4294967296;
  174. $h = $hi*2328 + (int)($m/4294967296);
  175. return pack ( "NN", $h, $l );
  176. }
  177. // x32, int
  178. if ( is_int($v) )
  179. return pack ( "NN", 0, $v );
  180. // x32, bcmath
  181. if ( function_exists("bcmul") )
  182. {
  183. $h = bcdiv ( $v, "4294967296", 0 );
  184. $l = bcmod ( $v, "4294967296" );
  185. return pack ( "NN", (float)$h, (float)$l ); // conversion to float is intentional; int would lose 31st bit
  186. }
  187. // x32, no-bcmath
  188. $p = max(0, strlen($v) - 13);
  189. $lo = (float)substr($v, $p);
  190. $hi = (float)substr($v, 0, $p);
  191. $m = $lo + $hi*1316134912.0;
  192. $q = floor($m / 4294967296.0);
  193. $l = $m - ($q * 4294967296.0);
  194. $h = $hi*2328.0 + $q;
  195. return pack ( "NN", $h, $l );
  196. }
  197. // unpack 64-bit unsigned
  198. function sphUnpackU64 ( $v )
  199. {
  200. list ( $hi, $lo ) = array_values ( unpack ( "N*N*", $v ) );
  201. if ( PHP_INT_SIZE>=8 )
  202. {
  203. if ( $hi<0 ) $hi += (1<<32); // because php 5.2.2 to 5.2.5 is totally fucked up again
  204. if ( $lo<0 ) $lo += (1<<32);
  205. // x64, int
  206. if ( $hi<=2147483647 )
  207. return ($hi<<32) + $lo;
  208. // x64, bcmath
  209. if ( function_exists("bcmul") )
  210. return bcadd ( $lo, bcmul ( $hi, "4294967296" ) );
  211. // x64, no-bcmath
  212. $C = 100000;
  213. $h = ((int)($hi / $C) << 32) + (int)($lo / $C);
  214. $l = (($hi % $C) << 32) + ($lo % $C);
  215. if ( $l>$C )
  216. {
  217. $h += (int)($l / $C);
  218. $l = $l % $C;
  219. }
  220. if ( $h==0 )
  221. return $l;
  222. return sprintf ( "%d%05d", $h, $l );
  223. }
  224. // x32, int
  225. if ( $hi==0 )
  226. {
  227. if ( $lo>0 )
  228. return $lo;
  229. return sprintf ( "%u", $lo );
  230. }
  231. $hi = sprintf ( "%u", $hi );
  232. $lo = sprintf ( "%u", $lo );
  233. // x32, bcmath
  234. if ( function_exists("bcmul") )
  235. return bcadd ( $lo, bcmul ( $hi, "4294967296" ) );
  236. // x32, no-bcmath
  237. $hi = (float)$hi;
  238. $lo = (float)$lo;
  239. $q = floor($hi/10000000.0);
  240. $r = $hi - $q*10000000.0;
  241. $m = $lo + $r*4967296.0;
  242. $mq = floor($m/10000000.0);
  243. $l = $m - $mq*10000000.0;
  244. $h = $q*4294967296.0 + $r*429.0 + $mq;
  245. $h = sprintf ( "%.0f", $h );
  246. $l = sprintf ( "%07.0f", $l );
  247. if ( $h=="0" )
  248. return sprintf( "%.0f", (float)$l );
  249. return $h . $l;
  250. }
  251. // unpack 64-bit signed
  252. function sphUnpackI64 ( $v )
  253. {
  254. list ( $hi, $lo ) = array_values ( unpack ( "N*N*", $v ) );
  255. // x64
  256. if ( PHP_INT_SIZE>=8 )
  257. {
  258. if ( $hi<0 ) $hi += (1<<32); // because php 5.2.2 to 5.2.5 is totally fucked up again
  259. if ( $lo<0 ) $lo += (1<<32);
  260. return ($hi<<32) + $lo;
  261. }
  262. // x32, int
  263. if ( $hi==0 )
  264. {
  265. if ( $lo>0 )
  266. return $lo;
  267. return sprintf ( "%u", $lo );
  268. }
  269. // x32, int
  270. elseif ( $hi==-1 )
  271. {
  272. if ( $lo<0 )
  273. return $lo;
  274. return sprintf ( "%.0f", $lo - 4294967296.0 );
  275. }
  276. $neg = "";
  277. $c = 0;
  278. if ( $hi<0 )
  279. {
  280. $hi = ~$hi;
  281. $lo = ~$lo;
  282. $c = 1;
  283. $neg = "-";
  284. }
  285. $hi = sprintf ( "%u", $hi );
  286. $lo = sprintf ( "%u", $lo );
  287. // x32, bcmath
  288. if ( function_exists("bcmul") )
  289. return $neg . bcadd ( bcadd ( $lo, bcmul ( $hi, "4294967296" ) ), $c );
  290. // x32, no-bcmath
  291. $hi = (float)$hi;
  292. $lo = (float)$lo;
  293. $q = floor($hi/10000000.0);
  294. $r = $hi - $q*10000000.0;
  295. $m = $lo + $r*4967296.0;
  296. $mq = floor($m/10000000.0);
  297. $l = $m - $mq*10000000.0 + $c;
  298. $h = $q*4294967296.0 + $r*429.0 + $mq;
  299. if ( $l==10000000 )
  300. {
  301. $l = 0;
  302. $h += 1;
  303. }
  304. $h = sprintf ( "%.0f", $h );
  305. $l = sprintf ( "%07.0f", $l );
  306. if ( $h=="0" )
  307. return $neg . sprintf( "%.0f", (float)$l );
  308. return $neg . $h . $l;
  309. }
  310. function sphFixUint ( $value )
  311. {
  312. if ( PHP_INT_SIZE>=8 )
  313. {
  314. // x64 route, workaround broken unpack() in 5.2.2+
  315. if ( $value<0 ) $value += (1<<32);
  316. return $value;
  317. }
  318. else
  319. {
  320. // x32 route, workaround php signed/unsigned braindamage
  321. return sprintf ( "%u", $value );
  322. }
  323. }
  324. /// sphinx searchd client class
  325. class SphinxClient
  326. {
  327. var $_host; ///< searchd host (default is "localhost")
  328. var $_port; ///< searchd port (default is 9312)
  329. var $_offset; ///< how many records to seek from result-set start (default is 0)
  330. var $_limit; ///< how many records to return from result-set starting at offset (default is 20)
  331. var $_mode; ///< query matching mode (default is SPH_MATCH_ALL)
  332. var $_weights; ///< per-field weights (default is 1 for all fields)
  333. var $_sort; ///< match sorting mode (default is SPH_SORT_RELEVANCE)
  334. var $_sortby; ///< attribute to sort by (defualt is "")
  335. var $_min_id; ///< min ID to match (default is 0, which means no limit)
  336. var $_max_id; ///< max ID to match (default is 0, which means no limit)
  337. var $_filters; ///< search filters
  338. var $_groupby; ///< group-by attribute name
  339. var $_groupfunc; ///< group-by function (to pre-process group-by attribute value with)
  340. var $_groupsort; ///< group-by sorting clause (to sort groups in result set with)
  341. var $_groupdistinct;///< group-by count-distinct attribute
  342. var $_maxmatches; ///< max matches to retrieve
  343. var $_cutoff; ///< cutoff to stop searching at (default is 0)
  344. var $_retrycount; ///< distributed retries count
  345. var $_retrydelay; ///< distributed retries delay
  346. var $_anchor; ///< geographical anchor point
  347. var $_indexweights; ///< per-index weights
  348. var $_ranker; ///< ranking mode (default is SPH_RANK_PROXIMITY_BM25)
  349. var $_maxquerytime; ///< max query time, milliseconds (default is 0, do not limit)
  350. var $_fieldweights; ///< per-field-name weights
  351. var $_overrides; ///< per-query attribute values overrides
  352. var $_select; ///< select-list (attributes or expressions, with optional aliases)
  353. var $_error; ///< last error message
  354. var $_warning; ///< last warning message
  355. var $_connerror; ///< connection error vs remote error flag
  356. var $_reqs; ///< requests array for multi-query
  357. var $_mbenc; ///< stored mbstring encoding
  358. var $_arrayresult; ///< whether $result["matches"] should be a hash or an array
  359. var $_timeout; ///< connect timeout
  360. /////////////////////////////////////////////////////////////////////////////
  361. // common stuff
  362. /////////////////////////////////////////////////////////////////////////////
  363. /// create a new client object and fill defaults
  364. function SphinxClient ()
  365. {
  366. // per-client-object settings
  367. $this->_host = "localhost";
  368. $this->_port = 9312;
  369. $this->_path = false;
  370. $this->_socket = false;
  371. // per-query settings
  372. $this->_offset = 0;
  373. $this->_limit = 20;
  374. $this->_mode = SPH_MATCH_ALL;
  375. $this->_weights = array ();
  376. $this->_sort = SPH_SORT_RELEVANCE;
  377. $this->_sortby = "";
  378. $this->_min_id = 0;
  379. $this->_max_id = 0;
  380. $this->_filters = array ();
  381. $this->_groupby = "";
  382. $this->_groupfunc = SPH_GROUPBY_DAY;
  383. $this->_groupsort = "@group desc";
  384. $this->_groupdistinct= "";
  385. $this->_maxmatches = 1000;
  386. $this->_cutoff = 0;
  387. $this->_retrycount = 0;
  388. $this->_retrydelay = 0;
  389. $this->_anchor = array ();
  390. $this->_indexweights= array ();
  391. $this->_ranker = SPH_RANK_PROXIMITY_BM25;
  392. $this->_maxquerytime= 0;
  393. $this->_fieldweights= array();
  394. $this->_overrides = array();
  395. $this->_select = "*";
  396. $this->_error = ""; // per-reply fields (for single-query case)
  397. $this->_warning = "";
  398. $this->_connerror = false;
  399. $this->_reqs = array (); // requests storage (for multi-query case)
  400. $this->_mbenc = "";
  401. $this->_arrayresult = false;
  402. $this->_timeout = 0;
  403. }
  404. function __destruct()
  405. {
  406. if ( $this->_socket !== false )
  407. fclose ( $this->_socket );
  408. }
  409. /// get last error message (string)
  410. function GetLastError ()
  411. {
  412. return $this->_error;
  413. }
  414. /// get last warning message (string)
  415. function GetLastWarning ()
  416. {
  417. return $this->_warning;
  418. }
  419. /// get last error flag (to tell network connection errors from searchd errors or broken responses)
  420. function IsConnectError()
  421. {
  422. return $this->_connerror;
  423. }
  424. /// set searchd host name (string) and port (integer)
  425. function SetServer ( $host, $port = 0 )
  426. {
  427. assert ( is_string($host) );
  428. if ( $host[0] == '/')
  429. {
  430. $this->_path = 'unix://' . $host;
  431. return;
  432. }
  433. if ( substr ( $host, 0, 7 )=="unix://" )
  434. {
  435. $this->_path = $host;
  436. return;
  437. }
  438. assert ( is_int($port) );
  439. $this->_host = $host;
  440. $this->_port = $port;
  441. $this->_path = '';
  442. }
  443. /// set server connection timeout (0 to remove)
  444. function SetConnectTimeout ( $timeout )
  445. {
  446. assert ( is_numeric($timeout) );
  447. $this->_timeout = $timeout;
  448. }
  449. function _Send ( $handle, $data, $length )
  450. {
  451. if ( feof($handle) || fwrite ( $handle, $data, $length ) !== $length )
  452. {
  453. $this->_error = 'connection unexpectedly closed (timed out?)';
  454. $this->_connerror = true;
  455. return false;
  456. }
  457. return true;
  458. }
  459. /////////////////////////////////////////////////////////////////////////////
  460. /// enter mbstring workaround mode
  461. function _MBPush ()
  462. {
  463. $this->_mbenc = "";
  464. if ( ini_get ( "mbstring.func_overload" ) & 2 )
  465. {
  466. $this->_mbenc = mb_internal_encoding();
  467. mb_internal_encoding ( "latin1" );
  468. }
  469. }
  470. /// leave mbstring workaround mode
  471. function _MBPop ()
  472. {
  473. if ( $this->_mbenc )
  474. mb_internal_encoding ( $this->_mbenc );
  475. }
  476. /// connect to searchd server
  477. function _Connect ()
  478. {
  479. if ( $this->_socket!==false )
  480. {
  481. // we are in persistent connection mode, so we have a socket
  482. // however, need to check whether it's still alive
  483. if ( [email protected] ( $this->_socket ) )
  484. return $this->_socket;
  485. // force reopen
  486. $this->_socket = false;
  487. }
  488. $errno = 0;
  489. $errstr = "";
  490. $this->_connerror = false;
  491. if ( $this->_path )
  492. {
  493. $host = $this->_path;
  494. $port = 0;
  495. }
  496. else
  497. {
  498. $host = $this->_host;
  499. $port = $this->_port;
  500. }
  501. if ( $this->_timeout<=0 )
  502. $fp = @fsockopen ( $host, $port, $errno, $errstr );
  503. else
  504. $fp = @fsockopen ( $host, $port, $errno, $errstr, $this->_timeout );
  505. if ( !$fp )
  506. {
  507. if ( $this->_path )
  508. $location = $this->_path;
  509. else
  510. $location = "{$this->_host}:{$this->_port}";
  511. $errstr = trim ( $errstr );
  512. $this->_error = "connection to $location failed (errno=$errno, msg=$errstr)";
  513. $this->_connerror = true;
  514. return false;
  515. }
  516. // send my version
  517. // this is a subtle part. we must do it before (!) reading back from searchd.
  518. // because otherwise under some conditions (reported on FreeBSD for instance)
  519. // TCP stack could throttle write-write-read pattern because of Nagle.
  520. if ( !$this->_Send ( $fp, pack ( "N", 1 ), 4 ) )
  521. {
  522. fclose ( $fp );
  523. $this->_error = "failed to send client protocol version";
  524. return false;
  525. }
  526. // check version
  527. list(,$v) = unpack ( "N*", fread ( $fp, 4 ) );
  528. $v = (int)$v;
  529. if ( $v<1 )
  530. {
  531. fclose ( $fp );
  532. $this->_error = "expected searchd protocol version 1+, got version '$v'";
  533. return false;
  534. }
  535. return $fp;
  536. }
  537. /// get and check response packet from searchd server
  538. function _GetResponse ( $fp, $client_ver )
  539. {
  540. $response = "";
  541. $len = 0;
  542. $header = fread ( $fp, 8 );
  543. if ( strlen($header)==8 )
  544. {
  545. list ( $status, $ver, $len ) = array_values ( unpack ( "n2a/Nb", $header ) );
  546. $left = $len;
  547. while ( $left>0 && !feof($fp) )
  548. {
  549. $chunk = fread ( $fp, min ( 8192, $left ) );
  550. if ( $chunk )
  551. {
  552. $response .= $chunk;
  553. $left -= strlen($chunk);
  554. }
  555. }
  556. }
  557. if ( $this->_socket === false )
  558. fclose ( $fp );
  559. // check response
  560. $read = strlen ( $response );
  561. if ( !$response || $read!=$len )
  562. {
  563. $this->_error = $len
  564. ? "failed to read searchd response (status=$status, ver=$ver, len=$len, read=$read)"
  565. : "received zero-sized searchd response";
  566. return false;
  567. }
  568. // check status
  569. if ( $status==SEARCHD_WARNING )
  570. {
  571. list(,$wlen) = unpack ( "N*", substr ( $response, 0, 4 ) );
  572. $this->_warning = substr ( $response, 4, $wlen );
  573. return substr ( $response, 4+$wlen );
  574. }
  575. if ( $status==SEARCHD_ERROR )
  576. {
  577. $this->_error = "searchd error: " . substr ( $response, 4 );
  578. return false;
  579. }
  580. if ( $status==SEARCHD_RETRY )
  581. {
  582. $this->_error = "temporary searchd error: " . substr ( $response, 4 );
  583. return false;
  584. }
  585. if ( $status!=SEARCHD_OK )
  586. {
  587. $this->_error = "unknown status code '$status'";
  588. return false;
  589. }
  590. // check version
  591. if ( $ver<$client_ver )
  592. {
  593. $this->_warning = sprintf ( "searchd command v.%d.%d older than client's v.%d.%d, some options might not work",
  594. $ver>>8, $ver&0xff, $client_ver>>8, $client_ver&0xff );
  595. }
  596. return $response;
  597. }
  598. /////////////////////////////////////////////////////////////////////////////
  599. // searching
  600. /////////////////////////////////////////////////////////////////////////////
  601. /// set offset and count into result set,
  602. /// and optionally set max-matches and cutoff limits
  603. function SetLimits ( $offset, $limit, $max=0, $cutoff=0 )
  604. {
  605. assert ( is_int($offset) );
  606. assert ( is_int($limit) );
  607. assert ( $offset>=0 );
  608. assert ( $limit>0 );
  609. assert ( $max>=0 );
  610. $this->_offset = $offset;
  611. $this->_limit = $limit;
  612. if ( $max>0 )
  613. $this->_maxmatches = $max;
  614. if ( $cutoff>0 )
  615. $this->_cutoff = $cutoff;
  616. }
  617. /// set maximum query time, in milliseconds, per-index
  618. /// integer, 0 means "do not limit"
  619. function SetMaxQueryTime ( $max )
  620. {
  621. assert ( is_int($max) );
  622. assert ( $max>=0 );
  623. $this->_maxquerytime = $max;
  624. }
  625. /// set matching mode
  626. function SetMatchMode ( $mode )
  627. {
  628. assert ( $mode==SPH_MATCH_ALL
  629. || $mode==SPH_MATCH_ANY
  630. || $mode==SPH_MATCH_PHRASE
  631. || $mode==SPH_MATCH_BOOLEAN
  632. || $mode==SPH_MATCH_EXTENDED
  633. || $mode==SPH_MATCH_FULLSCAN
  634. || $mode==SPH_MATCH_EXTENDED2 );
  635. $this->_mode = $mode;
  636. }
  637. /// set ranking mode
  638. function SetRankingMode ( $ranker )
  639. {
  640. assert ( $ranker>=0 && $ranker<SPH_RANK_TOTAL );
  641. $this->_ranker = $ranker;
  642. }
  643. /// set matches sorting mode
  644. function SetSortMode ( $mode, $sortby="" )
  645. {
  646. assert (
  647. $mode==SPH_SORT_RELEVANCE ||
  648. $mode==SPH_SORT_ATTR_DESC ||
  649. $mode==SPH_SORT_ATTR_ASC ||
  650. $mode==SPH_SORT_TIME_SEGMENTS ||
  651. $mode==SPH_SORT_EXTENDED ||
  652. $mode==SPH_SORT_EXPR );
  653. assert ( is_string($sortby) );
  654. assert ( $mode==SPH_SORT_RELEVANCE || strlen($sortby)>0 );
  655. $this->_sort = $mode;
  656. $this->_sortby = $sortby;
  657. }
  658. /// bind per-field weights by order
  659. /// DEPRECATED; use SetFieldWeights() instead
  660. function SetWeights ( $weights )
  661. {
  662. assert ( is_array($weights) );
  663. foreach ( $weights as $weight )
  664. assert ( is_int($weight) );
  665. $this->_weights = $weights;
  666. }
  667. /// bind per-field weights by name
  668. function SetFieldWeights ( $weights )
  669. {
  670. assert ( is_array($weights) );
  671. foreach ( $weights as $name=>$weight )
  672. {
  673. assert ( is_string($name) );
  674. assert ( is_int($weight) );
  675. }
  676. $this->_fieldweights = $weights;
  677. }
  678. /// bind per-index weights by name
  679. function SetIndexWeights ( $weights )
  680. {
  681. assert ( is_array($weights) );
  682. foreach ( $weights as $index=>$weight )
  683. {
  684. assert ( is_string($index) );
  685. assert ( is_int($weight) );
  686. }
  687. $this->_indexweights = $weights;
  688. }
  689. /// set IDs range to match
  690. /// only match records if document ID is beetwen $min and $max (inclusive)
  691. function SetIDRange ( $min, $max )
  692. {
  693. assert ( is_numeric($min) );
  694. assert ( is_numeric($max) );
  695. assert ( $min<=$max );
  696. $this->_min_id = $min;
  697. $this->_max_id = $max;
  698. }
  699. /// set values set filter
  700. /// only match records where $attribute value is in given set
  701. function SetFilter ( $attribute, $values, $exclude=false )
  702. {
  703. assert ( is_string($attribute) );
  704. assert ( is_array($values) );
  705. assert ( count($values) );
  706. if ( is_array($values) && count($values) )
  707. {
  708. foreach ( $values as $value )
  709. assert ( is_numeric($value) );
  710. $this->_filters[] = array ( "type"=>SPH_FILTER_VALUES, "attr"=>$attribute, "exclude"=>$exclude, "values"=>$values );
  711. }
  712. }
  713. /// set range filter
  714. /// only match records if $attribute value is beetwen $min and $max (inclusive)
  715. function SetFilterRange ( $attribute, $min, $max, $exclude=false )
  716. {
  717. assert ( is_string($attribute) );
  718. assert ( is_numeric($min) );
  719. assert ( is_numeric($max) );
  720. assert ( $min<=$max );
  721. $this->_filters[] = array ( "type"=>SPH_FILTER_RANGE, "attr"=>$attribute, "exclude"=>$exclude, "min"=>$min, "max"=>$max );
  722. }
  723. /// set float range filter
  724. /// only match records if $attribute value is beetwen $min and $max (inclusive)
  725. function SetFilterFloatRange ( $attribute, $min, $max, $exclude=false )
  726. {
  727. assert ( is_string($attribute) );
  728. assert ( is_float($min) );
  729. assert ( is_float($max) );
  730. assert ( $min<=$max );
  731. $this->_filters[] = array ( "type"=>SPH_FILTER_FLOATRANGE, "attr"=>$attribute, "exclude"=>$exclude, "min"=>$min, "max"=>$max );
  732. }
  733. /// setup anchor point for geosphere distance calculations
  734. /// required to use @geodist in filters and sorting
  735. /// latitude and longitude must be in radians
  736. function SetGeoAnchor ( $attrlat, $attrlong, $lat, $long )
  737. {
  738. assert ( is_string($attrlat) );
  739. assert ( is_string($attrlong) );
  740. assert ( is_float($lat) );
  741. assert ( is_float($long) );
  742. $this->_anchor = array ( "attrlat"=>$attrlat, "attrlong"=>$attrlong, "lat"=>$lat, "long"=>$long );
  743. }
  744. /// set grouping attribute and function
  745. function SetGroupBy ( $attribute, $func, $groupsort="@group desc" )
  746. {
  747. assert ( is_string($attribute) );
  748. assert ( is_string($groupsort) );
  749. assert ( $func==SPH_GROUPBY_DAY
  750. || $func==SPH_GROUPBY_WEEK
  751. || $func==SPH_GROUPBY_MONTH
  752. || $func==SPH_GROUPBY_YEAR
  753. || $func==SPH_GROUPBY_ATTR
  754. || $func==SPH_GROUPBY_ATTRPAIR );
  755. $this->_groupby = $attribute;
  756. $this->_groupfunc = $func;
  757. $this->_groupsort = $groupsort;
  758. }
  759. /// set count-distinct attribute for group-by queries
  760. function SetGroupDistinct ( $attribute )
  761. {
  762. assert ( is_string($attribute) );
  763. $this->_groupdistinct = $attribute;
  764. }
  765. /// set distributed retries count and delay
  766. function SetRetries ( $count, $delay=0 )
  767. {
  768. assert ( is_int($count) && $count>=0 );
  769. assert ( is_int($delay) && $delay>=0 );
  770. $this->_retrycount = $count;
  771. $this->_retrydelay = $delay;
  772. }
  773. /// set result set format (hash or array; hash by default)
  774. /// PHP specific; needed for group-by-MVA result sets that may contain duplicate IDs
  775. function SetArrayResult ( $arrayresult )
  776. {
  777. assert ( is_bool($arrayresult) );
  778. $this->_arrayresult = $arrayresult;
  779. }
  780. /// set attribute values override
  781. /// there can be only one override per attribute
  782. /// $values must be a hash that maps document IDs to attribute values
  783. function SetOverride ( $attrname, $attrtype, $values )
  784. {
  785. assert ( is_string ( $attrname ) );
  786. assert ( in_array ( $attrtype, array ( SPH_ATTR_INTEGER, SPH_ATTR_TIMESTAMP, SPH_ATTR_BOOL, SPH_ATTR_FLOAT, SPH_ATTR_BIGINT ) ) );
  787. assert ( is_array ( $values ) );
  788. $this->_overrides[$attrname] = array ( "attr"=>$attrname, "type"=>$attrtype, "values"=>$values );
  789. }
  790. /// set select-list (attributes or expressions), SQL-like syntax
  791. function SetSelect ( $select )
  792. {
  793. assert ( is_string ( $select ) );
  794. $this->_select = $select;
  795. }
  796. //////////////////////////////////////////////////////////////////////////////
  797. /// clear all filters (for multi-queries)
  798. function ResetFilters ()
  799. {
  800. $this->_filters = array();
  801. $this->_anchor = array();
  802. }
  803. /// clear groupby settings (for multi-queries)
  804. function ResetGroupBy ()
  805. {
  806. $this->_groupby = "";
  807. $this->_groupfunc = SPH_GROUPBY_DAY;
  808. $this->_groupsort = "@group desc";
  809. $this->_groupdistinct= "";
  810. }
  811. /// clear all attribute value overrides (for multi-queries)
  812. function ResetOverrides ()
  813. {
  814. $this->_overrides = array ();
  815. }
  816. //////////////////////////////////////////////////////////////////////////////
  817. /// connect to searchd server, run given search query through given indexes,
  818. /// and return the search results
  819. function Query ( $query, $index="*", $comment="" )
  820. {
  821. assert ( empty($this->_reqs) );
  822. $this->AddQuery ( $query, $index, $comment );
  823. $results = $this->RunQueries ();
  824. $this->_reqs = array (); // just in case it failed too early
  825. if ( !is_array($results) )
  826. return false; // probably network error; error message should be already filled
  827. $this->_error = $results[0]["error"];
  828. $this->_warning = $results[0]["warning"];
  829. if ( $results[0]["status"]==SEARCHD_ERROR )
  830. return false;
  831. else
  832. return $results[0];
  833. }
  834. /// helper to pack floats in network byte order
  835. function _PackFloat ( $f )
  836. {
  837. $t1 = pack ( "f", $f ); // machine order
  838. list(,$t2) = unpack ( "L*", $t1 ); // int in machine order
  839. return pack ( "N", $t2 );
  840. }
  841. /// add query to multi-query batch
  842. /// returns index into results array from RunQueries() call
  843. function AddQuery ( $query, $index="*", $comment="" )
  844. {
  845. // mbstring workaround
  846. $this->_MBPush ();
  847. // build request
  848. $req = pack ( "NNNNN", $this->_offset, $this->_limit, $this->_mode, $this->_ranker, $this->_sort ); // mode and limits
  849. $req .= pack ( "N", strlen($this->_sortby) ) . $this->_sortby;
  850. $req .= pack ( "N", strlen($query) ) . $query; // query itself
  851. $req .= pack ( "N", count($this->_weights) ); // weights
  852. foreach ( $this->_weights as $weight )
  853. $req .= pack ( "N", (int)$weight );
  854. $req .= pack ( "N", strlen($index) ) . $index; // indexes
  855. $req .= pack ( "N", 1 ); // id64 range marker
  856. $req .= sphPackU64 ( $this->_min_id ) . sphPackU64 ( $this->_max_id ); // id64 range
  857. // filters
  858. $req .= pack ( "N", count($this->_filters) );
  859. foreach ( $this->_filters as $filter )
  860. {
  861. $req .= pack ( "N", strlen($filter["attr"]) ) . $filter["attr"];
  862. $req .= pack ( "N", $filter["type"] );
  863. switch ( $filter["type"] )
  864. {
  865. case SPH_FILTER_VALUES:
  866. $req .= pack ( "N", count($filter["values"]) );
  867. foreach ( $filter["values"] as $value )
  868. $req .= sphPackI64 ( $value );
  869. break;
  870. case SPH_FILTER_RANGE:
  871. $req .= sphPackI64 ( $filter["min"] ) . sphPackI64 ( $filter["max"] );
  872. break;
  873. case SPH_FILTER_FLOATRANGE:
  874. $req .= $this->_PackFloat ( $filter["min"] ) . $this->_PackFloat ( $filter["max"] );
  875. break;
  876. default:
  877. assert ( 0 && "internal error: unhandled filter type" );
  878. }
  879. $req .= pack ( "N", $filter["exclude"] );
  880. }
  881. // group-by clause, max-matches count, group-sort clause, cutoff count
  882. $req .= pack ( "NN", $this->_groupfunc, strlen($this->_groupby) ) . $this->_groupby;
  883. $req .= pack ( "N", $this->_maxmatches );
  884. $req .= pack ( "N", strlen($this->_groupsort) ) . $this->_groupsort;
  885. $req .= pack ( "NNN", $this->_cutoff, $this->_retrycount, $this->_retrydelay );
  886. $req .= pack ( "N", strlen($this->_groupdistinct) ) . $this->_groupdistinct;
  887. // anchor point
  888. if ( empty($this->_anchor) )
  889. {
  890. $req .= pack ( "N", 0 );
  891. } else
  892. {
  893. $a =& $this->_anchor;
  894. $req .= pack ( "N", 1 );
  895. $req .= pack ( "N", strlen($a["attrlat"]) ) . $a["attrlat"];
  896. $req .= pack ( "N", strlen($a["attrlong"]) ) . $a["attrlong"];
  897. $req .= $this->_PackFloat ( $a["lat"] ) . $this->_PackFloat ( $a["long"] );
  898. }
  899. // per-index weights
  900. $req .= pack ( "N", count($this->_indexweights) );
  901. foreach ( $this->_indexweights as $idx=>$weight )
  902. $req .= pack ( "N", strlen($idx) ) . $idx . pack ( "N", $weight );
  903. // max query time
  904. $req .= pack ( "N", $this->_maxquerytime );
  905. // per-field weights
  906. $req .= pack ( "N", count($this->_fieldweights) );
  907. foreach ( $this->_fieldweights as $field=>$weight )
  908. $req .= pack ( "N", strlen($field) ) . $field . pack ( "N", $weight );
  909. // comment
  910. $req .= pack ( "N", strlen($comment) ) . $comment;
  911. // attribute overrides
  912. $req .= pack ( "N", count($this->_overrides) );
  913. foreach ( $this->_overrides as $key => $entry )
  914. {
  915. $req .= pack ( "N", strlen($entry["attr"]) ) . $entry["attr"];
  916. $req .= pack ( "NN", $entry["type"], count($entry["values"]) );
  917. foreach ( $entry["values"] as $id=>$val )
  918. {
  919. assert ( is_numeric($id) );
  920. assert ( is_numeric($val) );
  921. $req .= sphPackU64 ( $id );
  922. switch ( $entry["type"] )
  923. {
  924. case SPH_ATTR_FLOAT: $req .= $this->_PackFloat ( $val ); break;
  925. case SPH_ATTR_BIGINT: $req .= sphPackI64 ( $val ); break;
  926. default: $req .= pack ( "N", $val ); break;
  927. }
  928. }
  929. }
  930. // select-list
  931. $req .= pack ( "N", strlen($this->_select) ) . $this->_select;
  932. // mbstring workaround
  933. $this->_MBPop ();
  934. // store request to requests array
  935. $this->_reqs[] = $req;
  936. return count($this->_reqs)-1;
  937. }
  938. /// connect to searchd, run queries batch, and return an array of result sets
  939. function RunQueries ()
  940. {
  941. if ( empty($this->_reqs) )
  942. {
  943. $this->_error = "no queries defined, issue AddQuery() first";
  944. return false;
  945. }
  946. // mbstring workaround
  947. $this->_MBPush ();
  948. if (!( $fp = $this->_Connect() ))
  949. {
  950. $this->_MBPop ();
  951. return false;
  952. }
  953. // send query, get response
  954. $nreqs = count($this->_reqs);
  955. $req = join ( "", $this->_reqs );
  956. $len = 8+strlen($req);
  957. $req = pack ( "nnNNN", SEARCHD_COMMAND_SEARCH, VER_COMMAND_SEARCH, $len, 0, $nreqs ) . $req; // add header
  958. if ( !( $this->_Send ( $fp, $req, $len+8 ) ) ||
  959. !( $response = $this->_GetResponse ( $fp, VER_COMMAND_SEARCH ) ) )
  960. {
  961. $this->_MBPop ();
  962. return false;
  963. }
  964. // query sent ok; we can reset reqs now
  965. $this->_reqs = array ();
  966. // parse and return response
  967. return $this->_ParseSearchResponse ( $response, $nreqs );
  968. }
  969. /// parse and return search query (or queries) response
  970. function _ParseSearchResponse ( $response, $nreqs )
  971. {
  972. $p = 0; // current position
  973. $max = strlen($response); // max position for checks, to protect against broken responses
  974. $results = array ();
  975. for ( $ires=0; $ires<$nreqs && $p<$max; $ires++ )
  976. {
  977. $results[] = array();
  978. $result =& $results[$ires];
  979. $result["error"] = "";
  980. $result["warning"] = "";
  981. // extract status
  982. list(,$status) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4;
  983. $result["status"] = $status;
  984. if ( $status!=SEARCHD_OK )
  985. {
  986. list(,$len) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4;
  987. $message = substr ( $response, $p, $len ); $p += $len;
  988. if ( $status==SEARCHD_WARNING )
  989. {
  990. $result["warning"] = $message;
  991. } else
  992. {
  993. $result["error"] = $message;
  994. continue;
  995. }
  996. }
  997. // read schema
  998. $fields = array ();
  999. $attrs = array ();
  1000. list(,$nfields) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4;
  1001. while ( $nfields-->0 && $p<$max )
  1002. {
  1003. list(,$len) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4;
  1004. $fields[] = substr ( $response, $p, $len ); $p += $len;
  1005. }
  1006. $result["fields"] = $fields;
  1007. list(,$nattrs) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4;
  1008. while ( $nattrs-->0 && $p<$max )
  1009. {
  1010. list(,$len) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4;
  1011. $attr = substr ( $response, $p, $len ); $p += $len;
  1012. list(,$type) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4;
  1013. $attrs[$attr] = $type;
  1014. }
  1015. $result["attrs"] = $attrs;
  1016. // read match count
  1017. list(,$count) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4;
  1018. list(,$id64) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4;
  1019. // read matches
  1020. $idx = -1;
  1021. while ( $count-->0 && $p<$max )
  1022. {
  1023. // index into result array
  1024. $idx++;
  1025. // parse document id and weight
  1026. if ( $id64 )
  1027. {
  1028. $doc = sphUnpackU64 ( substr ( $response, $p, 8 ) ); $p += 8;
  1029. list(,$weight) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4;
  1030. }
  1031. else
  1032. {
  1033. list ( $doc, $weight ) = array_values ( unpack ( "N*N*",
  1034. substr ( $response, $p, 8 ) ) );
  1035. $p += 8;
  1036. $doc = sphFixUint($doc);
  1037. }
  1038. $weight = sprintf ( "%u", $weight );
  1039. // create match entry
  1040. if ( $this->_arrayresult )
  1041. $result["matches"][$idx] = array ( "id"=>$doc, "weight"=>$weight );
  1042. else
  1043. $result["matches"][$doc]["weight"] = $weight;
  1044. // parse and create attributes
  1045. $attrvals = array ();
  1046. foreach ( $attrs as $attr=>$type )
  1047. {
  1048. // handle 64bit ints
  1049. if ( $type==SPH_ATTR_BIGINT )
  1050. {
  1051. $attrvals[$attr] = sphUnpackI64 ( substr ( $response, $p, 8 ) ); $p += 8;
  1052. continue;
  1053. }
  1054. // handle floats
  1055. if ( $type==SPH_ATTR_FLOAT )
  1056. {
  1057. list(,$uval) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4;
  1058. list(,$fval) = unpack ( "f*", pack ( "L", $uval ) );
  1059. $attrvals[$attr] = $fval;
  1060. continue;
  1061. }
  1062. // handle everything else as unsigned ints
  1063. list(,$val) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4;
  1064. if ( $type & SPH_ATTR_MULTI )
  1065. {
  1066. $attrvals[$attr] = array ();
  1067. $nvalues = $val;
  1068. while ( $nvalues-->0 && $p<$max )
  1069. {
  1070. list(,$val) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4;
  1071. $attrvals[$attr][] = sphFixUint($val);
  1072. }
  1073. } else if ( $type==SPH_ATTR_STRING )
  1074. {
  1075. $attrvals[$attr] = substr ( $response, $p, $val );
  1076. $p += $val;
  1077. } else
  1078. {
  1079. $attrvals[$attr] = sphFixUint($val);
  1080. }
  1081. }
  1082. if ( $this->_arrayresult )
  1083. $result["matches"][$idx]["attrs"] = $attrvals;
  1084. else
  1085. $result["matches"][$doc]["attrs"] = $attrvals;
  1086. }
  1087. list ( $total, $total_found, $msecs, $words ) =
  1088. array_values ( unpack ( "N*N*N*N*", substr ( $response, $p, 16 ) ) );
  1089. $result["total"] = sprintf ( "%u", $total );
  1090. $result["total_found"] = sprintf ( "%u", $total_found );
  1091. $result["time"] = sprintf ( "%.3f", $msecs/1000 );
  1092. $p += 16;
  1093. while ( $words-->0 && $p<$max )
  1094. {
  1095. list(,$len) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4;
  1096. $word = substr ( $response, $p, $len ); $p += $len;
  1097. list ( $docs, $hits ) = array_values ( unpack ( "N*N*", substr ( $response, $p, 8 ) ) ); $p += 8;
  1098. $result["words"][$word] = array (
  1099. "docs"=>sprintf ( "%u", $docs ),
  1100. "hits"=>sprintf ( "%u", $hits ) );
  1101. }
  1102. }
  1103. $this->_MBPop ();
  1104. return $results;
  1105. }
  1106. /////////////////////////////////////////////////////////////////////////////
  1107. // excerpts generation
  1108. /////////////////////////////////////////////////////////////////////////////
  1109. /// connect to searchd server, and generate exceprts (snippets)
  1110. /// of given documents for given query. returns false on failure,
  1111. /// an array of snippets on success
  1112. function BuildExcerpts ( $docs, $index, $words, $opts=array() )
  1113. {
  1114. assert ( is_array($docs) );
  1115. assert ( is_string($index) );
  1116. assert ( is_string($words) );
  1117. assert ( is_array($opts) );
  1118. $this->_MBPush ();
  1119. if (!( $fp = $this->_Connect() ))
  1120. {
  1121. $this->_MBPop();
  1122. return false;
  1123. }
  1124. /////////////////
  1125. // fixup options
  1126. /////////////////
  1127. if ( !isset($opts["before_match"]) ) $opts["before_match"] = "<b>";
  1128. if ( !isset($opts["after_match"]) ) $opts["after_match"] = "</b>";
  1129. if ( !isset($opts["chunk_separator"]) ) $opts["chunk_separator"] = " ... ";
  1130. if ( !isset($opts["limit"]) ) $opts["limit"] = 256;
  1131. if ( !isset($opts["limit_passages"]) ) $opts["limit_passages"] = 0;
  1132. if ( !isset($opts["limit_words"]) ) $opts["limit_words"] = 0;
  1133. if ( !isset($opts["around"]) ) $opts["around"] = 5;
  1134. if ( !isset($opts["exact_phrase"]) ) $opts["exact_phrase"] = false;
  1135. if ( !isset($opts["single_passage"]) ) $opts["single_passage"] = false;
  1136. if ( !isset($opts["use_boundaries"]) ) $opts["use_boundaries"] = false;
  1137. if ( !isset($opts["weight_order"]) ) $opts["weight_order"] = false;
  1138. if ( !isset($opts["query_mode"]) ) $opts["query_mode"] = false;
  1139. if ( !isset($opts["force_all_words"]) ) $opts["force_all_words"] = false;
  1140. if ( !isset($opts["start_passage_id"]) ) $opts["start_passage_id"] = 1;
  1141. if ( !isset($opts["load_files"]) ) $opts["load_files"] = false;
  1142. if ( !isset($opts["html_strip_mode"]) ) $opts["html_strip_mode"] = "index";
  1143. if ( !isset($opts["allow_empty"]) ) $opts["allow_empty"] = false;
  1144. if ( !isset($opts["passage_boundary"]) ) $opts["passage_boundary"] = "none";
  1145. if ( !isset($opts["emit_zones"]) ) $opts["emit_zones"] = false;
  1146. /////////////////
  1147. // build request
  1148. /////////////////
  1149. // v.1.2 req
  1150. $flags = 1; // remove spaces
  1151. if ( $opts["exact_phrase"] ) $flags |= 2;
  1152. if ( $opts["single_passage"] ) $flags |= 4;
  1153. if ( $opts["use_boundaries"] ) $flags |= 8;
  1154. if ( $opts["weight_order"] ) $flags |= 16;
  1155. if ( $opts["query_mode"] ) $flags |= 32;
  1156. if ( $opts["force_all_words"] ) $flags |= 64;
  1157. if ( $opts["load_files"] ) $flags |= 128;
  1158. if ( $opts["allow_empty"] ) $flags |= 256;
  1159. if ( $opts["emit_zones"] ) $flags |= 512;
  1160. $req = pack ( "NN", 0, $flags ); // mode=0, flags=$flags
  1161. $req .= pack ( "N", strlen($index) ) . $index; // req index
  1162. $req .= pack ( "N", strlen($words) ) . $words; // req words
  1163. // options
  1164. $req .= pack ( "N", strlen($opts["before_match"]) ) . $opts["before_match"];
  1165. $req .= pack ( "N", strlen($opts["after_match"]) ) . $opts["after_match"];
  1166. $req .= pack ( "N", strlen($opts["chunk_separator"]) ) . $opts["chunk_separator"];
  1167. $req .= pack ( "NN", (int)$opts["limit"], (int)$opts["around"] );
  1168. $req .= pack ( "NNN", (int)$opts["limit_passages"], (int)$opts["limit_words"], (int)$opts["start_passage_id"] ); // v.1.2
  1169. $req .= pack ( "N", strlen($opts["html_strip_mode"]) ) . $opts["html_strip_mode"];
  1170. $req .= pack ( "N", strlen($opts["passage_boundary"]) ) . $opts["passage_boundary"];
  1171. // documents
  1172. $req .= pack ( "N", count($docs) );
  1173. foreach ( $docs as $doc )
  1174. {
  1175. assert ( is_string($doc) );
  1176. $req .= pack ( "N", strlen($doc) ) . $doc;
  1177. }
  1178. ////////////////////////////
  1179. // send query, get response
  1180. ////////////////////////////
  1181. $len = strlen($req);
  1182. $req = pack ( "nnN", SEARCHD_COMMAND_EXCERPT, VER_COMMAND_EXCERPT, $len ) . $req; // add header
  1183. if ( !( $this->_Send ( $fp, $req, $len+8 ) ) ||
  1184. !( $response = $this->_GetResponse ( $fp, VER_COMMAND_EXCERPT ) ) )
  1185. {
  1186. $this->_MBPop ();
  1187. return false;
  1188. }
  1189. //////////////////
  1190. // parse response
  1191. //////////////////
  1192. $pos = 0;
  1193. $res = array ();
  1194. $rlen = strlen($response);
  1195. for ( $i=0; $i<count($docs); $i++ )
  1196. {
  1197. list(,$len) = unpack ( "N*", substr ( $response, $pos, 4 ) );
  1198. $pos += 4;
  1199. if ( $pos+$len > $rlen )
  1200. {
  1201. $this->_error = "incomplete reply";
  1202. $this->_MBPop ();
  1203. return false;
  1204. }
  1205. $res[] = $len ? substr ( $response, $pos, $len ) : "";
  1206. $pos += $len;
  1207. }
  1208. $this->_MBPop ();
  1209. return $res;
  1210. }
  1211. /////////////////////////////////////////////////////////////////////////////
  1212. // keyword generation
  1213. /////////////////////////////////////////////////////////////////////////////
  1214. /// connect to searchd server, and generate keyword list for a given query
  1215. /// returns false on failure,
  1216. /// an array of words on success
  1217. function BuildKeywords ( $query, $index, $hits )
  1218. {
  1219. assert ( is_string($query) );
  1220. assert ( is_string($index) );
  1221. assert ( is_bool($hits) );
  1222. $this->_MBPush ();
  1223. if (!( $fp = $this->_Connect() ))
  1224. {
  1225. $this->_MBPop();
  1226. return false;
  1227. }
  1228. /////////////////
  1229. // build request
  1230. /////////////////
  1231. // v.1.0 req
  1232. $req = pack ( "N", strlen($query) ) . $query; // req query
  1233. $req .= pack ( "N", strlen($index) ) . $index; // req index
  1234. $req .= pack ( "N", (int)$hits );
  1235. ////////////////////////////
  1236. // send query, get response
  1237. ////////////////////////////
  1238. $len = strlen($req);
  1239. $req = pack ( "nnN", SEARCHD_COMMAND_KEYWORDS, VER_COMMAND_KEYWORDS, $len ) . $req; // add header
  1240. if ( !( $this->_Send ( $fp, $req, $len+8 ) ) ||
  1241. !( $response = $this->_GetResponse ( $fp, VER_COMMAND_KEYWORDS ) ) )
  1242. {
  1243. $this->_MBPop ();
  1244. return false;
  1245. }
  1246. //////////////////
  1247. // parse response
  1248. //////////////////
  1249. $pos = 0;
  1250. $res = array ();
  1251. $rlen = strlen($response);
  1252. list(,$nwords) = unpack ( "N*", substr ( $response, $pos, 4 ) );
  1253. $pos += 4;
  1254. for ( $i=0; $i<$nwords; $i++ )
  1255. {
  1256. list(,$len) = unpack ( "N*", substr ( $response, $pos, 4 ) ); $pos += 4;
  1257. $tokenized = $len ? substr ( $response, $pos, $len ) : "";
  1258. $pos += $len;
  1259. list(,$len) = unpack ( "N*", substr ( $response, $pos, 4 ) ); $pos += 4;
  1260. $normalized = $len ? substr ( $response, $pos, $len ) : "";
  1261. $pos += $len;
  1262. $res[] = array ( "tokenized"=>$tokenized, "normalized"=>$normalized );
  1263. if ( $hits )
  1264. {
  1265. list($ndocs,$nhits) = array_values ( unpack ( "N*N*", substr ( $response, $pos, 8 ) ) );
  1266. $pos += 8;
  1267. $res [$i]["docs"] = $ndocs;
  1268. $res [$i]["hits"] = $nhits;
  1269. }
  1270. if ( $pos > $rlen )
  1271. {
  1272. $this->_error = "incomplete reply";
  1273. $this->_MBPop ();
  1274. return false;
  1275. }
  1276. }
  1277. $this->_MBPop ();
  1278. return $res;
  1279. }
  1280. function EscapeString ( $string )
  1281. {
  1282. $from = array ( '\\', '(',')','|','-','!','@','~','"','&', '/', '^', '$', '=' );
  1283. $to = array ( '\\\\', '\(','\)','\|','\-','\!','\@','\~','\"', '\&', '\/', '\^', '\$', '\=' );
  1284. return str_replace ( $from, $to, $string );
  1285. }
  1286. /////////////////////////////////////////////////////////////////////////////
  1287. // attribute updates
  1288. /////////////////////////////////////////////////////////////////////////////
  1289. /// batch update given attributes in given rows in given indexes
  1290. /// returns amount of updated documents (0 or more) on success, or -1 on failure
  1291. function UpdateAttributes ( $index, $attrs, $values, $mva=false )
  1292. {
  1293. // verify everything
  1294. assert ( is_string($index) );
  1295. assert ( is_bool($mva) );
  1296. assert ( is_array($attrs) );
  1297. foreach ( $attrs as $attr )
  1298. assert ( is_string($attr) );
  1299. assert ( is_array($values) );
  1300. foreach ( $values as $id=>$entry )
  1301. {
  1302. assert ( is_numeric($id) );
  1303. assert ( is_array($entry) );
  1304. assert ( count($entry)==count($attrs) );
  1305. foreach ( $entry as $v )
  1306. {
  1307. if ( $mva )
  1308. {
  1309. assert ( is_array($v) );
  1310. foreach ( $v as $vv )
  1311. assert ( is_int($vv) );
  1312. } else
  1313. assert ( is_int($v) );
  1314. }
  1315. }
  1316. // build request
  1317. $this->_MBPush ();
  1318. $req = pack ( "N", strlen($index) ) . $index;
  1319. $req .= pack ( "N", count($attrs) );
  1320. foreach ( $attrs as $attr )
  1321. {
  1322. $req .= pack ( "N", strlen($attr) ) . $attr;
  1323. $req .= pack ( "N", $mva ? 1 : 0 );
  1324. }
  1325. $req .= pack ( "N", count($values) );
  1326. foreach ( $values as $id=>$entry )
  1327. {
  1328. $req .= sphPackU64 ( $id );
  1329. foreach ( $entry as $v )
  1330. {
  1331. $req .= pack ( "N", $mva ? count($v) : $v );
  1332. if ( $mva )
  1333. foreach ( $v as $vv )
  1334. $req .= pack ( "N", $vv );
  1335. }
  1336. }
  1337. // connect, send query, get response
  1338. if (!( $fp = $this->_Connect() ))
  1339. {
  1340. $this->_MBPop ();
  1341. return -1;
  1342. }
  1343. $len = strlen($req);
  1344. $req = pack ( "nnN", SEARCHD_COMMAND_UPDATE, VER_COMMAND_UPDATE, $len ) . $req; // add header
  1345. if ( !$this->_Send ( $fp, $req, $len+8 ) )
  1346. {
  1347. $this->_MBPop ();
  1348. return -1;
  1349. }
  1350. if (!( $response = $this->_GetResponse ( $fp, VER_COMMAND_UPDATE ) ))
  1351. {
  1352. $this->_MBPop ();
  1353. return -1;
  1354. }
  1355. // parse response
  1356. list(,$updated) = unpack ( "N*", substr ( $response, 0, 4 ) );
  1357. $this->_MBPop ();
  1358. return $updated;
  1359. }
  1360. /////////////////////////////////////////////////////////////////////////////
  1361. // persistent connections
  1362. /////////////////////////////////////////////////////////////////////////////
  1363. function Open()
  1364. {
  1365. if ( $this->_socket !== false )
  1366. {
  1367. $this->_error = 'already connected';
  1368. return false;
  1369. }
  1370. if ( !$fp = $this->_Connect() )
  1371. return false;
  1372. // command, command version = 0, body length = 4, body = 1
  1373. $req = pack ( "nnNN", SEARCHD_COMMAND_PERSIST, 0, 4, 1 );
  1374. if ( !$this->_Send ( $fp, $req, 12 ) )
  1375. return false;
  1376. $this->_socket = $fp;
  1377. return true;
  1378. }
  1379. function Close()
  1380. {
  1381. if ( $this->_socket === false )
  1382. {
  1383. $this->_error = 'not connected';
  1384. return false;
  1385. }
  1386. fclose ( $this->_socket );
  1387. $this->_socket = false;
  1388. return true;
  1389. }
  1390. //////////////////////////////////////////////////////////////////////////
  1391. // status
  1392. //////////////////////////////////////////////////////////////////////////
  1393. function Status ()
  1394. {
  1395. $this->_MBPush ();
  1396. if (!( $fp = $this->_Connect() ))
  1397. {
  1398. $this->_MBPop();
  1399. return false;
  1400. }
  1401. $req = pack ( "nnNN", SEARCHD_COMMAND_STATUS, VER_COMMAND_STATUS, 4, 1 ); // len=4, body=1
  1402. if ( !( $this->_Send ( $fp, $req, 12 ) ) ||
  1403. !( $response = $this->_GetResponse ( $fp, VER_COMMAND_STATUS ) ) )
  1404. {
  1405. $this->_MBPop ();
  1406. return false;
  1407. }
  1408. $res = substr ( $response, 4 ); // just ignore length, error handling, etc
  1409. $p = 0;
  1410. list ( $rows, $cols ) = array_values ( unpack ( "N*N*", substr ( $response, $p, 8 ) ) ); $p += 8;
  1411. $res = array();
  1412. for ( $i=0; $i<$rows; $i++ )
  1413. for ( $j=0; $j<$cols; $j++ )
  1414. {
  1415. list(,$len) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4;
  1416. $res[$i][] = substr ( $response, $p, $len ); $p += $len;
  1417. }
  1418. $this->_MBPop ();
  1419. return $res;
  1420. }
  1421. //////////////////////////////////////////////////////////////////////////
  1422. // flush
  1423. //////////////////////////////////////////////////////////////////////////
  1424. function FlushAttributes ()
  1425. {
  1426. $this->_MBPush ();
  1427. if (!( $fp = $this->_Connect() ))
  1428. {
  1429. $this->_MBPop();
  1430. return -1;
  1431. }
  1432. $req = pack ( "nnN", SEARCHD_COMMAND_FLUSHATTRS, VER_COMMAND_FLUSHATTRS, 0 ); // len=0
  1433. if ( !( $this->_Send ( $fp, $req, 8 ) ) ||
  1434. !( $response = $this->_GetResponse ( $fp, VER_COMMAND_FLUSHATTRS ) ) )
  1435. {
  1436. $this->_MBPop ();
  1437. return -1;
  1438. }
  1439. $tag = -1;
  1440. if ( strlen($response)==4 )
  1441. list(,$tag) = unpack ( "N*", $response );
  1442. else
  1443. $this->_error = "unexpected response length";
  1444. $this->_MBPop ();
  1445. return $tag;
  1446. }
  1447. }
  1448. //
  1449. // $Id: sphinxapi.php 2758 2011-04-04 11:10:44Z kevg $
  1450. //