replace xml_tag with HTMLElement

This commit is contained in:
Shish
2020-10-24 13:24:28 +01:00
parent c29fe0583f
commit 9b878d98d6
3 changed files with 21 additions and 43 deletions

View File

@ -1,5 +1,12 @@
<?php declare(strict_types=1);
use \MicroHTML\HTMLElement;
function TAGS(...$args) {return new HTMLElement("tags", $args);}
function TAG(...$args) {return new HTMLElement("tag", $args);}
function POSTS(...$args) {return new HTMLElement("posts", $args);}
function POST(...$args) {return new HTMLElement("post", $args);}
class DanbooruApi extends Extension
{
public function onPageRequest(PageRequestEvent $event)
@ -14,10 +21,10 @@ class DanbooruApi extends Extension
$this->api_add_post();
} elseif ($event->page_matches("api/danbooru/find_posts") || $event->page_matches("api/danbooru/post/index.xml")) {
$page->set_mime(MimeType::XML_APPLICATION);
$page->set_data($this->api_find_posts());
$page->set_data((string)$this->api_find_posts());
} elseif ($event->page_matches("api/danbooru/find_tags")) {
$page->set_mime(MimeType::XML_APPLICATION);
$page->set_data($this->api_find_tags());
$page->set_data((string)$this->api_find_tags());
}
// Hackery for danbooruup 0.3.2 providing the wrong view url. This simply redirects to the proper
@ -37,7 +44,7 @@ class DanbooruApi extends Extension
* Authenticates a user based on the contents of the login and password parameters
* or makes them anonymous. Does not set any cookies or anything permanent.
*/
private function authenticate_user()
private function authenticate_user(): void
{
global $config, $user;
@ -66,7 +73,7 @@ class DanbooruApi extends Extension
* - tags: any typical tag query. See Tag#parse_query for details.
* - after_id: limit results to tags with an id number after after_id. Useful if you only want to refresh
*/
private function api_find_tags(): string
private function api_find_tags(): HTMLElement
{
global $database;
$results = [];
@ -110,16 +117,15 @@ class DanbooruApi extends Extension
}
// Tag results collected, build XML output
$xml = "<tags>\n";
$xml = TAGS();
foreach ($results as $tag) {
$xml .= xml_tag("tag", [
$xml->appendChild(TAG([
"type" => "0",
"counts" => $tag[0],
"name" => $tag[1],
"id" => $tag[2],
]);
]));
}
$xml .= "</tags>";
return $xml;
}
@ -137,7 +143,7 @@ class DanbooruApi extends Extension
*
* #return string
*/
private function api_find_posts()
private function api_find_posts(): HTMLElement
{
$results = [];
@ -175,7 +181,7 @@ class DanbooruApi extends Extension
// Now we have the array $results filled with Image objects
// Let's display them
$xml = "<posts count=\"{$count}\" offset=\"{$start}\">\n";
$xml = POSTS(["count"=>$count, "offset"=>$start]);
foreach ($results as $img) {
// Sanity check to see if $img is really an image object
// If it isn't (e.g. someone requested an invalid md5 or id), break out of the this
@ -185,7 +191,7 @@ class DanbooruApi extends Extension
$taglist = $img->get_tag_list();
$owner = $img->get_owner();
$previewsize = get_thumbnail_size($img->width, $img->height);
$xml .= xml_tag("post", [
$xml->appendChild(TAG([
"id" => $img->id,
"md5" => $img->hash,
"file_name" => $img->filename,
@ -202,9 +208,8 @@ class DanbooruApi extends Extension
"source" => $img->source,
"score" => 0,
"author" => $owner->name
]);
]));
}
$xml .= "</posts>";
return $xml;
}
@ -235,7 +240,7 @@ class DanbooruApi extends Extension
* Get:
* - Redirected to the newly uploaded post.
*/
private function api_add_post()
private function api_add_post(): void
{
global $user, $page;
$danboorup_kludge = 1; // danboorup for firefox makes broken links out of location: /path