XS.php 76 KB

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