Send patches - preferably formatted by git format-patch - to patches at archlinux32 dot org.
summaryrefslogtreecommitdiff
path: root/lib/format.php
blob: f0f34b3c774abb4a0c5ac612b3aa81e4ee05f0a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php

# do not include twice
if (function_exists("export_as_requested"))
  return;

include_once BASE . "/lib/http.php";

function export_as_requested($content, $format = NULL) {
  if (isset($content["All"])) {
    $content["json"]=$content["All"];
    $content["tsv"]=$content["All"];
    unset($content["All"]);
  }
  if (isset($content["json"]) && ((empty($format) && array_key_exists("json", $_GET)) || ($format == 'json'))) {
    header ("Content-type: application/json");
    print json_encode(
      $content["json"],
      JSON_UNESCAPED_SLASHES
    );
  } elseif (isset($content["tsv"]) && ((empty($format) && array_key_exists("tsv", $_GET)) || ($format == 'tsv'))) {
    header ("Content-type: text/tab-separated-values");
    if (! array_key_exists("no-headers", $_GET))
      print implode("\t",array_keys($content["tsv"][0])) . "\n";
    print implode(
      "",
      array_map(
        function($row){
          return implode("\t",$row) . "\n";
        },
        $content["tsv"]
      )
    );
  } else {
    if (!empty($format))
      return false;
    throw_http_error(
      406,
      "Not Acceptable",
      implode(
        "<br>\n",
        array_merge(
          array(
            "Unknown output format.",
            "Accepted:"
          ),
          array_map(
            function($type){
              return "<a href=\"?" . $type . "\">" . $type . "</a>";
            },
            array_keys(
              $content
            )
          )
        )
      )
    );
  }
  return true;
}