1<?php
2
3/**
4 * DokuWiki Plugin move (Remote Component)
5 *
6 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
7 * @author Claus-Justus Heine <himself@claus-justus-heine.de>
8 */
9class remote_plugin_move extends DokuWiki_Remote_Plugin
10{
11    /**
12     * Rename/move a given page
13     *
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
18     */
19    public function renamePage(string $fromId, string $toId)
20    {
21        $fromId = cleanID($fromId);
22        $toId = cleanID($toId);
23
24        /** @var helper_plugin_move_op $MoveOperator */
25        $MoveOperator = plugin_load('helper', 'move_op');
26
27        global $MSG;
28        $MSG = [];
29        if (!$MoveOperator->movePage($fromId, $toId)) {
30            throw $this->msgToException($MSG);
31        }
32
33        return true;
34    }
35
36    /**
37     * Rename/move a given media file
38     *
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
43     */
44    public function renameMedia(string $fromId, string $toId)
45    {
46        $fromId = cleanID($fromId);
47        $toId = cleanID($toId);
48
49        /** @var helper_plugin_move_op $MoveOperator */
50        $MoveOperator = plugin_load('helper', 'move_op');
51
52        global $MSG;
53        $MSG = [];
54        if (!$MoveOperator->moveMedia($fromId, $toId)) {
55            throw $this->msgToException($MSG);
56        }
57
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);
79    }
80}
81