MediaWiki master
BooleanJoinFieldCondition.php
Go to the documentation of this file.
1<?php
2
4
5use stdClass;
7
15 public function __construct(
16 private string $fieldName,
17 private string $tableName
18 ) {
19 }
20
22 public function validateValue( $value ) {
23 if ( $value === null || is_bool( $value ) ) {
24 return $value;
25 }
26 throw new \InvalidArgumentException( 'Invalid value for tri-state boolean' );
27 }
28
30 public function evaluate( stdClass $row, $value ): bool {
31 $rowValue = $row->{ $this->fieldName };
32 if ( $rowValue !== null ) {
33 $rowValue = (bool)$rowValue;
34 }
35 return $rowValue === $value;
36 }
37
39 protected function prepareCapture( IReadableDatabase $dbr, QueryBackend $query ) {
40 $query->joinForFields( $this->tableName )->weakLeft();
41 $query->fields( $this->fieldName );
42 }
43
45 protected function prepareConds( IReadableDatabase $dbr, QueryBackend $query ) {
46 $set = $this->getEnumValues( [ false, true, null ] );
47 if ( $set === null ) {
48 return;
49 } elseif ( $set === [] ) {
50 $query->forceEmptySet();
51 }
52 $null = in_array( null, $set, true );
53 $false = in_array( false, $set, true );
54 $true = in_array( true, $set, true );
55 if ( $null ) {
56 $query->joinForConds( $this->tableName )->left();
57 } else {
58 $query->joinForConds( $this->tableName )->reorderable();
59 }
60
61 if ( !$true || !$false || $null ) {
62 $orConds = [];
63 foreach ( $set as $value ) {
64 $orConds[] = $dbr->expr( $this->fieldName, '=', $value );
65 }
66 if ( count( $orConds ) > 1 ) {
67 $query->where( $dbr->orExpr( $orConds ) );
68 } else {
69 $query->where( $orConds[0] );
70 }
71 } /* else implied by the join conditions */
72 }
73}
A tri-state boolean from a field of a potentially left-joined table.
__construct(private string $fieldName, private string $tableName)
evaluate(stdClass $row, $value)
Evaluate the filter condition against a row, determining whether it is true or false....
prepareConds(IReadableDatabase $dbr, QueryBackend $query)
Add conditions to the query according to the values passed to require() and exclude()....
validateValue( $value)
Validate a value and return its normalized form.mixed
The narrow interface passed to filter modules.
forceEmptySet()
Set a flag forcing the query to return no rows when it is executed.
fields( $fields)
Add fields to the query.
where(IExpression $expr)
Add a condition to the query.
joinForFields(string $table)
Join on the specified table and declare that it will be used to provide fields for the SELECT clause.
joinForConds(string $table)
Join on the specified table and declare that it will be used to provide fields for the WHERE clause.
A database connection without write operations.
expr(string $field, string $op, $value)
See Expression::__construct()
orExpr(array $conds)
See Expression::__construct()