Debian Codegraph @

includes/recentchanges/ChangesListBooleanFilter.php

raw symbols SCIP
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21 use MediaWiki\Html\FormOptions;
22 use MediaWiki\SpecialPage\ChangesListSpecialPage;
23 use Wikimedia\Rdbms\IReadableDatabase;
24
25 /**
26 * Represents a hide-based boolean filter (used on ChangesListSpecialPage and descendants)
27 *
28 * @since 1.29
29 * @ingroup RecentChanges
30 * @author Matthew Flaschen
31 */
32 class ChangesListBooleanFilter extends ChangesListFilter {
33 /**
34 * Main unstructured UI i18n key
35 *
36 * @var string
37 */
38 protected $showHide;
39
40 /**
41 * Whether there is a feature designed to replace this filter available on the
42 * structured UI
43 *
44 * @var bool
45 */
46 protected $isReplacedInStructuredUi;
47
48 /**
49 * Default
50 *
51 * @var bool
52 */
53 protected $defaultValue;
54
55 /**
56 * Callable used to do the actual query modification; see constructor
57 *
58 * @var callable
59 */
60 protected $queryCallable;
61
62 /**
63 * Value that defined when this filter is considered active
64 *
65 * @var bool
66 */
67 protected $activeValue;
68
69 /**
70 * Create a new filter with the specified configuration.
71 *
72 * It infers which UI (it can be either or both) to display the filter on based on
73 * which messages are provided.
74 *
75 * If 'label' is provided, it will be displayed on the structured UI. If
76 * 'showHide' is provided, it will be displayed on the unstructured UI. Thus,
77 * 'label', 'description', and 'showHide' are optional depending on which UI
78 * it's for.
79 *
80 * @param array $filterDefinition ChangesListFilter definition
81 * * $filterDefinition['name'] string Name. Used as URL parameter.
82 * * $filterDefinition['group'] ChangesListFilterGroup Group. Filter group this
83 * belongs to.
84 * * $filterDefinition['label'] string i18n key of label for structured UI.
85 * * $filterDefinition['description'] string i18n key of description for structured
86 * UI.
87 * * $filterDefinition['showHide'] string Main i18n key used for unstructured UI.
88 * * $filterDefinition['isReplacedInStructuredUi'] bool Whether there is an
89 * equivalent feature available in the structured UI; this is optional, defaulting
90 * to true. It does not need to be set if the exact same filter is simply visible
91 * on both.
92 * * $filterDefinition['default'] bool Default
93 * * $filterDefinition['activeValue'] bool This filter is considered active when
94 * its value is equal to its activeValue. Default is true.
95 * * $filterDefinition['priority'] int Priority integer. Higher value means higher
96 * up in the group's filter list.
97 * * $filterDefinition['queryCallable'] callable Callable accepting parameters, used
98 * to implement filter's DB query modification. Required, except for legacy
99 * filters that still use the query hooks directly. Callback parameters:
100 * * string $specialPageClassName Class name of current special page
101 * * IContextSource $context Context, for e.g. user
102 * * IDatabase $dbr Database, for addQuotes, makeList, and similar
103 * * array &$tables Array of tables; see IDatabase::select $table
104 * * array &$fields Array of fields; see IDatabase::select $vars
105 * * array &$conds Array of conditions; see IDatabase::select $conds
106 * * array &$query_options Array of query options; see IDatabase::select $options
107 * * array &$join_conds Array of join conditions; see IDatabase::select $join_conds
108 */
109 public function __construct( $filterDefinition ) {
110 parent::__construct( $filterDefinition );
111
112 if ( isset( $filterDefinition['showHide'] ) ) {
113 $this->showHide = $filterDefinition['showHide'];
114 }
115
116 $this->isReplacedInStructuredUi = $filterDefinition['isReplacedInStructuredUi'] ?? false;
117
118 if ( isset( $filterDefinition['default'] ) ) {
119 $this->setDefault( $filterDefinition['default'] );
120 } else {
121 throw new InvalidArgumentException( 'You must set a default' );
122 }
123
124 if ( isset( $filterDefinition['queryCallable'] ) ) {
125 $this->queryCallable = $filterDefinition['queryCallable'];
126 }
127
128 $this->activeValue = $filterDefinition['activeValue'] ?? true;
129 }
130
131 /**
132 * Get the default value
133 *
134 * @param bool $structuredUI Are we currently showing the structured UI
135 * @return bool|null Default value
136 */
137 public function getDefault( $structuredUI = false ) {
138 return $this->isReplacedInStructuredUi && $structuredUI ?
139 !$this->activeValue :
140 $this->defaultValue;
141 }
142
143 /**
144 * Sets default. It must be a boolean.
145 *
146 * It will be coerced to boolean.
147 *
148 * @param bool $defaultValue
149 */
150 public function setDefault( $defaultValue ) {
151 $this->defaultValue = (bool)$defaultValue;
152 }
153
154 /**
155 * @return string Main i18n key for unstructured UI
156 */
157 public function getShowHide() {
158 return $this->showHide;
159 }
160
161 /**
162 * @inheritDoc
163 */
164 public function displaysOnUnstructuredUi() {
165 return (bool)$this->showHide;
166 }
167
168 /**
169 * @inheritDoc
170 */
171 public function isFeatureAvailableOnStructuredUi() {
172 return $this->isReplacedInStructuredUi ||
173 parent::isFeatureAvailableOnStructuredUi();
174 }
175
176 /**
177 * Modifies the query to include the filter. This is only called if the filter is
178 * in effect (taking into account the default).
179 *
180 * @param IReadableDatabase $dbr Database, for addQuotes, makeList, and similar
181 * @param ChangesListSpecialPage $specialPage Current special page
182 * @param array &$tables Array of tables; see IDatabase::select $table
183 * @param array &$fields Array of fields; see IDatabase::select $vars
184 * @param array &$conds Array of conditions; see IDatabase::select $conds
185 * @param array &$query_options Array of query options; see IDatabase::select $options
186 * @param array &$join_conds Array of join conditions; see IDatabase::select $join_conds
187 */
188 public function modifyQuery( IReadableDatabase $dbr, ChangesListSpecialPage $specialPage,
189 &$tables, &$fields, &$conds, &$query_options, &$join_conds
190 ) {
191 if ( $this->queryCallable === null ) {
192 return;
193 }
194
195 ( $this->queryCallable )(
196 get_class( $specialPage ),
197 $specialPage->getContext(),
198 $dbr,
199 $tables,
200 $fields,
201 $conds,
202 $query_options,
203 $join_conds
204 );
205 }
206
207 /**
208 * @inheritDoc
209 */
210 public function getJsData() {
211 $output = parent::getJsData();
212
213 $output['default'] = $this->defaultValue;
214
215 return $output;
216 }
217
218 /**
219 * @inheritDoc
220 */
221 public function isSelected( FormOptions $opts ) {
222 return !$opts[ $this->getName() ] &&
223 array_filter(
224 $this->getSiblings(),
225 static function ( ChangesListBooleanFilter $sibling ) use ( $opts ) {
226 return $opts[ $sibling->getName() ];
227 }
228 );
229 }
230
231 /**
232 * @param FormOptions $opts Query parameters merged with defaults
233 * @param bool $isStructuredUI Whether the structured UI is currently enabled
234 * @return bool Whether this filter should be considered active
235 */
236 public function isActive( FormOptions $opts, $isStructuredUI ) {
237 if ( $this->isReplacedInStructuredUi && $isStructuredUI ) {
238 return false;
239 }
240
241 return $opts[ $this->getName() ] === $this->activeValue;
242 }
243 }