]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - lib/filteringnoticestream.php
Opps, PEAR sucks. Need to call find() before fetch() ... :-(
[quix0rs-gnu-social.git] / lib / filteringnoticestream.php
index f41221107418857b2f4fddd40e4f45b909ac3152..b4ec6687bb7c92831ed30114d0de9847cfdbab45 100644 (file)
@@ -56,7 +56,7 @@ abstract class FilteringNoticeStream extends NoticeStream
 
     abstract function filter($notice);
 
-    function getNotices($offset, $limit, $sinceId=null, $maxId=null)
+    function getNoticeIds($offset, $limit, $since_id, $max_id)
     {
         // "offset" is virtual; we have to get a lot
         $total = $offset + $limit;
@@ -70,20 +70,21 @@ abstract class FilteringNoticeStream extends NoticeStream
         // or we get nothing from upstream.
 
         $results = null;
+        $round = 0;
 
         do {
-
-            $raw = $this->upstream->getNotices($startAt, $askFor, $sinceId, $maxId);
+            $raw = $this->upstream->getNotices($startAt, $askFor, $since_id, $max_id);
 
             $results = $raw->N;
-
             if ($results == 0) {
                 break;
             }
 
-            while ($raw->fetch()) {
-                if ($this->filter($raw)) {
-                    $filtered[] = clone($raw);
+            $notices = $raw->fetchAll();
+            $this->prefill($notices);
+            foreach ($notices as $notice) {
+                if ($this->filter($notice)) {
+                    $filtered[] = $notice->id;
                     if (count($filtered) >= $total) {
                         break;
                     }
@@ -91,25 +92,24 @@ abstract class FilteringNoticeStream extends NoticeStream
             }
 
             // XXX: make these smarter; factor hit rate into $askFor
-
             $startAt += $askFor;
-            $askFor   = max($total - count($filtered), NOTICES_PER_PAGE);
+            $hits = count($filtered);
+            $lastAsk = $askFor;
+
+            if ($hits === 0) {
+                $askFor = max(min(2 * $askFor, NOTICES_PER_PAGE * 50), NOTICES_PER_PAGE);
+            } else {
+                $askFor = max(min(intval(ceil(($total - $hits)*$startAt/$hits)), NOTICES_PER_PAGE * 50), NOTICES_PER_PAGE);
+            }
 
-        } while (count($filtered) < $total && $results !== 0);
+            $round++;
+        } while (count($filtered) < $total && $results >= $lastAsk);
 
-        return new ArrayWrapper(array_slice($filtered, $offset, $limit));
+        return array_slice(array_values($filtered), $offset, $limit);
     }
 
-    function getNoticeIds($offset, $limit, $sinceId, $maxId)
+    function prefill($notices)
     {
-        $notices = $this->getNotices($offset, $limit, $sinceId, $maxId);
-
-        $ids = array();
-
-        while ($notices->fetch()) {
-            $ids[] = $notices->id;
-        }
-
-        return $ids;
+        return;
     }
 }