MediaWiki master
BooleanJoinFieldCondition.php
Go to the documentation of this file.
1<?php
2
4
5use stdClass;
7
17 public function __construct(
18 private string $fieldName,
19 private string $tableName
20 ) {
21 }
22
24 public function validateValue( $value ) {
25 if ( $value === null || is_bool( $value ) ) {
26 return $value;
27 }
28 throw new \InvalidArgumentException( 'Invalid value for tri-state boolean' );
29 }
30
32 public function evaluate( stdClass $row, $value ): bool {
33 $rowValue = $row->{ $this->fieldName };
34 if ( $rowValue !== null ) {
35 $rowValue = (bool)$rowValue;
36 }
37 return $rowValue === $value;
38 }
39
41 protected function prepareCapture( IReadableDatabase $dbr, QueryBackend $query ) {
42 $query->joinForFields( $this->tableName )->weakLeft();
43 $query->fields( $this->fieldName );
44 }
45
47 protected function prepareConds( IReadableDatabase $dbr, QueryBackend $query ) {
48 $set = $this->getEnumValues( [ false, true, null ] );
49 if ( $set === null ) {
50 return;
51 } elseif ( $set === [] ) {
52 $query->forceEmptySet();
53 }
54 $null = in_array( null, $set, true );
55 $false = in_array( false, $set, true );
56 $true = in_array( true, $set, true );
57 if ( $null ) {
58 $query->joinForConds( $this->tableName )->left();
59 } else {
60 $query->joinForConds( $this->tableName )->reorderable();
61 }
62
63 if ( !$true || !$false || $null ) {
64 $orConds = [];
65 foreach ( $set as $value ) {
66 $orConds[] = $dbr->expr( $this->fieldName, '=', $value );
67 }
68 if ( count( $orConds ) > 1 ) {
69 $query->where( $dbr->orExpr( $orConds ) );
70 } else {
71 $query->where( $orConds[0] );
72 }
73 } /* else implied by the join conditions */
74 }
75}
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()