Hoo man has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/51326


Change subject: Use the UploadVerifyFile hook
......................................................................

Use the UploadVerifyFile hook

Use the UploadVerifyFile hook instead of the UploadVerification
one as it provides more data about the upload.
This is the first step towards better upload filtering.

(Requires core change Ied2ac0397266a5ffd27da02ef6448b64c12fad86
to work with MediaWiki 1.21)

Change-Id: Ie535c7d20ed79a1e26d8d399a7c25d632c9c7fa0
---
M AbuseFilter.hooks.php
M AbuseFilter.php
2 files changed, 24 insertions(+), 13 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/AbuseFilter 
refs/changes/26/51326/1

diff --git a/AbuseFilter.hooks.php b/AbuseFilter.hooks.php
index a6edf6b..8007978 100644
--- a/AbuseFilter.hooks.php
+++ b/AbuseFilter.hooks.php
@@ -491,16 +491,19 @@
        }
 
        /**
-        * @param $saveName
-        * @param $tempName
-        * @param $error
+        * Handler for the UploadVerifyFile hook
+        *
+        * @param $upload UploadBase
+        * @param $mime
+        * @param $error array
+        *
         * @return bool
         */
-       public static function onUploadVerification( $saveName, $tempName, 
&$error ) {
+       public static function onUploadVerifyFile( $upload, $mime, &$error ) {
                $vars = new AbuseFilterVariableHolder;
 
                global $wgUser;
-               $title = Title::makeTitle( NS_FILE, $saveName );
+               $title = $upload->getTitle();
                $vars->addHolder(
                        AbuseFilterVariableHolder::merge(
                                AbuseFilter::generateUserVars( $wgUser ),
@@ -509,16 +512,24 @@
                );
 
                $vars->setVar( 'ACTION', 'upload' );
-               $vars->setVar( 'file_sha1', sha1_file( $tempName ) ); // TODO 
share with save
+
+               // We us the hexadecimal version of the file sha1
+               if ( version_compare( $wgVersion, '1.21', '<=' ) ) {
+                       // Use UploadBase::getTempFileSha1Base36 so that we 
don't have to calculate the sha1 sum again
+                       $sha1 = wfBaseConvert( $upload->getTempFileSha1Base36() 
, 36, 16, 40 );
+               } else {
+                       // UploadBase::getTempFileSha1Base36 wasn't public 
until 1.21
+                       $sha1 = sha1_file( $upload->getTempPath() );
+               }
+
+               $vars->setVar( 'file_sha1', $sha1 );
 
                $filter_result = AbuseFilter::filterAction( $vars, $title );
 
-               // XXX: HACK: return the first error, as an array containing 
the message key and
-               //      any parameters.
-               // XXX: $errors may in the future also contain Message objects. 
Make sure UploadBase can
-               //      deal with us returning that.
-               $errors = array_values( (array)$filter_result->getErrorsArray() 
);
-               $error = empty( $errors ) ? '' : $errors[0];
+               if ( !$filter_result->isOK() ) {
+                       $error = $filter_result->getErrorsArray();
+                       $error = $error[0];
+               }
 
                return $filter_result->isOK();
        }
diff --git a/AbuseFilter.php b/AbuseFilter.php
index 556c44d..fdee926 100644
--- a/AbuseFilter.php
+++ b/AbuseFilter.php
@@ -84,7 +84,7 @@
 $wgHooks['ListDefinedTags'][] = 'AbuseFilterHooks::onListDefinedTags';
 $wgHooks['LoadExtensionSchemaUpdates'][] = 
'AbuseFilterHooks::onLoadExtensionSchemaUpdates';
 $wgHooks['ContributionsToolLinks'][] = 
'AbuseFilterHooks::onContributionsToolLinks';
-$wgHooks['UploadVerification'][] = 'AbuseFilterHooks::onUploadVerification';
+$wgHooks['UploadVerifyFile'][] = 'AbuseFilterHooks::onUploadVerifyFile';
 $wgHooks['MakeGlobalVariablesScript'][] = 
'AbuseFilterHooks::onMakeGlobalVariablesScript';
 $wgHooks['ArticleSaveComplete'][] = 'AbuseFilterHooks::onArticleSaveComplete';
 

-- 
To view, visit https://gerrit.wikimedia.org/r/51326
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ie535c7d20ed79a1e26d8d399a7c25d632c9c7fa0
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/AbuseFilter
Gerrit-Branch: master
Gerrit-Owner: Hoo man <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to