Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?
- /**
- * SphinxContent
- * Класс для работы с индексом Sphinx.
- *
- * @package
- * @author TheRabbit
- * @copyright 2013
- * @version $Id$
- * @access public
- */
- class SphinxContent {
- /**
- * SphinxContent::set_tags()
- * Обновляет в таблицах индекса теги.
- *
- * @param integer $intID ID тега
- * @param string $strTitle название тега
- * @param string $strType тип тега (tag|star)
- * @return void
- */
- public static function set_tags($intID, $strTitle, $strType = 'tag', $strCategory = '') {
- $strTable = false;
- if ($strType == 'tag') $strTable = 'tag';
- elseif ($strType == 'star') $strTable = 'star';
- if ($strTable !== false) {
- $objSphinxDB = DB::connection('sphinx');
- $objV = $objSphinxDB->table($strTable)->where('id', '=', $intID)->first();
- if (!isset($objV->id)) $objSphinxDB->table($strTable)->insert(array('id' => $intID, 'title' => $strTitle, 'category' => $strCategory));
- else $objSphinxDB->table($strTable)->where('id', '=', $intID)->update(array('title' => $strTitle, 'category' => $strCategory));
- }//\\ if
- }//\\ set_tags
- /**
- * SphinxContent::set_video()
- * Добавляет в индекс видео.
- *
- * @param integer $intID ID видео
- * @return void
- */
- public static function set_video($intID) {
- $arrVideo = Video::get($intID);
- if ($arrVideo['id']) {
- // Обновим инфу о ролике для sphinx
- $objSphinxDB = DB::connection('sphinx');
- $objV = $objSphinxDB->table('video')->where('id', '=', intval($arrVideo['id']))->first();
- $arrV = array(
- 'title' => '',
- 'description' => '',
- 'tags' => '',
- 'stars' => '',
- 'timeadd' => 0,
- 'duration' => 0,
- 'category' => '',
- );
- if (isset($arrVideo['title'])) $arrV['title'] = $arrVideo['title'];
- if (isset($arrVideo['desc'])) $arrV['description'] = $arrVideo['desc'];
- if (isset($arrVideo['category'])) $arrV['category'] = $arrVideo['category'];
- if (isset($arrVideo['timeadd'])) $arrV['timeadd'] = $arrVideo['timeadd'];
- if (isset($arrVideo['duration'])) $arrV['duration'] = $arrVideo['duration'];
- if (isset($arrVideo['tags']) && $arrVideo['tags'] !== false && count($arrVideo['tags'])) {
- $arrT = array();
- foreach ($arrVideo['tags'] as $arrTag)
- if (strlen(trim($arrTag['title'])))
- $arrT[] = $arrTag['title'];
- if (count($arrT))
- $arrV['tags'] = implode(', ', $arrT);
- }//\\ if
- if (isset($arrVideo['stars']) && $arrVideo['stars'] !== false && count($arrVideo['stars'])) {
- $arrT = array();
- foreach ($arrVideo['stars'] as $arrTag)
- if (strlen(trim($arrTag['title'])))
- $arrT[] = $arrTag['title'];
- if (count($arrT))
- $arrV['stars'] = implode(', ', $arrT);
- }//\\ if
- if (!isset($objV->id)) $objSphinxDB->table('video')->insert(array('id' => $arrVideo['id'], 'title' => $arrV['title'], 'description' => $arrV['description'], 'tags' => $arrV['tags'], 'category' => $arrV['category'], 'stars' => $arrV['stars'], 'timeadd' => $arrV['timeadd'], 'duration' => $arrV['duration']));
- else $objSphinxDB->table('video')->where('id', '=', intval($arrVideo['id']))->update(array('title' => $arrV['title'], 'description' => $arrV['description'], 'tags' => $arrV['tags'], 'category' => $arrV['category'], 'stars' => $arrV['stars'], 'timeadd' => $arrV['timeadd'], 'duration' => $arrV['duration']));
- }//\\ if
- }//\\ set_video
- /**
- * SphinxContent::remove_video()
- * Удаляет из индекса видео.
- *
- * @param integer $intID ID видео
- * @return void
- */
- public static function remove_video($intID) {
- DB::connection('sphinx')->table('video')->where('id', '=', $intID)->delete();
- }//\\ remove_video
- /**
- * SphinxContent::search()
- * Осуществляет поиск.
- *
- * @param string $strSearch поисковая фраза
- * @param string|array $strIndex по каким индексам будем искать all|video|tag|star
- * @return void
- */
- public static function search($strSearch, $strIndex = 'all', $intNumPage = 1, $intNumOnPage = 50) {
- $arrAllIndex = array(
- 'all' => '*',
- 'video' => 'index_video',
- 'tag' => 'index_tag',
- 'star' => 'index_star',
- );
- $arrNumIndex = array(
- 1 => 'video',
- 2 => 'tag',
- 3 => 'star',
- );
- // Определимся
- $strQueryIndex = '*';
- if (is_string($strIndex)) $strQueryIndex = $arrAllIndex[$strIndex];
- elseif (is_array($strIndex) && count($strIndex)) {
- $arrT = array();
- foreach ($strIndex as $strT)
- if (isset($arrAllIndex[$strT]))
- $arrT[] = $arrAllIndex[$strT];
- $strQueryIndex = implode(';', $arrT);
- }//\\ if
- $arrResultID = array('result' => false);
- $strSearch = trim($strSearch);
- if (strlen($strSearch) > 3) {
- $objSphinx = new SphinxClient();
- $objSphinx->SetServer(Config::get('sphinx.host'), Config::get('sphinx.port'));
- // Совпадение по любому слову
- $objSphinx->SetMatchMode(SPH_MATCH_ANY);
- // Возвращает определенную страницу
- $objSphinx->SetLimits(($intNumPage - 1)*$intNumOnPage, $intNumOnPage);
- // Результаты сортировать по релевантности
- $objSphinx->SetSortMode(SPH_SORT_RELEVANCE);
- // Делаем запрос
- $arrResult = $objSphinx->Query($strSearch, $strQueryIndex);
- // обработка результатов запроса
- if ($arrResult === false) {
- Log::error('Sphinx ERROR Query failed: '.$objSphinx->GetLastError()); // выводим ошибку если произошла
- } else {
- if ( $objSphinx->GetLastWarning() ) Log::error('Sphinx WARNING: '.$objSphinx->GetLastWarning());
- $arrResultID['num_total'] = intval($arrResult['total_found']);
- $arrResultID['current_page'] = $intNumPage;
- $arrResultID['count_page'] = ceil(intval($arrResult['total_found']) / $intNumOnPage);
- if (!empty($arrResult['matches']) ) { // если есть результаты поиска - обрабатываем их
- foreach ($arrResult['matches'] as $intID => $arrInfo ) {
- $arrResultID['result'][$arrNumIndex[$intID % 10]][] = (($intID - ($intID % 10)) / 10);
- }//\\ foreach
- }//\\ if
- }//\\ if
- }//\\ if
- return $arrResultID;
- }//\\ search
- }//\\ SphinxContent
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement