Move IDatabase/IMaintainableDatabase to Rdbms namespace
[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 */
111 public function __construct( array $groupDefinition ) {
112 if ( !isset( $groupDefinition['isFullCoverage'] ) ) {
113 throw new MWException( 'You must specify isFullCoverage' );
114 }
115
116 $groupDefinition['type'] = self::TYPE;
117
118 parent::__construct( $groupDefinition );
119
120 $this->queryCallable = $groupDefinition['queryCallable'];
121
122 if ( isset( $groupDefinition['default'] ) ) {
123 $this->setDefault( $groupDefinition['default'] );
124 } else {
125 throw new MWException( 'You must specify a default' );
126 }
127 }
128
129 /**
130 * @inheritdoc
131 */
132 public function isPerGroupRequestParameter() {
133 return true;
134 }
135
136 /**
137 * Sets default of filter group.
138 *
139 * @param string $defaultValue
140 */
141 public function setDefault( $defaultValue ) {
142 $this->defaultValue = $defaultValue;
143 }
144
145 /**
146 * Gets default of filter group
147 *
148 * @return string $defaultValue
149 */
150 public function getDefault() {
151 return $this->defaultValue;
152 }
153
154 /**
155 * @inheritdoc
156 */
157 protected function createFilter( array $filterDefinition ) {
158 return new ChangesListStringOptionsFilter( $filterDefinition );
159 }
160
161 /**
162 * Registers a filter in this group
163 *
164 * @param ChangesListStringOptionsFilter $filter ChangesListStringOptionsFilter
165 */
166 public function registerFilter( ChangesListStringOptionsFilter $filter ) {
167 $this->filters[$filter->getName()] = $filter;
168 }
169
170 /**
171 * Modifies the query to include the filter group.
172 *
173 * The modification is only done if the filter group is in effect. This means that
174 * one or more valid and allowed filters were selected.
175 *
176 * @param IDatabase $dbr Database, for addQuotes, makeList, and similar
177 * @param ChangesListSpecialPage $specialPage Current special page
178 * @param array &$tables Array of tables; see IDatabase::select $table
179 * @param array &$fields Array of fields; see IDatabase::select $vars
180 * @param array &$conds Array of conditions; see IDatabase::select $conds
181 * @param array &$query_options Array of query options; see IDatabase::select $options
182 * @param array &$join_conds Array of join conditions; see IDatabase::select $join_conds
183 * @param string $value URL parameter value
184 */
185 public function modifyQuery( IDatabase $dbr, ChangesListSpecialPage $specialPage,
186 &$tables, &$fields, &$conds, &$query_options, &$join_conds, $value ) {
187
188 $allowedFilterNames = [];
189 foreach ( $this->filters as $filter ) {
190 if ( $filter->isAllowed( $specialPage ) ) {
191 $allowedFilterNames[] = $filter->getName();
192 }
193 }
194
195 if ( $value === self::ALL ) {
196 $selectedValues = $allowedFilterNames;
197 } else {
198 $selectedValues = explode( self::SEPARATOR, strtolower( $value ) );
199
200 // remove values that are not recognized or not currently allowed
201 $selectedValues = array_intersect(
202 $selectedValues,
203 $allowedFilterNames
204 );
205 }
206
207 // If there are now no values, because all are disallowed or invalid (also,
208 // the user may not have selected any), this is a no-op.
209
210 // If everything is unchecked, the group always has no effect, regardless
211 // of full-coverage.
212 if ( count( $selectedValues ) === 0 ) {
213 return;
214 }
215
216 sort( $selectedValues );
217
218 call_user_func_array(
219 $this->queryCallable,
220 [
221 get_class( $specialPage ),
222 $specialPage->getContext(),
223 $dbr,
224 &$tables,
225 &$fields,
226 &$conds,
227 &$query_options,
228 &$join_conds,
229 $selectedValues
230 ]
231 );
232 }
233
234 /**
235 * @inheritdoc
236 */
237 public function getJsData( ChangesListSpecialPage $specialPage ) {
238 $output = parent::getJsData( $specialPage );
239
240 $output['separator'] = self::SEPARATOR;
241 $output['default'] = $this->getDefault();
242
243 return $output;
244 }
245 }