1<?php
2/**
3 * DokuWiki Plugin interwiki (Action Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  haobug <qingxianhao@gmail.com>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) die('interwiki plugin die');
11
12if (!defined('DOKU_PLUGIN'))
13    define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
14
15define('INTERWIKI_ICONS',DOKU_INC.'lib/images/interwiki/');
16define('INTERWIKI_PREFIX','../interwiki/');
17
18require_once (DOKU_PLUGIN . 'action.php');
19
20class action_plugin_interwiki extends DokuWiki_Action_Plugin {
21    var $extensions = array('.png','.gif');
22    /**
23     * Register the eventhandlers
24     */
25    function register(Doku_Event_Handler $contr) {
26        $contr->register_hook('TOOLBAR_DEFINE', 'AFTER', $this, 'insert_button', array ());
27    }
28
29    function getInterwikiButtons() {
30        $wikis = getInterwiki();
31        if(count($wikis) < 1)
32            return array();
33
34        $btns = array();
35        $excludes = array_unique(
36            array_map('trim',
37                explode(',',
38                    strtolower(
39                         $this->getConf('excluded_shortcuts')
40                    )
41                )
42            )
43        );
44        sort($excludes);
45
46        foreach($wikis as $key => $url) {
47            if(in_array($key, $excludes))
48                continue;
49
50            $icon ='';
51            foreach ($this->extensions as $ext){
52                if(file_exists(INTERWIKI_ICONS.$key.$ext)){
53                    $icon = INTERWIKI_PREFIX.$key.$ext;
54                    break;
55                }
56            }
57            if(''==$icon)
58                continue;
59
60            $btn_tmp = array(
61                'type'   => 'format',
62                'title'  => $key,
63                'icon'   => $icon,
64                'open'   => '[['.$key.'>',
65                'close'  => ']]',
66            );
67            array_push($btns, $btn_tmp);
68        }
69        return $btns;
70    }
71    /**
72     * Inserts the toolbar button
73     */
74    function insert_button(&$event, $param) {
75        $event->data[] = array(
76            'type'   => 'picker',
77            'title'  => $this->getLang('qb_interwiki'),
78            'icon'   => '../../images/interwiki.png',
79            'list'   => $this->getInterwikiButtons(),
80        );
81
82    }
83}
84