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 $ins = -1; 49 $pos = 0; 50 foreach ($event->data->calls as $num => $call) { 51 // try to find the first header 52 if ($call[0] == 'header') { 53 $pos = $call[2]; 54 $ins = $num; 55 break; 56 } 57 58 // abort when after we looked at the first 150 bytes 59 if (isset($call[3]) && $call[3] > 150) { 60 break; 61 } 62 } 63 64 // insert our own call after the found position 65 array_splice( 66 $event->data->calls, 67 $ins + 1, 68 0, 69 array( 70 array( 71 'plugin', 72 array( 73 'struct_output', array('pos' => $pos), DOKU_LEXER_SPECIAL, '' 74 ), 75 $pos 76 ) 77 ) 78 ); 79 } 80 81 /** 82 * If the page has a schema assigned, add its struct data 83 * to dw2pdf's template replacements 84 * 85 * @param Doku_Event $event 86 * @param $param 87 */ 88 public function replaceDw2pdf(Doku_Event $event, $param) 89 { 90 if (!$event->data['id'] || !page_exists($event->data['id'])) return; 91 92 global $REV; 93 $rev = $REV ?: time(); 94 95 /** @var helper_plugin_struct $helper */ 96 $helper = plugin_load('helper', 'struct'); 97 $data = $helper->getData($event->data['id'], null, $rev); 98 99 if (!$data) return; 100 101 foreach ($data as $schema => $fields) { 102 foreach ($fields as $field => $value) { 103 $placeholder = sprintf('@%s_%s_%s@', self::DW2PDF_PLACEHOLDER_PREFIX, $schema, $field); 104 $event->data['replace'][$placeholder] = is_array($value) ? implode(', ', $value) : $value; 105 } 106 } 107 } 108 109 /** 110 * Remove struct placeholders still present after replacement. 111 * Requested data was not found. 112 * 113 * @param Doku_Event $event 114 * @param $param 115 */ 116 public function cleanupDw2pdf(Doku_Event $event, $param) 117 { 118 $pattern = '~@' . self::DW2PDF_PLACEHOLDER_PREFIX . '_[^@]+?@~'; 119 $event->data['content'] = preg_replace($pattern, '', $event->data['content']); 120 } 121} 122 123// vim:ts=4:sw=4:et: 124