1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
|
<?php
namespace MediaWiki\Extension\AbuseFilter\Variables;
use LogicException;
use MediaWiki\Extension\AbuseFilter\KeywordsManager;
use MediaWiki\Extension\AbuseFilter\Parser\AFPData;
/**
* Service that allows manipulating a VariableHolder
*/
class VariablesManager {
public const SERVICE_NAME = 'AbuseFilterVariablesManager';
/**
* Used in self::getVar() to determine what to do if the requested variable is missing. See
* the docs of that method for an explanation.
*/
public const GET_LAX = 0;
public const GET_STRICT = 1;
public const GET_BC = 2;
/** @var KeywordsManager */
private $keywordsManager;
/** @var LazyVariableComputer */
private $lazyComputer;
/**
* @param KeywordsManager $keywordsManager
* @param LazyVariableComputer $lazyComputer
*/
public function __construct(
KeywordsManager $keywordsManager,
LazyVariableComputer $lazyComputer
) {
$this->keywordsManager = $keywordsManager;
$this->lazyComputer = $lazyComputer;
}
/**
* Checks whether any deprecated variable is stored with the old name, and replaces it with
* the new name. This should normally only happen when a DB dump is retrieved from the DB.
*
* @param VariableHolder $holder
*/
public function translateDeprecatedVars( VariableHolder $holder ): void {
$deprecatedVars = $this->keywordsManager->getDeprecatedVariables();
foreach ( $holder->getVars() as $name => $value ) {
if ( array_key_exists( $name, $deprecatedVars ) ) {
$holder->setVar( $deprecatedVars[$name], $value );
$holder->removeVar( $name );
}
}
}
/**
* Get a variable from the current object
*
* @param VariableHolder $holder
* @param string $varName The variable name
* @param int $mode One of the self::GET_* constants, determines how to behave when the variable is unset:
* - GET_STRICT -> Throw UnsetVariableException
* - GET_LAX -> Return a DUNDEFINED AFPData
* - GET_BC -> Return a DNULL AFPData (this should only be used for BC, see T230256)
* @return AFPData
*/
public function getVar(
VariableHolder $holder,
string $varName,
$mode = self::GET_STRICT
): AFPData {
$varName = strtolower( $varName );
if ( $holder->varIsSet( $varName ) ) {
/** @var LazyLoadedVariable|AFPData $variable */
$variable = $holder->getVarThrow( $varName );
if ( $variable instanceof LazyLoadedVariable ) {
$getVarCB = function ( string $varName ) use ( $holder ): AFPData {
return $this->getVar( $holder, $varName );
};
$value = $this->lazyComputer->compute( $variable, $holder, $getVarCB );
$holder->setVar( $varName, $value );
return $value;
} elseif ( $variable instanceof AFPData ) {
return $variable;
} else {
// @codeCoverageIgnoreStart
throw new \UnexpectedValueException(
"Variable $varName has unexpected type " . get_debug_type( $variable )
);
// @codeCoverageIgnoreEnd
}
}
// The variable is not set.
switch ( $mode ) {
case self::GET_STRICT:
throw new UnsetVariableException( $varName );
case self::GET_LAX:
return new AFPData( AFPData::DUNDEFINED );
case self::GET_BC:
// Old behaviour, which can sometimes lead to unexpected results (e.g.
// `edit_delta < -5000` will match any non-edit action).
return new AFPData( AFPData::DNULL );
default:
// @codeCoverageIgnoreStart
throw new LogicException( "Mode '$mode' not recognized." );
// @codeCoverageIgnoreEnd
}
}
/**
* Dump all variables stored in the holder 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.
*
* @param VariableHolder $holder
* @param array|bool $compute Variables we should compute if not yet set
* @param bool $includeUserVars Include user set variables
* @return array
*/
public function dumpAllVars(
VariableHolder $holder,
$compute = [],
bool $includeUserVars = false
): array {
$coreVariables = [];
if ( !$includeUserVars ) {
// Compile a list of all variables set by the extension to be able
// to filter user set ones by name
$activeVariables = array_keys( $this->keywordsManager->getVarsMappings() );
$deprecatedVariables = array_keys( $this->keywordsManager->getDeprecatedVariables() );
$disabledVariables = array_keys( $this->keywordsManager->getDisabledVariables() );
$coreVariables = array_merge( $activeVariables, $deprecatedVariables, $disabledVariables );
$coreVariables = array_map( 'strtolower', $coreVariables );
}
$exported = [];
foreach ( array_keys( $holder->getVars() ) as $varName ) {
$computeThis = ( is_array( $compute ) && in_array( $varName, $compute ) ) || $compute === true;
if (
( $includeUserVars || in_array( strtolower( $varName ), $coreVariables ) ) &&
// Only include variables set in the extension in case $includeUserVars is false
( $computeThis || $holder->getVarThrow( $varName ) instanceof AFPData )
) {
$exported[$varName] = $this->getVar( $holder, $varName )->toNative();
}
}
return $exported;
}
/**
* Compute all vars which need DB access. Useful for vars which are going to be saved
* cross-wiki or used for offline analysis.
*
* @param VariableHolder $holder
*/
public function computeDBVars( VariableHolder $holder ): void {
static $dbTypes = [
'links-from-database',
'links-from-update',
'links-from-wikitext-or-database',
'load-recent-authors',
'page-age',
'revision-age-by-id',
'revision-age-by-title',
'previous-revision-age',
'get-page-restrictions',
'user-editcount',
'user-emailconfirm',
'user-groups',
'user-rights',
'user-age',
'user-block',
'revision-text-by-id',
'content-model-by-id',
];
/** @var LazyLoadedVariable[] $missingVars */
$missingVars = array_filter( $holder->getVars(), static function ( $el ) {
return ( $el instanceof LazyLoadedVariable );
} );
foreach ( $missingVars as $name => $var ) {
if ( in_array( $var->getMethod(), $dbTypes ) ) {
$holder->setVar( $name, $this->getVar( $holder, $name ) );
}
}
}
/**
* Export all variables stored in this object with their native (PHP) types.
*
* @param VariableHolder $holder
* @return array
*/
public function exportAllVars( VariableHolder $holder ): array {
$exported = [];
foreach ( array_keys( $holder->getVars() ) as $varName ) {
$exported[ $varName ] = $this->getVar( $holder, $varName )->toNative();
}
return $exported;
}
/**
* Export all non-lazy variables stored in this object as string
*
* @param VariableHolder $holder
* @return string[]
*/
public function exportNonLazyVars( VariableHolder $holder ): array {
$exported = [];
foreach ( $holder->getVars() as $varName => $data ) {
if ( !( $data instanceof LazyLoadedVariable ) ) {
$exported[$varName] = $holder->getComputedVariable( $varName )->toString();
}
}
return $exported;
}
}
|