XS.php 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780
  1. <?php
  2. /**
  3. * Xunsearch PHP-SDK 引导文件
  4. *
  5. * 这个文件是由开发工具中的 'build lite' 指令智能合并类定义的源码文件
  6. * 并删除所有注释而自动生成的。
  7. *
  8. * 当您编写搜索项目时,先通过 require 引入该文件即可使用所有的 PHP-SDK
  9. * 功能。合并的主要目的是便于拷贝,只要复制这个库文件即可,而不用拷贝一
  10. * 大堆文件。详细文档请阅读 {@link:http://www.xunsearch.com/doc/php/}
  11. *
  12. * 切勿手动修改本文件!生成时间:2016/08/25 13:44:37
  13. *
  14. * @author hightman
  15. * @link http://www.xunsearch.com/
  16. * @copyright Copyright &copy; 2011 HangZhou YunSheng Network Technology Co., Ltd.
  17. * @license http://www.xunsearch.com/license/
  18. * @version $Id$
  19. */
  20. define('XS_CMD_NONE', 0);
  21. define('XS_CMD_DEFAULT', XS_CMD_NONE);
  22. define('XS_CMD_PROTOCOL', 20110707);
  23. define('XS_CMD_USE', 1);
  24. define('XS_CMD_HELLO', 1);
  25. define('XS_CMD_DEBUG', 2);
  26. define('XS_CMD_TIMEOUT', 3);
  27. define('XS_CMD_QUIT', 4);
  28. define('XS_CMD_INDEX_SET_DB', 32);
  29. define('XS_CMD_INDEX_GET_DB', 33);
  30. define('XS_CMD_INDEX_SUBMIT', 34);
  31. define('XS_CMD_INDEX_REMOVE', 35);
  32. define('XS_CMD_INDEX_EXDATA', 36);
  33. define('XS_CMD_INDEX_CLEAN_DB', 37);
  34. define('XS_CMD_DELETE_PROJECT', 38);
  35. define('XS_CMD_INDEX_COMMIT', 39);
  36. define('XS_CMD_INDEX_REBUILD', 40);
  37. define('XS_CMD_FLUSH_LOGGING', 41);
  38. define('XS_CMD_INDEX_SYNONYMS', 42);
  39. define('XS_CMD_INDEX_USER_DICT', 43);
  40. define('XS_CMD_SEARCH_DB_TOTAL', 64);
  41. define('XS_CMD_SEARCH_GET_TOTAL', 65);
  42. define('XS_CMD_SEARCH_GET_RESULT', 66);
  43. define('XS_CMD_SEARCH_SET_DB', XS_CMD_INDEX_SET_DB);
  44. define('XS_CMD_SEARCH_GET_DB', XS_CMD_INDEX_GET_DB);
  45. define('XS_CMD_SEARCH_ADD_DB', 68);
  46. define('XS_CMD_SEARCH_FINISH', 69);
  47. define('XS_CMD_SEARCH_DRAW_TPOOL', 70);
  48. define('XS_CMD_SEARCH_ADD_LOG', 71);
  49. define('XS_CMD_SEARCH_GET_SYNONYMS', 72);
  50. define('XS_CMD_SEARCH_SCWS_GET', 73);
  51. define('XS_CMD_QUERY_GET_STRING', 96);
  52. define('XS_CMD_QUERY_GET_TERMS', 97);
  53. define('XS_CMD_QUERY_GET_CORRECTED', 98);
  54. define('XS_CMD_QUERY_GET_EXPANDED', 99);
  55. define('XS_CMD_OK', 128);
  56. define('XS_CMD_ERR', 129);
  57. define('XS_CMD_SEARCH_RESULT_DOC', 140);
  58. define('XS_CMD_SEARCH_RESULT_FIELD', 141);
  59. define('XS_CMD_SEARCH_RESULT_FACETS', 142);
  60. define('XS_CMD_SEARCH_RESULT_MATCHED', 143);
  61. define('XS_CMD_DOC_TERM', 160);
  62. define('XS_CMD_DOC_VALUE', 161);
  63. define('XS_CMD_DOC_INDEX', 162);
  64. define('XS_CMD_INDEX_REQUEST', 163);
  65. define('XS_CMD_IMPORT_HEADER', 191);
  66. define('XS_CMD_SEARCH_SET_SORT', 192);
  67. define('XS_CMD_SEARCH_SET_CUT', 193);
  68. define('XS_CMD_SEARCH_SET_NUMERIC', 194);
  69. define('XS_CMD_SEARCH_SET_COLLAPSE', 195);
  70. define('XS_CMD_SEARCH_KEEPALIVE', 196);
  71. define('XS_CMD_SEARCH_SET_FACETS', 197);
  72. define('XS_CMD_SEARCH_SCWS_SET', 198);
  73. define('XS_CMD_SEARCH_SET_CUTOFF', 199);
  74. define('XS_CMD_SEARCH_SET_MISC', 200);
  75. define('XS_CMD_QUERY_INIT', 224);
  76. define('XS_CMD_QUERY_PARSE', 225);
  77. define('XS_CMD_QUERY_TERM', 226);
  78. define('XS_CMD_QUERY_TERMS', 232);
  79. define('XS_CMD_QUERY_RANGEPROC', 227);
  80. define('XS_CMD_QUERY_RANGE', 228);
  81. define('XS_CMD_QUERY_VALCMP', 229);
  82. define('XS_CMD_QUERY_PREFIX', 230);
  83. define('XS_CMD_QUERY_PARSEFLAG', 231);
  84. define('XS_CMD_SORT_TYPE_RELEVANCE', 0);
  85. define('XS_CMD_SORT_TYPE_DOCID', 1);
  86. define('XS_CMD_SORT_TYPE_VALUE', 2);
  87. define('XS_CMD_SORT_TYPE_MULTI', 3);
  88. define('XS_CMD_SORT_TYPE_GEODIST', 4);
  89. define('XS_CMD_SORT_TYPE_MASK', 0x3f);
  90. define('XS_CMD_SORT_FLAG_RELEVANCE', 0x40);
  91. define('XS_CMD_SORT_FLAG_ASCENDING', 0x80);
  92. define('XS_CMD_QUERY_OP_AND', 0);
  93. define('XS_CMD_QUERY_OP_OR', 1);
  94. define('XS_CMD_QUERY_OP_AND_NOT', 2);
  95. define('XS_CMD_QUERY_OP_XOR', 3);
  96. define('XS_CMD_QUERY_OP_AND_MAYBE', 4);
  97. define('XS_CMD_QUERY_OP_FILTER', 5);
  98. define('XS_CMD_RANGE_PROC_STRING', 0);
  99. define('XS_CMD_RANGE_PROC_DATE', 1);
  100. define('XS_CMD_RANGE_PROC_NUMBER', 2);
  101. define('XS_CMD_VALCMP_LE', 0);
  102. define('XS_CMD_VALCMP_GE', 1);
  103. define('XS_CMD_PARSE_FLAG_BOOLEAN', 1);
  104. define('XS_CMD_PARSE_FLAG_PHRASE', 2);
  105. define('XS_CMD_PARSE_FLAG_LOVEHATE', 4);
  106. define('XS_CMD_PARSE_FLAG_BOOLEAN_ANY_CASE', 8);
  107. define('XS_CMD_PARSE_FLAG_WILDCARD', 16);
  108. define('XS_CMD_PARSE_FLAG_PURE_NOT', 32);
  109. define('XS_CMD_PARSE_FLAG_PARTIAL', 64);
  110. define('XS_CMD_PARSE_FLAG_SPELLING_CORRECTION', 128);
  111. define('XS_CMD_PARSE_FLAG_SYNONYM', 256);
  112. define('XS_CMD_PARSE_FLAG_AUTO_SYNONYMS', 512);
  113. define('XS_CMD_PARSE_FLAG_AUTO_MULTIWORD_SYNONYMS', 1536);
  114. define('XS_CMD_PREFIX_NORMAL', 0);
  115. define('XS_CMD_PREFIX_BOOLEAN', 1);
  116. define('XS_CMD_INDEX_WEIGHT_MASK', 0x3f);
  117. define('XS_CMD_INDEX_FLAG_WITHPOS', 0x40);
  118. define('XS_CMD_INDEX_FLAG_SAVEVALUE', 0x80);
  119. define('XS_CMD_INDEX_FLAG_CHECKSTEM', 0x80);
  120. define('XS_CMD_VALUE_FLAG_NUMERIC', 0x80);
  121. define('XS_CMD_INDEX_REQUEST_ADD', 0);
  122. define('XS_CMD_INDEX_REQUEST_UPDATE', 1);
  123. define('XS_CMD_INDEX_SYNONYMS_ADD', 0);
  124. define('XS_CMD_INDEX_SYNONYMS_DEL', 1);
  125. define('XS_CMD_SEARCH_MISC_SYN_SCALE', 1);
  126. define('XS_CMD_SEARCH_MISC_MATCHED_TERM', 2);
  127. define('XS_CMD_SCWS_GET_VERSION', 1);
  128. define('XS_CMD_SCWS_GET_RESULT', 2);
  129. define('XS_CMD_SCWS_GET_TOPS', 3);
  130. define('XS_CMD_SCWS_HAS_WORD', 4);
  131. define('XS_CMD_SCWS_GET_MULTI', 5);
  132. define('XS_CMD_SCWS_SET_IGNORE', 50);
  133. define('XS_CMD_SCWS_SET_MULTI', 51);
  134. define('XS_CMD_SCWS_SET_DUALITY', 52);
  135. define('XS_CMD_SCWS_SET_DICT', 53);
  136. define('XS_CMD_SCWS_ADD_DICT', 54);
  137. define('XS_CMD_ERR_UNKNOWN', 600);
  138. define('XS_CMD_ERR_NOPROJECT', 401);
  139. define('XS_CMD_ERR_TOOLONG', 402);
  140. define('XS_CMD_ERR_INVALIDCHAR', 403);
  141. define('XS_CMD_ERR_EMPTY', 404);
  142. define('XS_CMD_ERR_NOACTION', 405);
  143. define('XS_CMD_ERR_RUNNING', 406);
  144. define('XS_CMD_ERR_REBUILDING', 407);
  145. define('XS_CMD_ERR_WRONGPLACE', 450);
  146. define('XS_CMD_ERR_WRONGFORMAT', 451);
  147. define('XS_CMD_ERR_EMPTYQUERY', 452);
  148. define('XS_CMD_ERR_TIMEOUT', 501);
  149. define('XS_CMD_ERR_IOERR', 502);
  150. define('XS_CMD_ERR_NOMEM', 503);
  151. define('XS_CMD_ERR_BUSY', 504);
  152. define('XS_CMD_ERR_UNIMP', 505);
  153. define('XS_CMD_ERR_NODB', 506);
  154. define('XS_CMD_ERR_DBLOCKED', 507);
  155. define('XS_CMD_ERR_CREATE_HOME', 508);
  156. define('XS_CMD_ERR_INVALID_HOME', 509);
  157. define('XS_CMD_ERR_REMOVE_HOME', 510);
  158. define('XS_CMD_ERR_REMOVE_DB', 511);
  159. define('XS_CMD_ERR_STAT', 512);
  160. define('XS_CMD_ERR_OPEN_FILE', 513);
  161. define('XS_CMD_ERR_TASK_CANCELED', 514);
  162. define('XS_CMD_ERR_XAPIAN', 515);
  163. define('XS_CMD_OK_INFO', 200);
  164. define('XS_CMD_OK_PROJECT', 201);
  165. define('XS_CMD_OK_QUERY_STRING', 202);
  166. define('XS_CMD_OK_DB_TOTAL', 203);
  167. define('XS_CMD_OK_QUERY_TERMS', 204);
  168. define('XS_CMD_OK_QUERY_CORRECTED', 205);
  169. define('XS_CMD_OK_SEARCH_TOTAL', 206);
  170. define('XS_CMD_OK_RESULT_BEGIN', XS_CMD_OK_SEARCH_TOTAL);
  171. define('XS_CMD_OK_RESULT_END', 207);
  172. define('XS_CMD_OK_TIMEOUT_SET', 208);
  173. define('XS_CMD_OK_FINISHED', 209);
  174. define('XS_CMD_OK_LOGGED', 210);
  175. define('XS_CMD_OK_RQST_FINISHED', 250);
  176. define('XS_CMD_OK_DB_CHANGED', 251);
  177. define('XS_CMD_OK_DB_INFO', 252);
  178. define('XS_CMD_OK_DB_CLEAN', 253);
  179. define('XS_CMD_OK_PROJECT_ADD', 254);
  180. define('XS_CMD_OK_PROJECT_DEL', 255);
  181. define('XS_CMD_OK_DB_COMMITED', 256);
  182. define('XS_CMD_OK_DB_REBUILD', 257);
  183. define('XS_CMD_OK_LOG_FLUSHED', 258);
  184. define('XS_CMD_OK_DICT_SAVED', 259);
  185. define('XS_CMD_OK_RESULT_SYNONYMS', 280);
  186. define('XS_CMD_OK_SCWS_RESULT', 290);
  187. define('XS_CMD_OK_SCWS_TOPS', 291);
  188. define('XS_PACKAGE_BUGREPORT', "http://www.xunsearch.com/bugs");
  189. define('XS_PACKAGE_NAME', "xunsearch");
  190. define('XS_PACKAGE_TARNAME', "xunsearch");
  191. define('XS_PACKAGE_URL', "");
  192. define('XS_PACKAGE_VERSION', "1.4.10");
  193. define('XS_LIB_ROOT', dirname(__FILE__));
  194. class XSException extends Exception
  195. {
  196. public function __toString()
  197. {
  198. $string = '[' . __CLASS__ . '] ' . $this->getRelPath($this->getFile()) . '(' . $this->getLine() . '): ';
  199. $string .= $this->getMessage() . ($this->getCode() > 0 ? '(S#' . $this->getCode() . ')' : '');
  200. return $string;
  201. }
  202. public static function getRelPath($file)
  203. {
  204. $from = getcwd();
  205. $file = realpath($file);
  206. if (is_dir($file)) {
  207. $pos = false;
  208. $to = $file;
  209. } else {
  210. $pos = strrpos($file, '/');
  211. $to = substr($file, 0, $pos);
  212. }
  213. for ($rel = '';; $rel .= '../') {
  214. if ($from === $to) {
  215. break;
  216. }
  217. if ($from === dirname($from)) {
  218. $rel .= substr($to, 1);
  219. break;
  220. }
  221. if (!strncmp($from . '/', $to, strlen($from) + 1)) {
  222. $rel .= substr($to, strlen($from) + 1);
  223. break;
  224. }
  225. $from = dirname($from);
  226. }
  227. if (substr($rel, -1, 1) === '/') {
  228. $rel = substr($rel, 0, -1);
  229. }
  230. if ($pos !== false) {
  231. $rel .= substr($file, $pos);
  232. }
  233. return $rel;
  234. }
  235. }
  236. class XSErrorException extends XSException
  237. {
  238. private $_file, $_line;
  239. public function __construct($code, $message, $file, $line, $previous = null)
  240. {
  241. $this->_file = $file;
  242. $this->_line = $line;
  243. if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
  244. parent::__construct($message, $code, $previous);
  245. } else {
  246. parent::__construct($message, $code);
  247. }
  248. }
  249. public function __toString()
  250. {
  251. $string = '[' . __CLASS__ . '] ' . $this->getRelPath($this->_file) . '(' . $this->_line . '): ';
  252. $string .= $this->getMessage() . '(' . $this->getCode() . ')';
  253. return $string;
  254. }
  255. }
  256. class XSComponent
  257. {
  258. public function __get($name)
  259. {
  260. $getter = 'get' . $name;
  261. if (method_exists($this, $getter)) {
  262. return $this->$getter();
  263. }
  264. $msg = method_exists($this, 'set' . $name) ? 'Write-only' : 'Undefined';
  265. $msg .= ' property: ' . get_class($this) . '::$' . $name;
  266. throw new XSException($msg);
  267. }
  268. public function __set($name, $value)
  269. {
  270. $setter = 'set' . $name;
  271. if (method_exists($this, $setter)) {
  272. return $this->$setter($value);
  273. }
  274. $msg = method_exists($this, 'get' . $name) ? 'Read-only' : 'Undefined';
  275. $msg .= ' property: ' . get_class($this) . '::$' . $name;
  276. throw new XSException($msg);
  277. }
  278. public function __isset($name)
  279. {
  280. return method_exists($this, 'get' . $name);
  281. }
  282. public function __unset($name)
  283. {
  284. $this->__set($name, null);
  285. }
  286. }
  287. class XS extends XSComponent
  288. {
  289. private $_index;
  290. private $_search;
  291. private $_scws;
  292. private $_scheme, $_bindScheme;
  293. private $_config;
  294. private static $_lastXS;
  295. public function __construct($file)
  296. {
  297. $this->loadIniFile($file);
  298. self::$_lastXS = $this;
  299. }
  300. public function __destruct()
  301. {
  302. $this->_index = null;
  303. $this->_search = null;
  304. }
  305. public static function getLastXS()
  306. {
  307. return self::$_lastXS;
  308. }
  309. public function getScheme()
  310. {
  311. return $this->_scheme;
  312. }
  313. public function setScheme(XSFieldScheme $fs)
  314. {
  315. $fs->checkValid(true);
  316. $this->_scheme = $fs;
  317. if ($this->_search !== null) {
  318. $this->_search->markResetScheme();
  319. }
  320. }
  321. public function restoreScheme()
  322. {
  323. if ($this->_scheme !== $this->_bindScheme) {
  324. $this->_scheme = $this->_bindScheme;
  325. if ($this->_search !== null) {
  326. $this->_search->markResetScheme(true);
  327. }
  328. }
  329. }
  330. public function getConfig()
  331. {
  332. return $this->_config;
  333. }
  334. public function getName()
  335. {
  336. return $this->_config['project.name'];
  337. }
  338. public function setName($name)
  339. {
  340. $this->_config['project.name'] = $name;
  341. }
  342. public function getDefaultCharset()
  343. {
  344. return isset($this->_config['project.default_charset']) ?
  345. strtoupper($this->_config['project.default_charset']) : 'UTF-8';
  346. }
  347. public function setDefaultCharset($charset)
  348. {
  349. $this->_config['project.default_charset'] = strtoupper($charset);
  350. }
  351. public function getIndex()
  352. {
  353. if ($this->_index === null) {
  354. $adds = array();
  355. $conn = isset($this->_config['server.index']) ? $this->_config['server.index'] : 8383;
  356. if (($pos = strpos($conn, ';')) !== false) {
  357. $adds = explode(';', substr($conn, $pos + 1));
  358. $conn = substr($conn, 0, $pos);
  359. }
  360. $this->_index = new XSIndex($conn, $this);
  361. $this->_index->setTimeout(0);
  362. foreach ($adds as $conn) {
  363. $conn = trim($conn);
  364. if ($conn !== '') {
  365. $this->_index->addServer($conn)->setTimeout(0);
  366. }
  367. }
  368. }
  369. return $this->_index;
  370. }
  371. public function getSearch()
  372. {
  373. if ($this->_search === null) {
  374. $conns = array();
  375. if (!isset($this->_config['server.search'])) {
  376. $conns[] = 8384;
  377. } else {
  378. foreach (explode(';', $this->_config['server.search']) as $conn) {
  379. $conn = trim($conn);
  380. if ($conn !== '') {
  381. $conns[] = $conn;
  382. }
  383. }
  384. }
  385. if (count($conns) > 1) {
  386. shuffle($conns);
  387. }
  388. for ($i = 0; $i < count($conns); $i++) {
  389. try {
  390. $this->_search = new XSSearch($conns[$i], $this);
  391. $this->_search->setCharset($this->getDefaultCharset());
  392. return $this->_search;
  393. } catch (XSException $e) {
  394. if (($i + 1) === count($conns)) {
  395. throw $e;
  396. }
  397. }
  398. }
  399. }
  400. return $this->_search;
  401. }
  402. public function getScwsServer()
  403. {
  404. if ($this->_scws === null) {
  405. $conn = isset($this->_config['server.search']) ? $this->_config['server.search'] : 8384;
  406. $this->_scws = new XSServer($conn, $this);
  407. }
  408. return $this->_scws;
  409. }
  410. public function getFieldId()
  411. {
  412. return $this->_scheme->getFieldId();
  413. }
  414. public function getFieldTitle()
  415. {
  416. return $this->_scheme->getFieldTitle();
  417. }
  418. public function getFieldBody()
  419. {
  420. return $this->_scheme->getFieldBody();
  421. }
  422. public function getField($name, $throw = true)
  423. {
  424. return $this->_scheme->getField($name, $throw);
  425. }
  426. public function getAllFields()
  427. {
  428. return $this->_scheme->getAllFields();
  429. }
  430. public static function autoload($name)
  431. {
  432. $file = XS_LIB_ROOT . '/' . $name . '.class.php';
  433. if (file_exists($file)) {
  434. require_once $file;
  435. }
  436. }
  437. public static function convert($data, $to, $from)
  438. {
  439. if ($to == $from) {
  440. return $data;
  441. }
  442. if (is_array($data)) {
  443. foreach ($data as $key => $value) {
  444. $data[$key] = self::convert($value, $to, $from);
  445. }
  446. return $data;
  447. }
  448. if (is_string($data) && preg_match('/[\x81-\xfe]/', $data)) {
  449. if (function_exists('mb_convert_encoding')) {
  450. return mb_convert_encoding($data, $to, $from);
  451. } elseif (function_exists('iconv')) {
  452. return iconv($from, $to . '//TRANSLIT', $data);
  453. } else {
  454. throw new XSException('Cann\'t find the mbstring or iconv extension to convert encoding');
  455. }
  456. }
  457. return $data;
  458. }
  459. public static function geoDistance($lon1, $lat1, $lon2, $lat2)
  460. {
  461. $dx = $lon1 - $lon2;
  462. $dy = $lat1 - $lat2;
  463. $b = ($lat1 + $lat2) / 2;
  464. $lx = 6367000.0 * deg2rad($dx) * cos(deg2rad($b));
  465. $ly = 6367000.0 * deg2rad($dy);
  466. return sqrt($lx * $lx + $ly * $ly);
  467. }
  468. private function parseIniData($data)
  469. {
  470. $ret = array();
  471. $cur = &$ret;
  472. $lines = explode("\n", $data);
  473. foreach ($lines as $line) {
  474. if ($line === '' || $line[0] == ';' || $line[0] == '#') {
  475. continue;
  476. }
  477. $line = trim($line);
  478. if ($line === '') {
  479. continue;
  480. }
  481. if ($line[0] === '[' && substr($line, -1, 1) === ']') {
  482. $sec = substr($line, 1, -1);
  483. $ret[$sec] = array();
  484. $cur = &$ret[$sec];
  485. continue;
  486. }
  487. if (($pos = strpos($line, '=')) === false) {
  488. continue;
  489. }
  490. $key = trim(substr($line, 0, $pos));
  491. $value = trim(substr($line, $pos + 1), " '\t\"");
  492. $cur[$key] = $value;
  493. }
  494. return $ret;
  495. }
  496. private function loadIniFile($file)
  497. {
  498. $cache = false;
  499. $cache_write = '';
  500. if (strlen($file) < 255 && file_exists($file)) {
  501. $cache_key = md5(__CLASS__ . '::ini::' . realpath($file));
  502. if (function_exists('apc_fetch')) {
  503. $cache = apc_fetch($cache_key);
  504. $cache_write = 'apc_store';
  505. } elseif (function_exists('xcache_get') && php_sapi_name() !== 'cli') {
  506. $cache = xcache_get($cache_key);
  507. $cache_write = 'xcache_set';
  508. } elseif (function_exists('eaccelerator_get')) {
  509. $cache = eaccelerator_get($cache_key);
  510. $cache_write = 'eaccelerator_put';
  511. }
  512. if ($cache && isset($cache['mtime']) && isset($cache['scheme'])
  513. && filemtime($file) <= $cache['mtime']) {
  514. $this->_scheme = $this->_bindScheme = unserialize($cache['scheme']);
  515. $this->_config = $cache['config'];
  516. return;
  517. }
  518. $data = file_get_contents($file);
  519. } else {
  520. $data = file_get_contents($file);
  521. $file = substr(md5($file), 8, 8) . '.ini';
  522. }
  523. $this->_config = $this->parseIniData($data);
  524. if ($this->_config === false) {
  525. throw new XSException('Failed to parse project config file/string: \'' . substr($file, 0, 10) . '...\'');
  526. }
  527. $scheme = new XSFieldScheme;
  528. foreach ($this->_config as $key => $value) {
  529. if (is_array($value)) {
  530. $scheme->addField($key, $value);
  531. }
  532. }
  533. $scheme->checkValid(true);
  534. if (!isset($this->_config['project.name'])) {
  535. $this->_config['project.name'] = basename($file, '.ini');
  536. }
  537. $this->_scheme = $this->_bindScheme = $scheme;
  538. if ($cache_write != '') {
  539. $cache['mtime'] = filemtime($file);
  540. $cache['scheme'] = serialize($this->_scheme);
  541. $cache['config'] = $this->_config;
  542. call_user_func($cache_write, $cache_key, $cache);
  543. }
  544. }
  545. }
  546. function xsErrorHandler($errno, $error, $file, $line)
  547. {
  548. if (($errno & ini_get('error_reporting')) && !strncmp($file, XS_LIB_ROOT, strlen(XS_LIB_ROOT))) {
  549. throw new XSErrorException($errno, $error, $file, $line);
  550. }
  551. return false;
  552. }
  553. set_error_handler('xsErrorHandler');
  554. class XSDocument implements ArrayAccess, IteratorAggregate
  555. {
  556. private $_data;
  557. private $_terms, $_texts;
  558. private $_charset, $_meta;
  559. private static $_resSize = 20;
  560. private static $_resFormat = 'Idocid/Irank/Iccount/ipercent/fweight';
  561. public function __construct($p = null, $d = null)
  562. {
  563. $this->_data = array();
  564. if (is_array($p)) {
  565. $this->_data = $p;
  566. } elseif (is_string($p)) {
  567. if (strlen($p) !== self::$_resSize) {
  568. $this->setCharset($p);
  569. return;
  570. }
  571. $this->_meta = unpack(self::$_resFormat, $p);
  572. }
  573. if ($d !== null && is_string($d)) {
  574. $this->setCharset($d);
  575. }
  576. }
  577. public function __get($name)
  578. {
  579. if (!isset($this->_data[$name])) {
  580. return null;
  581. }
  582. return $this->autoConvert($this->_data[$name]);
  583. }
  584. public function __set($name, $value)
  585. {
  586. if ($this->_meta !== null) {
  587. throw new XSException('Magick property of result document is read-only');
  588. }
  589. $this->setField($name, $value);
  590. }
  591. public function __call($name, $args)
  592. {
  593. if ($this->_meta !== null) {
  594. $name = strtolower($name);
  595. if (isset($this->_meta[$name])) {
  596. return $this->_meta[$name];
  597. }
  598. }
  599. throw new XSException('Call to undefined method `' . get_class($this) . '::' . $name . '()\'');
  600. }
  601. public function getCharset()
  602. {
  603. return $this->_charset;
  604. }
  605. public function setCharset($charset)
  606. {
  607. $this->_charset = strtoupper($charset);
  608. if ($this->_charset == 'UTF8') {
  609. $this->_charset = 'UTF-8';
  610. }
  611. }
  612. public function getFields()
  613. {
  614. return $this->_data;
  615. }
  616. public function setFields($data)
  617. {
  618. if ($data === null) {
  619. $this->_data = array();
  620. $this->_meta = $this->_terms = $this->_texts = null;
  621. } else {
  622. $this->_data = array_merge($this->_data, $data);
  623. }
  624. }
  625. public function setField($name, $value, $isMeta = false)
  626. {
  627. if ($value === null) {
  628. if ($isMeta) {
  629. unset($this->_meta[$name]);
  630. } else {
  631. unset($this->_data[$name]);
  632. }
  633. } else {
  634. if ($isMeta) {
  635. $this->_meta[$name] = $value;
  636. } else {
  637. $this->_data[$name] = $value;
  638. }
  639. }
  640. }
  641. public function f($name)
  642. {
  643. return $this->__get(strval($name));
  644. }
  645. public function getAddTerms($field)
  646. {
  647. $field = strval($field);
  648. if ($this->_terms === null || !isset($this->_terms[$field])) {
  649. return null;
  650. }
  651. $terms = array();
  652. foreach ($this->_terms[$field] as $term => $weight) {
  653. $term = $this->autoConvert($term);
  654. $terms[$term] = $weight;
  655. }
  656. return $terms;
  657. }
  658. public function getAddIndex($field)
  659. {
  660. $field = strval($field);
  661. if ($this->_texts === null || !isset($this->_texts[$field])) {
  662. return null;
  663. }
  664. return $this->autoConvert($this->_texts[$field]);
  665. }
  666. public function addTerm($field, $term, $weight = 1)
  667. {
  668. $field = strval($field);
  669. if (!is_array($this->_terms)) {
  670. $this->_terms = array();
  671. }
  672. if (!isset($this->_terms[$field])) {
  673. $this->_terms[$field] = array($term => $weight);
  674. } elseif (!isset($this->_terms[$field][$term])) {
  675. $this->_terms[$field][$term] = $weight;
  676. } else {
  677. $this->_terms[$field][$term] += $weight;
  678. }
  679. }
  680. public function addIndex($field, $text)
  681. {
  682. $field = strval($field);
  683. if (!is_array($this->_texts)) {
  684. $this->_texts = array();
  685. }
  686. if (!isset($this->_texts[$field])) {
  687. $this->_texts[$field] = strval($text);
  688. } else {
  689. $this->_texts[$field] .= "\n" . strval($text);
  690. }
  691. }
  692. public function getIterator()
  693. {
  694. if ($this->_charset !== null && $this->_charset !== 'UTF-8') {
  695. $from = $this->_meta === null ? $this->_charset : 'UTF-8';
  696. $to = $this->_meta === null ? 'UTF-8' : $this->_charset;
  697. return new ArrayIterator(XS::convert($this->_data, $to, $from));
  698. }
  699. return new ArrayIterator($this->_data);
  700. }
  701. public function offsetExists($name)
  702. {
  703. return isset($this->_data[$name]);
  704. }
  705. public function offsetGet($name)
  706. {
  707. return $this->__get($name);
  708. }
  709. public function offsetSet($name, $value)
  710. {
  711. if (!is_null($name)) {
  712. $this->__set(strval($name), $value);
  713. }
  714. }
  715. public function offsetUnset($name)
  716. {
  717. unset($this->_data[$name]);
  718. }
  719. public function beforeSubmit(XSIndex $index)
  720. {
  721. if ($this->_charset === null) {
  722. $this->_charset = $index->xs->getDefaultCharset();
  723. }
  724. return true;
  725. }
  726. public function afterSubmit($index)
  727. {
  728. }
  729. private function autoConvert($value)
  730. {
  731. if ($this->_charset === null || $this->_charset == 'UTF-8'
  732. || !is_string($value) || !preg_match('/[\x81-\xfe]/', $value)) {
  733. return $value;
  734. }
  735. $from = $this->_meta === null ? $this->_charset : 'UTF-8';
  736. $to = $this->_meta === null ? 'UTF-8' : $this->_charset;
  737. return XS::convert($value, $to, $from);
  738. }
  739. }
  740. class XSFieldScheme implements IteratorAggregate
  741. {
  742. const MIXED_VNO = 255;
  743. private $_fields = array();
  744. private $_typeMap = array();
  745. private $_vnoMap = array();
  746. private static $_logger;
  747. public function __toString()
  748. {
  749. $str = '';
  750. foreach ($this->_fields as $field) {
  751. $str .= $field->toConfig() . "\n";
  752. }
  753. return $str;
  754. }
  755. public function getFieldId()
  756. {
  757. if (isset($this->_typeMap[XSFieldMeta::TYPE_ID])) {
  758. $name = $this->_typeMap[XSFieldMeta::TYPE_ID];
  759. return $this->_fields[$name];
  760. }
  761. return false;
  762. }
  763. public function getFieldTitle()
  764. {
  765. if (isset($this->_typeMap[XSFieldMeta::TYPE_TITLE])) {
  766. $name = $this->_typeMap[XSFieldMeta::TYPE_TITLE];
  767. return $this->_fields[$name];
  768. }
  769. foreach ($this->_fields as $name => $field) {
  770. if ($field->type === XSFieldMeta::TYPE_STRING && !$field->isBoolIndex()) {
  771. return $field;
  772. }
  773. }
  774. return false;
  775. }
  776. public function getFieldBody()
  777. {
  778. if (isset($this->_typeMap[XSFieldMeta::TYPE_BODY])) {
  779. $name = $this->_typeMap[XSFieldMeta::TYPE_BODY];
  780. return $this->_fields[$name];
  781. }
  782. return false;
  783. }
  784. public function getField($name, $throw = true)
  785. {
  786. if (is_int($name)) {
  787. if (!isset($this->_vnoMap[$name])) {
  788. if ($throw === true) {
  789. throw new XSException('Not exists field with vno: `' . $name . '\'');
  790. }
  791. return false;
  792. }
  793. $name = $this->_vnoMap[$name];
  794. }
  795. if (!isset($this->_fields[$name])) {
  796. if ($throw === true) {
  797. throw new XSException('Not exists field with name: `' . $name . '\'');
  798. }
  799. return false;
  800. }
  801. return $this->_fields[$name];
  802. }
  803. public function getAllFields()
  804. {
  805. return $this->_fields;
  806. }
  807. public function getVnoMap()
  808. {
  809. return $this->_vnoMap;
  810. }
  811. public function addField($field, $config = null)
  812. {
  813. if (!$field instanceof XSFieldMeta) {
  814. $field = new XSFieldMeta($field, $config);
  815. }
  816. if (isset($this->_fields[$field->name])) {
  817. throw new XSException('Duplicated field name: `' . $field->name . '\'');
  818. }
  819. if ($field->isSpeical()) {
  820. if (isset($this->_typeMap[$field->type])) {
  821. $prev = $this->_typeMap[$field->type];
  822. throw new XSException('Duplicated ' . strtoupper($config['type']) . ' field: `' . $field->name . '\' and `' . $prev . '\'');
  823. }
  824. $this->_typeMap[$field->type] = $field->name;
  825. }
  826. $field->vno = ($field->type == XSFieldMeta::TYPE_BODY) ? self::MIXED_VNO : count($this->_vnoMap);
  827. $this->_vnoMap[$field->vno] = $field->name;
  828. if ($field->type == XSFieldMeta::TYPE_ID) {
  829. $this->_fields = array_merge(array($field->name => $field), $this->_fields);
  830. } else {
  831. $this->_fields[$field->name] = $field;
  832. }
  833. }
  834. public function checkValid($throw = false)
  835. {
  836. if (!isset($this->_typeMap[XSFieldMeta::TYPE_ID])) {
  837. if ($throw) {
  838. throw new XSException('Missing field of type ID');
  839. }
  840. return false;
  841. }
  842. return true;
  843. }
  844. public function getIterator()
  845. {
  846. return new ArrayIterator($this->_fields);
  847. }
  848. public static function logger()
  849. {
  850. if (self::$_logger === null) {
  851. $scheme = new self;
  852. $scheme->addField('id', array('type' => 'id'));
  853. $scheme->addField('pinyin');
  854. $scheme->addField('partial');
  855. $scheme->addField('total', array('type' => 'numeric', 'index' => 'self'));
  856. $scheme->addField('lastnum', array('type' => 'numeric', 'index' => 'self'));
  857. $scheme->addField('currnum', array('type' => 'numeric', 'index' => 'self'));
  858. $scheme->addField('currtag', array('type' => 'string'));
  859. $scheme->addField('body', array('type' => 'body'));
  860. self::$_logger = $scheme;
  861. }
  862. return self::$_logger;
  863. }
  864. }
  865. class XSFieldMeta
  866. {
  867. const MAX_WDF = 0x3f;
  868. const TYPE_STRING = 0;
  869. const TYPE_NUMERIC = 1;
  870. const TYPE_DATE = 2;
  871. const TYPE_ID = 10;
  872. const TYPE_TITLE = 11;
  873. const TYPE_BODY = 12;
  874. const FLAG_INDEX_SELF = 0x01;
  875. const FLAG_INDEX_MIXED = 0x02;
  876. const FLAG_INDEX_BOTH = 0x03;
  877. const FLAG_WITH_POSITION = 0x10;
  878. const FLAG_NON_BOOL = 0x80; // 强制让该字段参与权重计算 (非布尔)
  879. public $name;
  880. public $cutlen = 0;
  881. public $weight = 1;
  882. public $type = 0;
  883. public $vno = 0;
  884. private $tokenizer = XSTokenizer::DFL;
  885. private $flag = 0;
  886. private static $_tokenizers = array();
  887. public function __construct($name, $config = null)
  888. {
  889. $this->name = strval($name);
  890. if (is_array($config)) {
  891. $this->fromConfig($config);
  892. }
  893. }
  894. public function __toString()
  895. {
  896. return $this->name;
  897. }
  898. public function val($value)
  899. {
  900. if ($this->type == self::TYPE_DATE) {
  901. if (!is_numeric($value) || strlen($value) !== 8) {
  902. $value = date('Ymd', is_numeric($value) ? $value : strtotime($value));
  903. }
  904. }
  905. return $value;
  906. }
  907. public function withPos()
  908. {
  909. return ($this->flag & self::FLAG_WITH_POSITION) ? true : false;
  910. }
  911. public function isBoolIndex()
  912. {
  913. if ($this->flag & self::FLAG_NON_BOOL) {
  914. return false;
  915. }
  916. return (!$this->hasIndex() || $this->tokenizer !== XSTokenizer::DFL);
  917. }
  918. public function isNumeric()
  919. {
  920. return ($this->type == self::TYPE_NUMERIC);
  921. }
  922. public function isSpeical()
  923. {
  924. return ($this->type == self::TYPE_ID || $this->type == self::TYPE_TITLE || $this->type == self::TYPE_BODY);
  925. }
  926. public function hasIndex()
  927. {
  928. return ($this->flag & self::FLAG_INDEX_BOTH) ? true : false;
  929. }
  930. public function hasIndexMixed()
  931. {
  932. return ($this->flag & self::FLAG_INDEX_MIXED) ? true : false;
  933. }
  934. public function hasIndexSelf()
  935. {
  936. return ($this->flag & self::FLAG_INDEX_SELF) ? true : false;
  937. }
  938. public function hasCustomTokenizer()
  939. {
  940. return ($this->tokenizer !== XSTokenizer::DFL);
  941. }
  942. public function getCustomTokenizer()
  943. {
  944. if (isset(self::$_tokenizers[$this->tokenizer])) {
  945. return self::$_tokenizers[$this->tokenizer];
  946. } else {
  947. if (($pos1 = strpos($this->tokenizer, '(')) !== false
  948. && ($pos2 = strrpos($this->tokenizer, ')', $pos1 + 1))) {
  949. $name = 'XSTokenizer' . ucfirst(trim(substr($this->tokenizer, 0, $pos1)));
  950. $arg = substr($this->tokenizer, $pos1 + 1, $pos2 - $pos1 - 1);
  951. } else {
  952. $name = 'XSTokenizer' . ucfirst($this->tokenizer);
  953. $arg = null;
  954. }
  955. if (!class_exists($name)) {
  956. $file = $name . '.class.php';
  957. if (file_exists($file)) {
  958. require_once $file;
  959. } else if (file_exists(XS_LIB_ROOT . DIRECTORY_SEPARATOR . $file)) {
  960. require_once XS_LIB_ROOT . DIRECTORY_SEPARATOR . $file;
  961. }
  962. if (!class_exists($name)) {
  963. throw new XSException('Undefined custom tokenizer `' . $this->tokenizer . '\' for field `' . $this->name . '\'');
  964. }
  965. }
  966. $obj = $arg === null ? new $name : new $name($arg);
  967. if (!$obj instanceof XSTokenizer) {
  968. throw new XSException($name . ' for field `' . $this->name . '\' dose not implement the interface: XSTokenizer');
  969. }
  970. self::$_tokenizers[$this->tokenizer] = $obj;
  971. return $obj;
  972. }
  973. }
  974. public function toConfig()
  975. {
  976. $str = "[" . $this->name . "]\n";
  977. if ($this->type === self::TYPE_NUMERIC) {
  978. $str .= "type = numeric\n";
  979. } elseif ($this->type === self::TYPE_DATE) {
  980. $str .= "type = date\n";
  981. } elseif ($this->type === self::TYPE_ID) {
  982. $str .= "type = id\n";
  983. } elseif ($this->type === self::TYPE_TITLE) {
  984. $str .= "type = title\n";
  985. } elseif ($this->type === self::TYPE_BODY) {
  986. $str .= "type = body\n";
  987. }
  988. if ($this->type !== self::TYPE_BODY && ($index = ($this->flag & self::FLAG_INDEX_BOTH))) {
  989. if ($index === self::FLAG_INDEX_BOTH) {
  990. if ($this->type !== self::TYPE_TITLE) {
  991. $str .= "index = both\n";
  992. }
  993. } elseif ($index === self::FLAG_INDEX_MIXED) {
  994. $str .= "index = mixed\n";
  995. } else {
  996. if ($this->type !== self::TYPE_ID) {
  997. $str .= "index = self\n";
  998. }
  999. }
  1000. }
  1001. if ($this->type !== self::TYPE_ID && $this->tokenizer !== XSTokenizer::DFL) {
  1002. $str .= "tokenizer = " . $this->tokenizer . "\n";
  1003. }
  1004. if ($this->cutlen > 0 && !($this->cutlen === 300 && $this->type === self::TYPE_BODY)) {
  1005. $str .= "cutlen = " . $this->cutlen . "\n";
  1006. }
  1007. if ($this->weight !== 1 && !($this->weight === 5 && $this->type === self::TYPE_TITLE)) {
  1008. $str .= "weight = " . $this->weight . "\n";
  1009. }
  1010. if ($this->flag & self::FLAG_WITH_POSITION) {
  1011. if ($this->type !== self::TYPE_BODY && $this->type !== self::TYPE_TITLE) {
  1012. $str .= "phrase = yes\n";
  1013. }
  1014. } else {
  1015. if ($this->type === self::TYPE_BODY || $this->type === self::TYPE_TITLE) {
  1016. $str .= "phrase = no\n";
  1017. }
  1018. }
  1019. if ($this->flag & self::FLAG_NON_BOOL) {
  1020. $str .= "non_bool = yes\n";
  1021. }
  1022. return $str;
  1023. }
  1024. public function fromConfig($config)
  1025. {
  1026. if (isset($config['type'])) {
  1027. $predef = 'self::TYPE_' . strtoupper($config['type']);
  1028. if (defined($predef)) {
  1029. $this->type = constant($predef);
  1030. if ($this->type == self::TYPE_ID) {
  1031. $this->flag = self::FLAG_INDEX_SELF;
  1032. $this->tokenizer = 'full';
  1033. } elseif ($this->type == self::TYPE_TITLE) {
  1034. $this->flag = self::FLAG_INDEX_BOTH | self::FLAG_WITH_POSITION;
  1035. $this->weight = 5;
  1036. } elseif ($this->type == self::TYPE_BODY) {
  1037. $this->vno = XSFieldScheme::MIXED_VNO;
  1038. $this->flag = self::FLAG_INDEX_SELF | self::FLAG_WITH_POSITION;
  1039. $this->cutlen = 300;
  1040. }
  1041. }
  1042. }
  1043. if (isset($config['index']) && $this->type != self::TYPE_BODY) {
  1044. $predef = 'self::FLAG_INDEX_' . strtoupper($config['index']);
  1045. if (defined($predef)) {
  1046. $this->flag &= ~ self::FLAG_INDEX_BOTH;
  1047. $this->flag |= constant($predef);
  1048. }
  1049. if ($this->type == self::TYPE_ID) {
  1050. $this->flag |= self::FLAG_INDEX_SELF;
  1051. }
  1052. }
  1053. if (isset($config['cutlen'])) {
  1054. $this->cutlen = intval($config['cutlen']);
  1055. }
  1056. if (isset($config['weight']) && $this->type != self::TYPE_BODY) {
  1057. $this->weight = intval($config['weight']) & self::MAX_WDF;
  1058. }
  1059. if (isset($config['phrase'])) {
  1060. if (!strcasecmp($config['phrase'], 'yes')) {
  1061. $this->flag |= self::FLAG_WITH_POSITION;
  1062. } elseif (!strcasecmp($config['phrase'], 'no')) {
  1063. $this->flag &= ~ self::FLAG_WITH_POSITION;
  1064. }
  1065. }
  1066. if (isset($config['non_bool'])) {
  1067. if (!strcasecmp($config['non_bool'], 'yes')) {
  1068. $this->flag |= self::FLAG_NON_BOOL;
  1069. } elseif (!strcasecmp($config['non_bool'], 'no')) {
  1070. $this->flag &= ~ self::FLAG_NON_BOOL;
  1071. }
  1072. }
  1073. if (isset($config['tokenizer']) && $this->type != self::TYPE_ID
  1074. && $config['tokenizer'] != 'default') {
  1075. $this->tokenizer = $config['tokenizer'];
  1076. }
  1077. }
  1078. }
  1079. class XSIndex extends XSServer
  1080. {
  1081. private $_buf = '';
  1082. private $_bufSize = 0;
  1083. private $_rebuild = false;
  1084. private static $_adds = array();
  1085. public function addServer($conn)
  1086. {
  1087. $srv = new XSServer($conn, $this->xs);
  1088. self::$_adds[] = $srv;
  1089. return $srv;
  1090. }
  1091. public function execCommand($cmd, $res_arg = XS_CMD_NONE, $res_cmd = XS_CMD_OK)
  1092. {
  1093. $res = parent::execCommand($cmd, $res_arg, $res_cmd);
  1094. foreach (self::$_adds as $srv) {
  1095. $srv->execCommand($cmd, $res_arg, $res_cmd);
  1096. }
  1097. return $res;
  1098. }
  1099. public function clean()
  1100. {
  1101. $this->execCommand(XS_CMD_INDEX_CLEAN_DB, XS_CMD_OK_DB_CLEAN);
  1102. return $this;
  1103. }
  1104. public function add(XSDocument $doc)
  1105. {
  1106. return $this->update($doc, true);
  1107. }
  1108. public function update(XSDocument $doc, $add = false)
  1109. {
  1110. if ($doc->beforeSubmit($this) === false) {
  1111. return $this;
  1112. }
  1113. $fid = $this->xs->getFieldId();
  1114. $key = $doc->f($fid);
  1115. if ($key === null || $key === '') {
  1116. throw new XSException('Missing value of primary key (FIELD:' . $fid . ')');
  1117. }
  1118. $cmd = new XSCommand(XS_CMD_INDEX_REQUEST, XS_CMD_INDEX_REQUEST_ADD);
  1119. if ($add !== true) {
  1120. $cmd->arg1 = XS_CMD_INDEX_REQUEST_UPDATE;
  1121. $cmd->arg2 = $fid->vno;
  1122. $cmd->buf = $key;
  1123. }
  1124. $cmds = array($cmd);
  1125. foreach ($this->xs->getAllFields() as $field) /* @var $field XSFieldMeta */ {
  1126. if (($value = $doc->f($field)) !== null) {
  1127. $varg = $field->isNumeric() ? XS_CMD_VALUE_FLAG_NUMERIC : 0;
  1128. $value = $field->val($value);
  1129. if (!$field->hasCustomTokenizer()) {
  1130. $wdf = $field->weight | ($field->withPos() ? XS_CMD_INDEX_FLAG_WITHPOS : 0);
  1131. if ($field->hasIndexMixed()) {
  1132. $cmds[] = new XSCommand(XS_CMD_DOC_INDEX, $wdf, XSFieldScheme::MIXED_VNO, $value);
  1133. }
  1134. if ($field->hasIndexSelf()) {
  1135. $wdf |= $field->isNumeric() ? 0 : XS_CMD_INDEX_FLAG_SAVEVALUE;
  1136. $cmds[] = new XSCommand(XS_CMD_DOC_INDEX, $wdf, $field->vno, $value);
  1137. }
  1138. if (!$field->hasIndexSelf() || $field->isNumeric()) {
  1139. $cmds[] = new XSCommand(XS_CMD_DOC_VALUE, $varg, $field->vno, $value);
  1140. }
  1141. } else {
  1142. if ($field->hasIndex()) {
  1143. $terms = $field->getCustomTokenizer()->getTokens($value, $doc);
  1144. if ($field->hasIndexSelf()) {
  1145. $wdf = $field->isBoolIndex() ? 1 : ($field->weight | XS_CMD_INDEX_FLAG_CHECKSTEM);
  1146. foreach ($terms as $term) {
  1147. if (strlen($term) > 200) {
  1148. continue;
  1149. }
  1150. $term = strtolower($term);
  1151. $cmds[] = new XSCommand(XS_CMD_DOC_TERM, $wdf, $field->vno, $term);
  1152. }
  1153. }
  1154. if ($field->hasIndexMixed()) {
  1155. $mtext = implode(' ', $terms);
  1156. $cmds[] = new XSCommand(XS_CMD_DOC_INDEX, $field->weight, XSFieldScheme::MIXED_VNO, $mtext);
  1157. }
  1158. }
  1159. $cmds[] = new XSCommand(XS_CMD_DOC_VALUE, $varg, $field->vno, $value);
  1160. }
  1161. }
  1162. if (($terms = $doc->getAddTerms($field)) !== null) {
  1163. $wdf1 = $field->isBoolIndex() ? 0 : XS_CMD_INDEX_FLAG_CHECKSTEM;
  1164. foreach ($terms as $term => $wdf) {
  1165. $term = strtolower($term);
  1166. if (strlen($term) > 200) {
  1167. continue;
  1168. }
  1169. $wdf2 = $field->isBoolIndex() ? 1 : $wdf * $field->weight;
  1170. while ($wdf2 > XSFieldMeta::MAX_WDF) {
  1171. $cmds[] = new XSCommand(XS_CMD_DOC_TERM, $wdf1 | XSFieldMeta::MAX_WDF, $field->vno, $term);
  1172. $wdf2 -= XSFieldMeta::MAX_WDF;
  1173. }
  1174. $cmds[] = new XSCommand(XS_CMD_DOC_TERM, $wdf1 | $wdf2, $field->vno, $term);
  1175. }
  1176. }
  1177. if (($text = $doc->getAddIndex($field)) !== null) {
  1178. if (!$field->hasCustomTokenizer()) {
  1179. $wdf = $field->weight | ($field->withPos() ? XS_CMD_INDEX_FLAG_WITHPOS : 0);
  1180. $cmds[] = new XSCommand(XS_CMD_DOC_INDEX, $wdf, $field->vno, $text);
  1181. } else {
  1182. $wdf = $field->isBoolIndex() ? 1 : ($field->weight | XS_CMD_INDEX_FLAG_CHECKSTEM);
  1183. $terms = $field->getCustomTokenizer()->getTokens($text, $doc);
  1184. foreach ($terms as $term) {
  1185. if (strlen($term) > 200) {
  1186. continue;
  1187. }
  1188. $term = strtolower($term);
  1189. $cmds[] = new XSCommand(XS_CMD_DOC_TERM, $wdf, $field->vno, $term);
  1190. }
  1191. }
  1192. }
  1193. }
  1194. $cmds[] = new XSCommand(XS_CMD_INDEX_SUBMIT);
  1195. if ($this->_bufSize > 0) {
  1196. $this->appendBuffer(implode('', $cmds));
  1197. } else {
  1198. for ($i = 0; $i < count($cmds) - 1; $i++) {
  1199. $this->execCommand($cmds[$i]);
  1200. }
  1201. $this->execCommand($cmds[$i], XS_CMD_OK_RQST_FINISHED);
  1202. }
  1203. $doc->afterSubmit($this);
  1204. return $this;
  1205. }
  1206. public function del($term, $field = null)
  1207. {
  1208. $field = $field === null ? $this->xs->getFieldId() : $this->xs->getField($field);
  1209. $cmds = array();
  1210. $terms = is_array($term) ? array_unique($term) : array($term);
  1211. $terms = XS::convert($terms, 'UTF-8', $this->xs->getDefaultCharset());
  1212. foreach ($terms as $term) {
  1213. $cmds[] = new XSCommand(XS_CMD_INDEX_REMOVE, 0, $field->vno, strtolower($term));
  1214. }
  1215. if ($this->_bufSize > 0) {
  1216. $this->appendBuffer(implode('', $cmds));
  1217. } elseif (count($cmds) == 1) {
  1218. $this->execCommand($cmds[0], XS_CMD_OK_RQST_FINISHED);
  1219. } else {
  1220. $cmd = array('cmd' => XS_CMD_INDEX_EXDATA, 'buf' => implode('', $cmds));
  1221. $this->execCommand($cmd, XS_CMD_OK_RQST_FINISHED);
  1222. }
  1223. return $this;
  1224. }
  1225. public function addExdata($data, $check_file = true)
  1226. {
  1227. if (strlen($data) < 255 && $check_file
  1228. && file_exists($data) && ($data = file_get_contents($data)) === false) {
  1229. throw new XSException('Failed to read exdata from file');
  1230. }
  1231. $first = ord(substr($data, 0, 1));
  1232. if ($first != XS_CMD_IMPORT_HEADER
  1233. && $first != XS_CMD_INDEX_REQUEST && $first != XS_CMD_INDEX_SYNONYMS
  1234. && $first != XS_CMD_INDEX_REMOVE && $first != XS_CMD_INDEX_EXDATA) {
  1235. throw new XSException('Invalid start command of exdata (CMD:' . $first . ')');
  1236. }
  1237. $cmd = array('cmd' => XS_CMD_INDEX_EXDATA, 'buf' => $data);
  1238. $this->execCommand($cmd, XS_CMD_OK_RQST_FINISHED);
  1239. return $this;
  1240. }
  1241. public function addSynonym($raw, $synonym)
  1242. {
  1243. $raw = strval($raw);
  1244. $synonym = strval($synonym);
  1245. if ($raw !== '' && $synonym !== '') {
  1246. $cmd = new XSCommand(XS_CMD_INDEX_SYNONYMS, XS_CMD_INDEX_SYNONYMS_ADD, 0, $raw, $synonym);
  1247. if ($this->_bufSize > 0) {
  1248. $this->appendBuffer(strval($cmd));
  1249. } else {
  1250. $this->execCommand($cmd, XS_CMD_OK_RQST_FINISHED);
  1251. }
  1252. }
  1253. return $this;
  1254. }
  1255. public function delSynonym($raw, $synonym = null)
  1256. {
  1257. $raw = strval($raw);
  1258. $synonym = $synonym === null ? '' : strval($synonym);
  1259. if ($raw !== '') {
  1260. $cmd = new XSCommand(XS_CMD_INDEX_SYNONYMS, XS_CMD_INDEX_SYNONYMS_DEL, 0, $raw, $synonym);
  1261. if ($this->_bufSize > 0) {
  1262. $this->appendBuffer(strval($cmd));
  1263. } else {
  1264. $this->execCommand($cmd, XS_CMD_OK_RQST_FINISHED);
  1265. }
  1266. }
  1267. return $this;
  1268. }
  1269. public function setScwsMulti($level)
  1270. {
  1271. $level = intval($level);
  1272. if ($level >= 0 && $level < 16) {
  1273. $cmd = array('cmd' => XS_CMD_SEARCH_SCWS_SET, 'arg1' => XS_CMD_SCWS_SET_MULTI, 'arg2' => $level);
  1274. $this->execCommand($cmd);
  1275. }
  1276. return $this;
  1277. }
  1278. public function getScwsMulti()
  1279. {
  1280. $cmd = array('cmd' => XS_CMD_SEARCH_SCWS_GET, 'arg1' => XS_CMD_SCWS_GET_MULTI);
  1281. $res = $this->execCommand($cmd, XS_CMD_OK_INFO);
  1282. return intval($res->buf);
  1283. }
  1284. public function openBuffer($size = 4)
  1285. {
  1286. if ($this->_buf !== '') {
  1287. $this->addExdata($this->_buf, false);
  1288. }
  1289. $this->_bufSize = intval($size) << 20;
  1290. $this->_buf = '';
  1291. return $this;
  1292. }
  1293. public function closeBuffer()
  1294. {
  1295. return $this->openBuffer(0);
  1296. }
  1297. public function beginRebuild()
  1298. {
  1299. $this->execCommand(array('cmd' => XS_CMD_INDEX_REBUILD, 'arg1' => 0), XS_CMD_OK_DB_REBUILD);
  1300. $this->_rebuild = true;
  1301. return $this;
  1302. }
  1303. public function endRebuild()
  1304. {
  1305. if ($this->_rebuild === true) {
  1306. $this->_rebuild = false;
  1307. $this->execCommand(array('cmd' => XS_CMD_INDEX_REBUILD, 'arg1' => 1), XS_CMD_OK_DB_REBUILD);
  1308. }
  1309. return $this;
  1310. }
  1311. public function stopRebuild()
  1312. {
  1313. try {
  1314. $this->execCommand(array('cmd' => XS_CMD_INDEX_REBUILD, 'arg1' => 2), XS_CMD_OK_DB_REBUILD);
  1315. $this->_rebuild = false;
  1316. } catch (XSException $e) {
  1317. if ($e->getCode() !== XS_CMD_ERR_WRONGPLACE) {
  1318. throw $e;
  1319. }
  1320. }
  1321. return $this;
  1322. }
  1323. public function setDb($name)
  1324. {
  1325. $this->execCommand(array('cmd' => XS_CMD_INDEX_SET_DB, 'buf' => $name), XS_CMD_OK_DB_CHANGED);
  1326. return $this;
  1327. }
  1328. public function flushLogging()
  1329. {
  1330. try {
  1331. $this->execCommand(XS_CMD_FLUSH_LOGGING, XS_CMD_OK_LOG_FLUSHED);
  1332. } catch (XSException $e) {
  1333. if ($e->getCode() === XS_CMD_ERR_BUSY) {
  1334. return false;
  1335. }
  1336. throw $e;
  1337. }
  1338. return true;
  1339. }
  1340. public function flushIndex()
  1341. {
  1342. try {
  1343. $this->execCommand(XS_CMD_INDEX_COMMIT, XS_CMD_OK_DB_COMMITED);
  1344. } catch (XSException $e) {
  1345. if ($e->getCode() === XS_CMD_ERR_BUSY || $e->getCode() === XS_CMD_ERR_RUNNING) {
  1346. return false;
  1347. }
  1348. throw $e;
  1349. }
  1350. return true;
  1351. }
  1352. public function getCustomDict()
  1353. {
  1354. $res = $this->execCommand(XS_CMD_INDEX_USER_DICT, XS_CMD_OK_INFO);
  1355. return $res->buf;
  1356. }
  1357. public function setCustomDict($content)
  1358. {
  1359. $cmd = array('cmd' => XS_CMD_INDEX_USER_DICT, 'arg1' => 1, 'buf' => $content);
  1360. $this->execCommand($cmd, XS_CMD_OK_DICT_SAVED);
  1361. }
  1362. public function close($ioerr = false)
  1363. {
  1364. $this->closeBuffer();
  1365. parent::close($ioerr);
  1366. }
  1367. private function appendBuffer($buf)
  1368. {
  1369. $this->_buf .= $buf;
  1370. if (strlen($this->_buf) >= $this->_bufSize) {
  1371. $this->addExdata($this->_buf, false);
  1372. $this->_buf = '';
  1373. }
  1374. }
  1375. public function __destruct()
  1376. {
  1377. if ($this->_rebuild === true) {
  1378. try {
  1379. $this->endRebuild();
  1380. } catch (Exception $e) {
  1381. }
  1382. }
  1383. foreach (self::$_adds as $srv) {
  1384. $srv->close();
  1385. }
  1386. self::$_adds = array();
  1387. parent::__destruct();
  1388. }
  1389. }
  1390. class XSSearch extends XSServer
  1391. {
  1392. const PAGE_SIZE = 10;
  1393. const LOG_DB = 'log_db';
  1394. private $_defaultOp = XS_CMD_QUERY_OP_AND;
  1395. private $_prefix, $_fieldSet, $_resetScheme = false;
  1396. private $_query, $_terms, $_count;
  1397. private $_lastCount, $_highlight;
  1398. private $_curDb, $_curDbs = array();
  1399. private $_lastDb, $_lastDbs = array();
  1400. private $_facets = array();
  1401. private $_limit = 0, $_offset = 0;
  1402. private $_charset = 'UTF-8';
  1403. public function open($conn)
  1404. {
  1405. parent::open($conn);
  1406. $this->_prefix = array();
  1407. $this->_fieldSet = false;
  1408. $this->_lastCount = false;
  1409. }
  1410. public function setCharset($charset)
  1411. {
  1412. $this->_charset = strtoupper($charset);
  1413. if ($this->_charset == 'UTF8') {
  1414. $this->_charset = 'UTF-8';
  1415. }
  1416. return $this;
  1417. }
  1418. public function setFuzzy($value = true)
  1419. {
  1420. $this->_defaultOp = $value === true ? XS_CMD_QUERY_OP_OR : XS_CMD_QUERY_OP_AND;
  1421. return $this;
  1422. }
  1423. public function setCutOff($percent, $weight = 0)
  1424. {
  1425. $percent = max(0, min(100, intval($percent)));
  1426. $weight = max(0, (intval($weight * 10) & 255));
  1427. $cmd = new XSCommand(XS_CMD_SEARCH_SET_CUTOFF, $percent, $weight);
  1428. $this->execCommand($cmd);
  1429. return $this;
  1430. }
  1431. public function setRequireMatchedTerm($value = true)
  1432. {
  1433. $arg1 = XS_CMD_SEARCH_MISC_MATCHED_TERM;
  1434. $arg2 = $value === true ? 1 : 0;
  1435. $cmd = new XSCommand(XS_CMD_SEARCH_SET_MISC, $arg1, $arg2);
  1436. $this->execCommand($cmd);
  1437. return $this;
  1438. }
  1439. public function setAutoSynonyms($value = true)
  1440. {
  1441. $flag = XS_CMD_PARSE_FLAG_BOOLEAN | XS_CMD_PARSE_FLAG_PHRASE | XS_CMD_PARSE_FLAG_LOVEHATE;
  1442. if ($value === true) {
  1443. $flag |= XS_CMD_PARSE_FLAG_AUTO_MULTIWORD_SYNONYMS;
  1444. }
  1445. $cmd = array('cmd' => XS_CMD_QUERY_PARSEFLAG, 'arg' => $flag);
  1446. $this->execCommand($cmd);
  1447. return $this;
  1448. }
  1449. public function setSynonymScale($value)
  1450. {
  1451. $arg1 = XS_CMD_SEARCH_MISC_SYN_SCALE;
  1452. $arg2 = max(0, (intval($value * 100) & 255));
  1453. $cmd = new XSCommand(XS_CMD_SEARCH_SET_MISC, $arg1, $arg2);
  1454. $this->execCommand($cmd);
  1455. return $this;
  1456. }
  1457. public function getAllSynonyms($limit = 0, $offset = 0, $stemmed = false)
  1458. {
  1459. $page = $limit > 0 ? pack('II', intval($offset), intval($limit)) : '';
  1460. $cmd = array('cmd' => XS_CMD_SEARCH_GET_SYNONYMS, 'buf1' => $page);
  1461. $cmd['arg1'] = $stemmed == true ? 1 : 0;
  1462. $res = $this->execCommand($cmd, XS_CMD_OK_RESULT_SYNONYMS);
  1463. $ret = array();
  1464. if (!empty($res->buf)) {
  1465. foreach (explode("\n", $res->buf) as $line) {
  1466. $value = explode("\t", $line);
  1467. $key = array_shift($value);
  1468. $ret[$key] = $value;
  1469. }
  1470. }
  1471. return $ret;
  1472. }
  1473. public function getSynonyms($term)
  1474. {
  1475. $term = strval($term);
  1476. if (strlen($term) === 0) {
  1477. return false;
  1478. }
  1479. $cmd = array('cmd' => XS_CMD_SEARCH_GET_SYNONYMS, 'arg1' => 2, 'buf' => $term);
  1480. $res = $this->execCommand($cmd, XS_CMD_OK_RESULT_SYNONYMS);
  1481. $ret = $res->buf === '' ? array() : explode("\n", $res->buf);
  1482. return $ret;
  1483. }
  1484. public function getQuery($query = null)
  1485. {
  1486. $query = $query === null ? '' : $this->preQueryString($query);
  1487. $cmd = new XSCommand(XS_CMD_QUERY_GET_STRING, 0, $this->_defaultOp, $query);
  1488. $res = $this->execCommand($cmd, XS_CMD_OK_QUERY_STRING);
  1489. if (strpos($res->buf, 'VALUE_RANGE') !== false) {
  1490. $regex = '/(VALUE_RANGE) (\d+) (\S+) (.+?)(?=\))/';
  1491. $res->buf = preg_replace_callback($regex, array($this, 'formatValueRange'), $res->buf);
  1492. }
  1493. if (strpos($res->buf, 'VALUE_GE') !== false || strpos($res->buf, 'VALUE_LE') !== false) {
  1494. $regex = '/(VALUE_[GL]E) (\d+) (.+?)(?=\))/';
  1495. $res->buf = preg_replace_callback($regex, array($this, 'formatValueRange'), $res->buf);
  1496. }
  1497. return XS::convert($res->buf, $this->_charset, 'UTF-8');
  1498. }
  1499. public function setQuery($query)
  1500. {
  1501. $this->clearQuery();
  1502. if ($query !== null) {
  1503. $this->_query = $query;
  1504. $this->addQueryString($query);
  1505. }
  1506. return $this;
  1507. }
  1508. public function setGeodistSort($fields, $reverse = false, $relevance_first = false)
  1509. {
  1510. if (!is_array($fields) || count($fields) < 2) {
  1511. throw new XSException("Fields of `setGeodistSort' should be an array contain two or more elements");
  1512. }
  1513. $buf = '';
  1514. foreach ($fields as $key => $value) {
  1515. $field = $this->xs->getField($key, true);
  1516. if (!$field->isNumeric()) {
  1517. throw new XSException("Type of GeoField `$key' shoud be numeric");
  1518. }
  1519. $vno = $field->vno;
  1520. $vbuf = strval(floatval($value));
  1521. $vlen = strlen($vbuf);
  1522. if ($vlen >= 255) {
  1523. throw new XSException("Value of `$key' too long");
  1524. }
  1525. $buf .= chr($vno) . chr($vlen) . $vbuf;
  1526. }
  1527. $type = XS_CMD_SORT_TYPE_GEODIST;
  1528. if ($relevance_first) {
  1529. $type |= XS_CMD_SORT_FLAG_RELEVANCE;
  1530. }
  1531. if (!$reverse) {
  1532. $type |= XS_CMD_SORT_FLAG_ASCENDING;
  1533. }
  1534. $cmd = new XSCommand(XS_CMD_SEARCH_SET_SORT, $type, 0, $buf);
  1535. $this->execCommand($cmd);
  1536. return $this;
  1537. }
  1538. public function setMultiSort($fields, $reverse = false, $relevance_first = false)
  1539. {
  1540. if (!is_array($fields)) {
  1541. return $this->setSort($fields, !$reverse, $relevance_first);
  1542. }
  1543. $buf = '';
  1544. foreach ($fields as $key => $value) {
  1545. if (is_bool($value)) {
  1546. $vno = $this->xs->getField($key, true)->vno;
  1547. $asc = $value;
  1548. } else {
  1549. $vno = $this->xs->getField($value, true)->vno;
  1550. $asc = false;
  1551. }
  1552. if ($vno != XSFieldScheme::MIXED_VNO) {
  1553. $buf .= chr($vno) . chr($asc ? 1 : 0);
  1554. }
  1555. }
  1556. if ($buf !== '') {
  1557. $type = XS_CMD_SORT_TYPE_MULTI;
  1558. if ($relevance_first) {
  1559. $type |= XS_CMD_SORT_FLAG_RELEVANCE;
  1560. }
  1561. if (!$reverse) {
  1562. $type |= XS_CMD_SORT_FLAG_ASCENDING;
  1563. }
  1564. $cmd = new XSCommand(XS_CMD_SEARCH_SET_SORT, $type, 0, $buf);
  1565. $this->execCommand($cmd);
  1566. }
  1567. return $this;
  1568. }
  1569. public function setSort($field, $asc = false, $relevance_first = false)
  1570. {
  1571. if (is_array($field)) {
  1572. return $this->setMultiSort($field, $asc, $relevance_first);
  1573. }
  1574. if ($field === null) {
  1575. $cmd = new XSCommand(XS_CMD_SEARCH_SET_SORT, XS_CMD_SORT_TYPE_RELEVANCE);
  1576. } else {
  1577. $type = XS_CMD_SORT_TYPE_VALUE;
  1578. if ($relevance_first) {
  1579. $type |= XS_CMD_SORT_FLAG_RELEVANCE;
  1580. }
  1581. if ($asc) {
  1582. $type |= XS_CMD_SORT_FLAG_ASCENDING;
  1583. }
  1584. $vno = $this->xs->getField($field, true)->vno;
  1585. $cmd = new XSCommand(XS_CMD_SEARCH_SET_SORT, $type, $vno);
  1586. }
  1587. $this->execCommand($cmd);
  1588. return $this;
  1589. }
  1590. public function setDocOrder($asc = false)
  1591. {
  1592. $type = XS_CMD_SORT_TYPE_DOCID | ($asc ? XS_CMD_SORT_FLAG_ASCENDING : 0);
  1593. $cmd = new XSCommand(XS_CMD_SEARCH_SET_SORT, $type);
  1594. $this->execCommand($cmd);
  1595. return $this;
  1596. }
  1597. public function setCollapse($field, $num = 1)
  1598. {
  1599. $vno = $field === null ? XSFieldScheme::MIXED_VNO : $this->xs->getField($field, true)->vno;
  1600. $max = min(255, intval($num));
  1601. $cmd = new XSCommand(XS_CMD_SEARCH_SET_COLLAPSE, $max, $vno);
  1602. $this->execCommand($cmd);
  1603. return $this;
  1604. }
  1605. public function addRange($field, $from, $to)
  1606. {
  1607. if ($from === '' || $from === false) {
  1608. $from = null;
  1609. }
  1610. if ($to === '' || $to === false) {
  1611. $to = null;
  1612. }
  1613. if ($from !== null || $to !== null) {
  1614. if (strlen($from) > 255 || strlen($to) > 255) {
  1615. throw new XSException('Value of range is too long');
  1616. }
  1617. $vno = $this->xs->getField($field)->vno;
  1618. $from = XS::convert($from, 'UTF-8', $this->_charset);
  1619. $to = XS::convert($to, 'UTF-8', $this->_charset);
  1620. if ($from === null) {
  1621. $cmd = new XSCommand(XS_CMD_QUERY_VALCMP, XS_CMD_QUERY_OP_FILTER, $vno, $to, chr(XS_CMD_VALCMP_LE));
  1622. } elseif ($to === null) {
  1623. $cmd = new XSCommand(XS_CMD_QUERY_VALCMP, XS_CMD_QUERY_OP_FILTER, $vno, $from, chr(XS_CMD_VALCMP_GE));
  1624. } else {
  1625. $cmd = new XSCommand(XS_CMD_QUERY_RANGE, XS_CMD_QUERY_OP_FILTER, $vno, $from, $to);
  1626. }
  1627. $this->execCommand($cmd);
  1628. }
  1629. return $this;
  1630. }
  1631. public function addWeight($field, $term, $weight = 1)
  1632. {
  1633. return $this->addQueryTerm($field, $term, XS_CMD_QUERY_OP_AND_MAYBE, $weight);
  1634. }
  1635. public function setFacets($field, $exact = false)
  1636. {
  1637. $buf = '';
  1638. if (!is_array($field)) {
  1639. $field = array($field);
  1640. }
  1641. foreach ($field as $name) {
  1642. $ff = $this->xs->getField($name);
  1643. if ($ff->type !== XSFieldMeta::TYPE_STRING) {
  1644. throw new XSException("Field `$name' cann't be used for facets search, can only be string type");
  1645. }
  1646. $buf .= chr($ff->vno);
  1647. }
  1648. $cmd = array('cmd' => XS_CMD_SEARCH_SET_FACETS, 'buf' => $buf);
  1649. $cmd['arg1'] = $exact === true ? 1 : 0;
  1650. $this->execCommand($cmd);
  1651. return $this;
  1652. }
  1653. public function getFacets($field = null)
  1654. {
  1655. if ($field === null) {
  1656. return $this->_facets;
  1657. }
  1658. return isset($this->_facets[$field]) ? $this->_facets[$field] : array();
  1659. }
  1660. public function setScwsMulti($level)
  1661. {
  1662. $level = intval($level);
  1663. if ($level >= 0 && $level < 16) {
  1664. $cmd = array('cmd' => XS_CMD_SEARCH_SCWS_SET, 'arg1' => XS_CMD_SCWS_SET_MULTI, 'arg2' => $level);
  1665. $this->execCommand($cmd);
  1666. }
  1667. return $this;
  1668. }
  1669. public function setLimit($limit, $offset = 0)
  1670. {
  1671. $this->_limit = intval($limit);
  1672. $this->_offset = intval($offset);
  1673. return $this;
  1674. }
  1675. public function setDb($name)
  1676. {
  1677. $name = strval($name);
  1678. $this->execCommand(array('cmd' => XS_CMD_SEARCH_SET_DB, 'buf' => strval($name)));
  1679. $this->_lastDb = $this->_curDb;
  1680. $this->_lastDbs = $this->_curDbs;
  1681. $this->_curDb = $name;
  1682. $this->_curDbs = array();
  1683. return $this;
  1684. }
  1685. public function addDb($name)
  1686. {
  1687. $name = strval($name);
  1688. $this->execCommand(array('cmd' => XS_CMD_SEARCH_ADD_DB, 'buf' => $name));
  1689. $this->_curDbs[] = $name;
  1690. return $this;
  1691. }
  1692. public function markResetScheme()
  1693. {
  1694. $this->_resetScheme = true;
  1695. }
  1696. public function terms($query = null, $convert = true)
  1697. {
  1698. $query = $query === null ? '' : $this->preQueryString($query);
  1699. if ($query === '' && $this->_terms !== null) {
  1700. $ret = $this->_terms;
  1701. } else {
  1702. $cmd = new XSCommand(XS_CMD_QUERY_GET_TERMS, 0, $this->_defaultOp, $query);
  1703. $res = $this->execCommand($cmd, XS_CMD_OK_QUERY_TERMS);
  1704. $ret = array();
  1705. $tmps = explode(' ', $res->buf);
  1706. for ($i = 0; $i < count($tmps); $i++) {
  1707. if ($tmps[$i] === '' || strpos($tmps[$i], ':') !== false) {
  1708. continue;
  1709. }
  1710. $ret[] = $tmps[$i];
  1711. }
  1712. if ($query === '') {
  1713. $this->_terms = $ret;
  1714. }
  1715. }
  1716. return $convert ? XS::convert($ret, $this->_charset, 'UTF-8') : $ret;
  1717. }
  1718. public function count($query = null)
  1719. {
  1720. $query = $query === null ? '' : $this->preQueryString($query);
  1721. if ($query === '' && $this->_count !== null) {
  1722. return $this->_count;
  1723. }
  1724. $cmd = new XSCommand(XS_CMD_SEARCH_GET_TOTAL, 0, $this->_defaultOp, $query);
  1725. $res = $this->execCommand($cmd, XS_CMD_OK_SEARCH_TOTAL);
  1726. $ret = unpack('Icount', $res->buf);
  1727. if ($query === '') {
  1728. $this->_count = $ret['count'];
  1729. }
  1730. return $ret['count'];
  1731. }
  1732. public function search($query = null, $saveHighlight = true)
  1733. {
  1734. if ($this->_curDb !== self::LOG_DB && $saveHighlight) {
  1735. $this->_highlight = $query;
  1736. }
  1737. $query = $query === null ? '' : $this->preQueryString($query);
  1738. $page = pack('II', $this->_offset, $this->_limit > 0 ? $this->_limit : self::PAGE_SIZE);
  1739. $cmd = new XSCommand(XS_CMD_SEARCH_GET_RESULT, 0, $this->_defaultOp, $query, $page);
  1740. $res = $this->execCommand($cmd, XS_CMD_OK_RESULT_BEGIN);
  1741. $tmp = unpack('Icount', $res->buf);
  1742. $this->_lastCount = $tmp['count'];
  1743. $ret = $this->_facets = array();
  1744. $vnoes = $this->xs->getScheme()->getVnoMap();
  1745. while (true) {
  1746. $res = $this->getRespond();
  1747. if ($res->cmd == XS_CMD_SEARCH_RESULT_FACETS) {
  1748. $off = 0;
  1749. while (($off + 6) < strlen($res->buf)) {
  1750. $tmp = unpack('Cvno/Cvlen/Inum', substr($res->buf, $off, 6));
  1751. if (isset($vnoes[$tmp['vno']])) {
  1752. $name = $vnoes[$tmp['vno']];
  1753. $value = substr($res->buf, $off + 6, $tmp['vlen']);
  1754. if (!isset($this->_facets[$name])) {
  1755. $this->_facets[$name] = array();
  1756. }
  1757. $this->_facets[$name][$value] = $tmp['num'];
  1758. }
  1759. $off += $tmp['vlen'] + 6;
  1760. }
  1761. } elseif ($res->cmd == XS_CMD_SEARCH_RESULT_DOC) {
  1762. $doc = new XSDocument($res->buf, $this->_charset);
  1763. $ret[] = $doc;
  1764. } elseif ($res->cmd == XS_CMD_SEARCH_RESULT_FIELD) {
  1765. if (isset($doc)) {
  1766. $name = isset($vnoes[$res->arg]) ? $vnoes[$res->arg] : $res->arg;
  1767. $doc->setField($name, $res->buf);
  1768. }
  1769. } elseif ($res->cmd == XS_CMD_SEARCH_RESULT_MATCHED) {
  1770. if (isset($doc)) {
  1771. $doc->setField('matched', explode(' ', $res->buf), true);
  1772. }
  1773. } elseif ($res->cmd == XS_CMD_OK && $res->arg == XS_CMD_OK_RESULT_END) {
  1774. break;
  1775. } else {
  1776. $msg = 'Unexpected respond in search {CMD:' . $res->cmd . ', ARG:' . $res->arg . '}';
  1777. throw new XSException($msg);
  1778. }
  1779. }
  1780. if ($query === '') {
  1781. $this->_count = $this->_lastCount;
  1782. if ($this->_curDb !== self::LOG_DB) {
  1783. $this->logQuery();
  1784. if ($saveHighlight) {
  1785. $this->initHighlight();
  1786. }
  1787. }
  1788. }
  1789. $this->_limit = $this->_offset = 0;
  1790. return $ret;
  1791. }
  1792. public function getLastCount()
  1793. {
  1794. return $this->_lastCount;
  1795. }
  1796. public function getDbTotal()
  1797. {
  1798. $cmd = new XSCommand(XS_CMD_SEARCH_DB_TOTAL);
  1799. $res = $this->execCommand($cmd, XS_CMD_OK_DB_TOTAL);
  1800. $tmp = unpack('Itotal', $res->buf);
  1801. return $tmp['total'];
  1802. }
  1803. public function getHotQuery($limit = 6, $type = 'total')
  1804. {
  1805. $ret = array();
  1806. $limit = max(1, min(50, intval($limit)));
  1807. $this->xs->setScheme(XSFieldScheme::logger());
  1808. try {
  1809. $this->setDb(self::LOG_DB)->setLimit($limit);
  1810. if ($type !== 'lastnum' && $type !== 'currnum') {
  1811. $type = 'total';
  1812. }
  1813. $result = $this->search($type . ':1');
  1814. foreach ($result as $doc) /* @var $doc XSDocument */ {
  1815. $body = $doc->body;
  1816. $ret[$body] = $doc->f($type);
  1817. }
  1818. $this->restoreDb();
  1819. } catch (XSException $e) {
  1820. if ($e->getCode() != XS_CMD_ERR_XAPIAN) {
  1821. throw $e;
  1822. }
  1823. }
  1824. $this->xs->restoreScheme();
  1825. return $ret;
  1826. }
  1827. public function getRelatedQuery($query = null, $limit = 6)
  1828. {
  1829. $ret = array();
  1830. $limit = max(1, min(20, intval($limit)));
  1831. if ($query === null) {
  1832. $query = $this->cleanFieldQuery($this->_query);
  1833. }
  1834. if (empty($query) || strpos($query, ':') !== false) {
  1835. return $ret;
  1836. }
  1837. $op = $this->_defaultOp;
  1838. $this->xs->setScheme(XSFieldScheme::logger());
  1839. try {
  1840. $result = $this->setDb(self::LOG_DB)->setFuzzy()->setLimit($limit + 1)->search($query);
  1841. foreach ($result as $doc) /* @var $doc XSDocument */ {
  1842. $doc->setCharset($this->_charset);
  1843. $body = $doc->body;
  1844. if (!strcasecmp($body, $query)) {
  1845. continue;
  1846. }
  1847. $ret[] = $body;
  1848. if (count($ret) == $limit) {
  1849. break;
  1850. }
  1851. }
  1852. } catch (XSException $e) {
  1853. if ($e->getCode() != XS_CMD_ERR_XAPIAN) {
  1854. throw $e;
  1855. }
  1856. }
  1857. $this->restoreDb();
  1858. $this->xs->restoreScheme();
  1859. $this->_defaultOp = $op;
  1860. return $ret;
  1861. }
  1862. public function getExpandedQuery($query, $limit = 10)
  1863. {
  1864. $ret = array();
  1865. $limit = max(1, min(20, intval($limit)));
  1866. try {
  1867. $buf = XS::convert($query, 'UTF-8', $this->_charset);
  1868. $cmd = array('cmd' => XS_CMD_QUERY_GET_EXPANDED, 'arg1' => $limit, 'buf' => $buf);
  1869. $res = $this->execCommand($cmd, XS_CMD_OK_RESULT_BEGIN);
  1870. while (true) {
  1871. $res = $this->getRespond();
  1872. if ($res->cmd == XS_CMD_SEARCH_RESULT_FIELD) {
  1873. $ret[] = XS::convert($res->buf, $this->_charset, 'UTF-8');
  1874. } elseif ($res->cmd == XS_CMD_OK && $res->arg == XS_CMD_OK_RESULT_END) {
  1875. break;
  1876. } else {
  1877. $msg = 'Unexpected respond in search {CMD:' . $res->cmd . ', ARG:' . $res->arg . '}';
  1878. throw new XSException($msg);
  1879. }
  1880. }
  1881. } catch (XSException $e) {
  1882. if ($e->getCode() != XS_CMD_ERR_XAPIAN) {
  1883. throw $e;
  1884. }
  1885. }
  1886. return $ret;
  1887. }
  1888. public function getCorrectedQuery($query = null)
  1889. {
  1890. $ret = array();
  1891. try {
  1892. if ($query === null) {
  1893. if ($this->_count > 0 && $this->_count > ceil($this->getDbTotal() * 0.001)) {
  1894. return $ret;
  1895. }
  1896. $query = $this->cleanFieldQuery($this->_query);
  1897. }
  1898. if (empty($query) || strpos($query, ':') !== false) {
  1899. return $ret;
  1900. }
  1901. $buf = XS::convert($query, 'UTF-8', $this->_charset);
  1902. $cmd = array('cmd' => XS_CMD_QUERY_GET_CORRECTED, 'buf' => $buf);
  1903. $res = $this->execCommand($cmd, XS_CMD_OK_QUERY_CORRECTED);
  1904. if ($res->buf !== '') {
  1905. $ret = explode("\n", XS::convert($res->buf, $this->_charset, 'UTF-8'));
  1906. }
  1907. } catch (XSException $e) {
  1908. if ($e->getCode() != XS_CMD_ERR_XAPIAN) {
  1909. throw $e;
  1910. }
  1911. }
  1912. return $ret;
  1913. }
  1914. public function addSearchLog($query, $wdf = 1)
  1915. {
  1916. $cmd = array('cmd' => XS_CMD_SEARCH_ADD_LOG, 'buf' => $query);
  1917. if ($wdf > 1) {
  1918. $cmd['buf1'] = pack('i', $wdf);
  1919. }
  1920. $this->execCommand($cmd, XS_CMD_OK_LOGGED);
  1921. }
  1922. public function highlight($value, $strtr = false)
  1923. {
  1924. if (empty($value)) {
  1925. return $value;
  1926. }
  1927. if (!is_array($this->_highlight)) {
  1928. $this->initHighlight();
  1929. }
  1930. if (isset($this->_highlight['pattern'])) {
  1931. $value = preg_replace($this->_highlight['pattern'], $this->_highlight['replace'], $value);
  1932. }
  1933. if (isset($this->_highlight['pairs'])) {
  1934. $value = $strtr ?
  1935. strtr($value, $this->_highlight['pairs']) :
  1936. str_replace(array_keys($this->_highlight['pairs']), array_values($this->_highlight['pairs']), $value);
  1937. }
  1938. return $value;
  1939. }
  1940. private function logQuery($query = null)
  1941. {
  1942. if ($this->isRobotAgent()) {
  1943. return;
  1944. }
  1945. if ($query !== '' && $query !== null) {
  1946. $terms = $this->terms($query, false);
  1947. } else {
  1948. $query = $this->_query;
  1949. if (!$this->_lastCount || ($this->_defaultOp == XS_CMD_QUERY_OP_OR && strpos($query, ' '))
  1950. || strpos($query, ' OR ') || strpos($query, ' NOT ') || strpos($query, ' XOR ')) {
  1951. return;
  1952. }
  1953. $terms = $this->terms(null, false);
  1954. }
  1955. $log = '';
  1956. $pos = $max = 0;
  1957. foreach ($terms as $term) {
  1958. $pos1 = ($pos > 3 && strlen($term) === 6) ? $pos - 3 : $pos;
  1959. if (($pos2 = strpos($query, $term, $pos1)) === false) {
  1960. continue;
  1961. }
  1962. if ($pos2 === $pos) {
  1963. $log .= $term;
  1964. } elseif ($pos2 < $pos) {
  1965. $log .= substr($term, 3);
  1966. } else {
  1967. if (++$max > 3 || strlen($log) > 42) {
  1968. break;
  1969. }
  1970. $log .= ' ' . $term;
  1971. }
  1972. $pos = $pos2 + strlen($term);
  1973. }
  1974. $log = trim($log);
  1975. if (strlen($log) < 2 || (strlen($log) == 3 && ord($log[0]) > 0x80)) {
  1976. return;
  1977. }
  1978. $this->addSearchLog($log);
  1979. }
  1980. private function clearQuery()
  1981. {
  1982. $cmd = new XSCommand(XS_CMD_QUERY_INIT);
  1983. if ($this->_resetScheme === true) {
  1984. $cmd->arg1 = 1;
  1985. $this->_prefix = array();
  1986. $this->_fieldSet = false;
  1987. $this->_resetScheme = false;
  1988. }
  1989. $this->execCommand($cmd);
  1990. $this->_query = $this->_count = $this->_terms = null;
  1991. }
  1992. public function addQueryString($query, $addOp = XS_CMD_QUERY_OP_AND, $scale = 1)
  1993. {
  1994. $query = $this->preQueryString($query);
  1995. $bscale = ($scale > 0 && $scale != 1) ? pack('n', intval($scale * 100)) : '';
  1996. $cmd = new XSCommand(XS_CMD_QUERY_PARSE, $addOp, $this->_defaultOp, $query, $bscale);
  1997. $this->execCommand($cmd);
  1998. return $query;
  1999. }
  2000. public function addQueryTerm($field, $term, $addOp = XS_CMD_QUERY_OP_AND, $scale = 1)
  2001. {
  2002. $term = XS::convert($term, 'UTF-8', $this->_charset);
  2003. $bscale = ($scale > 0 && $scale != 1) ? pack('n', intval($scale * 100)) : '';
  2004. $vno = $field === null ? XSFieldScheme::MIXED_VNO : $this->xs->getField($field, true)->vno;
  2005. $cmd = XS_CMD_QUERY_TERM;
  2006. if (is_array($term)) {
  2007. if (count($term) === 0) {
  2008. return $this;
  2009. } elseif (count($term) === 1) {
  2010. $term = current($term);
  2011. } else {
  2012. $term = implode("\t", $term);
  2013. $cmd = XS_CMD_QUERY_TERMS;
  2014. }
  2015. }
  2016. $cmd = new XSCommand($cmd, $addOp, $vno, $term, $bscale);
  2017. $this->execCommand($cmd);
  2018. return $this;
  2019. }
  2020. private function restoreDb()
  2021. {
  2022. $db = $this->_lastDb;
  2023. $dbs = $this->_lastDbs;
  2024. $this->setDb($db);
  2025. foreach ($dbs as $name) {
  2026. $this->addDb($name);
  2027. }
  2028. }
  2029. private function preQueryString($query)
  2030. {
  2031. $query = trim($query);
  2032. if ($this->_resetScheme === true) {
  2033. $this->clearQuery();
  2034. }
  2035. $this->initSpecialField();
  2036. $newQuery = '';
  2037. $parts = preg_split('/[ \t\r\n]+/', $query);
  2038. foreach ($parts as $part) {
  2039. if ($part === '') {
  2040. continue;
  2041. }
  2042. if ($newQuery != '') {
  2043. $newQuery .= ' ';
  2044. }
  2045. if (($pos = strpos($part, ':', 1)) !== false) {
  2046. for ($i = 0; $i < $pos; $i++) {
  2047. if (strpos('+-~(', $part[$i]) === false) {
  2048. break;
  2049. }
  2050. }
  2051. $name = substr($part, $i, $pos - $i);
  2052. if (($field = $this->xs->getField($name, false)) !== false
  2053. && $field->vno != XSFieldScheme::MIXED_VNO) {
  2054. $this->regQueryPrefix($name);
  2055. if ($field->hasCustomTokenizer()) {
  2056. $prefix = $i > 0 ? substr($part, 0, $i) : '';
  2057. $suffix = '';
  2058. $value = substr($part, $pos + 1);
  2059. if (substr($value, -1, 1) === ')') {
  2060. $suffix = ')';
  2061. $value = substr($value, 0, -1);
  2062. }
  2063. $terms = array();
  2064. $tokens = $field->getCustomTokenizer()->getTokens($value);
  2065. foreach ($tokens as $term) {
  2066. $terms[] = strtolower($term);
  2067. }
  2068. $terms = array_unique($terms);
  2069. $newQuery .= $prefix . $name . ':' . implode(' ' . $name . ':', $terms) . $suffix;
  2070. } elseif (substr($part, $pos + 1, 1) != '(' && preg_match('/[\x81-\xfe]/', $part)) {
  2071. $newQuery .= substr($part, 0, $pos + 1) . '(' . substr($part, $pos + 1) . ')';
  2072. } else {
  2073. $newQuery .= $part;
  2074. }
  2075. continue;
  2076. }
  2077. }
  2078. if (strlen($part) > 1 && ($part[0] == '+' || $part[0] == '-') && $part[1] != '('
  2079. && preg_match('/[\x81-\xfe]/', $part)) {
  2080. $newQuery .= substr($part, 0, 1) . '(' . substr($part, 1) . ')';
  2081. continue;
  2082. }
  2083. $newQuery .= $part;
  2084. }
  2085. return XS::convert($newQuery, 'UTF-8', $this->_charset);
  2086. }
  2087. private function regQueryPrefix($name)
  2088. {
  2089. if (!isset($this->_prefix[$name])
  2090. && ($field = $this->xs->getField($name, false))
  2091. && ($field->vno != XSFieldScheme::MIXED_VNO)) {
  2092. $type = $field->isBoolIndex() ? XS_CMD_PREFIX_BOOLEAN : XS_CMD_PREFIX_NORMAL;
  2093. $cmd = new XSCommand(XS_CMD_QUERY_PREFIX, $type, $field->vno, $name);
  2094. $this->execCommand($cmd);
  2095. $this->_prefix[$name] = true;
  2096. }
  2097. }
  2098. private function initSpecialField()
  2099. {
  2100. if ($this->_fieldSet === true) {
  2101. return;
  2102. }
  2103. foreach ($this->xs->getAllFields() as $field) /* @var $field XSFieldMeta */ {
  2104. if ($field->cutlen != 0) {
  2105. $len = min(127, ceil($field->cutlen / 10));
  2106. $cmd = new XSCommand(XS_CMD_SEARCH_SET_CUT, $len, $field->vno);
  2107. $this->execCommand($cmd);
  2108. }
  2109. if ($field->isNumeric()) {
  2110. $cmd = new XSCommand(XS_CMD_SEARCH_SET_NUMERIC, 0, $field->vno);
  2111. $this->execCommand($cmd);
  2112. }
  2113. }
  2114. $this->_fieldSet = true;
  2115. }
  2116. private function cleanFieldQuery($query)
  2117. {
  2118. $query = strtr($query, array(' AND ' => ' ', ' OR ' => ' '));
  2119. if (strpos($query, ':') !== false) {
  2120. $regex = '/(^|\s)([0-9A-Za-z_\.-]+):([^\s]+)/';
  2121. return preg_replace_callback($regex, array($this, 'cleanFieldCallback'), $query);
  2122. }
  2123. return $query;
  2124. }
  2125. private function cleanFieldCallback($match)
  2126. {
  2127. if (($field = $this->xs->getField($match[2], false)) === false) {
  2128. return $match[0];
  2129. }
  2130. if ($field->isBoolIndex()) {
  2131. return '';
  2132. }
  2133. if (substr($match[3], 0, 1) == '(' && substr($match[3], -1, 1) == ')') {
  2134. $match[3] = substr($match[3], 1, -1);
  2135. }
  2136. return $match[1] . $match[3];
  2137. }
  2138. private function initHighlight()
  2139. {
  2140. $terms = array();
  2141. $tmps = $this->terms($this->_highlight, false);
  2142. for ($i = 0; $i < count($tmps); $i++) {
  2143. if (strlen($tmps[$i]) !== 6 || ord(substr($tmps[$i], 0, 1)) < 0xc0) {
  2144. $terms[] = XS::convert($tmps[$i], $this->_charset, 'UTF-8');
  2145. continue;
  2146. }
  2147. for ($j = $i + 1; $j < count($tmps); $j++) {
  2148. if (strlen($tmps[$j]) !== 6 || substr($tmps[$j], 0, 3) !== substr($tmps[$j - 1], 3, 3)) {
  2149. break;
  2150. }
  2151. }
  2152. if (($k = ($j - $i)) === 1) {
  2153. $terms[] = XS::convert($tmps[$i], $this->_charset, 'UTF-8');
  2154. } else {
  2155. $i = $j - 1;
  2156. while ($k--) {
  2157. $j--;
  2158. if ($k & 1) {
  2159. $terms[] = XS::convert(substr($tmps[$j - 1], 0, 3) . $tmps[$j], $this->_charset, 'UTF-8');
  2160. }
  2161. $terms[] = XS::convert($tmps[$j], $this->_charset, 'UTF-8');
  2162. }
  2163. }
  2164. }
  2165. $pattern = $replace = $pairs = array();
  2166. foreach ($terms as $term) {
  2167. if (!preg_match('/[a-zA-Z]/', $term)) {
  2168. $pairs[$term] = '<em>' . $term . '</em>';
  2169. } else {
  2170. $pattern[] = '/' . strtr($term, array('+' => '\\+', '/' => '\\/')) . '/i';
  2171. $replace[] = '<em>$0</em>';
  2172. }
  2173. }
  2174. $this->_highlight = array();
  2175. if (count($pairs) > 0) {
  2176. $this->_highlight['pairs'] = $pairs;
  2177. }
  2178. if (count($pattern) > 0) {
  2179. $this->_highlight['pattern'] = $pattern;
  2180. $this->_highlight['replace'] = $replace;
  2181. }
  2182. }
  2183. private function formatValueRange($match)
  2184. {
  2185. $field = $this->xs->getField(intval($match[2]), false);
  2186. if ($field === false) {
  2187. return $match[0];
  2188. }
  2189. $val1 = $val2 = '~';
  2190. if (isset($match[4])) {
  2191. $val2 = $field->isNumeric() ? $this->xapianUnserialise($match[4]) : $match[4];
  2192. }
  2193. if ($match[1] === 'VALUE_LE') {
  2194. $val2 = $field->isNumeric() ? $this->xapianUnserialise($match[3]) : $match[3];
  2195. } else {
  2196. $val1 = $field->isNumeric() ? $this->xapianUnserialise($match[3]) : $match[3];
  2197. }
  2198. return $field->name . ':[' . $val1 . ',' . $val2 . ']';
  2199. }
  2200. private function numfromstr($str, $index)
  2201. {
  2202. return $index < strlen($str) ? ord($str[$index]) : 0;
  2203. }
  2204. private function xapianUnserialise($value)
  2205. {
  2206. if ($value === "\x80") {
  2207. return 0.0;
  2208. }
  2209. if ($value === str_repeat("\xff", 9)) {
  2210. return INF;
  2211. }
  2212. if ($value === '') {
  2213. return -INF;
  2214. }
  2215. $i = 0;
  2216. $c = ord($value[0]);
  2217. $c ^= ($c & 0xc0) >> 1;
  2218. $negative = !($c & 0x80) ? 1 : 0;
  2219. $exponent_negative = ($c & 0x40) ? 1 : 0;
  2220. $explen = !($c & 0x20) ? 1 : 0;
  2221. $exponent = $c & 0x1f;
  2222. if (!$explen) {
  2223. $exponent >>= 2;
  2224. if ($negative ^ $exponent_negative) {
  2225. $exponent ^= 0x07;
  2226. }
  2227. } else {
  2228. $c = $this->numfromstr($value, ++$i);
  2229. $exponent <<= 6;
  2230. $exponent |= ($c >> 2);
  2231. if ($negative ^ $exponent_negative) {
  2232. $exponent &= 0x07ff;
  2233. }
  2234. }
  2235. $word1 = ($c & 0x03) << 24;
  2236. $word1 |= $this->numfromstr($value, ++$i) << 16;
  2237. $word1 |= $this->numfromstr($value, ++$i) << 8;
  2238. $word1 |= $this->numfromstr($value, ++$i);
  2239. $word2 = 0;
  2240. if ($i < strlen($value)) {
  2241. $word2 = $this->numfromstr($value, ++$i) << 24;
  2242. $word2 |= $this->numfromstr($value, ++$i) << 16;
  2243. $word2 |= $this->numfromstr($value, ++$i) << 8;
  2244. $word2 |= $this->numfromstr($value, ++$i);
  2245. }
  2246. if (!$negative) {
  2247. $word1 |= 1 << 26;
  2248. } else {
  2249. $word1 = 0 - $word1;
  2250. if ($word2 != 0) {
  2251. ++$word1;
  2252. }
  2253. $word2 = 0 - $word2;
  2254. $word1 &= 0x03ffffff;
  2255. }
  2256. $mantissa = 0;
  2257. if ($word2) {
  2258. $mantissa = $word2 / 4294967296.0; // 1<<32
  2259. }
  2260. $mantissa += $word1;
  2261. $mantissa /= 1 << ($negative === 1 ? 26 : 27);
  2262. if ($exponent_negative) {
  2263. $exponent = 0 - $exponent;
  2264. }
  2265. $exponent += 8;
  2266. if ($negative) {
  2267. $mantissa = 0 - $mantissa;
  2268. }
  2269. return round($mantissa * pow(2, $exponent), 2);
  2270. }
  2271. private function isRobotAgent()
  2272. {
  2273. if (isset($_SERVER['HTTP_USER_AGENT'])) {
  2274. $agent = strtolower($_SERVER['HTTP_USER_AGENT']);
  2275. $keys = array('bot', 'slurp', 'spider', 'crawl', 'curl');
  2276. foreach ($keys as $key) {
  2277. if (strpos($agent, $key) !== false) {
  2278. return true;
  2279. }
  2280. }
  2281. }
  2282. return false;
  2283. }
  2284. }
  2285. class XSCommand extends XSComponent
  2286. {
  2287. public $cmd = XS_CMD_NONE;
  2288. public $arg1 = 0;
  2289. public $arg2 = 0;
  2290. public $buf = '';
  2291. public $buf1 = '';
  2292. public function __construct($cmd, $arg1 = 0, $arg2 = 0, $buf = '', $buf1 = '')
  2293. {
  2294. if (is_array($cmd)) {
  2295. foreach ($cmd as $key => $value) {
  2296. if ($key === 'arg' || property_exists($this, $key)) {
  2297. $this->$key = $value;
  2298. }
  2299. }
  2300. } else {
  2301. $this->cmd = $cmd;
  2302. $this->arg1 = $arg1;
  2303. $this->arg2 = $arg2;
  2304. $this->buf = $buf;
  2305. $this->buf1 = $buf1;
  2306. }
  2307. }
  2308. public function __toString()
  2309. {
  2310. if (strlen($this->buf1) > 0xff) {
  2311. $this->buf1 = substr($this->buf1, 0, 0xff);
  2312. }
  2313. return pack('CCCCI', $this->cmd, $this->arg1, $this->arg2, strlen($this->buf1), strlen($this->buf)) . $this->buf . $this->buf1;
  2314. }
  2315. public function getArg()
  2316. {
  2317. return $this->arg2 | ($this->arg1 << 8);
  2318. }
  2319. public function setArg($arg)
  2320. {
  2321. $this->arg1 = ($arg >> 8) & 0xff;
  2322. $this->arg2 = $arg & 0xff;
  2323. }
  2324. }
  2325. class XSServer extends XSComponent
  2326. {
  2327. const FILE = 0x01;
  2328. const BROKEN = 0x02;
  2329. public $xs;
  2330. protected $_sock, $_conn;
  2331. protected $_flag;
  2332. protected $_project;
  2333. protected $_sendBuffer;
  2334. public function __construct($conn = null, $xs = null)
  2335. {
  2336. $this->xs = $xs;
  2337. if ($conn !== null) {
  2338. $this->open($conn);
  2339. }
  2340. }
  2341. public function __destruct()
  2342. {
  2343. $this->xs = null;
  2344. $this->close();
  2345. }
  2346. public function open($conn)
  2347. {
  2348. $this->close();
  2349. $this->_conn = $conn;
  2350. $this->_flag = self::BROKEN;
  2351. $this->_sendBuffer = '';
  2352. $this->_project = null;
  2353. $this->connect();
  2354. $this->_flag ^= self::BROKEN;
  2355. if ($this->xs instanceof XS) {
  2356. $this->setProject($this->xs->getName());
  2357. }
  2358. }
  2359. public function reopen($force = false)
  2360. {
  2361. if ($this->_flag & self::BROKEN || $force === true) {
  2362. $this->open($this->_conn);
  2363. }
  2364. return $this;
  2365. }
  2366. public function close($ioerr = false)
  2367. {
  2368. if ($this->_sock && !($this->_flag & self::BROKEN)) {
  2369. if (!$ioerr && $this->_sendBuffer !== '') {
  2370. $this->write($this->_sendBuffer);
  2371. $this->_sendBuffer = '';
  2372. }
  2373. if (!$ioerr && !($this->_flag & self::FILE)) {
  2374. $cmd = new XSCommand(XS_CMD_QUIT);
  2375. fwrite($this->_sock, $cmd);
  2376. }
  2377. fclose($this->_sock);
  2378. $this->_flag |= self::BROKEN;
  2379. }
  2380. }
  2381. public function getConnString()
  2382. {
  2383. $str = $this->_conn;
  2384. if (is_int($str) || is_numeric($str)) {
  2385. $str = 'localhost:' . $str;
  2386. } elseif (strpos($str, ':') === false) {
  2387. $str = 'unix://' . $str;
  2388. }
  2389. return $str;
  2390. }
  2391. public function getSocket()
  2392. {
  2393. return $this->_sock;
  2394. }
  2395. public function getProject()
  2396. {
  2397. return $this->_project;
  2398. }
  2399. public function setProject($name, $home = '')
  2400. {
  2401. if ($name !== $this->_project) {
  2402. $cmd = array('cmd' => XS_CMD_USE, 'buf' => $name, 'buf1' => $home);
  2403. $this->execCommand($cmd, XS_CMD_OK_PROJECT);
  2404. $this->_project = $name;
  2405. }
  2406. }
  2407. public function setTimeout($sec)
  2408. {
  2409. $cmd = array('cmd' => XS_CMD_TIMEOUT, 'arg' => $sec);
  2410. $this->execCommand($cmd, XS_CMD_OK_TIMEOUT_SET);
  2411. }
  2412. public function execCommand($cmd, $res_arg = XS_CMD_NONE, $res_cmd = XS_CMD_OK)
  2413. {
  2414. if (!$cmd instanceof XSCommand) {
  2415. $cmd = new XSCommand($cmd);
  2416. }
  2417. if ($cmd->cmd & 0x80) {
  2418. $this->_sendBuffer .= $cmd;
  2419. return true;
  2420. }
  2421. $buf = $this->_sendBuffer . $cmd;
  2422. $this->_sendBuffer = '';
  2423. $this->write($buf);
  2424. if ($this->_flag & self::FILE) {
  2425. return true;
  2426. }
  2427. $res = $this->getRespond();
  2428. if ($res->cmd === XS_CMD_ERR && $res_cmd != XS_CMD_ERR) {
  2429. throw new XSException($res->buf, $res->arg);
  2430. }
  2431. if ($res->cmd != $res_cmd || ($res_arg != XS_CMD_NONE && $res->arg != $res_arg)) {
  2432. throw new XSException('Unexpected respond {CMD:' . $res->cmd . ', ARG:' . $res->arg . '}');
  2433. }
  2434. return $res;
  2435. }
  2436. public function sendCommand($cmd)
  2437. {
  2438. if (!$cmd instanceof XSCommand) {
  2439. $cmd = new XSCommand($cmd);
  2440. }
  2441. $this->write(strval($cmd));
  2442. }
  2443. public function getRespond()
  2444. {
  2445. $buf = $this->read(8);
  2446. $hdr = unpack('Ccmd/Carg1/Carg2/Cblen1/Iblen', $buf);
  2447. $res = new XSCommand($hdr);
  2448. $res->buf = $this->read($hdr['blen']);
  2449. $res->buf1 = $this->read($hdr['blen1']);
  2450. return $res;
  2451. }
  2452. public function hasRespond()
  2453. {
  2454. if ($this->_sock === null || $this->_flag & (self::BROKEN | self::FILE)) {
  2455. return false;
  2456. }
  2457. $wfds = $xfds = array();
  2458. $rfds = array($this->_sock);
  2459. $res = stream_select($rfds, $wfds, $xfds, 0, 0);
  2460. return $res > 0;
  2461. }
  2462. protected function write($buf, $len = 0)
  2463. {
  2464. $buf = strval($buf);
  2465. if ($len == 0 && ($len = $size = strlen($buf)) == 0) {
  2466. return true;
  2467. }
  2468. $this->check();
  2469. while (true) {
  2470. $bytes = fwrite($this->_sock, $buf, $len);
  2471. if ($bytes === false || $bytes === 0 || $bytes === $len) {
  2472. break;
  2473. }
  2474. $len -= $bytes;
  2475. $buf = substr($buf, $bytes);
  2476. }
  2477. if ($bytes === false || $bytes === 0) {
  2478. $meta = stream_get_meta_data($this->_sock);
  2479. $this->close(true);
  2480. $reason = $meta['timed_out'] ? 'timeout' : ($meta['eof'] ? 'closed' : 'unknown');
  2481. $msg = 'Failed to send the data to server completely ';
  2482. $msg .= '(SIZE:' . ($size - $len) . '/' . $size . ', REASON:' . $reason . ')';
  2483. throw new XSException($msg);
  2484. }
  2485. }
  2486. protected function read($len)
  2487. {
  2488. if ($len == 0) {
  2489. return '';
  2490. }
  2491. $this->check();
  2492. for ($buf = '', $size = $len;;) {
  2493. $bytes = fread($this->_sock, $len);
  2494. if ($bytes === false || strlen($bytes) == 0) {
  2495. break;
  2496. }
  2497. $len -= strlen($bytes);
  2498. $buf .= $bytes;
  2499. if ($len === 0) {
  2500. return $buf;
  2501. }
  2502. }
  2503. $meta = stream_get_meta_data($this->_sock);
  2504. $this->close(true);
  2505. $reason = $meta['timed_out'] ? 'timeout' : ($meta['eof'] ? 'closed' : 'unknown');
  2506. $msg = 'Failed to recv the data from server completely ';
  2507. $msg .= '(SIZE:' . ($size - $len) . '/' . $size . ', REASON:' . $reason . ')';
  2508. throw new XSException($msg);
  2509. }
  2510. protected function check()
  2511. {
  2512. if ($this->_sock === null) {
  2513. throw new XSException('No server connection');
  2514. }
  2515. if ($this->_flag & self::BROKEN) {
  2516. throw new XSException('Broken server connection');
  2517. }
  2518. }
  2519. protected function connect()
  2520. {
  2521. $conn = $this->_conn;
  2522. if (is_int($conn) || is_numeric($conn)) {
  2523. $host = 'localhost';
  2524. $port = intval($conn);
  2525. } elseif (!strncmp($conn, 'file://', 7)) {
  2526. $conn = substr($conn, 7);
  2527. if (($sock = @fopen($conn, 'wb')) === false) {
  2528. throw new XSException('Failed to open local file for writing: `' . $conn . '\'');
  2529. }
  2530. $this->_flag |= self::FILE;
  2531. $this->_sock = $sock;
  2532. return;
  2533. } elseif (($pos = strpos($conn, ':')) !== false) {
  2534. $host = substr($conn, 0, $pos);
  2535. $port = intval(substr($conn, $pos + 1));
  2536. } else {
  2537. $host = 'unix://' . $conn;
  2538. $port = -1;
  2539. }
  2540. if (($sock = @fsockopen($host, $port, $errno, $error, 5)) === false) {
  2541. throw new XSException($error . '(C#' . $errno . ', ' . $host . ':' . $port . ')');
  2542. }
  2543. $timeout = ini_get('max_execution_time');
  2544. $timeout = $timeout > 0 ? ($timeout - 1) : 30;
  2545. stream_set_blocking($sock, true);
  2546. stream_set_timeout($sock, $timeout);
  2547. $this->_sock = $sock;
  2548. }
  2549. }
  2550. interface XSTokenizer
  2551. {
  2552. const DFL = 0;
  2553. public function getTokens($value, XSDocument $doc = null);
  2554. }
  2555. class XSTokenizerNone implements XSTokenizer
  2556. {
  2557. public function getTokens($value, XSDocument $doc = null)
  2558. {
  2559. return array();
  2560. }
  2561. }
  2562. class XSTokenizerFull implements XSTokenizer
  2563. {
  2564. public function getTokens($value, XSDocument $doc = null)
  2565. {
  2566. return array($value);
  2567. }
  2568. }
  2569. class XSTokenizerSplit implements XSTokenizer
  2570. {
  2571. private $arg = ' ';
  2572. public function __construct($arg = null)
  2573. {
  2574. if ($arg !== null && $arg !== '') {
  2575. $this->arg = $arg;
  2576. }
  2577. }
  2578. public function getTokens($value, XSDocument $doc = null)
  2579. {
  2580. if (strlen($this->arg) > 2 && substr($this->arg, 0, 1) == '/' && substr($this->arg, -1, 1) == '/') {
  2581. return preg_split($this->arg, $value);
  2582. }
  2583. return explode($this->arg, $value);
  2584. }
  2585. }
  2586. class XSTokenizerXlen implements XSTokenizer
  2587. {
  2588. private $arg = 2;
  2589. public function __construct($arg = null)
  2590. {
  2591. if ($arg !== null && $arg !== '') {
  2592. $this->arg = intval($arg);
  2593. if ($this->arg < 1 || $this->arg > 255) {
  2594. throw new XSException('Invalid argument for ' . __CLASS__ . ': ' . $arg);
  2595. }
  2596. }
  2597. }
  2598. public function getTokens($value, XSDocument $doc = null)
  2599. {
  2600. $terms = array();
  2601. for ($i = 0; $i < strlen($value); $i += $this->arg) {
  2602. $terms[] = substr($value, $i, $this->arg);
  2603. }
  2604. return $terms;
  2605. }
  2606. }
  2607. class XSTokenizerXstep implements XSTokenizer
  2608. {
  2609. private $arg = 2;
  2610. public function __construct($arg = null)
  2611. {
  2612. if ($arg !== null && $arg !== '') {
  2613. $this->arg = intval($arg);
  2614. if ($this->arg < 1 || $this->arg > 255) {
  2615. throw new XSException('Invalid argument for ' . __CLASS__ . ': ' . $arg);
  2616. }
  2617. }
  2618. }
  2619. public function getTokens($value, XSDocument $doc = null)
  2620. {
  2621. $terms = array();
  2622. $i = $this->arg;
  2623. while (true) {
  2624. $terms[] = substr($value, 0, $i);
  2625. if ($i >= strlen($value)) {
  2626. break;
  2627. }
  2628. $i += $this->arg;
  2629. }
  2630. return $terms;
  2631. }
  2632. }
  2633. class XSTokenizerScws implements XSTokenizer
  2634. {
  2635. const MULTI_MASK = 15;
  2636. private static $_charset;
  2637. private $_setting = array();
  2638. private static $_server;
  2639. public function __construct($arg = null)
  2640. {
  2641. if (self::$_server === null) {
  2642. $xs = XS::getLastXS();
  2643. if ($xs === null) {
  2644. throw new XSException('An XS instance should be created before using ' . __CLASS__);
  2645. }
  2646. self::$_server = $xs->getScwsServer();
  2647. self::$_server->setTimeout(0);
  2648. self::$_charset = $xs->getDefaultCharset();
  2649. if (!defined('SCWS_MULTI_NONE')) {
  2650. define('SCWS_MULTI_NONE', 0);
  2651. define('SCWS_MULTI_SHORT', 1);
  2652. define('SCWS_MULTI_DUALITY', 2);
  2653. define('SCWS_MULTI_ZMAIN', 4);
  2654. define('SCWS_MULTI_ZALL', 8);
  2655. }
  2656. if (!defined('SCWS_XDICT_XDB')) {
  2657. define('SCWS_XDICT_XDB', 1);
  2658. define('SCWS_XDICT_MEM', 2);
  2659. define('SCWS_XDICT_TXT', 4);
  2660. }
  2661. }
  2662. if ($arg !== null && $arg !== '') {
  2663. $this->setMulti($arg);
  2664. }
  2665. }
  2666. public function getTokens($value, XSDocument $doc = null)
  2667. {
  2668. $tokens = array();
  2669. $this->setIgnore(true);
  2670. $_charset = self::$_charset;
  2671. self::$_charset = 'UTF-8';
  2672. $words = $this->getResult($value);
  2673. foreach ($words as $word) {
  2674. $tokens[] = $word['word'];
  2675. }
  2676. self::$_charset = $_charset;
  2677. return $tokens;
  2678. }
  2679. public function setCharset($charset)
  2680. {
  2681. self::$_charset = strtoupper($charset);
  2682. if (self::$_charset == 'UTF8') {
  2683. self::$_charset = 'UTF-8';
  2684. }
  2685. return $this;
  2686. }
  2687. public function setIgnore($yes = true)
  2688. {
  2689. $this->_setting['ignore'] = new XSCommand(XS_CMD_SEARCH_SCWS_SET, XS_CMD_SCWS_SET_IGNORE, $yes === false
  2690. ? 0 : 1);
  2691. return $this;
  2692. }
  2693. public function setMulti($mode = 3)
  2694. {
  2695. $mode = intval($mode) & self::MULTI_MASK;
  2696. $this->_setting['multi'] = new XSCommand(XS_CMD_SEARCH_SCWS_SET, XS_CMD_SCWS_SET_MULTI, $mode);
  2697. return $this;
  2698. }
  2699. public function setDict($fpath, $mode = null)
  2700. {
  2701. if (!is_int($mode)) {
  2702. $mode = stripos($fpath, '.txt') !== false ? SCWS_XDICT_TXT : SCWS_XDICT_XDB;
  2703. }
  2704. $this->_setting['set_dict'] = new XSCommand(XS_CMD_SEARCH_SCWS_SET, XS_CMD_SCWS_SET_DICT, $mode, $fpath);
  2705. unset($this->_setting['add_dict']);
  2706. return $this;
  2707. }
  2708. public function addDict($fpath, $mode = null)
  2709. {
  2710. if (!is_int($mode)) {
  2711. $mode = stripos($fpath, '.txt') !== false ? SCWS_XDICT_TXT : SCWS_XDICT_XDB;
  2712. }
  2713. if (!isset($this->_setting['add_dict'])) {
  2714. $this->_setting['add_dict'] = array();
  2715. }
  2716. $this->_setting['add_dict'][] = new XSCommand(XS_CMD_SEARCH_SCWS_SET, XS_CMD_SCWS_ADD_DICT, $mode, $fpath);
  2717. return $this;
  2718. }
  2719. public function setDuality($yes = true)
  2720. {
  2721. $this->_setting['duality'] = new XSCommand(XS_CMD_SEARCH_SCWS_SET, XS_CMD_SCWS_SET_DUALITY, $yes === false
  2722. ? 0 : 1);
  2723. return $this;
  2724. }
  2725. public function getVersion()
  2726. {
  2727. $cmd = new XSCommand(XS_CMD_SEARCH_SCWS_GET, XS_CMD_SCWS_GET_VERSION);
  2728. $res = self::$_server->execCommand($cmd, XS_CMD_OK_INFO);
  2729. return $res->buf;
  2730. }
  2731. public function getResult($text)
  2732. {
  2733. $words = array();
  2734. $text = $this->applySetting($text);
  2735. $cmd = new XSCommand(XS_CMD_SEARCH_SCWS_GET, XS_CMD_SCWS_GET_RESULT, 0, $text);
  2736. $res = self::$_server->execCommand($cmd, XS_CMD_OK_SCWS_RESULT);
  2737. while ($res->buf !== '') {
  2738. $tmp = unpack('Ioff/a4attr/a*word', $res->buf);
  2739. $tmp['word'] = XS::convert($tmp['word'], self::$_charset, 'UTF-8');
  2740. $words[] = $tmp;
  2741. $res = self::$_server->getRespond();
  2742. }
  2743. return $words;
  2744. }
  2745. public function getTops($text, $limit = 10, $xattr = '')
  2746. {
  2747. $words = array();
  2748. $text = $this->applySetting($text);
  2749. $cmd = new XSCommand(XS_CMD_SEARCH_SCWS_GET, XS_CMD_SCWS_GET_TOPS, $limit, $text, $xattr);
  2750. $res = self::$_server->execCommand($cmd, XS_CMD_OK_SCWS_TOPS);
  2751. while ($res->buf !== '') {
  2752. $tmp = unpack('Itimes/a4attr/a*word', $res->buf);
  2753. $tmp['word'] = XS::convert($tmp['word'], self::$_charset, 'UTF-8');
  2754. $words[] = $tmp;
  2755. $res = self::$_server->getRespond();
  2756. }
  2757. return $words;
  2758. }
  2759. public function hasWord($text, $xattr)
  2760. {
  2761. $text = $this->applySetting($text);
  2762. $cmd = new XSCommand(XS_CMD_SEARCH_SCWS_GET, XS_CMD_SCWS_HAS_WORD, 0, $text, $xattr);
  2763. $res = self::$_server->execCommand($cmd, XS_CMD_OK_INFO);
  2764. return $res->buf === 'OK';
  2765. }
  2766. private function applySetting($text)
  2767. {
  2768. self::$_server->reopen();
  2769. foreach ($this->_setting as $key => $cmd) {
  2770. if (is_array($cmd)) {
  2771. foreach ($cmd as $_cmd) {
  2772. self::$_server->execCommand($_cmd);
  2773. }
  2774. } else {
  2775. self::$_server->execCommand($cmd);
  2776. }
  2777. }
  2778. return XS::convert($text, 'UTF-8', self::$_charset);
  2779. }
  2780. }