1<?php
2/**
3 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
4 * @author     Esther Brunner <wikidesign@gmail.com>
5 */
6
7/**
8 * Action part of the tag plugin, handles tag display and index updates
9 */
10class action_plugin_tag extends DokuWiki_Action_Plugin {
11
12    /**
13     * register the eventhandlers
14     *
15     * @param Doku_Event_Handler $controller
16     */
17    public function register(Doku_Event_Handler $controller) {
18        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'catchTagAction', array());
19        $controller->register_hook('TPL_ACT_UNKNOWN', 'BEFORE', $this, 'performTagAction', array());
20        $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'beautifyKeywordsInMetaHeader', array());
21        if($this->getConf('toolbar_icon')) {
22            $controller->register_hook('TOOLBAR_DEFINE', 'AFTER', $this, 'insertToolbarButton', array ());
23        }
24        $controller->register_hook('INDEXER_VERSION_GET', 'BEFORE', $this, 'setTagIndexversion', array());
25        $controller->register_hook('INDEXER_PAGE_ADD', 'BEFORE', $this, 'addTagsToIndex', array());
26    }
27
28    /**
29     * Add a version string to the index so it is rebuilt
30     * whenever the stored data format changes.
31     */
32    public function setTagIndexversion(Doku_Event $event, $param) {
33        global $conf;
34        $event->data['plugin_tag'] = '0.2.deaccent='.$conf['deaccent'];
35    }
36
37    /**
38     * Add all data of the subject metadata to the metadata index.
39     */
40    public function addTagsToIndex(Doku_Event $event, $param) {
41        /* @var helper_plugin_tag $helper */
42        if ($helper = $this->loadHelper('tag')) {
43            // make sure the tags are cleaned and no duplicate tags are added to the index
44            $tags = p_get_metadata($event->data['page'], 'subject');
45            if (!is_array($tags)) {
46                $event->data['metadata']['subject'] = array();
47            } else {
48                $event->data['metadata']['subject'] = $helper->cleanTagList($tags);
49            }
50        }
51    }
52
53    /**
54     * catch tag action
55     *
56     * @author Michael Klier <chi@chimeric.de>
57     */
58    public function catchTagAction(Doku_Event $event, $param) {
59        if($event->data != 'showtag') return;
60        $event->preventDefault();
61    }
62
63    /**
64     * Display the tag page
65     *
66     * @param Doku_Event $event The TPL_ACT_UNKNOWN event
67     * @param array $param optional parameters (unused)
68     * @return void
69     */
70    public function performTagAction(Doku_Event $event, $param) {
71        global $lang, $INPUT;
72
73        if($event->data != 'showtag') return;
74        $event->preventDefault();
75
76        $tagns = $this->getConf('namespace');
77        $flags = explode(',', str_replace(" ", "", $this->getConf('pagelist_flags')));
78
79        $tag   = trim(str_replace($tagns.':', '', $INPUT->str('tag')));
80        $ns    = trim($INPUT->str('ns'));
81
82        /* @var helper_plugin_tag $helper */
83        if ($helper = $this->loadHelper('tag')) {
84            $pages = $helper->getTopic($ns, '', $tag);
85        }
86
87        if(!empty($pages)) {
88
89            // let Pagelist Plugin do the work for us
90            if (!$pagelist = $this->loadHelper('pagelist')) {
91                return;
92            }
93
94            /* @var helper_plugin_pagelist $pagelist */
95            $pagelist->setFlags($flags);
96            $pagelist->startList();
97            foreach ($pages as $page) {
98                $pagelist->addPage($page);
99            }
100
101            print '<h1>TAG: ' . hsc(str_replace('_', ' ', $INPUT->str('tag'))) . '</h1>' . DOKU_LF;
102            print '<div class="level1">' . DOKU_LF;
103            print $pagelist->finishList();
104            print '</div>' . DOKU_LF;
105
106        } else {
107            print '<div class="level1"><p>' . $lang['nothingfound'] . '</p></div>';
108        }
109    }
110
111    /**
112     * Inserts the tag toolbar button
113     */
114    public function insertToolbarButton(Doku_Event $event, $param) {
115        $event->data[] = array(
116            'type' => 'format',
117            'title' => $this->getLang('toolbar_icon'),
118            'icon' => '../../plugins/tag/images/tag-toolbar.png',
119            'open' => '{{tag>',
120            'close' => '}}'
121        );
122    }
123
124    /**
125     * Prevent displaying underscores instead of blanks inside the page keywords
126     */
127    public function beautifyKeywordsInMetaHeader(Doku_Event $event) {
128        global $ID;
129
130        // Fetch tags for the page; stop proceeding when no tags specified
131        $tags = p_get_metadata($ID, 'subject', METADATA_DONT_RENDER);
132        if(is_null($tags)) {
133            return;
134        }
135
136        // Replace underscores with blanks
137        foreach($event->data['meta'] as &$meta) {
138            if(isset($meta['name']) && $meta['name'] == 'keywords') {
139                $meta['content'] = str_replace('_', ' ', $meta['content']);
140            }
141        }
142    }
143}
144