MediaWiki  master
ChangesListStringOptionsFilterGroup.php
Go to the documentation of this file.
1 <?php
25 
41  const TYPE = 'string_options';
42 
46  const SEPARATOR = ';';
47 
51  const ALL = 'all';
52 
59  const NONE = '';
60 
66  protected $defaultValue;
67 
73  protected $queryCallable;
74 
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 
134  public function setDefault( $defaultValue ) {
135  $this->defaultValue = $defaultValue;
136  }
137 
143  public function getDefault() {
144  return $this->defaultValue;
145  }
146 
150  protected function createFilter( array $filterDefinition ) {
151  return new ChangesListStringOptionsFilter( $filterDefinition );
152  }
153 
160  public function registerFilter( ChangesListStringOptionsFilter $filter ) {
161  $this->filters[$filter->getName()] = $filter;
162  }
163 
167  public function modifyQuery( IDatabase $dbr, ChangesListSpecialPage $specialPage,
168  &$tables, &$fields, &$conds, &$query_options, &$join_conds,
169  FormOptions $opts, $isStructuredFiltersEnabled
170  ) {
171  if ( !$this->isActive( $isStructuredFiltersEnabled ) ) {
172  return;
173  }
174 
175  $value = $opts[ $this->getName() ];
176  $allowedFilterNames = [];
177  foreach ( $this->filters as $filter ) {
178  $allowedFilterNames[] = $filter->getName();
179  }
180 
181  if ( $value === self::ALL ) {
182  $selectedValues = $allowedFilterNames;
183  } else {
184  $selectedValues = explode( self::SEPARATOR, strtolower( $value ) );
185 
186  // remove values that are not recognized or not currently allowed
187  $selectedValues = array_intersect(
188  $selectedValues,
189  $allowedFilterNames
190  );
191  }
192 
193  // If there are now no values, because all are disallowed or invalid (also,
194  // the user may not have selected any), this is a no-op.
195 
196  // If everything is unchecked, the group always has no effect, regardless
197  // of full-coverage.
198  if ( count( $selectedValues ) === 0 ) {
199  return;
200  }
201 
202  sort( $selectedValues );
203 
205  get_class( $specialPage ),
206  $specialPage->getContext(),
207  $dbr,
208  $tables,
209  $fields,
210  $conds,
211  $query_options,
212  $join_conds,
213  $selectedValues
214  );
215  }
216 
220  public function getJsData() {
221  $output = parent::getJsData();
222 
223  $output['separator'] = self::SEPARATOR;
224  $output['default'] = $this->getDefault();
225 
226  return $output;
227  }
228 
232  public function addOptions( FormOptions $opts, $allowDefaults, $isStructuredFiltersEnabled ) {
233  $opts->add( $this->getName(), $allowDefaults ? $this->getDefault() : '' );
234  }
235 
242  private function isActive( $isStructuredUI ) {
243  // STRING_OPTIONS filter groups are exclusively active on Structured UI
244  return $isStructuredUI;
245  }
246 }
Helper class to keep track of options when mixing links and form elements.
Definition: FormOptions.php:35
addOptions(FormOptions $opts, $allowDefaults, $isStructuredFiltersEnabled)
getContext()
Gets the context this SpecialPage is executed in.
const TYPE
Type marker, used by JavaScript.
An individual filter in a ChangesListStringOptionsFilterGroup.
add( $name, $default, $type=self::AUTO)
Add an option to be handled by this FormOptions instance.
Definition: FormOptions.php:83
isActive( $isStructuredUI)
Check if this filter group is currently active.
__construct(array $groupDefinition)
Create a new filter group with the specified configuration.
setDefault( $defaultValue)
Sets default of filter group.
Represents a filter group (used on ChangesListSpecialPage and descendants)
modifyQuery(IDatabase $dbr, ChangesListSpecialPage $specialPage, &$tables, &$fields, &$conds, &$query_options, &$join_conds, FormOptions $opts, $isStructuredFiltersEnabled)
Represents a filter group with multiple string options.
const ALL
Signifies that all options in the group are selected.
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:38
const NONE
Signifies that no options in the group are selected, meaning the group has no effect.
Special page which uses a ChangesList to show query results.
$queryCallable
Callable used to do the actual query modification; see constructor.
registerFilter(ChangesListStringOptionsFilter $filter)
Registers a filter in this group.