xref: /plugin/struct/remote.php (revision 881e940c5447b444534e39519dc2fdb881587526)
1<?php
2/**
3 * DokuWiki Plugin struct (Helper Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Andreas Gohr, Michael Große <dokuwiki@cosmocode.de>
7 */
8
9// must be run within Dokuwiki
10use plugin\struct\meta\Assignments;
11use plugin\struct\meta\Schema;
12use plugin\struct\meta\SchemaData;
13use plugin\struct\meta\StructException;
14use plugin\struct\meta\Validator;
15
16if(!defined('DOKU_INC')) die();
17
18
19class remote_plugin_struct extends DokuWiki_Remote_Plugin {
20    /** @var helper_plugin_struct hlp */
21    protected $hlp;
22
23    /**
24     * remote_plugin_struct constructor.
25     */
26    public function __construct() {
27        parent::__construct();
28
29        /** @var helper_plugin_struct hlp */
30        $this->hlp = plugin_load('helper', 'struct');
31    }
32
33    /**
34     * Get the structured data of a given page
35     *
36     * @param string $page The page to get data for
37     * @param string $schema The schema to use empty for all
38     * @param int $time A timestamp if you want historic data (0 for now)
39     * @return array ('schema' => ( 'fieldlabel' => 'value', ...))
40     * @throws RemoteAccessDeniedException
41     * @throws RemoteException
42     */
43    public function getData($page, $schema, $time) {
44        $page = cleanID($page);
45
46        if(!auth_quickaclcheck($page) < AUTH_READ) {
47            throw new RemoteAccessDeniedException('no permissions to access data of that page');
48        }
49
50        if(!$schema) $schema = null;
51
52        try {
53            return $this->hlp->getData($page, $schema, $time);
54        } catch (StructException $e) {
55            throw new RemoteException($e->getMessage(), 0, $e);
56        }
57    }
58
59
60    /**
61     * Saves data for a given page (creates a new revision)
62     *
63     * If this call succeeds you can assume your data has either been saved or it was
64     * not necessary to save it because the data already existed in the wanted form or
65     * the given schemas are no longer assigned to that page.
66     *
67     * @param string $page
68     * @param array $data ('schema' => ( 'fieldlabel' => 'value', ...))
69     * @param string $summary
70     * @return bool returns always true
71     * @throws RemoteAccessDeniedException
72     * @throws RemoteException
73     */
74    public function saveData($page, $data, $summary) {
75        $page = cleanID($page);
76
77        if(!auth_quickaclcheck($page) < AUTH_EDIT) {
78            throw new RemoteAccessDeniedException('no permissions to save data for that page');
79        }
80
81        try {
82            $this->hlp->saveData($page, $data, $summary);
83            return true;
84        } catch (StructException $e) {
85            throw new RemoteException($e->getMessage(), 0, $e);
86        }
87    }
88
89    /**
90     * Get info about existing schemas columns
91     *
92     * Returns only current, enabled columns
93     *
94     * @param string $schema the schema to query, empty for all
95     * @return array
96     * @throws RemoteAccessDeniedException
97     * @throws RemoteException
98     */
99    public function getSchema($schema) {
100        if(!auth_ismanager()) {
101            throw new RemoteAccessDeniedException('you need to be manager to access schema info');
102        }
103
104        if(!$schema) $schema = null;
105        try {
106            $result = array();
107            $schemas = $this->hlp->getSchema($schema);
108            foreach($schemas as $name => $schema) {
109                $result[$name] = array();
110                foreach ($schema->getColumns(false) as $column) {
111                    $result[$name][] = array(
112                        'name' => $column->getLabel(),
113                        'type' =>  array_pop(explode('\\', get_class($column->getType()))),
114                        'ismulti' => $column->isMulti()
115                    );
116                }
117            }
118            return $result;
119        } catch (StructException $e) {
120            throw new RemoteException($e->getMessage(), 0, $e);
121        }
122    }
123}
124