2016-12-17 17:52:36 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class AbuseFilterVariableHolder {
|
2019-08-29 16:50:58 +00:00
|
|
|
/**
|
|
|
|
* @var (AFPData|AFComputedVariable)[]
|
|
|
|
* @fixme This should be private, but it isn't because of T231542: there are serialized instances
|
|
|
|
* stored in the DB, and mVars wouldn't be available in HHVM after deserializing them (T213006)
|
|
|
|
*/
|
|
|
|
public $mVars = [];
|
2016-12-17 17:52:36 +00:00
|
|
|
|
2019-01-05 17:30:37 +00:00
|
|
|
/** @var bool Whether this object is being used for an ongoing action being filtered */
|
|
|
|
public $forFilter = false;
|
2016-12-17 17:52:36 +00:00
|
|
|
|
2018-09-13 16:42:47 +00:00
|
|
|
/** @var int 2 is the default and means that new variables names (from T173889) should be used.
|
|
|
|
* 1 means that the old ones should be used, e.g. if this object is constructed from an
|
|
|
|
* afl_var_dump which still bears old variables.
|
|
|
|
*/
|
|
|
|
public $mVarsVersion = 2;
|
|
|
|
|
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
|
|
|
|
* @return AbuseFilterVariableHolder
|
|
|
|
*/
|
|
|
|
public static function newFromArray( array $vars ) : AbuseFilterVariableHolder {
|
|
|
|
$ret = new self;
|
|
|
|
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
|
|
|
*/
|
2018-04-04 21:14:25 +00:00
|
|
|
public function setVar( $variable, $datum ) {
|
2016-12-17 17:52:36 +00:00
|
|
|
$variable = strtolower( $variable );
|
|
|
|
if ( !( $datum instanceof AFPData || $datum instanceof AFComputedVariable ) ) {
|
|
|
|
$datum = AFPData::newFromPHPVar( $datum );
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->mVars[$variable] = $datum;
|
|
|
|
}
|
|
|
|
|
2018-12-27 17:06:56 +00:00
|
|
|
/**
|
|
|
|
* Get all variables stored in this object
|
|
|
|
*
|
|
|
|
* @return (AFPData|AFComputedVariable)[]
|
|
|
|
*/
|
|
|
|
public function getVars() {
|
|
|
|
return $this->mVars;
|
|
|
|
}
|
|
|
|
|
2019-08-26 13:01:09 +00:00
|
|
|
/**
|
|
|
|
* Get a lazy loader for a variable. This method is here for testing ease
|
|
|
|
* @param string $method
|
|
|
|
* @param array $parameters
|
|
|
|
* @return AFComputedVariable
|
|
|
|
*/
|
|
|
|
public function getLazyLoader( $method, $parameters ) {
|
|
|
|
return new AFComputedVariable( $method, $parameters );
|
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|
2018-04-04 21:14:25 +00:00
|
|
|
public function setLazyLoadVar( $variable, $method, $parameters ) {
|
2019-08-26 13:01:09 +00:00
|
|
|
$placeholder = $this->getLazyLoader( $method, $parameters );
|
2016-12-17 17:52:36 +00:00
|
|
|
$this->setVar( $variable, $placeholder );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a variable from the current object
|
|
|
|
*
|
2019-01-27 12:52:41 +00:00
|
|
|
* @param string $varName The variable name
|
2016-12-17 17:52:36 +00:00
|
|
|
* @return AFPData
|
|
|
|
*/
|
2019-01-27 12:52:41 +00:00
|
|
|
public function getVar( $varName ) {
|
|
|
|
$varName = strtolower( $varName );
|
|
|
|
if ( $this->mVarsVersion === 1 && in_array( $varName, AbuseFilter::getDeprecatedVariables() ) ) {
|
2018-09-13 16:42:47 +00:00
|
|
|
// Variables are stored with old names, but the parser has given us
|
|
|
|
// a new name. Translate it back.
|
2019-01-27 12:52:41 +00:00
|
|
|
$varName = array_search( $varName, AbuseFilter::getDeprecatedVariables() );
|
2018-09-13 16:42:47 +00:00
|
|
|
}
|
2019-01-27 12:52:41 +00:00
|
|
|
if ( isset( $this->mVars[$varName] ) ) {
|
|
|
|
/** @var $variable AFComputedVariable|AFPData */
|
|
|
|
$variable = $this->mVars[$varName];
|
|
|
|
if ( $variable instanceof AFComputedVariable ) {
|
|
|
|
$value = $variable->compute( $this );
|
|
|
|
$this->setVar( $varName, $value );
|
2016-12-17 17:52:36 +00:00
|
|
|
return $value;
|
2019-01-27 12:52:41 +00:00
|
|
|
} elseif ( $variable instanceof AFPData ) {
|
|
|
|
return $variable;
|
2016-12-17 17:52:36 +00:00
|
|
|
}
|
2019-01-27 12:52:41 +00:00
|
|
|
} elseif ( array_key_exists( $varName, AbuseFilter::$disabledVars ) ) {
|
|
|
|
wfWarn( "Disabled variable $varName requested. Please fix the filter as this will " .
|
2018-12-29 13:14:27 +00:00
|
|
|
"be removed soon." );
|
2016-12-17 17:52:36 +00:00
|
|
|
}
|
2019-08-03 15:52:14 +00:00
|
|
|
return new AFPData( AFPData::DUNDEFINED );
|
2016-12-17 17:52:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return AbuseFilterVariableHolder
|
|
|
|
*/
|
|
|
|
public static function merge() {
|
|
|
|
$newHolder = new AbuseFilterVariableHolder;
|
2018-06-08 03:16:42 +00:00
|
|
|
$newHolder->addHolders( ...func_get_args() );
|
2016-12-17 17:52:36 +00:00
|
|
|
|
|
|
|
return $newHolder;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Merge any number of holders given as arguments into this holder.
|
|
|
|
*
|
|
|
|
* @throws MWException
|
|
|
|
*/
|
|
|
|
public function addHolders() {
|
|
|
|
$holders = func_get_args();
|
|
|
|
|
|
|
|
foreach ( $holders as $addHolder ) {
|
|
|
|
if ( !is_object( $addHolder ) ) {
|
|
|
|
throw new MWException( 'Invalid argument to AbuseFilterVariableHolder::addHolders' );
|
|
|
|
}
|
|
|
|
$this->mVars = array_merge( $this->mVars, $addHolder->mVars );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-08-26 13:01:09 +00:00
|
|
|
* Export all variables stored in this object.
|
2016-12-17 17:52:36 +00:00
|
|
|
*
|
2019-08-26 13:01:09 +00:00
|
|
|
* @param bool $nativeTypes If true, variables will be exported with native types, otherwise
|
|
|
|
* they'll be cast to strings.
|
|
|
|
* @return array
|
2016-12-17 17:52:36 +00:00
|
|
|
*/
|
2019-08-26 13:01:09 +00:00
|
|
|
public function exportAllVars( $nativeTypes = false ) {
|
2016-12-17 17:52:36 +00:00
|
|
|
$exported = [];
|
|
|
|
foreach ( array_keys( $this->mVars ) as $varName ) {
|
2019-08-26 13:01:09 +00:00
|
|
|
if ( $nativeTypes ) {
|
|
|
|
$exported[ $varName ] = $this->getVar( $varName )->toNative();
|
|
|
|
} else {
|
|
|
|
$exported[ $varName ] = $this->getVar( $varName )->toString();
|
|
|
|
}
|
2016-12-17 17:52:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $exported;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Export all non-lazy variables stored in this object as string
|
|
|
|
*
|
|
|
|
* @return string[]
|
|
|
|
*/
|
2018-04-04 21:14:25 +00:00
|
|
|
public function exportNonLazyVars() {
|
2016-12-17 17:52:36 +00:00
|
|
|
$exported = [];
|
|
|
|
foreach ( $this->mVars as $varName => $data ) {
|
2019-01-05 18:28:03 +00:00
|
|
|
if ( !( $data instanceof AFComputedVariable ) ) {
|
2016-12-17 17:52:36 +00:00
|
|
|
$exported[$varName] = $this->getVar( $varName )->toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $exported;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dump all variables stored in this object in their native types.
|
|
|
|
* If you want a not yet set variable to be included in the results you can
|
|
|
|
* either set $compute to an array with the name of the variable or set
|
|
|
|
* $compute to true to compute all not yet set variables.
|
|
|
|
*
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param array|bool $compute Variables we should copute if not yet set
|
|
|
|
* @param bool $includeUserVars Include user set variables
|
2016-12-17 17:52:36 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
2017-06-15 14:23:34 +00:00
|
|
|
public function dumpAllVars( $compute = [], $includeUserVars = false ) {
|
|
|
|
$coreVariables = [];
|
2016-12-17 17:52:36 +00:00
|
|
|
|
|
|
|
if ( !$includeUserVars ) {
|
|
|
|
// Compile a list of all variables set by the extension to be able
|
|
|
|
// to filter user set ones by name
|
|
|
|
global $wgRestrictionTypes;
|
|
|
|
|
2018-12-29 17:32:12 +00:00
|
|
|
$activeVariables = array_keys( AbuseFilter::getBuilderValues()['vars'] );
|
2018-02-18 13:44:17 +00:00
|
|
|
$deprecatedVariables = array_keys( AbuseFilter::getDeprecatedVariables() );
|
2018-12-28 16:01:19 +00:00
|
|
|
$disabledVariables = array_keys( AbuseFilter::$disabledVars );
|
|
|
|
$coreVariables = array_merge( $activeVariables, $deprecatedVariables, $disabledVariables );
|
2016-12-17 17:52:36 +00:00
|
|
|
|
2018-12-29 17:32:12 +00:00
|
|
|
// @todo _restrictions variables should be handled in builderValues as well.
|
|
|
|
$prefixes = [ 'moved_from', 'moved_to', 'page' ];
|
|
|
|
foreach ( $wgRestrictionTypes as $action ) {
|
2016-12-17 17:52:36 +00:00
|
|
|
foreach ( $prefixes as $prefix ) {
|
2018-12-29 17:32:12 +00:00
|
|
|
$coreVariables[] = "{$prefix}_restrictions_$action";
|
2016-12-17 17:52:36 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-29 17:32:12 +00:00
|
|
|
|
2016-12-17 17:52:36 +00:00
|
|
|
$coreVariables = array_map( 'strtolower', $coreVariables );
|
|
|
|
}
|
|
|
|
|
2018-12-29 17:32:12 +00:00
|
|
|
$exported = [];
|
|
|
|
foreach ( array_keys( $this->mVars ) as $varName ) {
|
2019-01-30 19:30:59 +00:00
|
|
|
$computeThis = ( is_array( $compute ) && in_array( $varName, $compute ) ) || $compute === true;
|
2016-12-17 17:52:36 +00:00
|
|
|
if (
|
|
|
|
( $includeUserVars || in_array( strtolower( $varName ), $coreVariables ) ) &&
|
|
|
|
// Only include variables set in the extension in case $includeUserVars is false
|
2019-01-30 19:30:59 +00:00
|
|
|
( $computeThis || $this->mVars[$varName] instanceof AFPData )
|
2016-12-17 17:52:36 +00:00
|
|
|
) {
|
|
|
|
$exported[$varName] = $this->getVar( $varName )->toNative();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $exported;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param string $var
|
2016-12-17 17:52:36 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2018-04-04 21:14:25 +00:00
|
|
|
public function varIsSet( $var ) {
|
2016-12-17 17:52:36 +00:00
|
|
|
return array_key_exists( $var, $this->mVars );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compute all vars which need DB access. Useful for vars which are going to be saved
|
|
|
|
* cross-wiki or used for offline analysis.
|
|
|
|
*/
|
2018-04-04 21:14:25 +00:00
|
|
|
public function computeDBVars() {
|
2017-06-15 14:23:34 +00:00
|
|
|
static $dbTypes = [
|
2016-12-17 17:52:36 +00:00
|
|
|
'links-from-wikitext-or-database',
|
|
|
|
'load-recent-authors',
|
2015-04-01 00:41:09 +00:00
|
|
|
'page-age',
|
2016-12-17 17:52:36 +00:00
|
|
|
'get-page-restrictions',
|
|
|
|
'simple-user-accessor',
|
|
|
|
'user-age',
|
|
|
|
'user-groups',
|
|
|
|
'user-rights',
|
|
|
|
'revision-text-by-id',
|
|
|
|
'revision-text-by-timestamp'
|
2017-06-15 14:23:34 +00:00
|
|
|
];
|
2016-12-17 17:52:36 +00:00
|
|
|
|
2019-01-27 12:52:41 +00:00
|
|
|
/** @var AFComputedVariable[] $missingVars */
|
|
|
|
$missingVars = array_filter( $this->mVars, function ( $el ) {
|
|
|
|
return ( $el instanceof AFComputedVariable );
|
|
|
|
} );
|
|
|
|
foreach ( $missingVars as $name => $value ) {
|
|
|
|
if ( in_array( $value->mMethod, $dbTypes ) ) {
|
2016-12-17 17:52:36 +00:00
|
|
|
$value = $value->compute( $this );
|
|
|
|
$this->setVar( $name, $value );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|