Skip to content

Commit

Permalink
if nopg, guess for nodes without headpage based on the headpage setting
Browse files Browse the repository at this point in the history
If using default settings, `:start:` is always first, so behaviour is
unchanged. It uses the first enabled option if multiple options are
enabled. Improves #106

Edge case: if all headpage options are disabled and no custom headpage
name is given, it resulted in an empty string value, which was converted
in ns1: which is in general interpreted as ns1:<startpage>. Custom
headpage should be not blank.
  • Loading branch information
Klap-in committed Jan 4, 2024
1 parent db4b3bd commit 16565ad
Showing 1 changed file with 56 additions and 12 deletions.
68 changes: 56 additions & 12 deletions Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -325,9 +325,9 @@ public function searchIndexmenuItems(&$data, $base, $file, $type, $lvl, $opts)
}
//Set title and headpage
$title = static::getNamespaceTitle($id, $headpage, $hns);
//link namespace nodes to start pages when excluding page nodes
if (!$hns && $opts['nopg']) {
$hns = $id . ":" . $conf['start'];
// when excluding page nodes: guess a headpage based on the headpage setting
if ($opts['nopg'] && $hns === false) {
$hns = $this->guessHeadpage($headpage, $id);
}
} else {
//Nopg. Dont show pages
Expand Down Expand Up @@ -511,9 +511,10 @@ public function searchIndexmenuItemsNew(&$data, $base, $file, $type, $lvl, $opts

//Set title and headpage
$title = static::getNamespaceTitle($id, $headpage, $hns);
//link namespace nodes to start pages when excluding page nodes
if (!$hns && $opts['nopg']) {
$hns = $id . ":" . $conf['start'];

// when excluding page nodes: guess a headpage based on the headpage setting
if ($opts['nopg'] && $hns === false) {
$hns = $this->guessHeadpage($headpage, $id);
}
} else {
//Nopg.Dont show pages
Expand All @@ -539,8 +540,8 @@ public function searchIndexmenuItemsNew(&$data, $base, $file, $type, $lvl, $opts
//start page is in root
if ($id == $conf['start']) return false;

$ahp = explode(",", $headpage);
foreach ($ahp as $hp) {
$hpOptions = explode(",", $headpage);
foreach ($hpOptions as $hp) {
switch ($hp) {
case ":inside:":
if (noNS($id) == noNS(getNS($id))) return false;
Expand Down Expand Up @@ -677,7 +678,7 @@ public function customSearch(&$data, $base, $func, $opts, $dir = '', $lvl = 1)
*
* @param string $ns namespace
* @param string $headpage comma-separated headpages options and headpages
* @param string $hns reference pageid of headpage, false when not existing
* @param string|false $hns reference pageid of headpage, false when not existing
* @return string when headpage & heading on: title of headpage, otherwise: namespace name
*
* @author Samuele Tognini <[email protected]>
Expand All @@ -690,8 +691,8 @@ public static function getNamespaceTitle($ns, $headpage, &$hns)
if (empty($headpage)) {
return $title;
}
$ahp = explode(",", $headpage);
foreach ($ahp as $hp) {
$hpOptions = explode(",", $headpage);
foreach ($hpOptions as $hp) {
switch ($hp) {
case ":inside:":
$page = $ns . ":" . noNS($ns);
Expand All @@ -705,7 +706,9 @@ public static function getNamespaceTitle($ns, $headpage, &$hns)
break;
//inside pages
default:
$page = $ns . ":" . $hp;
if(!blank($hp)) { //empty setting results in empty string here
$page = $ns . ":" . $hp;
}
}
//check headpage
if (@file_exists(wikiFN($page)) && auth_quickaclcheck($page) >= AUTH_READ) {
Expand Down Expand Up @@ -789,4 +792,45 @@ private function getSortValue($item)
}
return $sort;
}

/**
* Guess based on first option of the headpage config setting (default :start: if enabled) the headpage of the node
*
* @param string $headpage config setting
* @param string $ns namespace
* @return string guessed headpage
*/
private function guessHeadpage(string $headpage, string $ns): string
{
global $conf;
$hns = false;

$hpOptions = explode(",", $headpage);
foreach ($hpOptions as $hp) {
switch ($hp) {
case ":inside:":
$hns = $ns . ":" . noNS($ns);
break 2;
case ":same:":
$hns = $ns;
break 2;
//it's an inside start
case ":start:":
$hns = ltrim($ns . ":" . $conf['start'], ":");
break 2;
//inside pages
default:
if (!blank($hp)) {
$hns = $ns . ":" . $hp;
break 2;
}
}
}

if ($hns === false) {
//fallback to start if headpage setting was empty
$hns = ltrim($ns . ":" . $conf['start'], ":");
}
return $hns;
}
}

0 comments on commit 16565ad

Please sign in to comment.