| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
namespace MediaWiki\Extension\AbuseFilter\Api; |
| 20 |
|
| 21 |
use InvalidArgumentException; |
| 22 |
use MediaWiki\Api\ApiBase; |
| 23 |
use MediaWiki\Api\ApiQuery; |
| 24 |
use MediaWiki\Api\ApiQueryBase; |
| 25 |
use MediaWiki\Extension\AbuseFilter\AbuseFilterPermissionManager; |
| 26 |
use MediaWiki\Extension\AbuseFilter\AbuseLoggerFactory; |
| 27 |
use MediaWiki\Extension\AbuseFilter\CentralDBNotAvailableException; |
| 28 |
use MediaWiki\Extension\AbuseFilter\Filter\FilterNotFoundException; |
| 29 |
use MediaWiki\Extension\AbuseFilter\Filter\Flags; |
| 30 |
use MediaWiki\Extension\AbuseFilter\FilterLookup; |
| 31 |
use MediaWiki\Extension\AbuseFilter\GlobalNameUtils; |
| 32 |
use MediaWiki\Extension\AbuseFilter\Special\SpecialAbuseLog; |
| 33 |
use MediaWiki\Extension\AbuseFilter\Variables\VariablesBlobStore; |
| 34 |
use MediaWiki\Extension\AbuseFilter\Variables\VariablesManager; |
| 35 |
use MediaWiki\Title\Title; |
| 36 |
use MediaWiki\User\UserFactory; |
| 37 |
use MediaWiki\Utils\MWTimestamp; |
| 38 |
use Wikimedia\IPUtils; |
| 39 |
use Wikimedia\ParamValidator\ParamValidator; |
| 40 |
use Wikimedia\ParamValidator\TypeDef\IntegerDef; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
class QueryAbuseLog extends ApiQueryBase { |
| 52 |
|
| 53 |
|
| 54 |
private $afFilterLookup; |
| 55 |
|
| 56 |
|
| 57 |
private $afPermManager; |
| 58 |
|
| 59 |
|
| 60 |
private $afVariablesBlobStore; |
| 61 |
|
| 62 |
|
| 63 |
private $afVariablesManager; |
| 64 |
|
| 65 |
|
| 66 |
private $userFactory; |
| 67 |
|
| 68 |
private AbuseLoggerFactory $abuseLoggerFactory; |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public function __construct( |
| 81 |
ApiQuery $query, |
| 82 |
$moduleName, |
| 83 |
FilterLookup $afFilterLookup, |
| 84 |
AbuseFilterPermissionManager $afPermManager, |
| 85 |
VariablesBlobStore $afVariablesBlobStore, |
| 86 |
VariablesManager $afVariablesManager, |
| 87 |
UserFactory $userFactory, |
| 88 |
AbuseLoggerFactory $abuseLoggerFactory |
| 89 |
) { |
| 90 |
parent::__construct( $query, $moduleName, 'afl' ); |
| 91 |
$this->afFilterLookup = $afFilterLookup; |
| 92 |
$this->afPermManager = $afPermManager; |
| 93 |
$this->afVariablesBlobStore = $afVariablesBlobStore; |
| 94 |
$this->afVariablesManager = $afVariablesManager; |
| 95 |
$this->userFactory = $userFactory; |
| 96 |
$this->abuseLoggerFactory = $abuseLoggerFactory; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
public function execute() { |
| 103 |
$lookup = $this->afFilterLookup; |
| 104 |
|
| 105 |
|
| 106 |
$this->checkUserRightsAny( 'abusefilter-log' ); |
| 107 |
|
| 108 |
$performer = $this->getAuthority(); |
| 109 |
$params = $this->extractRequestParams(); |
| 110 |
|
| 111 |
$prop = array_fill_keys( $params['prop'], true ); |
| 112 |
$fld_ids = isset( $prop['ids'] ); |
| 113 |
$fld_filter = isset( $prop['filter'] ); |
| 114 |
$fld_user = isset( $prop['user'] ); |
| 115 |
$fld_title = isset( $prop['title'] ); |
| 116 |
$fld_action = isset( $prop['action'] ); |
| 117 |
$fld_details = isset( $prop['details'] ); |
| 118 |
$fld_result = isset( $prop['result'] ); |
| 119 |
$fld_timestamp = isset( $prop['timestamp'] ); |
| 120 |
$fld_hidden = isset( $prop['hidden'] ); |
| 121 |
$fld_revid = isset( $prop['revid'] ); |
| 122 |
$isCentral = $this->getConfig()->get( 'AbuseFilterIsCentral' ); |
| 123 |
$fld_wiki = $isCentral && isset( $prop['wiki'] ); |
| 124 |
|
| 125 |
if ( $fld_details ) { |
| 126 |
$this->checkUserRightsAny( 'abusefilter-log-detail' ); |
| 127 |
} |
| 128 |
|
| 129 |
$canViewPrivate = $this->afPermManager->canViewPrivateFiltersLogs( $performer ); |
| 130 |
$canViewProtected = $this->afPermManager->canViewProtectedVariables( $performer ); |
| 131 |
$canViewProtectedValues = $this->afPermManager->canViewProtectedVariableValues( $performer ); |
| 132 |
|
| 133 |
|
| 134 |
$searchFilters = []; |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
if ( $params['filter'] ) { |
| 139 |
if ( !is_array( $params['filter'] ) ) { |
| 140 |
$params['filter'] = [ $params['filter'] ]; |
| 141 |
} |
| 142 |
$foundInvalid = false; |
| 143 |
foreach ( $params['filter'] as $filter ) { |
| 144 |
try { |
| 145 |
$searchFilters[] = GlobalNameUtils::splitGlobalName( $filter ); |
| 146 |
} catch ( InvalidArgumentException $e ) { |
| 147 |
$foundInvalid = true; |
| 148 |
continue; |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
if ( !$canViewPrivate || !$canViewProtected || !$canViewProtectedValues ) { |
| 153 |
foreach ( $searchFilters as [ $filterID, $global ] ) { |
| 154 |
try { |
| 155 |
$privacyLevel = $lookup->getFilter( $filterID, $global )->getPrivacyLevel(); |
| 156 |
} catch ( CentralDBNotAvailableException $_ ) { |
| 157 |
|
| 158 |
$privacyLevel = Flags::FILTER_HIDDEN | Flags::FILTER_USES_PROTECTED_VARS; |
| 159 |
} catch ( FilterNotFoundException $_ ) { |
| 160 |
$privacyLevel = Flags::FILTER_PUBLIC; |
| 161 |
$foundInvalid = true; |
| 162 |
} |
| 163 |
if ( !$canViewPrivate && ( Flags::FILTER_HIDDEN & $privacyLevel ) ) { |
| 164 |
$this->dieWithError( |
| 165 |
[ 'apierror-permissiondenied', $this->msg( 'action-abusefilter-log-private' ) ] |
| 166 |
); |
| 167 |
} |
| 168 |
if ( !$canViewProtected && ( Flags::FILTER_USES_PROTECTED_VARS & $privacyLevel ) ) { |
| 169 |
$this->dieWithError( |
| 170 |
[ 'apierror-permissiondenied', $this->msg( 'action-abusefilter-log-protected' ) ] |
| 171 |
); |
| 172 |
} |
| 173 |
if ( !$canViewProtectedValues && ( Flags::FILTER_USES_PROTECTED_VARS & $privacyLevel ) ) { |
| 174 |
$this->dieWithError( |
| 175 |
[ 'apierror-permissiondenied', $this->msg( 'action-abusefilter-log-protected-access' ) ] |
| 176 |
); |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
if ( $foundInvalid ) { |
| 182 |
|
| 183 |
$this->addWarning( 'abusefilter-log-invalid-filter' ); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
$result = $this->getResult(); |
| 188 |
|
| 189 |
$this->addTables( 'abuse_filter_log' ); |
| 190 |
$this->addFields( 'afl_timestamp' ); |
| 191 |
$this->addFields( 'afl_rev_id' ); |
| 192 |
$this->addFields( 'afl_deleted' ); |
| 193 |
$this->addFields( 'afl_filter_id' ); |
| 194 |
$this->addFields( 'afl_global' ); |
| 195 |
$this->addFields( 'afl_ip' ); |
| 196 |
$this->addFieldsIf( 'afl_id', $fld_ids ); |
| 197 |
$this->addFieldsIf( 'afl_user_text', $fld_user ); |
| 198 |
$this->addFieldsIf( [ 'afl_namespace', 'afl_title' ], $fld_title ); |
| 199 |
$this->addFieldsIf( 'afl_action', $fld_action ); |
| 200 |
$this->addFieldsIf( 'afl_var_dump', $fld_details ); |
| 201 |
$this->addFieldsIf( 'afl_actions', $fld_result ); |
| 202 |
$this->addFieldsIf( 'afl_wiki', $fld_wiki ); |
| 203 |
|
| 204 |
if ( $fld_filter ) { |
| 205 |
$this->addTables( 'abuse_filter' ); |
| 206 |
$this->addFields( 'af_public_comments' ); |
| 207 |
|
| 208 |
$this->addJoinConds( [ |
| 209 |
'abuse_filter' => [ |
| 210 |
'LEFT JOIN', |
| 211 |
[ |
| 212 |
'af_id=afl_filter_id', |
| 213 |
'afl_global' => 0 |
| 214 |
] |
| 215 |
] |
| 216 |
] ); |
| 217 |
} |
| 218 |
|
| 219 |
$this->addOption( 'LIMIT', $params['limit'] + 1 ); |
| 220 |
|
| 221 |
$this->addWhereIf( [ 'afl_id' => $params['logid'] ], isset( $params['logid'] ) ); |
| 222 |
|
| 223 |
$this->addWhereRange( 'afl_timestamp', $params['dir'], $params['start'], $params['end'] ); |
| 224 |
|
| 225 |
if ( isset( $params['user'] ) ) { |
| 226 |
$u = $this->userFactory->newFromName( $params['user'] ); |
| 227 |
if ( $u ) { |
| 228 |
|
| 229 |
$params['user'] = $u->getName(); |
| 230 |
$userId = $u->getId(); |
| 231 |
} elseif ( IPUtils::isIPAddress( $params['user'] ) ) { |
| 232 |
|
| 233 |
$params['user'] = IPUtils::sanitizeIP( $params['user'] ); |
| 234 |
$userId = 0; |
| 235 |
} |
| 236 |
|
| 237 |
if ( isset( $userId ) ) { |
| 238 |
|
| 239 |
|
| 240 |
$this->addWhere( |
| 241 |
[ |
| 242 |
'afl_user' => $userId, |
| 243 |
'afl_user_text' => $params['user'] |
| 244 |
] |
| 245 |
); |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
$this->addWhereIf( [ 'afl_deleted' => 0 ], !$this->afPermManager->canSeeHiddenLogEntries( $performer ) ); |
| 250 |
|
| 251 |
if ( $searchFilters ) { |
| 252 |
|
| 253 |
$filterConds = [ 'local' => [], 'global' => [] ]; |
| 254 |
foreach ( $searchFilters as $filter ) { |
| 255 |
$isGlobal = $filter[1]; |
| 256 |
$key = $isGlobal ? 'global' : 'local'; |
| 257 |
$filterConds[$key][] = $filter[0]; |
| 258 |
} |
| 259 |
$dbr = $this->getDB(); |
| 260 |
$conds = []; |
| 261 |
if ( $filterConds['local'] ) { |
| 262 |
$conds[] = $dbr->andExpr( [ |
| 263 |
'afl_global' => 0, |
| 264 |
|
| 265 |
'afl_filter_id' => $filterConds['local'], |
| 266 |
] ); |
| 267 |
} |
| 268 |
if ( $filterConds['global'] ) { |
| 269 |
$conds[] = $dbr->andExpr( [ |
| 270 |
'afl_global' => 1, |
| 271 |
|
| 272 |
'afl_filter_id' => $filterConds['global'], |
| 273 |
] ); |
| 274 |
} |
| 275 |
$this->addWhere( $dbr->orExpr( $conds ) ); |
| 276 |
} |
| 277 |
|
| 278 |
if ( isset( $params['wiki'] ) ) { |
| 279 |
|
| 280 |
$this->addWhereIf( [ 'afl_wiki' => $params['wiki'] ], $isCentral ); |
| 281 |
} |
| 282 |
|
| 283 |
$title = $params['title']; |
| 284 |
if ( $title !== null ) { |
| 285 |
$titleObj = Title::newFromText( $title ); |
| 286 |
if ( $titleObj === null ) { |
| 287 |
$this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $title ) ] ); |
| 288 |
} |
| 289 |
$this->addWhereFld( 'afl_namespace', $titleObj->getNamespace() ); |
| 290 |
$this->addWhereFld( 'afl_title', $titleObj->getDBkey() ); |
| 291 |
} |
| 292 |
$res = $this->select( __METHOD__ ); |
| 293 |
|
| 294 |
$count = 0; |
| 295 |
foreach ( $res as $row ) { |
| 296 |
if ( ++$count > $params['limit'] ) { |
| 297 |
|
| 298 |
$ts = new MWTimestamp( $row->afl_timestamp ); |
| 299 |
$this->setContinueEnumParameter( 'start', $ts->getTimestamp( TS_ISO_8601 ) ); |
| 300 |
break; |
| 301 |
} |
| 302 |
$visibility = SpecialAbuseLog::getEntryVisibilityForUser( $row, $performer, $this->afPermManager ); |
| 303 |
if ( $visibility !== SpecialAbuseLog::VISIBILITY_VISIBLE ) { |
| 304 |
continue; |
| 305 |
} |
| 306 |
|
| 307 |
$filterID = $row->afl_filter_id; |
| 308 |
$global = $row->afl_global; |
| 309 |
$fullName = GlobalNameUtils::buildGlobalName( $filterID, $global ); |
| 310 |
$privacyLevel = $lookup->getFilter( $filterID, $global )->getPrivacyLevel(); |
| 311 |
$canSeeDetails = $this->afPermManager->canSeeLogDetailsForFilter( $performer, $privacyLevel ); |
| 312 |
|
| 313 |
$entry = []; |
| 314 |
if ( $fld_ids ) { |
| 315 |
$entry['id'] = intval( $row->afl_id ); |
| 316 |
$entry['filter_id'] = $canSeeDetails ? $fullName : ''; |
| 317 |
} |
| 318 |
if ( $fld_filter ) { |
| 319 |
if ( $global ) { |
| 320 |
$entry['filter'] = $lookup->getFilter( $filterID, true )->getName(); |
| 321 |
} else { |
| 322 |
$entry['filter'] = $row->af_public_comments; |
| 323 |
} |
| 324 |
} |
| 325 |
if ( $fld_user ) { |
| 326 |
$entry['user'] = $row->afl_user_text; |
| 327 |
} |
| 328 |
if ( $fld_wiki ) { |
| 329 |
$entry['wiki'] = $row->afl_wiki; |
| 330 |
} |
| 331 |
if ( $fld_title ) { |
| 332 |
$title = Title::makeTitle( $row->afl_namespace, $row->afl_title ); |
| 333 |
ApiQueryBase::addTitleInfo( $entry, $title ); |
| 334 |
} |
| 335 |
if ( $fld_action ) { |
| 336 |
$entry['action'] = $row->afl_action; |
| 337 |
} |
| 338 |
if ( $fld_result ) { |
| 339 |
$entry['result'] = $row->afl_actions; |
| 340 |
} |
| 341 |
if ( $fld_revid && $row->afl_rev_id !== null ) { |
| 342 |
$entry['revid'] = $canSeeDetails ? (int)$row->afl_rev_id : ''; |
| 343 |
} |
| 344 |
if ( $fld_timestamp ) { |
| 345 |
$ts = new MWTimestamp( $row->afl_timestamp ); |
| 346 |
$entry['timestamp'] = $ts->getTimestamp( TS_ISO_8601 ); |
| 347 |
} |
| 348 |
if ( $fld_details ) { |
| 349 |
$entry['details'] = []; |
| 350 |
if ( $canSeeDetails ) { |
| 351 |
$vars = $this->afVariablesBlobStore->loadVarDump( $row ); |
| 352 |
$varManager = $this->afVariablesManager; |
| 353 |
$entry['details'] = $varManager->exportAllVars( $vars ); |
| 354 |
|
| 355 |
$usedProtectedVars = $this->afPermManager |
| 356 |
->getUsedProtectedVariables( array_keys( $entry['details'] ) ); |
| 357 |
if ( $usedProtectedVars ) { |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
$shouldLog = false; |
| 362 |
foreach ( $usedProtectedVars as $protectedVariable ) { |
| 363 |
if ( isset( $entry['details'][$protectedVariable] ) ) { |
| 364 |
if ( $canViewProtectedValues ) { |
| 365 |
$shouldLog = true; |
| 366 |
} else { |
| 367 |
$entry['details'][$protectedVariable] = ''; |
| 368 |
} |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
if ( $shouldLog ) { |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
if ( isset( $entry['details']['user_name'] ) || |
| 377 |
isset( $entry['details']['accountname'] ) |
| 378 |
) { |
| 379 |
$logger = $this->abuseLoggerFactory->getProtectedVarsAccessLogger(); |
| 380 |
$logger->logViewProtectedVariableValue( |
| 381 |
$performer->getUser(), |
| 382 |
$entry['details']['user_name'] ?? $entry['details']['accountname'] |
| 383 |
); |
| 384 |
} else { |
| 385 |
foreach ( $usedProtectedVars as $protectedVariable ) { |
| 386 |
if ( isset( $entry['details'][$protectedVariable] ) ) { |
| 387 |
$entry['details'][$protectedVariable] = ''; |
| 388 |
} |
| 389 |
} |
| 390 |
} |
| 391 |
|
| 392 |
} |
| 393 |
} |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
if ( $fld_hidden ) { |
| 398 |
$entry['hidden'] = (bool)$row->afl_deleted; |
| 399 |
} |
| 400 |
|
| 401 |
if ( $entry ) { |
| 402 |
$fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $entry ); |
| 403 |
if ( !$fit ) { |
| 404 |
$ts = new MWTimestamp( $row->afl_timestamp ); |
| 405 |
$this->setContinueEnumParameter( 'start', $ts->getTimestamp( TS_ISO_8601 ) ); |
| 406 |
break; |
| 407 |
} |
| 408 |
} |
| 409 |
} |
| 410 |
$result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'item' ); |
| 411 |
} |
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
public function getAllowedParams() { |
| 418 |
$params = [ |
| 419 |
'logid' => [ |
| 420 |
ParamValidator::PARAM_TYPE => 'integer' |
| 421 |
], |
| 422 |
'start' => [ |
| 423 |
ParamValidator::PARAM_TYPE => 'timestamp' |
| 424 |
], |
| 425 |
'end' => [ |
| 426 |
ParamValidator::PARAM_TYPE => 'timestamp' |
| 427 |
], |
| 428 |
'dir' => [ |
| 429 |
ParamValidator::PARAM_TYPE => [ |
| 430 |
'newer', |
| 431 |
'older' |
| 432 |
], |
| 433 |
ParamValidator::PARAM_DEFAULT => 'older', |
| 434 |
ApiBase::PARAM_HELP_MSG => 'api-help-param-direction', |
| 435 |
], |
| 436 |
'user' => null, |
| 437 |
'title' => null, |
| 438 |
'filter' => [ |
| 439 |
ParamValidator::PARAM_TYPE => 'string', |
| 440 |
ParamValidator::PARAM_ISMULTI => true, |
| 441 |
ApiBase::PARAM_HELP_MSG => [ |
| 442 |
'apihelp-query+abuselog-param-filter', |
| 443 |
GlobalNameUtils::GLOBAL_FILTER_PREFIX |
| 444 |
] |
| 445 |
], |
| 446 |
'limit' => [ |
| 447 |
ParamValidator::PARAM_DEFAULT => 10, |
| 448 |
ParamValidator::PARAM_TYPE => 'limit', |
| 449 |
IntegerDef::PARAM_MIN => 1, |
| 450 |
IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1, |
| 451 |
IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2 |
| 452 |
], |
| 453 |
'prop' => [ |
| 454 |
ParamValidator::PARAM_DEFAULT => 'ids|user|title|action|result|timestamp|hidden|revid', |
| 455 |
ParamValidator::PARAM_TYPE => [ |
| 456 |
'ids', |
| 457 |
'filter', |
| 458 |
'user', |
| 459 |
'title', |
| 460 |
'action', |
| 461 |
'details', |
| 462 |
'result', |
| 463 |
'timestamp', |
| 464 |
'hidden', |
| 465 |
'revid', |
| 466 |
], |
| 467 |
ParamValidator::PARAM_ISMULTI => true |
| 468 |
] |
| 469 |
]; |
| 470 |
if ( $this->getConfig()->get( 'AbuseFilterIsCentral' ) ) { |
| 471 |
$params['wiki'] = [ |
| 472 |
ParamValidator::PARAM_TYPE => 'string', |
| 473 |
]; |
| 474 |
$params['prop'][ParamValidator::PARAM_DEFAULT] .= '|wiki'; |
| 475 |
$params['prop'][ParamValidator::PARAM_TYPE][] = 'wiki'; |
| 476 |
$params['filter'][ApiBase::PARAM_HELP_MSG] = 'apihelp-query+abuselog-param-filter-central'; |
| 477 |
} |
| 478 |
return $params; |
| 479 |
} |
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
protected function getExamplesMessages() { |
| 486 |
return [ |
| 487 |
'action=query&list=abuselog' |
| 488 |
=> 'apihelp-query+abuselog-example-1', |
| 489 |
'action=query&list=abuselog&afltitle=API' |
| 490 |
=> 'apihelp-query+abuselog-example-2', |
| 491 |
]; |
| 492 |
} |
| 493 |
} |