Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 16 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
CampaignSelectQueryBuilder | |
0.00% |
0 / 16 |
|
0.00% |
0 / 6 |
90 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
whereEnabled | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
orderByIdAsc | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
fetchCampaignRecord | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
fetchCampaignRecords | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
prepareForSelect | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\MediaUploader\Campaign; |
4 | |
5 | use Iterator; |
6 | use Wikimedia\Rdbms\IDatabase; |
7 | use Wikimedia\Rdbms\SelectQueryBuilder; |
8 | |
9 | /** |
10 | * Provides a fluent interface for selecting rows from the mu_campaign |
11 | * database table. |
12 | */ |
13 | class CampaignSelectQueryBuilder extends SelectQueryBuilder { |
14 | |
15 | /** @var CampaignStore */ |
16 | private $store; |
17 | |
18 | /** |
19 | * @param IDatabase $db |
20 | * @param CampaignStore $store |
21 | * |
22 | * @internal only for use by CampaignStore |
23 | */ |
24 | public function __construct( IDatabase $db, CampaignStore $store ) { |
25 | parent::__construct( $db ); |
26 | |
27 | $this->store = $store; |
28 | } |
29 | |
30 | /** |
31 | * Filter campaigns by whether they are enabled or not. |
32 | * |
33 | * @param bool $enabled |
34 | * |
35 | * @return $this |
36 | */ |
37 | public function whereEnabled( bool $enabled ): self { |
38 | $this->where( [ 'campaign_enabled' => $enabled ? 1 : 0 ] ); |
39 | return $this; |
40 | } |
41 | |
42 | /** |
43 | * Sort campaigns by their corresponding page ids, ascending. |
44 | * |
45 | * @return $this |
46 | */ |
47 | public function orderByIdAsc(): self { |
48 | $this->orderBy( 'campaign_page_id', self::SORT_ASC ); |
49 | return $this; |
50 | } |
51 | |
52 | /** |
53 | * Fetch a single CampaignRecord. |
54 | * |
55 | * @param int $selectFlags Bitfield of CampaignStore::SELECT_* constants |
56 | * |
57 | * @return CampaignRecord|null |
58 | */ |
59 | public function fetchCampaignRecord( |
60 | int $selectFlags = CampaignStore::SELECT_MINIMAL |
61 | ): ?CampaignRecord { |
62 | $this->prepareForSelect( $selectFlags ); |
63 | |
64 | $row = $this->fetchRow(); |
65 | return $row ? $this->store->newRecordFromRow( $row, $selectFlags ) : null; |
66 | } |
67 | |
68 | /** |
69 | * Returns an iterator over resulting CampaignRecords. |
70 | * |
71 | * @param int $selectFlags Bitfield of CampaignStore::SELECT_* constants |
72 | * |
73 | * @return Iterator<CampaignRecord> |
74 | */ |
75 | public function fetchCampaignRecords( |
76 | int $selectFlags = CampaignStore::SELECT_MINIMAL |
77 | ): Iterator { |
78 | $this->prepareForSelect( $selectFlags ); |
79 | |
80 | $result = $this->fetchResultSet(); |
81 | foreach ( $result as $row ) { |
82 | yield $this->store->newRecordFromRow( $row, $selectFlags ); |
83 | } |
84 | } |
85 | |
86 | /** |
87 | * @param int $selectFlags |
88 | */ |
89 | private function prepareForSelect( int $selectFlags ): void { |
90 | $this->fields( $this->store->getSelectFields( $selectFlags ) ); |
91 | $this->tables( $this->store->getSelectTables( $selectFlags ) ); |
92 | $this->joinConds( $this->store->getJoinConds( $selectFlags ) ); |
93 | } |
94 | } |