1<?php
2/**
3 * DokuWiki Plugin dontfeedtemplates (Action Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Cilyan Olowen <gaknar@gmail.com>
7 */
8
9if(!defined('DOKU_INC')) die();
10
11class action_plugin_dontfeedtemplates extends DokuWiki_Action_Plugin {
12
13    /**
14     * Initialise the template inside constructor rather than in register,
15     * to keep things clean.
16     */
17    public function __construct() {
18        /* Load defaults from configuration */
19        /*    template name for current namespace */
20        $this->current_pagename_tpl   =
21            $this->GetConf("current_pagename_tpl");
22        /*    template name for current and child namespaces */
23        $this->inherited_pagename_tpl =
24            $this->GetConf("inherited_pagename_tpl");
25        /*    filtering is active */
26        $this->active = $this->GetConf("always");
27        /* Replace defaults with parameters from templatepagename */
28        if (!plugin_isdisabled('templatepagename')) {
29            $templatepagename =& plugin_load('action', 'templatepagename_TemplatePageName');
30            $this->current_pagename_tpl   =
31                $templatepagename->getConf('current_pagename_tpl');
32            $this->inherited_pagename_tpl =
33                $templatepagename->getConf('inherited_pagename_tpl');
34            /* When templatepagename is active, this plugin is always active */
35            /* no-config pattern. The right way to force the plugin to not   */
36            /* filter is to deactivate it.                                   */
37            $this->active = true;
38        }
39    }
40
41    /**
42     * Registers handle_feed_item_add for FEED_ITEM_ADD:BEFORE
43     *
44     * @param Doku_Event_Handler $controller DokuWiki's event controller object
45     * @return void
46     */
47    public function register(Doku_Event_Handler $controller) {
48        if ($this->active) {
49            $controller->register_hook(
50                'FEED_ITEM_ADD', 'BEFORE',
51                $this, 'handle_feed_item_add'
52            );
53        }
54    }
55
56    /**
57     * Filter out template pages from namespace listings
58     *
59     * @param Doku_Event $event  event object by reference
60     * @param mixed      $param  [the parameters passed as fifth argument to register_hook() when this
61     *                           handler was registered]
62     * @return void
63     */
64
65    public function handle_feed_item_add(Doku_Event &$event, $param) {
66        /* Only in list namespace mode */
67        if ($event->data['opt']['feed_mode'] == "list") {
68            /* Get the page name (strip out namespace part) */
69            $id = $event->data['ditem']['id'];
70            $pagename = $id;
71            $pos = strrpos((string)$id,':');
72            if($pos!==false){
73                $pagename = substr((string)$id,$pos+1);
74            }
75            /* Filter out current_pagename_tpl */
76            if ($pagename == $this->current_pagename_tpl) {
77                $event->preventDefault();
78                $event->stopPropagation();
79                $event->result = false;
80                return;
81            }
82            /* Filter out inherited_pagename_tpl */
83            if ($pagename == $this->inherited_pagename_tpl) {
84                $event->preventDefault();
85                $event->stopPropagation();
86                $event->result = false;
87                return;
88            }
89            /* The item is conform, it can be processed */
90        }
91    }
92
93}
94
95// vim:ts=4:sw=4:et:
96