Search.php 3.9 KB

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