1<?php 2 3/** 4 * DokuWiki Plugin struct (Action 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/** 11 * Class action_plugin_struct_output 12 * 13 * This action component handles the automatic output of all schema data that has been assigned 14 * to the current page by appending the appropriate instruction to the handler calls. 15 * 16 * The real output creation is done within the syntax component 17 * @see syntax_plugin_struct_output 18 */ 19class action_plugin_struct_output extends DokuWiki_Action_Plugin 20{ 21 22 const DW2PDF_PLACEHOLDER_PREFIX = 'PLUGIN_STRUCT'; 23 24 /** 25 * Registers a callback function for a given event 26 * 27 * @param Doku_Event_Handler $controller DokuWiki's event controller object 28 * @return void 29 */ 30 public function register(Doku_Event_Handler $controller) 31 { 32 $controller->register_hook('PARSER_HANDLER_DONE', 'AFTER', $this, 'handleOutput'); 33 $controller->register_hook('PLUGIN_DW2PDF_REPLACE', 'BEFORE', $this, 'replaceDw2pdf'); 34 $controller->register_hook('PLUGIN_DW2PDF_REPLACE', 'AFTER', $this, 'cleanupDw2pdf'); 35 } 36 37 /** 38 * Appends the instruction to render our syntax output component to each page 39 * after the first found headline or the very begining if no headline was found 40 * 41 * @param Doku_Event $event 42 * @param $param 43 */ 44 public function handleOutput(Doku_Event $event, $param) 45 { 46 global $ID; 47 if (!page_exists($ID)) return; 48 49 $pos = 0; 50 // display struct data at the bottom? 51 if ($this->getConf('bottomoutput')) { 52 $ins = count($event->data->calls); 53 } else { 54 $ins = -1; 55 foreach ($event->data->calls as $num => $call) { 56 // try to find the first header 57 if ($call[0] == 'header') { 58 $pos = $call[2]; 59 $ins = $num; 60 break; 61 } 62 63 // abort when after we looked at the first 150 bytes 64 if (isset($call[3]) && $call[3] > 150) { 65 break; 66 } 67 } 68 } 69 70 // insert our own call after the found position 71 array_splice( 72 $event->data->calls, 73 $ins + 1, 74 0, 75 array( 76 array( 77 'plugin', 78 array( 79 'struct_output', array('pos' => $pos), DOKU_LEXER_SPECIAL, '' 80 ), 81 $pos 82 ) 83 ) 84 ); 85 } 86 87 /** 88 * If the page has a schema assigned, add its struct data 89 * to dw2pdf's template replacements 90 * 91 * @param Doku_Event $event 92 * @param $param 93 */ 94 public function replaceDw2pdf(Doku_Event $event, $param) 95 { 96 if (!$event->data['id'] || !page_exists($event->data['id'])) return; 97 98 global $REV; 99 $rev = $REV ?: time(); 100 101 /** @var helper_plugin_struct $helper */ 102 $helper = plugin_load('helper', 'struct'); 103 $data = $helper->getData($event->data['id'], null, $rev); 104 105 if (!$data) return; 106 107 foreach ($data as $schema => $fields) { 108 foreach ($fields as $field => $value) { 109 $placeholder = sprintf('@%s_%s_%s@', self::DW2PDF_PLACEHOLDER_PREFIX, $schema, $field); 110 $event->data['replace'][$placeholder] = is_array($value) ? implode(', ', $value) : $value; 111 } 112 } 113 } 114 115 /** 116 * Remove struct placeholders still present after replacement. 117 * Requested data was not found. 118 * 119 * @param Doku_Event $event 120 * @param $param 121 */ 122 public function cleanupDw2pdf(Doku_Event $event, $param) 123 { 124 $pattern = '~@' . self::DW2PDF_PLACEHOLDER_PREFIX . '_[^@]+?@~'; 125 $event->data['content'] = preg_replace($pattern, '', $event->data['content']); 126 } 127} 128 129// vim:ts=4:sw=4:et: 130