MediaWiki REL1_27
BatchRowIterator.php
Go to the documentation of this file.
1<?php
26class BatchRowIterator implements RecursiveIterator {
27
31 protected $db;
32
36 protected $table;
37
41 protected $primaryKey;
42
46 protected $batchSize;
47
52 protected $conditions = [];
53
57 protected $joinConditions = [];
58
63 protected $fetchColumns;
64
68 protected $orderBy;
69
73 private $current = [];
74
78 private $key;
79
88 if ( $batchSize < 1 ) {
89 throw new MWException( 'Batch size must be at least 1 row.' );
90 }
91 $this->db = $db;
92 $this->table = $table;
93 $this->primaryKey = (array)$primaryKey;
94 $this->fetchColumns = $this->primaryKey;
95 $this->orderBy = implode( ' ASC,', $this->primaryKey ) . ' ASC';
96 $this->batchSize = $batchSize;
97 }
98
103 public function addConditions( array $conditions ) {
104 $this->conditions = array_merge( $this->conditions, $conditions );
105 }
106
112 $this->joinConditions = array_merge( $this->joinConditions, $conditions );
113 }
114
119 public function setFetchColumns( array $columns ) {
120 // If it's not the all column selector merge in the primary keys we need
121 if ( count( $columns ) === 1 && reset( $columns ) === '*' ) {
122 $this->fetchColumns = $columns;
123 } else {
124 $this->fetchColumns = array_unique( array_merge(
125 $this->primaryKey,
126 $columns
127 ) );
128 }
129 }
130
137 public function extractPrimaryKeys( $row ) {
138 $pk = [];
139 foreach ( $this->primaryKey as $column ) {
140 $pk[$column] = $row->$column;
141 }
142 return $pk;
143 }
144
148 public function current() {
149 return $this->current;
150 }
151
155 public function key() {
156 return $this->key;
157 }
158
162 public function rewind() {
163 $this->key = -1; // self::next() will turn this into 0
164 $this->current = [];
165 $this->next();
166 }
167
171 public function valid() {
172 return (bool)$this->current;
173 }
174
178 public function hasChildren() {
179 return $this->current && count( $this->current );
180 }
181
185 public function getChildren() {
186 return new NotRecursiveIterator( new ArrayIterator( $this->current ) );
187 }
188
192 public function next() {
193 $res = $this->db->select(
194 $this->table,
195 $this->fetchColumns,
196 $this->buildConditions(),
197 __METHOD__,
198 [
199 'LIMIT' => $this->batchSize,
200 'ORDER BY' => $this->orderBy,
201 ],
202 $this->joinConditions
203 );
204
205 // The iterator is converted to an array because in addition to
206 // returning it in self::current() we need to use the end value
207 // in self::buildConditions()
208 $this->current = iterator_to_array( $res );
209 $this->key++;
210 }
211
224 protected function buildConditions() {
225 if ( !$this->current ) {
226 return $this->conditions;
227 }
228
229 $maxRow = end( $this->current );
230 $maximumValues = [];
231 foreach ( $this->primaryKey as $column ) {
232 $maximumValues[$column] = $this->db->addQuotes( $maxRow->$column );
233 }
234
235 $pkConditions = [];
236 // For example: If we have 3 primary keys
237 // first run through will generate
238 // col1 = 4 AND col2 = 7 AND col3 > 1
239 // second run through will generate
240 // col1 = 4 AND col2 > 7
241 // and the final run through will generate
242 // col1 > 4
243 while ( $maximumValues ) {
244 $pkConditions[] = $this->buildGreaterThanCondition( $maximumValues );
245 array_pop( $maximumValues );
246 }
247
249 $conditions[] = sprintf( '( %s )', implode( ' ) OR ( ', $pkConditions ) );
250
251 return $conditions;
252 }
253
266 protected function buildGreaterThanCondition( array $quotedMaximumValues ) {
267 $keys = array_keys( $quotedMaximumValues );
268 $lastColumn = end( $keys );
269 $lastValue = array_pop( $quotedMaximumValues );
270 $conditions = [];
271 foreach ( $quotedMaximumValues as $column => $value ) {
272 $conditions[] = "$column = $value";
273 }
274 $conditions[] = "$lastColumn > $lastValue";
275
276 return implode( ' AND ', $conditions );
277 }
278}
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
next()
Fetch the next set of rows from the database.
addConditions(array $conditions)
rewind()
Reset the iterator to the begining of the table.
integer $key
key 0-indexed number of pages fetched since self::reset()
addJoinConditions(array $conditions)
setFetchColumns(array $columns)
extractPrimaryKeys( $row)
Extracts the primary key(s) from a database row.
buildGreaterThanCondition(array $quotedMaximumValues)
Given an array of column names and their maximum value generate an SQL condition where all keys excep...
buildConditions()
Uses the primary key list and the maximal result row from the previous iteration to build an SQL cond...
__construct(IDatabase $db, $table, $primaryKey, $batchSize)
MediaWiki exception.
$res
Definition database.txt:21
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
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
the array() calling protocol came about after MediaWiki 1.4rc1.
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
Basic database interface for live and lazy-loaded DB handles.
Definition IDatabase.php:35