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