search_api.class.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. defined('IN_PHPCMS') or exit('No permission resources.');
  3. /**
  4. * 全站搜索内容入库接口
  5. */
  6. class search_api extends admin {
  7. private $siteid,$categorys,$db;
  8. public function __construct() {
  9. $this->siteid = $this->get_siteid();
  10. $this->categorys = getcache('category_content_'.$this->siteid,'commons');
  11. $this->db = pc_base::load_model('content_model');
  12. }
  13. public function set_model($modelid) {
  14. $this->modelid = $modelid;
  15. $this->db->set_model($modelid);
  16. }
  17. /**
  18. * 全文索引API
  19. * @param $pagesize 每页条数
  20. * @param $page 当前页
  21. */
  22. public function fulltext_api($pagesize = 100, $page = 1) {
  23. $system_keys = $model_keys = array();
  24. $fulltext_array = getcache('model_field_'.$this->modelid,'model');
  25. foreach($fulltext_array AS $key=>$value) {
  26. if($value['issystem'] && $value['isfulltext']) {
  27. $system_keys[] = $key;
  28. }
  29. }
  30. if(empty($system_keys)) return '';
  31. $system_keys = 'id,inputtime,'.implode(',',$system_keys);
  32. $offset = $pagesize*($page-1);
  33. $result = $this->db->select('',$system_keys,"$offset, $pagesize");
  34. //模型从表字段
  35. foreach($fulltext_array AS $key=>$value) {
  36. if(!$value['issystem'] && $value['isfulltext']) {
  37. $model_keys[] = $key;
  38. }
  39. }
  40. if(empty($model_keys)) return '';
  41. $model_keys = 'id,'.implode(',',$model_keys);
  42. $this->db->table_name = $this->db->table_name.'_data';
  43. $result_data = $this->db->select('',$model_keys,"$offset, $pagesize",'','','id');
  44. //处理结果
  45. foreach($result as $r) {
  46. $fulltextcontent = '';
  47. foreach($r as $field=>$_r) {
  48. if($field=='id') continue;
  49. $fulltextcontent .= strip_tags($_r).' ';
  50. }
  51. if(!empty($result_data[$r['id']])) {
  52. foreach($result_data[$r['id']] as $_r) {
  53. if($field=='id') continue;
  54. $fulltextcontent .= strip_tags($_r).' ';
  55. }
  56. }
  57. $temp['fulltextcontent'] = str_replace("'",'',$fulltextcontent);
  58. $temp['title'] = addslashes($r['title']);
  59. $temp['adddate'] = $r['inputtime'];
  60. $data[$r['id']] = $temp;
  61. }
  62. return $data;
  63. }
  64. /**
  65. * 计算总数
  66. * @param $modelid
  67. */
  68. public function total($modelid) {
  69. $this->modelid = $modelid;
  70. $this->db->set_model($modelid);
  71. return $this->db->count();
  72. }
  73. }