diff --git a/GenericResolver.php b/GenericResolver.php index d02d69c..edbfae3 100644 --- a/GenericResolver.php +++ b/GenericResolver.php @@ -1,5 +1,7 @@ helper->get_flags(array()); - $pages = $this->helper->_get_included_pages('namespace', 'inclhidden:', '', '', $flags); + $flags = $this->helper->getFlags(array()); + $pages = $this->helper->getIncludedPages('namespace', 'inclhidden:', '', '', $flags); $this->assertEquals(array( array('id' => 'inclhidden:visible', 'exists' => true, 'parent_id' => ''), ), $pages); @@ -76,25 +76,25 @@ public function test_hidden() { * Test include depth limit */ public function test_depth() { - $flags = $this->helper->get_flags(array()); - $pages = $this->helper->_get_included_pages('namespace', 'incltest:', '', '', $flags); + $flags = $this->helper->getFlags(array()); + $pages = $this->helper->getIncludedPages('namespace', 'incltest:', '', '', $flags); $this->assertEquals(array( array('id' => 'incltest:level1', 'exists' => true, 'parent_id' => ''), ), $pages); - $flags = $this->helper->get_flags(array('depth=2')); - $pages = $this->helper->_get_included_pages('namespace', 'incltest:', '', '', $flags); + $flags = $this->helper->getFlags(array('depth=2')); + $pages = $this->helper->getIncludedPages('namespace', 'incltest:', '', '', $flags); $this->assertEquals(array( array('id' => 'incltest:level1', 'exists' => true, 'parent_id' => ''), array('id' => 'incltest:ns:level2', 'exists' => true, 'parent_id' => ''), ), $pages); - $flags = $this->helper->get_flags(array('depth=2')); - $pages = $this->helper->_get_included_pages('namespace', 'incltest:ns', '', '', $flags); + $flags = $this->helper->getFlags(array('depth=2')); + $pages = $this->helper->getIncludedPages('namespace', 'incltest:ns', '', '', $flags); $this->assertEquals(array( array('id' => 'incltest:ns:level2', 'exists' => true, 'parent_id' => ''), array('id' => 'incltest:ns:ns:level3', 'exists' => true, 'parent_id' => ''), ), $pages); - $flags = $this->helper->get_flags(array('depth=0')); - $pages = $this->helper->_get_included_pages('namespace', 'incltest:', '', '', $flags); + $flags = $this->helper->getFlags(array('depth=0')); + $pages = $this->helper->getIncludedPages('namespace', 'incltest:', '', '', $flags); $this->assertEquals(array( array('id' => 'incltest:level1', 'exists' => true, 'parent_id' => ''), array('id' => 'incltest:ns:level2', 'exists' => true, 'parent_id' => ''), @@ -102,11 +102,11 @@ public function test_depth() { ), $pages); // test include of the root namespace - $flags = $this->helper->get_flags(array()); - $pages = $this->helper->_get_included_pages('namespace', ':', '', '', $flags); + $flags = $this->helper->getFlags(array()); + $pages = $this->helper->getIncludedPages('namespace', ':', '', '', $flags); $this->assertEquals(array(array('id' => 'mailinglist', 'exists' => true, 'parent_id' => '')), $pages); - $flags = $this->helper->get_flags(array('depth=2')); - $pages = $this->helper->_get_included_pages('namespace', ':', '', '', $flags); + $flags = $this->helper->getFlags(array('depth=2')); + $pages = $this->helper->getIncludedPages('namespace', ':', '', '', $flags); $expected = array( array('id' => 'inclhidden:visible', 'exists' => true, 'parent_id' => ''), array('id' => 'inclorder:page1', 'exists' => true, 'parent_id' => ''), @@ -143,8 +143,8 @@ public function test_depth() { */ public function test_order() { - $flags = $this->helper->get_flags(array()); - $pages = $this->helper->_get_included_pages('namespace', 'inclorder:', '', '', $flags); + $flags = $this->helper->getFlags(array()); + $pages = $this->helper->getIncludedPages('namespace', 'inclorder:', '', '', $flags); $this->assertEquals(array( array('id' => 'inclorder:page1', 'exists' => true, 'parent_id' => ''), @@ -153,16 +153,16 @@ public function test_order() { array('id' => 'inclorder:page4', 'exists' => true, 'parent_id' => ''), ), $pages); - $flags = $this->helper->get_flags(array('rsort')); - $pages = $this->helper->_get_included_pages('namespace', 'inclorder:', '', '', $flags); + $flags = $this->helper->getFlags(array('rsort')); + $pages = $this->helper->getIncludedPages('namespace', 'inclorder:', '', '', $flags); $this->assertEquals(array( array('id' => 'inclorder:page4', 'exists' => true, 'parent_id' => ''), array('id' => 'inclorder:page3', 'exists' => true, 'parent_id' => ''), array('id' => 'inclorder:page2', 'exists' => true, 'parent_id' => ''), array('id' => 'inclorder:page1', 'exists' => true, 'parent_id' => ''), ), $pages); - $flags = $this->helper->get_flags(array('order=custom')); - $pages = $this->helper->_get_included_pages('namespace', 'inclorder:', '', '', $flags); + $flags = $this->helper->getFlags(array('order=custom')); + $pages = $this->helper->getIncludedPages('namespace', 'inclorder:', '', '', $flags); $this->assertEquals(array( array('id' => 'inclorder:page4', 'exists' => true, 'parent_id' => ''), array('id' => 'inclorder:page3', 'exists' => true, 'parent_id' => ''), @@ -170,8 +170,8 @@ public function test_order() { array('id' => 'inclorder:page2', 'exists' => true, 'parent_id' => ''), ), $pages); - $flags = $this->helper->get_flags(array('order=custom', 'rsort')); - $pages = $this->helper->_get_included_pages('namespace', 'inclorder:', '', '', $flags); + $flags = $this->helper->getFlags(array('order=custom', 'rsort')); + $pages = $this->helper->getIncludedPages('namespace', 'inclorder:', '', '', $flags); $this->assertEquals(array( array('id' => 'inclorder:page2', 'exists' => true, 'parent_id' => ''), array('id' => 'inclorder:page1', 'exists' => true, 'parent_id' => ''), diff --git a/action.php b/action.php index b5388ba..2db5690 100644 --- a/action.php +++ b/action.php @@ -1,4 +1,11 @@ * @author Michael Klier */ - -/** - * All DokuWiki plugins to extend the parser/rendering mechanism - * need to inherit from this class - */ -class action_plugin_include extends DokuWiki_Action_Plugin { - +class action_plugin_include extends ActionPlugin +{ /* @var helper_plugin_include $helper */ - var $helper = null; + public $helper; - function __construct() { + /** + * Constructor + * + * Initializes the helper + */ + public function __construct() + { $this->helper = plugin_load('helper', 'include'); } - /** - * plugin should use this method to register its handlers with the dokuwiki's event controller - */ - function register(Doku_Event_Handler $controller) { + /** @inheritdoc */ + public function register(EventHandler $controller) + { /* @var Doku_event_handler $controller */ - $controller->register_hook('INDEXER_PAGE_ADD', 'BEFORE', $this, 'handle_indexer'); - $controller->register_hook('INDEXER_VERSION_GET', 'BEFORE', $this, 'handle_indexer_version'); - $controller->register_hook('PARSER_CACHE_USE','BEFORE', $this, '_cache_prepare'); - $controller->register_hook('HTML_EDITFORM_OUTPUT', 'BEFORE', $this, 'handle_form'); // todo remove - $controller->register_hook('FORM_EDIT_OUTPUT', 'BEFORE', $this, 'handle_form'); - $controller->register_hook('HTML_CONFLICTFORM_OUTPUT', 'BEFORE', $this, 'handle_form'); // todo remove - $controller->register_hook('FORM_CONFLICT_OUTPUT', 'BEFORE', $this, 'handle_form'); - $controller->register_hook('HTML_DRAFTFORM_OUTPUT', 'BEFORE', $this, 'handle_form'); // todo remove - $controller->register_hook('FORM_DRAFT_OUTPUT', 'BEFORE', $this, 'handle_form'); - $controller->register_hook('ACTION_SHOW_REDIRECT', 'BEFORE', $this, 'handle_redirect'); - $controller->register_hook('PARSER_HANDLER_DONE', 'BEFORE', $this, 'handle_parser'); - $controller->register_hook('PARSER_METADATA_RENDER', 'AFTER', $this, 'handle_metadata'); - $controller->register_hook('HTML_SECEDIT_BUTTON', 'BEFORE', $this, 'handle_secedit_button'); - $controller->register_hook('PLUGIN_MOVE_HANDLERS_REGISTER', 'BEFORE', $this, 'handle_move_register'); + $controller->register_hook('INDEXER_PAGE_ADD', 'BEFORE', $this, 'handleIndexer'); + $controller->register_hook('INDEXER_VERSION_GET', 'BEFORE', $this, 'handleIndexerVersion'); + $controller->register_hook('PARSER_CACHE_USE', 'BEFORE', $this, 'handleCachePrepare'); + $controller->register_hook('FORM_EDIT_OUTPUT', 'BEFORE', $this, 'handleForm'); + $controller->register_hook('FORM_CONFLICT_OUTPUT', 'BEFORE', $this, 'handleForm'); + $controller->register_hook('FORM_DRAFT_OUTPUT', 'BEFORE', $this, 'handleForm'); + $controller->register_hook('ACTION_SHOW_REDIRECT', 'BEFORE', $this, 'handleRedirect'); + $controller->register_hook('PARSER_HANDLER_DONE', 'BEFORE', $this, 'handleParser'); + $controller->register_hook('PARSER_METADATA_RENDER', 'AFTER', $this, 'handleMetadata'); + $controller->register_hook('HTML_SECEDIT_BUTTON', 'BEFORE', $this, 'handleSeceditButton'); + $controller->register_hook('PLUGIN_MOVE_HANDLERS_REGISTER', 'BEFORE', $this, 'handleMoveRegister'); } /** * Add a version string to the index so it is rebuilt * whenever the handler is updated or the safeindex setting is changed */ - public function handle_indexer_version($event, $param) { - $event->data['plugin_include'] = '0.1.safeindex='.$this->getConf('safeindex'); + public function handleIndexerVersion(Event $event, $param) + { + $event->data['plugin_include'] = '0.1.safeindex=' . $this->getConf('safeindex'); } /** - * Handles the INDEXER_PAGE_ADD event, prevents indexing of metadata from included pages that aren't public if enabled + * Handles the INDEXER_PAGE_ADD event + * + * prevents indexing of metadata from included pages that aren't public if enabled * - * @param Doku_Event $event the event object - * @param array $params optional parameters (unused) + * @param Event $event the event object + * @param array $params optional parameters (unused) */ - public function handle_indexer(Doku_Event $event, $params) { + public function handleIndexer(Event $event, $params) + { global $USERINFO; // check if the feature is enabled at all @@ -72,7 +80,7 @@ public function handle_indexer(Doku_Event $event, $params) { // check if the current metadata indicates that non-public pages were included if ($inclmeta !== null && isset($inclmeta['pages'])) { foreach ($inclmeta['pages'] as $page) { - if (auth_aclcheck($page['id'], '', array()) < AUTH_READ) { // is $page public? + if (auth_aclcheck($page['id'], '', []) < AUTH_READ) { // is $page public? $all_public = false; break; } @@ -90,31 +98,32 @@ public function handle_indexer(Doku_Event $event, $params) { // metadata is only rendered once for a page in one request - thus we need to render manually. $meta = p_read_metadata($event->data['page']); // load the original metdata $meta = p_render_metadata($event->data['page'], $meta); // render the metadata - p_save_metadata($event->data['page'], $meta); // save the metadata so other event handlers get the public metadata, too + p_save_metadata($event->data['page'], $meta); // save the metadata so other event handlers get it, too $meta = $meta['current']; // we are only interested in current metadata. // check if the tag plugin handler has already been called before the include plugin $tag_called = isset($event->data['metadata']['subject']); - // Reset the metadata in the renderer. This removes data from all other event handlers, but we need to be on the safe side here. - $event->data['metadata'] = array('title' => $meta['title']); + // Reset the metadata in the renderer. + // This removes data from all other event handlers, but we need to be on the safe side here. + $event->data['metadata'] = ['title' => $meta['title']]; // restore the relation references metadata if (isset($meta['relation']['references'])) { $event->data['metadata']['relation_references'] = array_keys($meta['relation']['references']); } else { - $event->data['metadata']['relation_references'] = array(); + $event->data['metadata']['relation_references'] = []; } // restore the tag metadata if the tag plugin handler has been called before the include plugin handler. if ($tag_called) { $tag_helper = $this->loadHelper('tag', false); if ($tag_helper) { - if (isset($meta['subject'])) { + if (isset($meta['subject'])) { $event->data['metadata']['subject'] = $tag_helper->_cleanTagList($meta['subject']); } else { - $event->data['metadata']['subject'] = array(); + $event->data['metadata']['subject'] = []; } } } @@ -128,9 +137,10 @@ public function handle_indexer(Doku_Event $event, $params) { /** * Used for debugging purposes only */ - function handle_metadata(&$event, $param) { + public function handleMetadata(Event $event, $param) + { global $conf; - if($conf['allowdebug'] && $this->getConf('debugoutput')) { + if ($conf['allowdebug'] && $this->getConf('debugoutput')) { dbglog('---- PLUGIN INCLUDE META DATA START ----'); dbglog($event->data); dbglog('---- PLUGIN INCLUDE META DATA END ----'); @@ -143,31 +153,23 @@ function handle_metadata(&$event, $param) { * @author Michael Klier * @author Michael Hamann */ - function handle_parser(Doku_Event $event, $param) { + public function handleParser(Event $event, $param) + { global $ID; $level = 0; $ins =& $event->data->calls; $num = count($ins); - for($i=0; $i<$num; $i++) { - switch($ins[$i][0]) { - case 'plugin': - switch($ins[$i][1][0]) { - case 'include_include': - $ins[$i][1][1][4] = $level; + for ($i = 0; $i < $num; $i++) { + switch ($ins[$i][0]) { + case 'plugin': + if ($ins[$i][1][0] === 'include_include') { + $ins[$i][1][1][4] = $level; + } break; - /* FIXME: this doesn't work anymore that way with the new structure - // some plugins already close open sections - // so we need to make sure we don't close them twice - case 'box': - $this->helper->sec_close = false; + case 'section_open': + $level = $ins[$i][1][0]; break; - */ - } - break; - case 'section_open': - $level = $ins[$i][1][0]; - break; } } } @@ -175,11 +177,11 @@ function handle_parser(Doku_Event $event, $param) { /** * Add a hidden input to the form to preserve the redirect_id */ - function handle_form(Doku_Event $event, $param) + public function handleForm(Event $event, $param) { if (!array_key_exists('redirect_id', $_REQUEST)) return; - if(is_a($event->data, \dokuwiki\Form\Form::class)) { + if (is_a($event->data, Form::class)) { $event->data->setHiddenField('redirect_id', cleanID($_REQUEST['redirect_id'])); } else { // todo remove when old FORM events are no longer supported @@ -190,62 +192,57 @@ function handle_form(Doku_Event $event, $param) /** * Modify the data for the redirect when there is a redirect_id set */ - function handle_redirect(Doku_Event &$event, $param) { - if (array_key_exists('redirect_id', $_REQUEST)) { - // Render metadata when this is an older DokuWiki version where - // metadata is not automatically re-rendered as the page has probably - // been changed but is not directly displayed - $versionData = getVersionData(); - if ($versionData['date'] < '2010-11-23') { - p_set_metadata($event->data['id'], array(), true); + public function handleRedirect(Event $event, $param) + { + if (array_key_exists('redirect_id', $_REQUEST)) { + // Render metadata when this is an older DokuWiki version where + // metadata is not automatically re-rendered as the page has probably + // been changed but is not directly displayed + $versionData = getVersionData(); + if ($versionData['date'] < '2010-11-23') { + p_set_metadata($event->data['id'], [], true); + } + $event->data['id'] = cleanID($_REQUEST['redirect_id']); + $event->data['title'] = ''; } - $event->data['id'] = cleanID($_REQUEST['redirect_id']); - $event->data['title'] = ''; - } } /** * prepare the cache object for default _useCache action */ - function _cache_prepare(Doku_Event &$event, $param) { - global $conf; - + public function handleCachePrepare(Event $event, $param) + { /* @var cache_renderer $cache */ $cache =& $event->data; - if(!isset($cache->page)) return; - if(!isset($cache->mode) || $cache->mode == 'i') return; + if (!isset($cache->page)) return; + if (!isset($cache->mode) || $cache->mode == 'i') return; $depends = p_get_metadata($cache->page, 'plugin_include'); - if($conf['allowdebug'] && $this->getConf('debugoutput')) { - dbglog('---- PLUGIN INCLUDE CACHE DEPENDS START ----'); - dbglog($depends); - dbglog('---- PLUGIN INCLUDE CACHE DEPENDS END ----'); + if ($this->getConf('debugoutput')) { + Logger::debug('include plugin: cache depends for ' . $cache->page, $depends); } if (!is_array($depends)) return; // nothing to do for us - if (!is_array($depends['pages']) || + if ( + !is_array($depends['pages']) || !is_array($depends['instructions']) || - $depends['pages'] != $this->helper->_get_included_pages_from_meta_instructions($depends['instructions']) || + $depends['pages'] != $this->helper->getIncludedPagesFromMetaInstructions($depends['instructions']) || // the include_content url parameter may change the behavior for included pages - $depends['include_content'] != isset($_REQUEST['include_content'])) { - + $depends['include_content'] != isset($_REQUEST['include_content']) + ) { $cache->depends['purge'] = true; // included pages changed or old metadata - request purge. - if($conf['allowdebug'] && $this->getConf('debugoutput')) { - dbglog('---- PLUGIN INCLUDE: REQUESTING CACHE PURGE ----'); - dbglog('---- PLUGIN INCLUDE CACHE PAGES FROM META START ----'); - dbglog($depends['pages']); - dbglog('---- PLUGIN INCLUDE CACHE PAGES FROM META END ----'); - dbglog('---- PLUGIN INCLUDE CACHE PAGES FROM META_INSTRUCTIONS START ----'); - dbglog($this->helper->_get_included_pages_from_meta_instructions($depends['instructions'])); - dbglog('---- PLUGIN INCLUDE CACHE PAGES FROM META_INSTRUCTIONS END ----'); - + if ($this->getConf('debugoutput')) { + Logger::debug('include plugin: cache purge for ' . $cache->page, [ + 'meta-pages' => $depends['pages'], + 'inst-pages' => $this->helper->getIncludedPagesFromMetaInstructions($depends['instructions']), + ]); } } else { // add plugin.info.txt to depends for nicer upgrades - $cache->depends['files'][] = dirname(__FILE__) . '/plugin.info.txt'; + $cache->depends['files'][] = __DIR__ . '/plugin.info.txt'; foreach ($depends['pages'] as $page) { if (!$page['exists']) continue; $file = wikiFN($page['id']); @@ -261,9 +258,10 @@ function _cache_prepare(Doku_Event &$event, $param) { * and replace normal section edit buttons when the current page is different from the * global $ID. */ - function handle_secedit_button(Doku_Event &$event, $params) { + public function handleSeceditButton(Event $event, $params) + { // stack of included pages in the form ('id' => page, 'rev' => modification time, 'writable' => bool) - static $page_stack = array(); + static $page_stack = []; global $ID, $lang; @@ -273,31 +271,39 @@ function handle_secedit_button(Doku_Event &$event, $params) { // handle the "section edits" added by the include plugin $fn = wikiFN($data['name']); $perm = auth_quickaclcheck($data['name']); - array_unshift($page_stack, array( - 'id' => $data['name'], - 'rev' => @filemtime($fn), - 'writable' => (page_exists($data['name']) ? (is_writable($fn) && $perm >= AUTH_EDIT) : $perm >= AUTH_CREATE), - 'redirect' => ($data['target'] == 'plugin_include_start'), - )); + array_unshift( + $page_stack, + [ + 'id' => $data['name'], + 'rev' => @filemtime($fn), + 'writable' => + (page_exists($data['name']) + ? (is_writable($fn) && $perm >= AUTH_EDIT) + : $perm >= AUTH_CREATE), + 'redirect' => ($data['target'] == 'plugin_include_start')] + ); } elseif ($data['target'] == 'plugin_include_end') { array_shift($page_stack); } elseif ($data['target'] == 'plugin_include_editbtn') { if ($page_stack[0]['writable']) { - $params = array('do' => 'edit', - 'id' => $page_stack[0]['id']); + $params = ['do' => 'edit', 'id' => $page_stack[0]['id']]; if ($page_stack[0]['redirect']) { $params['redirect_id'] = $ID; $params['hid'] = $data['hid']; } $event->result = '
' . DOKU_LF . - html_btn('incledit', $page_stack[0]['id'], '', - $params, 'post', + html_btn( + 'incledit', + $page_stack[0]['id'], + '', + $params, + 'post', $data['name'], - $lang['btn_secedit'].' ('.$page_stack[0]['id'].')') . + $lang['btn_secedit'] . ' (' . $page_stack[0]['id'] . ')' + ) . '
' . DOKU_LF; } } elseif (!empty($page_stack)) { - // Special handling for the edittable plugin if ($data['target'] == 'table' && !plugin_isdisabled('edittable')) { /* @var action_plugin_edittable_editor $edittable */ @@ -319,11 +325,17 @@ function handle_secedit_button(Doku_Event &$event, $params) { $event->result = "
" . - html_btn('secedit', $page_stack[0]['id'], '', - array_merge(array('do' => 'edit', - 'rev' => $page_stack[0]['rev'], - 'summary' => '['.$name.'] '), $data), - 'post', $name) . '
'; + html_btn( + 'secedit', + $page_stack[0]['id'], + '', + array_merge( + ['do' => 'edit', 'rev' => $page_stack[0]['rev'], 'summary' => '[' . $name . '] '], + $data + ), + 'post', + $name + ) . ''; } else { $event->result = ''; } @@ -335,36 +347,33 @@ function handle_secedit_button(Doku_Event &$event, $params) { $event->stopPropagation(); } - public function handle_move_register(Doku_Event $event, $params) { - $event->data['handlers']['include_include'] = array($this, 'rewrite_include'); + public function handleMoveRegister(Event $event, $params) + { + $event->data['handlers']['include_include'] = [$this, 'rewriteInclude']; } - public function rewrite_include($match, $pos, $state, $plugin, helper_plugin_move_handler $handler) { + public function rewriteInclude($match, $pos, $state, $plugin, helper_plugin_move_handler $handler) + { $syntax = substr($match, 2, -2); // strip markup $replacers = explode('|', $syntax); $syntax = array_shift($replacers); - list($syntax, $flags) = array_pad(explode('&', $syntax, 2), 2, ""); + [$syntax, $flags] = array_pad(explode('&', $syntax, 2), 2, ""); // break the pattern up into its parts - list($mode, $page, $sect) = array_pad(preg_split('/>|#/u', $syntax, 3), 3, ""); + [$mode, $page, $sect] = array_pad(preg_split('/>|#/u', $syntax, 3), 3, ""); - if (method_exists($handler, 'adaptRelativeId')) { // move plugin before version 2015-05-16 - $newpage = $handler->adaptRelativeId($page); - } else { - $newpage = $handler->resolveMoves($page, 'page'); - $newpage = $handler->relativeLink($page, $newpage, 'page'); - } + $newpage = $handler->resolveMoves($page, 'page'); + $newpage = $handler->relativeLink($page, $newpage, 'page'); if ($newpage == $page) { return $match; } else { - $result = '{{'.$mode.'>'.$newpage; - if ($sect) $result .= '#'.$sect; - if ($flags) $result .= '&'.$flags; - if ($replacers) $result .= '|'.$replacers; + $result = '{{' . $mode . '>' . $newpage; + if ($sect) $result .= '#' . $sect; + if ($flags) $result .= '&' . $flags; + if ($replacers) $result .= '|' . $replacers; $result .= '}}'; return $result; } } } -// vim:ts=4:sw=4:et: diff --git a/conf/default.php b/conf/default.php index c619005..734ed03 100644 --- a/conf/default.php +++ b/conf/default.php @@ -1,7 +1,9 @@ */ + $meta['noheader'] = array('onoff'); $meta['firstseconly'] = array('onoff'); $meta['showtaglogos'] = array('onoff'); @@ -25,7 +27,10 @@ $meta['pageexists'] = array('onoff'); $meta['parlink'] = array('onoff'); $meta['safeindex'] = array('onoff'); -$meta['order'] = array('multichoice', '_choices' => array('id', 'title', 'created', 'modified', 'indexmenu', 'custom')); +$meta['order'] = array( + 'multichoice', + '_choices' => array('id', 'title', 'created', 'modified', 'indexmenu', 'custom') +); $meta['rsort'] = array('onoff'); $meta['depth'] = array('numeric', '_min' => 0); $meta['readmore'] = array('onoff'); diff --git a/helper.php b/helper.php index 9dca93b..d0b027b 100644 --- a/helper.php +++ b/helper.php @@ -1,4 +1,5 @@ @@ -6,73 +7,120 @@ * @author Gina Häußge, Michael Klier * @author Michael Hamann */ + +use dokuwiki\Extension\Plugin; use dokuwiki\plugin\include\GenericResolver; use dokuwiki\File\PageResolver; /** * Helper functions for the include plugin and other plugins that want to include pages. */ -class helper_plugin_include extends DokuWiki_Plugin { // DokuWiki_Helper_Plugin +class helper_plugin_include extends Plugin +{ + // DokuWiki_Helper_Plugin - var $defaults = array(); - var $sec_close = true; + public $defaults = []; + public $sec_close = true; /** @var helper_plugin_tag $taghelper */ - var $taghelper = null; - var $includes = array(); // deprecated - compatibility code for the blog plugin + public $taghelper; + public $includes = []; // deprecated - compatibility code for the blog plugin /** * Constructor loads default config settings once */ - function __construct() { - $this->defaults['noheader'] = $this->getConf('noheader'); - $this->defaults['firstsec'] = $this->getConf('firstseconly'); - $this->defaults['editbtn'] = $this->getConf('showeditbtn'); - $this->defaults['taglogos'] = $this->getConf('showtaglogos'); - $this->defaults['footer'] = $this->getConf('showfooter'); - $this->defaults['redirect'] = $this->getConf('doredirect'); - $this->defaults['date'] = $this->getConf('showdate'); - $this->defaults['mdate'] = $this->getConf('showmdate'); - $this->defaults['user'] = $this->getConf('showuser'); - $this->defaults['comments'] = $this->getConf('showcomments'); + public function __construct() + { + $this->defaults['noheader'] = $this->getConf('noheader'); + $this->defaults['firstsec'] = $this->getConf('firstseconly'); + $this->defaults['editbtn'] = $this->getConf('showeditbtn'); + $this->defaults['taglogos'] = $this->getConf('showtaglogos'); + $this->defaults['footer'] = $this->getConf('showfooter'); + $this->defaults['redirect'] = $this->getConf('doredirect'); + $this->defaults['date'] = $this->getConf('showdate'); + $this->defaults['mdate'] = $this->getConf('showmdate'); + $this->defaults['user'] = $this->getConf('showuser'); + $this->defaults['comments'] = $this->getConf('showcomments'); $this->defaults['linkbacks'] = $this->getConf('showlinkbacks'); - $this->defaults['tags'] = $this->getConf('showtags'); - $this->defaults['link'] = $this->getConf('showlink'); + $this->defaults['tags'] = $this->getConf('showtags'); + $this->defaults['link'] = $this->getConf('showlink'); $this->defaults['permalink'] = $this->getConf('showpermalink'); - $this->defaults['indent'] = $this->getConf('doindent'); - $this->defaults['linkonly'] = $this->getConf('linkonly'); - $this->defaults['title'] = $this->getConf('title'); - $this->defaults['pageexists'] = $this->getConf('pageexists'); - $this->defaults['parlink'] = $this->getConf('parlink'); - $this->defaults['inline'] = false; - $this->defaults['order'] = $this->getConf('order'); - $this->defaults['rsort'] = $this->getConf('rsort'); - $this->defaults['depth'] = $this->getConf('depth'); - $this->defaults['readmore'] = $this->getConf('readmore'); + $this->defaults['indent'] = $this->getConf('doindent'); + $this->defaults['linkonly'] = $this->getConf('linkonly'); + $this->defaults['title'] = $this->getConf('title'); + $this->defaults['pageexists'] = $this->getConf('pageexists'); + $this->defaults['parlink'] = $this->getConf('parlink'); + $this->defaults['inline'] = false; + $this->defaults['order'] = $this->getConf('order'); + $this->defaults['rsort'] = $this->getConf('rsort'); + $this->defaults['depth'] = $this->getConf('depth'); + $this->defaults['readmore'] = $this->getConf('readmore'); } /** * Available methods for other plugins */ - function getMethods() { - $result = array(); - $result[] = array( - 'name' => 'get_flags', - 'desc' => 'overrides standard values for showfooter and firstseconly settings', - 'params' => array('flags' => 'array'), + public function getMethods() + { + return [ + [ + 'name' => 'getFlags', + 'desc' => 'overrides standard values for showfooter and firstseconly settings', + 'params' => ['flags' => 'array'] + ] + ]; + } + + /** + * Magic method to handle deprecated method calls + * + * @param string $func The name of the method being called + * @param array $args The arguments passed to the method + * @return mixed + */ + public function __call($func, $args) + { + switch ($func) { + case 'get_flags': + dbg_deprecated('getFlags()'); + return $this->getFlags($args[0]); + case '_get_instructions': + dbg_deprecated('getInstructions()'); + return $this->getInstructions( + $args[0], // page + $args[1], // sect + $args[2], // mode + $args[3], // lvl + $args[4], // flags + $args[5] ?? null, // root_id + $args[6] ?? [] // included_pages ); - return $result; + case '_get_included_pages': + dbg_deprecated('getIncludedPages()'); + return $this->getIncludedPages( + $args[0], // mode + $args[1], // page + $args[2], // sect + $args[3], // parent_id + $args[4] // flags + ); + default: + throw new \BadMethodCallException("Method $func does not exist in " . __CLASS__); + } } /** * Overrides standard values for showfooter and firstseconly settings */ - function get_flags($setflags) { + public function getFlags($setflags) + { + global $INPUT; + // load defaults $flags = $this->defaults; foreach ($setflags as $flag) { $value = ''; if (strpos($flag, '=') !== false) { - list($flag, $value) = explode('=', $flag, 2); + [$flag, $value] = explode('=', $flag, 2); } switch ($flag) { case 'footer': @@ -225,18 +273,17 @@ function get_flags($setflags) { } } // the include_content URL parameter overrides flags - if (isset($_REQUEST['include_content'])) + if ($INPUT->has('include_content')) { $flags['linkonly'] = 0; + } return $flags; } /** * Returns the converted instructions of a give page/section - * - * @author Michael Klier - * @author Michael Hamann */ - function _get_instructions($page, $sect, $mode, $lvl, $flags, $root_id = null, $included_pages = array()) { + public function getInstructions($page, $sect, $mode, $lvl, $flags, $root_id = null, $included_pages = []) + { $key = ($sect) ? $page . '#' . $sect : $page; $this->includes[$key] = true; // legacy code for keeping compatibility with other plugins @@ -247,34 +294,31 @@ function _get_instructions($page, $sect, $mode, $lvl, $flags, $root_id = null, $ } if ($flags['linkonly']) { - if (page_exists($page) || $flags['pageexists'] == 0) { + if (page_exists($page) || $flags['pageexists'] == 0) { $title = ''; if ($flags['title']) $title = p_get_first_heading($page); - if($flags['parlink']) { - $ins = array( - array('p_open', array()), - array('internallink', array(':'.$key, $title)), - array('p_close', array()), - ); + if ($flags['parlink']) { + $ins = [['p_open', []], ['internallink', [':' . $key, $title]], ['p_close', []]]; } else { - $ins = array(array('internallink', array(':'.$key,$title))); + $ins = [['internallink', [':' . $key, $title]]]; } - }else { - $ins = array(); + } else { + $ins = []; } } else { if (page_exists($page)) { global $ID; + // Change the global $ID as otherwise plugins like the discussion plugin will save data for wrong page $backupID = $ID; - $ID = $page; // Change the global $ID as otherwise plugins like the discussion plugin will save data for the wrong page + $ID = $page; $ins = p_cached_instructions(wikiFN($page), false, $page); $ID = $backupID; } else { - $ins = array(); + $ins = []; } - $this->_convert_instructions($ins, $lvl, $page, $sect, $flags, $root_id, $included_pages); + $this->convertInstructions($ins, $lvl, $page, $sect, $flags, $root_id, $included_pages); } return $ins; } @@ -282,41 +326,42 @@ function _get_instructions($page, $sect, $mode, $lvl, $flags, $root_id = null, $ /** * Converts instructions of the included page * - * The funcion iterates over the given list of instructions and generates + * The function iterates over the given list of instructions and generates * an index of header and section indicies. It also removes document * start/end instructions, converts links, and removes unwanted * instructions like tags, comments, linkbacks. * - * Later all header/section levels are convertet to match the current + * Later all header/section levels are converted to match the current * inclusion level. * * @author Michael Klier */ - function _convert_instructions(&$ins, $lvl, $page, $sect, $flags, $root_id, $included_pages = array()) { + protected function convertInstructions(&$ins, $lvl, $page, $sect, $flags, $root_id, $included_pages = []) + { global $conf; // filter instructions if needed - if(!empty($sect)) { - $this->_get_section($ins, $sect); // section required + if (!empty($sect)) { + $this->getSection($ins, $sect); // section required } - if($flags['firstsec']) { - $this->_get_firstsec($ins, $page, $flags); // only first section + if ($flags['firstsec']) { + $this->getFirstSection($ins, $page, $flags); // only first section } $num = count($ins); - $conv_idx = array(); // conversion index - $lvl_max = false; // max level + $conv_idx = []; // conversion index + $lvl_max = false; // max level $first_header = -1; - $no_header = false; + $no_header = false; $sect_title = false; - $endpos = null; // end position of the raw wiki text + $endpos = null; // end position of the raw wiki text - $this->adapt_links($ins, $page, $included_pages); + $this->adaptLinks($ins, $page, $included_pages); - for($i=0; $i<$num; $i++) { - switch($ins[$i][0]) { + for ($i = 0; $i < $num; $i++) { + switch ($ins[$i][0]) { case 'document_start': case 'document_end': case 'section_edit': @@ -324,38 +369,37 @@ function _convert_instructions(&$ins, $lvl, $page, $sect, $flags, $root_id, $inc break; case 'header': // get section title of first section - if($sect && !$sect_title) { + if ($sect && !$sect_title) { $sect_title = $ins[$i][1][0]; } // check if we need to skip the first header - if((!$no_header) && $flags['noheader']) { + if ((!$no_header) && $flags['noheader']) { $no_header = true; } $conv_idx[] = $i; // get index of first header - if($first_header == -1) $first_header = $i; + if ($first_header == -1) $first_header = $i; // get max level of this instructions set - if(!$lvl_max || ($ins[$i][1][1] < $lvl_max)) { + if (!$lvl_max || ($ins[$i][1][1] < $lvl_max)) { $lvl_max = $ins[$i][1][1]; } break; case 'section_open': if ($flags['inline']) unset($ins[$i]); - else - $conv_idx[] = $i; + else $conv_idx[] = $i; break; case 'section_close': if ($flags['inline']) unset($ins[$i]); break; case 'nest': - $this->adapt_links($ins[$i][1][0], $page, $included_pages); + $this->adaptLinks($ins[$i][1][0], $page, $included_pages); break; case 'plugin': // FIXME skip other plugins? - switch($ins[$i][1][0]) { + switch ($ins[$i][1][0]) { case 'tag_tag': // skip tags case 'discussion_comments': // skip comments case 'linkback': // skip linkbacks @@ -394,42 +438,43 @@ function _convert_instructions(&$ins, $lvl, $page, $sect, $flags, $root_id, $inc if ($no_header) $diff -= 1; // push up one level if "noheader" // convert headers and set footer/permalink - $hdr_deleted = false; - $has_permalink = false; - $footer_lvl = false; + $hdr_deleted = false; + $has_permalink = false; + $footer_lvl = false; $contains_secedit = false; $section_close_at = false; - foreach($conv_idx as $idx) { - if($ins[$idx][0] == 'header') { - if ($section_close_at === false && isset($ins[$idx+1]) && $ins[$idx+1][0] == 'section_open') { + foreach ($conv_idx as $idx) { + if ($ins[$idx][0] == 'header') { + if ($section_close_at === false && isset($ins[$idx + 1]) && $ins[$idx + 1][0] == 'section_open') { // store the index of the first heading that is followed by a new section - // the wrap plugin creates sections without section_open so the section shouldn't be closed before them + // the wrap plugin creates sections without section_open so the section + // shouldn't be closed before them $section_close_at = $idx; } - if($no_header && !$hdr_deleted) { - unset ($ins[$idx]); + if ($no_header && !$hdr_deleted) { + unset($ins[$idx]); $hdr_deleted = true; continue; } - if($flags['indent']) { + if ($flags['indent']) { $lvl_new = (($ins[$idx][1][1] + $diff) > 5) ? 5 : ($ins[$idx][1][1] + $diff); $ins[$idx][1][1] = $lvl_new; } - if($ins[$idx][1][1] <= $conf['maxseclevel']) + if ($ins[$idx][1][1] <= $conf['maxseclevel']) $contains_secedit = true; // set permalink - if($flags['link'] && !$has_permalink && ($idx == $first_header)) { - $this->_permalink($ins[$idx], $page, $sect, $flags); + if ($flags['link'] && !$has_permalink && ($idx == $first_header)) { + $ins[$idx] = $this->permalinkHeaderInstruction($ins[$idx], $page, $sect, $flags); $has_permalink = true; } // set footer level - if(!$footer_lvl && ($idx == $first_header) && !$no_header) { - if($flags['indent'] && isset($lvl_new)) { + if (!$footer_lvl && ($idx == $first_header) && !$no_header) { + if ($flags['indent'] && isset($lvl_new)) { $footer_lvl = $lvl_new; } else { $footer_lvl = $lvl_max; @@ -437,14 +482,14 @@ function _convert_instructions(&$ins, $lvl, $page, $sect, $flags, $root_id, $inc } } else { // it's a section - if($flags['indent']) { + if ($flags['indent']) { $lvl_new = (($ins[$idx][1][0] + $diff) > 5) ? 5 : ($ins[$idx][1][0] + $diff); $ins[$idx][1][0] = $lvl_new; } // check if noheader is used and set the footer level to the first section - if($no_header && !$footer_lvl) { - if($flags['indent'] && isset($lvl_new)) { + if ($no_header && !$footer_lvl) { + if ($flags['indent'] && isset($lvl_new)) { $footer_lvl = $lvl_new; } else { $footer_lvl = $lvl_max; @@ -455,107 +500,143 @@ function _convert_instructions(&$ins, $lvl, $page, $sect, $flags, $root_id, $inc // close last open section of the included page if there is any if ($contains_secedit) { - array_push($ins, array('plugin', array('include_closelastsecedit', array($endpos)))); + $ins[] = ['plugin', ['include_closelastsecedit', [$endpos]]]; } - $include_secid = (isset($flags['include_secid']) ? $flags['include_secid'] : NULL); + $include_secid = ($flags['include_secid'] ?? null); // add edit button - if($flags['editbtn']) { - $this->_editbtn($ins, $page, $sect, $sect_title, ($flags['redirect'] ? $root_id : false), $include_secid); + if ($flags['editbtn']) { + $ins[] = $this->editButtonInstruction( + $page, + $sect, + $sect_title, + ($flags['redirect'] ? $root_id : false), + $include_secid + ); } // add footer - if($flags['footer']) { - $ins[] = $this->_footer($page, $sect, $sect_title, $flags, $footer_lvl, $root_id); + if ($flags['footer']) { + $ins[] = $this->footerInstruction($page, $sect, $sect_title, $flags, $footer_lvl, $root_id); } // wrap content at the beginning of the include that is not in a section in a section if ($lvl > 0 && $section_close_at !== 0 && $flags['indent'] && !$flags['inline']) { if ($section_close_at === false) { - $ins[] = array('section_close', array()); - array_unshift($ins, array('section_open', array($lvl))); + $ins[] = ['section_close', []]; + array_unshift($ins, ['section_open', [$lvl]]); } else { $section_close_idx = array_search($section_close_at, array_keys($ins)); if ($section_close_idx > 0) { $before_ins = array_slice($ins, 0, $section_close_idx); $after_ins = array_slice($ins, $section_close_idx); - $ins = array_merge($before_ins, array(array('section_close', array())), $after_ins); - array_unshift($ins, array('section_open', array($lvl))); + $ins = array_merge($before_ins, [['section_close', []]], $after_ins); + array_unshift($ins, ['section_open', [$lvl]]); } } } // add instructions entry wrapper - array_unshift($ins, array('plugin', array('include_wrap', array('open', $page, $flags['redirect'], $include_secid)))); + array_unshift($ins, ['plugin', ['include_wrap', ['open', $page, $flags['redirect'], $include_secid]]]); if (isset($flags['beforeeach'])) - array_unshift($ins, array('entity', array($flags['beforeeach']))); - array_push($ins, array('plugin', array('include_wrap', array('close')))); + array_unshift($ins, ['entity', [$flags['beforeeach']]]); + $ins[] = ['plugin', ['include_wrap', ['close']]]; if (isset($flags['aftereach'])) - array_push($ins, array('entity', array($flags['aftereach']))); + $ins[] = ['entity', [$flags['aftereach']]]; // close previous section if any and re-open after inclusion - if($lvl != 0 && $this->sec_close && !$flags['inline']) { - array_unshift($ins, array('section_close', array())); - $ins[] = array('section_open', array($lvl)); + if ($lvl != 0 && $this->sec_close && !$flags['inline']) { + array_unshift($ins, ['section_close', []]); + $ins[] = ['section_open', [$lvl]]; } } /** - * Appends instruction item for the include plugin footer - * - * @author Michael Klier + * Returns instruction item for the include plugin footer */ - function _footer($page, $sect, $sect_title, $flags, $footer_lvl, $root_id) { - $footer = array(); - $footer[0] = 'plugin'; - $footer[1] = array('include_footer', array($page, $sect, $sect_title, $flags, $root_id, $footer_lvl)); - return $footer; + protected function footerInstruction($page, $sect, $sect_title, $flags, $footer_lvl, $root_id) + { + return [ + 0 => 'plugin', + 1 => [ + 'include_footer', + [ + $page, + $sect, + $sect_title, + $flags, + $root_id, + $footer_lvl + ] + ] + ]; } /** - * Appends instruction item for an edit button + * Returns instruction item for an edit button * * @author Michael Klier */ - function _editbtn(&$ins, $page, $sect, $sect_title, $root_id, $hid = '') { + public function editButtonInstruction($page, $sect, $sect_title, $root_id, $hid = '') + { $title = ($sect) ? $sect_title : $page; - $editbtn = array(); + $editbtn = []; $editbtn[0] = 'plugin'; - $editbtn[1] = array('include_editbtn', array($title, $hid)); - $ins[] = $editbtn; + $editbtn[1] = ['include_editbtn', [$title, $hid]]; + return $editbtn; } /** - * Convert instruction item for a permalink header + * Convert the given header into instruction item for a permalink header * * @author Michael Klier */ - function _permalink(&$ins, $page, $sect, $flags) { - $ins[0] = 'plugin'; - $ins[1] = array('include_header', array($ins[1][0], $ins[1][1], $ins[1][2], $page, $sect, $flags)); + public function permalinkHeaderInstruction($originalHeader, $page, $sect, $flags) + { + return [ + 0 => 'plugin', + 1 => [ + 'include_permalink', + [ + $originalHeader[1][0], + $originalHeader[1][1], + $originalHeader[1][2], + $page, + $sect, + $flags + ] + ] + ]; } /** * Convert internal and local links depending on the included pages * - * @param array $ins The instructions that shall be adapted - * @param string $page The included page - * @param array $included_pages The array of pages that are included + * @param array $ins The instructions that shall be adapted + * @param string $page The included page + * @param array $included_pages The array of pages that are included */ - private function adapt_links(&$ins, $page, $included_pages = null) { + private function adaptLinks(&$ins, $page, $included_pages = null) + { $num = count($ins); - for($i=0; $i<$num; $i++) { + for ($i = 0; $i < $num; $i++) { // adjust links with image titles - if (strpos($ins[$i][0], 'link') !== false && isset($ins[$i][1][1]) && is_array($ins[$i][1][1]) && $ins[$i][1][1]['type'] == 'internalmedia') { + if ( + strpos($ins[$i][0], 'link') !== false && + isset($ins[$i][1][1]) && + is_array($ins[$i][1][1]) && + $ins[$i][1][1]['type'] == 'internalmedia' + ) { // resolve relative ids, but without cleaning in order to preserve the name $media_id = (new GenericResolver($page))->resolveId($ins[$i][1][1]['src']); // make sure that after resolving the link again it will be the same link - if ($media_id[0] != ':') $media_id = ':'.$media_id; + if ($media_id[0] != ':') $media_id = ':' . $media_id; $ins[$i][1][1]['src'] = $media_id; } - switch($ins[$i][0]) { + + switch ($ins[$i][0]) { case 'internallink': case 'internalmedia': // make sure parameters aren't touched @@ -569,9 +650,9 @@ private function adapt_links(&$ins, $page, $included_pages = null) { // resolve the id without cleaning it $link_id = (new GenericResolver($page))->resolveId($link_id); // this id is internal (i.e. absolute) now, add ':' to make resolve_id work again - if ($link_id[0] != ':') $link_id = ':'.$link_id; + if ($link_id[0] != ':') $link_id = ':' . $link_id; // restore parameters - $ins[$i][1][0] = ($link_params != '') ? $link_id.'?'.$link_params : $link_id; + $ins[$i][1][0] = ($link_params != '') ? $link_id . '?' . $link_params : $link_id; if ($ins[$i][0] == 'internallink' && !empty($included_pages)) { // change links to included pages into local links @@ -584,18 +665,27 @@ private function adapt_links(&$ins, $page, $included_pages = null) { $link_parts = explode('#', $link_id, 2); $hash = ''; if (count($link_parts) === 2) { - list($link_id, $hash) = $link_parts; + [$link_id, $hash] = $link_parts; } if (array_key_exists($link_id, $included_pages)) { if ($hash) { - // hopefully the hash is also unique in the including page (otherwise this might be the wrong link target) + // hopefully the hash is also unique in the including page + // (otherwise this might be the wrong link target) $ins[$i][0] = 'locallink'; $ins[$i][1][0] = $hash; } else { - // the include section ids are different from normal section ids (so they won't conflict) but this + // the include section ids are different from normal section ids + // (so they won't conflict) but this // also means that the normal locallink function can't be used $ins[$i][0] = 'plugin'; - $ins[$i][1] = array('include_locallink', array($included_pages[$link_id]['hid'], $ins[$i][1][1], $ins[$i][1][0])); + $ins[$i][1] = [ + 'include_locallink', + [ + $included_pages[$link_id]['hid'], + $ins[$i][1][1], + $ins[$i][1][0] + ] + ]; } } } @@ -605,7 +695,7 @@ private function adapt_links(&$ins, $page, $included_pages = null) { /* Convert local links to internal links if the page hasn't been fully included */ if ($included_pages == null || !array_key_exists($page, $included_pages)) { $ins[$i][0] = 'internallink'; - $ins[$i][1][0] = ':'.$page.'#'.$ins[$i][1][0]; + $ins[$i][1][0] = ':' . $page . '#' . $ins[$i][1][0]; } break; } @@ -614,38 +704,37 @@ private function adapt_links(&$ins, $page, $included_pages = null) { /** * Get a section including its subsections - * - * @author Michael Klier */ - function _get_section(&$ins, $sect) { + protected function getSection(&$ins, $sect) + { $num = count($ins); $offset = false; - $lvl = false; - $end = false; + $lvl = false; + $end = false; $endpos = null; // end position in the input text, needed for section edit buttons - $check = array(); // used for sectionID() in order to get the same ids as the xhtml renderer + $check = []; // used for sectionID() in order to get the same ids as the xhtml renderer - for($i=0; $i<$num; $i++) { + for ($i = 0; $i < $num; $i++) { if ($ins[$i][0] == 'header') { - // found the right header if (sectionID($ins[$i][1][0], $check) == $sect) { $offset = $i; - $lvl = $ins[$i][1][1]; + $lvl = $ins[$i][1][1]; } elseif ($offset && $lvl && ($ins[$i][1][1] <= $lvl)) { $end = $i - $offset; - $endpos = $ins[$i][1][2]; // the position directly after the found section, needed for the section edit button + // the position directly after the found section, needed for the section edit button: + $endpos = $ins[$i][1][2]; break; } } } - $offset = $offset ? $offset : 0; - $end = $end ? $end : ($num - 1); - if(is_array($ins)) { + $offset = $offset ?: 0; + $end = $end ?: $num - 1; + if (is_array($ins)) { $ins = array_slice($ins, $offset, $end); // store the end position in the include_closelastsecedit instruction so it can generate a matching button - $ins[] = array('plugin', array('include_closelastsecedit', array($endpos))); + $ins[] = ['plugin', ['include_closelastsecedit', [$endpos]]]; } } @@ -654,12 +743,13 @@ function _get_section(&$ins, $sect) { * * @author Michael Klier */ - function _get_firstsec(&$ins, $page, $flags) { + protected function getFirstSection(&$ins, $page, $flags) + { $num = count($ins); $first_sect = false; $endpos = null; // end position in the input text - for($i=0; $i<$num; $i++) { - if($ins[$i][0] == 'section_close') { + for ($i = 0; $i < $num; $i++) { + if ($ins[$i][0] == 'section_close') { $first_sect = $i; } if ($ins[$i][0] == 'header') { @@ -673,14 +763,14 @@ function _get_firstsec(&$ins, $page, $flags) { } // only truncate the content and add the read more link when there is really // more than that first section - if(($first_sect) && ($ins[$i][0] == 'section_open')) { + if (($first_sect) && ($ins[$i][0] == 'section_open')) { $ins = array_slice($ins, 0, $first_sect); if ($flags['readmore']) { - $ins[] = array('plugin', array('include_readmore', array($page))); + $ins[] = ['plugin', ['include_readmore', [$page]]]; } - $ins[] = array('section_close', array()); - // store the end position in the include_closelastsecedit instruction so it can generate a matching button - $ins[] = array('plugin', array('include_closelastsecedit', array($endpos))); + $ins[] = ['section_close', []]; + // store end position in the include_closelastsecedit instruction so it can generate a matching button + $ins[] = ['plugin', ['include_closelastsecedit', [$endpos]]]; return; } } @@ -688,64 +778,63 @@ function _get_firstsec(&$ins, $page, $flags) { /** * Gives a list of pages for a given include statement - * - * @author Michael Hamann */ - function _get_included_pages($mode, $page, $sect, $parent_id, $flags) { + public function getIncludedPages($mode, $page, $sect, $parent_id, $flags) + { global $conf; - $pages = array(); - switch($mode) { - case 'namespace': - $page = cleanID($page); - $ns = utf8_encodeFN(str_replace(':', '/', $page)); - // depth is absolute depth, not relative depth, but 0 has a special meaning. - $depth = $flags['depth'] ? $flags['depth'] + substr_count($page, ':') + ($page ? 1 : 0) : 0; - search($pagearrays, $conf['datadir'], 'search_allpages', array('depth' => $depth, 'skipacl' => false), $ns); - if (is_array($pagearrays)) { + $pages = []; + switch ($mode) { + case 'namespace': + $page = cleanID($page); + $ns = utf8_encodeFN(str_replace(':', '/', $page)); + // depth is absolute depth, not relative depth, but 0 has a special meaning. + $depth = $flags['depth'] ? $flags['depth'] + substr_count($page, ':') + ($page ? 1 : 0) : 0; + search($pagearrays, $conf['datadir'], 'search_allpages', ['depth' => $depth, 'skipacl' => false], $ns); + if (is_array($pagearrays)) { + foreach ($pagearrays as $pagearray) { + if (!isHiddenPage($pagearray['id'])) // skip hidden pages + $pages[] = $pagearray['id']; + } + } + break; + case 'tagtopic': + if (!$this->taghelper) + $this->taghelper = plugin_load('helper', 'tag'); + if (!$this->taghelper) { + msg('You have to install the tag plugin to use this functionality!', -1); + return []; + } + $tag = $page; + $sect = ''; + $pagearrays = $this->taghelper->getTopic('', null, $tag); foreach ($pagearrays as $pagearray) { - if (!isHiddenPage($pagearray['id'])) // skip hidden pages - $pages[] = $pagearray['id']; + $pages[] = $pagearray['id']; } - } - break; - case 'tagtopic': - if (!$this->taghelper) - $this->taghelper = plugin_load('helper', 'tag'); - if(!$this->taghelper) { - msg('You have to install the tag plugin to use this functionality!', -1); - return array(); - } - $tag = $page; - $sect = ''; - $pagearrays = $this->taghelper->getTopic('', null, $tag); - foreach ($pagearrays as $pagearray) { - $pages[] = $pagearray['id']; - } - break; - default: - $page = $this->_apply_macro($page, $parent_id); - // resolve shortcuts and clean ID - $page = (new PageResolver($parent_id))->resolveId($page); - if (auth_quickaclcheck($page) >= AUTH_READ) - $pages[] = $page; + break; + default: + $page = $this->applyMacro($page, $parent_id); + // resolve shortcuts and clean ID + $page = (new PageResolver($parent_id))->resolveId($page); + if (auth_quickaclcheck($page) >= AUTH_READ) + $pages[] = $page; } if (isset($flags['exclude'])) $pages = array_filter($pages, function ($page) use ($flags) { if (@preg_match($flags['exclude'], $page)) - return FALSE; - return TRUE; + return false; + return true; }); if (count($pages) > 1) { if ($flags['order'] === 'id') { if ($flags['rsort']) { - usort($pages, array($this, '_r_strnatcasecmp')); + usort($pages, [$this, 'reverseStrnatcasecmp']); } else { natcasesort($pages); } } else { - $ordered_pages = array(); + $ordered_pages = []; foreach ($pages as $page) { $key = ''; switch ($flags['order']) { @@ -769,11 +858,11 @@ function _get_included_pages($mode, $page, $sect, $parent_id, $flags) { $key = ''; break; } - $key .= '_'.$page; + $key .= '_' . $page; $ordered_pages[$key] = $page; } if ($flags['rsort']) { - uksort($ordered_pages, array($this, '_r_strnatcasecmp')); + uksort($ordered_pages, [$this, 'reverseStrnatcasecmp']); } else { uksort($ordered_pages, 'strnatcasecmp'); } @@ -781,10 +870,10 @@ function _get_included_pages($mode, $page, $sect, $parent_id, $flags) { } } - $result = array(); + $result = []; foreach ($pages as $page) { $exists = page_exists($page); - $result[] = array('id' => $page, 'exists' => $exists, 'parent_id' => $parent_id); + $result[] = ['id' => $page, 'exists' => $exists, 'parent_id' => $parent_id]; } return $result; } @@ -800,7 +889,8 @@ function _get_included_pages($mode, $page, $sect, $parent_id, $flags) { * 0 if str1 is lesser than * str2, and 0 if they are equal. */ - function _r_strnatcasecmp($a, $b) { + public function reverseStrnatcasecmp($a, $b) + { return strnatcasecmp($b, $a); } @@ -808,15 +898,16 @@ function _r_strnatcasecmp($a, $b) { * This function generates the list of all included pages from a list of metadata * instructions. */ - function _get_included_pages_from_meta_instructions($instructions) { - $pages = array(); + public function getIncludedPagesFromMetaInstructions($instructions) + { + $pages = []; foreach ($instructions as $instruction) { - $mode = $instruction['mode']; - $page = $instruction['page']; - $sect = $instruction['sect']; + $mode = $instruction['mode']; + $page = $instruction['page']; + $sect = $instruction['sect']; $parent_id = $instruction['parent_id']; - $flags = $instruction['flags']; - $pages = array_merge($pages, $this->_get_included_pages($mode, $page, $sect, $parent_id, $flags)); + $flags = $instruction['flags']; + $pages = array_merge($pages, $this->getIncludedPages($mode, $page, $sect, $parent_id, $flags)); } return $pages; } @@ -825,39 +916,45 @@ function _get_included_pages_from_meta_instructions($instructions) { * Get wiki language from "HTTP_ACCEPT_LANGUAGE" * We allow the pattern e.g. "ja,en-US;q=0.7,en;q=0.3" */ - function _get_language_of_wiki($id, $parent_id) { - global $conf; - $result = $conf['lang']; - if(strpos($id, '@BROWSER_LANG@') !== false){ - $brlangp = "/([a-zA-Z]{1,8}(-[a-zA-Z]{1,8})*|\*)(;q=(0(.[0-9]{0,3})?|1(.0{0,3})?))?/"; - if(preg_match_all( - $brlangp, $_SERVER["HTTP_ACCEPT_LANGUAGE"], - $matches, PREG_SET_ORDER - )){ - $langs = array(); - foreach($matches as $match){ - $langname = $match[1] == '*' ? $conf['lang'] : $match[1]; - $qvalue = $match[4] == '' ? 1.0 : $match[4]; - $langs[$langname] = $qvalue; - } - arsort($langs); - foreach($langs as $lang => $langq){ - $testpage = $this->_apply_macro(str_replace('@BROWSER_LANG@', $lang, $id), $parent_id); - $testpage = (new PageResolver($parent_id))->resolveId($testpage); - if (page_exists($testpage)) { - $result = $lang; - break; - } - } - } - } - return cleanID($result); + protected function getLanguageOfWiki($id, $parent_id) + { + global $conf; + $result = $conf['lang']; + if (strpos($id, '@BROWSER_LANG@') !== false) { + $brlangp = "/([a-zA-Z]{1,8}(-[a-zA-Z]{1,8})*|\\*)(;q=(0(.\\d{0,3})?|1(.0{0,3})?))?/"; + if ( + preg_match_all( + $brlangp, + $_SERVER["HTTP_ACCEPT_LANGUAGE"], + $matches, + PREG_SET_ORDER + ) + ) { + $langs = []; + foreach ($matches as $match) { + $langname = $match[1] == '*' ? $conf['lang'] : $match[1]; + $qvalue = $match[4] == '' ? 1.0 : $match[4]; + $langs[$langname] = $qvalue; + } + arsort($langs); + foreach (array_keys($langs) as $lang) { + $testpage = $this->applyMacro(str_replace('@BROWSER_LANG@', $lang, $id), $parent_id); + $testpage = (new PageResolver($parent_id))->resolveId($testpage); + if (page_exists($testpage)) { + $result = $lang; + break; + } + } + } + } + return cleanID($result); } /** * Makes user or date dependent includes possible */ - function _apply_macro($id, $parent_id) { + protected function applyMacro($id, $parent_id) + { global $USERINFO; /* @var Input $INPUT */ global $INPUT; @@ -865,8 +962,8 @@ function _apply_macro($id, $parent_id) { // The following is basicaly copied from basicinfo() because // this function can be called from within pageinfo() in // p_get_metadata and thus we cannot rely on $INFO being set - if($INPUT->server->has('REMOTE_USER')) { - $user = $INPUT->server->str('REMOTE_USER'); + if ($INPUT->server->has('REMOTE_USER')) { + $user = $INPUT->server->str('REMOTE_USER'); } else { // no registered user - use IP $user = clientIP(true); @@ -874,7 +971,7 @@ function _apply_macro($id, $parent_id) { // Take user's name if possible, login name otherwise if (!empty($USERINFO['name'])) { - $name = $USERINFO['name']; + $name = $USERINFO['name']; } else { $name = $user; } @@ -887,55 +984,54 @@ function _apply_macro($id, $parent_id) { } $time_stamp = time(); - if(preg_match('/@DATE(\w+)@/',$id,$matches)) { - switch($matches[1]) { - case 'PMONTH': - $time_stamp = strtotime("-1 month"); - break; - case 'NMONTH': - $time_stamp = strtotime("+1 month"); - break; - case 'NWEEK': - $time_stamp = strtotime("+1 week"); - break; - case 'PWEEK': - $time_stamp = strtotime("-1 week"); - break; - case 'TOMORROW': - $time_stamp = strtotime("+1 day"); - break; - case 'YESTERDAY': - $time_stamp = strtotime("-1 day"); - break; - case 'NYEAR': - $time_stamp = strtotime("+1 year"); - break; - case 'PYEAR': - $time_stamp = strtotime("-1 year"); - break; + if (preg_match('/@DATE(\w+)@/', $id, $matches)) { + switch ($matches[1]) { + case 'PMONTH': + $time_stamp = strtotime("-1 month"); + break; + case 'NMONTH': + $time_stamp = strtotime("+1 month"); + break; + case 'NWEEK': + $time_stamp = strtotime("+1 week"); + break; + case 'PWEEK': + $time_stamp = strtotime("-1 week"); + break; + case 'TOMORROW': + $time_stamp = strtotime("+1 day"); + break; + case 'YESTERDAY': + $time_stamp = strtotime("-1 day"); + break; + case 'NYEAR': + $time_stamp = strtotime("+1 year"); + break; + case 'PYEAR': + $time_stamp = strtotime("-1 year"); + break; } - $id = preg_replace('/@DATE(\w+)@/','', $id); + $id = preg_replace('/@DATE(\w+)@/', '', $id); } - $replace = array( - '@USER@' => cleanID($user), - '@NAME@' => cleanID($name), - '@GROUP@' => cleanID($group), - '@BROWSER_LANG@' => $this->_get_language_of_wiki($id, $parent_id), - '@YEAR@' => date('Y',$time_stamp), - '@MONTH@' => date('m',$time_stamp), - '@WEEK@' => date('W',$time_stamp), - '@DAY@' => date('d',$time_stamp), - '@YEARPMONTH@' => date('Ym',strtotime("-1 month")), - '@PMONTH@' => date('m',strtotime("-1 month")), - '@NMONTH@' => date('m',strtotime("+1 month")), - '@YEARNMONTH@' => date('Ym',strtotime("+1 month")), - '@YEARPWEEK@' => date('YW',strtotime("-1 week")), - '@PWEEK@' => date('W',strtotime("-1 week")), - '@NWEEK@' => date('W',strtotime("+1 week")), - '@YEARNWEEK@' => date('YW',strtotime("+1 week")), - ); + $replace = [ + '@USER@' => cleanID($user), + '@NAME@' => cleanID($name), + '@GROUP@' => cleanID($group), + '@BROWSER_LANG@' => $this->getLanguageOfWiki($id, $parent_id), + '@YEAR@' => date('Y', $time_stamp), + '@MONTH@' => date('m', $time_stamp), + '@WEEK@' => date('W', $time_stamp), + '@DAY@' => date('d', $time_stamp), + '@YEARPMONTH@' => date('Ym', strtotime("-1 month")), + '@PMONTH@' => date('m', strtotime("-1 month")), + '@NMONTH@' => date('m', strtotime("+1 month")), + '@YEARNMONTH@' => date('Ym', strtotime("+1 month")), + '@YEARPWEEK@' => date('YW', strtotime("-1 week")), + '@PWEEK@' => date('W', strtotime("-1 week")), + '@NWEEK@' => date('W', strtotime("+1 week")), + '@YEARNWEEK@' => date('YW', strtotime("+1 week")) + ]; return str_replace(array_keys($replace), array_values($replace), $id); } } -// vim:ts=4:sw=4:et: diff --git a/syntax/closelastsecedit.php b/syntax/closelastsecedit.php index a5741c8..c3fede4 100644 --- a/syntax/closelastsecedit.php +++ b/syntax/closelastsecedit.php @@ -1,36 +1,45 @@ */ - -class syntax_plugin_include_closelastsecedit extends DokuWiki_Syntax_Plugin { - - function getType() { +class syntax_plugin_include_closelastsecedit extends SyntaxPlugin +{ + /** @inheritdoc */ + public function getType() + { return 'formatting'; } - function getSort() { + /** @inheritdoc */ + public function getSort() + { return 50; } - function handle($match, $state, $pos, Doku_Handler $handler) { + /** @inheritdoc */ + public function handle($match, $state, $pos, Doku_Handler $handler) + { // this is a syntax plugin that doesn't offer any syntax, so there's nothing to handle by the parser } /** * Finishes the last open section edit + * + * @inheritdoc */ - function render($mode, Doku_Renderer $renderer, $data) { - if ($mode == 'xhtml') { - /** @var Doku_Renderer_xhtml $renderer */ - list($endpos) = $data; - $renderer->finishSectionEdit($endpos); - return true; - } - return false; + public function render($mode, Doku_Renderer $renderer, $data) + { + if ($mode != 'xhtml') return false; + + /** @var Doku_Renderer_xhtml $renderer */ + [$endpos] = $data; + $renderer->finishSectionEdit($endpos); + return true; } } -// vim:ts=4:sw=4:et: diff --git a/syntax/editbtn.php b/syntax/editbtn.php index 4d9367f..b441796 100644 --- a/syntax/editbtn.php +++ b/syntax/editbtn.php @@ -1,43 +1,45 @@ */ - -class syntax_plugin_include_editbtn extends DokuWiki_Syntax_Plugin { - - function getType() { +class syntax_plugin_include_editbtn extends SyntaxPlugin +{ + /** @inheritdoc */ + public function getType() + { return 'formatting'; } - - function getSort() { + + /** @inheritdoc */ + public function getSort() + { return 50; } - function handle($match, $state, $pos, Doku_Handler $handler) { + /** @inheritdoc */ + public function handle($match, $state, $pos, Doku_Handler $handler) + { // this is a syntax plugin that doesn't offer any syntax, so there's nothing to handle by the parser } /** * Renders an include edit button * - * @author Michael Klier + * @inheritdoc */ - function render($mode, Doku_Renderer $renderer, $data) { - list($title, $hid) = $data; - if ($mode == 'xhtml') { - if (defined('SEC_EDIT_PATTERN')) { // for DokuWiki Greebo and more recent versions - $renderer->startSectionEdit(0, array('target' => 'plugin_include_editbtn', 'name' => $title, 'hid' => $hid)); - } else { - $renderer->startSectionEdit(0, 'plugin_include_editbtn', $title); - } + public function render($mode, Doku_Renderer $renderer, $data) + { + [$title, $hid] = $data; + if ($mode != 'xhtml') return false; - $renderer->finishSectionEdit(); - return true; - } - return false; + $renderer->startSectionEdit(0, ['target' => 'plugin_include_editbtn', 'name' => $title, 'hid' => $hid]); + $renderer->finishSectionEdit(); + return true; } } -// vim:ts=4:sw=4:et: diff --git a/syntax/footer.php b/syntax/footer.php index 523d005..d910400 100644 --- a/syntax/footer.php +++ b/syntax/footer.php @@ -1,22 +1,30 @@ */ - -class syntax_plugin_include_footer extends DokuWiki_Syntax_Plugin { - - function getType() { +class syntax_plugin_include_footer extends SyntaxPlugin +{ + /** @inheritdoc */ + public function getType() + { return 'formatting'; } - function getSort() { + /** @inheritdoc */ + public function getSort() + { return 300; } - function handle($match, $state, $pos, Doku_Handler $handler) { + /** @inheritdoc */ + public function handle($match, $state, $pos, Doku_Handler $handler) + { // this is a syntax plugin that doesn't offer any syntax, so there's nothing to handle by the parser } @@ -25,16 +33,18 @@ function handle($match, $state, $pos, Doku_Handler $handler) { * * Code heavily copied from the header renderer from inc/parser/xhtml.php, just * added an href parameter to the anchor tag linking to the wikilink. + * + * @inheritdoc */ - function render($mode, Doku_Renderer $renderer, $data) { + public function render($mode, Doku_Renderer $renderer, $data) + { + if ($mode != 'xhtml') return false; + /** @var Doku_Renderer_xhtml $renderer */ - list($page, $sect, $sect_title, $flags, $redirect_id, $footer_lvl) = $data; + [$page, $sect, $sect_title, $flags, $redirect_id, $footer_lvl] = $data; - if ($mode == 'xhtml') { - $renderer->doc .= $this->html_footer($page, $sect, $sect_title, $flags, $footer_lvl, $renderer); - return true; - } - return false; + $renderer->doc .= $this->htmlFooter($page, $sect, $sect_title, $flags, $footer_lvl, $renderer); + return true; } /** @@ -42,29 +52,31 @@ function render($mode, Doku_Renderer $renderer, $data) { * @param $renderer Doku_Renderer_xhtml The (xhtml) renderer * @return string The HTML code of the footer */ - function html_footer($page, $sect, $sect_title, $flags, $footer_lvl, &$renderer) { - global $conf, $ID; + protected function htmlFooter($page, $sect, $sect_title, $flags, $footer_lvl, $renderer) + { + global $conf; - if(!$flags['footer']) return ''; + if (!$flags['footer']) return ''; - $meta = p_get_metadata($page); + $meta = p_get_metadata($page); $exists = page_exists($page); - $xhtml = array(); + $xhtml = []; + // permalink if ($flags['permalink']) { $class = ($exists ? 'wikilink1' : 'wikilink2'); - $url = ($sect) ? wl($page) . '#' . $sect : wl($page); - $name = ($sect) ? $sect_title : $page; + $url = ($sect) ? wl($page) . '#' . $sect : wl($page); + $name = ($sect) ? $sect_title : $page; $title = ($sect) ? $page . '#' . $sect : $page; if (!$title) $title = str_replace('_', ' ', noNS($page)); - $link = array( - 'url' => $url, - 'title' => $title, - 'name' => $name, - 'target' => $conf['target']['wiki'], - 'class' => $class . ' permalink', - 'more' => 'rel="bookmark"', - ); + $link = [ + 'url' => $url, + 'title' => $title, + 'name' => $name, + 'target' => $conf['target']['wiki'], + 'class' => $class . ' permalink', + 'more' => 'rel="bookmark"' + ]; $xhtml[] = $renderer->_formatLink($link); } @@ -72,9 +84,9 @@ function html_footer($page, $sect, $sect_title, $flags, $footer_lvl, &$renderer) if ($flags['date'] && $exists) { $date = $meta['date']['created']; if ($date) { - $xhtml[] = '' - . dformat($date) - . ''; + $xhtml[] = '' + . dformat($date) + . ''; } } @@ -82,32 +94,38 @@ function html_footer($page, $sect, $sect_title, $flags, $footer_lvl, &$renderer) if ($flags['mdate'] && $exists) { $mdate = $meta['date']['modified']; if ($mdate) { - $xhtml[] = '' - . dformat($mdate) - . ''; + $xhtml[] = '' + . dformat($mdate) + . ''; } } // author if ($flags['user'] && $exists) { - $author = $meta['user']; + $author = $meta['user']; if ($author) { - if (function_exists('userlink')) { - $xhtml[] = '' . userlink($author) . ''; - } else { // DokuWiki versions < 2014-05-05 doesn't have userlink support, fall back to not providing a link - $xhtml[] = '' . editorinfo($author) . ''; - } + $xhtml[] = '' . userlink($author) . ''; } } // comments - let Discussion Plugin do the work for us - if (empty($sect) && $flags['comments'] && (!plugin_isdisabled('discussion')) && ($discussion = plugin_load('helper', 'discussion'))) { + if ( + empty($sect) && + $flags['comments'] && + (!plugin_isdisabled('discussion')) && + ($discussion = plugin_load('helper', 'discussion')) + ) { $disc = $discussion->td($page); if ($disc) $xhtml[] = '' . $disc . ''; } // linkbacks - let Linkback Plugin do the work for us - if (empty($sect) && $flags['linkbacks'] && (!plugin_isdisabled('linkback')) && ($linkback = plugin_load('helper', 'linkback'))) { + if ( + empty($sect) && + $flags['linkbacks'] && + (!plugin_isdisabled('linkback')) && + ($linkback = plugin_load('helper', 'linkback')) + ) { $link = $linkback->td($page); if ($link) $xhtml[] = '' . $link . ''; } @@ -115,12 +133,17 @@ function html_footer($page, $sect, $sect_title, $flags, $footer_lvl, &$renderer) $xhtml = implode(DOKU_LF . DOKU_TAB . '· ', $xhtml); // tags - let Tag Plugin do the work for us - if (empty($sect) && $flags['tags'] && (!plugin_isdisabled('tag')) && ($tag = plugin_load('helper', 'tag'))) { + if ( + empty($sect) && + $flags['tags'] && + (!plugin_isdisabled('tag')) && + ($tag = plugin_load('helper', 'tag')) + ) { $tags = $tag->td($page); - if($tags) { + if ($tags) { $xhtml .= '
' . DOKU_LF - . DOKU_TAB . $tags . DOKU_LF - . DOKU_TAB . '
' . DOKU_LF; + . DOKU_TAB . $tags . DOKU_LF + . DOKU_TAB . '' . DOKU_LF; } } @@ -130,4 +153,3 @@ function html_footer($page, $sect, $sect_title, $flags, $footer_lvl, &$renderer) return '
' . DOKU_LF . DOKU_TAB . $xhtml . DOKU_LF . '
' . DOKU_LF; } } -// vim:ts=4:sw=4:et: diff --git a/syntax/header.php b/syntax/header.php index 3f14dcb..89fc4d0 100644 --- a/syntax/header.php +++ b/syntax/header.php @@ -1,4 +1,7 @@ * @author Michael Klier */ - -class syntax_plugin_include_header extends DokuWiki_Syntax_Plugin { - - function getType() { +class syntax_plugin_include_header extends SyntaxPlugin +{ + /** @inheritdoc */ + public function getType() + { return 'formatting'; } - - function getSort() { + + /** @inheritdoc */ + public function getSort() + { return 50; } - function handle($match, $state, $pos, Doku_Handler $handler) { + /** @inheritdoc */ + public function handle($match, $state, $pos, Doku_Handler $handler) + { // this is a syntax plugin that doesn't offer any syntax, so there's nothing to handle by the parser } /** * Renders a permalink header. - * + * * Code heavily copied from the header renderer from inc/parser/xhtml.php, just * added an href parameter to the anchor tag linking to the wikilink. + * @inheritdoc */ - function render($mode, Doku_Renderer $renderer, $data) { + public function render($mode, Doku_Renderer $renderer, $data) + { global $conf; - list($headline, $lvl, $pos, $page, $sect, $flags) = $data; + [$headline, $lvl, $pos, $page, $sect, $flags] = $data; - if ($mode == 'xhtml') { - /** @var Doku_Renderer_xhtml $renderer */ - $hid = $renderer->_headerToLink($headline, true); - $renderer->toc_additem($hid, $headline, $lvl); - $url = ($sect) ? wl($page) . '#' . $sect : wl($page); - $renderer->doc .= DOKU_LF.'_get_firsttag($page); - if($tag) { - $classes[] = 'include_firsttag__' . $tag; - } - } - // the include header instruction is always at the beginning of the first section edit inside the include - // wrap so there is no need to close a previous section edit. - if ($lvl <= $conf['maxseclevel']) { - if (defined('SEC_EDIT_PATTERN')) { // for DokuWiki Greebo and more recent versions - $classes[] = $renderer->startSectionEdit($pos, array('target' => 'section', 'name' => $headline, 'hid' => $hid)); - } else { - $classes[] = $renderer->startSectionEdit($pos, 'section', $headline); - } - } - if ($classes) { - $renderer->doc .= ' class="'. implode(' ', $classes) . '"'; - } - $headline = $renderer->_xmlEntities($headline); - $renderer->doc .= ' id="'.$hid.'">'; - $renderer->doc .= $headline; - $renderer->doc .= '' . DOKU_LF; - return true; - } else { + if ($mode != 'xhtml') { + // just output a standard header for all non-xhtml modes $renderer->header($headline, $lvl, $pos); + return true; + } + + /** @var Doku_Renderer_xhtml $renderer */ + $hid = $renderer->_headerToLink($headline, true); + $renderer->toc_additem($hid, $headline, $lvl); + $url = ($sect) ? wl($page) . '#' . $sect : wl($page); + $renderer->doc .= 'getFirsttag($page); + if ($tag) { + $classes[] = 'include_firsttag__' . $tag; + } + } + // the include header instruction is always at the beginning of the first section edit inside the include + // wrap so there is no need to close a previous section edit. + if ($lvl <= $conf['maxseclevel']) { + $classes[] = $renderer->startSectionEdit($pos, ['target' => 'section', 'name' => $headline, 'hid' => $hid]); + } + if ($classes) { + $renderer->doc .= ' class="' . implode(' ', $classes) . '"'; } - return false; + $headline = $renderer->_xmlEntities($headline); + $renderer->doc .= ' id="' . $hid . '">'; + $renderer->doc .= $headline; + $renderer->doc .= ''; + return true; } /** @@ -75,21 +82,21 @@ function render($mode, Doku_Renderer $renderer, $data) { * * @author Michael Klier */ - function _get_firsttag($page) { - if(plugin_isdisabled('tag') || (!plugin_load('helper', 'tag'))) { + protected function getFirsttag($page) + { + if (plugin_isdisabled('tag') || (!plugin_load('helper', 'tag'))) { return false; } $subject = p_get_metadata($page, 'subject'); if (is_array($subject)) { $tag = $subject[0]; } else { - list($tag, $rest) = explode(' ', $subject, 2); + [$tag, $rest] = explode(' ', $subject, 2); } - if($tag) { + if ($tag) { return $tag; } else { return false; } } } -// vim:ts=4:sw=4:et: diff --git a/syntax/include.php b/syntax/include.php index 44b803a..e279c47 100644 --- a/syntax/include.php +++ b/syntax/include.php @@ -1,4 +1,7 @@ * @author Gina Häußge, Michael Klier */ +class syntax_plugin_include_include extends SyntaxPlugin +{ + /** @var $helper helper_plugin_include */ + public $helper; -/** - * All DokuWiki plugins to extend the parser/rendering mechanism - * need to inherit from this class - */ -class syntax_plugin_include_include extends DokuWiki_Syntax_Plugin { + /** @inheritdoc */ + public function getType() + { + return 'substition'; + } - /** @var $helper helper_plugin_include */ - var $helper = null; - - /** - * Get syntax plugin type. - * - * @return string The plugin type. - */ - function getType() { return 'substition'; } - - /** - * Get sort order of syntax plugin. - * - * @return int The sort order. - */ - function getSort() { return 303; } - - /** - * Get paragraph type. - * - * @return string The paragraph type. - */ - function getPType() { return 'block'; } - - /** - * Connect patterns/modes - * - * @param $mode mixed The current mode - */ - function connectTo($mode) { + /** @inheritdoc */ + public function getSort() + { + return 303; + } + + /** @inheritdoc */ + public function getPType() + { + return 'block'; + } + + /** @inheritdoc */ + public function connectTo($mode) + { $this->Lexer->addSpecialPattern("{{page>.+?}}", $mode, 'plugin_include_include'); $this->Lexer->addSpecialPattern("{{section>.+?}}", $mode, 'plugin_include_include'); $this->Lexer->addSpecialPattern("{{namespace>.+?}}", $mode, 'plugin_include_include'); $this->Lexer->addSpecialPattern("{{tagtopic>.+?}}", $mode, 'plugin_include_include'); } - /** - * Handle syntax matches - * - * @param string $match The current match - * @param int $state The match state - * @param int $pos The position of the match - * @param Doku_Handler $handler The hanlder object - * @return array The instructions of the plugin - */ - function handle($match, $state, $pos, Doku_Handler $handler) { + /** @inheritdoc */ + public function handle($match, $state, $pos, Doku_Handler $handler) + { $match = substr($match, 2, -2); // strip markup - list($match, $flags) = array_pad(explode('&', $match, 2), 2, ''); + [$match, $flags] = array_pad(explode('&', $match, 2), 2, ''); // break the pattern up into its parts - list($mode, $page, $sect) = array_pad(preg_split('/>|#/u', $match, 3), 3, null); + [$mode, $page, $sect] = array_pad(preg_split('/>|#/u', $match, 3), 3, null); $check = false; if (isset($sect)) $sect = sectionID($sect, $check); - $level = NULL; - return array($mode, $page, $sect, explode('&', $flags), $level, $pos); + $level = null; + return [$mode, $page, $sect, explode('&', $flags), $level, $pos]; } - /** - * Renders the included page(s) - * - * @author Michael Hamann - */ - function render($format, Doku_Renderer $renderer, $data) { + /** @inheritdoc */ + public function render($format, Doku_Renderer $renderer, $data) + { global $ID; // static stack that records all ancestors of the child pages - static $page_stack = array(); + static $page_stack = []; // when there is no id just assume the global $ID is the current id if (empty($page_stack)) $page_stack[] = $ID; - $parent_id = $page_stack[count($page_stack)-1]; + $parent_id = $page_stack[count($page_stack) - 1]; $root_id = $page_stack[0]; - list($mode, $page, $sect, $flags, $level, $pos) = $data; + [$mode, $page, $sect, $flags, $level, $pos] = $data; - if (!$this->helper) + if (!$this->helper) { $this->helper = plugin_load('helper', 'include'); - $flags = $this->helper->get_flags($flags); + } + $flags = $this->helper->getFlags($flags); - $pages = $this->helper->_get_included_pages($mode, $page, $sect, $parent_id, $flags); + $pages = $this->helper->getIncludedPages($mode, $page, $sect, $parent_id, $flags); if ($format == 'metadata') { /** @var Doku_Renderer_metadata $renderer */ @@ -112,14 +95,24 @@ function render($format, Doku_Renderer $renderer, $data) { unset($renderer->meta['plugin_include']); } - $renderer->meta['plugin_include']['instructions'][] = compact('mode', 'page', 'sect', 'parent_id', 'flags'); - if (!isset($renderer->meta['plugin_include']['pages'])) - $renderer->meta['plugin_include']['pages'] = array(); // add an array for array_merge - $renderer->meta['plugin_include']['pages'] = array_merge($renderer->meta['plugin_include']['pages'], $pages); + $renderer->meta['plugin_include']['instructions'][] = [ + 'mode' => $mode, + 'page' => $page, + 'sect' => $sect, + 'parent_id' => $parent_id, + 'flags' => $flags + ]; + if (!isset($renderer->meta['plugin_include']['pages'])) { + $renderer->meta['plugin_include']['pages'] = []; // add an array for array_merge + } + $renderer->meta['plugin_include']['pages'] = array_merge( + $renderer->meta['plugin_include']['pages'], + $pages + ); $renderer->meta['plugin_include']['include_content'] = isset($_REQUEST['include_content']); } - $secids = array(); + $secids = []; if ($format == 'xhtml' || $format == 'odt') { $secids = p_get_metadata($ID, 'plugin_include secids'); } @@ -130,14 +123,22 @@ function render($format, Doku_Renderer $renderer, $data) { $exists = $page['exists']; if (in_array($id, $page_stack)) continue; - array_push($page_stack, $id); + $page_stack[] = $id; // add references for backlink if ($format == 'metadata') { $renderer->meta['relation']['references'][$id] = $exists; - $renderer->meta['relation']['haspart'][$id] = $exists; - if (!$sect && !$flags['firstsec'] && !$flags['linkonly'] && !isset($renderer->meta['plugin_include']['secids'][$id])) { - $renderer->meta['plugin_include']['secids'][$id] = array('hid' => 'plugin_include__'.str_replace(':', '__', $id), 'pos' => $pos); + $renderer->meta['relation']['haspart'][$id] = $exists; + if ( + !$sect && + !$flags['firstsec'] && + !$flags['linkonly'] && + !isset($renderer->meta['plugin_include']['secids'][$id]) + ) { + $renderer->meta['plugin_include']['secids'][$id] = [ + 'hid' => 'plugin_include__' . str_replace(':', '__', $id), + 'pos' => $pos + ]; } } @@ -147,7 +148,15 @@ function render($format, Doku_Renderer $renderer, $data) { unset($flags['include_secid']); } - $instructions = $this->helper->_get_instructions($id, $sect, $mode, $level, $flags, $root_id, $secids); + $instructions = $this->helper->getInstructions( + $id, + $sect, + $mode, + $level, + $flags, + $root_id, + $secids + ); if (!$flags['editbtn']) { global $conf; @@ -170,4 +179,3 @@ function render($format, Doku_Renderer $renderer, $data) { return true; } } -// vim:ts=4:sw=4:et: diff --git a/syntax/locallink.php b/syntax/locallink.php index 2a6a612..507422d 100644 --- a/syntax/locallink.php +++ b/syntax/locallink.php @@ -1,45 +1,52 @@ */ - -class syntax_plugin_include_locallink extends DokuWiki_Syntax_Plugin { - - function getType() { +class syntax_plugin_include_locallink extends SyntaxPlugin +{ + /** @inheritdoc */ + public function getType() + { return 'formatting'; } - function getSort() { + /** @inheritdoc */ + public function getSort() + { return 50; } - function handle($match, $state, $pos, Doku_Handler $handler) { + /** @inheritdoc */ + public function handle($match, $state, $pos, Doku_Handler $handler) + { // this is a syntax plugin that doesn't offer any syntax, so there's nothing to handle by the parser } /** * Displays a local link to an included page * - * @author Michael Hamann + * @inheritdoc */ - function render($mode, Doku_Renderer $renderer, $data) { + public function render($mode, Doku_Renderer $renderer, $data) + { global $ID; - if ($mode == 'xhtml') { - /** @var Doku_Renderer_xhtml $renderer */ - list($hash, $name, $id) = $data; - // construct title in the same way it would be done for internal links - $default = $renderer->_simpleTitle($id); - $name = $renderer->_getLinkTitle($name, $default, $isImage, $id); - $title = $ID.' ↵'; - $renderer->doc .= ''; - $renderer->doc .= $name; - $renderer->doc .= ''; - return true; - } - return false; + if ($mode != 'xhtml') return false; + + /** @var Doku_Renderer_xhtml $renderer */ + [$hash, $name, $id] = $data; + // construct title in the same way it would be done for internal links + $default = $renderer->_simpleTitle($id); + $name = $renderer->_getLinkTitle($name, $default, $isImage, $id); + $title = $ID . ' ↵'; + $renderer->doc .= ''; + $renderer->doc .= $name; + $renderer->doc .= ''; + return true; } } -// vim:ts=4:sw=4:et: diff --git a/syntax/readmore.php b/syntax/readmore.php index 689e11d..93c8514 100644 --- a/syntax/readmore.php +++ b/syntax/readmore.php @@ -1,30 +1,44 @@ */ - -class syntax_plugin_include_readmore extends DokuWiki_Syntax_Plugin { - - function getType() { +class syntax_plugin_include_readmore extends SyntaxPlugin +{ + /** @inheritdoc */ + public function getType() + { return 'formatting'; } - function getSort() { + /** @inheritdoc */ + public function getSort() + { return 50; } - function handle($match, $state, $pos, Doku_Handler $handler) { + /** @inheritdoc */ + public function handle($match, $state, $pos, Doku_Handler $handler) + { // this is a syntax plugin that doesn't offer any syntax, so there's nothing to handle by the parser } - function render($mode, Doku_Renderer $renderer, $data) { - list($page) = $data; + /** + * Renders the readmore link for the included page. + * + * @inheritdoc + */ + public function render($mode, Doku_Renderer $renderer, $data) + { + [$page] = $data; if ($mode == 'xhtml') { - $renderer->doc .= DOKU_LF.'

'.DOKU_LF; + $renderer->doc .= '

'; } else { $renderer->p_open(); } @@ -32,7 +46,7 @@ function render($mode, Doku_Renderer $renderer, $data) { $renderer->internallink($page, $this->getLang('readmore')); if ($mode == 'xhtml') { - $renderer->doc .= DOKU_LF.'

'.DOKU_LF; + $renderer->doc .= '

'; } else { $renderer->p_close(); } @@ -40,4 +54,3 @@ function render($mode, Doku_Renderer $renderer, $data) { return true; } } -// vim:ts=4:sw=4:et: diff --git a/syntax/sorttag.php b/syntax/sorttag.php index c7704ba..44034fd 100644 --- a/syntax/sorttag.php +++ b/syntax/sorttag.php @@ -1,57 +1,50 @@ * @author Michael Hamann - * */ -class syntax_plugin_include_sorttag extends DokuWiki_Syntax_Plugin { - - /** - * What kind of syntax are we? - */ - public function getType(){ +class syntax_plugin_include_sorttag extends SyntaxPlugin +{ + /** @inheritdoc */ + public function getType() + { return 'substition'; } - /** - * The paragraph type - block, we don't need paragraph tags - * - * @return string The paragraph type - */ - public function getPType() { + /** @inheritdoc */ + public function getPType() + { return 'block'; } - /** - * Where to sort in? - */ - public function getSort(){ + /** @inheritdoc */ + public function getSort() + { return 139; } - /** - * Connect pattern to lexer - */ - public function connectTo($mode) { - $this->Lexer->addSpecialPattern('{{include_n>.+?}}',$mode,'plugin_include_sorttag'); + /** @inheritdoc */ + public function connectTo($mode) + { + $this->Lexer->addSpecialPattern('{{include_n>.+?}}', $mode, 'plugin_include_sorttag'); } - /** - * Handle the match - */ - public function handle($match, $state, $pos, Doku_Handler $handler){ - $match = substr($match,12,-2); - return array($match); + /** @inheritdoc */ + public function handle($match, $state, $pos, Doku_Handler $handler) + { + $match = substr($match, 12, -2); + return [$match]; } - /** - * Render output - */ - public function render($mode, Doku_Renderer $renderer, $data) { + /** @inheritdoc */ + public function render($mode, Doku_Renderer $renderer, $data) + { if ($mode === 'metadata') { /** @var Doku_Renderer_metadata $renderer */ $renderer->meta['include_n'] = $data[0]; diff --git a/syntax/wrap.php b/syntax/wrap.php index 1cb630c..a9249e6 100644 --- a/syntax/wrap.php +++ b/syntax/wrap.php @@ -1,4 +1,7 @@ * @author Michael Hamann */ - -class syntax_plugin_include_wrap extends DokuWiki_Syntax_Plugin { - - function getType() { +class syntax_plugin_include_wrap extends SyntaxPlugin +{ + /** @inheritdoc */ + public function getType() + { return 'formatting'; } - function getSort() { + /** @inheritdoc */ + public function getSort() + { return 50; } - function handle($match, $state, $pos, Doku_Handler $handler) { + /** @inheritdoc */ + public function handle($match, $state, $pos, Doku_Handler $handler) + { // this is a syntax plugin that doesn't offer any syntax, so there's nothing to handle by the parser } @@ -25,54 +33,47 @@ function handle($match, $state, $pos, Doku_Handler $handler) { * Wraps the included page in a div and writes section edits for the action component * so it can detect where an included page starts/ends. * - * @author Michael Klier - * @author Michael Hamann + * @inheritdoc */ - function render($mode, Doku_Renderer $renderer, $data) { - if ($mode == 'xhtml') { - $state = array_shift($data); - switch($state) { - case 'open': - list($page, $redirect, $secid) = $data; - if ($redirect) { - if (defined('SEC_EDIT_PATTERN')) { // for DokuWiki Greebo and more recent versions - $renderer->startSectionEdit(0, array('target' => 'plugin_include_start', 'name' => $page, 'hid' => '')); - } else { - $renderer->startSectionEdit(0, 'plugin_include_start', $page); - } - } else { - if (defined('SEC_EDIT_PATTERN')) { // for DokuWiki Greebo and more recent versions - $renderer->startSectionEdit(0, array('target' => 'plugin_include_start_noredirect', 'name' => $page, 'hid' => '')); - } else { - $renderer->startSectionEdit(0, 'plugin_include_start_noredirect', $page); - } - } - $renderer->finishSectionEdit(); - // Start a new section with type != section so headers in the included page - // won't print section edit buttons of the parent page - if (defined('SEC_EDIT_PATTERN')) { // for DokuWiki Greebo and more recent versions - $renderer->startSectionEdit(0, array('target' => 'plugin_include_end', 'name' => $page, 'hid' => '')); - } else { - $renderer->startSectionEdit(0, 'plugin_include_end', $page); - } - if ($secid === NULL) { - $id = ''; - } else { - $id = ' id="'.$secid.'"'; - } - $renderer->doc .= '
' . DOKU_LF; - if (is_a($renderer,'renderer_plugin_dw2pdf')) { - $renderer->doc .= ''; - } - break; - case 'close': - $renderer->finishSectionEdit(); - $renderer->doc .= '
' . DOKU_LF; - break; - } - return true; + public function render($mode, Doku_Renderer $renderer, $data) + { + if ($mode !== 'xhtml') return false; + + $state = array_shift($data); + switch ($state) { + case 'open': + [$page, $redirect, $secid] = $data; + if ($redirect) { + $renderer->startSectionEdit( + 0, + ['target' => 'plugin_include_start', 'name' => $page, 'hid' => ''] + ); + } else { + $renderer->startSectionEdit( + 0, + ['target' => 'plugin_include_start_noredirect', 'name' => $page, 'hid' => ''] + ); + } + $renderer->finishSectionEdit(); + // Start a new section with type != section so headers in the included page + // won't print section edit buttons of the parent page + $renderer->startSectionEdit(0, ['target' => 'plugin_include_end', 'name' => $page, 'hid' => '']); + if ($secid === null) { + $id = ''; + } else { + $id = ' id="' . $secid . '"'; + } + $renderer->doc .= ''; + break; } - return false; + + return true; } } -// vim:ts=4:sw=4:et: