1
0
Fork 0
mirror of https://github.com/sbrl/Pepperminty-Wiki.git synced 2024-12-22 13:45:02 +00:00

Add api status action for #138

This commit is contained in:
Starbeamrainbowlabs 2017-06-26 11:36:04 +01:00
parent efce5d2bb6
commit 7a326cb52f
4 changed files with 142 additions and 0 deletions

View file

@ -833,6 +833,23 @@ function system_extension_mime_type($ext) {
$ext = strtolower($ext);
return isset($types[$ext]) ? $types[$ext] : null;
}
/**
* Figures out whether a given http accepts header contains a
* specified mime type.
* @param string $accept_header The accept header to search.
* @param string $mime_type The mime type to search for.
* @return bool Whether the specified mime type was found
* in the specified accepts header.
*/
function accept_contains_mime($accept_header, $mime_type)
{
$accepted_mimes = explode(",", $accept_header);
foreach($accepted_mimes as $accepted_mime) {
if(explode(";", $accepted_mime)[0] == $mime_type)
return true;
}
return false;
}
/**
* Generates a stack trace.
@ -1988,6 +2005,56 @@ register_module([
register_module([
"name" => "API status",
"version" => "0.1",
"author" => "Starbeamrainbowlabs",
"description" => "Provides a basic JSON status action that provices a few useful bits of information for API consumption.",
"id" => "api-status",
"code" => function() {
global $settings;
/**
* @api {get} ?action=raw&page={pageName} Get the raw source code of a page
* @apiName RawSource
* @apiGroup Page
* @apiPermission Anonymous
*
* @apiParam {string} page The page to return the source of.
*/
add_action("status", function() {
global $version, $env, $settings, $actions;
// Make sure the client can accept JSON
if(!accept_contains_mime($_SERVER["HTTP_ACCEPT"] ?? "application/json", "application/json")) {
http_response_code(406);
header("content-type: text/plain");
exit("Unfortunately, this API is currently only available in application/json at the moment, which you haven't indicated you accept in your http accept header. You said this in your accept header:\n" . $_SERVER["HTTP_ACCEPT"]);
}
$action_names = array_keys(get_object_vars($actions));
sort($action_names);
$result = new stdClass();
$result->status = "ok";
$result->version = $version;
$result->available_actions = $action_names;
$result->wiki_name = $settings->sitename;
$result->logo_url = $settings->favicon;
header("content-type: application/json");
exit(json_encode($result, JSON_PRETTY_PRINT) . "\n");
});
add_help_section("960-api-status", "Wiki Status API", "<p></p>");
}
]);
register_module([
"name" => "Sidebar",

View file

@ -480,6 +480,23 @@ function system_extension_mime_type($ext) {
$ext = strtolower($ext);
return isset($types[$ext]) ? $types[$ext] : null;
}
/**
* Figures out whether a given http accepts header contains a
* specified mime type.
* @param string $accept_header The accept header to search.
* @param string $mime_type The mime type to search for.
* @return bool Whether the specified mime type was found
* in the specified accepts header.
*/
function accept_contains_mime($accept_header, $mime_type)
{
$accepted_mimes = explode(",", $accept_header);
foreach($accepted_mimes as $accepted_mime) {
if(explode(";", $accepted_mime)[0] == $mime_type)
return true;
}
return false;
}
/**
* Generates a stack trace.

View file

@ -35,6 +35,15 @@
"lastupdate": 1476906745,
"optional": false
},
{
"name": "API status",
"version": "0.1",
"author": "Starbeamrainbowlabs",
"description": "Provides a basic JSON status action that provices a few useful bits of information for API consumption.",
"id": "api-status",
"lastupdate": 1498472652,
"optional": false
},
{
"name": "Sidebar",
"version": "0.3",

49
modules/api-status.php Normal file
View file

@ -0,0 +1,49 @@
<?php
register_module([
"name" => "API status",
"version" => "0.1",
"author" => "Starbeamrainbowlabs",
"description" => "Provides a basic JSON status action that provices a few useful bits of information for API consumption.",
"id" => "api-status",
"code" => function() {
global $settings;
/**
* @api {get} ?action=raw&page={pageName} Get the raw source code of a page
* @apiName RawSource
* @apiGroup Page
* @apiPermission Anonymous
*
* @apiParam {string} page The page to return the source of.
*/
add_action("status", function() {
global $version, $env, $settings, $actions;
// Make sure the client can accept JSON
if(!accept_contains_mime($_SERVER["HTTP_ACCEPT"] ?? "application/json", "application/json")) {
http_response_code(406);
header("content-type: text/plain");
exit("Unfortunately, this API is currently only available in application/json at the moment, which you haven't indicated you accept in your http accept header. You said this in your accept header:\n" . $_SERVER["HTTP_ACCEPT"]);
}
$action_names = array_keys(get_object_vars($actions));
sort($action_names);
$result = new stdClass();
$result->status = "ok";
$result->version = $version;
$result->available_actions = $action_names;
$result->wiki_name = $settings->sitename;
$result->logo_url = $settings->favicon;
header("content-type: application/json");
exit(json_encode($result, JSON_PRETTY_PRINT) . "\n");
});
add_help_section("960-api-status", "Wiki Status API", "<p></p>");
}
]);
?>