From dcc823f57a7b25abd9211cc4c0d26ca7efd37045 Mon Sep 17 00:00:00 2001 From: Gerrit Uitslag Date: Thu, 4 Jan 2024 17:12:36 +0100 Subject: [PATCH] Sort namespaces and pages together with nsort option Fixes #66 Fixes #202 Based on #259, which was based on #111 --- Search.php | 34 +++++++++++++++++++--------------- _test/AjaxRequestsTest.php | 16 ++++++++-------- 2 files changed, 27 insertions(+), 23 deletions(-) diff --git a/Search.php b/Search.php index c76191a..923cda2 100644 --- a/Search.php +++ b/Search.php @@ -626,19 +626,27 @@ public function customSearch(&$data, $base, $func, $opts, $dir = '', $lvl = 1) } closedir($dh); + //Collect and sort files + foreach ($files as $file) { + call_user_func_array($func, [&$files_tmp, $base, $file, 'f', $lvl, $opts]); + } + usort($files_tmp, [$this, "compareNodes"]); + //Collect and sort dirs if ($this->nsort) { //collect the wanted directories in dirs_tmp foreach ($dirs as $dir) { call_user_func_array($func, [&$dirs_tmp, $base, $dir, 'd', $lvl, $opts]); } - //sort directories - usort($dirs_tmp, [$this, "compareNodes"]); + //combine directories and pages and sort together + $dirsAndFiles = array_merge($dirs_tmp, $files_tmp); + usort($dirsAndFiles, [$this, "compareNodes"]); + //add and search each directory - foreach ($dirs_tmp as $dir) { - $data[] = $dir; - if ($dir['shouldBeTraversed']) { - $this->customSearch($data, $base, $func, $opts, $dir['file'], $lvl + 1); + foreach ($dirsAndFiles as $dirOrFile) { + $data[] = $dirOrFile; + if ($dirOrFile['type'] != 'f' && $dirOrFile['shouldBeTraversed']) { + $this->customSearch($data, $base, $func, $opts, $dirOrFile['file'], $lvl + 1); } } } else { @@ -652,24 +660,20 @@ public function customSearch(&$data, $base, $func, $opts, $dir = '', $lvl = 1) } } - //Collect and sort files - foreach ($files as $file) { - call_user_func_array($func, [&$files_tmp, $base, $file, 'f', $lvl, $opts]); - } - usort($files_tmp, [$this, "compareNodes"]); - //count added items $added = count($data) - $count; if ($added === 0 && $files_tmp === []) { //remove empty directory again, only if it has not a headpage associated - $v = end($data); - if (!$v['hns']) { + $lastItem = end($data); + if (!$lastItem['hns']) { array_pop($data); } } else { //add files to index - $data = array_merge($data, $files_tmp); + if(!$this->nsort) { + $data = array_merge($data, $files_tmp); + } } } diff --git a/_test/AjaxRequestsTest.php b/_test/AjaxRequestsTest.php index 455a78e..4fe5163 100644 --- a/_test/AjaxRequestsTest.php +++ b/_test/AjaxRequestsTest.php @@ -124,7 +124,7 @@ public function testBasicSorting($call, $post, $expectedResult) public function test_params() { - print_r(AjaxRequestsTest::prepareParams(['level' => 2])); +// print_r(AjaxRequestsTest::prepareParams(['level' => 2])); $this->assertTrue(true); } @@ -319,6 +319,12 @@ public function expectedResultNs1TitleSortNamespaceSort() 'url' => '/./doku.php?id=ns1:ns1:start' ], 1 => [ + 'title' => 'Dd', + 'key' => 'ns1:apage', + 'hns' => false, + 'url' => '/./doku.php?id=ns1:apage' + ], + 2 => [ 'title' => 'ns0', 'key' => 'ns1:ns0:', 'hns' => false, @@ -326,19 +332,13 @@ public function expectedResultNs1TitleSortNamespaceSort() 'lazy' => true, 'url' => false ], - 2 => [ + 3 => [ 'title' => 'ns2', 'key' => 'ns1:ns2:', 'hns' => false, 'folder' => true, 'lazy' => true, 'url' => false - ], - 3 => [ - 'title' => 'Dd', - 'key' => 'ns1:apage', - 'hns' => false, - 'url' => '/./doku.php?id=ns1:apage' ] ]]; }