command line interface to update and force since feeds, feed entries and ping urls
[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(Dependencies $deps)
14     {
15         $this->deps = $deps;
16         $this->db   = $deps->db;
17         $this->log  = $deps->log;
18
19         $this->pbc = new \PEAR2\Services\Pingback\Client();
20
21         $req = new \HTTP_Request2();
22         $req->setConfig(
23             array(
24                 'ssl_verify_peer' => false,
25                 'ssl_verify_host' => false
26             )
27         );
28         $this->pbc->setRequest($req);
29         $this->pbc->setDebug(true);
30     }
31
32     public function pingAll()
33     {
34         $this->log->info('Pinging all URLs..');
35         $res = $this->db->query(
36             'SELECT fe_url, feu_id, feu_url FROM feedentries, feedentryurls'
37             . ' WHERE fe_id = feu_fe_id' . $this->sqlNeedsUpdate()
38         );
39         $items = 0;
40         while ($row = $res->fetch(\PDO::FETCH_OBJ)) {
41             $this->log->info(
42                 'Pinging URL #%d: %s', $row->feu_id, $row->feu_url
43             );
44             $this->ping($row);
45             ++$items;
46         }
47         $this->log->info('Finished pinging %d URLs.', $items);
48     }
49
50     public function pingSome($urlOrIds)
51     {
52         $options = array();
53         foreach ($urlOrIds as $urlOrId) {
54             if (is_numeric($urlOrId)) {
55                 $options[] = 'feu_id = ' . intval($urlOrId);
56             } else {
57                 $options[] = 'feu_url = ' . $this->db->quote($urlOrId);
58             }
59         }
60
61         $this->log->info('Pinging %d URLs..', count($options));
62         $res = $this->db->query(
63             'SELECT fe_url, feu_id, feu_url FROM feedentries, feedentryurls'
64             . ' WHERE fe_id = feu_fe_id'
65             . $this->sqlNeedsUpdate()
66             . ' AND (' . implode(' OR ', $options) . ')'
67         );
68         $items = 0;
69         while ($row = $res->fetch(\PDO::FETCH_OBJ)) {
70             $this->log->info(
71                 'Pinging URL #%d: %s', $row->feu_id, $row->feu_url
72             );
73             $this->ping($row);
74             ++$items;
75         }
76         $this->log->info('Finished pinging %d URLs.', $items);
77     }
78
79     public function ping($row)
80     {
81         $from = $row->fe_url;
82         $to   = $row->feu_url;
83
84         try {
85             $res = $this->pbc->send($from, $to);
86         } catch (\Exception $e) {
87             $this->log->err('Exception: ' . $e->getMessage());
88             $this->db->exec(
89                 'UPDATE feedentryurls SET'
90                 . '  feu_pinged = 1'
91                 . ', feu_updated = NOW()'
92                 . ', feu_error = ' . $this->db->quote($e->getMessage())
93                 . ' WHERE feu_id = ' . $this->db->quote($row->feu_id)
94             );
95             return;
96         }
97
98         if (!$res->isError()) {
99             //all fine
100             $this->log->info('ok');
101             $this->db->exec(
102                 'UPDATE feedentryurls SET'
103                 . '  feu_pinged = 1'
104                 . ', feu_updated = NOW()'
105                 . ', feu_error = ""'
106                 . ' WHERE feu_id = ' . $this->db->quote($row->feu_id)
107             );
108         } else {
109             //error
110             $this->log->err('Error: ' . $res->getCode() . ': ' . $res->getMessage());
111             $httpRes = $res->getResponse();
112             if ($httpRes) {
113                 $this->log->info(
114                     'Pingback response: Status code ' . $httpRes->getStatus()
115                     . ', headers: ' . print_r($httpRes->getHeader(), true)
116                     . ', body: ' . $httpRes->getBody()
117                 );
118             }
119             $this->db->exec(
120                 'UPDATE feedentryurls SET'
121                 . '  feu_pinged = 1'
122                 . ', feu_updated = NOW()'
123                 . ', feu_error = '
124                 . $this->db->quote($res->getCode() . ': ' . $res->getMessage())
125                 . ' WHERE feu_id = ' . $this->db->quote($row->feu_id)
126             );
127         }
128     }
129
130     protected function sqlNeedsUpdate()
131     {
132         if ($this->deps->options['force']) {
133             return '';
134         }
135         return '  AND feu_active = 1 AND feu_pinged = 0';
136     }
137 }
138 ?>