1b36f8833SAndreas Gohr<?php 2d6d97f60SAnna 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 115e29103aSanndause dokuwiki\Remote\RemoteException; 127234bfb1Ssplitbrainuse dokuwiki\Extension\RemotePlugin; 137234bfb1Ssplitbrainuse dokuwiki\Remote\AccessDeniedException; 147234bfb1Ssplitbrainuse dokuwiki\plugin\struct\meta\Value; 15935a9fa3SMichael Großeuse dokuwiki\plugin\struct\meta\ConfigParser; 16935a9fa3SMichael Großeuse dokuwiki\plugin\struct\meta\SearchConfig; 17ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\StructException; 18b36f8833SAndreas Gohr 197234bfb1Ssplitbrainclass remote_plugin_struct extends RemotePlugin 20d6d97f60SAnna Dabrowska{ 21b36f8833SAndreas Gohr /** @var helper_plugin_struct hlp */ 22b36f8833SAndreas Gohr protected $hlp; 23b36f8833SAndreas Gohr 24b36f8833SAndreas Gohr /** 25b36f8833SAndreas Gohr * remote_plugin_struct constructor. 26b36f8833SAndreas Gohr */ 27d6d97f60SAnna Dabrowska public function __construct() 28d6d97f60SAnna Dabrowska { 29b36f8833SAndreas Gohr parent::__construct(); 30b36f8833SAndreas Gohr 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', ...)) 415e29103aSannda * @throws AccessDeniedException 42b36f8833SAndreas Gohr * @throws RemoteException 43b36f8833SAndreas Gohr */ 44d6d97f60SAnna Dabrowska public function getData($page, $schema, $time) 45d6d97f60SAnna Dabrowska { 46b36f8833SAndreas Gohr $page = cleanID($page); 47b36f8833SAndreas Gohr 4899d15ae8SEnno Lohmeier if (auth_quickaclcheck($page) < AUTH_READ) { 497234bfb1Ssplitbrain throw new AccessDeniedException('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) { 575e29103aSannda 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 745e29103aSannda * @throws AccessDeniedException 75b36f8833SAndreas Gohr * @throws RemoteException 76b36f8833SAndreas Gohr */ 77d6d97f60SAnna Dabrowska public function saveData($page, $data, $summary, $minor = false) 78d6d97f60SAnna Dabrowska { 79b36f8833SAndreas Gohr $page = cleanID($page); 80b36f8833SAndreas Gohr 8199d15ae8SEnno Lohmeier if (auth_quickaclcheck($page) < AUTH_EDIT) { 827234bfb1Ssplitbrain throw new AccessDeniedException('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) { 895e29103aSannda 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 1005e29103aSannda * @throws AccessDeniedException 101b36f8833SAndreas Gohr * @throws RemoteException 102b36f8833SAndreas Gohr */ 103d6d97f60SAnna Dabrowska public function getSchema($schema = null) 104d6d97f60SAnna Dabrowska { 105b36f8833SAndreas Gohr if (!auth_ismanager()) { 1067234bfb1Ssplitbrain throw new AccessDeniedException('you need to be manager to access schema info'); 107b36f8833SAndreas Gohr } 108b36f8833SAndreas Gohr 109b36f8833SAndreas Gohr try { 1107234bfb1Ssplitbrain $result = []; 1116c9d1a10SAnna Dabrowska $schemas = $this->hlp::getSchema($schema ?: null); 112b36f8833SAndreas Gohr foreach ($schemas as $name => $schema) { 1137234bfb1Ssplitbrain $result[$name] = []; 114b36f8833SAndreas Gohr foreach ($schema->getColumns(false) as $column) { 1157fe2cdf2SAndreas Gohr $class = explode('\\', get_class($column->getType())); 1167fe2cdf2SAndreas Gohr $class = array_pop($class); 1177fe2cdf2SAndreas Gohr $result[$name][] = [ 1187fe2cdf2SAndreas Gohr 'name' => $column->getLabel(), 1197fe2cdf2SAndreas Gohr 'type' => $class, 1207fe2cdf2SAndreas Gohr 'ismulti' => $column->isMulti()]; 121b36f8833SAndreas Gohr } 122b36f8833SAndreas Gohr } 123b36f8833SAndreas Gohr return $result; 124b36f8833SAndreas Gohr } catch (StructException $e) { 1255e29103aSannda throw new RemoteException($e->getMessage(), 0, $e); 126b36f8833SAndreas Gohr } 127b36f8833SAndreas Gohr } 128935a9fa3SMichael Große 129935a9fa3SMichael Große /** 130935a9fa3SMichael Große * Get the data that would be shown in an aggregation 131935a9fa3SMichael Große * 132935a9fa3SMichael Große * @param array $schemas array of strings with the schema-names 133935a9fa3SMichael Große * @param array $cols array of strings with the columns 134935a9fa3SMichael Große * @param array $filter array of arrays with ['logic'=> 'and'|'or', 'condition' => 'your condition'] 135935a9fa3SMichael Große * @param string $sort string indicating the column to sort by 136935a9fa3SMichael Große * 137935a9fa3SMichael Große * @return array array of rows, each row is an array of the column values 138935a9fa3SMichael Große * @throws RemoteException 139935a9fa3SMichael Große */ 140d6d97f60SAnna Dabrowska public function getAggregationData(array $schemas, array $cols, array $filter = [], $sort = '') 141d6d97f60SAnna Dabrowska { 142935a9fa3SMichael Große $schemaLine = 'schema: ' . implode(', ', $schemas); 143935a9fa3SMichael Große $columnLine = 'cols: ' . implode(', ', $cols); 144b8d3247dSAnna Dabrowska $filterLines = array_map( 145b8d3247dSAnna Dabrowska static fn($filter) => 'filter' . $filter['logic'] . ': ' . $filter['condition'], 146b8d3247dSAnna Dabrowska $filter 147b8d3247dSAnna Dabrowska ); 148935a9fa3SMichael Große $sortLine = 'sort: ' . $sort; 149935a9fa3SMichael Große // schemas, cols, REV?, filter, order 150935a9fa3SMichael Große 151935a9fa3SMichael Große try { 152935a9fa3SMichael Große $parser = new ConfigParser(array_merge([$schemaLine, $columnLine, $sortLine], $filterLines)); 153935a9fa3SMichael Große $config = $parser->getConfig(); 154935a9fa3SMichael Große $search = new SearchConfig($config); 155*ba7f5789SAnna Dabrowska $results = $search->getRows(); 156935a9fa3SMichael Große $data = []; 1577234bfb1Ssplitbrain /** @var Value[] $rowValues */ 158935a9fa3SMichael Große foreach ($results as $rowValues) { 159935a9fa3SMichael Große $row = []; 160935a9fa3SMichael Große foreach ($rowValues as $value) { 161935a9fa3SMichael Große $row[$value->getColumn()->getFullQualifiedLabel()] = $value->getDisplayValue(); 162935a9fa3SMichael Große } 163935a9fa3SMichael Große $data[] = $row; 164935a9fa3SMichael Große } 165935a9fa3SMichael Große return $data; 166935a9fa3SMichael Große } catch (StructException $e) { 1675e29103aSannda throw new RemoteException($e->getMessage(), 0, $e); 168935a9fa3SMichael Große } 169935a9fa3SMichael Große } 170b36f8833SAndreas Gohr} 171