diff --git a/core/polyfills.php b/core/polyfills.php index 328ebca2..725abec3 100644 --- a/core/polyfills.php +++ b/core/polyfills.php @@ -190,8 +190,8 @@ function stream_file(string $file, int $start, int $end): void } } -if (!function_exists('http_parse_headers')) { #http://www.php.net/manual/en/function.http-parse-headers.php#112917 - +# http://www.php.net/manual/en/function.http-parse-headers.php#112917 +if (!function_exists('http_parse_headers')) { /** * #return string[] */ @@ -478,25 +478,6 @@ function clamp(?int $val, ?int $min=null, ?int $max=null): int return $val; } -function xml_tag(string $name, array $attrs=[], array $children=[]): string -{ - $xml = "<$name "; - foreach ($attrs as $k => $v) { - $xv = str_replace(''', ''', htmlspecialchars((string)$v, ENT_QUOTES)); - $xml .= "$k=\"$xv\" "; - } - if (count($children) > 0) { - $xml .= ">\n"; - foreach ($children as $child) { - $xml .= xml_tag($child); - } - $xml .= "\n"; - } else { - $xml .= "/>\n"; - } - return $xml; -} - /** * Original PHP code by Chirp Internet: www.chirp.com.au * Please acknowledge use of this code by including this header. diff --git a/core/tests/polyfills.test.php b/core/tests/polyfills.test.php index 84c3eea0..ba5b6fcd 100644 --- a/core/tests/polyfills.test.php +++ b/core/tests/polyfills.test.php @@ -76,14 +76,6 @@ class PolyfillsTest extends TestCase $this->assertEquals(clamp(15, 5, 10), 10); } - public function test_xml_tag() - { - $this->assertEquals( - "\n\n\n", - xml_tag("test", ["foo"=>"bar"], ["cake"]) - ); - } - public function test_truncate() { $this->assertEquals(truncate("test words", 10), "test words"); diff --git a/ext/danbooru_api/main.php b/ext/danbooru_api/main.php index df845532..51541a53 100644 --- a/ext/danbooru_api/main.php +++ b/ext/danbooru_api/main.php @@ -1,5 +1,12 @@ 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 = "\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 .= ""; 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 = "\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 .= ""; 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