-
Notifications
You must be signed in to change notification settings - Fork 37
/
action.php
143 lines (124 loc) · 4.78 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
<?php
/**
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Esther Brunner <[email protected]>
*/
/**
* Action part of the tag plugin, handles tag display and index updates
*/
class action_plugin_tag extends DokuWiki_Action_Plugin {
/**
* register the eventhandlers
*
* @param Doku_Event_Handler $controller
*/
public function register(Doku_Event_Handler $controller) {
$controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'catchTagAction', array());
$controller->register_hook('TPL_ACT_UNKNOWN', 'BEFORE', $this, 'performTagAction', array());
$controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'beautifyKeywordsInMetaHeader', array());
if($this->getConf('toolbar_icon')) {
$controller->register_hook('TOOLBAR_DEFINE', 'AFTER', $this, 'insertToolbarButton', array ());
}
$controller->register_hook('INDEXER_VERSION_GET', 'BEFORE', $this, 'setTagIndexversion', array());
$controller->register_hook('INDEXER_PAGE_ADD', 'BEFORE', $this, 'addTagsToIndex', array());
}
/**
* Add a version string to the index so it is rebuilt
* whenever the stored data format changes.
*/
public function setTagIndexversion(Doku_Event $event, $param) {
global $conf;
$event->data['plugin_tag'] = '0.2.deaccent='.$conf['deaccent'];
}
/**
* Add all data of the subject metadata to the metadata index.
*/
public function addTagsToIndex(Doku_Event $event, $param) {
/* @var helper_plugin_tag $helper */
if ($helper = $this->loadHelper('tag')) {
// make sure the tags are cleaned and no duplicate tags are added to the index
$tags = p_get_metadata($event->data['page'], 'subject');
if (!is_array($tags)) {
$event->data['metadata']['subject'] = array();
} else {
$event->data['metadata']['subject'] = $helper->cleanTagList($tags);
}
}
}
/**
* catch tag action
*
* @author Michael Klier <[email protected]>
*/
public function catchTagAction(Doku_Event $event, $param) {
if($event->data != 'showtag') return;
$event->preventDefault();
}
/**
* Display the tag page
*
* @param Doku_Event $event The TPL_ACT_UNKNOWN event
* @param array $param optional parameters (unused)
* @return void
*/
public function performTagAction(Doku_Event $event, $param) {
global $lang, $INPUT;
if($event->data != 'showtag') return;
$event->preventDefault();
$tagns = $this->getConf('namespace');
$flags = explode(',', str_replace(" ", "", $this->getConf('pagelist_flags')));
$tag = trim(str_replace($tagns.':', '', $INPUT->str('tag')));
$ns = trim($INPUT->str('ns'));
/* @var helper_plugin_tag $helper */
if ($helper = $this->loadHelper('tag')) {
$pages = $helper->getTopic($ns, '', $tag);
}
if(!empty($pages)) {
// let Pagelist Plugin do the work for us
if (!$pagelist = $this->loadHelper('pagelist')) {
return;
}
/* @var helper_plugin_pagelist $pagelist */
$pagelist->setFlags($flags);
$pagelist->startList();
foreach ($pages as $page) {
$pagelist->addPage($page);
}
print '<h1>TAG: ' . hsc(str_replace('_', ' ', $INPUT->str('tag'))) . '</h1>' . DOKU_LF;
print '<div class="level1">' . DOKU_LF;
print $pagelist->finishList();
print '</div>' . DOKU_LF;
} else {
print '<div class="level1"><p>' . $lang['nothingfound'] . '</p></div>';
}
}
/**
* Inserts the tag toolbar button
*/
public function insertToolbarButton(Doku_Event $event, $param) {
$event->data[] = array(
'type' => 'format',
'title' => $this->getLang('toolbar_icon'),
'icon' => '../../plugins/tag/images/tag-toolbar.png',
'open' => '{{tag>',
'close' => '}}'
);
}
/**
* Prevent displaying underscores instead of blanks inside the page keywords
*/
public function beautifyKeywordsInMetaHeader(Doku_Event $event) {
global $ID;
// Fetch tags for the page; stop proceeding when no tags specified
$tags = p_get_metadata($ID, 'subject', METADATA_DONT_RENDER);
if(is_null($tags)) {
return;
}
// Replace underscores with blanks
foreach($event->data['meta'] as &$meta) {
if(isset($meta['name']) && $meta['name'] == 'keywords') {
$meta['content'] = str_replace('_', ' ', $meta['content']);
}
}
}
}