summaryrefslogtreecommitdiff
path: root/platform/www/lib/plugins/tag/action.php
blob: d008628d8d7ae5737444eeee8c600acc01170a1c (plain)
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
<?php
/**
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Esther Brunner <wikidesign@gmail.com>
 */

/**
 * 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 $contr
     */
    function register(Doku_Event_Handler $contr) {
        $contr->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, '_handle_act', array());
        $contr->register_hook('TPL_ACT_UNKNOWN', 'BEFORE', $this, '_handle_tpl_act', array());
        $contr->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, '_handle_keywords', array());
        if($this->getConf('toolbar_icon')) $contr->register_hook('TOOLBAR_DEFINE', 'AFTER', $this, 'insert_toolbar_button', array ());
        $contr->register_hook('INDEXER_VERSION_GET', 'BEFORE', $this, '_indexer_version', array());
        $contr->register_hook('INDEXER_PAGE_ADD', 'BEFORE', $this, '_indexer_index_tags', array());
    }

    /**
     * Add a version string to the index so it is rebuilt
     * whenever the stored data format changes.
     */
    function _indexer_version(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.
     */
    function _indexer_index_tags(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 <chi@chimeric.de>
     */
    function _handle_act(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)
     */
    function _handle_tpl_act(Doku_Event $event, $param) {
        global $lang;

        if($event->data != 'showtag') return;
        $event->preventDefault();

        $tagns = $this->getConf('namespace');
        $flags = explode(',', str_replace(" ", "", $this->getConf('pagelist_flags')));

        $tag   = trim(str_replace($this->getConf('namespace').':', '', $_REQUEST['tag']));
        $ns    = trim($_REQUEST['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 false;
            }

            /* @var helper_plugin_pagelist $pagelist */
            $pagelist->setFlags($flags);
            $pagelist->startList();
            foreach ($pages as $page) {
                $pagelist->addPage($page);
            }

            print '<h1>TAG: ' . hsc(str_replace('_', ' ', $_REQUEST['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
     */
    function insert_toolbar_button(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
     */
    function _handle_keywords(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)) true;

        // Replace underscores with blanks
        foreach($event->data['meta'] as &$meta) {
            if($meta['name'] == 'keywords') {
                $meta['content'] = str_replace('_', ' ', $meta['content']);
            }
        }
    }
}

// vim:ts=4:sw=4:et: