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