first work on linkback result visualization
[stapibas.git] / www / api / links.php
1 <?php
2 namespace stapibas;
3 /**
4  * Fetch the list of links in the given URL as well as their ping status.
5  * Tells you if it was pinged, and what result it had.
6  */
7
8 require_once '../www-header.php';
9 header('Access-Control-Allow-Origin: *');
10
11 if (!isset($_GET['url'])) {
12     header('HTTP/1.0 400 Bad Request');
13     echo "url missing\n";
14     exit(1);
15 }
16
17 $url = $_GET['url'];
18 $res = $db->query(
19     'SELECT * FROM feedentries WHERE fe_url = ' . $db->quote($url)
20 );
21 $urlRow = $res->fetch(PDO::FETCH_OBJ);
22 if ($urlRow === false) {
23     header('HTTP/1.0 404 Not Found');
24     echo "Url not found\n";
25     exit(1);
26 }
27 $json = (object) array(
28     'url'         => $urlRow->fe_url,
29     'updated'     => $urlRow->fe_updated,
30     'needsUpdate' => (bool) $urlRow->fe_needs_update,
31     'links'       => array()
32 );
33
34 $res = $db->query(
35     'SELECT * FROM feedentryurls'
36     . ' WHERE feu_fe_id = ' . $db->quote($urlRow->fe_id)
37 );
38 while ($linkRow = $res->fetch(\PDO::FETCH_OBJ)) {
39     $status = null;
40     if (!$linkRow->feu_pinged) {
41         $status = 'queued';
42     } else if ($linkRow->feu_retry && $linkRow->feu_tries < 5) {
43         $status = 'pinging';
44     } else if ($linkRow->feu_error) {
45         if ($linkRow->feu_error_code == 200) {
46             $status = 'unsupported';
47         } else {
48             $status = 'error';
49         }
50     } else {
51         $status = 'ok';
52     }
53     $json->links[$linkRow->feu_url] = (object) array(
54         'url'     => $linkRow->feu_url,
55         'pinged'  => (bool) $linkRow->feu_pinged,
56         'updated' => $linkRow->feu_updated,
57         'status'  => $status,
58         'error'   => (object) array(
59             'code'    => $linkRow->feu_error_code,
60             'message' => $linkRow->feu_error_message
61         ),
62         'tries'   => $linkRow->feu_tries
63     );
64 }
65
66 header('HTTP/1.0 200 OK');
67 header('Content-type: application/json');
68 echo json_encode($json) . "\n";
69 ?>