Example #1
0
 /**
  * Initializes, create XS object
  */
 public function init()
 {
     $this->xs = new \XS($this->iniFile);
     if ($this->charset !== null) {
         $this->xs->setDefaultCharset($this->charset);
     }
 }
Example #2
0
 /**
  * 把用户输入的字符串转换为 UTF-8 编码
  * @param string $buf 要转换字符串
  * @return string 转换后的字符串
  */
 public static function convertIn($buf)
 {
     if (self::$charset !== null) {
         return XS::convert($buf, 'UTF-8', self::$charset);
     }
     return $buf;
 }
 public function testSetCharset()
 {
     $text = '全文检索';
     $tk = $this->object;
     $words = $tk->getResult($text);
     $this->assertEquals($words[0]['word'], $text);
     $gbk = XS::convert($text, 'GBK', 'UTF-8');
     $tk->setCharset('GBK');
     $words = $tk->getResult($gbk);
     $this->assertEquals($words[0]['word'], $gbk);
 }
Example #4
0
 function search()
 {
     if (isset($_GET['prid'])) {
         $this->project_id = intval($_GET['prid']);
     } else {
         $this->project_id = 1;
     }
     if (empty($_GET['q'])) {
         return "请输入搜索的关键词";
     }
     $this->getProjectInfo();
     $this->getProjectLinks();
     $this->getTreeData();
     $_GET['id'] = $this->project['home_id'];
     $pagesize = 10;
     $page = empty($_GET['page']) ? 1 : intval($_GET['page']);
     $xs = new \XS(WEBPATH . '/search.ini');
     $search = $xs->getSearch();
     $q = trim($_GET['q']);
     $search->setQuery($q);
     $total = $search->count();
     if ($page * $pagesize > $total) {
         $page = 1;
     }
     $search->setLimit($pagesize, ($page - 1) * $pagesize);
     $pager = new Swoole\Pager(array('page' => $page, 'perpage' => $pagesize, 'total' => $total));
     $docs = $search->search();
     $list = array();
     foreach ($docs as $doc) {
         $li['id'] = $doc->pid;
         $li['title'] = $doc->subject;
         $li['desc'] = $doc->message;
         $list[] = $li;
     }
     $pager->page_tpl = "/wiki/search/?q=" . urlencode($_GET['q']) . '&page=%s';
     $this->tpl->assign('list', $list);
     $this->tpl->assign('pager', $pager->render());
     $this->tpl->display("wiki/noframe/search.html");
 }
Example #5
0
 public function testAddSearchLog()
 {
     $search = self::$xs->search;
     $search->addSearchLog('php 教程');
     $search->addSearchLog('php 教学');
     $search->addSearchLog('php 教导', 999);
     $search->addSearchLog('php 教程');
     self::$xs->index->reopen(true)->flushLogging();
     sleep(2);
     self::$xs->setScheme(XSFieldScheme::logger());
     $search->reopen(true);
     $docs = $search->setDb(XSSearch::LOG_DB)->search('php');
     $search->setDb(null);
     self::$xs->restoreScheme();
     $this->assertEquals($docs[0]->total, 999);
     $this->assertEquals($docs[1]->total, 2);
     $this->assertEquals($docs[2]->total, 1);
 }
Example #6
0
 public function testLastXS()
 {
     $lastXS = XS::getLastXS();
     $this->assertEquals($this->xs2, $lastXS);
     $this->assertNotEquals($this->xs1, $lastXS);
 }
Example #7
0
    -h|--help           显示帮助信息

EOF;
    exit(0);
}
// create xs project
$ini = XSUtil::toProjectIni($project);
if (!file_exists($ini)) {
    echo "错误:无效的项目名称 ({$project}),不存在相应的配置文件。\n";
    exit(-1);
}
try {
    $db = XSSearch::LOG_DB;
    $log_ready = false;
    $xs = new XS($ini);
    $xs->setScheme(XSFieldScheme::logger());
    $search = $xs->search;
    try {
        // NOTE: use setQuery to call preQueryString for preparing fieldset
        $search->setDb($db)->setQuery('dummy');
        $search->setTimeout(0);
        // sometimes user may import lots of terms
        $log_ready = true;
    } catch (Exception $e) {
    }
    // hot, query ==> read-only
    if ($hot !== null) {
        $limit = $limit === null ? 10 : intval($limit);
        $type = $hot === 'cur' ? 'currnum' : ($hot === 'last' ? 'lastnum' : 'total');
        $result = $search->getHotQuery($limit, $type);
Example #8
0
 public function __construct($mod)
 {
     $mod = PDP_DIR_CONF . '/' . PDP_APP . '/' . $mod . '.ini';
     parent::__construct($mod);
 }
Example #9
0
 /**
  * 初始始化高亮替换数据
  */
 private function initHighlight()
 {
     $terms = array();
     $tmps = $this->terms($this->_highlight, false);
     for ($i = 0; $i < count($tmps); $i++) {
         if (strlen($tmps[$i]) !== 6 || ord(substr($tmps[$i], 0, 1)) < 0xc0) {
             $terms[] = XS::convert($tmps[$i], $this->_charset, 'UTF-8');
             continue;
         }
         // auto fixed duality in libscws
         // ABC => AB,BC => ABC,BC,AB
         // ABCD => AB,BC,CD => CD,ABC,BC,AB
         // ABCDE => AB,BC,CD,DE => CDE,DE,CD,ABC,BC,AB
         for ($j = $i + 1; $j < count($tmps); $j++) {
             if (strlen($tmps[$j]) !== 6 || substr($tmps[$j], 0, 3) !== substr($tmps[$j - 1], 3, 3)) {
                 break;
             }
         }
         if (($k = $j - $i) === 1) {
             $terms[] = XS::convert($tmps[$i], $this->_charset, 'UTF-8');
         } else {
             $i = $j - 1;
             while ($k--) {
                 $j--;
                 if ($k & 1) {
                     $terms[] = XS::convert(substr($tmps[$j - 1], 0, 3) . $tmps[$j], $this->_charset, 'UTF-8');
                 }
                 $terms[] = XS::convert($tmps[$j], $this->_charset, 'UTF-8');
             }
         }
     }
     $pattern = $replace = $pairs = array();
     foreach ($terms as $term) {
         if (!preg_match('/[a-zA-Z]/', $term)) {
             $pairs[$term] = '<em>' . $term . '</em>';
         } else {
             $pattern[] = '/' . strtr($term, array('+' => '\\+', '/' => '\\/')) . '/i';
             $replace[] = '<em>$0</em>';
         }
     }
     $this->_highlight = array();
     if (count($pairs) > 0) {
         $this->_highlight['pairs'] = $pairs;
     }
     if (count($pattern) > 0) {
         $this->_highlight['pattern'] = $pattern;
         $this->_highlight['replace'] = $replace;
     }
 }
Example #10
0
<?php

require dirname(__DIR__) . '/config.php';
require APPSPATH . '/classes/xunsearch/lib/XS.php';
$xs = new XS(WEBPATH . '/search.ini');
$search = $xs->getSearch();
$search->setLimit(2);
$docs = $search->search($argv[1]);
foreach ($docs as $doc) {
    echo '#' . $doc->pid . '. ' . $doc->subject . " [" . $doc->percent() . "%] - ";
    echo date("Y-m-d", $doc->chrono) . "\n";
}
Example #11
0
 private function autoConvert($value)
 {
     if ($this->_charset === null || $this->_charset == 'UTF-8' || !is_string($value) || !preg_match('/[\\x81-\\xfe]/', $value)) {
         return $value;
     }
     $from = $this->_meta === null ? $this->_charset : 'UTF-8';
     $to = $this->_meta === null ? 'UTF-8' : $this->_charset;
     return XS::convert($value, $to, $from);
 }
Example #12
0
 /**
  * 构造函数
  * 特别说明一个小技巧, 参数 $file 可以直接是配置文件的内容, 还可以是仅仅是文件名,
  * 如果只是文件名会自动查找 XS_LIB_ROOT/../app/$file.ini
  * @param string $file 要加载的项目配置文件
  */
 public function __construct($file)
 {
     if (strlen($file) < 255 && !is_file($file)) {
         $appRoot = getenv('XS_APP_ROOT');
         if ($appRoot === false) {
             $appRoot = defined('XS_APP_ROOT') ? XS_APP_ROOT : XS_LIB_ROOT . '/../app';
         }
         $file2 = $appRoot . '/' . $file . '.ini';
         if (is_file($file2)) {
             $file = $file2;
         }
     }
     $this->loadIniFile($file);
     self::$_lastXS = $this;
 }
Example #13
0
 /**
  * 构造函数
  * 特别说明一个小技巧, 参数 $file 可以直接是配置文件的内容, 还可以是仅仅是文件名,
  * 如果只是文件名会自动查找 XS_LIB_ROOT/../app/$file.ini
  * @param string $file 要加载的项目配置文件
  */
 public function __construct($file)
 {
     if (strlen($file) < 255 && !is_file($file)) {
         $file2 = XS_LIB_ROOT . '/../app/' . $file . '.ini';
         if (is_file($file2)) {
             $file = $file2;
         }
     }
     $this->loadIniFile($file);
     self::$_lastXS = $this;
 }
Example #14
0
 public static function setUpBeforeClass()
 {
     self::$data = array('pid' => 1234, 'subject' => "Hello, 测试标题", 'message' => "您好,这儿是真正的测试内容\n另起一行用英文\n\nHello, the world!", 'chrono' => time());
     self::$data_gbk = XS::convert(self::$data, 'GBK', 'UTF-8');
 }
Example #15
0
 public function testGetAddIndex()
 {
     $this->assertNull($this->doc2->getAddIndex('subject2'));
     $this->assertNull($this->doc2->getAddIndex('subject'));
     $subject = new XSFieldMeta('subject');
     $this->doc2->addIndex('subject', 'hello the world');
     $this->doc2->addIndex($subject, XS::convert('您好世界', 'GBK', 'UTF-8'));
     $this->assertEquals(self::$data['subject'], $this->doc2->subject);
     $this->assertEquals("hello the world\n您好世界", $this->doc2->getAddIndex($subject));
 }
Example #16
0
$ini = XSUtil::toProjectIni($project);
if (!file_exists($ini)) {
    echo "错误:无效的项目名称 ({$project}),不存在相应的配置文件。\n";
    exit(-1);
}
// execute the search
try {
    // params
    $params = array('hot', 'suggest', 'correct', 'related', 'output', 'limit');
    foreach ($params as $_) {
        ${$_} = XSUtil::getOpt(null, $_);
    }
    $limit1 = $limit === null ? 10 : intval($limit);
    $db = XSUtil::getOpt('d', 'db');
    // create xs object
    $xs = new XS($ini);
    $search = $xs->search;
    $search->setCharset('UTF-8');
    if ($db !== null) {
        $dbs = explode(',', $db);
        $search->setDb(trim($dbs[0]));
        for ($i = 1; $i < count($dbs); $i++) {
            $search->addDb(trim($dbs[$i]));
        }
    }
    if ($scws_multi !== null) {
        $search->setScwsMulti($scws_multi);
    }
    if ($hot !== null) {
        $type = $hot === 'cur' ? 'currnum' : ($hot === 'last' ? 'lastnum' : 'total');
        $result = $search->getHotQuery($limit1, $type);
 private function applySetting($text)
 {
     self::$_server->reopen();
     foreach ($this->_setting as $key => $cmd) {
         if (is_array($cmd)) {
             foreach ($cmd as $_cmd) {
                 self::$_server->execCommand($_cmd);
             }
         } else {
             self::$_server->execCommand($cmd);
         }
     }
     return XS::convert($text, 'UTF-8', self::$_charset);
 }
Example #18
0
 function xs_output_encoding($buf)
 {
     return XS::convert($buf, $GLOBALS['oe'], 'UTF-8');
 }
Example #19
0
 /**
  * 智能字符集编码转换
  * 将 XS 内部用的 UTF-8 与指定的文档编码按需相互转换
  * 索引文档: ... -> UTF-8, 搜索结果文档: ... <-- UTF-8
  * @param string $value 要转换的字符串	 
  * @return string 转好的字符串
  * @see setCharset
  */
 private function autoConvert($value)
 {
     // Is the value need to convert
     if ($this->_charset === null || $this->_charset == 'UTF-8' || !is_string($value) || !preg_match('/[\\x81-\\xfe]/', $value)) {
         return $value;
     }
     // _meta === null ? index document : search result document
     $from = $this->_meta === null ? $this->_charset : 'UTF-8';
     $to = $this->_meta === null ? 'UTF-8' : $this->_charset;
     return XS::convert($value, $to, $from);
 }
Example #20
0
if ($output === null) {
    $output = '.';
}
if (!is_dir($output)) {
    echo "错误:输出目录 ({$output}) 不是一个有效的目录。\n";
    exit(-1);
}
if (!is_writable($output)) {
    echo "错误:输出目录 ({$output}) 不可写入,请注意检查权限。\n";
    exit(-1);
}
// execute the search
try {
    // create xs object
    echo "初始化项目对象 ...\n";
    $xs = new XS($project);
    // generate varialbes
    echo "解析字段,生成变量清单 ...\n";
    $vars = array();
    // timezone
    if (!ini_get('date.timezone')) {
        date_default_timezone_set('Asia/Chongqing');
    }
    // basic
    $vars['@project@'] = is_file($project) ? realpath($project) : $project;
    $vars['@charset@'] = $xs->getDefaultCharset();
    if ($vars['@charset@'] !== 'GB2312' && $vars['@charset@'] !== 'GBK') {
        $vars['@charset@'] = 'UTF-8';
    }
    $vars['@xs_lib_root@'] = XS_LIB_ROOT;
    $vars['@date_time@'] = date('Y-m-d H:i:s');
Example #21
0
File: XS.php Project: h3len/Project
 private function applySetting($text)
 {
     self::$_server->reopen();
     foreach ($this->_setting as $key => $cmd) {
         self::$_server->execCommand($cmd);
     }
     return XS::convert($text, 'UTF-8', $this->_charset);
 }
Example #22
0
            $class = basename($filter);
            require_once $filter . '.php';
            if (class_exists($class)) {
                $filter = new $class();
            }
        }
    }
    if (!is_object($filter) || !$filter instanceof XSDataFilter) {
        $filter = null;
        echo "注意:自动忽略无效的过滤器 [" . $original . "]\n";
    }
}
// execute the indexer
try {
    // create xs object
    $xs = new XS($ini);
    $index = $xs->index;
    if ($db !== null) {
        $index->setDb($db);
    }
    // scws multi
    if ($scws_multi !== null && $scws_multi !== true) {
        $index->setScwsMulti($scws_multi);
        if (!empty($source)) {
            $scws_multi = null;
        }
    }
    // special actions
    if ($info !== null) {
        echo "---------- SERVER INFO BEGIN ----------\n";
        $res = $index->execCommand(XS_CMD_DEBUG);
Example #23
0
 public function del($term, $field = null)
 {
     $field = $field === null ? $this->xs->getFieldId() : $this->xs->getField($field);
     $cmds = array();
     $terms = is_array($term) ? array_unique($term) : array($term);
     $terms = XS::convert($terms, 'UTF-8', $this->xs->getDefaultCharset());
     foreach ($terms as $term) {
         $cmds[] = new XSCommand(XS_CMD_INDEX_REMOVE, 0, $field->vno, strtolower($term));
     }
     if ($this->_bufSize > 0) {
         $this->appendBuffer(implode('', $cmds));
     } elseif (count($cmds) == 1) {
         $this->execCommand($cmds[0], XS_CMD_OK_RQST_FINISHED);
     } else {
         $cmd = array('cmd' => XS_CMD_INDEX_EXDATA, 'buf' => implode('', $cmds));
         $this->execCommand($cmd, XS_CMD_OK_RQST_FINISHED);
     }
     return $this;
 }