1549a0837SAndreas Gohr<?php 2549a0837SAndreas Gohr/** 3549a0837SAndreas Gohr * DokuWiki Plugin struct (Syntax Component) 4549a0837SAndreas Gohr * 5549a0837SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 6549a0837SAndreas Gohr * @author Andreas Gohr, Michael Große <dokuwiki@cosmocode.de> 7549a0837SAndreas Gohr */ 8549a0837SAndreas Gohr 9ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\AggregationTable; 10ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\ConfigParser; 11ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\SearchConfig; 12ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\StructException; 1315929be2SAndreas Gohr 1407993756SAndreas Gohr// must be run within Dokuwiki 15549a0837SAndreas Gohrif(!defined('DOKU_INC')) die(); 16549a0837SAndreas Gohr 17549a0837SAndreas Gohrclass syntax_plugin_struct_table extends DokuWiki_Syntax_Plugin { 183f2a8309SAndreas Gohr 193f2a8309SAndreas Gohr /** @var string which class to use for output */ 203f2a8309SAndreas Gohr protected $tableclass = AggregationTable::class; 213f2a8309SAndreas Gohr 22549a0837SAndreas Gohr /** 23549a0837SAndreas Gohr * @return string Syntax mode type 24549a0837SAndreas Gohr */ 25549a0837SAndreas Gohr public function getType() { 2615929be2SAndreas Gohr return 'substition'; 27549a0837SAndreas Gohr } 283f2a8309SAndreas Gohr 29549a0837SAndreas Gohr /** 30549a0837SAndreas Gohr * @return string Paragraph type 31549a0837SAndreas Gohr */ 32549a0837SAndreas Gohr public function getPType() { 3315929be2SAndreas Gohr return 'block'; 34549a0837SAndreas Gohr } 353f2a8309SAndreas Gohr 36549a0837SAndreas Gohr /** 37549a0837SAndreas Gohr * @return int Sort order - Low numbers go before high numbers 38549a0837SAndreas Gohr */ 39549a0837SAndreas Gohr public function getSort() { 405511bd5bSAndreas Gohr return 155; 41549a0837SAndreas Gohr } 42549a0837SAndreas Gohr 43549a0837SAndreas Gohr /** 44549a0837SAndreas Gohr * Connect lookup pattern to lexer. 45549a0837SAndreas Gohr * 46549a0837SAndreas Gohr * @param string $mode Parser mode 47549a0837SAndreas Gohr */ 48549a0837SAndreas Gohr public function connectTo($mode) { 495511bd5bSAndreas Gohr $this->Lexer->addSpecialPattern('----+ *struct table *-+\n.*?\n----+', $mode, 'plugin_struct_table'); 50549a0837SAndreas Gohr } 51549a0837SAndreas Gohr 52549a0837SAndreas Gohr /** 53549a0837SAndreas Gohr * Handle matches of the struct syntax 54549a0837SAndreas Gohr * 55549a0837SAndreas Gohr * @param string $match The match of the syntax 56549a0837SAndreas Gohr * @param int $state The state of the handler 57549a0837SAndreas Gohr * @param int $pos The position in the document 58549a0837SAndreas Gohr * @param Doku_Handler $handler The handler 59549a0837SAndreas Gohr * @return array Data for the renderer 60549a0837SAndreas Gohr */ 61ab466032SAndreas Gohr public function handle($match, $state, $pos, Doku_Handler $handler) { 62bd363da9SAndreas Gohr global $conf; 63549a0837SAndreas Gohr 645511bd5bSAndreas Gohr $lines = explode("\n", $match); 655511bd5bSAndreas Gohr array_shift($lines); 665511bd5bSAndreas Gohr array_pop($lines); 675511bd5bSAndreas Gohr 685511bd5bSAndreas Gohr try { 695511bd5bSAndreas Gohr $parser = new ConfigParser($lines); 700659dc64SMichael Grosse $config = $parser->getConfig(); 71*0ceefd5cSAnna Dabrowska 72*0ceefd5cSAnna Dabrowska $config = $this->addTypeFilter($config); 73*0ceefd5cSAnna Dabrowska 740659dc64SMichael Grosse return $config; 755511bd5bSAndreas Gohr } catch(StructException $e) { 765511bd5bSAndreas Gohr msg($e->getMessage(), -1, $e->getLine(), $e->getFile()); 77bd363da9SAndreas Gohr if($conf['allowdebug']) msg('<pre>' . hsc($e->getTraceAsString()) . '</pre>', -1); 785511bd5bSAndreas Gohr return null; 795511bd5bSAndreas Gohr } 80549a0837SAndreas Gohr } 81549a0837SAndreas Gohr 82549a0837SAndreas Gohr /** 83549a0837SAndreas Gohr * Render xhtml output or metadata 84549a0837SAndreas Gohr * 85549a0837SAndreas Gohr * @param string $mode Renderer mode (supported modes: xhtml) 86549a0837SAndreas Gohr * @param Doku_Renderer $renderer The renderer 87549a0837SAndreas Gohr * @param array $data The data from the handler() function 88549a0837SAndreas Gohr * @return bool If rendering was successful. 89549a0837SAndreas Gohr */ 90ab466032SAndreas Gohr public function render($mode, Doku_Renderer $renderer, $data) { 915511bd5bSAndreas Gohr if(!$data) return false; 9206fee43aSMichael Grosse global $INFO; 93bd363da9SAndreas Gohr global $conf; 9429877279SMichael Große 9515929be2SAndreas Gohr try { 965511bd5bSAndreas Gohr $search = new SearchConfig($data); 976ce83f43SAndreas Gohr if($mode == 'struct_csv') { 986ce83f43SAndreas Gohr // no pagination in export 996ce83f43SAndreas Gohr $search->setLimit(0); 1006ce83f43SAndreas Gohr $search->setOffset(0); 1016ce83f43SAndreas Gohr } 1026ce83f43SAndreas Gohr 1033f2a8309SAndreas Gohr /** @var AggregationTable $table */ 10406fee43aSMichael Grosse $table = new $this->tableclass($INFO['id'], $mode, $renderer, $search); 10507993756SAndreas Gohr $table->render(); 10616b7d914SAndreas Gohr 10716b7d914SAndreas Gohr if($mode == 'metadata') { 10816b7d914SAndreas Gohr /** @var Doku_Renderer_metadata $renderer */ 10916b7d914SAndreas Gohr $renderer->meta['plugin']['struct']['hasaggregation'] = $search->getCacheFlag(); 11016b7d914SAndreas Gohr } 11116b7d914SAndreas Gohr 1125511bd5bSAndreas Gohr } catch(StructException $e) { 11315929be2SAndreas Gohr msg($e->getMessage(), -1, $e->getLine(), $e->getFile()); 114bd363da9SAndreas Gohr if($conf['allowdebug']) msg('<pre>' . hsc($e->getTraceAsString()) . '</pre>', -1); 11515929be2SAndreas Gohr } 11615929be2SAndreas Gohr 117549a0837SAndreas Gohr return true; 118549a0837SAndreas Gohr } 119*0ceefd5cSAnna Dabrowska 120*0ceefd5cSAnna Dabrowska /** 121*0ceefd5cSAnna Dabrowska * Filter based on primary key columns 122*0ceefd5cSAnna Dabrowska * 123*0ceefd5cSAnna Dabrowska * @param array $config 124*0ceefd5cSAnna Dabrowska * @return array 125*0ceefd5cSAnna Dabrowska */ 126*0ceefd5cSAnna Dabrowska protected function addTypeFilter($config) 127*0ceefd5cSAnna Dabrowska { 128*0ceefd5cSAnna Dabrowska $config['filter'][] = ['%rowid%', '=', (string)\dokuwiki\plugin\struct\meta\AccessTableData::DEFAULT_PAGE_RID, 'AND']; 129*0ceefd5cSAnna Dabrowska return $config; 130*0ceefd5cSAnna Dabrowska } 131549a0837SAndreas Gohr} 132