1<?php 2/** 3 * DokuWiki Plugin struct (Syntax 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 9use dokuwiki\plugin\struct\meta\AggregationTable; 10use dokuwiki\plugin\struct\meta\ConfigParser; 11use dokuwiki\plugin\struct\meta\SearchConfig; 12use dokuwiki\plugin\struct\meta\StructException; 13 14// must be run within Dokuwiki 15if(!defined('DOKU_INC')) die(); 16 17class syntax_plugin_struct_table extends DokuWiki_Syntax_Plugin { 18 19 /** @var string which class to use for output */ 20 protected $tableclass = AggregationTable::class; 21 22 /** 23 * @return string Syntax mode type 24 */ 25 public function getType() { 26 return 'substition'; 27 } 28 29 /** 30 * @return string Paragraph type 31 */ 32 public function getPType() { 33 return 'block'; 34 } 35 36 /** 37 * @return int Sort order - Low numbers go before high numbers 38 */ 39 public function getSort() { 40 return 155; 41 } 42 43 /** 44 * Connect lookup pattern to lexer. 45 * 46 * @param string $mode Parser mode 47 */ 48 public function connectTo($mode) { 49 $this->Lexer->addSpecialPattern('----+ *struct table *-+\n.*?\n----+', $mode, 'plugin_struct_table'); 50 } 51 52 /** 53 * Handle matches of the struct syntax 54 * 55 * @param string $match The match of the syntax 56 * @param int $state The state of the handler 57 * @param int $pos The position in the document 58 * @param Doku_Handler $handler The handler 59 * @return array Data for the renderer 60 */ 61 public function handle($match, $state, $pos, Doku_Handler $handler) { 62 global $conf; 63 64 $lines = explode("\n", $match); 65 array_shift($lines); 66 array_pop($lines); 67 68 try { 69 $parser = new ConfigParser($lines); 70 $config = $parser->getConfig(); 71 72 $config = $this->addTypeFilter($config); 73 74 return $config; 75 } catch(StructException $e) { 76 msg($e->getMessage(), -1, $e->getLine(), $e->getFile()); 77 if($conf['allowdebug']) msg('<pre>' . hsc($e->getTraceAsString()) . '</pre>', -1); 78 return null; 79 } 80 } 81 82 /** 83 * Render xhtml output or metadata 84 * 85 * @param string $mode Renderer mode (supported modes: xhtml) 86 * @param Doku_Renderer $renderer The renderer 87 * @param array $data The data from the handler() function 88 * @return bool If rendering was successful. 89 */ 90 public function render($mode, Doku_Renderer $renderer, $data) { 91 if(!$data) return false; 92 global $INFO; 93 global $conf; 94 95 try { 96 $search = new SearchConfig($data); 97 if($mode == 'struct_csv') { 98 // no pagination in export 99 $search->setLimit(0); 100 $search->setOffset(0); 101 } 102 103 /** @var AggregationTable $table */ 104 $table = new $this->tableclass($INFO['id'], $mode, $renderer, $search); 105 $table->render(); 106 107 if($mode == 'metadata') { 108 /** @var Doku_Renderer_metadata $renderer */ 109 $renderer->meta['plugin']['struct']['hasaggregation'] = $search->getCacheFlag(); 110 } 111 112 } catch(StructException $e) { 113 msg($e->getMessage(), -1, $e->getLine(), $e->getFile()); 114 if($conf['allowdebug']) msg('<pre>' . hsc($e->getTraceAsString()) . '</pre>', -1); 115 } 116 117 return true; 118 } 119 120 /** 121 * Filter based on primary key columns 122 * 123 * @param array $config 124 * @return array 125 */ 126 protected function addTypeFilter($config) 127 { 128 $config['filter'][] = ['%rowid%', '=', (string)\dokuwiki\plugin\struct\meta\AccessTableData::DEFAULT_PAGE_RID, 'AND']; 129 return $config; 130 } 131} 132