Logger.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. #!/usr/bin/env php
  2. <?php
  3. /**
  4. * Xunsearch PHP-SDK 搜索日志管理工具
  5. *
  6. * @author hpxl
  7. * @link http://www.xunsearch.com/
  8. * @copyright Copyright &copy; 2011 HangZhou YunSheng Network Technology Co., Ltd.
  9. * @license http://www.xunsearch.com/license/
  10. * @version $Id$
  11. */
  12. require_once dirname(__FILE__) . '/../lib/XS.php';
  13. require_once dirname(__FILE__) . '/XSUtil.class.php';
  14. // check arguments
  15. XSUtil::parseOpt(array('p', 'c', 'project', 'charset', 'hot', 'del', 'put', 'query', 'import', 'limit'));
  16. $project = XSUtil::getOpt('p', 'project', true);
  17. $query = XSUtil::getOpt(null, 'query', true);
  18. // magick output charset
  19. $charset = XSUtil::getOpt('c', 'charset');
  20. XSUtil::setCharset($charset);
  21. // long options
  22. $params = array('import', 'del', 'put', 'flush', 'hot', 'clean', 'limit');
  23. foreach ($params as $_) {
  24. $k = strtr($_, '-', '_');
  25. $$k = XSUtil::getOpt(null, $_);
  26. }
  27. if ($query === null && $put === null && $del === null
  28. && $flush === null && $import === null
  29. && $hot === null && $clean === null) {
  30. $hot = 'total';
  31. }
  32. // help message
  33. if (XSUtil::getOpt('h', 'help') !== null || !is_string($project)
  34. || ($import !== null && !is_string($import)) || ($query !== null && !is_string($query))
  35. || ($put !== null && !is_string($put)) || ($del !== null && !is_string($del))) {
  36. $version = PACKAGE_NAME . '/' . PACKAGE_VERSION;
  37. echo <<<EOF
  38. Logger - 搜索日志管理工具 ($version)
  39. 用法
  40. {$_SERVER['argv'][0]} [options] [-p|--project] [project] [[--query] <word>]
  41. 选项说明
  42. --project=<name|ini>
  43. -p <project> 用于指定要搜索的项目名称或项目配置文件的路径,
  44. 如果指定的是名称,则使用 ../app/<name>.ini 作为配置文件
  45. --charset=<gbk|utf-8>
  46. -c <charset> 指定您当前在用以及导入数据源的字符集,以便系统进行
  47. 智能转换(默认:UTF-8)
  48. --import=<file> 导入搜索日志文件, 一行一个词, 每行的数据中
  49. 可以用\\t(Tab键)分开指定次数,没有次数默认为1
  50. --put=<word1[:wdf1][,word2[:wdf2]]>
  51. 添加搜索日志词汇,词与次数之间用半角冒号分隔,
  52. 默认为 1 次。多个词之间用半角的逗号分隔,
  53. 词之间如果包含空格,请将参数用引号包围起来。
  54. --del=<word1[,word2[,word3]]>
  55. 删除搜索日志中的词汇记录,若不存在则会给出提示,
  56. 多个词之间用半角的逗号分隔,如果包含空格,请将参数用引号包围起来。
  57. --flush 刷新搜索日志变动,如急着看效果,请调用该选项强制刷新所有提交。
  58. --clean 清空全部搜索日志内容
  59. --limit=<num> 用于控制 query 和 hot 选项的返回关键词个数
  60. --query=<word> 以 word 为关键词查找相关搜索词,用 limit 选项设置个数,默认 6 个
  61. --hot=<total|last|cur>
  62. 列出热门搜索词汇,可结合 limit 指定个数,默认 10 个
  63. 参数依次表示总次数、上期次数、本期次数
  64. -h|--help 显示帮助信息
  65. EOF;
  66. exit(0);
  67. }
  68. // create xs project
  69. $ini = file_exists($project) ? $project : dirname(__FILE__) . '/../app/' . $project . '.ini';
  70. if (!file_exists($ini)) {
  71. echo "错误:无效的项目名称 ($project),不存在相应的配置文件。\n";
  72. exit(-1);
  73. }
  74. try {
  75. $db = XSSearch::LOG_DB;
  76. $log_ready = false;
  77. $xs = new XS($ini);
  78. $xs->setScheme(XSFieldScheme::logger());
  79. $search = $xs->search;
  80. try {
  81. // NOTE: use setQuery to call preQueryString for preparing fieldset
  82. $search->setDb($db)->setQuery('dummy');
  83. $search->setTimeout(0); // sometimes user may import lots of terms
  84. $log_ready = true;
  85. } catch (Exception $e) {
  86. }
  87. // hot, query ==> read-only
  88. if ($hot !== null) {
  89. $limit = $limit === null ? 10 : intval($limit);
  90. $type = $hot === 'cur' ? 'currnum' : ($hot === 'last' ? 'lastnum' : 'total');
  91. $result = $search->getHotQuery($limit, $type);
  92. if (count($result) === 0) {
  93. echo "暂无相关热门搜索记录。\n";
  94. } else {
  95. $i = 1;
  96. printf("序 %s %s\n%s\n", XSUtil::fixWidth('搜索热门关键词(' . $type . ')', 40),
  97. XSUtil::fixWidth('次数', 10), XSUtil::fixWidth('', 56, '-'));
  98. foreach ($result as $word => $freq) {
  99. printf("%2d. %s %d\n", $i, XSUtil::fixWidth($word, 40), $freq);
  100. $i++;
  101. }
  102. }
  103. } elseif ($query !== null) {
  104. $query = XSUtil::convertIn($query);
  105. $limit = $limit === null ? 6 : intval($limit);
  106. $result = $log_ready ? $search->setFuzzy()->setLimit($limit)->search($query) : array();
  107. if (count($result) === 0) {
  108. echo "目前还没有与 \033[7m" . $query . "\033[m 相关的搜索词。\n";
  109. } else {
  110. printf("序 %s %s\n%s\n", XSUtil::fixWidth("相关搜索词($query)", 41), XSUtil::fixWidth('次数', 10),
  111. XSUtil::fixWidth('', 50, '-'));
  112. for ($i = 0, $total = count($result); $i < $total; $i++) {
  113. printf("%2d. %s %s\n", $i + 1, XSUtil::fixWidth($result[$i]->body, 40), $result[$i]->total);
  114. }
  115. }
  116. } else {
  117. // check clean
  118. if ($clean !== null) {
  119. echo "清空已有搜索日志数据 ...\n";
  120. $xs->index->setDb($db)->clean();
  121. }
  122. // import from file
  123. if ($import !== null) {
  124. if (!file_exists($import) || !($fd = @fopen($import, "r"))) {
  125. echo "要导入的文件 [$import] 不存在或无法读取!\n";
  126. } else {
  127. $search->setTimeout(0);
  128. echo "开始导入搜索日志文件 ...\n";
  129. while (($line = fgets($fd, 1024)) !== false) {
  130. if ($line[0] === '#' || $line[0] === ';') {
  131. continue;
  132. }
  133. if (($pos = strpos($line, "\t")) !== false) {
  134. $word = trim(substr($line, 0, $pos));
  135. $wdf = intval(substr($line, $pos + 1));
  136. } else {
  137. $word = trim($line);
  138. $wdf = 1;
  139. }
  140. addSearchLog($word, $wdf);
  141. }
  142. fclose($fd);
  143. }
  144. }
  145. // delete word
  146. if ($del !== null) {
  147. $limit = $limit === null ? 3 : intval($limit);
  148. $del = XSUtil::convertIn($del);
  149. foreach (explode(",", $del) as $word) {
  150. $word = trim($word);
  151. if ($word === '') {
  152. continue;
  153. }
  154. if ($log_ready) {
  155. $search->setQuery(null)->addQueryTerm('id', $word);
  156. if ($search->count() === 1) {
  157. $xs->index->setDb($db)->del($word);
  158. echo "成功删除 \033[7m$word\033[m!\n";
  159. continue;
  160. }
  161. }
  162. echo "不存在 \033[7m$word\033[m,";
  163. $docs = $log_ready ? $search->setFuzzy()->setLimit($limit)->search($word) : array();
  164. if (count($docs) === 0) {
  165. echo "并且没有相关的搜索词。";
  166. } else {
  167. echo "相关词:";
  168. foreach ($docs as $doc) {
  169. echo "\033[7m" . $doc->body . "\033[m ";
  170. }
  171. }
  172. echo "\n";
  173. }
  174. } elseif ($put !== null) {
  175. echo "开始增加/更新搜索词 ... \n";
  176. $put = XSUtil::convertIn($put);
  177. foreach (explode(',', $put) as $tmp) {
  178. if (($pos = strpos($tmp, ':')) !== false) {
  179. $word = trim(substr($tmp, 0, $pos));
  180. $wdf = intval(substr($tmp, $pos + 1));
  181. } else {
  182. $word = trim($tmp);
  183. $wdf = 1;
  184. }
  185. add_search_log($word, $wdf);
  186. }
  187. }
  188. // check flush
  189. if ($flush !== null || $del !== null) {
  190. echo "刷新已提交的日志索引 ...";
  191. $res = $xs->index->setDb($db)->flushIndex();
  192. for ($i = 0; $i < 3 && $flush !== null; $i++) {
  193. echo ".";
  194. XSUtil::flush();
  195. sleep(1);
  196. }
  197. echo $res ? " 成功\n" : " 失败\n";
  198. }
  199. if ($flush !== null || $import !== null || $put !== null) {
  200. echo "强制刷新未处理的日志记录 ... ";
  201. echo $xs->index->flushLogging() ? "成功" : "失败";
  202. echo "\n注意:后台更新需要一些时间,并不是实时完成!\n";
  203. }
  204. }
  205. } catch (XSException $e) {
  206. // Exception
  207. $start = dirname(dirname(__FILE__));
  208. $relative = XSException::getRelPath($start);
  209. $traceString = $e->getTraceAsString();
  210. $traceString = str_replace(dirname(__FILE__) . '/', '', $traceString);
  211. $traceString = str_replace($start . ($relative === '' ? '/' : ''), $relative, $traceString);
  212. echo $e . "\n" . $traceString . "\n";
  213. }
  214. // local function add word
  215. function addSearchLog($word, $wdf)
  216. {
  217. global $search, $log_ready;
  218. static $record = array();
  219. if ($word !== '' && $wdf > 0) {
  220. if (!isset($record[$word]) && $log_ready) {
  221. $docs = $search->setQuery(null)->addQueryTerm('id', $word)->search();
  222. if (isset($docs[0])) {
  223. $record[$word] = $docs[0]->total;
  224. }
  225. }
  226. if (isset($record[$word])) {
  227. echo "更新 \033[7m$word\033[m 的次数:" . $record[$word] . " + " . $wdf . "\n";
  228. $record[$word] += $wdf;
  229. } else {
  230. echo "新增 \033[7m$word\033[m 次数:$wdf\n";
  231. $record[$word] = $wdf;
  232. }
  233. $search->addSearchLog($word, $wdf);
  234. }
  235. }