Skip to content

Commit

Permalink
Sort namespaces and pages together with nsort option
Browse files Browse the repository at this point in the history
Fixes #66
Fixes #202
Based on #259, which was based on #111
  • Loading branch information
Klap-in committed Jan 4, 2024
1 parent d960e8e commit dcc823f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
34 changes: 19 additions & 15 deletions Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
}
}
}

Expand Down
16 changes: 8 additions & 8 deletions _test/AjaxRequestsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -319,26 +319,26 @@ 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,
'folder' => true,
'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'
]
]];
}
Expand Down

0 comments on commit dcc823f

Please sign in to comment.