| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
use MediaWiki\Context\IContextSource; |
| 22 |
use MediaWiki\Html\FormOptions; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
abstract class ChangesListFilter { |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
protected $name; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
protected $cssClassSuffix; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
protected $isRowApplicableCallable; |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
protected $group; |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
protected $label; |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
protected $description; |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
protected $conflictingGroups = []; |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
protected $conflictingFilters = []; |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
protected $subsetFilters = []; |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
protected $priority; |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
protected $defaultHighlightColor; |
| 111 |
|
| 112 |
private const RESERVED_NAME_CHAR = '_'; |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
public function __construct( array $filterDefinition ) { |
| 146 |
if ( isset( $filterDefinition['group'] ) ) { |
| 147 |
$this->group = $filterDefinition['group']; |
| 148 |
} else { |
| 149 |
throw new InvalidArgumentException( 'You must use \'group\' to specify the ' . |
| 150 |
'ChangesListFilterGroup this filter belongs to' ); |
| 151 |
} |
| 152 |
|
| 153 |
if ( strpos( $filterDefinition['name'], self::RESERVED_NAME_CHAR ) !== false ) { |
| 154 |
throw new InvalidArgumentException( 'Filter names may not contain \'' . |
| 155 |
self::RESERVED_NAME_CHAR . |
| 156 |
'\'. Use the naming convention: \'lowercase\'' |
| 157 |
); |
| 158 |
} |
| 159 |
|
| 160 |
if ( $this->group->getFilter( $filterDefinition['name'] ) ) { |
| 161 |
throw new InvalidArgumentException( 'Two filters in a group cannot have the ' . |
| 162 |
"same name: '{$filterDefinition['name']}'" ); |
| 163 |
} |
| 164 |
|
| 165 |
$this->name = $filterDefinition['name']; |
| 166 |
|
| 167 |
if ( isset( $filterDefinition['cssClassSuffix'] ) ) { |
| 168 |
$this->cssClassSuffix = $filterDefinition['cssClassSuffix']; |
| 169 |
|
| 170 |
$this->isRowApplicableCallable = $filterDefinition['isRowApplicableCallable']; |
| 171 |
} |
| 172 |
|
| 173 |
if ( isset( $filterDefinition['label'] ) ) { |
| 174 |
$this->label = $filterDefinition['label']; |
| 175 |
$this->description = $filterDefinition['description']; |
| 176 |
} |
| 177 |
|
| 178 |
$this->priority = $filterDefinition['priority']; |
| 179 |
|
| 180 |
$this->group->registerFilter( $this ); |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
public function conflictsWith( $other, string $globalKey, string $forwardKey, string $backwardKey ) { |
| 198 |
$this->setUnidirectionalConflict( |
| 199 |
$other, |
| 200 |
$globalKey, |
| 201 |
$forwardKey |
| 202 |
); |
| 203 |
|
| 204 |
$other->setUnidirectionalConflict( |
| 205 |
$this, |
| 206 |
$globalKey, |
| 207 |
$backwardKey |
| 208 |
); |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
public function setUnidirectionalConflict( $other, $globalDescription, $contextDescription ) { |
| 223 |
if ( $other instanceof ChangesListFilterGroup ) { |
| 224 |
$this->conflictingGroups[] = [ |
| 225 |
'group' => $other->getName(), |
| 226 |
'groupObject' => $other, |
| 227 |
'globalDescription' => $globalDescription, |
| 228 |
'contextDescription' => $contextDescription, |
| 229 |
]; |
| 230 |
} elseif ( $other instanceof ChangesListFilter ) { |
| 231 |
$this->conflictingFilters[] = [ |
| 232 |
'group' => $other->getGroup()->getName(), |
| 233 |
'filter' => $other->getName(), |
| 234 |
'filterObject' => $other, |
| 235 |
'globalDescription' => $globalDescription, |
| 236 |
'contextDescription' => $contextDescription, |
| 237 |
]; |
| 238 |
} else { |
| 239 |
throw new InvalidArgumentException( |
| 240 |
'You can only pass in a ChangesListFilterGroup or a ChangesListFilter' |
| 241 |
); |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
public function setAsSupersetOf( ChangesListFilter $other ) { |
| 255 |
if ( $other->getGroup() !== $this->getGroup() ) { |
| 256 |
throw new InvalidArgumentException( 'Supersets can only be defined for filters in the same group' ); |
| 257 |
} |
| 258 |
|
| 259 |
$this->subsetFilters[] = [ |
| 260 |
|
| 261 |
|
| 262 |
'group' => $other->getGroup()->getName(), |
| 263 |
'filter' => $other->getName(), |
| 264 |
]; |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
public function getName() { |
| 271 |
return $this->name; |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
public function getGroup() { |
| 278 |
return $this->group; |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
public function getLabel() { |
| 285 |
return $this->label; |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
public function getDescription() { |
| 292 |
return $this->description; |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
abstract public function displaysOnUnstructuredUi(); |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
public function displaysOnStructuredUi() { |
| 309 |
return $this->label !== null; |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
public function isFeatureAvailableOnStructuredUi() { |
| 320 |
return $this->displaysOnStructuredUi(); |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
public function getPriority() { |
| 327 |
return $this->priority; |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
protected function getCssClass() { |
| 336 |
if ( $this->cssClassSuffix !== null ) { |
| 337 |
return ChangesList::CSS_CLASS_PREFIX . $this->cssClassSuffix; |
| 338 |
} else { |
| 339 |
return null; |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
public function applyCssClassIfNeeded( IContextSource $ctx, RecentChange $rc, array &$classes ) { |
| 351 |
if ( $this->isRowApplicableCallable === null ) { |
| 352 |
return; |
| 353 |
} |
| 354 |
|
| 355 |
if ( call_user_func( $this->isRowApplicableCallable, $ctx, $rc ) ) { |
| 356 |
$classes[] = $this->getCssClass(); |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
public function getJsData() { |
| 368 |
$output = [ |
| 369 |
'name' => $this->getName(), |
| 370 |
'label' => $this->getLabel(), |
| 371 |
'description' => $this->getDescription(), |
| 372 |
'cssClass' => $this->getCssClass(), |
| 373 |
'priority' => $this->priority, |
| 374 |
'subset' => $this->subsetFilters, |
| 375 |
'conflicts' => [], |
| 376 |
'defaultHighlightColor' => $this->defaultHighlightColor |
| 377 |
]; |
| 378 |
|
| 379 |
$output['messageKeys'] = [ |
| 380 |
$this->getLabel(), |
| 381 |
$this->getDescription(), |
| 382 |
]; |
| 383 |
|
| 384 |
$conflicts = array_merge( |
| 385 |
$this->conflictingGroups, |
| 386 |
$this->conflictingFilters |
| 387 |
); |
| 388 |
|
| 389 |
foreach ( $conflicts as $conflictInfo ) { |
| 390 |
unset( $conflictInfo['filterObject'] ); |
| 391 |
unset( $conflictInfo['groupObject'] ); |
| 392 |
$output['conflicts'][] = $conflictInfo; |
| 393 |
array_push( |
| 394 |
$output['messageKeys'], |
| 395 |
$conflictInfo['globalDescription'], |
| 396 |
$conflictInfo['contextDescription'] |
| 397 |
); |
| 398 |
} |
| 399 |
|
| 400 |
return $output; |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
abstract public function isSelected( FormOptions $opts ); |
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
public function getConflictingGroups() { |
| 417 |
return array_column( $this->conflictingGroups, 'groupObject' ); |
| 418 |
} |
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
public function getConflictingFilters() { |
| 426 |
return array_column( $this->conflictingFilters, 'filterObject' ); |
| 427 |
} |
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
public function activelyInConflictWithGroup( ChangesListFilterGroup $group, FormOptions $opts ) { |
| 437 |
if ( $group->anySelected( $opts ) && $this->isSelected( $opts ) ) { |
| 438 |
|
| 439 |
foreach ( $this->getSiblings() as $siblingFilter ) { |
| 440 |
if ( $siblingFilter->isSelected( $opts ) && !$siblingFilter->hasConflictWithGroup( $group ) ) { |
| 441 |
return false; |
| 442 |
} |
| 443 |
} |
| 444 |
return true; |
| 445 |
} |
| 446 |
return false; |
| 447 |
} |
| 448 |
|
| 449 |
private function hasConflictWithGroup( ChangesListFilterGroup $group ) { |
| 450 |
return in_array( $group, $this->getConflictingGroups() ); |
| 451 |
} |
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
public function activelyInConflictWithFilter( ChangesListFilter $filter, FormOptions $opts ) { |
| 461 |
if ( $this->isSelected( $opts ) && $filter->isSelected( $opts ) ) { |
| 462 |
|
| 463 |
foreach ( $this->getSiblings() as $siblingFilter ) { |
| 464 |
if ( |
| 465 |
$siblingFilter->isSelected( $opts ) && |
| 466 |
!$siblingFilter->hasConflictWithFilter( $filter ) |
| 467 |
) { |
| 468 |
return false; |
| 469 |
} |
| 470 |
} |
| 471 |
return true; |
| 472 |
} |
| 473 |
return false; |
| 474 |
} |
| 475 |
|
| 476 |
private function hasConflictWithFilter( ChangesListFilter $filter ) { |
| 477 |
return in_array( $filter, $this->getConflictingFilters() ); |
| 478 |
} |
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
protected function getSiblings() { |
| 486 |
return array_filter( |
| 487 |
$this->getGroup()->getFilters(), |
| 488 |
function ( $filter ) { |
| 489 |
return $filter !== $this; |
| 490 |
} |
| 491 |
); |
| 492 |
} |
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
public function setDefaultHighlightColor( $defaultHighlightColor ) { |
| 498 |
$this->defaultHighlightColor = $defaultHighlightColor; |
| 499 |
} |
| 500 |
} |