retry pinging only 5 times
[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, feu_tries'
37             . ' FROM feedentries, feedentryurls'
38             . ' WHERE fe_id = feu_fe_id' . $this->sqlNeedsUpdate()
39         );
40         $items = 0;
41         while ($row = $res->fetch(\PDO::FETCH_OBJ)) {
42             $this->log->info(
43                 'Pinging URL #%d: %s', $row->feu_id, $row->feu_url
44             );
45             $this->ping($row);
46             ++$items;
47         }
48         $this->log->info('Finished pinging %d URLs.', $items);
49     }
50
51     public function pingSome($urlOrIds)
52     {
53         $options = array();
54         foreach ($urlOrIds as $urlOrId) {
55             if (is_numeric($urlOrId)) {
56                 $options[] = 'feu_id = ' . intval($urlOrId);
57             } else {
58                 $options[] = 'feu_url = ' . $this->db->quote($urlOrId);
59             }
60         }
61
62         $this->log->info('Pinging %d URLs..', count($options));
63         $res = $this->db->query(
64             'SELECT fe_url, feu_id, feu_url FROM feedentries, feedentryurls'
65             . ' WHERE fe_id = feu_fe_id'
66             . $this->sqlNeedsUpdate()
67             . ' AND (' . implode(' OR ', $options) . ')'
68         );
69         $items = 0;
70         while ($row = $res->fetch(\PDO::FETCH_OBJ)) {
71             $this->log->info(
72                 'Pinging URL #%d: %s', $row->feu_id, $row->feu_url
73             );
74             $this->ping($row);
75             ++$items;
76         }
77         $this->log->info('Finished pinging %d URLs.', $items);
78     }
79
80     public function ping($row)
81     {
82         $from = $row->fe_url;
83         $to   = $row->feu_url;
84
85         try {
86             $res = $this->pbc->send($from, $to);
87         } catch (\Exception $e) {
88             $this->log->err('Exception: ' . $e->getMessage());
89             $this->db->exec(
90                 'UPDATE feedentryurls SET'
91                 . '  feu_pinged = 1'
92                 . ', feu_updated = NOW()'
93                 . ', feu_error = 1'
94                 . ', feu_error_code = ' . $this->db->quote($e->getCode())
95                 . ', feu_error_message = ' . $this->db->quote($e->getMessage())
96                 . ', feu_tries = ' . $this->db->quote($row->feu_tries + 1)
97                 . ', feu_retry = ' . $this->sqlRetry($e)
98                 . ' WHERE feu_id = ' . $this->db->quote($row->feu_id)
99             );
100             return;
101         }
102
103         if (!$res->isError()) {
104             //all fine
105             $this->log->info('ok');
106             $this->db->exec(
107                 'UPDATE feedentryurls SET'
108                 . '  feu_pinged = 1'
109                 . ', feu_updated = NOW()'
110                 . ', feu_error = 0'
111                 . ', feu_error_code = ""'
112                 . ', feu_error_message = ""'
113                 . ', feu_tries = ' . $this->db->quote($row->feu_tries + 1)
114                 . ', feu_retry = 0'
115                 . ' WHERE feu_id = ' . $this->db->quote($row->feu_id)
116             );
117         } else {
118             //error
119             $this->log->err('Error: ' . $res->getCode() . ': ' . $res->getMessage());
120             $httpRes = $res->getResponse();
121             if ($httpRes) {
122                 $this->log->info(
123                     'Pingback response: Status code ' . $httpRes->getStatus()
124                     . ', headers: ' . print_r($httpRes->getHeader(), true)
125                 );
126                 //. ', body: ' .$httpRes->getBody()
127             }
128             $this->db->exec(
129                 'UPDATE feedentryurls SET'
130                 . '  feu_pinged = 1'
131                 . ', feu_updated = NOW()'
132                 . ', feu_error = 1'
133                 . ', feu_error_code = ' . $this->db->quote($res->getCode())
134                 . ', feu_error_message = ' . $this->db->quote($res->getMessage())
135                 . ', feu_tries = ' . $this->db->quote($row->feu_tries + 1)
136                 . ', feu_retry = ' . $this->sqlRetry($res)
137                 . ' WHERE feu_id = ' . $this->db->quote($row->feu_id)
138             );
139         }
140     }
141
142     protected function sqlNeedsUpdate()
143     {
144         if ($this->deps->options['force']) {
145             return '';
146         }
147         $sqlRetry = '(feu_retry = 1 AND feu_tries < 5)';
148         //FIXME: wait at least 1 hour before retrying
149
150         return ' AND feu_active = 1 AND (feu_pinged = 0 OR ' . $sqlRetry . ')';
151     }
152
153     /**
154      * Determines if it should be retried to pingback the URL after some time
155      *
156      * @param $obj mixed Exception or Pingback response
157      */
158     protected function sqlRetry($obj)
159     {
160         if ($obj instanceof \Exception) {
161             return '1';
162         }
163
164         switch ($obj->getCode()) {
165         case -32601:  //they have xmp-rpc, but do not support pingback
166         case 17:  //they think we don't link to them
167         case 18:  //they think we send out pingback spam
168         case 48:  //already registered
169         case 49:  //access denied
170         case 200: //pingback not supported
171         case 201: //Unvalid target URI
172             return '0';
173         }
174
175         return '1';
176     }
177 }
178 ?>