1549a0837SAndreas Gohr<?php 261356325SAnna Dabrowska 3549a0837SAndreas Gohr/** 4549a0837SAndreas Gohr * DokuWiki Plugin struct (Syntax Component) 5549a0837SAndreas Gohr * 6549a0837SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 7549a0837SAndreas Gohr * @author Andreas Gohr, Michael Große <dokuwiki@cosmocode.de> 8549a0837SAndreas Gohr */ 9549a0837SAndreas Gohr 10*d90aa848SAndreas Gohruse dokuwiki\plugin\struct\meta\Aggregation; 11ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\AggregationTable; 12ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\ConfigParser; 13ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\SearchConfig; 14ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\StructException; 1515929be2SAndreas Gohr 1661356325SAnna Dabrowskaclass syntax_plugin_struct_table extends DokuWiki_Syntax_Plugin 1761356325SAnna Dabrowska{ 183f2a8309SAndreas Gohr /** @var string which class to use for output */ 193f2a8309SAndreas Gohr protected $tableclass = AggregationTable::class; 203f2a8309SAndreas Gohr 21*d90aa848SAndreas Gohr /** @var string Config options that are not allowed for this syntax mode */ 22*d90aa848SAndreas Gohr protected $illegalOptions = ['nesting']; 23*d90aa848SAndreas Gohr 24549a0837SAndreas Gohr /** 25549a0837SAndreas Gohr * @return string Syntax mode type 26549a0837SAndreas Gohr */ 2761356325SAnna Dabrowska public function getType() 2861356325SAnna Dabrowska { 2915929be2SAndreas Gohr return 'substition'; 30549a0837SAndreas Gohr } 313f2a8309SAndreas Gohr 32549a0837SAndreas Gohr /** 33549a0837SAndreas Gohr * @return string Paragraph type 34549a0837SAndreas Gohr */ 3561356325SAnna Dabrowska public function getPType() 3661356325SAnna Dabrowska { 3715929be2SAndreas Gohr return 'block'; 38549a0837SAndreas Gohr } 393f2a8309SAndreas Gohr 40549a0837SAndreas Gohr /** 41549a0837SAndreas Gohr * @return int Sort order - Low numbers go before high numbers 42549a0837SAndreas Gohr */ 4361356325SAnna Dabrowska public function getSort() 4461356325SAnna Dabrowska { 455511bd5bSAndreas Gohr return 155; 46549a0837SAndreas Gohr } 47549a0837SAndreas Gohr 48549a0837SAndreas Gohr /** 49549a0837SAndreas Gohr * Connect lookup pattern to lexer. 50549a0837SAndreas Gohr * 51549a0837SAndreas Gohr * @param string $mode Parser mode 52549a0837SAndreas Gohr */ 5361356325SAnna Dabrowska public function connectTo($mode) 5461356325SAnna Dabrowska { 555511bd5bSAndreas Gohr $this->Lexer->addSpecialPattern('----+ *struct table *-+\n.*?\n----+', $mode, 'plugin_struct_table'); 56549a0837SAndreas Gohr } 57549a0837SAndreas Gohr 58549a0837SAndreas Gohr /** 59549a0837SAndreas Gohr * Handle matches of the struct syntax 60549a0837SAndreas Gohr * 61549a0837SAndreas Gohr * @param string $match The match of the syntax 62549a0837SAndreas Gohr * @param int $state The state of the handler 63549a0837SAndreas Gohr * @param int $pos The position in the document 64549a0837SAndreas Gohr * @param Doku_Handler $handler The handler 65549a0837SAndreas Gohr * @return array Data for the renderer 66549a0837SAndreas Gohr */ 6761356325SAnna Dabrowska public function handle($match, $state, $pos, Doku_Handler $handler) 6861356325SAnna Dabrowska { 69bd363da9SAndreas Gohr global $conf; 70549a0837SAndreas Gohr 715511bd5bSAndreas Gohr $lines = explode("\n", $match); 725511bd5bSAndreas Gohr array_shift($lines); 735511bd5bSAndreas Gohr array_pop($lines); 745511bd5bSAndreas Gohr 755511bd5bSAndreas Gohr try { 765511bd5bSAndreas Gohr $parser = new ConfigParser($lines); 770659dc64SMichael Grosse $config = $parser->getConfig(); 78*d90aa848SAndreas Gohr $this->checkForInvalidOptions($config); 790659dc64SMichael Grosse return $config; 805511bd5bSAndreas Gohr } catch (StructException $e) { 815511bd5bSAndreas Gohr msg($e->getMessage(), -1, $e->getLine(), $e->getFile()); 82bd363da9SAndreas Gohr if ($conf['allowdebug']) msg('<pre>' . hsc($e->getTraceAsString()) . '</pre>', -1); 835511bd5bSAndreas Gohr return null; 845511bd5bSAndreas Gohr } 85549a0837SAndreas Gohr } 86549a0837SAndreas Gohr 87549a0837SAndreas Gohr /** 88549a0837SAndreas Gohr * Render xhtml output or metadata 89549a0837SAndreas Gohr * 9034ea6e10SAnna Dabrowska * @param string $format Renderer mode (supported modes: xhtml) 91549a0837SAndreas Gohr * @param Doku_Renderer $renderer The renderer 9230bf61e6SAndreas Gohr * @param array $config The parsed config data from the handler() function 93549a0837SAndreas Gohr * @return bool If rendering was successful. 94549a0837SAndreas Gohr */ 9530bf61e6SAndreas Gohr public function render($format, Doku_Renderer $renderer, $config) 9661356325SAnna Dabrowska { 9706fee43aSMichael Grosse global $INFO; 98bd363da9SAndreas Gohr global $conf; 9929877279SMichael Große 10030bf61e6SAndreas Gohr if (!$config) return false; 10130bf61e6SAndreas Gohr $config = $this->addTypeFilter($config); // add type specific filters 10230bf61e6SAndreas Gohr 10325ed0c0dSAndreas Gohr // always use the main page's ID @todo might make sense as utility method somewhere 10425ed0c0dSAndreas Gohr if ($INFO !== null) { 10525ed0c0dSAndreas Gohr $mainId = $INFO['id']; 10625ed0c0dSAndreas Gohr } else { 10725ed0c0dSAndreas Gohr $mainId = getID(); 10825ed0c0dSAndreas Gohr } 10925ed0c0dSAndreas Gohr 11015929be2SAndreas Gohr try { 11130bf61e6SAndreas Gohr $search = new SearchConfig($config); 11234ea6e10SAnna Dabrowska if ($format === 'struct_csv') { 1136ce83f43SAndreas Gohr // no pagination in export 1146ce83f43SAndreas Gohr $search->setLimit(0); 1156ce83f43SAndreas Gohr $search->setOffset(0); 1166ce83f43SAndreas Gohr } 1176ce83f43SAndreas Gohr 118*d90aa848SAndreas Gohr /** @var Aggregation $table */ 11925ed0c0dSAndreas Gohr $table = new $this->tableclass($mainId, $format, $renderer, $search); 120*d90aa848SAndreas Gohr $table->render(true); 12116b7d914SAndreas Gohr 12234ea6e10SAnna Dabrowska if ($format === 'metadata') { 12316b7d914SAndreas Gohr /** @var Doku_Renderer_metadata $renderer */ 12416b7d914SAndreas Gohr $renderer->meta['plugin']['struct']['hasaggregation'] = $search->getCacheFlag(); 12516b7d914SAndreas Gohr } 1265511bd5bSAndreas Gohr } catch (StructException $e) { 12715929be2SAndreas Gohr msg($e->getMessage(), -1, $e->getLine(), $e->getFile()); 128bd363da9SAndreas Gohr if ($conf['allowdebug']) msg('<pre>' . hsc($e->getTraceAsString()) . '</pre>', -1); 12915929be2SAndreas Gohr } 13015929be2SAndreas Gohr 131549a0837SAndreas Gohr return true; 132549a0837SAndreas Gohr } 1330ceefd5cSAnna Dabrowska 1340ceefd5cSAnna Dabrowska /** 135c797933dSAnna Dabrowska * Filter based on primary key columns, applicable in child classes 1360ceefd5cSAnna Dabrowska * 1370ceefd5cSAnna Dabrowska * @param array $config 1380ceefd5cSAnna Dabrowska * @return array 1390ceefd5cSAnna Dabrowska */ 1400ceefd5cSAnna Dabrowska protected function addTypeFilter($config) 1410ceefd5cSAnna Dabrowska { 1420ceefd5cSAnna Dabrowska return $config; 1430ceefd5cSAnna Dabrowska } 144*d90aa848SAndreas Gohr 145*d90aa848SAndreas Gohr /** 146*d90aa848SAndreas Gohr * Checks for options that do not work in this aggregation 147*d90aa848SAndreas Gohr * 148*d90aa848SAndreas Gohr * @param array $config 149*d90aa848SAndreas Gohr */ 150*d90aa848SAndreas Gohr protected function checkForInvalidOptions($config) 151*d90aa848SAndreas Gohr { 152*d90aa848SAndreas Gohr foreach ($this->illegalOptions as $illegalOption) { 153*d90aa848SAndreas Gohr if (!empty($config[$illegalOption])) { 154*d90aa848SAndreas Gohr throw new StructException('illegal option', $illegalOption); 155*d90aa848SAndreas Gohr } 156*d90aa848SAndreas Gohr } 157*d90aa848SAndreas Gohr } 158549a0837SAndreas Gohr} 159