1<?php
2/**
3 * DokuWiki Plugin HipChat (Action Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Jeremy Ebler <jebler@gmail.com> 2011-09-29
7 *
8 * DokuWiki log: https://github.com/cosmocode/log.git
9 * @author  Adrian Lang <lang@cosmocode.de> 2010-03-28
10 *
11 * Hippy: https://github.com/rcrowe/Hippy.git
12 * @author Rob Crowe <rcrowe@github>
13 */
14
15// must be run within Dokuwiki
16if (!defined('DOKU_INC')) die();
17
18if (!defined('DOKU_LF')) define('DOKU_LF', "\n");
19if (!defined('DOKU_TAB')) define('DOKU_TAB', "\t");
20if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
21
22require 'vendor/autoload.php';
23
24class action_plugin_hipchat extends DokuWiki_Action_Plugin {
25
26    function register(&$controller) {
27       $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handle_action_act_preprocess');
28    }
29
30    function handle_action_act_preprocess(&$event, $param) {
31        global $lang;
32        if (isset($event->data['save'])) {
33            if ($event->data['save'] == $lang['btn_save']) {
34                $this->handle();
35            }
36        }
37        return;
38    }
39
40    private function handle() {
41       global $SUM;
42        global $INFO;
43
44        /* Namespace filter */
45        $ns = $this->getConf('hipchat_namespaces');
46        if (!empty($ns)) {
47            $namespaces = explode(',', $ns);
48            $current_namespace = explode(':', $INFO['namespace']);
49            if (!in_array($current_namespace[0], $namespaces)) {
50                return;
51            }
52        }
53
54        $fullname = $INFO['userinfo']['name'];
55        $username = $INFO['client'];
56        $page     = $INFO['namespace'] . $INFO['id'];
57        $summary  = $SUM;
58        $minor    = (boolean) $_REQUEST['minor'];
59
60        $token = $this->getConf('hipchat_token');
61        $room = $this->getConf('hipchat_room');
62        $from = $this->getConf('hipchat_name');
63
64        $transport = new rcrowe\Hippy\Transport\Guzzle($token, $room, $from);
65        $hippy = new rcrowe\Hippy\Client($transport);
66
67        $say = '<b>' . $fullname . '</b> '.$this->getLang('hipchat_update').'<b><a href="' . $this->urlize() . '">' . $INFO['id'] . '</a></b>';
68        if ($minor) $say = $say . ' ['.$this->getLang('hipchat_minor').']';
69        if ($summary) $say = $say . '<br /><em>' . $summary . '</em>';
70
71        $message = new rcrowe\Hippy\Message(!$minor, rcrowe\Hippy\Message::BACKGROUND_GREEN);
72        $message->setHtml($say);
73        $hippy->send($message);
74    }
75
76    /* Make our URLs! */
77    private function urlize() {
78
79        global $INFO;
80        global $conf;
81        $page = $INFO['id'];
82
83        switch($conf['userewrite']) {
84            case 0:
85                $url = DOKU_URL . "doku.php?id=" . $page;
86                break;
87            case 1:
88                if ($conf['useslash']) {
89                    $page = str_replace(":", "/", $page);
90                }
91                $url = DOKU_URL . $page;
92                break;
93            case 2:
94                if ($conf['useslash']) {
95                    $page = str_replace(":", "/", $page);
96                }
97                $url = DOKU_URL . "doku.php/" . $page;
98                break;
99        }
100        return $url;
101    }
102}
103
104// vim:ts=4:sw=4:et:enc=utf-8:
105