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