ARTICLES
phpcms v9不限模型全站搜索
2015-08-18 16:30:520字号:
简单修改一下v9默认的搜索功能,可以不按模型搜索全站内容
下面是被修改后的search模块中的index.php文件
  1. defined('IN_PHPCMS') or exit('No permission resources.');
  2. pc_base::load_sys_class('form','',0);
  3. pc_base::load_sys_class('format','',0);
  4. class index {
  5.     function __construct() {
  6.         $this->db = pc_base::load_model('search_model');
  7.         $this->content_db = pc_base::load_model('content_model');
  8.     }
  9.     
  10.     /** 
  11.      * 关键词搜索 
  12.      */
  13.     public function init() {
  14.         //获取siteid
  15.         $siteid = isset($_REQUEST['siteid']) && trim($_REQUEST['siteid']) ? intval($_REQUEST['siteid']) : 1;
  16.         $SEO = seo($siteid);
  17.  
  18.         //搜索配置
  19.         $search_setting = getcache('search');
  20.         $setting = $search_setting[$siteid];
  21.  
  22.         $search_model = getcache('search_model_'.$siteid);
  23.         $type_module = getcache('type_module_'.$siteid);
  24.  
  25.         if(isset($_GET['q'])) {
  26.             if(trim($_GET['q'])=='') {
  27.                 header('Location: '.APP_PATH.'index.php?m=search');exit;
  28.             }
  29.             $typeid = emptyempty($_GET['typeid']) ? 0 : intval($_GET['typeid']);
  30.             $time = emptyempty($_GET['time']) || !in_array($_GET['time'],array('all','day','month','year','week')) ? 'all' : trim($_GET['time']);
  31.             $page = isset($_GET['page']) ? intval($_GET['page']) : 1;
  32.             $pagesize = 10;
  33.             $q = safe_replace(trim($_GET['q']));
  34.             $q = new_html_special_chars(strip_tags($q));
  35.             $q = str_replace('%', '', $q);    //过滤'%',用户全文搜索
  36.             $search_q = $q;    //搜索原内容
  37.  
  38.             $sql_time = $sql_tid = '';
  39.             if($typeid) $sql_tid = ' AND typeid = '.$typeid;
  40.             //按时间搜索
  41.             if($time == 'day') {
  42.                 $search_time = SYS_TIME - 86400;
  43.                 $sql_time = ' AND adddate > '.$search_time;
  44.             } elseif($time == 'week') {
  45.                 $search_time = SYS_TIME - 604800;
  46.                 $sql_time = ' AND adddate > '.$search_time;
  47.             } elseif($time == 'month') {
  48.                 $search_time = SYS_TIME - 2592000;
  49.                 $sql_time = ' AND adddate > '.$search_time;
  50.             } elseif($time == 'year') {
  51.                 $search_time = SYS_TIME - 31536000;
  52.                 $sql_time = ' AND adddate > '.$search_time;
  53.             } else {
  54.                 $search_time = 0;
  55.                 $sql_time = '';
  56.             }
  57.             if($page==&& !$setting['sphinxenable']) {
  58.                 //精确搜索
  59.                 $commend = $this->db->get_one("`siteid`= '$siteid' $sql_tid $sql_time AND `data` like '%$q%'");
  60.             } else {
  61.                 $commend = '';
  62.             }
  63.             //如果开启sphinx
  64.             if($setting['sphinxenable']) {
  65.                 $sphinx = pc_base::load_app_class('search_interface', '', 0);
  66.                 $sphinx = new search_interface();
  67.                 
  68.                 $offset = $pagesize*($page-1);
  69.                 $res = $sphinx->search($q, array($siteid), array($typeid), array($search_time, SYS_TIME), $offset, $pagesize, '@weight desc');
  70.                 $totalnums = $res['total'];
  71.                 //如果结果不为空
  72.                 if(!emptyempty($res['matches'])) {
  73.                     $result = $res['matches'];
  74.                 }
  75.             } else {
  76.                 
  77.                 $sql = "`siteid`= '$siteid' $sql_tid $sql_time AND `data` like '%$q%'";
  78.                 
  79.  
  80.                 $result = $this->db->listinfo($sql, 'searchid DESC', $page, 10);
  81.             }
  82.           
  83.             //如果结果不为空
  84.             if(!emptyempty($result) || !emptyempty($commend['id'])) {
  85.                 foreach($result as $_v) {
  86.                     if($_v['typeid']) $sids[$_v['typeid']][] = $_v['id'];
  87.                 }
  88.  
  89.                 if(!emptyempty($commend['id'])) {
  90.                     if($commend['typeid']) $sids[$commend['typeid']][] = $commend['id'];
  91.                 }
  92.                 $model_type_cache = getcache('type_model_'.$siteid,'search');
  93.                 $model_type_cache = array_flip($model_type_cache);
  94.                 $data = array();
  95.                 foreach($sids as $_k=>$_val) {
  96.                     $tid = $_k;
  97.                     $ids = array_unique($_val);
  98.  
  99.                     $where = to_sqls($ids, '', 'id');
  100.                     //获取模型id
  101.                     $modelid = $model_type_cache[$tid];
  102.  
  103.                     //是否读取其他模块接口
  104.                     if($modelid) {
  105.                         $this->content_db->set_model($modelid);
  106.                     
  107.                         /** 
  108.                         * 如果表名为空,则为黄页模型 
  109.                         */
  110.                         if(emptyempty($this->content_db->model_tablename)) {
  111.                             $this->content_db = pc_base::load_model('yp_content_model');
  112.                             $this->content_db->set_model($modelid);
  113.  
  114.                         }
  115.                         $datas = $this->content_db->select($where, '*');
  116.                     }
  117.                     $data = array_merge($data,$datas);
  118.                 }
  119.                 $pages = $this->db->pages;
  120.                 $totalnums = $this->db->number;
  121.            
  122.                 //如果分词结果为空
  123.                 if(!emptyempty($segment_q)) {
  124.                     $replace = explode(' ', $segment_q);
  125.                     foreach($replace as $replace_arr_v) {
  126.                         $replace_arr[] =  ''.$replace_arr_v.'';
  127.                     }
  128.                     foreach($data as $_k=>$_v) {
  129.                         $data[$_k]['title'] = str_replace($replace, $replace_arr, $_v['title']);
  130.                         $data[$_k]['description'] = str_replace($replace, $replace_arr, $_v['description']);
  131.                     }
  132.                 } else {
  133.                     foreach($data as $_k=>$_v) {
  134.                         $data[$_k]['title'] = str_replace($q, ''.$q.'', $_v['title']);
  135.                         $data[$_k]['description'] = str_replace($q, ''.$q.'', $_v['description']);
  136.                     }
  137.                 }
  138.             }
  139.             $execute_time = execute_time();
  140.             $pages = isset($pages) ? $pages : '';
  141.             $totalnums = isset($totalnums) ? $totalnums : 0;
  142.             $data = isset($data) ? $data : '';
  143.             
  144.             include    template('search','list');
  145.         } else {
  146.             include    template('search','index');
  147.         }
  148.     }
  149.  
  150.     
  151.     public function public_get_suggest_keyword() {
  152.         $url = $_GET['url'].'&q='.$_GET['q'];
  153.         $trust_url = array('c8430fcf851e85818b546addf5bc4dd3');
  154.         $urm_md5 = md5($url);
  155.         if (!in_array($urm_md5, $trust_url)) exit;
  156.         
  157.         $res = @file_get_contents($url);
  158.         if(CHARSET != 'gbk') {
  159.             $res = iconv('gbk', CHARSET, $res);
  160.         }
  161.         echo $res;
  162.     }
  163.     
  164.     /** 
  165.      * 提示搜索接口 
  166.      * TODO 暂时未启用,用的是google的接口 
  167.      */
  168.     public function public_suggest_search() {
  169.         //关键词转换为拼音
  170.         pc_base::load_sys_func('iconv');
  171.         $pinyin = gbk_to_pinyin($q);
  172.         if(is_array($pinyin)) {
  173.             $pinyin = implode('', $pinyin);
  174.         }
  175.         $this->keyword_db = pc_base::load_model('search_keyword_model');
  176.         $suggest = $this->keyword_db->select("pinyin like '$pinyin%'", '*', 10, 'searchnums DESC');
  177.         
  178.         foreach($suggest as $v) {
  179.             echo $v['keyword']."\n";
  180.         }
  181.  
  182.         
  183.     }
  184. }
  185. ?>
然后在header.html模板上面增加一个“不限”的搜索条件,typeid对应的值为0,search中的index.html和lists.html也做相同处理,效果如本站,这样只要不选择模型那么搜索出来的结果就是所有模型中符合条件的数据