4 * Increased whenever the API is changed
6 define('DOKU_API_VERSION', 7);
12 public function __construct(RemoteAPI $api) {
16 function __getRemoteInfo() {
18 'dokuwiki.getVersion' => array(
21 'doc' => 'Returns the running DokuWiki version.'
22 ), 'dokuwiki.login' => array(
23 'args' => array('string', 'string'),
25 'doc' => 'Tries to login with the given credentials and sets auth cookies.',
27 ), 'dokuwiki.getPagelist' => array(
28 'args' => array('string', 'array'),
30 'doc' => 'List all pages within the given namespace.',
31 'name' => 'readNamespace'
32 ), 'dokuwiki.search' => array(
33 'args' => array('string'),
35 'doc' => 'Perform a fulltext search and return a list of matching pages'
36 ), 'dokuwiki.getTime' => array(
39 'doc' => 'Returns the current time at the remote wiki server as Unix timestamp.',
40 ), 'dokuwiki.setLocks' => array(
41 'args' => array('array'),
43 'doc' => 'Lock or unlock pages.'
44 ), 'dokuwiki.getTitle' => array(
47 'doc' => 'Returns the wiki title.',
49 ), 'dokuwiki.appendPage' => array(
50 'args' => array('string', 'string', 'array'),
52 'doc' => 'Append text to a wiki page.'
53 ), 'wiki.getPage' => array(
54 'args' => array('string'),
56 'doc' => 'Get the raw Wiki text of page, latest version.',
58 ), 'wiki.getPageVersion' => array(
59 'args' => array('string', 'int'),
62 'doc' => 'Return a raw wiki page'
63 ), 'wiki.getPageHTML' => array(
64 'args' => array('string'),
66 'doc' => 'Return page in rendered HTML, latest version.',
68 ), 'wiki.getPageHTMLVersion' => array(
69 'args' => array('string', 'int'),
71 'doc' => 'Return page in rendered HTML.',
73 ), 'wiki.getAllPages' => array(
76 'doc' => 'Returns a list of all pages. The result is an array of utf8 pagenames.',
78 ), 'wiki.getAttachments' => array(
79 'args' => array('string', 'array'),
81 'doc' => 'Returns a list of all media files.',
82 'name' => 'listAttachments'
83 ), 'wiki.getBackLinks' => array(
84 'args' => array('string'),
86 'doc' => 'Returns the pages that link to this page.',
87 'name' => 'listBackLinks'
88 ), 'wiki.getPageInfo' => array(
89 'args' => array('string'),
91 'doc' => 'Returns a struct with infos about the page.',
93 ), 'wiki.getPageInfoVersion' => array(
94 'args' => array('string', 'int'),
96 'doc' => 'Returns a struct with infos about the page.',
98 ), 'wiki.getPageVersions' => array(
99 'args' => array('string', 'int'),
101 'doc' => 'Returns the available revisions of the page.',
102 'name' => 'pageVersions'
103 ), 'wiki.putPage' => array(
104 'args' => array('string', 'string', 'array'),
106 'doc' => 'Saves a wiki page.'
107 ), 'wiki.listLinks' => array(
108 'args' => array('string'),
110 'doc' => 'Lists all links contained in a wiki page.'
111 ), 'wiki.getRecentChanges' => array(
112 'args' => array('int'),
114 'Returns a struct about all recent changes since given timestamp.'
115 ), 'wiki.getRecentMediaChanges' => array(
116 'args' => array('int'),
118 'Returns a struct about all recent media changes since given timestamp.'
119 ), 'wiki.aclCheck' => array(
120 'args' => array('string'),
122 'doc' => 'Returns the permissions of a given wiki page.'
123 ), 'wiki.putAttachment' => array(
124 'args' => array('string', 'file', 'array'),
126 'doc' => 'Upload a file to the wiki.'
127 ), 'wiki.deleteAttachment' => array(
128 'args' => array('string'),
130 'doc' => 'Delete a file from the wiki.'
131 ), 'wiki.getAttachment' => array(
132 'args' => array('string'),
133 'doc' => 'Return a media file',
135 'name' => 'getAttachment',
136 ), 'wiki.getAttachmentInfo' => array(
137 'args' => array('string'),
139 'doc' => 'Returns a struct with infos about the attachment.'
140 ), 'dokuwiki.getXMLRPCAPIVersion' => array(
142 'name' => 'getAPIVersion',
144 'doc' => 'Returns the XMLRPC API version.',
146 ), 'wiki.getRPCVersionSupported' => array(
148 'name' => 'wiki_RPCVersion',
150 'doc' => 'Returns 2 with the supported RPC API version.',
157 function getVersion() {
166 * Return a raw wiki page
167 * @param string $id wiki page id
168 * @param string $rev revision number of the page
171 function rawPage($id,$rev=''){
172 $id = $this->resolvePageId($id);
173 if(auth_quickaclcheck($id) < AUTH_READ){
174 throw new RemoteAccessDeniedException('You are not allowed to read this file', 111);
176 $text = rawWiki($id,$rev);
178 return pageTemplate($id);
185 * Return a media file
187 * @author Gina Haeussge <osd@foosel.net>
188 * @param string $id file id
191 function getAttachment($id){
193 if (auth_quickaclcheck(getNS($id).':*') < AUTH_READ) {
194 throw new RemoteAccessDeniedException('You are not allowed to read this file', 211);
197 $file = mediaFN($id);
198 if (!@ file_exists($file)) {
199 throw new RemoteException('The requested file does not exist', 221);
202 $data = io_readFile($file, false);
203 return $this->api->toFile($data);
207 * Return info about a media file
209 * @author Gina Haeussge <osd@foosel.net>
211 function getAttachmentInfo($id){
214 'lastModified' => $this->api->toDate(0),
218 $file = mediaFN($id);
219 if ((auth_quickaclcheck(getNS($id).':*') >= AUTH_READ) && file_exists($file)){
220 $info['lastModified'] = $this->api->toDate(filemtime($file));
221 $info['size'] = filesize($file);
228 * Return a wiki page rendered to html
230 function htmlPage($id,$rev=''){
231 $id = $this->resolvePageId($id);
232 if(auth_quickaclcheck($id) < AUTH_READ){
233 throw new RemoteAccessDeniedException('You are not allowed to read this page', 111);
235 return p_wiki_xhtml($id,$rev,false);
239 * List all pages - we use the indexer list here
241 function listPages(){
243 $pages = idx_get_indexer()->getPages();
244 $pages = array_filter(array_filter($pages,'isVisiblePage'),'page_exists');
246 foreach(array_keys($pages) as $idx) {
247 $perm = auth_quickaclcheck($pages[$idx]);
248 if($perm < AUTH_READ) {
252 $page['id'] = trim($pages[$idx]);
253 $page['perms'] = $perm;
254 $page['size'] = @filesize(wikiFN($pages[$idx]));
255 $page['lastModified'] = $this->api->toDate(@filemtime(wikiFN($pages[$idx])));
263 * List all pages in the given namespace (and below)
265 function readNamespace($ns,$opts){
268 if(!is_array($opts)) $opts=array();
271 $dir = utf8_encodeFN(str_replace(':', '/', $ns));
273 $opts['skipacl'] = 0; // no ACL skipping for XMLRPC
274 search($data, $conf['datadir'], 'search_allpages', $opts, $dir);
279 * List all pages in the given namespace (and below)
281 function search($query){
283 $data = ft_pageSearch($query,$regex);
286 // prepare additional data
288 foreach($data as $id => $score){
291 if($idx < FT_SNIPPET_NUMBER){
292 $snippet = ft_snippet($id,$regex);
300 'score' => intval($score),
301 'rev' => filemtime($file),
302 'mtime' => filemtime($file),
303 'size' => filesize($file),
304 'snippet' => $snippet,
305 'title' => useHeading('navigation') ? p_get_first_heading($id) : $id
312 * Returns the wiki title.
316 return $conf['title'];
320 * List all media files.
322 * Available options are 'recursive' for also including the subnamespaces
323 * in the listing, and 'pattern' for filtering the returned files against
324 * a regular expression matching their name.
326 * @author Gina Haeussge <osd@foosel.net>
328 function listAttachments($ns, $options = array()) {
333 if (!is_array($options)) $options = array();
334 $options['skipacl'] = 0; // no ACL skipping for XMLRPC
337 if(auth_quickaclcheck($ns.':*') >= AUTH_READ) {
338 $dir = utf8_encodeFN(str_replace(':', '/', $ns));
341 search($data, $conf['mediadir'], 'search_media', $options, $dir);
343 if(!$len) return array();
345 for($i=0; $i<$len; $i++) {
346 unset($data[$i]['meta']);
347 $data[$i]['lastModified'] = $this->api->toDate($data[$i]['mtime']);
351 throw new RemoteAccessDeniedException('You are not allowed to list media files.', 215);
356 * Return a list of backlinks
358 function listBackLinks($id){
359 return ft_backlinks($this->resolvePageId($id));
363 * Return some basic data about a page
365 function pageInfo($id,$rev=''){
366 $id = $this->resolvePageId($id);
367 if(auth_quickaclcheck($id) < AUTH_READ){
368 throw new RemoteAccessDeniedException('You are not allowed to read this page', 111);
370 $file = wikiFN($id,$rev);
371 $time = @filemtime($file);
373 throw new RemoteException(10, 'The requested page does not exist', 121);
376 $info = getRevisionInfo($id, $time, 1024);
380 'lastModified' => $this->api->toDate($time),
381 'author' => (($info['user']) ? $info['user'] : $info['ip']),
391 * @author Michael Klier <chi@chimeric.de>
393 function putPage($id, $text, $params) {
397 $id = $this->resolvePageId($id);
398 $TEXT = cleanText($text);
399 $sum = $params['sum'];
400 $minor = $params['minor'];
403 throw new RemoteException('Empty page ID', 131);
406 if(!page_exists($id) && trim($TEXT) == '' ) {
407 throw new RemoteException('Refusing to write an empty new wiki page', 132);
410 if(auth_quickaclcheck($id) < AUTH_EDIT) {
411 throw new RemoteAccessDeniedException('You are not allowed to edit this page', 112);
414 // Check, if page is locked
416 throw new RemoteException('The page is currently locked', 133);
420 if(checkwordblock()) {
421 throw new RemoteException('Positive wordblock check', 134);
424 // autoset summary on new pages
425 if(!page_exists($id) && empty($sum)) {
426 $sum = $lang['created'];
429 // autoset summary on deleted pages
430 if(page_exists($id) && empty($TEXT) && empty($sum)) {
431 $sum = $lang['deleted'];
436 saveWikiText($id,$TEXT,$sum,$minor);
440 // run the indexer if page wasn't indexed yet
447 * Appends text to a wiki page.
449 function appendPage($id, $text, $params) {
450 $currentpage = $this->rawPage($id);
451 if (!is_string($currentpage)) {
454 return $this->putPage($id, $currentpage.$text, $params);
458 * Uploads a file to the wiki.
460 * Michael Klier <chi@chimeric.de>
462 function putAttachment($id, $file, $params) {
464 $auth = auth_quickaclcheck(getNS($id).':*');
467 throw new RemoteException('Filename not given.', 231);
472 $ftmp = $conf['tmpdir'] . '/' . md5($id.clientIP());
474 // save temporary file
476 io_saveFile($ftmp, $file);
478 $res = media_save(array('name' => $ftmp), $id, $params['ow'], $auth, 'rename');
479 if (is_array($res)) {
480 throw new RemoteException($res[0], -$res[1]);
487 * Deletes a file from the wiki.
489 * @author Gina Haeussge <osd@foosel.net>
491 function deleteAttachment($id){
493 $auth = auth_quickaclcheck(getNS($id).':*');
494 $res = media_delete($id, $auth);
495 if ($res & DOKU_MEDIA_DELETED) {
497 } elseif ($res & DOKU_MEDIA_NOT_AUTH) {
498 throw new RemoteAccessDeniedException('You don\'t have permissions to delete files.', 212);
499 } elseif ($res & DOKU_MEDIA_INUSE) {
500 throw new RemoteException('File is still referenced', 232);
502 throw new RemoteException('Could not delete file', 233);
507 * Returns the permissions of a given wiki page
509 function aclCheck($id) {
510 $id = $this->resolvePageId($id);
511 return auth_quickaclcheck($id);
515 * Lists all links contained in a wiki page
517 * @author Michael Klier <chi@chimeric.de>
519 function listLinks($id) {
520 $id = $this->resolvePageId($id);
521 if(auth_quickaclcheck($id) < AUTH_READ){
522 throw new RemoteAccessDeniedException('You are not allowed to read this page', 111);
526 // resolve page instructions
527 $ins = p_cached_instructions(wikiFN($id));
529 // instantiate new Renderer - needed for interwiki links
530 include(DOKU_INC.'inc/parser/xhtml.php');
531 $Renderer = new Doku_Renderer_xhtml();
532 $Renderer->interwiki = getInterwiki();
534 // parse parse instructions
535 foreach($ins as $in) {
539 $link['type'] = 'local';
540 $link['page'] = $in[1][0];
541 $link['href'] = wl($in[1][0]);
542 array_push($links,$link);
545 $link['type'] = 'extern';
546 $link['page'] = $in[1][0];
547 $link['href'] = $in[1][0];
548 array_push($links,$link);
550 case 'interwikilink':
551 $url = $Renderer->_resolveInterWiki($in[1][2],$in[1][3]);
552 $link['type'] = 'extern';
553 $link['page'] = $url;
554 $link['href'] = $url;
555 array_push($links,$link);
564 * Returns a list of recent changes since give timestamp
566 * @author Michael Hamann <michael@content-space.de>
567 * @author Michael Klier <chi@chimeric.de>
569 function getRecentChanges($timestamp) {
570 if(strlen($timestamp) != 10) {
571 throw new RemoteException('The provided value is not a valid timestamp', 311);
574 $recents = getRecentsSince($timestamp);
578 foreach ($recents as $recent) {
580 $change['name'] = $recent['id'];
581 $change['lastModified'] = $this->api->toDate($recent['date']);
582 $change['author'] = $recent['user'];
583 $change['version'] = $recent['date'];
584 $change['perms'] = $recent['perms'];
585 $change['size'] = @filesize(wikiFN($recent['id']));
586 array_push($changes, $change);
589 if (!empty($changes)) {
592 // in case we still have nothing at this point
593 return new RemoteException('There are no changes in the specified timeframe', 321);
598 * Returns a list of recent media changes since give timestamp
600 * @author Michael Hamann <michael@content-space.de>
601 * @author Michael Klier <chi@chimeric.de>
603 function getRecentMediaChanges($timestamp) {
604 if(strlen($timestamp) != 10)
605 throw new RemoteException('The provided value is not a valid timestamp', 311);
607 $recents = getRecentsSince($timestamp, null, '', RECENTS_MEDIA_CHANGES);
611 foreach ($recents as $recent) {
613 $change['name'] = $recent['id'];
614 $change['lastModified'] = $this->api->toDate($recent['date']);
615 $change['author'] = $recent['user'];
616 $change['version'] = $recent['date'];
617 $change['perms'] = $recent['perms'];
618 $change['size'] = @filesize(mediaFN($recent['id']));
619 array_push($changes, $change);
622 if (!empty($changes)) {
625 // in case we still have nothing at this point
626 throw new RemoteException('There are no changes in the specified timeframe', 321);
631 * Returns a list of available revisions of a given wiki page
633 * @author Michael Klier <chi@chimeric.de>
635 function pageVersions($id, $first) {
636 $id = $this->resolvePageId($id);
637 if(auth_quickaclcheck($id) < AUTH_READ) {
638 throw new RemoteAccessDeniedException('You are not allowed to read this page', 111);
645 throw new RemoteException('Empty page ID', 131);
648 $revisions = getRevisions($id, $first, $conf['recent']+1);
650 if(count($revisions)==0 && $first!=0) {
652 $revisions = getRevisions($id, $first, $conf['recent']+1);
655 if(count($revisions)>0 && $first==0) {
656 array_unshift($revisions, ''); // include current revision
657 array_pop($revisions); // remove extra log entry
660 if(count($revisions) > $conf['recent']) {
661 array_pop($revisions); // remove extra log entry
664 if(!empty($revisions)) {
665 foreach($revisions as $rev) {
666 $file = wikiFN($id,$rev);
667 $time = @filemtime($file);
668 // we check if the page actually exists, if this is not the
669 // case this can lead to less pages being returned than
670 // specified via $conf['recent']
672 $info = getRevisionInfo($id, $time, 1024);
674 $data['user'] = $info['user'];
675 $data['ip'] = $info['ip'];
676 $data['type'] = $info['type'];
677 $data['sum'] = $info['sum'];
678 $data['modified'] = $this->api->toDate($info['date']);
679 $data['version'] = $info['date'];
680 array_push($versions, $data);
691 * The version of Wiki RPC API supported
693 function wiki_RPCVersion(){
699 * Locks or unlocks a given batch of pages
701 * Give an associative array with two keys: lock and unlock. Both should contain a
702 * list of pages to lock or unlock
704 * Returns an associative array with the keys locked, lockfail, unlocked and
705 * unlockfail, each containing lists of pages.
707 function setLocks($set){
711 $unlockfail = array();
713 foreach((array) $set['lock'] as $id){
714 $id = $this->resolvePageId($id);
715 if(auth_quickaclcheck($id) < AUTH_EDIT || checklock($id)){
723 foreach((array) $set['unlock'] as $id){
724 $id = $this->resolvePageId($id);
725 if(auth_quickaclcheck($id) < AUTH_EDIT || !unlock($id)){
734 'lockfail' => $lockfail,
735 'unlocked' => $unlocked,
736 'unlockfail' => $unlockfail,
740 function getAPIVersion(){
741 return DOKU_API_VERSION;
744 function login($user,$pass){
747 if(!$conf['useacl']) return 0;
750 @session_start(); // reopen session for login
751 if($auth->canDo('external')){
752 $ok = $auth->trustExternal($user,$pass,false);
760 $ok = trigger_event('AUTH_LOGIN_CHECK', $evdata, 'auth_login_wrapper');
762 session_write_close(); // we're done with the session
767 private function resolvePageId($id) {
771 $id = cleanID($conf['start']);