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