command line interface to update and force since feeds, feed entries and ping urls
[stapibas.git] / src / stapibas / Feed / PingUrls.php
index 17fff7918a6d721b0b0daabca0cd1ab825a2f4c0..22993527d8601c6e76adfdb901c68470ce599e87 100644 (file)
@@ -10,8 +10,12 @@ class Feed_PingUrls
     public $log;
     public $pbc;
 
-    public function __construct()
+    public function __construct(Dependencies $deps)
     {
+        $this->deps = $deps;
+        $this->db   = $deps->db;
+        $this->log  = $deps->log;
+
         $this->pbc = new \PEAR2\Services\Pingback\Client();
 
         $req = new \HTTP_Request2();
@@ -30,17 +34,46 @@ class Feed_PingUrls
         $this->log->info('Pinging all URLs..');
         $res = $this->db->query(
             'SELECT fe_url, feu_id, feu_url FROM feedentries, feedentryurls'
-            . ' WHERE fe_id = feu_fe_id AND feu_active = 1 AND feu_pinged = 0'
+            . ' WHERE fe_id = feu_fe_id' . $this->sqlNeedsUpdate()
+        );
+        $items = 0;
+        while ($row = $res->fetch(\PDO::FETCH_OBJ)) {
+            $this->log->info(
+                'Pinging URL #%d: %s', $row->feu_id, $row->feu_url
+            );
+            $this->ping($row);
+            ++$items;
+        }
+        $this->log->info('Finished pinging %d URLs.', $items);
+    }
+
+    public function pingSome($urlOrIds)
+    {
+        $options = array();
+        foreach ($urlOrIds as $urlOrId) {
+            if (is_numeric($urlOrId)) {
+                $options[] = 'feu_id = ' . intval($urlOrId);
+            } else {
+                $options[] = 'feu_url = ' . $this->db->quote($urlOrId);
+            }
+        }
+
+        $this->log->info('Pinging %d URLs..', count($options));
+        $res = $this->db->query(
+            'SELECT fe_url, feu_id, feu_url FROM feedentries, feedentryurls'
+            . ' WHERE fe_id = feu_fe_id'
+            . $this->sqlNeedsUpdate()
+            . ' AND (' . implode(' OR ', $options) . ')'
         );
         $items = 0;
         while ($row = $res->fetch(\PDO::FETCH_OBJ)) {
             $this->log->info(
-                sprintf('Pinging URL #%d: %s', $row->feu_id, $row->feu_url)
+                'Pinging URL #%d: %s', $row->feu_id, $row->feu_url
             );
             $this->ping($row);
             ++$items;
         }
-        $this->log->info(sprintf('Finished pinging %d URLs.', $items));
+        $this->log->info('Finished pinging %d URLs.', $items);
     }
 
     public function ping($row)
@@ -75,11 +108,14 @@ class Feed_PingUrls
         } else {
             //error
             $this->log->err('Error: ' . $res->getCode() . ': ' . $res->getMessage());
-            $this->log->info(
-                'Pingback response: Status code ' . $res->getResponse()->getStatus()
-                . ', headers: ' . print_r($res->getResponse()->getHeader(), true)
-                . ', body: ' . $res->getResponse()->getBody()
-            );
+            $httpRes = $res->getResponse();
+            if ($httpRes) {
+                $this->log->info(
+                    'Pingback response: Status code ' . $httpRes->getStatus()
+                    . ', headers: ' . print_r($httpRes->getHeader(), true)
+                    . ', body: ' . $httpRes->getBody()
+                );
+            }
             $this->db->exec(
                 'UPDATE feedentryurls SET'
                 . '  feu_pinged = 1'
@@ -90,5 +126,13 @@ class Feed_PingUrls
             );
         }
     }
+
+    protected function sqlNeedsUpdate()
+    {
+        if ($this->deps->options['force']) {
+            return '';
+        }
+        return '  AND feu_active = 1 AND feu_pinged = 0';
+    }
 }
 ?>