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