xref: /plugin/struct/remote.php (revision e46eaffdf6990bc2902b95956d33701b9687af85)
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 dokuwiki\plugin\struct\meta\ConfigParser;
11use dokuwiki\plugin\struct\meta\SearchConfig;
12use dokuwiki\plugin\struct\meta\StructException;
13
14if(!defined('DOKU_INC')) die();
15
16
17class remote_plugin_struct extends DokuWiki_Remote_Plugin {
18    /** @var helper_plugin_struct hlp */
19    protected $hlp;
20
21    /**
22     * remote_plugin_struct constructor.
23     */
24    public function __construct() {
25        parent::__construct();
26
27        /** @var helper_plugin_struct hlp */
28        $this->hlp = plugin_load('helper', 'struct');
29    }
30
31    /**
32     * Get the structured data of a given page
33     *
34     * @param string $page The page to get data for
35     * @param string $schema The schema to use empty for all
36     * @param int $time A timestamp if you want historic data (0 for now)
37     * @return array ('schema' => ( 'fieldlabel' => 'value', ...))
38     * @throws RemoteAccessDeniedException
39     * @throws RemoteException
40     */
41    public function getData($page, $schema, $time) {
42        $page = cleanID($page);
43
44        if(auth_quickaclcheck($page) < AUTH_READ) {
45            throw new RemoteAccessDeniedException('no permissions to access data of that page');
46        }
47
48        if(!$schema) $schema = null;
49
50        try {
51            return $this->hlp->getData($page, $schema, $time);
52        } catch (StructException $e) {
53            throw new RemoteException($e->getMessage(), 0, $e);
54        }
55    }
56
57
58    /**
59     * Saves data for a given page (creates a new revision)
60     *
61     * If this call succeeds you can assume your data has either been saved or it was
62     * not necessary to save it because the data already existed in the wanted form or
63     * the given schemas are no longer assigned to that page.
64     *
65     * @param string $page
66     * @param array $data ('schema' => ( 'fieldlabel' => 'value', ...))
67     * @param string $summary
68     * @param bool $minor
69     * @return bool returns always true
70     * @throws RemoteAccessDeniedException
71     * @throws RemoteException
72     */
73    public function saveData($page, $data, $summary, $minor = false) {
74        $page = cleanID($page);
75
76        if(auth_quickaclcheck($page) < AUTH_EDIT) {
77            throw new RemoteAccessDeniedException('no permissions to save data for that page');
78        }
79
80        try {
81            $this->hlp->saveData($page, $data, $summary, $minor);
82            return true;
83        } catch (StructException $e) {
84            throw new RemoteException($e->getMessage(), 0, $e);
85        }
86    }
87
88    /**
89     * Get info about existing schemas columns
90     *
91     * Returns only current, enabled columns
92     *
93     * @param string $schema the schema to query, empty for all
94     * @return array
95     * @throws RemoteAccessDeniedException
96     * @throws RemoteException
97     */
98    public function getSchema($schema) {
99        if(!auth_ismanager()) {
100            throw new RemoteAccessDeniedException('you need to be manager to access schema info');
101        }
102
103        if(!$schema) $schema = null;
104        try {
105            $result = array();
106            $schemas = $this->hlp->getSchema($schema);
107            foreach($schemas as $name => $schema) {
108                $result[$name] = array();
109                foreach ($schema->getColumns(false) as $column) {
110                    $result[$name][] = array(
111                        'name' => $column->getLabel(),
112                        'type' =>  array_pop(explode('\\', get_class($column->getType()))),
113                        'ismulti' => $column->isMulti()
114                    );
115                }
116            }
117            return $result;
118        } catch (StructException $e) {
119            throw new RemoteException($e->getMessage(), 0, $e);
120        }
121    }
122
123    /**
124     * Get the data that would be shown in an aggregation
125     *
126     * @param array  $schemas array of strings with the schema-names
127     * @param array  $cols array of strings with the columns
128     * @param array  $filter array of arrays with ['logic'=> 'and'|'or', 'condition' => 'your condition']
129     * @param string $sort string indicating the column to sort by
130     *
131     * @return array array of rows, each row is an array of the column values
132     * @throws RemoteException
133     */
134    public function getAggregationData(array $schemas, array $cols, array $filter = [], $sort = '') {
135        $schemaLine = 'schema: ' . implode(', ', $schemas);
136        $columnLine = 'cols: ' . implode(', ', $cols);
137        $filterLines = array_map(function ($filter) {
138            return 'filter' . $filter['logic'] . ': ' . $filter['condition'];
139        }, $filter);
140        $sortLine = 'sort: ' . $sort;
141        // schemas, cols, REV?, filter, order
142
143        try {
144            $parser = new ConfigParser(array_merge([$schemaLine, $columnLine, $sortLine], $filterLines));
145            $config = $parser->getConfig();
146            $search = new SearchConfig($config);
147            $results = $search->execute();
148            $data = [];
149            /** @var \dokuwiki\plugin\struct\meta\Value[] $rowValues */
150            foreach ($results as $rowValues) {
151                $row = [];
152                foreach ($rowValues as $value) {
153                    $row[$value->getColumn()->getFullQualifiedLabel()] = $value->getDisplayValue();
154                }
155                $data[] = $row;
156            }
157            return $data;
158        } catch (StructException $e) {
159            throw new RemoteException($e->getMessage(), 0, $e);
160        }
161    }
162}
163