MediaWiki master
RevisionTypeCondition.php
Go to the documentation of this file.
1<?php
2
4
5use stdClass;
8
10
11 private const TYPES = [ 'none', 'old', 'latest' ];
12
14 public function validateValue( $value ) {
15 if ( !in_array( $value, self::TYPES, true ) ) {
16 throw new \InvalidArgumentException(
17 "revisionType must be one of: none, old, latest"
18 );
19 }
20 return $value;
21 }
22
24 public function evaluate( stdClass $row, $value ): bool {
25 if ( (int)$row->rc_this_oldid === 0 ) {
26 $type = 'none';
27 } elseif ( (int)$row->rc_this_oldid === (int)$row->page_latest ) {
28 $type = 'latest';
29 } else {
30 $type = 'old';
31 }
32 return $value === $type;
33 }
34
36 protected function prepareCapture( IReadableDatabase $dbr, QueryBackend $query ) {
37 $query->joinForFields( 'page' )->weakLeft();
38 $query->fields( [ 'rc_this_oldid', 'page_latest' ] );
39 }
40
42 protected function prepareConds( IReadableDatabase $dbr, QueryBackend $query ) {
43 $required = $this->getEnumValues( self::TYPES );
44 if ( $required === null ) {
45 return;
46 } elseif ( !$required ) {
47 $query->forceEmptySet();
48 } else {
49 $query->joinForConds( 'page' )->weakLeft();
50 $orConds = [];
51 $req = $this->flip( $required );
52 if ( $req['latest'] xor $req['old'] ) {
53 $op = $req['latest'] ? '=' : '!=';
54 $orConds[] = $dbr->expr( 'rc_this_oldid', $op, new RawSQLValue( 'page_latest' ) );
55 }
56 if ( $req['none'] ) {
57 $orConds[] = $dbr->expr( 'rc_this_oldid', '=', 0 );
58 }
59 if ( $orConds ) {
60 $query->where( $dbr->orExpr( $orConds ) );
61 }
62 }
63 }
64
70 private function flip( $values ) {
71 return array_fill_keys( $values, true ) +
72 array_fill_keys( self::TYPES, false );
73 }
74}
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:69
prepareCapture(IReadableDatabase $dbr, QueryBackend $query)
evaluate(stdClass $row, $value)
Evaluate the filter condition against a row, determining whether it is true or false....
validateValue( $value)
Validate a value and return its normalized form.mixed
prepareConds(IReadableDatabase $dbr, QueryBackend $query)
Add conditions to the query according to the values passed to require() and exclude()....
Raw SQL value to be used in query builders.
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()