1<?php
2/**
3 * DokuWiki Plugin duoshuo (Action Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Matt <caijiamx@gmail.com>
7 */
8
9// must be run within Dokuwiki
10if(!defined('DOKU_INC')) die();
11
12class action_plugin_duoshuo extends DokuWiki_Action_Plugin {
13    static $DUOSHUO = 0;
14    /**
15     * Registers a callback function for a given event
16     *
17     * @param Doku_Event_Handler $controller DokuWiki's event controller object
18     * @return void
19     */
20    public function register(Doku_Event_Handler &$controller) {
21
22       $controller->register_hook('PARSER_WIKITEXT_PREPROCESS', 'BEFORE', $this, 'handle_parser_wikitext_perprocess');
23       $controller->register_hook('TPL_CONTENT_DISPLAY', 'BEFORE', $this, 'handle_tpl_content_display');
24    }
25
26    /**
27     * [Custom event handler which performs action]
28     *
29     * @param Doku_Event $event  event object by reference
30     * @param mixed      $param  [the parameters passed as fifth argument to register_hook() when this
31     *                           handler was registered]
32     * @return void
33     */
34
35    public function handle_parser_wikitext_perprocess(Doku_Event &$event, $param) {
36        $flag = $this->_canShowDuoshuo($event->data);
37        if($flag === 1 || $flag === 3){
38            $event->data = preg_replace('/[^<nowiki>]' . syntax_plugin_duoshuo::DUOSHUO_SYNTAX . '/', '', $event->data);
39        }
40        return true;
41    }
42
43    public function handle_tpl_content_display(Doku_Event &$event, $param) {
44        $flag = $this->_canShowDuoshuo($event->data);
45        if($flag === 1){
46            $event->data .= $this->getDuoshuoScript();
47        }
48        return true;
49    }
50    /**
51     * check canshow duoshuo
52     *
53     * @param string $data  wikitest
54     * @return bool
55     */
56    private function _canShowDuoshuo($data){
57        $flag = 0;
58        $no_duoshuo = preg_match('/[^<nowiki>]' . syntax_plugin_duoshuo::NODUOSHUO_SYNTAX . '/' , $data , $matches);
59        if($no_duoshuo >= 1 ||self::$DUOSHUO == 1){
60            $flag = 3;
61            self::$DUOSHUO = 1;
62        }else{
63            self::$DUOSHUO = 0;
64            $auto = $this->getConf('auto');
65            $no_admin = isset( $_REQUEST['do'] ) ? false : true ;
66            $info = pageinfo();
67            $exists = $info['exists'];
68            if($auto){
69                if($auto && $exists && $no_admin){
70                    $flag = 1;
71                }
72            }else{
73                $count = preg_match('/[^<nowiki>]' . syntax_plugin_duoshuo::DUOSHUO_SYNTAX . '/' , $data , $matches);
74                if($count >= 1 && $exists && $no_admin){
75                    $flag = 2;
76                }
77            }
78        }
79        return $flag;
80    }
81
82    public function getDuoshuoScript(){
83        $syntax = new syntax_plugin_duoshuo();
84        return $syntax->getDuoshuoScript();
85    }
86}
87
88// vim:ts=4:sw=4:et:
89