リビジョン | b44cc2c343f4af5cbab6aa1b3bdb5b8cc9750abf (tree) |
---|---|
日時 | 2018-06-01 06:01:37 |
作者 | umorigu <umorigu@gmai...> |
コミッター | umorigu |
BugTrack/2475 template plugin: Return template pages in JSON format
@@ -313,20 +313,17 @@ EOS; | ||
313 | 313 | // Show 'edit' form |
314 | 314 | function edit_form($page, $postdata, $digest = FALSE, $b_template = TRUE) |
315 | 315 | { |
316 | - global $vars, $rows, $cols, $hr, $function_freeze; | |
316 | + global $vars, $rows, $cols; | |
317 | 317 | 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; | |
319 | 319 | global $notimeupdate; |
320 | - global $_title_list; | |
321 | 320 | global $_msg_edit_cancel_confirm, $_msg_edit_unloadbefore_message; |
322 | 321 | global $rule_page; |
323 | 322 | |
324 | 323 | $script = get_base_uri(); |
325 | 324 | // Newly generate $digest or not |
326 | 325 | if ($digest === FALSE) $digest = md5(join('', get_source($page))); |
327 | - | |
328 | 326 | $refer = $template = ''; |
329 | - | |
330 | 327 | // Add plugin |
331 | 328 | $addtag = $add_top = ''; |
332 | 329 | if(isset($vars['add'])) { |
@@ -339,46 +336,14 @@ function edit_form($page, $postdata, $digest = FALSE, $b_template = TRUE) | ||
339 | 336 | '<span class="small">' . $_btn_addtop . '</span>' . |
340 | 337 | '</label>'; |
341 | 338 | } |
342 | - | |
343 | 339 | 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(); | |
376 | 341 | $tpages = array(); // Template pages |
377 | - foreach($tpage_names2 as $p) { | |
342 | + foreach($template_page_list as $p) { | |
378 | 343 | $ps = htmlsc($p); |
379 | 344 | $tpages[] = ' <option value="' . $ps . '">' . $ps . '</option>'; |
380 | 345 | } |
381 | - if (count($tpage_names2) > 0) { | |
346 | + if (count($template_page_list) > 0) { | |
382 | 347 | $s_tpages = join("\n", $tpages); |
383 | 348 | } else { |
384 | 349 | $s_tpages = ' <option value="">(no template pages)</option>'; |
@@ -460,6 +425,47 @@ EOD; | ||
460 | 425 | return $body; |
461 | 426 | } |
462 | 427 | |
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 | + | |
463 | 469 | // Related pages |
464 | 470 | function make_related($page, $tag = '') |
465 | 471 | { |
@@ -20,9 +20,13 @@ function plugin_template_action() | ||
20 | 20 | |
21 | 21 | $script = get_base_uri(); |
22 | 22 | 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 | + } | |
24 | 28 | return FALSE; |
25 | - | |
29 | + } | |
26 | 30 | $refer = $vars['refer']; |
27 | 31 | // Ensure page is readable, or show Login UI and exit |
28 | 32 | ensure_page_readable($refer); |
@@ -95,3 +99,20 @@ EOD; | ||
95 | 99 | |
96 | 100 | return $retvar; |
97 | 101 | } |
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 | +} |