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 $contr
16     */
17    function register(Doku_Event_Handler $contr) {
18        $contr->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, '_handle_act', array());
19        $contr->register_hook('TPL_ACT_UNKNOWN', 'BEFORE', $this, '_handle_tpl_act', array());
20        $contr->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, '_handle_keywords', array());
21        if($this->getConf('toolbar_icon')) $contr->register_hook('TOOLBAR_DEFINE', 'AFTER', $this, 'insert_toolbar_button', array ());
22        $contr->register_hook('INDEXER_VERSION_GET', 'BEFORE', $this, '_indexer_version', array());
23        $contr->register_hook('INDEXER_PAGE_ADD', 'BEFORE', $this, '_indexer_index_tags', array());
24    }
25
26    /**
27     * Add a version string to the index so it is rebuilt
28     * whenever the stored data format changes.
29     */
30    function _indexer_version(Doku_Event $event, $param) {
31        global $conf;
32        $event->data['plugin_tag'] = '0.2.deaccent='.$conf['deaccent'];
33    }
34
35    /**
36     * Add all data of the subject metadata to the metadata index.
37     */
38    function _indexer_index_tags(Doku_Event $event, $param) {
39        /* @var helper_plugin_tag $helper */
40        if ($helper = $this->loadHelper('tag')) {
41            // make sure the tags are cleaned and no duplicate tags are added to the index
42            $tags = p_get_metadata($event->data['page'], 'subject');
43            if (!is_array($tags)) {
44                $event->data['metadata']['subject'] = array();
45            } else {
46                $event->data['metadata']['subject'] = $helper->_cleanTagList($tags);
47            }
48        }
49    }
50
51    /**
52     * catch tag action
53     *
54     * @author Michael Klier <chi@chimeric.de>
55     */
56    function _handle_act(Doku_Event $event, $param) {
57        if($event->data != 'showtag') return;
58        $event->preventDefault();
59    }
60
61    /**
62     * Display the tag page
63     *
64     * @param Doku_Event $event The TPL_ACT_UNKNOWN event
65     * @param array      $param optional parameters (unused)
66     */
67    function _handle_tpl_act(Doku_Event $event, $param) {
68        global $lang;
69
70        if($event->data != 'showtag') return;
71        $event->preventDefault();
72
73        $tagns = $this->getConf('namespace');
74        $flags = explode(',', str_replace(" ", "", $this->getConf('pagelist_flags')));
75
76        $tag   = trim(str_replace($this->getConf('namespace').':', '', $_REQUEST['tag']));
77        $ns    = trim($_REQUEST['ns']);
78
79        /* @var helper_plugin_tag $helper */
80        if ($helper = $this->loadHelper('tag')) $pages = $helper->getTopic($ns, '', $tag);
81
82        if(!empty($pages)) {
83
84            // let Pagelist Plugin do the work for us
85            if ((!$pagelist = $this->loadHelper('pagelist'))) {
86                return false;
87            }
88
89            /* @var helper_plugin_pagelist $pagelist */
90            $pagelist->setFlags($flags);
91            $pagelist->startList();
92            foreach ($pages as $page) {
93                $pagelist->addPage($page);
94            }
95
96            print '<h1>TAG: ' . hsc(str_replace('_', ' ', $_REQUEST['tag'])) . '</h1>' . DOKU_LF;
97            print '<div class="level1">' . DOKU_LF;
98            print $pagelist->finishList();
99            print '</div>' . DOKU_LF;
100
101        } else {
102            print '<div class="level1"><p>' . $lang['nothingfound'] . '</p></div>';
103        }
104    }
105
106    /**
107     * Inserts the tag toolbar button
108     */
109    function insert_toolbar_button(Doku_Event $event, $param) {
110        $event->data[] = array(
111            'type' => 'format',
112            'title' => $this->getLang('toolbar_icon'),
113            'icon' => '../../plugins/tag/images/tag-toolbar.png',
114            'open' => '{{tag>',
115            'close' => '}}'
116        );
117    }
118
119    /**
120     * Prevent displaying underscores instead of blanks inside the page keywords
121     */
122    function _handle_keywords(Doku_Event $event) {
123        global $ID;
124
125        // Fetch tags for the page; stop proceeding when no tags specified
126        $tags = p_get_metadata($ID, 'subject', METADATA_DONT_RENDER);
127        if(is_null($tags)) true;
128
129        // Replace underscores with blanks
130        foreach($event->data['meta'] as &$meta) {
131            if($meta['name'] == 'keywords') {
132                $meta['content'] = str_replace('_', ' ', $meta['content']);
133            }
134        }
135    }
136}
137
138// vim:ts=4:sw=4:et:
139