Add comments-fetch action for #138

This commit is contained in:
Starbeamrainbowlabs 2018-04-17 20:16:59 +01:00
parent 3b43493bcd
commit 46d520644a
Signed by: sbrl
GPG Key ID: 1BE5172E637709C2
4 changed files with 86 additions and 1 deletions

View File

@ -9,6 +9,7 @@ This file holds the changelog for Pepperminty Wiki. This is the master list of t
- Hyperlinked image preview on file pages to the original image (#153)
- [Rest API] Added support for the `mode` parameter to the `random` action
- [Rest API] Added `format` parameter to `recentchanges` action
- [Rest API] Added `comments-fetch` action to return a page's comments as JSON
### Fixed
- Fixed various issues with both the module api & the rest api docs.

View File

@ -2696,6 +2696,48 @@ register_module([
exit(page_renderer::render_main("Comment Deleted - $settings->sitename", "<p>The comment with the id <code>" . htmlentities($target_id) . "</code> on the page <em>$env->page</em> has been deleted successfully. <a href='?page=" . rawurlencode($env->page) . "&redirect=no'>Go back</a> to " . htmlentities($env->page) . ".</p>"));
});
/**
* @api {post} ?action=comments-fetch&page={page_name} Fetch the comments for a page
* @apiName CommentsFetch
* @apiGroup Comment
* @apiPermission Anonymous
* @apiDescription Fetches the comments for the specified page. Returns them in a nested JSON structure.
*
* @apiUse PageParameter
* @apiError PageNoteFound The page to fetch the comments for was not found.
*/
/*
* ██████ ██████ ███ ███ ███ ███ ███████ ███ ██ ████████
* ██ ██ ██ ████ ████ ████ ████ ██ ████ ██ ██
* ██ ██ ██ ██ ████ ██ ██ ████ ██ █████ ██ ██ ██ ██ █████
* ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
* ██████ ██████ ██ ██ ██ ██ ███████ ██ ████ ██
*
* ███████ ███████ ████████ ██████ ██ ██
* ██ ██ ██ ██ ██ ██
* █████ █████ ██ ██ ███████
* ██ ██ ██ ██ ██ ██
* ██ ███████ ██ ██████ ██ ██
*/
add_action("comments-fetch", function() {
global $env;
$comments_filename = get_comment_filename($env->page);
if(!file_exists($comments_filename)) {
http_response_code(404);
header("content-type: text/plain");
exit("Error: No comments file was found for the page '$env->page'.");
}
$comments_data = json_decode(file_get_contents($comments_filename));
$result = json_encode($comments_data);
header("content-type: application/json");
header("content-length: " . strlen($result));
exit($result);
});
if($env->action == "view") {
page_renderer::register_part_preprocessor(function(&$parts) {

View File

@ -59,7 +59,7 @@
"author": "Starbeamrainbowlabs",
"description": "Adds threaded comments to the bottom of every page.",
"id": "feature-comments",
"lastupdate": 1511450038,
"lastupdate": 1523989232,
"optional": false
},
{

View File

@ -192,6 +192,48 @@ register_module([
exit(page_renderer::render_main("Comment Deleted - $settings->sitename", "<p>The comment with the id <code>" . htmlentities($target_id) . "</code> on the page <em>$env->page</em> has been deleted successfully. <a href='?page=" . rawurlencode($env->page) . "&redirect=no'>Go back</a> to " . htmlentities($env->page) . ".</p>"));
});
/**
* @api {post} ?action=comments-fetch&page={page_name} Fetch the comments for a page
* @apiName CommentsFetch
* @apiGroup Comment
* @apiPermission Anonymous
* @apiDescription Fetches the comments for the specified page. Returns them in a nested JSON structure.
*
* @apiUse PageParameter
* @apiError PageNoteFound The page to fetch the comments for was not found.
*/
/*
* ██████ ██████ ███ ███ ███ ███ ███████ ███ ██ ████████
* ██ ██ ██ ████ ████ ████ ████ ██ ████ ██ ██
* ██ ██ ██ ██ ████ ██ ██ ████ ██ █████ ██ ██ ██ ██ █████
* ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
* ██████ ██████ ██ ██ ██ ██ ███████ ██ ████ ██
*
* ███████ ███████ ████████ ██████ ██ ██
* ██ ██ ██ ██ ██ ██
* █████ █████ ██ ██ ███████
* ██ ██ ██ ██ ██ ██
* ██ ███████ ██ ██████ ██ ██
*/
add_action("comments-fetch", function() {
global $env;
$comments_filename = get_comment_filename($env->page);
if(!file_exists($comments_filename)) {
http_response_code(404);
header("content-type: text/plain");
exit("Error: No comments file was found for the page '$env->page'.");
}
$comments_data = json_decode(file_get_contents($comments_filename));
$result = json_encode($comments_data);
header("content-type: application/json");
header("content-length: " . strlen($result));
exit($result);
});
if($env->action == "view") {
page_renderer::register_part_preprocessor(function(&$parts) {