forked from chimeric/dokuwiki-plugin-backlinks
-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
syntax.php
157 lines (136 loc) · 4.44 KB
/
syntax.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
use dokuwiki\Extension\SyntaxPlugin;
/**
* DokuWiki Syntax Plugin Backlinks.
*
* Shows a list of pages that link back to a given page.
*
* Syntax: {{backlinks>[pagename][#filterNS|!#filterNS]}}
*
* [pagename] - a valid wiki pagename or a . for the current page
* [filterNS] - a valid,absolute namespace name, optionally prepended with ! to exclude
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Michael Klier <chi@chimeric.de>
* @author Mark C. Prins <mprins@users.sf.net>
*/
/**
* All DokuWiki plugins to extend the parser/rendering mechanism
* need to inherit from this class.
*/
class syntax_plugin_backlinks extends SyntaxPlugin
{
/**
* Syntax Type.
*
* Needs to return one of the mode types defined in $PARSER_MODES in parser.php.
*
* @see DokuWiki_Syntax_Plugin::getType()
*/
public function getType()
{
return 'substition';
}
/**
* @see DokuWiki_Syntax_Plugin::getPType()
*/
public function getPType()
{
return 'block';
}
/**
* @see Doku_Parser_Mode::getSort()
*/
public function getSort()
{
return 304;
}
/**
* Connect pattern to lexer.
*
* @see Doku_Parser_Mode::connectTo()
*/
public function connectTo($mode)
{
$this->Lexer->addSpecialPattern('\{\{backlinks>.+?\}\}', $mode, 'plugin_backlinks');
}
/**
* Handler to prepare matched data for the rendering process.
*
* @see DokuWiki_Syntax_Plugin::handle()
*/
public function handle($match, $state, $pos, Doku_Handler $handler)
{
// strip {{backlinks> from start and }} from end
$match = substr($match, 12, -2);
$includeNS = '';
if (strstr($match, "#")) {
$includeNS = substr(strstr($match, "#", false), 1);
$match = strstr($match, "#", true);
}
return ([$match, $includeNS]);
}
/**
* Handles the actual output creation.
*
* @see DokuWiki_Syntax_Plugin::render()
*/
public function render($format, Doku_Renderer $renderer, $data)
{
global $lang;
global $INFO;
global $ID;
$id = $ID;
// If it's a sidebar, get the original id.
if ($INFO != null) {
$id = $INFO['id'];
}
$match = $data[0];
$match = ($match == '.') ? $id : $match;
if (strstr($match, ".:")) {
resolve_pageid(getNS($id), $match, $exists);
}
if ($format == 'xhtml') {
$renderer->info['cache'] = false;
$backlinks = ft_backlinks($match);
dbglog($backlinks, "backlinks: all backlinks to: $match");
$renderer->doc .= '<div id="plugin__backlinks">' . "\n";
$filterNS = $data[1];
if (!empty($backlinks) && !empty($filterNS)) {
if (stripos($filterNS, "!", 0) === 0) {
$filterNS = substr($filterNS, 1);
dbglog($filterNS, "backlinks: exluding all of namespace: $filterNS");
$backlinks = array_filter(
$backlinks,
static fn($ns) => stripos($ns, $filterNS, 0) !== 0
);
} else {
dbglog($filterNS, "backlinks: including namespace: $filterNS only");
$backlinks = array_filter(
$backlinks,
static fn($ns) => stripos($ns, (string) $filterNS, 0) === 0
);
}
}
dbglog($backlinks, "backlinks: all backlinks to be rendered");
if (!empty($backlinks)) {
$renderer->doc .= '<ul class="idx">';
foreach ($backlinks as $backlink) {
$name = p_get_metadata($backlink, 'title');
if (empty($name)) {
$name = $backlink;
}
$renderer->doc .= '<li><div class="li">';
$renderer->doc .= html_wikilink(':' . $backlink, $name);
$renderer->doc .= '</div></li>' . "\n";
}
$renderer->doc .= '</ul>' . "\n";
} else {
$renderer->doc .= "<strong>Plugin Backlinks: " . $lang['nothingfound'] . "</strong>" . "\n";
}
$renderer->doc .= '</div>' . "\n";
return true;
}
return false;
}
}