2016-12-17 17:52:36 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class AbuseFilterVariableHolder {
|
2018-04-29 17:52:45 +00:00
|
|
|
/** @var (AFPData|AFComputedVariable)[] */
|
2017-06-15 14:23:34 +00:00
|
|
|
public $mVars = [];
|
2016-12-17 17:52:36 +00:00
|
|
|
|
2018-04-08 11:03:52 +00:00
|
|
|
/** @var string[] Variables used to store meta-data, we'd better be safe. See T191715 */
|
2019-01-05 17:30:37 +00:00
|
|
|
public static $varBlacklist = [ 'global_log_ids', 'local_log_ids' ];
|
|
|
|
|
|
|
|
/** @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;
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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 ) {
|
2016-12-17 17:52:36 +00:00
|
|
|
$placeholder = new AFComputedVariable( $method, $parameters );
|
|
|
|
$this->setVar( $variable, $placeholder );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a variable from the current object
|
|
|
|
*
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param string $variable
|
2016-12-17 17:52:36 +00:00
|
|
|
* @return AFPData
|
|
|
|
*/
|
2018-04-04 21:14:25 +00:00
|
|
|
public function getVar( $variable ) {
|
2016-12-17 17:52:36 +00:00
|
|
|
$variable = strtolower( $variable );
|
2018-09-13 16:42:47 +00:00
|
|
|
if ( $this->mVarsVersion === 1 && in_array( $variable, AbuseFilter::getDeprecatedVariables() ) ) {
|
|
|
|
// Variables are stored with old names, but the parser has given us
|
|
|
|
// a new name. Translate it back.
|
|
|
|
$variable = array_search( $variable, AbuseFilter::getDeprecatedVariables() );
|
|
|
|
}
|
2016-12-17 17:52:36 +00:00
|
|
|
if ( isset( $this->mVars[$variable] ) ) {
|
|
|
|
if ( $this->mVars[$variable] instanceof AFComputedVariable ) {
|
2018-04-29 17:52:45 +00:00
|
|
|
/** @suppress PhanUndeclaredMethod False positive */
|
2016-12-17 17:52:36 +00:00
|
|
|
$value = $this->mVars[$variable]->compute( $this );
|
|
|
|
$this->setVar( $variable, $value );
|
|
|
|
return $value;
|
|
|
|
} elseif ( $this->mVars[$variable] instanceof AFPData ) {
|
|
|
|
return $this->mVars[$variable];
|
|
|
|
}
|
2018-12-29 13:14:27 +00:00
|
|
|
} elseif ( array_key_exists( $variable, AbuseFilter::$disabledVars ) ) {
|
|
|
|
wfWarn( "Disabled variable $variable requested. Please fix the filter as this will " .
|
|
|
|
"be removed soon." );
|
2016-12-17 17:52:36 +00:00
|
|
|
}
|
|
|
|
return new AFPData();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Export all variables stored in this object as string
|
|
|
|
*
|
|
|
|
* @return string[]
|
|
|
|
*/
|
2018-04-04 21:14:25 +00:00
|
|
|
public function exportAllVars() {
|
2016-12-17 17:52:36 +00:00
|
|
|
$exported = [];
|
|
|
|
foreach ( array_keys( $this->mVars ) as $varName ) {
|
|
|
|
if ( !in_array( $varName, self::$varBlacklist ) ) {
|
|
|
|
$exported[$varName] = $this->getVar( $varName )->toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 ) {
|
|
|
|
if (
|
|
|
|
!( $data instanceof AFComputedVariable )
|
|
|
|
&& !in_array( $varName, self::$varBlacklist )
|
|
|
|
) {
|
|
|
|
$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-29 17:32:12 +00:00
|
|
|
$coreVariables = array_merge( $activeVariables, $deprecatedVariables );
|
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 ) {
|
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
|
|
|
|
!in_array( $varName, self::$varBlacklist ) &&
|
|
|
|
( $compute === true ||
|
|
|
|
( is_array( $compute ) && in_array( $varName, $compute ) ) ||
|
|
|
|
$this->mVars[$varName] instanceof AFPData
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
$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
|
|
|
|
|
|
|
foreach ( $this->mVars as $name => $value ) {
|
|
|
|
if ( $value instanceof AFComputedVariable &&
|
|
|
|
in_array( $value->mMethod, $dbTypes )
|
|
|
|
) {
|
|
|
|
$value = $value->compute( $this );
|
|
|
|
$this->setVar( $name, $value );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|