1<?php
2
3if (!defined('DOKU_INC')) die();
4if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
5require_once DOKU_PLUGIN.'action.php';
6
7/**
8 * Plugin autotweet: Automatically posts tweets to your Twitter app when pages are changed.
9 *
10 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
11 * @version    1.0
12 * @date       October 2011
13 * @author     J. Drost-Tenfelde <info@drost-tenfelde.de>
14 *
15 */
16class action_plugin_autotweet extends DokuWiki_Action_Plugin {
17    /**
18     * Return plugin information.
19     *
20     * @return Plugin information.
21     */
22    function getInfo() {
23      return array(
24        'author' => 'J. Drost-Tenfelde',
25        'email'  => 'info@drost-tenfelde.de',
26        'date'   => '2011-09-28',
27        'name'   => 'autotweet',
28        'desc'   => 'Automatically posts tweets to your Twitter app when pages are changed',
29        'url'    => 'http://www.drost-tenfelde.de/?id=dokuwiki:plugins:autotweet',
30      );
31    }
32
33    /**
34     * Register the hooks.
35     *
36     * @param controller Doku_Event_Handler.
37     */
38    public function register(Doku_Event_Handler &$controller) {
39        // Catch after the page has been written to
40        $controller->register_hook('IO_WIKIPAGE_WRITE', 'AFTER', $this, 'handle_wikipage_write' );
41    }
42
43    /**
44     * Handles the event IO_WIKIPAGE_WRITE.
45     *
46     * @param event Doku_Event IO_WIKIPAGE_WRITE.
47     *
48     * @param param Parameters.
49     */
50    public function handle_wikipage_write( Doku_Event &$event, $param ) {
51        global $ID;
52        $data = $event->data;
53
54        // Make sure the event data is set properly
55        if ( is_array( $data ) )
56        {
57            $page_contents = $data[0][1];
58
59            // Check that the tag ~~AUTOTWEET: is inside the page contents
60            if ( strpos($page_contents,'~~AUTOTWEET:') === FALSE ) {
61                return;
62            }
63
64            // Get the configuration that was retrieved by the syntax plugin
65            $autotweet = p_get_metadata( $ID, 'autotweet', true );
66
67            // Get the plugin parameters
68            $consumer_key = $autotweet['consumer_key'];
69            $consumer_secret = $autotweet['consumer_secret'];
70            $access_token = $autotweet['access_token'];
71            $access_token_secret = $autotweet['access_token_secret'];
72            // Get configuration parameters
73            $message_template = $this->getConf('message_template');
74            $date_format = $this->getConf('date_format');
75
76            // Get the last change information of the current page
77            $last_change = p_get_metadata( $ID, 'last_change', false );
78
79            // Get the change type, [C,E,D,R] and make it readable
80            $change_type = $last_change['type'];
81            if ( $change_type == 'C') {
82                $change_type = $this->getLang('creat');
83            }
84            else if ( $change_type == 'E') {
85                $change_type = $this->getLang('edit');
86            }
87            else if ( $change_type == 'e') {
88                $change_type = $this->getLang('minor_edit');
89            }
90            else if ( $change_type == 'D') {
91                $change_type = $this->getLang('delet');
92            }
93            else if ( $change_type == 'R') {
94                $change_type = $this->getLang('revert');
95            }
96
97            // Assemble the tweet
98            $message = $message_template;
99            $message = str_replace('{date}', strftime( $date_format ), $message );
100            $message = str_replace('{type}', $change_type, $message );
101            $message = str_replace('{user}', $last_change['user'], $message );
102            $message = str_replace('{summary}', $_POST['summary'], $message );
103            $message = str_replace('{page}', $last_change['id'], $message );
104            $message = str_replace('{extra}', $last_change['extra'], $message );
105
106            // Make sure that CURL is installed
107            if ( function_exists('curl_init') )  {
108                include_once DOKU_PLUGIN.'autotweet/twitteroauth.php';
109                // Access Twitter via OAuth
110                $tweet = new TwitterOAuth($consumer_key, $consumer_secret, $access_token, $access_token_secret);
111                //Send the tweet
112                $result = $tweet->post('statuses/update', array('status' => $message));
113            }
114        }
115    }
116}
117