1<?php
2/**
3 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
4 * @author     Thorsten Staerk <dokuwiki@staerk.de>, Esther Brunner <wikidesign@gmail.com>
5 */
6
7
8
9class action_plugin_mediasyntax extends DokuWiki_Action_Plugin
10{
11    public $supportedModes = array('xhtml', 'i');
12    public $helper = null;
13
14    function __construct()
15    {
16        $this->helper = plugin_load('helper', 'mediasyntax');
17    }
18
19    /**
20     * register the eventhandlers
21     */
22    function register(Doku_Event_Handler $controller)
23    {
24        $controller->register_hook('TOOLBAR_DEFINE',
25                          'AFTER',
26                          $this,
27                          'define_toolbar',
28                           array());
29        $controller->register_hook('PARSER_CACHE_USE','BEFORE', $this, '_cache_prepare');
30        $controller->register_hook('HTML_EDITFORM_OUTPUT', 'BEFORE', $this, 'handle_form');
31        $controller->register_hook('HTML_CONFLICTFORM_OUTPUT', 'BEFORE', $this, 'handle_form');
32        $controller->register_hook('HTML_DRAFTFORM_OUTPUT', 'BEFORE', $this, 'handle_form');
33        $controller->register_hook('ACTION_SHOW_REDIRECT', 'BEFORE', $this, 'handle_redirect');
34        $controller->register_hook('PARSER_HANDLER_DONE', 'BEFORE', $this, 'handle_parser');
35        $controller->register_hook('PARSER_METADATA_RENDER', 'AFTER', $this, 'handle_metadata');
36    }
37
38    /**
39     * Used for debugging purposes only
40     */
41    function handle_metadata(Doku_Event $event, $param)
42    {
43        dbglog("entering function ".__FUNCTION__);
44        //$event->data contains things like creator, last change etc.
45    }
46
47    /**
48     * Supplies the current section level to the include syntax plugin
49     *
50     * @author Michael Klier <chi@chimeric.de>
51     */
52    function handle_parser(Doku_Event $event, $param)
53    {
54        global $ID;
55
56        // check for stored toplevel ID in helper plugin
57        // if it's missing lets see if we have to do anything at all
58        if(!isset($this->helper->toplevel_id))
59        {
60            $ins =& $event->data->calls;
61            $num = count($ins);
62            for($i=0; $i<$num; $i++)
63            {
64                if(($ins[$i][0] == 'plugin'))
65                {
66                    switch($ins[$i][1][0])
67                    {
68                        case 'mediasyntax_include':
69                            if(!isset($this->helper->toplevel_id))
70                            {
71                                $this->helper->toplevel_id = $ID;
72                            }
73                            $this->helper->parse_instructions($ID, $ins);
74                            break;
75                        // some plugins already close open sections
76                        // so we need to make sure we don't close them twice
77                        case 'box':
78                            $this->helper->sec_close = false;
79                            break;
80                    }
81                }
82            }
83        }
84    }
85
86    /**
87     * Add a hidden input to the form to preserve the redirect_id
88     */
89    function handle_form(Doku_Event $event, $param)
90    {
91        if (array_key_exists('redirect_id', $_REQUEST))
92        {
93            $event->data->addHidden('redirect_id', cleanID($_REQUEST['redirect_id']));
94        }
95    }
96
97    /**
98     * Modify the data for the redirect when there is a redirect_id set
99     */
100    function handle_redirect(Doku_Event $event, $param)
101    {
102        if (array_key_exists('redirect_id', $_REQUEST))
103        {
104            $event->data['id'] = cleanID($_REQUEST['redirect_id']);
105            $event->data['title'] = '';
106        }
107    }
108
109    /**
110     * prepare the cache object for default _useCache action
111     */
112    function _cache_prepare(Doku_Event $event, $param)
113    {
114        global $ID;
115        global $INFO;
116        global $conf;
117
118        $cache =& $event->data;
119
120        // we're only interested in instructions of the current page
121        // without the ID check we'd get the cache objects for included pages as well
122        if(!isset($cache->page) || ($cache->page != $ID)) return;
123        if(!isset($cache->mode) || !in_array($cache->mode, $this->supportedModes)) return;
124
125        if(!empty($INFO['userinfo']))
126        {
127            $include_key = $INFO['userinfo']['name'] . '|' . implode('|', $INFO['userinfo']['grps']);
128        }
129        else
130        {
131            $include_key = '@ALL';
132        }
133
134        $depends = p_get_metadata($ID, 'plugin_mediasyntax');
135
136        if($conf['allowdebug'])
137        {
138            dbglog('---- PLUGIN INCLUDE INCLUDE KEY START ----');
139            dbglog($include_key);
140            dbglog('---- PLUGIN INCLUDE INCLUDE KEY END ----');
141            dbglog('---- PLUGIN INCLUDE CACHE DEPENDS START ----');
142            dbglog($depends);
143            dbglog('---- PLUGIN INCLUDE CACHE DEPENDS END ----');
144        }
145
146        $cache->depends['purge'] = true; // kill some performance
147        if(is_array($depends))
148        {
149            $pages = array();
150            if(!isset($depends['keys'][$include_key]))
151            {
152                $cache->depends['purge'] = true; // include key not set - request purge
153            }
154            else
155            {
156                $pages = $depends['pages'];
157            }
158        }
159        else
160        {
161            // nothing to do for us
162            return;
163        }
164
165        // add plugin.info.txt to depends for nicer upgrades
166        $cache->depends['files'][] = dirname(__FILE__) . '/plugin.info.txt';
167
168        $key = '';
169        foreach($pages as $page)
170        {
171            $page = cleanID($this->helper->_apply_macro($page));
172            resolve_pageid(getNS($ID), $page, $exists);
173            $file = wikiFN($page);
174            if(!in_array($cache->depends['files'], array($file)) && @file_exists($file))
175            {
176                $cache->depends['files'][] = $file;
177                $key .= '#' . $page . '|ACL' . auth_quickaclcheck($page);
178            }
179        }
180
181        // empty $key implies no includes, so nothing to do
182        if(empty($key)) return;
183
184        // mark the cache as being modified by the include plugin
185        $cache->include = true;
186
187        // set new cache key & cache name
188        // now also dependent on included page ids and their ACL_READ status
189        $cache->key .= $key;
190        $cache->cache = getCacheName($cache->key, $cache->ext);
191    }
192
193    /**
194     * modifiy the toolbar JS defines
195     *
196     * @author  Esther Brunner  <wikidesign@gmail.com>
197     */
198    function define_toolbar(Doku_Event $event, $param)
199    {
200        dbglog("entering function ".__FUNCTION__);
201        dbglog("event->data follows");
202        dbglog($event->data);
203        dbglog("event->data ends");
204        array_splice($event->data, 5,3);
205        $c = count($event->data);
206        for ($i = 0; $i <= $c; $i++)
207        {
208            if ($event->data[$i]['icon'] == 'ol.png')
209            {
210                $event->data[$i]['open']  = "# ";
211            }
212            elseif ($event->data[$i]['icon'] == 'h.png')
213            {
214                $event->data[$i]['list'][0]['open'] = "= ";
215                $event->data[$i]['list'][0]['close']  = " =\\n";
216                $event->data[$i]['list'][1]['open'] = "== ";
217                $event->data[$i]['list'][1]['close']  = " ==\\n";
218            }
219            elseif ($event->data[$i]['icon'] == 'ul.png')
220            {
221                $event->data[$i]['open']  = "* ";
222            }
223            elseif ($event->data[$i]['icon'] == 'ol.png')
224            {
225                $event->data[$i]['open']  = "# ";
226            }
227            elseif ($event->data[$i]['icon'] == 'italic.png')
228            {
229                $event->data[$i]['open']  = "''";
230                $event->data[$i]['close']  = "''";
231            }
232            elseif ($event->data[$i]['icon'] == 'mono.png')
233            {
234                $event->data[$i]['open']  = "<tt>";
235                $event->data[$i]['close']  = "</tt>";
236                $event->data[$i]['block']  = 1;
237            }
238            elseif ($event->data[$i]['icon'] == 'bold.png')
239            {
240                $event->data[$i]['open']  = "'''";
241                $event->data[$i]['close']  = "'''";
242            }
243       }
244       return true;
245    }
246}
247
248//Setup VIM: ex: et ts=4 enc=utf-8 :
249