1
0
Fork 0
mirror of https://github.com/sbrl/Pepperminty-Wiki.git synced 2024-11-22 04:23:01 +00:00

Add json & plain text formats to list-tags

This commit is contained in:
Starbeamrainbowlabs 2017-09-18 22:07:17 +01:00
parent b9eaa1931f
commit 4935bb2ecf
3 changed files with 147 additions and 67 deletions

View file

@ -2453,7 +2453,7 @@ function render_sidebar($pageindex, $root_pagename = "")
continue; continue;
// If the page already appears on the sidebar, skip it // 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; continue;
// If the part of the current pagename that comes after the root // If the part of the current pagename that comes after the root
@ -6565,6 +6565,7 @@ register_module([
* @apiPermission Anonymous * @apiPermission Anonymous
* *
* @apiParam {string} tag Optional. If provided a list of all the pages with that tag is returned instead. * @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,21 +6578,23 @@ register_module([
add_action("list-tags", function() { add_action("list-tags", function() {
global $pageindex, $settings; 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", "<p>Error: The format '$format' is not currently supported by $settings->sitename. Supported formats: " . implode(", ", $supported_formats) . "."));
}
if(!isset($_GET["tag"])) if(!isset($_GET["tag"]))
{ {
// Render a list of all tags // Render a list of all tags
$all_tags = []; $all_tags = get_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;
}
}
sort($all_tags, SORT_NATURAL); sort($all_tags, SORT_NATURAL);
switch($format) {
case "html":
$content = "<h1>All tags</h1> $content = "<h1>All tags</h1>
<ul class='tag-list'>\n"; <ul class='tag-list'>\n";
foreach($all_tags as $tag) foreach($all_tags as $tag)
@ -6601,6 +6604,16 @@ register_module([
$content .= "</ul>\n"; $content .= "</ul>\n";
exit(page_renderer::render("All tags - $settings->sitename", $content)); exit(page_renderer::render("All tags - $settings->sitename", $content));
break;
case "json":
header("content-type: application/json");
exit(json_encode($all_tags, JSON_PRETTY_PRINT));
case "text":
header("content-type: text/plain");
exit(implode("\n", $all_tags));
}
} }
$tag = $_GET["tag"]; $tag = $_GET["tag"];
@ -6616,12 +6629,25 @@ register_module([
$pagelist[] = $pagename; $pagelist[] = $pagename;
} }
switch($format)
{
case "html":
$content = "<h1>Tag List: $tag</h1>\n"; $content = "<h1>Tag List: $tag</h1>\n";
$content .= generate_page_list($pagelist); $content .= generate_page_list($pagelist);
$content .= "<p>(<a href='?action=list-tags'>All tags</a>)</p>\n"; $content .= "<p>(<a href='?action=list-tags'>All tags</a>)</p>\n";
exit(page_renderer::render("$tag - Tag List - $settings->sitename", $content)); 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));
}
}); });
statistic_add([ statistic_add([
@ -6630,18 +6656,9 @@ register_module([
"type" => "scalar", "type" => "scalar",
"update" => function($old_data) { "update" => function($old_data) {
global $pageindex; 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 = new stdClass(); // value, state, completed
$result->value = count($all_tags); $result->value = count(get_all_tags());
$result->completed = true; $result->completed = true;
return $result; 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. * Renders a list of pages as HTML.
* @package page-list * @package page-list

View file

@ -50,7 +50,7 @@
"author": "Starbeamrainbowlabs", "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.", "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", "id": "extra-sidebar",
"lastupdate": 1505512998, "lastupdate": 1505768459,
"optional": false "optional": false
}, },
{ {
@ -194,7 +194,7 @@
"author": "Starbeamrainbowlabs", "author": "Starbeamrainbowlabs",
"description": "Adds a page that lists all the pages in the index along with their metadata.", "description": "Adds a page that lists all the pages in the index along with their metadata.",
"id": "page-list", "id": "page-list",
"lastupdate": 1505579417, "lastupdate": 1505768745,
"optional": false "optional": false
}, },
{ {

View file

@ -44,6 +44,7 @@ register_module([
* @apiPermission Anonymous * @apiPermission Anonymous
* *
* @apiParam {string} tag Optional. If provided a list of all the pages with that tag is returned instead. * @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,21 +57,23 @@ register_module([
add_action("list-tags", function() { add_action("list-tags", function() {
global $pageindex, $settings; 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", "<p>Error: The format '$format' is not currently supported by $settings->sitename. Supported formats: " . implode(", ", $supported_formats) . "."));
}
if(!isset($_GET["tag"])) if(!isset($_GET["tag"]))
{ {
// Render a list of all tags // Render a list of all tags
$all_tags = []; $all_tags = get_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;
}
}
sort($all_tags, SORT_NATURAL); sort($all_tags, SORT_NATURAL);
switch($format) {
case "html":
$content = "<h1>All tags</h1> $content = "<h1>All tags</h1>
<ul class='tag-list'>\n"; <ul class='tag-list'>\n";
foreach($all_tags as $tag) foreach($all_tags as $tag)
@ -80,6 +83,16 @@ register_module([
$content .= "</ul>\n"; $content .= "</ul>\n";
exit(page_renderer::render("All tags - $settings->sitename", $content)); exit(page_renderer::render("All tags - $settings->sitename", $content));
break;
case "json":
header("content-type: application/json");
exit(json_encode($all_tags, JSON_PRETTY_PRINT));
case "text":
header("content-type: text/plain");
exit(implode("\n", $all_tags));
}
} }
$tag = $_GET["tag"]; $tag = $_GET["tag"];
@ -95,12 +108,25 @@ register_module([
$pagelist[] = $pagename; $pagelist[] = $pagename;
} }
switch($format)
{
case "html":
$content = "<h1>Tag List: $tag</h1>\n"; $content = "<h1>Tag List: $tag</h1>\n";
$content .= generate_page_list($pagelist); $content .= generate_page_list($pagelist);
$content .= "<p>(<a href='?action=list-tags'>All tags</a>)</p>\n"; $content .= "<p>(<a href='?action=list-tags'>All tags</a>)</p>\n";
exit(page_renderer::render("$tag - Tag List - $settings->sitename", $content)); 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));
}
}); });
statistic_add([ statistic_add([
@ -109,18 +135,9 @@ register_module([
"type" => "scalar", "type" => "scalar",
"update" => function($old_data) { "update" => function($old_data) {
global $pageindex; 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 = new stdClass(); // value, state, completed
$result->value = count($all_tags); $result->value = count(get_all_tags());
$result->completed = true; $result->completed = true;
return $result; 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. * Renders a list of pages as HTML.
* @package page-list * @package page-list