You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

342 lines
11 KiB

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