Legoktm has uploaded a new change for review. (
https://gerrit.wikimedia.org/r/365866 )
Change subject: [POC] Refactor spam filter edit checks
......................................................................
[POC] Refactor spam filter edit checks
EditAttempt is a dumb value/container object to represent an attemped
edit and accompanying context that the EditPage class would normally
have been used for.
EditFilterBase is an abstract class that all filters will inherit from.
EditFilterManager constructs and dispatches to the individual filters.
Bug: T170184
Change-Id: I62295b530400757300474396b6ced9c4bf9a7551
---
M autoload.php
A includes/edit/EditAttempt.php
A includes/edit/EditFilterBase.php
A includes/edit/EditFilterManager.php
A includes/edit/SimpleAntiSpamFilter.php
5 files changed, 218 insertions(+), 0 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core
refs/changes/66/365866/1
diff --git a/autoload.php b/autoload.php
index a6128a4..cb1539a 100644
--- a/autoload.php
+++ b/autoload.php
@@ -875,7 +875,11 @@
'MediaWiki\\Auth\\UsernameAuthenticationRequest' => __DIR__ .
'/includes/auth/UsernameAuthenticationRequest.php',
'MediaWiki\\Diff\\ComplexityException' => __DIR__ .
'/includes/diff/ComplexityException.php',
'MediaWiki\\Diff\\WordAccumulator' => __DIR__ .
'/includes/diff/WordAccumulator.php',
+ 'MediaWiki\\Edit\\EditAttempt' => __DIR__ .
'/includes/edit/EditAttempt.php',
+ 'MediaWiki\\Edit\\EditFilterBase' => __DIR__ .
'/includes/edit/EditFilterBase.php',
+ 'MediaWiki\\Edit\\EditFilterManager' => __DIR__ .
'/includes/edit/EditFilterManager.php',
'MediaWiki\\Edit\\PreparedEdit' => __DIR__ .
'/includes/edit/PreparedEdit.php',
+ 'MediaWiki\\Edit\\SimpleAntiSpamFilter' => __DIR__ .
'/includes/edit/SimpleAntiSpamFilter.php',
'MediaWiki\\HeaderCallback' => __DIR__ . '/includes/HeaderCallback.php',
'MediaWiki\\Interwiki\\ClassicInterwikiLookup' => __DIR__ .
'/includes/interwiki/ClassicInterwikiLookup.php',
'MediaWiki\\Interwiki\\InterwikiLookup' => __DIR__ .
'/includes/interwiki/InterwikiLookup.php',
diff --git a/includes/edit/EditAttempt.php b/includes/edit/EditAttempt.php
new file mode 100644
index 0000000..78b22d2
--- /dev/null
+++ b/includes/edit/EditAttempt.php
@@ -0,0 +1,61 @@
+<?php
+/**
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ */
+
+
+namespace MediaWiki\Edit;
+
+
+/**
+ * Container for variables related to an attempted edit
+ *
+ * @since 1.30
+ */
+class EditAttempt {
+
+ /**
+ * @var \Content
+ */
+ public $content;
+
+ /**
+ * @var string
+ */
+ public $summary;
+
+ /**
+ * @var \WebRequest
+ */
+ public $request;
+
+ /**
+ * @var \User
+ */
+ public $user;
+
+ /**
+ * @var \WikiPage
+ */
+ public $wikiPage;
+
+ /**
+ * @var \Title
+ */
+ public $title;
+}
diff --git a/includes/edit/EditFilterBase.php b/includes/edit/EditFilterBase.php
new file mode 100644
index 0000000..58b78d5
--- /dev/null
+++ b/includes/edit/EditFilterBase.php
@@ -0,0 +1,38 @@
+<?php
+/**
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ */
+
+
+namespace MediaWiki\Edit;
+
+
+abstract class EditFilterBase {
+
+ /**
+ * @var int Numerical priority. Higher means to run earlier
+ */
+ protected $priority = 5;
+
+ /**
+ * @param EditAttempt $attempt
+ * @return \StatusValue
+ */
+ abstract public function filter( EditAttempt $attempt );
+
+}
diff --git a/includes/edit/EditFilterManager.php
b/includes/edit/EditFilterManager.php
new file mode 100644
index 0000000..aae6743
--- /dev/null
+++ b/includes/edit/EditFilterManager.php
@@ -0,0 +1,59 @@
+<?php
+/**
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ */
+
+
+namespace MediaWiki\Edit;
+
+use Hooks;
+use StatusValue;
+
+class EditFilterManager {
+
+ /**
+ * @var EditFilterBase[]
+ */
+ private $filters = [];
+
+ public function __construct() {
+ // register core filters..
+ $this->filters[] = new SimpleAntiSpamFilter();
+
+ // register extension filters
+
+ // usort based on priority
+ }
+
+ /**
+ * @param EditAttempt $attempt
+ * @return StatusValue
+ */
+ public function filter( EditAttempt $attempt ) {
+ $status = new StatusValue();
+ foreach ( $this->filters as $filter ) {
+ $status->merge( $filter->filter( $attempt ) );
+ if ( !$status->isOK() ) {
+ return $status;
+ }
+ }
+
+ return $status;
+ }
+
+}
diff --git a/includes/edit/SimpleAntiSpamFilter.php
b/includes/edit/SimpleAntiSpamFilter.php
new file mode 100644
index 0000000..8460492
--- /dev/null
+++ b/includes/edit/SimpleAntiSpamFilter.php
@@ -0,0 +1,56 @@
+<?php
+/**
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ */
+
+
+namespace MediaWiki\Edit;
+
+use MediaWiki\Logger\LoggerFactory;
+use StatusValue;
+
+
+class SimpleAntiSpamFilter extends EditFilterBase {
+ protected $priority = 100;
+
+ /**
+ * Check if the 'wpAntiSpam' form field was filled in, and
+ * reject the edit if it was.
+ *
+ * @param EditAttempt $attempt
+ * @return StatusValue
+ */
+ public function filter( EditAttempt $attempt ) {
+ $spam = $attempt->request->getText( 'wpAntispam' );
+ if ( $spam !== '' ) {
+ $logger = LoggerFactory::getInstance( 'SimpleAntiSpam'
);
+ $logger->info(
+ '{user} editing "{page}" submitted bogus field
"{spam}"',
+ [
+ 'user' => $attempt->user->getName(),
+ 'page' =>
$attempt->title->getPrefixedText(),
+ 'spam' => $spam,
+ ]
+ );
+ return StatusValue::newFatal( 'spamprotectionmatch' );
+ }
+
+ return StatusValue::newGood();
+ }
+
+}
--
To view, visit https://gerrit.wikimedia.org/r/365866
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I62295b530400757300474396b6ced9c4bf9a7551
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Legoktm <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits