include.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. /**
  3. * Include Plugin: displays a wiki page within another
  4. * Usage:
  5. * {{page>page}} for "page" in same namespace
  6. * {{page>:page}} for "page" in top namespace
  7. * {{page>namespace:page}} for "page" in namespace "namespace"
  8. * {{page>.namespace:page}} for "page" in subnamespace "namespace"
  9. * {{page>page#section}} for a section of "page"
  10. *
  11. * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
  12. * @author Esther Brunner <wikidesign@gmail.com>
  13. * @author Christopher Smith <chris@jalakai.co.uk>
  14. * @author Gina Häußge, Michael Klier <dokuwiki@chimeric.de>
  15. */
  16. /**
  17. * All DokuWiki plugins to extend the parser/rendering mechanism
  18. * need to inherit from this class
  19. */
  20. class syntax_plugin_include_include extends DokuWiki_Syntax_Plugin {
  21. /** @var $helper helper_plugin_include */
  22. var $helper = null;
  23. /**
  24. * Get syntax plugin type.
  25. *
  26. * @return string The plugin type.
  27. */
  28. function getType() { return 'substition'; }
  29. /**
  30. * Get sort order of syntax plugin.
  31. *
  32. * @return int The sort order.
  33. */
  34. function getSort() { return 303; }
  35. /**
  36. * Get paragraph type.
  37. *
  38. * @return string The paragraph type.
  39. */
  40. function getPType() { return 'block'; }
  41. /**
  42. * Connect patterns/modes
  43. *
  44. * @param $mode mixed The current mode
  45. */
  46. function connectTo($mode) {
  47. $this->Lexer->addSpecialPattern("{{page>.+?}}", $mode, 'plugin_include_include');
  48. $this->Lexer->addSpecialPattern("{{section>.+?}}", $mode, 'plugin_include_include');
  49. $this->Lexer->addSpecialPattern("{{namespace>.+?}}", $mode, 'plugin_include_include');
  50. $this->Lexer->addSpecialPattern("{{tagtopic>.+?}}", $mode, 'plugin_include_include');
  51. }
  52. /**
  53. * Handle syntax matches
  54. *
  55. * @param string $match The current match
  56. * @param int $state The match state
  57. * @param int $pos The position of the match
  58. * @param Doku_Handler $handler The hanlder object
  59. * @return array The instructions of the plugin
  60. */
  61. function handle($match, $state, $pos, Doku_Handler $handler) {
  62. $match = substr($match, 2, -2); // strip markup
  63. list($match, $flags) = array_pad(explode('&', $match, 2), 2, '');
  64. // break the pattern up into its parts
  65. list($mode, $page, $sect) = array_pad(preg_split('/>|#/u', $match, 3), 3, null);
  66. $check = false;
  67. if (isset($sect)) $sect = sectionID($sect, $check);
  68. $level = NULL;
  69. return array($mode, $page, $sect, explode('&', $flags), $level, $pos);
  70. }
  71. /**
  72. * Renders the included page(s)
  73. *
  74. * @author Michael Hamann <michael@content-space.de>
  75. */
  76. function render($format, Doku_Renderer $renderer, $data) {
  77. global $ID;
  78. // static stack that records all ancestors of the child pages
  79. static $page_stack = array();
  80. // when there is no id just assume the global $ID is the current id
  81. if (empty($page_stack)) $page_stack[] = $ID;
  82. $parent_id = $page_stack[count($page_stack)-1];
  83. $root_id = $page_stack[0];
  84. list($mode, $page, $sect, $flags, $level, $pos) = $data;
  85. if (!$this->helper)
  86. $this->helper = plugin_load('helper', 'include');
  87. $flags = $this->helper->get_flags($flags);
  88. $pages = $this->helper->_get_included_pages($mode, $page, $sect, $parent_id, $flags);
  89. if ($format == 'metadata') {
  90. /** @var Doku_Renderer_metadata $renderer */
  91. // remove old persistent metadata of previous versions of the include plugin
  92. if (isset($renderer->persistent['plugin_include'])) {
  93. unset($renderer->persistent['plugin_include']);
  94. unset($renderer->meta['plugin_include']);
  95. }
  96. $renderer->meta['plugin_include']['instructions'][] = compact('mode', 'page', 'sect', 'parent_id', 'flags');
  97. if (!isset($renderer->meta['plugin_include']['pages']))
  98. $renderer->meta['plugin_include']['pages'] = array(); // add an array for array_merge
  99. $renderer->meta['plugin_include']['pages'] = array_merge($renderer->meta['plugin_include']['pages'], $pages);
  100. $renderer->meta['plugin_include']['include_content'] = isset($_REQUEST['include_content']);
  101. }
  102. $secids = array();
  103. if ($format == 'xhtml' || $format == 'odt') {
  104. $secids = p_get_metadata($ID, 'plugin_include secids');
  105. }
  106. foreach ($pages as $page) {
  107. extract($page);
  108. $id = $page['id'];
  109. $exists = $page['exists'];
  110. if (in_array($id, $page_stack)) continue;
  111. array_push($page_stack, $id);
  112. // add references for backlink
  113. if ($format == 'metadata') {
  114. $renderer->meta['relation']['references'][$id] = $exists;
  115. $renderer->meta['relation']['haspart'][$id] = $exists;
  116. if (!$sect && !$flags['firstsec'] && !$flags['linkonly'] && !isset($renderer->meta['plugin_include']['secids'][$id])) {
  117. $renderer->meta['plugin_include']['secids'][$id] = array('hid' => 'plugin_include__'.str_replace(':', '__', $id), 'pos' => $pos);
  118. }
  119. }
  120. if (isset($secids[$id]) && $pos === $secids[$id]['pos']) {
  121. $flags['include_secid'] = $secids[$id]['hid'];
  122. } else {
  123. unset($flags['include_secid']);
  124. }
  125. $instructions = $this->helper->_get_instructions($id, $sect, $mode, $level, $flags, $root_id, $secids);
  126. if (!$flags['editbtn']) {
  127. global $conf;
  128. $maxseclevel_org = $conf['maxseclevel'];
  129. $conf['maxseclevel'] = 0;
  130. }
  131. $renderer->nest($instructions);
  132. if (isset($maxseclevel_org)) {
  133. $conf['maxseclevel'] = $maxseclevel_org;
  134. unset($maxseclevel_org);
  135. }
  136. array_pop($page_stack);
  137. }
  138. // When all includes have been handled remove the current id
  139. // in order to allow the rendering of other pages
  140. if (count($page_stack) == 1) array_pop($page_stack);
  141. return true;
  142. }
  143. }
  144. // vim:ts=4:sw=4:et: