diff options
author | ahriman <ahriman@falte.red> | 2018-12-03 19:22:25 -0500 |
---|---|---|
committer | ahriman <ahriman@falte.red> | 2018-12-03 19:22:25 -0500 |
commit | 0ae8cbf5c0b1a198b963490985b7738392ebcb97 (patch) | |
tree | b2c77ae72c6b717e2b97492065196ac5ffb2d9e2 /wiki/inc/Action/Search.php | |
parent | f57f6cc5a2d159f90168d292437dc4bd8cd7f934 (diff) | |
download | site-0ae8cbf5c0b1a198b963490985b7738392ebcb97.tar.gz |
installed dokuwiki, added to navbar, updated news
Diffstat (limited to 'wiki/inc/Action/Search.php')
-rw-r--r-- | wiki/inc/Action/Search.php | 135 |
1 files changed, 135 insertions, 0 deletions
diff --git a/wiki/inc/Action/Search.php b/wiki/inc/Action/Search.php new file mode 100644 index 0000000..382fc47 --- /dev/null +++ b/wiki/inc/Action/Search.php @@ -0,0 +1,135 @@ +<?php + +namespace dokuwiki\Action; + +use dokuwiki\Action\Exception\ActionAbort; + +/** + * Class Search + * + * Search for pages and content + * + * @package dokuwiki\Action + */ +class Search extends AbstractAction { + + protected $pageLookupResults = array(); + protected $fullTextResults = array(); + protected $highlight = array(); + + /** @inheritdoc */ + public function minimumPermission() { + return AUTH_NONE; + } + + /** + * we only search if a search word was given + * + * @inheritdoc + */ + public function checkPreconditions() { + parent::checkPreconditions(); + } + + public function preProcess() + { + global $QUERY, $ID, $conf, $INPUT; + $s = cleanID($QUERY); + + if ($ID !== $conf['start'] && !$INPUT->has('q')) { + parse_str($INPUT->server->str('QUERY_STRING'), $urlParts); + $urlParts['q'] = $urlParts['id']; + $urlParts['id'] = $conf['start']; + $url = DOKU_URL . DOKU_SCRIPT . '?' . http_build_query($urlParts, null, '&'); + send_redirect($url); + } + + if ($s === '') throw new ActionAbort(); + $this->adjustGlobalQuery(); + } + + /** @inheritdoc */ + public function tplContent() + { + $this->execute(); + + $search = new \dokuwiki\Ui\Search($this->pageLookupResults, $this->fullTextResults, $this->highlight); + $search->show(); + } + + + /** + * run the search + */ + protected function execute() + { + global $INPUT, $QUERY; + $after = $INPUT->str('min'); + $before = $INPUT->str('max'); + $this->pageLookupResults = ft_pageLookup($QUERY, true, useHeading('navigation'), $after, $before); + $this->fullTextResults = ft_pageSearch($QUERY, $highlight, $INPUT->str('srt'), $after, $before); + $this->highlight = $highlight; + } + + /** + * Adjust the global query accordingly to the config search_nslimit and search_fragment + * + * This will only do something if the search didn't originate from the form on the searchpage itself + */ + protected function adjustGlobalQuery() + { + global $conf, $INPUT, $QUERY, $ID; + + if ($INPUT->bool('sf')) { + return; + } + + $Indexer = idx_get_indexer(); + $parsedQuery = ft_queryParser($Indexer, $QUERY); + + if (empty($parsedQuery['ns']) && empty($parsedQuery['notns'])) { + if ($conf['search_nslimit'] > 0) { + if (getNS($ID) !== false) { + $nsParts = explode(':', getNS($ID)); + $ns = implode(':', array_slice($nsParts, 0, $conf['search_nslimit'])); + $QUERY .= " @$ns"; + } + } + } + + if ($conf['search_fragment'] !== 'exact') { + if (empty(array_diff($parsedQuery['words'], $parsedQuery['and']))) { + if (strpos($QUERY, '*') === false) { + $queryParts = explode(' ', $QUERY); + $queryParts = array_map(function ($part) { + if (strpos($part, '@') === 0) { + return $part; + } + if (strpos($part, 'ns:') === 0) { + return $part; + } + if (strpos($part, '^') === 0) { + return $part; + } + if (strpos($part, '-ns:') === 0) { + return $part; + } + + global $conf; + + if ($conf['search_fragment'] === 'starts_with') { + return $part . '*'; + } + if ($conf['search_fragment'] === 'ends_with') { + return '*' . $part; + } + + return '*' . $part . '*'; + + }, $queryParts); + $QUERY = implode(' ', $queryParts); + } + } + } + } +} |