2016-12-17 17:52:36 +00:00
|
|
|
<?php
|
|
|
|
|
2021-01-02 13:41:31 +00:00
|
|
|
namespace MediaWiki\Extension\AbuseFilter\Variables;
|
|
|
|
|
2020-12-03 22:22:43 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\Parser\AFPData;
|
2019-08-11 13:11:20 +00:00
|
|
|
|
2020-10-18 22:25:05 +00:00
|
|
|
/**
|
|
|
|
* Mutable value object that holds a list of variables
|
|
|
|
*/
|
2021-01-02 14:01:00 +00:00
|
|
|
class VariableHolder {
|
2019-08-29 16:50:58 +00:00
|
|
|
/**
|
2021-01-02 14:01:00 +00:00
|
|
|
* @var (AFPData|LazyLoadedVariable)[]
|
2019-08-29 16:50:58 +00:00
|
|
|
*/
|
2019-01-06 17:49:22 +00:00
|
|
|
private $mVars = [];
|
2016-12-17 17:52:36 +00:00
|
|
|
|
2019-02-24 14:55:19 +00:00
|
|
|
/**
|
|
|
|
* Utility function to translate an array with shape [ varname => value ] into a self instance
|
|
|
|
*
|
|
|
|
* @param array $vars
|
2021-01-02 14:01:00 +00:00
|
|
|
* @return VariableHolder
|
2019-02-24 14:55:19 +00:00
|
|
|
*/
|
2021-07-21 18:51:12 +00:00
|
|
|
public static function newFromArray( array $vars ): VariableHolder {
|
2020-10-18 22:25:05 +00:00
|
|
|
$ret = new self();
|
2019-02-24 14:55:19 +00:00
|
|
|
foreach ( $vars as $var => $value ) {
|
|
|
|
$ret->setVar( $var, $value );
|
|
|
|
}
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
2016-12-17 17:52:36 +00:00
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param string $variable
|
|
|
|
* @param mixed $datum
|
2016-12-17 17:52:36 +00:00
|
|
|
*/
|
2021-07-21 18:51:12 +00:00
|
|
|
public function setVar( string $variable, $datum ): void {
|
2016-12-17 17:52:36 +00:00
|
|
|
$variable = strtolower( $variable );
|
2021-01-02 14:01:00 +00:00
|
|
|
if ( !( $datum instanceof AFPData || $datum instanceof LazyLoadedVariable ) ) {
|
2016-12-17 17:52:36 +00:00
|
|
|
$datum = AFPData::newFromPHPVar( $datum );
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->mVars[$variable] = $datum;
|
|
|
|
}
|
|
|
|
|
2018-12-27 17:06:56 +00:00
|
|
|
/**
|
|
|
|
* Get all variables stored in this object
|
|
|
|
*
|
2021-01-02 14:01:00 +00:00
|
|
|
* @return (AFPData|LazyLoadedVariable)[]
|
2018-12-27 17:06:56 +00:00
|
|
|
*/
|
2021-07-21 18:51:12 +00:00
|
|
|
public function getVars(): array {
|
2018-12-27 17:06:56 +00:00
|
|
|
return $this->mVars;
|
|
|
|
}
|
|
|
|
|
2016-12-17 17:52:36 +00:00
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param string $variable
|
|
|
|
* @param string $method
|
|
|
|
* @param array $parameters
|
2016-12-17 17:52:36 +00:00
|
|
|
*/
|
2021-07-21 18:51:12 +00:00
|
|
|
public function setLazyLoadVar( string $variable, string $method, array $parameters ): void {
|
2021-01-02 14:01:00 +00:00
|
|
|
$placeholder = new LazyLoadedVariable( $method, $parameters );
|
2016-12-17 17:52:36 +00:00
|
|
|
$this->setVar( $variable, $placeholder );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-10-18 22:25:05 +00:00
|
|
|
* Get a variable from the current object, or throw if not set
|
2016-12-17 17:52:36 +00:00
|
|
|
*
|
2020-10-18 22:25:05 +00:00
|
|
|
* @param string $varName The variable name
|
2021-01-02 14:01:00 +00:00
|
|
|
* @return AFPData|LazyLoadedVariable
|
2016-12-17 17:52:36 +00:00
|
|
|
*/
|
2020-10-18 22:25:05 +00:00
|
|
|
public function getVarThrow( string $varName ) {
|
|
|
|
$varName = strtolower( $varName );
|
|
|
|
if ( !$this->varIsSet( $varName ) ) {
|
|
|
|
throw new UnsetVariableException( $varName );
|
2016-12-17 17:52:36 +00:00
|
|
|
}
|
2020-10-18 22:25:05 +00:00
|
|
|
return $this->mVars[$varName];
|
2016-12-17 17:52:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-10-18 22:25:05 +00:00
|
|
|
* A stronger version of self::getVarThrow that also asserts that the variable was computed
|
|
|
|
* @param string $varName
|
|
|
|
* @return AFPData
|
|
|
|
* @codeCoverageIgnore
|
2016-12-17 17:52:36 +00:00
|
|
|
*/
|
2021-07-21 18:51:12 +00:00
|
|
|
public function getComputedVariable( string $varName ): AFPData {
|
2020-10-18 22:25:05 +00:00
|
|
|
return $this->getVarThrow( $varName );
|
2016-12-17 17:52:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-10-18 22:25:05 +00:00
|
|
|
* Merge any number of holders given as arguments into this holder.
|
2016-12-17 17:52:36 +00:00
|
|
|
*
|
2021-01-02 14:01:00 +00:00
|
|
|
* @param VariableHolder ...$holders
|
2016-12-17 17:52:36 +00:00
|
|
|
*/
|
2021-07-21 18:51:12 +00:00
|
|
|
public function addHolders( VariableHolder ...$holders ): void {
|
2020-10-18 22:25:05 +00:00
|
|
|
foreach ( $holders as $addHolder ) {
|
|
|
|
$this->mVars = array_merge( $this->mVars, $addHolder->mVars );
|
2016-12-17 17:52:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param string $var
|
2016-12-17 17:52:36 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2021-07-21 18:51:12 +00:00
|
|
|
public function varIsSet( string $var ): bool {
|
2016-12-17 17:52:36 +00:00
|
|
|
return array_key_exists( $var, $this->mVars );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-10-18 22:25:05 +00:00
|
|
|
* @param string $varName
|
2020-09-25 22:32:01 +00:00
|
|
|
*/
|
2021-07-21 18:51:12 +00:00
|
|
|
public function removeVar( string $varName ): void {
|
2020-10-18 22:25:05 +00:00
|
|
|
unset( $this->mVars[$varName] );
|
2020-09-25 22:32:01 +00:00
|
|
|
}
|
2016-12-17 17:52:36 +00:00
|
|
|
}
|