group = $filterDefinition['group']; } else { throw new MWException( 'You must use \'group\' to specify the ' . 'ChangesListFilterGroup this filter belongs to' ); } if ( strpos( $filterDefinition['name'], self::RESERVED_NAME_CHAR ) !== false ) { throw new MWException( 'Filter names may not contain \'' . self::RESERVED_NAME_CHAR . '\'. Use the naming convention: \'lowercase\'' ); } if ( $this->group->getFilter( $filterDefinition['name'] ) ) { throw new MWException( 'Two filters in a group cannot have the ' . "same name: '{$filterDefinition['name']}'" ); } $this->name = $filterDefinition['name']; if ( isset( $filterDefinition['cssClassSuffix'] ) ) { $this->cssClassSuffix = $filterDefinition['cssClassSuffix']; $this->isRowApplicableCallable = $filterDefinition['isRowApplicableCallable']; } if ( isset( $filterDefinition['label'] ) ) { $this->label = $filterDefinition['label']; $this->description = $filterDefinition['description']; } $this->priority = $filterDefinition['priority']; $this->group->registerFilter( $this ); } /** * Marks that the given ChangesListFilterGroup or ChangesListFilter conflicts with this object. * * WARNING: This means there is a conflict when both things are *shown* * (not filtered out), even for the hide-based filters. So e.g. conflicting with * 'hideanons' means there is a conflict if only anonymous users are *shown*. * * @param ChangesListFilterGroup|ChangesListFilter $other Other * ChangesListFilterGroup or ChangesListFilter * @param string $globalKey i18n key for top-level conflict message * @param string $forwardKey i18n key for conflict message in this * direction (when in UI context of $this object) * @param string $backwardKey i18n key for conflict message in reverse * direction (when in UI context of $other object) */ public function conflictsWith( $other, $globalKey, $forwardKey, $backwardKey ) { if ( $globalKey === null || $forwardKey === null || $backwardKey === null ) { throw new MWException( 'All messages must be specified' ); } $this->setUnidirectionalConflict( $other, $globalKey, $forwardKey ); $other->setUnidirectionalConflict( $this, $globalKey, $backwardKey ); } /** * Marks that the given ChangesListFilterGroup or ChangesListFilter conflicts with * this object. * * Internal use ONLY. * * @param ChangesListFilterGroup|ChangesListFilter $other Other * ChangesListFilterGroup or ChangesListFilter * @param string $globalDescription i18n key for top-level conflict message * @param string $contextDescription i18n key for conflict message in this * direction (when in UI context of $this object) */ public function setUnidirectionalConflict( $other, $globalDescription, $contextDescription ) { if ( $other instanceof ChangesListFilterGroup ) { $this->conflictingGroups[] = [ 'group' => $other->getName(), 'groupObject' => $other, 'globalDescription' => $globalDescription, 'contextDescription' => $contextDescription, ]; } elseif ( $other instanceof ChangesListFilter ) { $this->conflictingFilters[] = [ 'group' => $other->getGroup()->getName(), 'filter' => $other->getName(), 'filterObject' => $other, 'globalDescription' => $globalDescription, 'contextDescription' => $contextDescription, ]; } else { throw new MWException( 'You can only pass in a ChangesListFilterGroup or a ChangesListFilter' ); } } /** * Marks that the current instance is (also) a superset of the filter passed in. * This can be called more than once. * * This means that anything in the results for the other filter is also in the * results for this one. * * @param ChangesListFilter $other The filter the current instance is a superset of */ public function setAsSupersetOf( ChangesListFilter $other ) { if ( $other->getGroup() !== $this->getGroup() ) { throw new MWException( 'Supersets can only be defined for filters in the same group' ); } $this->subsetFilters[] = [ // It's always the same group, but this makes the representation // more consistent with conflicts. 'group' => $other->getGroup()->getName(), 'filter' => $other->getName(), ]; } /** * @return string Name, e.g. hideanons */ public function getName() { return $this->name; } /** * @return ChangesListFilterGroup Group this belongs to */ public function getGroup() { return $this->group; } /** * @return string i18n key of label for structured UI */ public function getLabel() { return $this->label; } /** * @return string i18n key of description for structured UI */ public function getDescription() { return $this->description; } /** * Checks whether the filter should display on the unstructured UI * * @return bool Whether to display */ abstract public function displaysOnUnstructuredUi(); /** * Checks whether the filter should display on the structured UI * This refers to the exact filter. See also isFeatureAvailableOnStructuredUi. * * @return bool Whether to display */ public function displaysOnStructuredUi() { return $this->label !== null; } /** * Checks whether an equivalent feature for this filter is available on the * structured UI. * * This can either be the exact filter, or a new filter that replaces it. * @return bool */ public function isFeatureAvailableOnStructuredUi() { return $this->displaysOnStructuredUi(); } /** * @return int Priority. Higher value means higher up in the group list */ public function getPriority() { return $this->priority; } /** * Gets the CSS class * * @return string|null CSS class, or null if not defined */ protected function getCssClass() { if ( $this->cssClassSuffix !== null ) { return ChangesList::CSS_CLASS_PREFIX . $this->cssClassSuffix; } else { return null; } } /** * Add CSS class if needed * * @param IContextSource $ctx Context source * @param RecentChange $rc Recent changes object * @param array &$classes Non-associative array of CSS class names; appended to if needed */ public function applyCssClassIfNeeded( IContextSource $ctx, RecentChange $rc, array &$classes ) { if ( $this->isRowApplicableCallable === null ) { return; } if ( call_user_func( $this->isRowApplicableCallable, $ctx, $rc ) ) { $classes[] = $this->getCssClass(); } } /** * Gets the JS data required by the front-end of the structured UI * * @return array Associative array Data required by the front-end. messageKeys is * a special top-level value, with the value being an array of the message keys to * send to the client. */ public function getJsData() { $output = [ 'name' => $this->getName(), 'label' => $this->getLabel(), 'description' => $this->getDescription(), 'cssClass' => $this->getCssClass(), 'priority' => $this->priority, 'subset' => $this->subsetFilters, 'conflicts' => [], 'defaultHighlightColor' => $this->defaultHighlightColor ]; $output['messageKeys'] = [ $this->getLabel(), $this->getDescription(), ]; $conflicts = array_merge( $this->conflictingGroups, $this->conflictingFilters ); foreach ( $conflicts as $conflictInfo ) { unset( $conflictInfo['filterObject'] ); unset( $conflictInfo['groupObject'] ); $output['conflicts'][] = $conflictInfo; array_push( $output['messageKeys'], $conflictInfo['globalDescription'], $conflictInfo['contextDescription'] ); } return $output; } /** * Checks whether this filter is selected in the provided options * * @param FormOptions $opts * @return bool */ abstract public function isSelected( FormOptions $opts ); /** * Get groups conflicting with this filter * * @return ChangesListFilterGroup[] */ public function getConflictingGroups() { return array_map( function ( $conflictDesc ) { return $conflictDesc[ 'groupObject' ]; }, $this->conflictingGroups ); } /** * Get filters conflicting with this filter * * @return ChangesListFilter[] */ public function getConflictingFilters() { return array_map( function ( $conflictDesc ) { return $conflictDesc[ 'filterObject' ]; }, $this->conflictingFilters ); } /** * Check if the conflict with a group is currently "active" * * @param ChangesListFilterGroup $group * @param FormOptions $opts * @return bool */ public function activelyInConflictWithGroup( ChangesListFilterGroup $group, FormOptions $opts ) { if ( $group->anySelected( $opts ) && $this->isSelected( $opts ) ) { /** @var ChangesListFilter $siblingFilter */ foreach ( $this->getSiblings() as $siblingFilter ) { if ( $siblingFilter->isSelected( $opts ) && !$siblingFilter->hasConflictWithGroup( $group ) ) { return false; } } return true; } return false; } private function hasConflictWithGroup( ChangesListFilterGroup $group ) { return in_array( $group, $this->getConflictingGroups() ); } /** * Check if the conflict with a filter is currently "active" * * @param ChangesListFilter $filter * @param FormOptions $opts * @return bool */ public function activelyInConflictWithFilter( ChangeslistFilter $filter, FormOptions $opts ) { if ( $this->isSelected( $opts ) && $filter->isSelected( $opts ) ) { /** @var ChangesListFilter $siblingFilter */ foreach ( $this->getSiblings() as $siblingFilter ) { if ( $siblingFilter->isSelected( $opts ) && !$siblingFilter->hasConflictWithFilter( $filter ) ) { return false; } } return true; } return false; } private function hasConflictWithFilter( ChangeslistFilter $filter ) { return in_array( $filter, $this->getConflictingFilters() ); } /** * Get filters in the same group * * @return ChangesListFilter[] */ protected function getSiblings() { return array_filter( $this->getGroup()->getFilters(), function ( $filter ) { return $filter !== $this; } ); } /** * @param string $defaultHighlightColor */ public function setDefaultHighlightColor( $defaultHighlightColor ) { $this->defaultHighlightColor = $defaultHighlightColor; } }