1<?php
2
3if (!defined('DOKU_INC')) die();
4if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
5
6require_once(DOKU_PLUGIN.'syntax.php');
7
8/**
9 * Plugin autotweet: Automatically posts tweets to your Twitter app when pages are changed.
10 *
11 *
12 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
13 * @version    1.0
14 * @date       October 2011
15 * @author     J. Drost-Tenfelde <info@drost-tenfelde.de>
16 *
17 */
18class syntax_plugin_autotweet_autotweet extends DokuWiki_Syntax_Plugin {
19    /**
20     * Return the syntax plugin type.
21     *
22     * @return Plugin type.
23     */
24    public function getType() {
25        return 'substition';
26    }
27
28    /**
29     * Return the paragraph type.
30     *
31     * @return Paragrahp type.
32     */
33	public function getPType() { return 'block'; }
34
35    /**
36     * Return the sort type.
37     *
38     * @return Sort type.
39     */
40	public function getSort() { return 305; }
41
42   /**
43    * Connects the plugin to the lexer.
44    *
45    * @param mode Rendering mode.
46    */
47	public function connectTo($mode) {
48		$this->Lexer->addSpecialPattern('~~AUTOTWEET:.*?~~', $mode, 'plugin_autotweet_autotweet');
49	}
50
51    /**
52     * Handles matched patterns and assembles data for the plugin.
53     *
54     * @param match Matched pattern.
55     * @param state State.
56     * @param handler handler.
57     *
58     * @return array with plugin data.
59     */
60	public function handle($match, $state, $pos, &$handler){
61        $data = array();
62
63        # Get the tags from the syntax
64        $match = trim( substr($match, 12, -2) );
65        // Get the parameters (key=value), seperated by &
66		$pairs = explode('&', $match);
67		// Turn the pairs into key=>value
68        foreach ($pairs as $pair) {
69			list($key, $value) = explode('=', $pair, 2);
70			$data[trim($key)] = trim($value);
71        }
72		// Turn all keys to lower case
73        $data = array_change_key_case($data, CASE_LOWER);
74
75        // Get all essential (non-set) parameters from the configuration
76        if ( !isset( $data['consumer_key'] ) ) {
77            $data['consumer_key'] = $this->getConf('consumer_key');
78        }
79        if ( !isset( $data['consumer_secret'] ) ) {
80            $data['consumer_secret'] = $this->getConf('consumer_secret');
81        }
82        if ( !isset( $data['access_token'] ) ) {
83            $data['access_token'] = $this->getConf('access_token');
84        }
85        if ( !isset( $data['access_token_secret'] ) ) {
86            $data['access_token_secret'] = $this->getConf('access_token_secret');
87        }
88
89        return $data;
90	}
91
92    /**
93     * Renders the syntax and passes on configuration to the action plugin.
94     *
95     * @param mode rendering mode.
96     * @param renderer Renderer.
97     * @param data Data returned by the handle function.
98     *
99     */
100	public function render($mode, &$renderer, $data) {
101	   global $ID;
102		if ( $data === false) {
103            return false;
104        }
105
106        // Pass on the configuration to the action plugin
107        if ( $mode == 'metadata') {
108            $renderer->meta['autotweet']['consumer_key'] = $data['consumer_key'];
109            $renderer->meta['autotweet']['consumer_secret'] = $data['consumer_secret'];
110            $renderer->meta['autotweet']['access_token'] = $data['access_token'];
111            $renderer->meta['autotweet']['access_token_secret'] = $data['access_token_secret'];
112            return true;
113        }
114        else if ( $mode == 'xhtml')
115        {
116            // Do not render anything special
117            return true;
118        }
119        return false;
120	}
121}
122