Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 89
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
CNBannerPager
0.00% covered (danger)
0.00%
0 / 89
0.00% covered (danger)
0.00%
0 / 7
650
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
2
 getNavigationBar
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
12
 getQueryInfo
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
12
 getIndexField
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 formatRow
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 1
90
 getBody
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
56
 getDefaultQuery
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3// FIXME Unused? See T161907
4// It's not unused, see Special:CentralNoticeBanners.  Probably needs to be
5// merged with TemplatePager.
6
7class CNBannerPager extends ReverseChronologicalPager {
8
9    /** @var bool True if the form is to be created with editable elements */
10    private $editable = false;
11
12    /** @var string Space separated strings to filter banner titles on */
13    private $filter = '';
14
15    /** @var array[] HTMLFormFields to add to the results before every banner entry */
16    private $prependPrototypes = [];
17
18    /** @var array[] HTMLFormFields to add to the results after every banner entry */
19    private $appendPrototypes = [];
20
21    /** @var string 'Section' attribute to apply to the banner elements generated */
22    private $formSection = null;
23
24    /** @var SpecialCentralNoticeBanners the page on which we appear */
25    private $hostSpecialPage;
26
27    /**
28     * @param SpecialCentralNoticeBanners $hostSpecialPage
29     * @param string|null $formSection
30     * @param array $prependPrototypes
31     * @param array $appendPrototypes
32     * @param string $bannerFilter
33     * @param bool $editable
34     */
35    public function __construct( SpecialCentralNoticeBanners $hostSpecialPage,
36        $formSection = null, $prependPrototypes = [],
37        $appendPrototypes = [], $bannerFilter = '', $editable = false
38    ) {
39        $this->editable = $editable;
40        $this->filter = $bannerFilter;
41        // Set database before parent constructor to avoid setting it there
42        $this->mDb = CNDatabase::getDb();
43
44        parent::__construct();
45
46        $this->prependPrototypes = $prependPrototypes;
47        $this->appendPrototypes = $appendPrototypes;
48        $this->formSection = $formSection;
49
50        $this->hostSpecialPage = $hostSpecialPage;
51        // Override paging defaults
52        [ $this->mLimit, $this->mOffset ] = $this->mRequest->getLimitOffsetForUser(
53            $this->getUser(),
54            20,
55            ''
56        );
57        $this->mLimitsShown = [ 20, 50, 100 ];
58    }
59
60    /**
61     * @inheritDoc
62     * @suppress PhanTypeMismatchDimAssignment
63     */
64    public function getNavigationBar() {
65        if ( isset( $this->mNavigationBar ) ) {
66            return $this->mNavigationBar;
67        }
68
69        // Sets mNavigation bar with the default text which we will then wrap
70        parent::getNavigationBar();
71
72        // @phan-suppress-next-line PhanTypeMismatchPropertyProbablyReal
73        $this->mNavigationBar = [
74            'class' => HTMLBannerPagerNavigation::class,
75            'value' => $this->mNavigationBar
76        ];
77
78        if ( $this->formSection ) {
79            // @phan-suppress-next-line PhanTypeMismatchPropertyProbablyReal
80            $this->mNavigationBar['section'] = $this->formSection;
81        }
82
83        // @phan-suppress-next-line PhanTypeMismatchReturnProbablyReal
84        return $this->mNavigationBar;
85    }
86
87    /**
88     * Set the database query to retrieve all the banners in the database
89     *
90     * @return array of query settings
91     */
92    public function getQueryInfo() {
93        // When the filter comes in it is space delimited, so break that...
94        $likeArray = preg_split( '/\s/', $this->filter );
95
96        // ...and then insert all the wildcards between search terms
97        if ( !$likeArray ) {
98            $likeArray = $this->mDb->anyString();
99        } else {
100            $anyStringToken = $this->mDb->anyString();
101            $tempArray = [ $anyStringToken ];
102            foreach ( $likeArray as $likePart ) {
103                $tempArray[] = $likePart;
104                $tempArray[] = $anyStringToken;
105            }
106            $likeArray = $tempArray;
107        }
108
109        return [
110            'tables' => [ 'templates' => 'cn_templates' ],
111            'fields' => [ 'templates.tmp_name', 'templates.tmp_id', 'templates.tmp_is_template' ],
112            'conds' => [ 'templates.tmp_name' . $this->mDb->buildLike( $likeArray ) ],
113        ];
114    }
115
116    /**
117     * Sort the banner list by tmp_id (generally equals reverse chronological)
118     *
119     * @return string
120     */
121    public function getIndexField() {
122        return 'templates.tmp_id';
123    }
124
125    /**
126     * Generate the contents of the table pager; intended to be consumed by the HTMLForm
127     *
128     * @param stdClass $row database row
129     *
130     * @return array HTMLFormElement classes
131     * @suppress PhanParamSignatureMismatch
132     */
133    public function formatRow( $row ) {
134        $retval = [];
135
136        $bannerId = $row->tmp_id;
137        $bannerName = $row->tmp_name;
138
139        // Add the prepend prototypes
140        foreach ( $this->prependPrototypes as $prototypeName => $prototypeValues ) {
141            $retval[ "{$prototypeName}-{$bannerName}" ] = $prototypeValues;
142            if ( array_key_exists( 'id', $prototypeValues ) ) {
143                $retval[ "{$prototypeName}-{$bannerId}" ][ 'id' ] .= "-$bannerName";
144            }
145        }
146
147        // Now do the banner
148        $rowText = BannerRenderer::linkToBanner( $bannerName );
149        if ( (bool)$row->tmp_is_template ) {
150            $rowText = implode( ' ', [
151                $rowText, $this->msg( "centralnotice-banner-template-info" )->escaped()
152            ] );
153        }
154        $retval["cn-banner-list-element-$bannerId"] = [
155            'class' => HTMLInfoField::class,
156            'default' => $rowText . " (" . BannerRenderer::getPreviewLink( $bannerName ) . ")",
157            'raw' => true,
158        ];
159        if ( $this->formSection ) {
160            $retval["cn-banner-list-element-$bannerId"]['section'] = $this->formSection;
161        }
162
163        // Append prototypes
164        foreach ( $this->appendPrototypes as $prototypeName => $prototypeValues ) {
165            $retval[ $prototypeName . "-$bannerId" ] = $prototypeValues;
166            if ( array_key_exists( 'id', $prototypeValues ) ) {
167                $retval[ $prototypeName . "-$bannerId" ][ 'id' ] .= "-$bannerId";
168            }
169        }
170
171        // Set the disabled attribute
172        if ( !$this->editable ) {
173            foreach ( $retval as &$prototypeValues ) {
174                $prototypeValues['disabled'] = true;
175            }
176        }
177
178        return $retval;
179    }
180
181    /**
182     * Get the formatted result list. Calls getStartBody(), formatRow() and
183     * getEndBody(), concatenates the results and returns them.
184     *
185     * @return array
186     * @suppress PhanParamSignatureMismatch
187     */
188    public function getBody() {
189        if ( !$this->mQueryDone ) {
190            $this->doQuery();
191        }
192
193        if ( $this->mResult->numRows() ) {
194            # Do any special query batches before display
195            $this->doBatchLookups();
196        }
197
198        # Don't use any extra rows returned by the query
199        $numRows = min( $this->mResult->numRows(), $this->mLimit );
200
201        $retval = [];
202
203        if ( $numRows ) {
204            if ( $this->mIsBackwards ) {
205                for ( $i = $numRows - 1; $i >= 0; $i-- ) {
206                    $this->mResult->seek( $i );
207                    $row = $this->mResult->fetchObject();
208                    $retval += $this->formatRow( $row );
209                }
210            } else {
211                $this->mResult->seek( 0 );
212                for ( $i = 0; $i < $numRows; $i++ ) {
213                    $row = $this->mResult->fetchObject();
214                    $retval += $this->formatRow( $row );
215                }
216            }
217        } else {
218            // TODO: empty value
219        }
220        return $retval;
221    }
222
223    /**
224     * @inheritDoc
225     */
226    public function getDefaultQuery() {
227        parent::getDefaultQuery();
228        $filterQuery = $this->hostSpecialPage->getFilterUrlParamAsArray();
229        $this->mDefaultQuery = array_merge( $this->mDefaultQuery, $filterQuery );
230        return $this->mDefaultQuery;
231    }
232}