Пример #1
0
 private function executeSearch($query, $page)
 {
     if (!function_exists('udm_alloc_agent')) {
         throw new UserErrorException("Поиск не работает", 500, "Поиск временно недоступен", "Функции поиска недоступны серверу, требуется вмешательство администратора сайта.");
     }
     $config = $this->ctx->config->get('modules/search');
     $udm = udm_alloc_agent($config['mg_dsn']);
     if ($udm === false or $udm === null) {
         throw new UserErrorException("Поиск не работает", 500, "Поиск временно недоступен", "Не удалось подключиться к серверу MnoGoSearch, требуется вмешательство администратора сайта.");
     }
     $params = array(UDM_FIELD_CHARSET => 'UTF8', UDM_PARAM_CHARSET => 'UTF8', UDM_PARAM_LOCAL_CHARSET => 'UTF8', UDM_PARAM_BROWSER_CHARSET => 'UTF8', UDM_PARAM_SEARCH_MODE => UDM_MODE_ALL, UDM_PARAM_PAGE_SIZE => $this->per_page, UDM_PARAM_PAGE_NUM => $page - 1, UDM_PARAM_QUERY => $query);
     foreach ($params as $key => $value) {
         if (udm_set_agent_param($udm, $key, $value) == false) {
             throw new UserErrorException("Поиск не работает", 500, "Поиск временно недоступен", "Не удалось установить параметр {$key}, требуется вмешательство администратора сайта.  Текст ошибки: " . udm_error($udm));
         }
     }
     $params_ex = array('s' => 'RPD', 'ExcerptSize' => 1024);
     foreach ($params_ex as $key => $value) {
         if (udm_set_agent_param_ex($udm, $key, $value) == false) {
             throw new UserErrorException("Поиск не работает", 500, "Поиск временно недоступен", "Не удалось установить параметр {$key}, требуется вмешательство администратора сайта.  Текст ошибки: " . udm_error($udm));
         }
     }
     $res = udm_add_search_limit($udm, UDM_LIMIT_URL, $_SERVER['HTTP_HOST']);
     if ($res == false) {
         throw new UserErrorException("Поиск не работает", 500, "Поиск временно недоступен", "Не удалось установить привязку к домену, требуется вмешательство администратора сайта.");
     }
     if (!empty($config['mg_ispell']) and is_dir($config['mg_ispell'])) {
         $ispell_langs = array('ru' => array('utf-8', 'russian'), 'en' => array('iso-8859-1', 'english'));
         $i = 0;
         foreach ($ispell_langs as $code => $data) {
             $files_path = $config['mg_ispell'] . '/' . $data[1];
             if (!file_exists($files_path . '.aff') or !file_exists($files_path . '.dict')) {
                 throw new InvalidArgumentException('Не удалось обнаружить файл со словарём или аффиксами для языка "' . $code . '"');
             }
             $sort = intval(++$i == count($ispell_langs));
             // сортировать нужно одновременно с добавлением последнего языка
             if (!udm_load_ispell_data($udm, UDM_ISPELL_TYPE_AFFIX, $code, $data[0], $files_path . '.aff', 0)) {
                 throw new InvalidArgumentException('Ошибка загрузки аффикса "' . $files_path . '.aff": ' . udm_error($udm));
             }
             if (!udm_load_ispell_data($udm, UDM_ISPELL_TYPE_SPELL, $code, $data[0], $files_path . '.dict', $sort)) {
                 throw new InvalidArgumentException('Ошибка загрузки словаря "' . $files_path . '.dict": ' . udm_error($udm));
             }
         }
     }
     $res = udm_find($udm, $query);
     if ($res === false or $res === null) {
         throw new InvalidArgumentException('Ошибка поиска: ' . udm_error($udm));
     }
     return $res;
 }
Пример #2
0
 function search()
 {
     include_once 'HTTP/Header.php';
     $header = new HTTP_Header();
     $search = htmlentities($this->getParam('search'));
     $html = '';
     $this->set('request_uri', NServer::env('PHP_SELF'));
     $this->set('search', $search);
     $html .= $this->render(array('action' => 'search_form', 'return' => true));
     if ($search && function_exists('udm_alloc_agent')) {
         $current_page = $this->getParam('page') ? (int) $this->getParam('page') : 0;
         // instantiate the search agent
         $udm_agent = udm_alloc_agent(DB_DSN_SEARCH);
         $page_size = 10;
         // pull in the stopwords and remove them from the query
         $stopfile = defined('MNOGO_STOPFILE') ? constant('MNOGO_STOPFILE') : '/usr/local/mnogosearch/etc/stopwords/en.huge.sl';
         $tmp = file($stopfile);
         $stopwords = array();
         for ($i = 0; $i < count($tmp); $i++) {
             $tmp[$i] = trim($tmp[$i]);
             if (!$tmp[$i] || preg_match('/^(#|Language:|Charset:)/', $tmp[$i])) {
                 continue;
             }
             $stopwords[] = $tmp[$i];
         }
         unset($tmp);
         $stopped_words = array();
         foreach ($stopwords as $stopword) {
             if (($tmp_search = preg_replace("/\\b{$stopword}\\b/", '', $search)) != $search) {
                 $stopped_words[] = $stopword;
                 $search = str_replace('  ', ' ', $tmp_search);
             }
         }
         // set some values
         // paging values
         udm_set_agent_param($udm_agent, UDM_PARAM_PAGE_SIZE, $page_size);
         udm_set_agent_param($udm_agent, UDM_PARAM_PAGE_NUM, $current_page);
         udm_set_agent_param($udm_agent, UDM_PARAM_QUERY, $search);
         udm_set_agent_param($udm_agent, UDM_PARAM_STOPFILE, $stopfile);
         // perform the search
         $res = udm_find($udm_agent, $search);
         // get search result values
         $total_rows = udm_get_res_param($res, UDM_PARAM_FOUND);
         $total_rows = $total_rows > 0 ? $total_rows - 1 : $total_rows;
         $page_rows = udm_get_res_param($res, UDM_PARAM_NUM_ROWS);
         $first_doc = udm_get_res_param($res, UDM_PARAM_FIRST_DOC);
         $last_doc = udm_get_res_param($res, UDM_PARAM_LAST_DOC);
         $total_pages = ceil($total_rows / $page_size);
         // set general template values
         $this->set('rows', $total_rows);
         $template_pages = array();
         for ($i = 0; $i < $total_pages; $i++) {
             $template_pages[] = $i + 1;
         }
         $search_url = NServer::env('PHP_SELF') . '?search=' . $search . '&amp;page=';
         $this->set('stopped_words', $stopped_words);
         $this->set('search_url', $search_url);
         $this->set('current_page', $current_page);
         $this->set('pages', $template_pages);
         $this->set('previous_page', $current_page > 0 ? $current_page - 1 : -1);
         $this->set('next_page', $current_page + 1 < $total_pages ? $current_page + 1 : -1);
         // gather the results and pass them to the template
         $items = array();
         for ($i = 0; $i < $page_rows; $i++) {
             $item['title'] = udm_get_res_field($res, $i, UDM_FIELD_TITLE);
             $item['url'] = udm_get_res_field($res, $i, UDM_FIELD_URL);
             $item['text'] = udm_get_res_field($res, $i, UDM_FIELD_TEXT);
             $item['size'] = udm_get_res_field($res, $i, UDM_FIELD_SIZE);
             $item['filesize'] = udm_get_res_field($res, $i, UDM_FIELD_SIZE);
             if ($item['filesize']) {
                 $item['filesize'] = NFilesystem::filesize_format($item['filesize']);
             }
             $item['rating'] = udm_get_res_field($res, $i, UDM_FIELD_RATING);
             $item['title'] = $item['title'] ? $this->cleanupItem(htmlspecialchars($item['title'])) : basename($item['url']);
             $item['text'] = $this->cleanupItem($item['text']);
             $items[] = $item;
         }
         $this->set('items', $items);
         $html .= $this->render(array('action' => 'found_items', 'return' => true));
         udm_free_res($res);
         udm_free_agent($udm_agent);
     } else {
         $html .= '<p>You have not entered any search queries - please enter one in the form above.</p>';
     }
     return $html;
 }