Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 25 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
PagesWithPropertyIterator | |
0.00% |
0 / 25 |
|
0.00% |
0 / 2 |
20 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
getIterator | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
12 |
1 | <?php |
2 | |
3 | namespace Flow\Utils; |
4 | |
5 | use BatchRowIterator; |
6 | use Iterator; |
7 | use IteratorAggregate; |
8 | use MediaWiki\Extension\Notifications\Iterator\CallbackIterator; |
9 | use MediaWiki\Title\Title; |
10 | use RecursiveIteratorIterator; |
11 | use Wikimedia\Rdbms\IReadableDatabase; |
12 | |
13 | /** |
14 | * Iterates over all titles that have the specified page property |
15 | */ |
16 | class PagesWithPropertyIterator implements IteratorAggregate { |
17 | /** |
18 | * @var IReadableDatabase |
19 | */ |
20 | protected $db; |
21 | |
22 | /** |
23 | * @var string |
24 | */ |
25 | protected $propName; |
26 | |
27 | /** |
28 | * Page id to start at (inclusive) |
29 | * |
30 | * @var int|null |
31 | */ |
32 | protected $startId = null; |
33 | |
34 | /** |
35 | * Page id to stop at (exclusive) |
36 | * |
37 | * @var int|null |
38 | */ |
39 | protected $stopId = null; |
40 | |
41 | /** |
42 | * @param IReadableDatabase $db |
43 | * @param string $propName |
44 | * @param int|null $startId Page id to start at (inclusive) |
45 | * @param int|null $stopId Page id to stop at (exclusive) |
46 | */ |
47 | public function __construct( IReadableDatabase $db, $propName, $startId = null, $stopId = null ) { |
48 | $this->db = $db; |
49 | $this->propName = $propName; |
50 | $this->startId = $startId; |
51 | $this->stopId = $stopId; |
52 | } |
53 | |
54 | /** |
55 | * @return Iterator<Title> |
56 | */ |
57 | public function getIterator() { |
58 | $it = new BatchRowIterator( |
59 | $this->db, |
60 | /* tables */ [ 'page_props', 'page' ], |
61 | /* pk */ 'pp_page', |
62 | /* rows per batch */ 500 |
63 | ); |
64 | |
65 | $conditions = [ 'pp_propname' => $this->propName ]; |
66 | if ( $this->startId !== null ) { |
67 | $conditions[] = $this->db->expr( 'pp_page', '>=', $this->startId ); |
68 | } |
69 | if ( $this->stopId !== null ) { |
70 | $conditions[] = $this->db->expr( 'pp_page', '<', $this->stopId ); |
71 | } |
72 | $it->addConditions( $conditions ); |
73 | |
74 | $it->addJoinConditions( [ |
75 | 'page' => [ 'JOIN', 'pp_page=page_id' ], |
76 | ] ); |
77 | $it->setFetchColumns( [ 'page_namespace', 'page_title' ] ); |
78 | $it->setCaller( __METHOD__ ); |
79 | |
80 | $it = new RecursiveIteratorIterator( $it ); |
81 | |
82 | return new CallbackIterator( $it, static function ( $row ) { |
83 | return Title::makeTitle( $row->page_namespace, $row->page_title ); |
84 | } ); |
85 | } |
86 | } |