xref: /plugin/move/remote.php (revision c408662e80d5d53f9907fa131c963e25cb1d49cf)
1a99025d4SClaus-Justus Heine<?php
2a99025d4SClaus-Justus Heine
3a99025d4SClaus-Justus Heine/**
4*c408662eSAndreas Gohr * DokuWiki Plugin move (Remote Component)
5a99025d4SClaus-Justus Heine *
6a99025d4SClaus-Justus Heine * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
7a99025d4SClaus-Justus Heine * @author Claus-Justus Heine <himself@claus-justus-heine.de>
8a99025d4SClaus-Justus Heine */
9a99025d4SClaus-Justus Heineclass remote_plugin_move extends DokuWiki_Remote_Plugin
10a99025d4SClaus-Justus Heine{
11a99025d4SClaus-Justus Heine    /**
12*c408662eSAndreas Gohr     * Rename/move a given page
13a99025d4SClaus-Justus Heine     *
14*c408662eSAndreas Gohr     * @param string $fromId The original page ID
15*c408662eSAndreas Gohr     * @param string $toId The new page ID
16*c408662eSAndreas Gohr     * @return true Always true when no error occured
17*c408662eSAndreas Gohr     * @throws \dokuwiki\Remote\RemoteException when renaming fails
18a99025d4SClaus-Justus Heine     */
1999425b03SClaus-Justus Heine    public function renamePage(string $fromId, string $toId)
20a99025d4SClaus-Justus Heine    {
216d351392SClaus-Justus Heine        $fromId = cleanID($fromId);
226d351392SClaus-Justus Heine        $toId = cleanID($toId);
236d351392SClaus-Justus Heine
24a99025d4SClaus-Justus Heine        /** @var helper_plugin_move_op $MoveOperator */
25*c408662eSAndreas Gohr        $MoveOperator = plugin_load('helper', 'move_op');
26a99025d4SClaus-Justus Heine
27*c408662eSAndreas Gohr        global $MSG;
28*c408662eSAndreas Gohr        $MSG = [];
29*c408662eSAndreas Gohr        if (!$MoveOperator->movePage($fromId, $toId)) {
30*c408662eSAndreas Gohr            throw $this->msgToException($MSG);
31a99025d4SClaus-Justus Heine        }
32a99025d4SClaus-Justus Heine
33*c408662eSAndreas Gohr        return true;
34a99025d4SClaus-Justus Heine    }
35e45414edSClaus-Justus Heine
36e45414edSClaus-Justus Heine    /**
37*c408662eSAndreas Gohr     * Rename/move a given media file
38e45414edSClaus-Justus Heine     *
39*c408662eSAndreas Gohr     * @param string $fromId The original media ID
40*c408662eSAndreas Gohr     * @param string $toId The new media ID
41*c408662eSAndreas Gohr     * @return true Always true when no error occured
42*c408662eSAndreas Gohr     * @throws \dokuwiki\Remote\RemoteException when renaming fails
43e45414edSClaus-Justus Heine     */
44e45414edSClaus-Justus Heine    public function renameMedia(string $fromId, string $toId)
45e45414edSClaus-Justus Heine    {
46e45414edSClaus-Justus Heine        $fromId = cleanID($fromId);
47e45414edSClaus-Justus Heine        $toId = cleanID($toId);
48e45414edSClaus-Justus Heine
49e45414edSClaus-Justus Heine        /** @var helper_plugin_move_op $MoveOperator */
50*c408662eSAndreas Gohr        $MoveOperator = plugin_load('helper', 'move_op');
51e45414edSClaus-Justus Heine
52*c408662eSAndreas Gohr        global $MSG;
53*c408662eSAndreas Gohr        $MSG = [];
54*c408662eSAndreas Gohr        if (!$MoveOperator->moveMedia($fromId, $toId)) {
55*c408662eSAndreas Gohr            throw $this->msgToException($MSG);
56e45414edSClaus-Justus Heine        }
57e45414edSClaus-Justus Heine
58*c408662eSAndreas Gohr        return true;
59*c408662eSAndreas Gohr    }
60*c408662eSAndreas Gohr
61*c408662eSAndreas Gohr    /**
62*c408662eSAndreas Gohr     * Get an exception for the first error message found in the DokuWiki message array.
63*c408662eSAndreas Gohr     *
64*c408662eSAndreas Gohr     * Ideally the move operation should throw an exception, but currently only a return code is available.
65*c408662eSAndreas Gohr     *
66*c408662eSAndreas Gohr     * @param array $messages The DokuWiki message array
67*c408662eSAndreas Gohr     * @return \dokuwiki\Remote\RemoteException
68*c408662eSAndreas Gohr     */
69*c408662eSAndreas Gohr    protected function msgToException($messages)
70*c408662eSAndreas Gohr    {
71*c408662eSAndreas Gohr        foreach ($messages as $msg) {
72*c408662eSAndreas Gohr            if ($msg['lvl'] === -1) {
73*c408662eSAndreas Gohr                // error found return it
74*c408662eSAndreas Gohr                return new \dokuwiki\Remote\RemoteException($msg['msg'], 100);
75*c408662eSAndreas Gohr            }
76*c408662eSAndreas Gohr        }
77*c408662eSAndreas Gohr        // If we reach this point, no error was found
78*c408662eSAndreas Gohr        return new \dokuwiki\Remote\RemoteException('Unknown error', 100);
79e45414edSClaus-Justus Heine    }
80a99025d4SClaus-Justus Heine}
81