syntax.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. <?php
  2. /**
  3. * Info Plugin: Displays information about various DokuWiki internals
  4. *
  5. * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
  6. * @author Andreas Gohr <andi@splitbrain.org>
  7. * @author Esther Brunner <wikidesign@gmail.com>
  8. */
  9. class syntax_plugin_info extends DokuWiki_Syntax_Plugin
  10. {
  11. /**
  12. * What kind of syntax are we?
  13. */
  14. public function getType()
  15. {
  16. return 'substition';
  17. }
  18. /**
  19. * What about paragraphs?
  20. */
  21. public function getPType()
  22. {
  23. return 'block';
  24. }
  25. /**
  26. * Where to sort in?
  27. */
  28. public function getSort()
  29. {
  30. return 155;
  31. }
  32. /**
  33. * Connect pattern to lexer
  34. */
  35. public function connectTo($mode)
  36. {
  37. $this->Lexer->addSpecialPattern('~~INFO:\w+~~', $mode, 'plugin_info');
  38. }
  39. /**
  40. * Handle the match
  41. *
  42. * @param string $match The text matched by the patterns
  43. * @param int $state The lexer state for the match
  44. * @param int $pos The character position of the matched text
  45. * @param Doku_Handler $handler The Doku_Handler object
  46. * @return array Return an array with all data you want to use in render
  47. */
  48. public function handle($match, $state, $pos, Doku_Handler $handler)
  49. {
  50. $match = substr($match, 7, -2); //strip ~~INFO: from start and ~~ from end
  51. return array(strtolower($match));
  52. }
  53. /**
  54. * Create output
  55. *
  56. * @param string $format string output format being rendered
  57. * @param Doku_Renderer $renderer the current renderer object
  58. * @param array $data data created by handler()
  59. * @return boolean rendered correctly?
  60. */
  61. public function render($format, Doku_Renderer $renderer, $data)
  62. {
  63. if ($format == 'xhtml') {
  64. /** @var Doku_Renderer_xhtml $renderer */
  65. //handle various info stuff
  66. switch ($data[0]) {
  67. case 'syntaxmodes':
  68. $renderer->doc .= $this->renderSyntaxModes();
  69. break;
  70. case 'syntaxtypes':
  71. $renderer->doc .= $this->renderSyntaxTypes();
  72. break;
  73. case 'syntaxplugins':
  74. $this->renderPlugins('syntax', $renderer);
  75. break;
  76. case 'adminplugins':
  77. $this->renderPlugins('admin', $renderer);
  78. break;
  79. case 'actionplugins':
  80. $this->renderPlugins('action', $renderer);
  81. break;
  82. case 'rendererplugins':
  83. $this->renderPlugins('renderer', $renderer);
  84. break;
  85. case 'helperplugins':
  86. $this->renderPlugins('helper', $renderer);
  87. break;
  88. case 'authplugins':
  89. $this->renderPlugins('auth', $renderer);
  90. break;
  91. case 'remoteplugins':
  92. $this->renderPlugins('remote', $renderer);
  93. break;
  94. case 'helpermethods':
  95. $this->renderHelperMethods($renderer);
  96. break;
  97. case 'hooks':
  98. $this->renderHooks($renderer);
  99. break;
  100. case 'datetime':
  101. $renderer->doc .= date('r');
  102. break;
  103. default:
  104. $renderer->doc .= "no info about " . htmlspecialchars($data[0]);
  105. }
  106. return true;
  107. }
  108. return false;
  109. }
  110. /**
  111. * list all installed plugins
  112. *
  113. * uses some of the original renderer methods
  114. *
  115. * @param string $type
  116. * @param Doku_Renderer $renderer
  117. */
  118. protected function renderPlugins($type, Doku_Renderer $renderer)
  119. {
  120. global $lang;
  121. $plugins = plugin_list($type);
  122. $plginfo = array();
  123. // remove subparts
  124. foreach ($plugins as $p) {
  125. if (!$po = plugin_load($type, $p)) continue;
  126. list($name,/* $part */) = explode('_', $p, 2);
  127. $plginfo[$name] = $po->getInfo();
  128. }
  129. // list them
  130. $renderer->listu_open();
  131. foreach ($plginfo as $info) {
  132. $renderer->listitem_open(1);
  133. $renderer->listcontent_open();
  134. $renderer->externallink($info['url'], $info['name']);
  135. $renderer->cdata(' ');
  136. $renderer->emphasis_open();
  137. $renderer->cdata($info['date']);
  138. $renderer->emphasis_close();
  139. $renderer->cdata(' ' . $lang['by'] . ' ');
  140. $renderer->emaillink($info['email'], $info['author']);
  141. $renderer->linebreak();
  142. $renderer->cdata($info['desc']);
  143. $renderer->listcontent_close();
  144. $renderer->listitem_close();
  145. }
  146. $renderer->listu_close();
  147. }
  148. /**
  149. * list all installed plugins
  150. *
  151. * uses some of the original renderer methods
  152. *
  153. * @param Doku_Renderer_xhtml $renderer
  154. */
  155. protected function renderHelperMethods(Doku_Renderer_xhtml $renderer)
  156. {
  157. $plugins = plugin_list('helper');
  158. foreach ($plugins as $p) {
  159. if (!$po = plugin_load('helper', $p)) continue;
  160. if (!method_exists($po, 'getMethods')) continue;
  161. $methods = $po->getMethods();
  162. $info = $po->getInfo();
  163. $hid = $this->addToToc($info['name'], 2, $renderer);
  164. $doc = '<h2><a name="' . $hid . '" id="' . $hid . '">' . hsc($info['name']) . '</a></h2>';
  165. $doc .= '<div class="level2">';
  166. $doc .= '<p>' . strtr(hsc($info['desc']), array("\n" => "<br />")) . '</p>';
  167. $doc .= '<pre class="code">$' . $p . " = plugin_load('helper', '" . $p . "');</pre>";
  168. $doc .= '</div>';
  169. foreach ($methods as $method) {
  170. $title = '$' . $p . '->' . $method['name'] . '()';
  171. $hid = $this->addToToc($title, 3, $renderer);
  172. $doc .= '<h3><a name="' . $hid . '" id="' . $hid . '">' . hsc($title) . '</a></h3>';
  173. $doc .= '<div class="level3">';
  174. $doc .= '<div class="table"><table class="inline"><tbody>';
  175. $doc .= '<tr><th>Description</th><td colspan="2">' . $method['desc'] .
  176. '</td></tr>';
  177. if ($method['params']) {
  178. $c = count($method['params']);
  179. $doc .= '<tr><th rowspan="' . $c . '">Parameters</th><td>';
  180. $params = array();
  181. foreach ($method['params'] as $desc => $type) {
  182. $params[] = hsc($desc) . '</td><td>' . hsc($type);
  183. }
  184. $doc .= join('</td></tr><tr><td>', $params) . '</td></tr>';
  185. }
  186. if ($method['return']) {
  187. $doc .= '<tr><th>Return value</th><td>' . hsc(key($method['return'])) .
  188. '</td><td>' . hsc(current($method['return'])) . '</td></tr>';
  189. }
  190. $doc .= '</tbody></table></div>';
  191. $doc .= '</div>';
  192. }
  193. unset($po);
  194. $renderer->doc .= $doc;
  195. }
  196. }
  197. /**
  198. * lists all known syntax types and their registered modes
  199. *
  200. * @return string
  201. */
  202. protected function renderSyntaxTypes()
  203. {
  204. global $PARSER_MODES;
  205. $doc = '';
  206. $doc .= '<div class="table"><table class="inline"><tbody>';
  207. foreach ($PARSER_MODES as $mode => $modes) {
  208. $doc .= '<tr>';
  209. $doc .= '<td class="leftalign">';
  210. $doc .= $mode;
  211. $doc .= '</td>';
  212. $doc .= '<td class="leftalign">';
  213. $doc .= join(', ', $modes);
  214. $doc .= '</td>';
  215. $doc .= '</tr>';
  216. }
  217. $doc .= '</tbody></table></div>';
  218. return $doc;
  219. }
  220. /**
  221. * lists all known syntax modes and their sorting value
  222. *
  223. * @return string
  224. */
  225. protected function renderSyntaxModes()
  226. {
  227. $modes = p_get_parsermodes();
  228. $compactmodes = array();
  229. foreach ($modes as $mode) {
  230. $compactmodes[$mode['sort']][] = $mode['mode'];
  231. }
  232. $doc = '';
  233. $doc .= '<div class="table"><table class="inline"><tbody>';
  234. foreach ($compactmodes as $sort => $modes) {
  235. $rowspan = '';
  236. if (count($modes) > 1) {
  237. $rowspan = ' rowspan="' . count($modes) . '"';
  238. }
  239. foreach ($modes as $index => $mode) {
  240. $doc .= '<tr>';
  241. $doc .= '<td class="leftalign">';
  242. $doc .= $mode;
  243. $doc .= '</td>';
  244. if ($index === 0) {
  245. $doc .= '<td class="rightalign" ' . $rowspan . '>';
  246. $doc .= $sort;
  247. $doc .= '</td>';
  248. }
  249. $doc .= '</tr>';
  250. }
  251. }
  252. $doc .= '</tbody></table></div>';
  253. return $doc;
  254. }
  255. /**
  256. * Render all currently registered event handlers
  257. *
  258. * @param Doku_Renderer $renderer
  259. */
  260. protected function renderHooks(Doku_Renderer $renderer)
  261. {
  262. global $EVENT_HANDLER;
  263. $list = $EVENT_HANDLER->getEventHandlers();
  264. ksort($list);
  265. $renderer->listu_open();
  266. foreach ($list as $event => $handlers) {
  267. $renderer->listitem_open(1);
  268. $renderer->listcontent_open();
  269. $renderer->cdata($event);
  270. $renderer->listcontent_close();
  271. $renderer->listo_open();
  272. foreach ($handlers as $sequence) {
  273. foreach ($sequence as $handler) {
  274. $renderer->listitem_open(2);
  275. $renderer->listcontent_open();
  276. $renderer->cdata(get_class($handler[0]) . '::' . $handler[1] . '()');
  277. $renderer->listcontent_close();
  278. $renderer->listitem_close();
  279. }
  280. }
  281. $renderer->listo_close();
  282. $renderer->listitem_close();
  283. }
  284. $renderer->listu_close();
  285. }
  286. /**
  287. * Adds a TOC item
  288. *
  289. * @param string $text
  290. * @param int $level
  291. * @param Doku_Renderer_xhtml $renderer
  292. * @return string
  293. */
  294. protected function addToToc($text, $level, Doku_Renderer_xhtml $renderer)
  295. {
  296. global $conf;
  297. $hid = '';
  298. if (($level >= $conf['toptoclevel']) && ($level <= $conf['maxtoclevel'])) {
  299. $hid = $renderer->_headerToLink($text, true);
  300. $renderer->toc[] = array(
  301. 'hid' => $hid,
  302. 'title' => $text,
  303. 'type' => 'ul',
  304. 'level' => $level - $conf['toptoclevel'] + 1,
  305. );
  306. }
  307. return $hid;
  308. }
  309. }