*/ if(!defined('DOKU_INC')) die(); if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); require_once(DOKU_INC.'inc/parser/parser.php'); require_once(DOKU_PLUGIN . 'action.php'); require_once(DOKU_PLUGIN . 'sphinxsearch/sphinxapi.php'); require_once(DOKU_PLUGIN . 'sphinxsearch/PageMapper.php'); require_once(DOKU_PLUGIN . 'sphinxsearch/SphinxSearch.php'); require_once(DOKU_PLUGIN . 'sphinxsearch/functions.php'); class action_plugin_sphinxsearch extends DokuWiki_Action_Plugin { var $_search = null; /** * return some info */ function getInfo() { return confToHash(dirname(__FILE__).'/plugin.info.txt'); } /** * Register to the content display event to place the results under it. */ /** * register the eventhandlers */ function register(&$controller){ $controller->register_hook('TPL_CONTENT_DISPLAY', 'BEFORE', $this, 'handle_act_unknown', array()); } /** * If our own 'googlesearch' action was given we produce our content here */ function handle_act_unknown(&$event, $param){ global $ACT; global $QUERY; if($ACT != 'search') return; // nothing to do for us // we can handle it -> prevent others $event->stopPropagation(); $event->preventDefault(); $this->_search($QUERY,$_REQUEST['start'],$_REQUEST['prev']); } /** * do the search and displays the result */ function _search($query, $start, $prev) { global $conf; $start = (int) $start; if($start < 0){ $start = 0; } if(empty($prev)){ $prev = 0; } $categories = $this->_getCategories($query); $keywords = $this->_getKeywords($query); $search = new SphinxSearch($this->getConf('host'), $this->getConf('port'), $this->getConf('index')); $search->setSnippetSize($this->getConf('snippetsize')); $search->setArroundWordsCount($this->getConf('aroundwords')); $search->setTitlePriority($this->getConf('title_priority')); $search->setBodyPriority($this->getConf('body_priority')); $search->setNamespacePriority($this->getConf('namespace_priority')); $search->setPagenamePriority($this->getConf('pagename_priority')); if (!empty($keywords) && empty($categories)){ $search->setSearchAllQuery($keywords, $categories); } elseif (!empty($keywords)) { $search->setSearchAllQueryWithCategoryFilter($keywords, $categories); } else { echo 'Your search - ' . $query . ' - did not match any documents.'; return; } $result = $search->search($start, $this->getConf('maxresults')); $this->_search = $search; if (!$result || $search->getError()){ echo 'Your search - ' . $query . ' - did not match any documents.'; return; } $pagesList = $search->getPages($keywords); $totalFound = $search->getTotalFound(); if(empty($pagesList) || 0 == $totalFound){ echo 'Your search - ' . $query . ' - did not match any documents.'; return; } else { echo ' '; echo '

Found '.$totalFound . ($totalFound == 1 ? ' match ' : ' matches ') . ' for query "' . hsc($query).'"

'; echo ''; } } function _showResult($row, $keywords, $subpages = false) { $page = $row['page']; $bodyExcerpt = $row['bodyExcerpt']; $titleTextExcerpt = $row['titleTextExcerpt']; $hid = $row['hid']; $metaData = p_get_metadata($page); if (!empty($titleTextExcerpt)){ $titleText = $titleTextExcerpt; } elseif(!empty($row['title_text'])){ $titleText = $row['title_text']; } elseif(!empty($metaData['title'])){ $titleText = hsc($metaData['title']); } else { $titleText = hsc($page); } $namespaces = getNsLinks($page, $keywords, $this->_search); $href = !empty($hid) ? (wl($page).'#'.$hid) : wl($page); if($subpages){ echo '
'; } else { echo '
'; } echo ''.$titleText.'
'; echo '
'; echo strip_tags($bodyExcerpt, ','); echo '
'; $sep=':'; $i = 0; echo ''; foreach ($namespaces as $name){ $link = $name['link']; $pageTitle = $name['title']; tpl_link($link, $pageTitle); if ($i++ < count($namespaces)-1){ echo $sep; } } if (!empty($hid)){ echo '#'.$hid; } echo ''; if (!empty($metaData['last_change']['date'])){ echo ' - Last modified '.date("Y-m-d H:i",$metaData['last_change']['date']).' '; } else if (!empty($metaData['date']['created'])){ echo ' - Last modified '.date("Y-m-d H:i",$metaData['date']['created']).' '; } if(!empty($metaData['last_change']['user'])){ echo 'by '.$metaData['last_change']['user'].' '; } else if(!empty($metaData['creator'])){ echo 'by '.$metaData['creator'].' '; } if (!empty($row['subpages'])){ echo '
'; echo '
More matches in this document
'; }else { echo '
'; } echo '
'; echo '
'; } function searchform(){ global $lang; global $ACT; global $QUERY; // don't print the search form if search action has been disabled if (!actionOk('search')) return false; print ''; return true; } function _getCategories($query) { $categories = ''; $query = urldecode($query); if (false !== ($pos = strpos($query, "@cat"))){; $categories = substr($query, $pos + strlen("@cat")); } return trim($categories); } function _getKeywords($query) { $keywords = $query; $query = urldecode($query); if (false !== ($pos = strpos($query, "-@cat"))){; $keywords = substr($keywords, 0, $pos); }else if (false !== ($pos = strpos($query, "@cat"))){; $keywords = substr($keywords, 0, $pos); } return trim($keywords); } function _getMatchingPagenames($keywords, $categories) { $this->_search->setSearchCategoryQuery($keywords, $categories); $this->_search->setNamespacePriority($this->getConf('mp_namespace_priority')); $this->_search->setPagenamePriority($this->getConf('mp_pagename_priority')); $res = $this->_search->search(0, 10); if (!$res){ return false; } $pageIds = $this->_search->getPagesIds(); $matchPages = array(); foreach($pageIds as $page){ $matchPages[$page['page']] = $page['hid']; } return array_unique($matchPages); } } ?>