1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
|
<?php
namespace dokuwiki\Ui;
class SearchState
{
/**
* @var array
*/
protected $parsedQuery = [];
/**
* SearchState constructor.
*
* @param array $parsedQuery
*/
public function __construct(array $parsedQuery)
{
global $INPUT;
$this->parsedQuery = $parsedQuery;
if (!isset($parsedQuery['after'])) {
$this->parsedQuery['after'] = $INPUT->str('min');
}
if (!isset($parsedQuery['before'])) {
$this->parsedQuery['before'] = $INPUT->str('max');
}
if (!isset($parsedQuery['sort'])) {
$this->parsedQuery['sort'] = $INPUT->str('srt');
}
}
/**
* Get a search state for the current search limited to a new namespace
*
* @param string $ns the namespace to which to limit the search, falsy to remove the limitation
* @param array $notns
*
* @return SearchState
*/
public function withNamespace($ns, array $notns = [])
{
$parsedQuery = $this->parsedQuery;
$parsedQuery['ns'] = $ns ? [$ns] : [];
$parsedQuery['notns'] = $notns;
return new SearchState($parsedQuery);
}
/**
* Get a search state for the current search with new search fragments and optionally phrases
*
* @param array $and
* @param array $not
* @param array $phrases
*
* @return SearchState
*/
public function withFragments(array $and, array $not, array $phrases = [])
{
$parsedQuery = $this->parsedQuery;
$parsedQuery['and'] = $and;
$parsedQuery['not'] = $not;
$parsedQuery['phrases'] = $phrases;
return new SearchState($parsedQuery);
}
/**
* Get a search state for the current search with with adjusted time limitations
*
* @param $after
* @param $before
*
* @return SearchState
*/
public function withTimeLimitations($after, $before)
{
$parsedQuery = $this->parsedQuery;
$parsedQuery['after'] = $after;
$parsedQuery['before'] = $before;
return new SearchState($parsedQuery);
}
/**
* Get a search state for the current search with adjusted sort preference
*
* @param $sort
*
* @return SearchState
*/
public function withSorting($sort)
{
$parsedQuery = $this->parsedQuery;
$parsedQuery['sort'] = $sort;
return new SearchState($parsedQuery);
}
/**
* Get a link that represents the current search state
*
* Note that this represents only a simplified version of the search state.
* Grouping with braces and "OR" conditions are not supported.
*
* @param $label
*
* @return string
*/
public function getSearchLink($label)
{
global $ID, $conf;
$parsedQuery = $this->parsedQuery;
$tagAttributes = [
'target' => $conf['target']['wiki'],
];
$newQuery = ft_queryUnparser_simple(
$parsedQuery['and'],
$parsedQuery['not'],
$parsedQuery['phrases'],
$parsedQuery['ns'],
$parsedQuery['notns']
);
$hrefAttributes = ['do' => 'search', 'sf' => '1', 'q' => $newQuery];
if ($parsedQuery['after']) {
$hrefAttributes['min'] = $parsedQuery['after'];
}
if ($parsedQuery['before']) {
$hrefAttributes['max'] = $parsedQuery['before'];
}
if ($parsedQuery['sort']) {
$hrefAttributes['srt'] = $parsedQuery['sort'];
}
$href = wl($ID, $hrefAttributes, false, '&');
return "<a href='$href' " . buildAttributes($tagAttributes, true) . ">$label</a>";
}
}
|