Logger.php 8.6 KB

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