MediaWiki  REL1_31
Go to the documentation of this file.
1 <?php
29 class BatchRowIterator implements RecursiveIterator {
34  protected $db;
39  protected $table;
44  protected $primaryKey;
49  protected $batchSize;
55  protected $conditions = [];
60  protected $joinConditions = [];
66  protected $fetchColumns;
71  protected $orderBy;
76  private $current = [];
81  private $key;
86  protected $options = [];
96  if ( $batchSize < 1 ) {
97  throw new InvalidArgumentException( 'Batch size must be at least 1 row.' );
98  }
99  $this->db = $db;
100  $this->table = $table;
101  $this->primaryKey = (array)$primaryKey;
102  $this->fetchColumns = $this->primaryKey;
103  $this->orderBy = implode( ' ASC,', $this->primaryKey ) . ' ASC';
104  $this->batchSize = $batchSize;
105  }
111  public function addConditions( array $conditions ) {
112  $this->conditions = array_merge( $this->conditions, $conditions );
113  }
119  public function addOptions( array $options ) {
120  $this->options = array_merge( $this->options, $options );
121  }
127  public function addJoinConditions( array $conditions ) {
128  $this->joinConditions = array_merge( $this->joinConditions, $conditions );
129  }
135  public function setFetchColumns( array $columns ) {
136  // If it's not the all column selector merge in the primary keys we need
137  if ( count( $columns ) === 1 && reset( $columns ) === '*' ) {
138  $this->fetchColumns = $columns;
139  } else {
140  $this->fetchColumns = array_unique( array_merge(
141  $this->primaryKey,
142  $columns
143  ) );
144  }
145  }
153  public function extractPrimaryKeys( $row ) {
154  $pk = [];
155  foreach ( $this->primaryKey as $alias => $column ) {
156  $name = is_numeric( $alias ) ? $column : $alias;
157  $pk[$name] = $row->{$name};
158  }
159  return $pk;
160  }
165  public function current() {
166  return $this->current;
167  }
172  public function key() {
173  return $this->key;
174  }
179  public function rewind() {
180  $this->key = -1; // self::next() will turn this into 0
181  $this->current = [];
182  $this->next();
183  }
188  public function valid() {
189  return (bool)$this->current;
190  }
195  public function hasChildren() {
196  return $this->current && count( $this->current );
197  }
202  public function getChildren() {
203  return new NotRecursiveIterator( new ArrayIterator( $this->current ) );
204  }
209  public function next() {
210  $res = $this->db->select(
211  $this->table,
212  $this->fetchColumns,
213  $this->buildConditions(),
214  __METHOD__,
215  [
216  'LIMIT' => $this->batchSize,
217  'ORDER BY' => $this->orderBy,
218  ] + $this->options,
219  $this->joinConditions
220  );
222  // The iterator is converted to an array because in addition to
223  // returning it in self::current() we need to use the end value
224  // in self::buildConditions()
225  $this->current = iterator_to_array( $res );
226  $this->key++;
227  }
241  protected function buildConditions() {
242  if ( !$this->current ) {
243  return $this->conditions;
244  }
246  $maxRow = end( $this->current );
247  $maximumValues = [];
248  foreach ( $this->primaryKey as $alias => $column ) {
249  $name = is_numeric( $alias ) ? $column : $alias;
250  $maximumValues[$column] = $this->db->addQuotes( $maxRow->{$name} );
251  }
253  $pkConditions = [];
254  // For example: If we have 3 primary keys
255  // first run through will generate
256  // col1 = 4 AND col2 = 7 AND col3 > 1
257  // second run through will generate
258  // col1 = 4 AND col2 > 7
259  // and the final run through will generate
260  // col1 > 4
261  while ( $maximumValues ) {
262  $pkConditions[] = $this->buildGreaterThanCondition( $maximumValues );
263  array_pop( $maximumValues );
264  }
266  $conditions = $this->conditions;
267  $conditions[] = sprintf( '( %s )', implode( ' ) OR ( ', $pkConditions ) );
269  return $conditions;
270  }
284  protected function buildGreaterThanCondition( array $quotedMaximumValues ) {
285  $keys = array_keys( $quotedMaximumValues );
286  $lastColumn = end( $keys );
287  $lastValue = array_pop( $quotedMaximumValues );
288  $conditions = [];
289  foreach ( $quotedMaximumValues as $column => $value ) {
290  $conditions[] = "$column = $value";
291  }
292  $conditions[] = "$lastColumn > $lastValue";
294  return implode( ' AND ', $conditions );
295  }
296 }
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
Definition: APACHE-LICENSE-2.0.txt:10
the array() calling protocol came about after MediaWiki 1.4rc1.
Definition: BatchRowIterator.php:55
Definition: BatchRowIterator.php:60
setFetchColumns(array $columns)
Definition: BatchRowIterator.php:135
Reset the iterator to the begining of the table.
Definition: BatchRowIterator.php:179
Definition: BatchRowIterator.php:188
addConditions(array $conditions)
Definition: BatchRowIterator.php:111
Definition: BatchRowIterator.php:29
buildGreaterThanCondition(array $quotedMaximumValues)
Given an array of column names and their maximum value generate an SQL condition where all keys excep...
Definition: BatchRowIterator.php:284
extractPrimaryKeys( $row)
Extracts the primary key(s) from a database row.
Definition: BatchRowIterator.php:153
Definition: database.txt:21
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:37
Definition: BatchRowIterator.php:71
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:38
Definition: BatchRowIterator.php:195
Definition: BatchRowIterator.php:39
Fetch the next set of rows from the database.
Definition: BatchRowIterator.php:209
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add in any and then calling but I prefer the flexibility This should also do the output encoding The system allocates a global one in $wgOut Title Represents the title of an and does all the work of translating among various forms such as plain database key
Definition: design.txt:26
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global then executing the whole list after the page is displayed We don t do anything smart like collating updates to the same table or such because the list is almost always going to have just one item on if so it s not worth the trouble Since there is a job queue in the jobs table
Definition: deferred.txt:16
Definition: BatchRowIterator.php:76
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for and distribution as defined by Sections through of this document Licensor shall mean the copyright owner or entity authorized by the copyright owner that is granting the License Legal Entity shall mean the union of the acting entity and all other entities that control are controlled by or are under common control with that entity For the purposes of this definition control direct or to cause the direction or management of such whether by contract or including but not limited to software source documentation and configuration files Object form shall mean any form resulting from mechanical transformation or translation of a Source including but not limited to compiled object generated and conversions to other media types Work shall mean the work of whether in Source or Object made available under the as indicated by a copyright notice that is included in or attached to the whether in Source or Object that is based or other modifications as a an original work of authorship For the purposes of this Derivative Works shall not include works that remain separable or merely the Work and Derivative Works thereof Contribution shall mean any work of including the original version of the Work and any modifications or additions to that Work or Derivative Works that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner For the purposes of this submitted means any form of or written communication sent to the Licensor or its including but not limited to communication on electronic mailing source code control and issue tracking systems that are managed or on behalf the Licensor for the purpose of discussing and improving the but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as Not a Contribution Contributor shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work Grant of Copyright License Subject to the terms and conditions of this each Contributor hereby grants to You a non no royalty irrevocable copyright license to prepare Derivative Works publicly publicly and distribute the Work and such Derivative Works in Source or Object form Grant of Patent License Subject to the terms and conditions of this each Contributor hereby grants to You a non no royalty have offer to and otherwise transfer the where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed Redistribution You may reproduce and distribute copies of the Work or Derivative Works thereof in any with or without and in Source or Object provided that You meet the following conditions
to add to the query
Definition: APACHE-LICENSE-2.0.txt:96
Definition: BatchRowIterator.php:172
Uses the primary key list and the maximal result row from the previous iteration to build an SQL cond...
Definition: BatchRowIterator.php:241
addOptions(array $options)
Definition: BatchRowIterator.php:119
Definition: NotRecursiveIterator.php:27
__construct(IDatabase $db, $table, $primaryKey, $batchSize)
Definition: BatchRowIterator.php:95
Definition: BatchRowIterator.php:49
Definition: BatchRowIterator.php:34
Definition: styleTest.css.php:45
Definition: BatchRowIterator.php:44
We ve cleaned up the code here by removing clumps of infrequently used code and moving them off somewhere else It s much easier for someone working with this code to see what s _really_ going and make changes or fix bugs In we can take all the code that deals with the little used title reversing options(say) and put it in one place. Instead of having little title-reversing if-blocks spread all over the codebase in showAnArticle
int $key
key 0-indexed number of pages fetched since self::reset()
Definition: BatchRowIterator.php:81
addJoinConditions(array $conditions)
Definition: BatchRowIterator.php:127
Definition: BatchRowIterator.php:66
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:302
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:22
Definition: testCompression.php:67
Definition: BatchRowIterator.php:165
Definition: BatchRowIterator.php:202
array $options
Additional query options.
Definition: BatchRowIterator.php:86