Skip to content
Snippets Groups Projects
Unverified Commit 52d4cd42 authored by Michael Große's avatar Michael Große
Browse files

refactor(search): rewite SearchState to make behavior more obvious

As suggested by @splitbrain SearchState now behaves simliar to the PSR-7
message interfaces.
parent 2ce8affc
No related branches found
No related tags found
No related merge requests found
...@@ -130,11 +130,8 @@ class Search extends Ui ...@@ -130,11 +130,8 @@ class Search extends Ui
$listItem->addClass('active'); $listItem->addClass('active');
$searchForm->addHTML($option['label']); $searchForm->addHTML($option['label']);
} else { } else {
$this->searchState->addSearchLinkSort( $link = $this->searchState->withSorting($option['sort'])->getSearchLink($option['label']);
$searchForm, $searchForm->addHTML($link);
$option['label'],
$option['sort']
);
} }
$searchForm->addTagClose('li'); $searchForm->addTagClose('li');
} }
...@@ -266,12 +263,11 @@ class Search extends Ui ...@@ -266,12 +263,11 @@ class Search extends Ui
$listItem->addClass('active'); $listItem->addClass('active');
$searchForm->addHTML($option['label']); $searchForm->addHTML($option['label']);
} else { } else {
$this->searchState->addSearchLinkFragment( $link = $this->searchState
$searchForm, ->withFragments($option['and'], $option['not'])
$option['label'], ->getSearchLink($option['label'])
$option['and'], ;
$option['not'] $searchForm->addHTML($link);
);
} }
$searchForm->addTagClose('li'); $searchForm->addTagClose('li');
} }
...@@ -315,11 +311,8 @@ class Search extends Ui ...@@ -315,11 +311,8 @@ class Search extends Ui
$listItem = $searchForm->addTagOpen('li'); $listItem = $searchForm->addTagOpen('li');
if ($baseNS) { if ($baseNS) {
$listItem->addClass('active'); $listItem->addClass('active');
$this->searchState->addSeachLinkNS( $link = $this->searchState->withNamespace('')->getSearchLink($lang['search_any_ns']);
$searchForm, $searchForm->addHTML($link);
$lang['search_any_ns'],
''
);
} else { } else {
$searchForm->addHTML($lang['search_any_ns']); $searchForm->addHTML($lang['search_any_ns']);
} }
...@@ -333,11 +326,8 @@ class Search extends Ui ...@@ -333,11 +326,8 @@ class Search extends Ui
$listItem->addClass('active'); $listItem->addClass('active');
$searchForm->addHTML($label); $searchForm->addHTML($label);
} else { } else {
$this->searchState->addSeachLinkNS( $link = $this->searchState->withNamespace($ns)->getSearchLink($label);
$searchForm, $searchForm->addHTML($link);
$label,
$ns
);
} }
$searchForm->addTagClose('li'); $searchForm->addTagClose('li');
} }
...@@ -436,12 +426,11 @@ class Search extends Ui ...@@ -436,12 +426,11 @@ class Search extends Ui
$listItem->addClass('active'); $listItem->addClass('active');
$searchForm->addHTML($option['label']); $searchForm->addHTML($option['label']);
} else { } else {
$this->searchState->addSearchLinkTime( $link = $this->searchState
$searchForm, ->withTimeLimitations($option['after'], $option['before'])
$option['label'], ->getSearchLink($option['label'])
$option['after'], ;
$option['before'] $searchForm->addHTML($link);
);
} }
$searchForm->addTagClose('li'); $searchForm->addTagClose('li');
} }
...@@ -620,9 +609,8 @@ class Search extends Ui ...@@ -620,9 +609,8 @@ class Search extends Ui
if (!empty($this->parsedQuery['ns']) && $this->parsedQuery['ns'][0] === $ns) { if (!empty($this->parsedQuery['ns']) && $this->parsedQuery['ns'][0] === $ns) {
return false; return false;
} }
$name = '@' . $ns; $name = '@' . $ns;
$tmpForm = new Form(); return $this->searchState->withNamespace($ns)->getSearchLink($name);
$this->searchState->addSeachLinkNS($tmpForm, $name, $ns);
return $tmpForm->toHTML();
} }
} }
...@@ -2,8 +2,6 @@ ...@@ -2,8 +2,6 @@
namespace dokuwiki\Ui; namespace dokuwiki\Ui;
use dokuwiki\Form\Form;
class SearchState class SearchState
{ {
/** /**
...@@ -11,85 +9,113 @@ class SearchState ...@@ -11,85 +9,113 @@ class SearchState
*/ */
protected $parsedQuery = []; protected $parsedQuery = [];
/**
* SearchState constructor.
*
* @param array $parsedQuery
*/
public function __construct(array $parsedQuery) public function __construct(array $parsedQuery)
{ {
global $INPUT; global $INPUT;
$this->parsedQuery = $parsedQuery; $this->parsedQuery = $parsedQuery;
$this->parsedQuery['after'] = $INPUT->str('dta'); if (!isset($parsedQuery['after'])) {
$this->parsedQuery['before'] = $INPUT->str('dtb'); $this->parsedQuery['after'] = $INPUT->str('dta');
$this->parsedQuery['sort'] = $INPUT->str('srt'); }
if (!isset($parsedQuery['before'])) {
$this->parsedQuery['before'] = $INPUT->str('dtb');
}
if (!isset($parsedQuery['sort'])) {
$this->parsedQuery['sort'] = $INPUT->str('srt');
}
} }
/** /**
* Add a link to the form which limits the search to the provided namespace * 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
* *
* @param Form $searchForm * @return SearchState
* @param string $label
* @param string $ns namespace to which to limit the search, empty string to remove namespace limitation
*/ */
public function addSeachLinkNS(Form $searchForm, $label, $ns) public function withNamespace($ns, array $notns = [])
{ {
$parsedQuery = $this->parsedQuery; $parsedQuery = $this->parsedQuery;
$parsedQuery['notns'] = [];
$parsedQuery['ns'] = $ns ? [$ns] : []; $parsedQuery['ns'] = $ns ? [$ns] : [];
$this->addSearchLink($searchForm, $label, $parsedQuery); $parsedQuery['notns'] = $notns;
return new SearchState($parsedQuery);
} }
/** /**
* Add a link to the form which searches only for the provided words, but keeps the namespace and time limitations * Get a search state for the current search with new search fragments and optionally phrases
* *
* @param Form $searchForm * @param array $and
* @param string $label * @param array $not
* @param array $and * @param array $phrases
* @param array $not *
* @return SearchState
*/ */
public function addSearchLinkFragment(Form $searchForm, $label, array $and, array $not) public function withFragments(array $and, array $not, array $phrases = [])
{ {
$parsedQuery = $this->parsedQuery; $parsedQuery = $this->parsedQuery;
$parsedQuery['and'] = $and; $parsedQuery['and'] = $and;
$parsedQuery['not'] = $not; $parsedQuery['not'] = $not;
$this->addSearchLink($searchForm, $label, $parsedQuery); $parsedQuery['phrases'] = $phrases;
return new SearchState($parsedQuery);
} }
/** /**
* Add a link to the form which modifies the current search's time limitations * Get a search state for the current search with with adjusted time limitations
* *
* @param Form $searchForm * @param $after
* @param string $label * @param $before
* @param string $after *
* @param null|string $before * @return SearchState
*/ */
public function addSearchLinkTime(Form $searchForm, $label, $after, $before = null) public function withTimeLimitations($after, $before)
{ {
$parsedQuery = $this->parsedQuery; $parsedQuery = $this->parsedQuery;
$parsedQuery['after'] = $after; $parsedQuery['after'] = $after;
$parsedQuery['before'] = $before; $parsedQuery['before'] = $before;
$this->addSearchLink($searchForm, $label, $parsedQuery); return new SearchState($parsedQuery);
} }
/** /**
* Add a link to the form which sets the sort preference for the current search * Get a search state for the current search with adjusted sort preference
*
* @param $sort
* *
* @param Form $searchForm * @return SearchState
* @param string $label
* @param string $sort
*/ */
public function addSearchLinkSort(Form $searchForm, $label, $sort) public function withSorting($sort)
{ {
$parsedQuery = $this->parsedQuery; $parsedQuery = $this->parsedQuery;
$parsedQuery['sort'] = $sort; $parsedQuery['sort'] = $sort;
$this->addSearchLink($searchForm, $label, $parsedQuery); return new SearchState($parsedQuery);
} }
protected function addSearchLink( /**
Form $searchForm, * Get a link that represents the current search state
$label, *
$parsedQuery * Note that this represents only a simplified version of the search state.
) { * Grouping with braces and "OR" conditions are not supported.
global $ID; *
* @param $label
*
* @return string
*/
public function getSearchLink($label)
{
global $ID, $conf;
$parsedQuery = $this->parsedQuery;
$tagAttributes = [
'target' => $conf['target']['wiki'],
];
$newQuery = ft_queryUnparser_simple( $newQuery = ft_queryUnparser_simple(
$parsedQuery['and'], $parsedQuery['and'],
...@@ -108,11 +134,8 @@ class SearchState ...@@ -108,11 +134,8 @@ class SearchState
if ($parsedQuery['sort']) { if ($parsedQuery['sort']) {
$hrefAttributes['srt'] = $parsedQuery['sort']; $hrefAttributes['srt'] = $parsedQuery['sort'];
} }
$searchForm->addTagOpen('a')
->attrs([ $href = wl($ID, $hrefAttributes, false, '&');
'href' => wl($ID, $hrefAttributes, false, '&') return "<a href='$href' " . buildAttributes($tagAttributes) . ">$label</a>";
]);
$searchForm->addHTML($label);
$searchForm->addTagClose('a');
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment