17fff7918a6d721b0b0daabca0cd1ab825a2f4c0
[stapibas.git] / src / stapibas / Feed / PingUrls.php
1 <?php
2 namespace stapibas;
3
4 /**
5  * Pings all URLs that have not been pinged yet
6  */
7 class Feed_PingUrls
8 {
9     public $db;
10     public $log;
11     public $pbc;
12
13     public function __construct()
14     {
15         $this->pbc = new \PEAR2\Services\Pingback\Client();
16
17         $req = new \HTTP_Request2();
18         $req->setConfig(
19             array(
20                 'ssl_verify_peer' => false,
21                 'ssl_verify_host' => false
22             )
23         );
24         $this->pbc->setRequest($req);
25         $this->pbc->setDebug(true);
26     }
27
28     public function pingAll()
29     {
30         $this->log->info('Pinging all URLs..');
31         $res = $this->db->query(
32             'SELECT fe_url, feu_id, feu_url FROM feedentries, feedentryurls'
33             . ' WHERE fe_id = feu_fe_id AND feu_active = 1 AND feu_pinged = 0'
34         );
35         $items = 0;
36         while ($row = $res->fetch(\PDO::FETCH_OBJ)) {
37             $this->log->info(
38                 sprintf('Pinging URL #%d: %s', $row->feu_id, $row->feu_url)
39             );
40             $this->ping($row);
41             ++$items;
42         }
43         $this->log->info(sprintf('Finished pinging %d URLs.', $items));
44     }
45
46     public function ping($row)
47     {
48         $from = $row->fe_url;
49         $to   = $row->feu_url;
50
51         try {
52             $res = $this->pbc->send($from, $to);
53         } catch (\Exception $e) {
54             $this->log->err('Exception: ' . $e->getMessage());
55             $this->db->exec(
56                 'UPDATE feedentryurls SET'
57                 . '  feu_pinged = 1'
58                 . ', feu_updated = NOW()'
59                 . ', feu_error = ' . $this->db->quote($e->getMessage())
60                 . ' WHERE feu_id = ' . $this->db->quote($row->feu_id)
61             );
62             return;
63         }
64
65         if (!$res->isError()) {
66             //all fine
67             $this->log->info('ok');
68             $this->db->exec(
69                 'UPDATE feedentryurls SET'
70                 . '  feu_pinged = 1'
71                 . ', feu_updated = NOW()'
72                 . ', feu_error = ""'
73                 . ' WHERE feu_id = ' . $this->db->quote($row->feu_id)
74             );
75         } else {
76             //error
77             $this->log->err('Error: ' . $res->getCode() . ': ' . $res->getMessage());
78             $this->log->info(
79                 'Pingback response: Status code ' . $res->getResponse()->getStatus()
80                 . ', headers: ' . print_r($res->getResponse()->getHeader(), true)
81                 . ', body: ' . $res->getResponse()->getBody()
82             );
83             $this->db->exec(
84                 'UPDATE feedentryurls SET'
85                 . '  feu_pinged = 1'
86                 . ', feu_updated = NOW()'
87                 . ', feu_error = '
88                 . $this->db->quote($res->getCode() . ': ' . $res->getMessage())
89                 . ' WHERE feu_id = ' . $this->db->quote($row->feu_id)
90             );
91         }
92     }
93 }
94 ?>