Skip to content

Commit c408662

Browse files
committed
Clean up the remote API
Introduces proper remote exceptions on errors.
1 parent e45414e commit c408662

File tree

1 file changed

+43
-25
lines changed

1 file changed

+43
-25
lines changed

remote.php

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,80 @@
11
<?php
22

33
/**
4-
* DokuWiki Plugin move (Helper Component)
4+
* DokuWiki Plugin move (Remote Component)
55
*
66
* @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
77
* @author Claus-Justus Heine <[email protected]>
88
*/
9-
109
class remote_plugin_move extends DokuWiki_Remote_Plugin
1110
{
1211
/**
13-
* Rename a Wiki page using XMLRPC.
12+
* Rename/move a given page
1413
*
15-
* @param string $fromId
16-
* @param string $toId
17-
* @return int
14+
* @param string $fromId The original page ID
15+
* @param string $toId The new page ID
16+
* @return true Always true when no error occured
17+
* @throws \dokuwiki\Remote\RemoteException when renaming fails
1818
*/
1919
public function renamePage(string $fromId, string $toId)
2020
{
21-
// Other RPC methods also implicitly sanitze the name, so ...
2221
$fromId = cleanID($fromId);
2322
$toId = cleanID($toId);
2423

2524
/** @var helper_plugin_move_op $MoveOperator */
26-
$moveOperator = plugin_load('helper', 'move_op');
27-
$moveAction = plugin_load('action', 'move_rename');
28-
29-
if (!$moveAction->renameOkay($fromId)) {
30-
return 0;
31-
}
25+
$MoveOperator = plugin_load('helper', 'move_op');
3226

33-
if (!$moveOperator->movePage($fromId, $toId)) {
34-
return 0;
27+
global $MSG;
28+
$MSG = [];
29+
if (!$MoveOperator->movePage($fromId, $toId)) {
30+
throw $this->msgToException($MSG);
3531
}
3632

37-
return 1;
33+
return true;
3834
}
3935

4036
/**
41-
* Rename a Wiki media file using XMLRPC.
37+
* Rename/move a given media file
4238
*
43-
* @param string $fromId
44-
* @param string $toId
45-
* @return int
39+
* @param string $fromId The original media ID
40+
* @param string $toId The new media ID
41+
* @return true Always true when no error occured
42+
* @throws \dokuwiki\Remote\RemoteException when renaming fails
4643
*/
4744
public function renameMedia(string $fromId, string $toId)
4845
{
49-
// Other RPC methods also implicitly sanitze the name, so ...
5046
$fromId = cleanID($fromId);
5147
$toId = cleanID($toId);
5248

5349
/** @var helper_plugin_move_op $MoveOperator */
54-
$moveOperator = plugin_load('helper', 'move_op');
50+
$MoveOperator = plugin_load('helper', 'move_op');
5551

56-
if (!$moveOperator->moveMedia($fromId, $toId)) {
57-
return 0;
52+
global $MSG;
53+
$MSG = [];
54+
if (!$MoveOperator->moveMedia($fromId, $toId)) {
55+
throw $this->msgToException($MSG);
5856
}
5957

60-
return 1;
58+
return true;
59+
}
60+
61+
/**
62+
* Get an exception for the first error message found in the DokuWiki message array.
63+
*
64+
* Ideally the move operation should throw an exception, but currently only a return code is available.
65+
*
66+
* @param array $messages The DokuWiki message array
67+
* @return \dokuwiki\Remote\RemoteException
68+
*/
69+
protected function msgToException($messages)
70+
{
71+
foreach ($messages as $msg) {
72+
if ($msg['lvl'] === -1) {
73+
// error found return it
74+
return new \dokuwiki\Remote\RemoteException($msg['msg'], 100);
75+
}
76+
}
77+
// If we reach this point, no error was found
78+
return new \dokuwiki\Remote\RemoteException('Unknown error', 100);
6179
}
6280
}

0 commit comments

Comments
 (0)