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