1<?php
2
3use dokuwiki\Remote\Api;
4
5if (!defined('DOKU_INC')) die();
6
7/**
8 * Class action_plugin_api
9 * Implements a rest api wrapper around XML rpc
10 *
11 * https://www.dokuwiki.org/devel:xmlrpc
12 *
13 * Test:
14 * http://localhost:81/lib/exe/ajax.php?call=api
15 *
16 * @see RemoteAPI for the entry point
17 * @see RemoteAPICore for the implementation of each functions
18 */
19class  action_plugin_api extends DokuWiki_Action_Plugin
20{
21
22    const PLUGIN_NAME = 'api';
23
24    function register(Doku_Event_Handler $controller)
25    {
26        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, '_ajax_call');
27    }
28
29    /**
30     * handle ajax requests
31     * @param $event Doku_Event
32     */
33    function _ajax_call(&$event)
34    {
35        $info = confToHash(__DIR__ . '/plugin.info.txt');
36
37        if ($event->data !== self::getPluginName()) {
38            return;
39        }
40        //no other ajax call handlers needed
41        $event->stopPropagation();
42        $event->preventDefault();
43
44        global $conf;
45        $conf['remote'] = true;
46        $conf['remoteuser'] = '@ALL';
47        $response_code = 200;
48
49        global $INPUT;
50        $fn = $INPUT->str('fn');
51
52        //$remote = new RemoteAPI();
53        $remote = new API();
54        switch ($fn) {
55            case '':
56                $data = array(
57                    "api" => self::PLUGIN_NAME,
58                    "version" => $info['date']
59                );
60                break;
61            case 'version':
62                $wikiVersion = $remote->call('dokuwiki.getVersion');
63                $rpcVersion = $remote->call('wiki.getRPCVersionSupported');
64                $pluginApiVersion = $info['date'];
65                $data = array(
66                    'wiki' => $wikiVersion,
67                    self::PLUGIN_NAME => $pluginApiVersion,
68                    'rpc' => $rpcVersion,
69                );
70                break;
71            case 'wiki':
72                $wikiTitle = $remote->call('dokuwiki.getTitle');
73                $wikiVersion = $remote->call('dokuwiki.getVersion');
74                $data = array(
75                    'version' => $wikiVersion,
76                    'title' => $wikiTitle,
77                );
78                break;
79            case 'pages':
80                $allPages = $remote->call('wiki.getAllPages');
81                $data = array();
82                $limit = $INPUT->str('limit');
83                if (!$limit) {
84                    $limit = PHP_INT_MAX;
85                }
86                foreach ($allPages as $key => $pages) {
87                    $pageData = array();
88                    $pageData['id'] = $pages['id'];
89                    $pageData['title'] = tpl_pagetitle($pages['id'], true);
90                    $data[] = $pageData;
91                    if ($key >= $limit - 1) {
92                        break;
93                    }
94                }
95                break;
96            case 'page':
97                $id = $INPUT->str('id');
98                if ($id == '') {
99                    $response_code = 400;
100                    $data = array(
101                        'error' => 'The id query parameters is mandatory when asking data of a page'
102                    );
103                    break;
104                }
105                $data['title'] = tpl_pagetitle($id, true);
106                $data['html'] = $remote->call('wiki.getPageHTML', array($id));
107                $data['backlinks'] = $remote->call('wiki.getBackLinks', array($id));
108                $allLinks = $remote->call('wiki.listLinks', array($id));
109                $links = array();
110                $externalLinks = array();
111                foreach ($allLinks as $link) {
112                    if ($link['type'] == 'local') {
113                        $links[] = $link['page'];
114                    } else {
115                        $externalLinks[] = $link['href'];
116                    }
117                }
118                $data['links'] = $links;
119                $data['external_links'] = $externalLinks;
120                break;
121            default:
122                $data = 'Function (' . $fn . ') was not found';
123                $response_code = 404;
124        }
125
126
127        // Return
128
129        header('Content-Type: application/json');
130        http_response_code($response_code);
131        if ($_GET["callback"] ?? null) {
132            echo $_GET["callback"] . "(" . json_encode($data) . ")";
133        } else {
134            echo json_encode($data);
135        }
136    }
137
138
139}
140