1
0
Fork 0
mirror of https://github.com/sbrl/Pepperminty-Wiki.git synced 2024-12-22 13:45:02 +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;
// 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", "<p>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 = "<h1>All tags</h1>
<ul class='tag-list'>\n";
foreach($all_tags as $tag)
{
$content .= " <li><a href='?action=list-tags&tag=" . rawurlencode($tag) . "' class='mini-tag'>$tag</a></li>\n";
switch($format) {
case "html":
$content = "<h1>All tags</h1>
<ul class='tag-list'>\n";
foreach($all_tags as $tag)
{
$content .= " <li><a href='?action=list-tags&tag=" . rawurlencode($tag) . "' class='mini-tag'>$tag</a></li>\n";
}
$content .= "</ul>\n";
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));
}
$content .= "</ul>\n";
exit(page_renderer::render("All tags - $settings->sitename", $content));
}
$tag = $_GET["tag"];
@ -6616,12 +6629,25 @@ register_module([
$pagelist[] = $pagename;
}
$content = "<h1>Tag List: $tag</h1>\n";
$content .= generate_page_list($pagelist);
switch($format)
{
case "html":
$content = "<h1>Tag List: $tag</h1>\n";
$content .= generate_page_list($pagelist);
$content .= "<p>(<a href='?action=list-tags'>All tags</a>)</p>\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 .= "<p>(<a href='?action=list-tags'>All tags</a>)</p>\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

View file

@ -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
},
{

View file

@ -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", "<p>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 = "<h1>All tags</h1>
<ul class='tag-list'>\n";
foreach($all_tags as $tag)
{
$content .= " <li><a href='?action=list-tags&tag=" . rawurlencode($tag) . "' class='mini-tag'>$tag</a></li>\n";
switch($format) {
case "html":
$content = "<h1>All tags</h1>
<ul class='tag-list'>\n";
foreach($all_tags as $tag)
{
$content .= " <li><a href='?action=list-tags&tag=" . rawurlencode($tag) . "' class='mini-tag'>$tag</a></li>\n";
}
$content .= "</ul>\n";
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));
}
$content .= "</ul>\n";
exit(page_renderer::render("All tags - $settings->sitename", $content));
}
$tag = $_GET["tag"];
@ -95,12 +108,25 @@ register_module([
$pagelist[] = $pagename;
}
$content = "<h1>Tag List: $tag</h1>\n";
$content .= generate_page_list($pagelist);
switch($format)
{
case "html":
$content = "<h1>Tag List: $tag</h1>\n";
$content .= generate_page_list($pagelist);
$content .= "<p>(<a href='?action=list-tags'>All tags</a>)</p>\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 .= "<p>(<a href='?action=list-tags'>All tags</a>)</p>\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