-
Notifications
You must be signed in to change notification settings - Fork 6
/
action.php
171 lines (138 loc) · 5.2 KB
/
action.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
/**
* Bloglinks Plugin: displays a link to the previous and the next blog entry above posts in configured namespaces
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Gina Haeussge <[email protected]>
*/
class action_plugin_bloglinks extends DokuWiki_Action_Plugin {
/**
* Register the eventhandlers.
*/
function register(Doku_Event_Handler $controller) {
$controller->register_hook('TPL_ACT_RENDER', 'BEFORE', $this, 'handle_act_render', array ());
$controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'handle_metaheader_output', array ());
}
function handle_metaheader_output(Doku_Event $event, $params) {
global $ACT;
if ($ACT != 'show')
return;
$namespace = $this->_getActiveNamespace();
if (!$namespace)
return;
$relatedEntries = $this->_getRelatedEntries($namespace);
if (isset ($relatedEntries['prev'])) {
$event->data['link'][] = array (
'rel' => 'prev',
'href' => wl($relatedEntries['prev']['id'], '')
);
}
if (isset ($relatedEntries['next'])) {
$event->data['link'][] = array (
'rel' => 'next',
'href' => wl($relatedEntries['next']['id'], '')
);
}
return true;
}
function handle_act_render(Doku_Event $event, $params) {
global $ACT;
if ($ACT != 'show')
return;
$namespace = $this->_getActiveNamespace();
if ($namespace)
$this->_printLinks($this->_getRelatedEntries($namespace));
return true;
}
function _getActiveNamespace() {
global $ID;
global $INFO;
$pattern = $this->getConf('excluded_pages');
if (strlen($pattern) > 0 && preg_match($pattern, $ID)) {
return false;
}
if (!$INFO['exists'])
return false;
$namespaces = explode(',', $this->getConf('enabled_namespaces'));
foreach ($namespaces as $namespace) {
if (trim($namespace) && (strpos($ID, $namespace . ':') === 0)) {
return $namespace;
}
}
return false;
}
function _getRelatedEntries($namespace) {
global $ID;
// get the blog entries for the namespace
if ($my = & plugin_load('helper', 'blog'))
$entries = $my->getBlog($namespace);
if (!$entries)
return;
// normalize keys
$entries = array_values($entries);
// prepare search for current page
$meta = p_get_metadata($ID);
if ($my->sort == 'mdate') {
$date = $meta['date']['modified'];
if (!$date) $date = filemtime(wikiFN($ID));
} else {
$date = $meta['date']['created'];
if (!$date) $date = filectime(wikiFN($ID));
}
$perm = auth_quickaclcheck($ID);
// Diagnostic
$curPage = array (
'id' => $ID,
'title' => $meta['title'],
'date' => $date,
'user' => $meta['creator'],
'desc' => $meta['description']['abstract'],
'exists' => true,
'perm' => $perm,
'draft' => (isset($meta['type']) && $meta['type'] == 'draft'),
);
// get index of current page
$curIndex = array_search($curPage, $entries);
// Initialize prev, cur and next with null
$cur = null;
$prev = null;
$next = null;
// get previous and next entries
if ($curIndex > 0 && $curIndex < count($entries) - 1) { // got a prev and a next
list ($next, $cur, $prev) = array_slice($entries, $curIndex -1, 3);
} else if ($curIndex == 0) { // only got a prev
list ($cur, $prev) = array_slice($entries, $curIndex, 2);
} else { // only got a next
list ($next, $cur) = array_slice($entries, $curIndex -1, 2);
}
return array('prev' => $prev, 'cur' => $cur, 'next' => $next);
}
/**
* Prints the links to the related entries
*/
function _printLinks($relatedEntries) {
// display links
echo '<div id="plugin_bloglinks__links">';
foreach(array('prev', 'next') as $type) {
if (isset ($relatedEntries[$type])) {
echo '<div class="plugin_bloglinks__'.$type.'">';
echo '<a href="' . wl($relatedEntries[$type]['id'], '') . '" class="wikilink1" rel="'.$type.'">' . $this->_linkTemplate($relatedEntries[$type], $type) . '</a>';
echo '</div>';
}
}
echo DOKU_LF . '</div>';
}
function _linkTemplate($entry, $type) {
global $conf;
$replace = array(
'@@TITLE@@' => $entry['title'],
'@@AUTHOR@@' => $entry['user'],
'@@DATE@@' => date($conf['dformat'], $entry['date']),
'@@NAME@@' => $this->getLang($type . '_link'),
);
$linktext = $this->getConf($type.'_template');
$linktext = str_replace(array_keys($replace), array_values($replace), $linktext);
return $linktext;
}
}
//Setup VIM: ex: et ts=4 enc=utf-8 :