xref: /plugin/struct/remote.php (revision d6d97f6064c3b0f90310be8341edc9585520ee54)
1b36f8833SAndreas Gohr<?php
2*d6d97f60SAnna Dabrowska
3b36f8833SAndreas Gohr/**
4b36f8833SAndreas Gohr * DokuWiki Plugin struct (Helper Component)
5b36f8833SAndreas Gohr *
6b36f8833SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
7b36f8833SAndreas Gohr * @author  Andreas Gohr, Michael Große <dokuwiki@cosmocode.de>
8b36f8833SAndreas Gohr */
9b36f8833SAndreas Gohr
10b36f8833SAndreas Gohr// must be run within Dokuwiki
11935a9fa3SMichael Großeuse dokuwiki\plugin\struct\meta\ConfigParser;
12935a9fa3SMichael Großeuse dokuwiki\plugin\struct\meta\SearchConfig;
13ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\StructException;
14b36f8833SAndreas Gohr
15b36f8833SAndreas Gohrif (!defined('DOKU_INC')) die();
16b36f8833SAndreas Gohr
17b36f8833SAndreas Gohr
18*d6d97f60SAnna Dabrowskaclass remote_plugin_struct extends DokuWiki_Remote_Plugin
19*d6d97f60SAnna Dabrowska{
20b36f8833SAndreas Gohr    /** @var helper_plugin_struct hlp */
21b36f8833SAndreas Gohr    protected $hlp;
22b36f8833SAndreas Gohr
23b36f8833SAndreas Gohr    /**
24b36f8833SAndreas Gohr     * remote_plugin_struct constructor.
25b36f8833SAndreas Gohr     */
26*d6d97f60SAnna Dabrowska    public function __construct()
27*d6d97f60SAnna Dabrowska    {
28b36f8833SAndreas Gohr        parent::__construct();
29b36f8833SAndreas Gohr
30b36f8833SAndreas Gohr        /** @var helper_plugin_struct hlp */
31b36f8833SAndreas Gohr        $this->hlp = plugin_load('helper', 'struct');
32b36f8833SAndreas Gohr    }
33b36f8833SAndreas Gohr
34b36f8833SAndreas Gohr    /**
35b36f8833SAndreas Gohr     * Get the structured data of a given page
36b36f8833SAndreas Gohr     *
37b36f8833SAndreas Gohr     * @param string $page The page to get data for
38b36f8833SAndreas Gohr     * @param string $schema The schema to use empty for all
39b36f8833SAndreas Gohr     * @param int $time A timestamp if you want historic data (0 for now)
40b36f8833SAndreas Gohr     * @return array ('schema' => ( 'fieldlabel' => 'value', ...))
41b36f8833SAndreas Gohr     * @throws RemoteAccessDeniedException
42b36f8833SAndreas Gohr     * @throws RemoteException
43b36f8833SAndreas Gohr     */
44*d6d97f60SAnna Dabrowska    public function getData($page, $schema, $time)
45*d6d97f60SAnna Dabrowska    {
46b36f8833SAndreas Gohr        $page = cleanID($page);
47b36f8833SAndreas Gohr
4899d15ae8SEnno Lohmeier        if (auth_quickaclcheck($page) < AUTH_READ) {
49b36f8833SAndreas Gohr            throw new RemoteAccessDeniedException('no permissions to access data of that page');
50b36f8833SAndreas Gohr        }
51b36f8833SAndreas Gohr
52b36f8833SAndreas Gohr        if (!$schema) $schema = null;
53b36f8833SAndreas Gohr
54b36f8833SAndreas Gohr        try {
55b36f8833SAndreas Gohr            return $this->hlp->getData($page, $schema, $time);
56b36f8833SAndreas Gohr        } catch (StructException $e) {
57b36f8833SAndreas Gohr            throw new RemoteException($e->getMessage(), 0, $e);
58b36f8833SAndreas Gohr        }
59b36f8833SAndreas Gohr    }
60b36f8833SAndreas Gohr
61b36f8833SAndreas Gohr
62b36f8833SAndreas Gohr    /**
63b36f8833SAndreas Gohr     * Saves data for a given page (creates a new revision)
64b36f8833SAndreas Gohr     *
65b36f8833SAndreas Gohr     * If this call succeeds you can assume your data has either been saved or it was
66b36f8833SAndreas Gohr     * not necessary to save it because the data already existed in the wanted form or
67b36f8833SAndreas Gohr     * the given schemas are no longer assigned to that page.
68b36f8833SAndreas Gohr     *
69b36f8833SAndreas Gohr     * @param string $page
70b36f8833SAndreas Gohr     * @param array $data ('schema' => ( 'fieldlabel' => 'value', ...))
71b36f8833SAndreas Gohr     * @param string $summary
72178d3992SElan Ruusamäe     * @param bool $minor
73b36f8833SAndreas Gohr     * @return bool returns always true
74b36f8833SAndreas Gohr     * @throws RemoteAccessDeniedException
75b36f8833SAndreas Gohr     * @throws RemoteException
76b36f8833SAndreas Gohr     */
77*d6d97f60SAnna Dabrowska    public function saveData($page, $data, $summary, $minor = false)
78*d6d97f60SAnna Dabrowska    {
79b36f8833SAndreas Gohr        $page = cleanID($page);
80b36f8833SAndreas Gohr
8199d15ae8SEnno Lohmeier        if (auth_quickaclcheck($page) < AUTH_EDIT) {
82b36f8833SAndreas Gohr            throw new RemoteAccessDeniedException('no permissions to save data for that page');
83b36f8833SAndreas Gohr        }
84b36f8833SAndreas Gohr
85b36f8833SAndreas Gohr        try {
86178d3992SElan Ruusamäe            $this->hlp->saveData($page, $data, $summary, $minor);
87b36f8833SAndreas Gohr            return true;
88b36f8833SAndreas Gohr        } catch (StructException $e) {
89b36f8833SAndreas Gohr            throw new RemoteException($e->getMessage(), 0, $e);
90b36f8833SAndreas Gohr        }
91b36f8833SAndreas Gohr    }
92b36f8833SAndreas Gohr
93b36f8833SAndreas Gohr    /**
94b36f8833SAndreas Gohr     * Get info about existing schemas columns
95b36f8833SAndreas Gohr     *
96b36f8833SAndreas Gohr     * Returns only current, enabled columns
97b36f8833SAndreas Gohr     *
98b36f8833SAndreas Gohr     * @param string $schema the schema to query, empty for all
99b36f8833SAndreas Gohr     * @return array
100b36f8833SAndreas Gohr     * @throws RemoteAccessDeniedException
101b36f8833SAndreas Gohr     * @throws RemoteException
102b36f8833SAndreas Gohr     */
103*d6d97f60SAnna Dabrowska    public function getSchema($schema = null)
104*d6d97f60SAnna Dabrowska    {
105b36f8833SAndreas Gohr        if (!auth_ismanager()) {
106b36f8833SAndreas Gohr            throw new RemoteAccessDeniedException('you need to be manager to access schema info');
107b36f8833SAndreas Gohr        }
108b36f8833SAndreas Gohr
109b36f8833SAndreas Gohr        try {
110b36f8833SAndreas Gohr            $result = array();
1111f07a6f8SElan Ruusamäe            $schemas = $this->hlp->getSchema($schema ?: null);
112b36f8833SAndreas Gohr            foreach ($schemas as $name => $schema) {
113b36f8833SAndreas Gohr                $result[$name] = array();
114b36f8833SAndreas Gohr                foreach ($schema->getColumns(false) as $column) {
115b36f8833SAndreas Gohr                    $result[$name][] = array(
116b36f8833SAndreas Gohr                        'name' => $column->getLabel(),
117b36f8833SAndreas Gohr                        'type' =>  array_pop(explode('\\', get_class($column->getType()))),
118b36f8833SAndreas Gohr                        'ismulti' => $column->isMulti()
119b36f8833SAndreas Gohr                    );
120b36f8833SAndreas Gohr                }
121b36f8833SAndreas Gohr            }
122b36f8833SAndreas Gohr            return $result;
123b36f8833SAndreas Gohr        } catch (StructException $e) {
124b36f8833SAndreas Gohr            throw new RemoteException($e->getMessage(), 0, $e);
125b36f8833SAndreas Gohr        }
126b36f8833SAndreas Gohr    }
127935a9fa3SMichael Große
128935a9fa3SMichael Große    /**
129935a9fa3SMichael Große     * Get the data that would be shown in an aggregation
130935a9fa3SMichael Große     *
131935a9fa3SMichael Große     * @param array  $schemas array of strings with the schema-names
132935a9fa3SMichael Große     * @param array  $cols array of strings with the columns
133935a9fa3SMichael Große     * @param array  $filter array of arrays with ['logic'=> 'and'|'or', 'condition' => 'your condition']
134935a9fa3SMichael Große     * @param string $sort string indicating the column to sort by
135935a9fa3SMichael Große     *
136935a9fa3SMichael Große     * @return array array of rows, each row is an array of the column values
137935a9fa3SMichael Große     * @throws RemoteException
138935a9fa3SMichael Große     */
139*d6d97f60SAnna Dabrowska    public function getAggregationData(array $schemas, array $cols, array $filter = [], $sort = '')
140*d6d97f60SAnna Dabrowska    {
141935a9fa3SMichael Große        $schemaLine = 'schema: ' . implode(', ', $schemas);
142935a9fa3SMichael Große        $columnLine = 'cols: ' . implode(', ', $cols);
143935a9fa3SMichael Große        $filterLines = array_map(function ($filter) {
144935a9fa3SMichael Große            return 'filter' . $filter['logic'] . ': ' . $filter['condition'];
145935a9fa3SMichael Große        }, $filter);
146935a9fa3SMichael Große        $sortLine = 'sort: ' . $sort;
147935a9fa3SMichael Große        // schemas, cols, REV?, filter, order
148935a9fa3SMichael Große
149935a9fa3SMichael Große        try {
150935a9fa3SMichael Große            $parser = new ConfigParser(array_merge([$schemaLine, $columnLine, $sortLine], $filterLines));
151935a9fa3SMichael Große            $config = $parser->getConfig();
152935a9fa3SMichael Große            $search = new SearchConfig($config);
153935a9fa3SMichael Große            $results = $search->execute();
154935a9fa3SMichael Große            $data = [];
155935a9fa3SMichael Große            /** @var \dokuwiki\plugin\struct\meta\Value[] $rowValues */
156935a9fa3SMichael Große            foreach ($results as $rowValues) {
157935a9fa3SMichael Große                $row = [];
158935a9fa3SMichael Große                foreach ($rowValues as $value) {
159935a9fa3SMichael Große                    $row[$value->getColumn()->getFullQualifiedLabel()] = $value->getDisplayValue();
160935a9fa3SMichael Große                }
161935a9fa3SMichael Große                $data[] = $row;
162935a9fa3SMichael Große            }
163935a9fa3SMichael Große            return $data;
164935a9fa3SMichael Große        } catch (StructException $e) {
165935a9fa3SMichael Große            throw new RemoteException($e->getMessage(), 0, $e);
166935a9fa3SMichael Große        }
167935a9fa3SMichael Große    }
168b36f8833SAndreas Gohr}
169