• R/O
  • HTTP
  • SSH
  • HTTPS

pukiwiki: Commit


Commit MetaInfo

Revisionb44cc2c343f4af5cbab6aa1b3bdb5b8cc9750abf (tree)
Time2018-06-01 06:01:37
Authorumorigu <umorigu@gmai...>
Commiterumorigu

Log Message

BugTrack/2475 template plugin: Return template pages in JSON format

Change Summary

Incremental Difference

--- a/lib/html.php
+++ b/lib/html.php
@@ -313,20 +313,17 @@ EOS;
313313 // Show 'edit' form
314314 function edit_form($page, $postdata, $digest = FALSE, $b_template = TRUE)
315315 {
316- global $vars, $rows, $cols, $hr, $function_freeze;
316+ global $vars, $rows, $cols;
317317 global $_btn_preview, $_btn_repreview, $_btn_update, $_btn_cancel, $_msg_help;
318- global $whatsnew, $_btn_template, $_btn_load, $load_template_func;
318+ global $_btn_template, $_btn_load, $load_template_func;
319319 global $notimeupdate;
320- global $_title_list;
321320 global $_msg_edit_cancel_confirm, $_msg_edit_unloadbefore_message;
322321 global $rule_page;
323322
324323 $script = get_base_uri();
325324 // Newly generate $digest or not
326325 if ($digest === FALSE) $digest = md5(join('', get_source($page)));
327-
328326 $refer = $template = '';
329-
330327 // Add plugin
331328 $addtag = $add_top = '';
332329 if(isset($vars['add'])) {
@@ -339,46 +336,14 @@ function edit_form($page, $postdata, $digest = FALSE, $b_template = TRUE)
339336 '<span class="small">' . $_btn_addtop . '</span>' .
340337 '</label>';
341338 }
342-
343339 if($load_template_func && $b_template) {
344- $tpage_names = array(); // Pages marked as template
345- $template_page = ':config/Templates';
346- $page_max = 100;
347- foreach(get_source($template_page) as $_templates) {
348- $m = array();
349- if (! preg_match('#\-\s*\[\[([^\[\]]+)\]\]#', $_templates, $m)) continue;
350- $tpage = preg_replace('#^./#', "$template_page/", $m[1]);
351- if (! is_page($tpage)) continue;
352- $tpage_names[] = $tpage;
353- }
354- $page_names = array();
355- $page_list = get_existpages();
356- if (count($page_list) > $page_max) {
357- // Extract only template name pages
358- $target_pages = array();
359- foreach ($page_list as $_page) {
360- if (preg_match('/template/i', $_page)) {
361- $target_pages[] = $_page;
362- }
363- }
364- } else {
365- $target_pages = $page_list;
366- }
367- foreach ($target_pages as $_page) {
368- if ($_page == $whatsnew || check_non_list($_page) ||
369- !is_page_readable($_page)) {
370- continue;
371- }
372- $tpage_names[] = $_page;
373- }
374- $tpage_names2 = array_values(array_unique($tpage_names));
375- natcasesort($tpage_names2);
340+ $template_page_list = get_template_page_list();
376341 $tpages = array(); // Template pages
377- foreach($tpage_names2 as $p) {
342+ foreach($template_page_list as $p) {
378343 $ps = htmlsc($p);
379344 $tpages[] = ' <option value="' . $ps . '">' . $ps . '</option>';
380345 }
381- if (count($tpage_names2) > 0) {
346+ if (count($template_page_list) > 0) {
382347 $s_tpages = join("\n", $tpages);
383348 } else {
384349 $s_tpages = ' <option value="">(no template pages)</option>';
@@ -460,6 +425,47 @@ EOD;
460425 return $body;
461426 }
462427
428+/**
429+ * Get template page list.
430+ */
431+function get_template_page_list()
432+{
433+ global $whatsnew;
434+ $tpage_names = array(); // Pages marked as template
435+ $template_page = ':config/Templates';
436+ $page_max = 100;
437+ foreach(get_source($template_page) as $_templates) {
438+ $m = array();
439+ if (! preg_match('#\-\s*\[\[([^\[\]]+)\]\]#', $_templates, $m)) continue;
440+ $tpage = preg_replace('#^./#', "$template_page/", $m[1]);
441+ if (! is_page($tpage)) continue;
442+ $tpage_names[] = $tpage;
443+ }
444+ $page_names = array();
445+ $page_list = get_existpages();
446+ if (count($page_list) > $page_max) {
447+ // Extract only template name pages
448+ $target_pages = array();
449+ foreach ($page_list as $_page) {
450+ if (preg_match('/template/i', $_page)) {
451+ $target_pages[] = $_page;
452+ }
453+ }
454+ } else {
455+ $target_pages = $page_list;
456+ }
457+ foreach ($target_pages as $_page) {
458+ if ($_page == $whatsnew || check_non_list($_page) ||
459+ !is_page_readable($_page)) {
460+ continue;
461+ }
462+ $tpage_names[] = $_page;
463+ }
464+ $tempalte_page_list = array_values(array_unique($tpage_names));
465+ natcasesort($tempalte_page_list);
466+ return $tempalte_page_list;
467+}
468+
463469 // Related pages
464470 function make_related($page, $tag = '')
465471 {
--- a/plugin/template.inc.php
+++ b/plugin/template.inc.php
@@ -20,9 +20,13 @@ function plugin_template_action()
2020
2121 $script = get_base_uri();
2222 if (PKWK_READONLY) die_message('PKWK_READONLY prohibits editing');
23- if (! isset($vars['refer']) || ! is_page($vars['refer']))
23+ if (! isset($vars['refer']) || ! is_page($vars['refer'])) {
24+ if (isset($vars['action']) && $vars['action'] === 'list') {
25+ plugin_template_output_list();
26+ exit;
27+ }
2428 return FALSE;
25-
29+ }
2630 $refer = $vars['refer'];
2731 // Ensure page is readable, or show Login UI and exit
2832 ensure_page_readable($refer);
@@ -95,3 +99,20 @@ EOD;
9599
96100 return $retvar;
97101 }
102+
103+function plugin_template_output_list()
104+{
105+ $template_page_key = 'template_pages';
106+ $empty_result = '{"' . $template_page_key . '":[]}';
107+ header('Content-Type: application/json; charset=UTF-8');
108+ // PHP 5.4+
109+ $enabled = defined('JSON_UNESCAPED_UNICODE') && defined('PKWK_UTF8_ENABLE');
110+ if (!$enabled) {
111+ print($empty_result);
112+ exit;
113+ }
114+ $template_pages = array_values(get_template_page_list());
115+ $ar = array($template_page_key => $template_pages);
116+ print(json_encode($ar, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
117+ exit;
118+}
Show on old repository browser