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