xref: /plugin/struct/remote.php (revision 1f07a6f88b4ac6479d253b66b9ceb0f802c00f9c)
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     * @return bool returns always true
69     * @throws RemoteAccessDeniedException
70     * @throws RemoteException
71     */
72    public function saveData($page, $data, $summary) {
73        $page = cleanID($page);
74
75        if(auth_quickaclcheck($page) < AUTH_EDIT) {
76            throw new RemoteAccessDeniedException('no permissions to save data for that page');
77        }
78
79        try {
80            $this->hlp->saveData($page, $data, $summary);
81            return true;
82        } catch (StructException $e) {
83            throw new RemoteException($e->getMessage(), 0, $e);
84        }
85    }
86
87    /**
88     * Get info about existing schemas columns
89     *
90     * Returns only current, enabled columns
91     *
92     * @param string $schema the schema to query, empty for all
93     * @return array
94     * @throws RemoteAccessDeniedException
95     * @throws RemoteException
96     */
97    public function getSchema($schema = null) {
98        if(!auth_ismanager()) {
99            throw new RemoteAccessDeniedException('you need to be manager to access schema info');
100        }
101
102        try {
103            $result = array();
104            $schemas = $this->hlp->getSchema($schema ?: null);
105            foreach($schemas as $name => $schema) {
106                $result[$name] = array();
107                foreach ($schema->getColumns(false) as $column) {
108                    $result[$name][] = array(
109                        'name' => $column->getLabel(),
110                        'type' =>  array_pop(explode('\\', get_class($column->getType()))),
111                        'ismulti' => $column->isMulti()
112                    );
113                }
114            }
115            return $result;
116        } catch (StructException $e) {
117            throw new RemoteException($e->getMessage(), 0, $e);
118        }
119    }
120
121    /**
122     * Get the data that would be shown in an aggregation
123     *
124     * @param array  $schemas array of strings with the schema-names
125     * @param array  $cols array of strings with the columns
126     * @param array  $filter array of arrays with ['logic'=> 'and'|'or', 'condition' => 'your condition']
127     * @param string $sort string indicating the column to sort by
128     *
129     * @return array array of rows, each row is an array of the column values
130     * @throws RemoteException
131     */
132    public function getAggregationData(array $schemas, array $cols, array $filter = [], $sort = '') {
133        $schemaLine = 'schema: ' . implode(', ', $schemas);
134        $columnLine = 'cols: ' . implode(', ', $cols);
135        $filterLines = array_map(function ($filter) {
136            return 'filter' . $filter['logic'] . ': ' . $filter['condition'];
137        }, $filter);
138        $sortLine = 'sort: ' . $sort;
139        // schemas, cols, REV?, filter, order
140
141        try {
142            $parser = new ConfigParser(array_merge([$schemaLine, $columnLine, $sortLine], $filterLines));
143            $config = $parser->getConfig();
144            $search = new SearchConfig($config);
145            $results = $search->execute();
146            $data = [];
147            /** @var \dokuwiki\plugin\struct\meta\Value[] $rowValues */
148            foreach ($results as $rowValues) {
149                $row = [];
150                foreach ($rowValues as $value) {
151                    $row[$value->getColumn()->getFullQualifiedLabel()] = $value->getDisplayValue();
152                }
153                $data[] = $row;
154            }
155            return $data;
156        } catch (StructException $e) {
157            throw new RemoteException($e->getMessage(), 0, $e);
158        }
159    }
160}
161