1<?php 2 3namespace dokuwiki\Action; 4 5use dokuwiki\Action\Exception\ActionAbort; 6use dokuwiki\Search\FulltextSearch; 7use dokuwiki\Search\MetadataSearch; 8use dokuwiki\Search\QueryParser; 9 10/** 11 * Class Search 12 * 13 * Search for pages and content 14 * 15 * @package dokuwiki\Action 16 */ 17class Search extends AbstractAction { 18 19 protected $pageLookupResults = array(); 20 protected $fullTextResults = array(); 21 protected $highlight = array(); 22 23 /** @inheritdoc */ 24 public function minimumPermission() { 25 return AUTH_NONE; 26 } 27 28 /** 29 * we only search if a search word was given 30 * 31 * @inheritdoc 32 */ 33 public function checkPreconditions() { 34 parent::checkPreconditions(); 35 } 36 37 public function preProcess() 38 { 39 global $QUERY, $ID, $conf, $INPUT; 40 $s = cleanID($QUERY); 41 42 if ($ID !== $conf['start'] && !$INPUT->has('q')) { 43 parse_str($INPUT->server->str('QUERY_STRING'), $urlParts); 44 $urlParts['q'] = $urlParts['id']; 45 unset($urlParts['id']); 46 $url = wl($ID, $urlParts, true, '&'); 47 send_redirect($url); 48 } 49 50 if ($s === '') throw new ActionAbort(); 51 $this->adjustGlobalQuery(); 52 } 53 54 /** @inheritdoc */ 55 public function tplContent() 56 { 57 $this->execute(); 58 59 $search = new \dokuwiki\Ui\Search($this->pageLookupResults, $this->fullTextResults, $this->highlight); 60 $search->show(); 61 } 62 63 64 /** 65 * run the search 66 */ 67 protected function execute() 68 { 69 global $INPUT, $QUERY; 70 $after = $INPUT->str('min'); 71 $before = $INPUT->str('max'); 72 $this->pageLookupResults = (new MetadataSearch)->pageLookup( 73 $QUERY, true, useHeading('navigation'), $after, $before 74 ); 75 $this->fullTextResults = (new FulltextSearch)->pageSearch( 76 $QUERY, $highlight, $INPUT->str('srt'), $after, $before 77 ); 78 $this->highlight = $highlight; 79 } 80 81 /** 82 * Adjust the global query accordingly to the config search_nslimit and search_fragment 83 * 84 * This will only do something if the search didn't originate from the form on the searchpage itself 85 */ 86 protected function adjustGlobalQuery() 87 { 88 global $conf, $INPUT, $QUERY, $ID; 89 90 if ($INPUT->bool('sf')) { 91 return; 92 } 93 94 $parsedQuery = (new QueryParser)->convert($QUERY); 95 96 if (empty($parsedQuery['ns']) && empty($parsedQuery['notns'])) { 97 if ($conf['search_nslimit'] > 0) { 98 if (getNS($ID) !== false) { 99 $nsParts = explode(':', getNS($ID)); 100 $ns = implode(':', array_slice($nsParts, 0, $conf['search_nslimit'])); 101 $QUERY .= " @$ns"; 102 } 103 } 104 } 105 106 if ($conf['search_fragment'] !== 'exact') { 107 if (empty(array_diff($parsedQuery['words'], $parsedQuery['and']))) { 108 if (strpos($QUERY, '*') === false) { 109 $queryParts = explode(' ', $QUERY); 110 $queryParts = array_map(function ($part) { 111 if (strpos($part, '@') === 0) { 112 return $part; 113 } 114 if (strpos($part, 'ns:') === 0) { 115 return $part; 116 } 117 if (strpos($part, '^') === 0) { 118 return $part; 119 } 120 if (strpos($part, '-ns:') === 0) { 121 return $part; 122 } 123 124 global $conf; 125 126 if ($conf['search_fragment'] === 'starts_with') { 127 return $part . '*'; 128 } 129 if ($conf['search_fragment'] === 'ends_with') { 130 return '*' . $part; 131 } 132 133 return '*' . $part . '*'; 134 135 }, $queryParts); 136 $QUERY = implode(' ', $queryParts); 137 } 138 } 139 } 140 } 141} 142