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