Skip to content

Commit 8b7e143

Browse files
committed
$NSORID$ filter place holder. implements #712
This filter will return the current page unless when called on a namespace start page where it will return the namespace instead.
1 parent 074c9a5 commit 8b7e143

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

_test/SearchConfigTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,40 @@ public function test_filtervars_simple()
3333
$this->assertEquals(date('Y-m-d'), $searchConfig->applyFilterVars('$DATE(now)$'));
3434
}
3535

36+
public function test_filtervars_nsorid()
37+
{
38+
global $INFO;
39+
40+
41+
$searchConfig = new SearchConfig([]);
42+
43+
// normal page
44+
$INFO['id'] = 'foo:bar:baz';
45+
$this->assertEquals('foo:bar:baz', $searchConfig->applyFilterVars('$NSORID$'));
46+
47+
// start page: start in namespace
48+
$INFO['id'] = 'foo:bar:start';
49+
saveWikiText($INFO['id'], 'start page', 'start created');
50+
$this->assertEquals('foo:bar', $searchConfig->applyFilterVars('$NSORID$'));
51+
saveWikiText($INFO['id'], '', 'start page deleted');
52+
clearstatcache();
53+
54+
// start page: same as namespace in namespace
55+
$INFO['id'] = 'foo:bar:bar';
56+
saveWikiText($INFO['id'], 'start page', 'start created');
57+
$this->assertEquals('foo:bar', $searchConfig->applyFilterVars('$NSORID$'));
58+
saveWikiText($INFO['id'], '', 'start page deleted');
59+
clearstatcache();
60+
61+
// start page: same as namespace in above namespace
62+
// incidally this is the same as a normal page
63+
$INFO['id'] = 'foo:bar';
64+
saveWikiText($INFO['id'], 'start page', 'start created');
65+
$this->assertEquals('foo:bar', $searchConfig->applyFilterVars('$NSORID$'));
66+
saveWikiText($INFO['id'], '', 'start page deleted');
67+
clearstatcache();
68+
}
69+
3670
public function test_filtervars_struct()
3771
{
3872
global $INFO;

meta/SearchConfig.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace dokuwiki\plugin\struct\meta;
44

5+
use dokuwiki\File\PageResolver;
6+
57
/**
68
* Class SearchConfig
79
*
@@ -116,6 +118,7 @@ protected function applyFilterVars($filter)
116118
if (!isset($INFO['id'])) {
117119
$INFO['id'] = '';
118120
}
121+
$ns = getNS($INFO['id']);
119122

120123
// apply inexpensive filters first
121124
$filter = str_replace(
@@ -128,14 +131,30 @@ protected function applyFilterVars($filter)
128131
],
129132
[
130133
$INFO['id'],
131-
getNS($INFO['id']),
134+
$ns,
132135
noNS($INFO['id']),
133136
$INPUT->server->str('REMOTE_USER'),
134137
date('Y-m-d')
135138
],
136139
$filter
137140
);
138141

142+
// apply namespace or id placeholder #712
143+
// returns the namespace for start pages, otherwise the ID
144+
if (preg_match('/\$NSORID\$/', $filter)) {
145+
$resolver = new PageResolver('');
146+
147+
$start = $resolver->resolveId($ns . ':');
148+
if($start === $INFO['id']) {
149+
// This is a start page, we return the namespace
150+
$val = $ns;
151+
} else {
152+
// This is a normal page, we return the ID
153+
$val = $INFO['id'];
154+
}
155+
$filter = str_replace('$NSORID$', $val, $filter);
156+
}
157+
139158
// apply struct column placeholder (we support only one!)
140159
// or apply date formula, given as strtotime
141160
if (preg_match('/^(.*?)(?:\$STRUCT\.(.*?)\$)(.*?)$/', $filter, $match)) {

0 commit comments

Comments
 (0)