xref: /dokuwiki/lib/exe/xmlrpc.php (revision 96cda5bf14b9eeeb072a6446a684055a0b58e895)
1<?php
2if(!defined('DOKU_INC')) define('DOKU_INC',dirname(__FILE__).'/../../');
3
4// fix when '<?xml' isn't on the very first line
5if(isset($HTTP_RAW_POST_DATA)) $HTTP_RAW_POST_DATA = trim($HTTP_RAW_POST_DATA);
6
7
8//EXPERIMENTAL CODE
9die('remove me to get it work');
10
11require_once(DOKU_INC.'inc/init.php');
12require_once(DOKU_INC.'inc/common.php');
13require_once(DOKU_INC.'inc/auth.php');
14session_write_close();  //close session
15require_once(DOKU_INC.'inc/IXR_Library.php');
16
17
18/**
19 * Contains needed wrapper functions and registers all available
20 * XMLRPC functions.
21 */
22class dokuwiki_xmlrpc_server extends IXR_IntrospectionServer {
23    var $methods = array();
24
25    /**
26     * Constructor. Register methods and run Server
27     */
28    function dokuwiki_xmlrpc_server(){
29        $this->IXR_IntrospectionServer();
30
31        /* DokuWiki's own methods */
32        $this->addCallback(
33            'dokuwiki.getVersion',
34            'getVersion',
35            array('string'),
36            'Returns the running DokuWiki version'
37        );
38
39        /* Wiki API v2 http://www.jspwiki.org/wiki/WikiRPCInterface2 */
40        $this->addCallback(
41            'wiki.getRPCVersionSupported',
42            'this:wiki_RPCVersion',
43            array('int'),
44            'Returns 2 with the supported RPC API version'
45        );
46        $this->addCallback(
47            'wiki.getPage',
48            'this:rawPage',
49            array('string','string'),
50            'Get the raw Wiki text of page, latest version.'
51        );
52        $this->addCallback(
53            'wiki.getPageVersion',
54            'this:rawPage',
55            array('string','string','int'),
56            'Get the raw Wiki text of page.'
57        );
58        $this->addCallback(
59            'wiki.getPageHTML',
60            'this:htmlPage',
61            array('string','string'),
62            'Return page in rendered HTML, latest version.'
63        );
64        $this->addCallback(
65            'wiki.getPageHTMLVersion',
66            'this:htmlPage',
67            array('string','string','int'),
68            'Return page in rendered HTML.'
69        );
70        $this->addCallback(
71            'wiki.getAllPages',
72            'this:listPages',
73            array('struct'),
74            'Returns a list of all pages. The result is an array of utf8 pagenames.'
75        );
76        $this->addCallback(
77            'wiki.getBackLinks',
78            'this:listBackLinks',
79            array('struct','string'),
80            'Returns the pages that link to this page.'
81        );
82        $this->addCallback(
83            'wiki.getPageInfo',
84            'this:pageInfo',
85            array('struct','string'),
86            'Returns a struct with infos about the page.'
87        );
88        $this->addCallback(
89            'wiki.getPageInfoVersion',
90            'this:pageInfo',
91            array('struct','string','int'),
92            'Returns a struct with infos about the page.'
93        );
94        $this->addCallback(
95            'wiki.putPage',
96            'this:putPage',
97            array('int', 'string', 'string'),
98            'Saves a wiki page'
99        );
100        $this->addCallback(
101            'wiki.listLinks',
102            'this:listLinks',
103            array('struct','string'),
104            'Lists all links contained in a wiki page'
105        );
106        $this->addCallback(
107            'wiki.getRecentChanges',
108            'this:getRecentChanges',
109            array('struct','int'),
110            'Returns a strukt about all recent changes since given timestamp.'
111        );
112
113        $this->serve();
114    }
115
116    /**
117     * Return a raw wiki page
118     */
119    function rawPage($id,$rev=''){
120        if(auth_quickaclcheck($id) < AUTH_READ){
121            return new IXR_Error(1, 'You are not allowed to read this page');
122        }
123        return rawWiki($id,$rev);
124    }
125
126    /**
127     * Return a wiki page rendered to html
128     */
129    function htmlPage($id,$rev=''){
130        if(auth_quickaclcheck($id) < AUTH_READ){
131            return new IXR_Error(1, 'You are not allowed to read this page');
132        }
133        return p_wiki_xhtml($id,$rev,false);
134    }
135
136    /**
137     * List all pages - we use the indexer list here
138     */
139    function listPages(){
140        require_once(DOKU_INC.'inc/fulltext.php');
141        return ft_pageLookup('');
142    }
143
144    /**
145     * Return a list of backlinks
146     */
147    function listBackLinks($id){
148        require_once(DOKU_INC.'inc/fulltext.php');
149        return ft_backlinks($id);
150    }
151
152    /**
153     * Return some basic data about a page
154     */
155    function pageInfo($id,$rev=''){
156        if(auth_quickaclcheck($id) < AUTH_READ){
157            return new IXR_Error(1, 'You are not allowed to read this page');
158        }
159        $file = wikiFN($id,$rev);
160        $time = @filemtime($file);
161        if(!$time){
162            return new IXR_Error(10, 'The requested page does not exist');
163        }
164
165        $info = getRevisionInfo($id, $time, 1024);
166
167        $data = array(
168            'name'         => $id,
169            'lastModified' => new IXR_Date($time),
170            'author'       => (($info['user']) ? $info['user'] : $info['ip']),
171            'version'      => $time
172        );
173
174        return ($data);
175    }
176
177    /**
178     * Save a wiki page
179     * FIXME check ACL !!!
180     */
181    function putPage($put_id, $put_text, $put_summary='', $put_minor=0, $put_rev='', $put_pre='', $put_suf='') {
182        global $TEXT;
183
184        $TEXT = $put_text;
185
186        // Check, if page is locked
187        if (checklock($put_id) !== false)
188            return 1;
189
190        //spam check
191        if(checkwordblock())
192            return 2;
193
194        lock($put_id);
195
196        saveWikiText($put_id,con($put_pre,$put_text,$put_suf,1),$put_summary,$put_minor);
197
198        unlock($put_id);
199
200        return 0;
201    }
202
203    /**
204     * Lists all links contained in a wiki page
205     *
206     * @author Michael Klier <chi@chimeric.de>
207     */
208    function listLinks($id) {
209        if(auth_quickaclcheck($id) < AUTH_READ){
210            return new IXR_Error(1, 'You are not allowed to read this page');
211        }
212        $links = array();
213
214        // resolve page instructions
215        $ins   = p_cached_instructions(wikiFN(cleanID($id)));
216
217        // instantiate new Renderer - needed for interwiki links
218        include(DOKU_INC.'inc/parser/xhtml.php');
219        $Renderer = new Doku_Renderer_xhtml();
220        $Renderer->interwiki = getInterwiki();
221
222        // parse parse instructions
223        foreach($ins as $in) {
224            $link = array();
225            switch($in[0]) {
226                case 'internallink':
227                    $link['type'] = 'local';
228                    $link['page'] = $in[1][0];
229                    $link['href'] = wl($in[1][0]);
230                    array_push($links,$link);
231                    break;
232                case 'externallink':
233                    $link['type'] = 'extern';
234                    $link['page'] = $in[1][0];
235                    $link['href'] = $in[1][0];
236                    array_push($links,$link);
237                    break;
238                case 'interwikilink':
239                    $url = $Renderer->_resolveInterWiki($in[1][2],$in[1][3]);
240                    $link['type'] = 'extern';
241                    $link['page'] = $url;
242                    $link['href'] = $url;
243                    array_push($links,$link);
244                    break;
245            }
246        }
247
248        return ($links);
249    }
250
251    /**
252     * Returns a list of recent changes since give timestamp
253     *
254     * @author Michael Klier <chi@chimeric.de>
255     */
256    function getRecentChanges($timestamp) {
257        global $conf;
258
259        if(strlen($timestamp) != 10)
260            return new IXR_Error(20, 'The provided value is not a valid timestamp');
261
262        $changes = array();
263
264        require_once(DOKU_INC.'inc/changelog.php');
265        require_once(DOKU_INC.'inc/pageutils.php');
266
267        // read changes
268        $lines = @file($conf['changelog']);
269
270        if(empty($lines))
271            return new IXR_Error(10, 'The changelog could not be read');
272
273        // we start searching at the end of the list
274        $lines = array_reverse($lines);
275
276        // cache seen pages and skip them
277        $seen = array();
278
279        foreach($lines as $line) {
280
281            if(empty($line)) continue; // skip empty lines
282
283            $logline = parseChangelogLine($line);
284
285            if($logline === false) continue;
286
287            // skip seen ones
288            if(isset($seen[$logline['id']])) continue;
289
290            // skip minors
291            if($logline['type'] === DOKU_CHANGE_TYPE_MINOR_EDIT && ($flags & RECENTS_SKIP_MINORS)) continue;
292
293            // remember in seen to skip additional sights
294            $seen[$logline['id']] = 1;
295
296            // check if it's a hidden page
297            if(isHiddenPage($logline['id'])) continue;
298
299            // check ACL
300            if(auth_quickaclcheck($logline['id']) < AUTH_READ) continue;
301
302            // check existance
303            if((!@file_exists(wikiFN($logline['id']))) && ($flags & RECENTS_SKIP_DELETED)) continue;
304
305            // check if logline is still in the queried time frame
306            if($logline['date'] >= $timestamp) {
307                $change['name']         = $logline['id'];
308                $change['lastModified'] = $logline['date'];
309                $change['author']       = $logline['user'];
310                $change['version']      = $logline['date'];
311                array_push($changes, $change);
312            } else {
313                $changes = array_reverse($changes);
314                return ($changes);
315            }
316        }
317        // in case we still have nothing at this point
318        return new IXR_Error(30, 'There are no changes in the specified timeframe');
319    }
320
321    /**
322     * The version of Wiki RPC API supported
323     */
324    function wiki_RPCVersion(){
325        return 2;
326    }
327
328}
329
330$server = new dokuwiki_xmlrpc_server();
331
332// vim:ts=4:sw=4:enc=utf-8:
333