| 1 | <?php |
|---|
| 2 | // wcf imports |
|---|
| 3 | require_once(WCF_DIR.'lib/page/SearchResultPage.class.php'); |
|---|
| 4 | require_once(WCF_DIR.'lib/data/solr/SolrService.php'); |
|---|
| 5 | |
|---|
| 6 | /** |
|---|
| 7 | * SearchForm handles given search request and shows the extended search form. |
|---|
| 8 | * |
|---|
| 9 | */ |
|---|
| 10 | class SolrSearchPage extends SearchResultPage { |
|---|
| 11 | public $templateName = 'solr'; |
|---|
| 12 | protected $total = 0; |
|---|
| 13 | |
|---|
| 14 | /** |
|---|
| 15 | * Creates a new SearchResultPage object. without searchid |
|---|
| 16 | */ |
|---|
| 17 | public function __construct() { |
|---|
| 18 | parent::__construct(0); |
|---|
| 19 | } |
|---|
| 20 | |
|---|
| 21 | /** |
|---|
| 22 | * removes all duplicate whitespaces |
|---|
| 23 | * |
|---|
| 24 | * @param string $string |
|---|
| 25 | * @return string |
|---|
| 26 | */ |
|---|
| 27 | protected function convertSingleWhitespace($string) { |
|---|
| 28 | $string = str_replace(array("\t", " ", "\r", "\n", "-", urldecode("%C2%A0")), " ", $string); |
|---|
| 29 | while(strpos($string, ' ') !== false) { |
|---|
| 30 | $string = str_replace(' ', ' ', $string); |
|---|
| 31 | } |
|---|
| 32 | return $string; |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 35 | /** |
|---|
| 36 | * if the client is (or claims to be) connected via HTTPS |
|---|
| 37 | * @return boolean |
|---|
| 38 | */ |
|---|
| 39 | protected function isHTTPS() { |
|---|
| 40 | return isset($_SERVER["HTTP_X_PROTO"]) || |
|---|
| 41 | (isset($_SERVER['HTTPS']) && |
|---|
| 42 | !empty($_SERVER['HTTPS']) && |
|---|
| 43 | $_SERVER['HTTPS'] !== 'off'); |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | /** |
|---|
| 47 | * Gets the data of the selected search from database. |
|---|
| 48 | */ |
|---|
| 49 | protected function readSearch() { |
|---|
| 50 | |
|---|
| 51 | // seo friendly redirect of page 1 |
|---|
| 52 | if(isset($_GET['pageNo']) && $_GET['pageNo'] == 1) { |
|---|
| 53 | $url = ($this->isHTTPS() ? 'https' : 'http').'://'.$_SERVER['HTTP_HOST'].str_replace("&pageNo=1", "", $_SERVER['REQUEST_URI']); |
|---|
| 54 | HeaderUtil::redirect($url, false, true); |
|---|
| 55 | exit; |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | // get search query |
|---|
| 59 | if (isset($_REQUEST['q'])) $this->query = $_REQUEST['q']; |
|---|
| 60 | |
|---|
| 61 | if(!$this->query) { |
|---|
| 62 | return; |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | // param |
|---|
| 66 | $offset = (max($this->pageNo,1) - 1) * $this->itemsPerPage; |
|---|
| 67 | $i = $offset; |
|---|
| 68 | |
|---|
| 69 | // query search |
|---|
| 70 | $solr = new SolrService(SOLR_URL); |
|---|
| 71 | $tmp = $solr->search($this->query, $offset, $this->itemsPerPage); |
|---|
| 72 | $this->total = intval($tmp->response->numFound); |
|---|
| 73 | |
|---|
| 74 | if(!$tmp || !$tmp->highlighting) { |
|---|
| 75 | return; |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | // transform data in wcf compatible format |
|---|
| 79 | foreach($tmp->highlighting as $url => $row) { |
|---|
| 80 | // press first dimension of stdobject into clean array |
|---|
| 81 | $data = array(); |
|---|
| 82 | $data['messageID'] = $i; |
|---|
| 83 | $data['message'] = $this->convertSingleWhitespace($row->content[0]); |
|---|
| 84 | $data['subject'] = $row->title[0]; |
|---|
| 85 | $data['url'] = $url; |
|---|
| 86 | $data['displayurl'] = $row->url[0]; |
|---|
| 87 | $data['image'] = 'http://image.browsershots.de/'.parse_url($data['url'], PHP_URL_HOST); |
|---|
| 88 | $data['time'] = time(); // TODO: time |
|---|
| 89 | $data['messageType'] = 'solr'; |
|---|
| 90 | $data['type'] = 'solr'; |
|---|
| 91 | |
|---|
| 92 | // set solr defaults |
|---|
| 93 | if(!empty($row->messageType)) { |
|---|
| 94 | $data['messageType'] = $row->messageType; |
|---|
| 95 | } |
|---|
| 96 | if(!empty($row->type)) { |
|---|
| 97 | $data['type'] = $row->type; |
|---|
| 98 | } |
|---|
| 99 | if(!empty($row->messageID)) { |
|---|
| 100 | $data['messageID'] = $row->messageID; |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | // increment message key position |
|---|
| 104 | $this->result[$i++] = $data; |
|---|
| 105 | } |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | /** |
|---|
| 109 | * Caches the message data. |
|---|
| 110 | */ |
|---|
| 111 | protected function cacheMessageData() { |
|---|
| 112 | parent::cacheMessageData(); |
|---|
| 113 | |
|---|
| 114 | $messageCache = array(); |
|---|
| 115 | foreach($this->result as $key => $row) { |
|---|
| 116 | if($row['messageType'] == 'solr') { |
|---|
| 117 | $messageCache[$key] = $row; |
|---|
| 118 | } |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | if(count($messageCache)) { |
|---|
| 122 | $object = SearchEngine::getSearchTypeObject('solr'); |
|---|
| 123 | $object->cacheMessageData(null, $messageCache); |
|---|
| 124 | } |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | /** |
|---|
| 128 | * @see MultipleLinkPage::countItems() |
|---|
| 129 | */ |
|---|
| 130 | public function countItems() { |
|---|
| 131 | parent::countItems(); |
|---|
| 132 | |
|---|
| 133 | return $this->total; |
|---|
| 134 | } |
|---|
| 135 | } |
|---|
| 136 | ?> |
|---|