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