pingback to u-in-reply-to also when it is not in e-content
[stapibas.git] / src / stapibas / Feed / UpdateEntries.php
index b0daf6f36d7614a2899ef23987601482c0258810..975f6a9a5304407bc137c6c324f47897f536aac5 100644 (file)
@@ -9,27 +9,60 @@ class Feed_UpdateEntries
     public $db;
     public $log;
 
+    public function __construct(Dependencies $deps)
+    {
+        $this->deps = $deps;
+        $this->db   = $deps->db;
+        $this->log  = $deps->log;
+    }
+
     public function updateAll()
     {
         $this->log->info('Updating feed entries..');
         $res = $this->db->query(
             'SELECT * FROM feedentries'
-            . ' WHERE fe_needs_update = 1 OR fe_updated = "0000-00-00 00:00:00"'
+            . ' WHERE ' . $this->sqlNeedsUpdate()
         );
+        $items = 0;
         while ($entryRow = $res->fetch(\PDO::FETCH_OBJ)) {
-            $this->log->info(
-                sprintf(
-                    'Updating feed entry #%d: %s',
-                    $entryRow->fe_id, $entryRow->fe_url
-                )
-            );
+            ++$items;
             $this->updateEntry($entryRow);
         }
-        $this->log->info('Finished updating entries.');
+        $this->log->info('Finished updating %d entries.', $items);
+    }
+
+    public function updateSome($urlOrIds)
+    {
+        $options = array();
+        foreach ($urlOrIds as $urlOrId) {
+            if (is_numeric($urlOrId)) {
+                $options[] = 'fe_id = ' . intval($urlOrId);
+            } else {
+                $options[] = 'fe_url = ' . $this->db->quote($urlOrId);
+            }
+        }
+
+        $this->log->info('Updating %d feed entries..', count($options));
+        $res = $this->db->query(
+            'SELECT * FROM feedentries'
+            . ' WHERE ' . $this->sqlNeedsUpdate()
+            . ' AND (' . implode(' OR ', $options) . ')'
+        );
+
+        $items = 0;
+        while ($entryRow = $res->fetch(\PDO::FETCH_OBJ)) {
+            ++$items;
+            $this->updateEntry($entryRow);
+        }
+        $this->log->info('Finished updating %d entries.', $items);
     }
 
     protected function updateEntry($entryRow)
     {
+        $this->log->info(
+            'Updating feed entry #%d: %s', $entryRow->fe_id, $entryRow->fe_url
+        );
+
         $req = new \HTTP_Request2($entryRow->fe_url);
         $req->setHeader('User-Agent', 'stapibas');
         $req->setHeader(
@@ -122,10 +155,8 @@ class Feed_UpdateEntries
             );
         }
         $this->log->info(
-            sprintf(
-                'Feed entry #%d: %d new, %d updated, %d deleted of %d URLs',
-                $entryRow->fe_id, $new, $updated, $deleted, $items
-            )
+            'Feed entry #%d: %d new, %d updated, %d deleted of %d URLs',
+            $entryRow->fe_id, $new, $updated, $deleted, $items
         );
     }
 
@@ -145,11 +176,19 @@ class Feed_UpdateEntries
 
         $xpath = new \DOMXPath($doc);
         $xpath->registerNamespace('h', 'http://www.w3.org/1999/xhtml');
+        // all links in e-content AND u-in-reply-to links
         $query = '//*[' . $this->xpc('h-entry') . ' or ' . $this->xpc('hentry') . ']'
             . '//*[' . $this->xpc('e-content') . ' or ' . $this->xpc('entry-content') . ']'
-            . '//*[(self::a or self::h:a) and @href and not(starts-with(@href, "#"))]';
+            . '//*[(self::a or self::h:a) and @href and not(starts-with(@href, "#"))]'
+            . ' | '
+            . '//*[' . $this->xpc('h-entry') . ' or ' . $this->xpc('hentry') . ']'
+            . '//*['
+            . '(self::a or self::h:a) and @href and not(starts-with(@href, "#"))'
+            . 'and ' . $this->xpc('u-in-reply-to')
+            . ']';
+;
         $links = $xpath->query($query);
-        $this->log->info(sprintf('%d links found', $links->length));
+        $this->log->info('%d links found', $links->length);
 
         $entryUrl = new \Net_URL2($entryRow->fe_url);
         //FIXME: base URL in html code
@@ -193,5 +232,12 @@ class Feed_UpdateEntries
         );
     }
 
+    protected function sqlNeedsUpdate()
+    {
+        if ($this->deps->options['force']) {
+            return ' 1';
+        }
+        return ' (fe_needs_update = 1 OR fe_updated = "0000-00-00 00:00:00")';
+    }
 }
 ?>