From f2e05b105b39b9b683542bce0f966ecf7fcd9084 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Dziewo=C5=84ski?= Date: Wed, 3 Aug 2016 16:30:18 +0200 Subject: [PATCH] Only run filters once for direct uploads (without stash) Uses the new UploadStashFile hook. Bug: T140522 Depends-On: I2f574b355cd33b2e9fa7ff8e1793503b257cce65 Change-Id: Ic7c2dbc54c6ad300d26172796ee21027a8c372ee --- AbuseFilter.hooks.php | 33 ++++++++++++++++++++++++++------- extension.json | 1 + 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/AbuseFilter.hooks.php b/AbuseFilter.hooks.php index ce30f16ab..1e60a21ab 100644 --- a/AbuseFilter.hooks.php +++ b/AbuseFilter.hooks.php @@ -699,6 +699,8 @@ class AbuseFilterHooks { } /** + * Filter an upload. + * * @param UploadBase $upload * @param User $user * @param array $props @@ -713,6 +715,23 @@ class AbuseFilterHooks { return self::filterUpload( 'upload', $upload, $user, $props, $comment, $pageText, $error ); } + /** + * Filter an upload to stash. If a filter doesn't need to check the page contents or + * upload comment, it can use `action='stashupload'` to provide better experience to e.g. + * UploadWizard (rejecting files immediately, rather than after the user adds the details). + * + * @param UploadBase $upload + * @param User $user + * @param array $props + * @param array|ApiMessage &$error + * @return bool + */ + public static function onUploadStashFile( UploadBase $upload, User $user, + array $props, &$error + ) { + return self::filterUpload( 'stashupload', $upload, $user, $props, null, null, $error ); + } + /** * @param UploadBase $upload * @param string $mime @@ -722,17 +741,17 @@ class AbuseFilterHooks { public static function onUploadVerifyFile( $upload, $mime, &$error ) { global $wgUser, $wgVersion; - // On MW 1.27 and older, this is the only hook we can use, even though it's deficient. - // On MW 1.28 and newer, we use UploadVerifyUpload to check file uploads, and this one only - // to check file uploads to stash. If a filter doesn't need to check the page contents or - // upload comment, it can use `action='stashupload'` to provide better experience to e.g. - // UploadWizard (rejecting files immediately, rather than after the user adds the details). - $action = version_compare( $wgVersion, '1.28', '<' ) ? 'upload' : 'stashupload'; + // We only use this hook on MW 1.27 and older, as it's is the only hook we have. + // On MW 1.28 and newer, we use UploadVerifyUpload to check file uploads, and + // UploadStashFile to check file uploads to stash. + if ( version_compare( $wgVersion, '1.28', '>=' ) ) { + return; + } // UploadBase makes it absolutely impossible to get these out of it, even though it knows them. $props = FSFile::getPropsFromPath( $upload->getTempPath() ); - return self::filterUpload( $action, $upload, $wgUser, $props, null, null, $error ); + return self::filterUpload( 'upload', $upload, $wgUser, $props, null, null, $error ); } /** diff --git a/extension.json b/extension.json index 032ccf17a..b87967d8c 100644 --- a/extension.json +++ b/extension.json @@ -180,6 +180,7 @@ "ContributionsToolLinks": "AbuseFilterHooks::onContributionsToolLinks", "UploadVerifyFile": "AbuseFilterHooks::onUploadVerifyFile", "UploadVerifyUpload": "AbuseFilterHooks::onUploadVerifyUpload", + "UploadStashFile": "AbuseFilterHooks::onUploadStashFile", "MakeGlobalVariablesScript": "AbuseFilterHooks::onMakeGlobalVariablesScript", "ArticleSaveComplete": "AbuseFilterHooks::onArticleSaveComplete", "UserMergeAccountFields": "AbuseFilterHooks::onUserMergeAccountFields",