2016-12-17 17:52:36 +00:00
|
|
|
<?php
|
2020-12-03 22:22:43 +00:00
|
|
|
|
|
|
|
namespace MediaWiki\Extension\AbuseFilter\Parser;
|
|
|
|
|
2016-12-17 17:52:36 +00:00
|
|
|
/**
|
|
|
|
* Represents a node of a parser tree.
|
|
|
|
*/
|
|
|
|
class AFPTreeNode {
|
|
|
|
// Each of the constants below represents a node corresponding to a level
|
|
|
|
// of the parser, from the top of the tree to the bottom.
|
|
|
|
|
|
|
|
// ENTRY is always one-element and thus does not have its own node.
|
|
|
|
|
|
|
|
// SEMICOLON is a many-children node, denoting that the nodes have to be
|
|
|
|
// evaluated in order and the last value has to be returned.
|
2020-01-21 11:13:11 +00:00
|
|
|
public const SEMICOLON = 'SEMICOLON';
|
2016-12-17 17:52:36 +00:00
|
|
|
|
|
|
|
// ASSIGNMENT (formerly known as SET) is a node which is responsible for
|
|
|
|
// assigning values to variables. ASSIGNMENT is a (variable name [string],
|
|
|
|
// value [tree node]) tuple, INDEX_ASSIGNMENT (which is used to assign
|
2018-04-16 15:37:10 +00:00
|
|
|
// values at array offsets) is a (variable name [string], index [tree node],
|
|
|
|
// value [tree node]) tuple, and ARRAY_APPEND has the form of (variable name
|
2016-12-17 17:52:36 +00:00
|
|
|
// [string], value [tree node]).
|
2020-01-21 11:13:11 +00:00
|
|
|
public const ASSIGNMENT = 'ASSIGNMENT';
|
|
|
|
public const INDEX_ASSIGNMENT = 'INDEX_ASSIGNMENT';
|
|
|
|
public const ARRAY_APPEND = 'ARRAY_APPEND';
|
2016-12-17 17:52:36 +00:00
|
|
|
|
|
|
|
// CONDITIONAL represents both a ternary operator and an if-then-else-end
|
2019-08-20 16:19:31 +00:00
|
|
|
// construct. The format is (condition, evaluated-if-true, evaluated-in-false).
|
|
|
|
// The first two are tree nodes, the last one can be a node, or null if there's no else.
|
2020-01-21 11:13:11 +00:00
|
|
|
public const CONDITIONAL = 'CONDITIONAL';
|
2016-12-17 17:52:36 +00:00
|
|
|
|
|
|
|
// LOGIC is a logic operator accepted by AFPData::boolOp. The format is
|
|
|
|
// (operation, left operand, right operand).
|
2020-01-21 11:13:11 +00:00
|
|
|
public const LOGIC = 'LOGIC';
|
2016-12-17 17:52:36 +00:00
|
|
|
|
|
|
|
// COMPARE is a comparison operator accepted by AFPData::boolOp. The format is
|
|
|
|
// (operation, left operand, right operand).
|
2020-01-21 11:13:11 +00:00
|
|
|
public const COMPARE = 'COMPARE';
|
2016-12-17 17:52:36 +00:00
|
|
|
|
|
|
|
// SUM_REL is either '+' or '-'. The format is (operation, left operand,
|
|
|
|
// right operand).
|
2020-01-21 11:13:11 +00:00
|
|
|
public const SUM_REL = 'SUM_REL';
|
2016-12-17 17:52:36 +00:00
|
|
|
|
|
|
|
// MUL_REL is a multiplication-related operation accepted by AFPData::mulRel.
|
|
|
|
// The format is (operation, left operand, right operand).
|
2020-01-21 11:13:11 +00:00
|
|
|
public const MUL_REL = 'MUL_REL';
|
2016-12-17 17:52:36 +00:00
|
|
|
|
|
|
|
// POW is an exponentiation operator. The format is (base, exponent).
|
2020-01-21 11:13:11 +00:00
|
|
|
public const POW = 'POW';
|
2016-12-17 17:52:36 +00:00
|
|
|
|
|
|
|
// BOOL_INVERT is a boolean inversion operator. The format is (operand).
|
2020-01-21 11:13:11 +00:00
|
|
|
public const BOOL_INVERT = 'BOOL_INVERT';
|
2016-12-17 17:52:36 +00:00
|
|
|
|
|
|
|
// KEYWORD_OPERATOR is one of the binary keyword operators supported by the
|
|
|
|
// filter language. The format is (keyword, left operand, right operand).
|
2020-01-21 11:13:11 +00:00
|
|
|
public const KEYWORD_OPERATOR = 'KEYWORD_OPERATOR';
|
2016-12-17 17:52:36 +00:00
|
|
|
|
2018-07-17 15:17:44 +00:00
|
|
|
// UNARY is either unary minus or unary plus. The format is (operator, operand).
|
2020-01-21 11:13:11 +00:00
|
|
|
public const UNARY = 'UNARY';
|
2016-12-17 17:52:36 +00:00
|
|
|
|
2018-04-16 15:37:10 +00:00
|
|
|
// ARRAY_INDEX is an operation of accessing an array by an offset. The format
|
|
|
|
// is (array, offset).
|
2020-01-21 11:13:11 +00:00
|
|
|
public const ARRAY_INDEX = 'ARRAY_INDEX';
|
2016-12-17 17:52:36 +00:00
|
|
|
|
|
|
|
// Since parenthesis only manipulate precedence of the operators, they are
|
|
|
|
// not explicitly represented in the tree.
|
|
|
|
|
|
|
|
// FUNCTION_CALL is an invocation of built-in function. The format is a
|
|
|
|
// tuple where the first element is a function name, and all subsequent
|
|
|
|
// elements are the arguments.
|
2020-01-21 11:13:11 +00:00
|
|
|
public const FUNCTION_CALL = 'FUNCTION_CALL';
|
2016-12-17 17:52:36 +00:00
|
|
|
|
2018-04-16 15:37:10 +00:00
|
|
|
// ARRAY_DEFINITION is an array literal. The $children field contains tree
|
|
|
|
// nodes for the values of each of the array element used.
|
2020-01-21 11:13:11 +00:00
|
|
|
public const ARRAY_DEFINITION = 'ARRAY_DEFINITION';
|
2016-12-17 17:52:36 +00:00
|
|
|
|
|
|
|
// ATOM is a node representing a literal. The only element of $children is a
|
|
|
|
// token corresponding to the literal.
|
2020-01-21 11:13:11 +00:00
|
|
|
public const ATOM = 'ATOM';
|
2016-12-17 17:52:36 +00:00
|
|
|
|
|
|
|
/** @var string Type of the node, one of the constants above */
|
|
|
|
public $type;
|
|
|
|
/**
|
|
|
|
* Parameters of the value. Typically it is an array of children nodes,
|
|
|
|
* which might be either strings (for parametrization of the node) or another
|
|
|
|
* node. In case of ATOM it's a parser token.
|
|
|
|
* @var AFPTreeNode[]|string[]|AFPToken
|
|
|
|
*/
|
|
|
|
public $children;
|
|
|
|
|
2019-09-09 17:20:47 +00:00
|
|
|
/** @var int Position used for error reporting. */
|
2016-12-17 17:52:36 +00:00
|
|
|
public $position;
|
|
|
|
|
2019-09-09 17:20:47 +00:00
|
|
|
/**
|
|
|
|
* @var string[] Names of the variables assigned in this node or any of its descendants
|
|
|
|
* @todo We could change this to be an instance of a new AFPScope class (holding a var map)
|
|
|
|
* if we'll have the need to store other scope-specific data,
|
|
|
|
* see <https://phabricator.wikimedia.org/T230982#5475400>.
|
|
|
|
*/
|
|
|
|
private $innerAssignments = [];
|
|
|
|
|
2018-04-04 21:14:25 +00:00
|
|
|
/**
|
|
|
|
* @param string $type
|
2019-12-07 17:20:10 +00:00
|
|
|
* @param (AFPTreeNode|null)[]|string[]|AFPToken $children
|
2018-04-04 21:14:25 +00:00
|
|
|
* @param int $position
|
|
|
|
*/
|
2016-12-17 17:52:36 +00:00
|
|
|
public function __construct( $type, $children, $position ) {
|
|
|
|
$this->type = $type;
|
|
|
|
$this->children = $children;
|
|
|
|
$this->position = $position;
|
2019-09-09 17:20:47 +00:00
|
|
|
$this->populateInnerAssignments();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save in this node all the variable names used in the children, and in this node if it's an
|
|
|
|
* assignment-related node. Note that this doesn't check whether the variable is custom or builtin:
|
|
|
|
* this is already checked when calling setUserVariable.
|
|
|
|
* In case we'll ever need to store other data in a node, or maybe even a Scope object, this could
|
|
|
|
* be moved to a different class which could also re-visit the whole AST.
|
|
|
|
*/
|
|
|
|
private function populateInnerAssignments() {
|
|
|
|
if ( $this->type === self::ATOM ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
$this->type === self::ASSIGNMENT ||
|
|
|
|
$this->type === self::INDEX_ASSIGNMENT ||
|
|
|
|
$this->type === self::ARRAY_APPEND
|
|
|
|
) {
|
|
|
|
$this->innerAssignments = [ $this->children[0] ];
|
|
|
|
} elseif (
|
|
|
|
$this->type === self::FUNCTION_CALL &&
|
|
|
|
in_array( $this->children[0], [ 'set', 'set_var' ] ) &&
|
|
|
|
// If unset, parsing will fail when checking arguments
|
|
|
|
isset( $this->children[1] )
|
|
|
|
) {
|
|
|
|
$varnameNode = $this->children[1];
|
|
|
|
if ( $varnameNode->type !== self::ATOM ) {
|
|
|
|
// Shouldn't happen since variable variables are not allowed
|
2020-01-28 20:09:01 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
2021-02-10 14:34:16 +00:00
|
|
|
throw new AFPInternalException( "Got non-atom type {$varnameNode->type} for set_var" );
|
2020-01-28 20:09:01 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|
2019-09-09 17:20:47 +00:00
|
|
|
}
|
|
|
|
$this->innerAssignments = [ $varnameNode->children->value ];
|
|
|
|
}
|
|
|
|
|
|
|
|
// @phan-suppress-next-line PhanTypeSuspiciousNonTraversableForeach ATOM excluded above
|
|
|
|
foreach ( $this->children as $child ) {
|
|
|
|
if ( $child instanceof self ) {
|
|
|
|
$this->innerAssignments = array_merge( $this->innerAssignments, $child->getInnerAssignments() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function getInnerAssignments() : array {
|
|
|
|
return $this->innerAssignments;
|
2016-12-17 17:52:36 +00:00
|
|
|
}
|
|
|
|
|
2018-04-04 21:14:25 +00:00
|
|
|
/**
|
|
|
|
* @return string
|
2018-08-22 14:33:35 +00:00
|
|
|
* @codeCoverageIgnore
|
2018-04-04 21:14:25 +00:00
|
|
|
*/
|
2016-12-17 17:52:36 +00:00
|
|
|
public function toDebugString() {
|
|
|
|
return implode( "\n", $this->toDebugStringInner() );
|
|
|
|
}
|
|
|
|
|
2018-08-22 14:33:35 +00:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
* @codeCoverageIgnore
|
|
|
|
*/
|
2016-12-17 17:52:36 +00:00
|
|
|
private function toDebugStringInner() {
|
2018-08-26 08:34:42 +00:00
|
|
|
if ( $this->type === self::ATOM ) {
|
2016-12-17 17:52:36 +00:00
|
|
|
return [ "ATOM({$this->children->type} {$this->children->value})" ];
|
|
|
|
}
|
|
|
|
|
|
|
|
$align = function ( $line ) {
|
|
|
|
return ' ' . $line;
|
|
|
|
};
|
|
|
|
|
|
|
|
$lines = [ "{$this->type}" ];
|
2019-03-02 09:26:14 +00:00
|
|
|
// @phan-suppress-next-line PhanTypeSuspiciousNonTraversableForeach children is array here
|
2016-12-17 17:52:36 +00:00
|
|
|
foreach ( $this->children as $subnode ) {
|
|
|
|
if ( $subnode instanceof AFPTreeNode ) {
|
|
|
|
$sublines = array_map( $align, $subnode->toDebugStringInner() );
|
|
|
|
} elseif ( is_string( $subnode ) ) {
|
|
|
|
$sublines = [ " {$subnode}" ];
|
|
|
|
} else {
|
2021-02-10 14:34:16 +00:00
|
|
|
throw new AFPInternalException( "Each node parameter has to be either a node or a string" );
|
2016-12-17 17:52:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$lines = array_merge( $lines, $sublines );
|
|
|
|
}
|
|
|
|
return $lines;
|
|
|
|
}
|
|
|
|
}
|