Debian Codegraph @

includes/recentchanges/ChangesListStringOptionsFilterGroup.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 filter group with multiple string options. They are passed to the server as
27 * a single form parameter separated by a delimiter. The parameter name is the
28 * group name. E.g. groupname=opt1;opt2 .
29 *
30 * If all options are selected they are replaced by the term "all".
31 *
32 * There is also a single DB query modification for the whole group.
33 *
34 * @since 1.29
35 * @ingroup RecentChanges
36 * @author Matthew Flaschen
37 */
38 class ChangesListStringOptionsFilterGroup extends ChangesListFilterGroup {
39 /**
40 * Type marker, used by JavaScript
41 */
42 public const TYPE = 'string_options';
43
44 /**
45 * Delimiter
46 */
47 public const SEPARATOR = ';';
48
49 /**
50 * Signifies that all options in the group are selected.
51 */
52 public const ALL = 'all';
53
54 /**
55 * Signifies that no options in the group are selected, meaning the group has no effect.
56 *
57 * For full-coverage groups, this is the same as ALL if all filters are allowed.
58 * For others, it is not.
59 */
60 public const NONE = '';
61
62 /**
63 * Default parameter value
64 *
65 * @var string
66 */
67 protected $defaultValue;
68
69 /**
70 * Callable used to do the actual query modification; see constructor
71 *
72 * @var callable
73 */
74 protected $queryCallable;
75
76 /**
77 * Create a new filter group with the specified configuration
78 *
79 * @param array $groupDefinition Configuration of group
80 * * $groupDefinition['name'] string Group name
81 * * $groupDefinition['title'] string i18n key for title (optional, can be omitted
82 * only if none of the filters in the group display in the structured UI)
83 * * $groupDefinition['priority'] int Priority integer. Higher means higher in the
84 * group list.
85 * * $groupDefinition['filters'] array Numeric array of filter definitions, each of which
86 * is an associative array to be passed to the filter constructor. However,
87 * 'priority' is optional for the filters. Any filter that has priority unset
88 * will be put to the bottom, in the order given.
89 * * $groupDefinition['default'] string Default for group.
90 * * $groupDefinition['isFullCoverage'] bool Whether the group is full coverage;
91 * if true, this means that checking every item in the group means no
92 * changes list entries are filtered out.
93 * * $groupDefinition['queryCallable'] callable Callable accepting parameters:
94 * * string $specialPageClassName Class name of current special page
95 * * IContextSource $context Context, for e.g. user
96 * * IDatabase $dbr Database, for addQuotes, makeList, and similar
97 * * array &$tables Array of tables; see IDatabase::select $table
98 * * array &$fields Array of fields; see IDatabase::select $vars
99 * * array &$conds Array of conditions; see IDatabase::select $conds
100 * * array &$query_options Array of query options; see IDatabase::select $options
101 * * array &$join_conds Array of join conditions; see IDatabase::select $join_conds
102 * * array $selectedValues The allowed and requested values, lower-cased and sorted
103 * * $groupDefinition['whatsThisHeader'] string i18n key for header of "What's
104 * This" popup (optional).
105 * * $groupDefinition['whatsThisBody'] string i18n key for body of "What's This"
106 * popup (optional).
107 * * $groupDefinition['whatsThisUrl'] string URL for main link of "What's This"
108 * popup (optional).
109 * * $groupDefinition['whatsThisLinkText'] string i18n key of text for main link of
110 * "What's This" popup (optional).
111 */
112 public function __construct( array $groupDefinition ) {
113 if ( !isset( $groupDefinition['isFullCoverage'] ) ) {
114 throw new InvalidArgumentException( 'You must specify isFullCoverage' );
115 }
116
117 $groupDefinition['type'] = self::TYPE;
118
119 parent::__construct( $groupDefinition );
120
121 $this->queryCallable = $groupDefinition['queryCallable'];
122
123 if ( isset( $groupDefinition['default'] ) ) {
124 $this->setDefault( $groupDefinition['default'] );
125 } else {
126 throw new InvalidArgumentException( 'You must specify a default' );
127 }
128 }
129
130 /**
131 * Sets default of filter group.
132 *
133 * @param string $defaultValue
134 */
135 public function setDefault( $defaultValue ) {
136 $this->defaultValue = $defaultValue;
137 }
138
139 /**
140 * Gets default of filter group
141 *
142 * @return string
143 */
144 public function getDefault() {
145 return $this->defaultValue;
146 }
147
148 /**
149 * @inheritDoc
150 */
151 protected function createFilter( array $filterDefinition ) {
152 return new ChangesListStringOptionsFilter( $filterDefinition );
153 }
154
155 /**
156 * Registers a filter in this group
157 *
158 * @param ChangesListStringOptionsFilter $filter
159 * @suppress PhanParamSignaturePHPDocMismatchHasParamType,PhanParamSignatureMismatch
160 */
161 public function registerFilter( ChangesListStringOptionsFilter $filter ) {
162 $this->filters[$filter->getName()] = $filter;
163 }
164
165 /**
166 * @inheritDoc
167 */
168 public function modifyQuery( IReadableDatabase $dbr, ChangesListSpecialPage $specialPage,
169 &$tables, &$fields, &$conds, &$query_options, &$join_conds,
170 FormOptions $opts, $isStructuredFiltersEnabled
171 ) {
172 // STRING_OPTIONS filter groups are exclusively active on Structured UI
173 if ( !$isStructuredFiltersEnabled ) {
174 return;
175 }
176
177 $value = $opts[ $this->getName() ];
178 $allowedFilterNames = [];
179 foreach ( $this->filters as $filter ) {
180 $allowedFilterNames[] = $filter->getName();
181 }
182
183 if ( $value === self::ALL ) {
184 $selectedValues = $allowedFilterNames;
185 } else {
186 $selectedValues = explode( self::SEPARATOR, strtolower( $value ) );
187
188 // remove values that are not recognized or not currently allowed
189 $selectedValues = array_intersect(
190 $selectedValues,
191 $allowedFilterNames
192 );
193 }
194
195 // If there are now no values, because all are disallowed or invalid (also,
196 // the user may not have selected any), this is a no-op.
197
198 // If everything is unchecked, the group always has no effect, regardless
199 // of full-coverage.
200 if ( count( $selectedValues ) === 0 ) {
201 return;
202 }
203
204 sort( $selectedValues );
205
206 ( $this->queryCallable )(
207 get_class( $specialPage ),
208 $specialPage->getContext(),
209 $dbr,
210 $tables,
211 $fields,
212 $conds,
213 $query_options,
214 $join_conds,
215 $selectedValues
216 );
217 }
218
219 /**
220 * @inheritDoc
221 */
222 public function getJsData() {
223 $output = parent::getJsData();
224
225 $output['separator'] = self::SEPARATOR;
226 $output['default'] = $this->getDefault();
227
228 return $output;
229 }
230
231 /**
232 * @inheritDoc
233 */
234 public function addOptions( FormOptions $opts, $allowDefaults, $isStructuredFiltersEnabled ) {
235 $opts->add( $this->getName(), $allowDefaults ? $this->getDefault() : '' );
236 }
237 }