Merge "MimeAnalyzer: Add testcases for mp3 detection"
[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 * @license GPL 2+
22 * @author Matthew Flaschen
23 */
24
25 use Wikimedia\Rdbms\IDatabase;
26
27 /**
28 * An individual filter in a boolean group
29 *
30 * @since 1.29
31 */
32 class ChangesListBooleanFilter extends ChangesListFilter {
33 // This can sometimes be different on Special:RecentChanges
34 // and Special:Watchlist, due to the double-legacy hooks
35 // (SpecialRecentChangesFilters and SpecialWatchlistFilters)
36
37 // but there will be separate sets of ChangesListFilterGroup and ChangesListFilter instances
38 // for those pages (it should work even if they're both loaded
39 // at once, but that can't happen).
40 /**
41 * Main unstructured UI i18n key
42 *
43 * @var string $showHide
44 */
45 protected $showHide;
46
47 /**
48 * Whether there is a feature designed to replace this filter available on the
49 * structured UI
50 *
51 * @var bool $isReplacedInStructuredUi
52 */
53 protected $isReplacedInStructuredUi;
54
55 /**
56 * Default
57 *
58 * @var bool $defaultValue
59 */
60 protected $defaultValue;
61
62 /**
63 * Callable used to do the actual query modification; see constructor
64 *
65 * @var callable $queryCallable
66 */
67 protected $queryCallable;
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['priority'] int Priority integer. Higher value means higher
94 * up in the group's filter list.
95 * * $filterDefinition['queryCallable'] callable Callable accepting parameters, used
96 * to implement filter's DB query modification. Required, except for legacy
97 * filters that still use the query hooks directly. Callback parameters:
98 * * string $specialPageClassName Class name of current special page
99 * * IContextSource $context Context, for e.g. user
100 * * IDatabase $dbr Database, for addQuotes, makeList, and similar
101 * * array &$tables Array of tables; see IDatabase::select $table
102 * * array &$fields Array of fields; see IDatabase::select $vars
103 * * array &$conds Array of conditions; see IDatabase::select $conds
104 * * array &$query_options Array of query options; see IDatabase::select $options
105 * * array &$join_conds Array of join conditions; see IDatabase::select $join_conds
106 */
107 public function __construct( $filterDefinition ) {
108 parent::__construct( $filterDefinition );
109
110 if ( isset( $filterDefinition['showHide'] ) ) {
111 $this->showHide = $filterDefinition['showHide'];
112 }
113
114 if ( isset( $filterDefinition['isReplacedInStructuredUi'] ) ) {
115 $this->isReplacedInStructuredUi = $filterDefinition['isReplacedInStructuredUi'];
116 } else {
117 $this->isReplacedInStructuredUi = false;
118 }
119
120 if ( isset( $filterDefinition['default'] ) ) {
121 $this->defaultValue = $filterDefinition['default'];
122 } else {
123 throw new MWException( 'You must set a default' );
124 }
125
126 if ( isset( $filterDefinition['queryCallable'] ) ) {
127 $this->queryCallable = $filterDefinition['queryCallable'];
128 }
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 false :
140 $this->defaultValue;
141 }
142
143 /**
144 * Sets default
145 *
146 * @param bool $defaultValue
147 */
148 public function setDefault( $defaultValue ) {
149 $this->defaultValue = $defaultValue;
150 }
151
152 /**
153 * @return string Main i18n key for unstructured UI
154 */
155 public function getShowHide() {
156 return $this->showHide;
157 }
158
159 /**
160 * @inheritdoc
161 */
162 public function displaysOnUnstructuredUi() {
163 return !!$this->showHide;
164 }
165
166 /**
167 * @inheritdoc
168 */
169 public function isFeatureAvailableOnStructuredUi() {
170 return $this->isReplacedInStructuredUi ||
171 parent::isFeatureAvailableOnStructuredUi();
172 }
173
174 /**
175 * Modifies the query to include the filter. This is only called if the filter is
176 * in effect (taking into account the default).
177 *
178 * @param IDatabase $dbr Database, for addQuotes, makeList, and similar
179 * @param ChangesListSpecialPage $specialPage Current special page
180 * @param array &$tables Array of tables; see IDatabase::select $table
181 * @param array &$fields Array of fields; see IDatabase::select $vars
182 * @param array &$conds Array of conditions; see IDatabase::select $conds
183 * @param array &$query_options Array of query options; see IDatabase::select $options
184 * @param array &$join_conds Array of join conditions; see IDatabase::select $join_conds
185 */
186 public function modifyQuery( IDatabase $dbr, ChangesListSpecialPage $specialPage,
187 &$tables, &$fields, &$conds, &$query_options, &$join_conds
188 ) {
189 if ( $this->queryCallable === null ) {
190 return;
191 }
192
193 call_user_func_array(
194 $this->queryCallable,
195 [
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 /**
209 * @inheritdoc
210 */
211 public function getJsData() {
212 $output = parent::getJsData();
213
214 $output['default'] = $this->defaultValue;
215
216 return $output;
217 }
218
219 /**
220 * @inheritdoc
221 */
222 public function isSelected( FormOptions $opts ) {
223 return !$opts[ $this->getName() ] &&
224 array_filter( $this->getSiblings(), function ( $sibling ) use ( $opts ) {
225 return $opts[ $sibling->getName() ];
226 } );
227 }
228 }