xref: /plugin/approve/remote.php (revision 85d094b0e9288dfb869c943f9805eff0e173d5ca)
1<?php
2
3use dokuwiki\Extension\RemotePlugin;
4use dokuwiki\plugin\approve\PageRemoteResponse;
5use dokuwiki\Remote\RemoteException;
6
7/**
8 * DokuWiki Plugin approve (Action Component)
9 *
10 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
11 * @author Andreas Gohr <dokuwiki@cosmocode.de>
12 */
13class remote_plugin_approve extends RemotePlugin
14{
15
16    /**
17     * Get wiki pages and their approval status
18     *
19     * Return all pages that are under control of the approval plugin, optionally filtered.
20     *
21     * @param string[] $states Return only pages matchin this state [approved, draft, ready_for_approval]
22     * @param string $namespace Only return pages within this namespace, empty for all
23     * @param string $filter Only return pages matching this regex, empty for all
24     * @param string $approver Only return pages to be approved by this user or group, empty for all
25     * @return PageRemoteResponse[]
26     * @throws RemoteException
27     */
28    public function getPages(
29        $states = ['approved', 'draft', 'ready_for_approval'], $namespace = '', $filter = '', $approver = ''
30    )
31    {
32        global $auth;
33
34        if (array_diff($states, ['approved', 'draft', 'ready_for_approval'])) {
35            throw new RemoteException('Invalid state(s) provided', 122);
36        }
37
38        $namespace = cleanID($namespace);
39
40        if (@preg_match('/' . $filter . '/', null) === false) {
41            throw new RemoteException('Invalid filter regex', 123);
42        }
43
44        $approver = $auth->cleanUser($approver);
45
46        /** @var helper_plugin_approve_db $db */
47        $db = plugin_load('helper', 'approve_db');
48        $pages = $db->getPages($approver, $states, $namespace, $filter);
49
50        return array_map(function ($data) {
51            return new PageRemoteResponse($data);
52        }, $pages);
53    }
54
55    /**
56     * Set the approval status of a page
57     *
58     * Mark a given page as approved or ready for approval
59     *
60     * @param string $page The page id
61     * @param string $status The new status [approved, ready_for_approval]
62     * @return true
63     * @throws RemoteException
64     */
65    public function setStatus($page, $status)
66    {
67        /** @var helper_plugin_approve_acl $acl */
68        $acl = plugin_load('helper', 'approve_acl');
69
70        /** @var helper_plugin_approve_db $db */
71        $db = plugin_load('helper', 'approve_db');
72
73        if (!page_exists($page)) {
74            throw new RemoteException('Page does not exist', 121);
75        }
76
77        if (!$acl->useApproveHere($page)) {
78            throw new RemoteException('This page is not under control of the approve plugin', 124);
79        }
80
81        global $INFO;
82        global $USERINFO;
83        $INFO['userinfo'] = $USERINFO;
84
85        if ($status == 'approved') {
86            if (!$acl->clientCanApprove($page)) {
87                throw new RemoteException('You are not allowed to approve this page', 111);
88            }
89            $db->setApprovedStatus($page);
90        } elseif ($status == 'ready_for_approval') {
91            if (!$acl->clientCanMarkReadyForApproval($page)) {
92                throw new RemoteException('You are not allowed to mark this page as ready for approval', 111);
93            }
94            $db->setReadyForApprovalStatus($page);
95        } else {
96            throw new RemoteException('Invalid status', 122);
97
98        }
99
100        return true;
101    }
102}
103