xref: /plugin/discussion/syntax/comments.php (revision b8efbcc2bc3516a7bfc8875ffe6c14c5a948c807)
1<?php
2/**
3 * Discussion Plugin
4 *
5 * Enables/disables discussion features based on config settings.
6 *
7 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
8 * @author  Esther Brunner <wikidesign@gmail.com>
9 * @author  Dave Lawson <dlawson@masterytech.com>
10 */
11
12// must be run within Dokuwiki
13if(!defined('DOKU_INC')) die();
14
15if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
16require_once(DOKU_PLUGIN.'syntax.php');
17
18/**
19 * All DokuWiki plugins to extend the parser/rendering mechanism
20 * need to inherit from this class
21 */
22class syntax_plugin_discussion_comments extends DokuWiki_Syntax_Plugin {
23
24    function getType() { return 'substition'; }
25    function getPType() { return 'block'; }
26    function getSort() { return 230; }
27
28    /**
29     * Connect pattern to lexer
30     */
31    function connectTo($mode) {
32        if ($mode == 'base') {
33            $this->Lexer->addSpecialPattern('~~DISCUSSION[^\r\n]*?~~', $mode, 'plugin_discussion_comments');
34        }
35    }
36
37    /**
38     * Handle the match
39     */
40    function handle($match, $state, $pos, &$handler) {
41        global $ID, $ACT;
42
43        // strip markup
44        $match = substr($match, 12, -2);
45
46        // split title (if there is one)
47        list($match, $title) = explode('|', $match, 2);
48
49        // assign discussion state
50        if ($match == ':off') $status = 0;
51        else if ($match == ':closed') $status = 2;
52        else $status = 1;
53
54        if ($ACT == 'preview') return;
55
56        // get discussion meta file name
57        $file = metaFN($ID, '.comments');
58
59        $data = array();
60        if (@file_exists($file)) {
61            $data = unserialize(io_readFile($file, false));
62        }
63        $data['title']  = $title;
64        $data['status'] = $status;
65        io_saveFile($file, serialize($data));
66
67        return $status;
68    }
69
70    function render($mode, &$renderer, $status) {
71        return true; // do nothing -> everything is handled in action component
72    }
73}
74// vim:ts=4:sw=4:et:enc=utf-8:
75