From 4935bb2ecf73465b01f5bc94637d53cc4d069650 Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Mon, 18 Sep 2017 22:07:17 +0100 Subject: [PATCH] Add json & plain text formats to list-tags --- build/index.php | 106 +++++++++++++++++++++++++++++------------- module_index.json | 4 +- modules/page-list.php | 104 ++++++++++++++++++++++++++++------------- 3 files changed, 147 insertions(+), 67 deletions(-) diff --git a/build/index.php b/build/index.php index daa3011..2e47aea 100644 --- a/build/index.php +++ b/build/index.php @@ -2453,7 +2453,7 @@ function render_sidebar($pageindex, $root_pagename = "") continue; // If the page already appears on the sidebar, skip it - if(preg_match("/>$pagename<\a>/m", $result) === 1) + if(preg_match("/>" . preg_quote($pagename) . "<\a>/m", $result) === 1) continue; // If the part of the current pagename that comes after the root @@ -6565,6 +6565,7 @@ register_module([ * @apiPermission Anonymous * * @apiParam {string} tag Optional. If provided a list of all the pages with that tag is returned instead. + * @apiParam {string} format Optional. If specified sets the format of the returned result. Supported values: html, json. Default: html */ /* @@ -6577,30 +6578,42 @@ register_module([ add_action("list-tags", function() { global $pageindex, $settings; + $supported_formats = [ "html", "json", "text" ]; + $format = $_GET["format"] ?? "html"; + + if(!in_array($format, $supported_formats)) { + http_response_code(400); + exit(page_renderer::render_main("Format error - $settings->sitename", "

Error: The format '$format' is not currently supported by $settings->sitename. Supported formats: " . implode(", ", $supported_formats) . ".")); + } + if(!isset($_GET["tag"])) { // Render a list of all tags - $all_tags = []; - foreach($pageindex as $entry) - { - if(!isset($entry->tags)) continue; - foreach($entry->tags as $tag) - { - if(!in_array($tag, $all_tags)) $all_tags[] = $tag; - } - } + $all_tags = get_all_tags(); sort($all_tags, SORT_NATURAL); - $content = "

All tags

- \n"; - - exit(page_renderer::render("All tags - $settings->sitename", $content)); } $tag = $_GET["tag"]; @@ -6616,12 +6629,25 @@ register_module([ $pagelist[] = $pagename; } - $content = "

Tag List: $tag

\n"; - $content .= generate_page_list($pagelist); + switch($format) + { + case "html": + $content = "

Tag List: $tag

\n"; + $content .= generate_page_list($pagelist); + + $content .= "

(All tags)

\n"; + + exit(page_renderer::render("$tag - Tag List - $settings->sitename", $content)); + + case "json": + header("content-type: application/json"); + exit(json_encode($pagelist, JSON_PRETTY_PRINT)); + + case "text": + header("content-type: text/plain"); + exit(implode("\n", $pagelist)); + } - $content .= "

(All tags)

\n"; - - exit(page_renderer::render("$tag - Tag List - $settings->sitename", $content)); }); statistic_add([ @@ -6630,18 +6656,9 @@ register_module([ "type" => "scalar", "update" => function($old_data) { global $pageindex; - $all_tags = []; - foreach($pageindex as $page_entry) { - if(empty($page_entry->tags)) - continue; - - foreach($page_entry->tags as $tag) { - if(!in_array($tag, $all_tags)) $all_tags[] = $tag; - } - } $result = new stdClass(); // value, state, completed - $result->value = count($all_tags); + $result->value = count(get_all_tags()); $result->completed = true; return $result; } @@ -6712,6 +6729,29 @@ register_module([ } ]); +/** + * Gets a list of all the tags currently used across the wiki. + * @package page-list + * @since v0.15 + * @return string[] A list of all unique tags present on all pages across the wiki. + */ +function get_all_tags() +{ + global $pageindex; + + $all_tags = []; + foreach($pageindex as $page_entry) { + if(empty($page_entry->tags)) + continue; + + foreach($page_entry->tags as $tag) { + if(!in_array($tag, $all_tags)) + $all_tags[] = $tag; + } + } + return $all_tags; +} + /** * Renders a list of pages as HTML. * @package page-list diff --git a/module_index.json b/module_index.json index 76a7cbc..7f0f14f 100755 --- a/module_index.json +++ b/module_index.json @@ -50,7 +50,7 @@ "author": "Starbeamrainbowlabs", "description": "Adds a sidebar to the left hand side of every page. Add '$settings->sidebar_show = true;' to your configuration, or append '&sidebar=yes' to the url to enable. Adding to the url sets a cookie to remember your setting.", "id": "extra-sidebar", - "lastupdate": 1505512998, + "lastupdate": 1505768459, "optional": false }, { @@ -194,7 +194,7 @@ "author": "Starbeamrainbowlabs", "description": "Adds a page that lists all the pages in the index along with their metadata.", "id": "page-list", - "lastupdate": 1505579417, + "lastupdate": 1505768745, "optional": false }, { diff --git a/modules/page-list.php b/modules/page-list.php index b5c9e3f..98f74e4 100644 --- a/modules/page-list.php +++ b/modules/page-list.php @@ -44,6 +44,7 @@ register_module([ * @apiPermission Anonymous * * @apiParam {string} tag Optional. If provided a list of all the pages with that tag is returned instead. + * @apiParam {string} format Optional. If specified sets the format of the returned result. Supported values: html, json. Default: html */ /* @@ -56,30 +57,42 @@ register_module([ add_action("list-tags", function() { global $pageindex, $settings; + $supported_formats = [ "html", "json", "text" ]; + $format = $_GET["format"] ?? "html"; + + if(!in_array($format, $supported_formats)) { + http_response_code(400); + exit(page_renderer::render_main("Format error - $settings->sitename", "

Error: The format '$format' is not currently supported by $settings->sitename. Supported formats: " . implode(", ", $supported_formats) . ".")); + } + if(!isset($_GET["tag"])) { // Render a list of all tags - $all_tags = []; - foreach($pageindex as $entry) - { - if(!isset($entry->tags)) continue; - foreach($entry->tags as $tag) - { - if(!in_array($tag, $all_tags)) $all_tags[] = $tag; - } - } + $all_tags = get_all_tags(); sort($all_tags, SORT_NATURAL); - $content = "

All tags

- \n"; - - exit(page_renderer::render("All tags - $settings->sitename", $content)); } $tag = $_GET["tag"]; @@ -95,12 +108,25 @@ register_module([ $pagelist[] = $pagename; } - $content = "

Tag List: $tag

\n"; - $content .= generate_page_list($pagelist); + switch($format) + { + case "html": + $content = "

Tag List: $tag

\n"; + $content .= generate_page_list($pagelist); + + $content .= "

(All tags)

\n"; + + exit(page_renderer::render("$tag - Tag List - $settings->sitename", $content)); + + case "json": + header("content-type: application/json"); + exit(json_encode($pagelist, JSON_PRETTY_PRINT)); + + case "text": + header("content-type: text/plain"); + exit(implode("\n", $pagelist)); + } - $content .= "

(All tags)

\n"; - - exit(page_renderer::render("$tag - Tag List - $settings->sitename", $content)); }); statistic_add([ @@ -109,18 +135,9 @@ register_module([ "type" => "scalar", "update" => function($old_data) { global $pageindex; - $all_tags = []; - foreach($pageindex as $page_entry) { - if(empty($page_entry->tags)) - continue; - - foreach($page_entry->tags as $tag) { - if(!in_array($tag, $all_tags)) $all_tags[] = $tag; - } - } $result = new stdClass(); // value, state, completed - $result->value = count($all_tags); + $result->value = count(get_all_tags()); $result->completed = true; return $result; } @@ -191,6 +208,29 @@ register_module([ } ]); +/** + * Gets a list of all the tags currently used across the wiki. + * @package page-list + * @since v0.15 + * @return string[] A list of all unique tags present on all pages across the wiki. + */ +function get_all_tags() +{ + global $pageindex; + + $all_tags = []; + foreach($pageindex as $page_entry) { + if(empty($page_entry->tags)) + continue; + + foreach($page_entry->tags as $tag) { + if(!in_array($tag, $all_tags)) + $all_tags[] = $tag; + } + } + return $all_tags; +} + /** * Renders a list of pages as HTML. * @package page-list