API: Have generator=random set a non-continuation value
authorBrad Jorsch <bjorsch@wikimedia.org>
Tue, 6 Jun 2017 14:15:49 +0000 (10:15 -0400)
committerBrad Jorsch <bjorsch@wikimedia.org>
Tue, 6 Jun 2017 14:15:49 +0000 (10:15 -0400)
When used as a generator, it needs a non-continuation value for
continuation to work properly. As a list it doesn't matter because lists
never non-continue.

Bug: T167141
Change-Id: I7b8ddaf73f918b31414e22fc3a13e6245c2d57d7

includes/api/ApiQueryRandom.php

index cc1fc89..ce62226 100644 (file)
@@ -48,7 +48,7 @@ class ApiQueryRandom extends ApiQueryGeneratorBase {
         * @param ApiPageSet|null $resultPageSet
         * @param int $limit Number of pages to fetch
         * @param string|null $start Starting page_random
-        * @param int|null $startId Starting page_id
+        * @param int $startId Starting page_id
         * @param string|null $end Ending page_random
         * @return array (int, string|null) Number of pages left to query and continuation string
         */
@@ -75,8 +75,8 @@ class ApiQueryRandom extends ApiQueryGeneratorBase {
 
                if ( $start !== null ) {
                        $start = $this->getDB()->addQuotes( $start );
-                       if ( $startId !== null ) {
-                               $startId = (int)$startId;
+                       if ( $startId > 0 ) {
+                               $startId = (int)$startId; // safety
                                $this->addWhere( "page_random = $start AND page_id >= $startId OR page_random > $start" );
                        } else {
                                $this->addWhere( "page_random >= $start" );
@@ -144,10 +144,19 @@ class ApiQueryRandom extends ApiQueryGeneratorBase {
                } else {
                        $rand = wfRandom();
                        $start = $rand;
-                       $startId = null;
+                       $startId = 0;
                        $end = null;
                }
 
+               // Set the non-continue if this is being used as a generator
+               // (as a list it doesn't matter because lists never non-continue)
+               if ( $resultPageSet !== null ) {
+                       $endFlag = $end === null ? 0 : 1;
+                       $this->getContinuationManager()->addGeneratorNonContinueParam(
+                               $this, 'continue', "$rand|$start|$startId|$endFlag"
+                       );
+               }
+
                list( $left, $continue ) =
                        $this->runQuery( $resultPageSet, $params['limit'], $start, $startId, $end );
                if ( $end === null && $continue === null ) {