Add integration test for MWHttpRequest user/pass options
[lhc/web/wiklou.git] / includes / utils / BatchRowIterator.php
index 07cb2bc..ef2c14a 100644 (file)
@@ -31,7 +31,7 @@ class BatchRowIterator implements RecursiveIterator {
        protected $db;
 
        /**
-        * @var string $table The name of the table to read from
+        * @var string|array $table The name or names of the table to read from
         */
        protected $table;
 
@@ -49,12 +49,12 @@ class BatchRowIterator implements RecursiveIterator {
         * @var array $conditions Array of strings containing SQL conditions
         *  to add to the query
         */
-       protected $conditions = array();
+       protected $conditions = [];
 
        /**
         * @var array $joinConditions
         */
-       protected $joinConditions = array();
+       protected $joinConditions = [];
 
        /**
         * @var array $fetchColumns List of column names to select from the
@@ -70,23 +70,28 @@ class BatchRowIterator implements RecursiveIterator {
        /**
         * @var array $current The current iterator value
         */
-       private $current = array();
+       private $current = [];
 
        /**
         * @var integer key 0-indexed number of pages fetched since self::reset()
         */
        private $key;
 
+       /**
+        * @var array Additional query options
+        */
+       protected $options = [];
+
        /**
         * @param IDatabase $db The database to read from
-        * @param string       $table      The name of the table to read from
+        * @param string|array $table      The name or names of the table to read from
         * @param string|array $primaryKey The name or names of the primary key columns
         * @param integer      $batchSize  The number of rows to fetch per iteration
-        * @throws MWException
+        * @throws InvalidArgumentException
         */
        public function __construct( IDatabase $db, $table, $primaryKey, $batchSize ) {
                if ( $batchSize < 1 ) {
-                       throw new MWException( 'Batch size must be at least 1 row.' );
+                       throw new InvalidArgumentException( 'Batch size must be at least 1 row.' );
                }
                $this->db = $db;
                $this->table = $table;
@@ -97,7 +102,7 @@ class BatchRowIterator implements RecursiveIterator {
        }
 
        /**
-        * @param array $condition Query conditions suitable for use with
+        * @param array $conditions Query conditions suitable for use with
         *  IDatabase::select
         */
        public function addConditions( array $conditions ) {
@@ -105,7 +110,15 @@ class BatchRowIterator implements RecursiveIterator {
        }
 
        /**
-        * @param array $condition Query join conditions suitable for use
+        * @param array $options Query options suitable for use with
+        *  IDatabase::select
+        */
+       public function addOptions( array $options ) {
+               $this->options = array_merge( $this->options, $options );
+       }
+
+       /**
+        * @param array $conditions Query join conditions suitable for use
         *  with IDatabase::select
         */
        public function addJoinConditions( array $conditions ) {
@@ -135,9 +148,10 @@ class BatchRowIterator implements RecursiveIterator {
         * @return array Map of primary key column to value within the row
         */
        public function extractPrimaryKeys( $row ) {
-               $pk = array();
-               foreach ( $this->primaryKey as $column ) {
-                       $pk[$column] = $row->$column;
+               $pk = [];
+               foreach ( $this->primaryKey as $alias => $column ) {
+                       $name = is_numeric( $alias ) ? $column : $alias;
+                       $pk[$name] = $row->{$name};
                }
                return $pk;
        }
@@ -161,19 +175,19 @@ class BatchRowIterator implements RecursiveIterator {
         */
        public function rewind() {
                $this->key = -1; // self::next() will turn this into 0
-               $this->current = array();
+               $this->current = [];
                $this->next();
        }
 
        /**
-        * @return boolean True when the iterator is in a valid state
+        * @return bool True when the iterator is in a valid state
         */
        public function valid() {
                return (bool)$this->current;
        }
 
        /**
-        * @return boolean True when this result set has rows
+        * @return bool True when this result set has rows
         */
        public function hasChildren() {
                return $this->current && count( $this->current );
@@ -195,10 +209,10 @@ class BatchRowIterator implements RecursiveIterator {
                        $this->fetchColumns,
                        $this->buildConditions(),
                        __METHOD__,
-                       array(
+                       [
                                'LIMIT' => $this->batchSize,
                                'ORDER BY' => $this->orderBy,
-                       ),
+                       ] + $this->options,
                        $this->joinConditions
                );
 
@@ -216,7 +230,7 @@ class BatchRowIterator implements RecursiveIterator {
         * `=` conditions while the final key uses a `>` condition
         *
         * Example output:
-        *        array( '( foo = 42 AND bar > 7 ) OR ( foo > 42 )' )
+        *        [ '( foo = 42 AND bar > 7 ) OR ( foo > 42 )' ]
         *
         * @return array The SQL conditions necessary to select the next set
         *  of rows in the batched query
@@ -227,12 +241,13 @@ class BatchRowIterator implements RecursiveIterator {
                }
 
                $maxRow = end( $this->current );
-               $maximumValues = array();
-               foreach ( $this->primaryKey as $column ) {
-                       $maximumValues[$column] = $this->db->addQuotes( $maxRow->$column );
+               $maximumValues = [];
+               foreach ( $this->primaryKey as $alias => $column ) {
+                       $name = is_numeric( $alias ) ? $column : $alias;
+                       $maximumValues[$column] = $this->db->addQuotes( $maxRow->{$name} );
                }
 
-               $pkConditions = array();
+               $pkConditions = [];
                // For example: If we have 3 primary keys
                // first run through will generate
                //   col1 = 4 AND col2 = 7 AND col3 > 1
@@ -267,7 +282,7 @@ class BatchRowIterator implements RecursiveIterator {
                $keys = array_keys( $quotedMaximumValues );
                $lastColumn = end( $keys );
                $lastValue = array_pop( $quotedMaximumValues );
-               $conditions = array();
+               $conditions = [];
                foreach ( $quotedMaximumValues as $column => $value ) {
                        $conditions[] = "$column = $value";
                }