1<?php
2/**
3 * Ajax chat plugin
4 *
5 * Enables/disables Ajax driven chat
6 *
7 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
8 * @author  Pavel Vitis <pavel [dot] vitis [at] seznam [dot] cz>
9 */
10
11if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
12if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
13require_once(DOKU_PLUGIN.'syntax.php');
14require_once('chat.php');
15
16global $ajax_chat_included;
17
18/**
19 * All DokuWiki plugins to extend the parser/rendering mechanism
20 * need to inherit from this class
21 */
22class syntax_plugin_chat extends DokuWiki_Syntax_Plugin {
23
24    /**
25     * return some info
26     */
27    function getInfo(){
28        return array(
29            'author' => 'Pavel Vitis',
30            'email'  => 'pavel [dot] vitis [at] seznam [dot] cz',
31            'date'   => '2006-02-30',
32            'name'   => 'Chat Plugin',
33            'desc'   => 'Enables/disables Ajax-driven chat.',
34            'url'    => 'http://wiki.splitbrain.org/plugin:chat',
35        );
36    }
37
38    /**
39     * What kind of syntax are we?
40     */
41    function getType(){
42        return 'substition';
43    }
44
45    /**
46     * What about paragraphs?
47     */
48    function getPType(){
49        return 'block';
50    }
51
52    /**
53     * Where to sort in?
54     */
55    function getSort(){
56        return 230;
57    }
58
59
60    /**
61     * Connect pattern to lexer
62     */
63    function connectTo($mode) {
64        if ($mode == 'base') {
65            $this->Lexer->addSpecialPattern('~~CHAT~~', $mode, 'plugin_chat');
66            $this->Lexer->addSpecialPattern('~~NOCHAT~~', $mode, 'plugin_chat');
67        }
68    }
69
70
71    /**
72     * Handle the match
73     */
74    function handle($match, $state, $pos, &$handler) {
75        global $conf;
76        global $lang;
77        global $ID;
78
79        $dID = chat_plugin::addChatNS($ID);
80        $dFN = wikiFN($dID);
81
82        if ($match == '~~CHAT~~' && !chat_plugin::isChatID($ID)) {
83            if (!file_exists($dFN)) {
84                // Current discussion page doesn't exist.
85                $revs = getRevisions($dID);
86                if (count($revs)) {
87                    // Restore previously delete discussion page.
88                    $rev = array_shift($revs);
89                    $txt = rawWiki($dID, $rev);
90                    $sum = $lang['chat_restored'] . $rev;
91                }
92                else {
93                    // Create new discussion page.
94                    $txt = ' ';
95                    $sum = $lang['chat_created'];
96                }
97                saveWikiText($dID, $txt, $sum);
98            }
99	    else {
100	    }
101        }
102        else if ($match == '~~NOCHAT~~') {
103            if (file_exists($dFN)) {
104                saveWikiText($dID, '', $lang['chat_deleted']);
105            }
106        }
107
108	return array($dID);
109    }
110
111    /**
112     * Create output
113     */
114    function render($mode, &$renderer, $data) {
115	global $ajax_chat_included;
116	if ($mode == 'xhtml') {
117	    if (isset($data[0]) && !$ajax_chat_included) {
118		$renderer->info['cache'] = FALSE;//TODO: solve this. It remembers wrong username
119//		$renderer->doc .= "D:".$data[0];
120		$renderer->doc .= chat_plugin::chatHtml($data[0]);
121		$ajax_chat_included = true;
122		return true;
123	    }
124	}
125        return true;
126    }
127
128}
129