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