r46497 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r46496‎ | r46497 | r46498 >
Date:23:54, 28 January 2009
Author:werdna
Status:deferred
Tags:
Comment:
Add Special:AbuseFilter/test, which allows (trusted for now, due to DoS potential) users to enter a filter, and have it checked against the last 100 RecentChanges items while-u-wait.
Some related cleanup to change tagging in ChangesList.
Modified paths:
  • /trunk/extensions/AbuseFilter/AbuseFilter.class.php (modified) (history)
  • /trunk/extensions/AbuseFilter/AbuseFilter.hooks.php (modified) (history)
  • /trunk/extensions/AbuseFilter/AbuseFilter.i18n.php (modified) (history)
  • /trunk/extensions/AbuseFilter/AbuseFilter.php (modified) (history)
  • /trunk/extensions/AbuseFilter/SpecialAbuseFilter.php (modified) (history)
  • /trunk/extensions/AbuseFilter/Views/AbuseFilterViewList.php (modified) (history)
  • /trunk/extensions/AbuseFilter/Views/AbuseFilterViewTest.php (added) (history)
  • /trunk/extensions/AbuseFilter/edit.js (modified) (history)
  • /trunk/phase3/includes/ChangesList.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/ChangesList.php
@@ -340,6 +340,9 @@
341341 }
342342
343343 protected function insertTags( &$s, &$rc, &$classes ) {
 344+ if ( empty($rc->mAttribs['ts_tags']) )
 345+ return;
 346+
344347 list($tagSummary, $newClasses) = ChangeTags::formatSummaryRow( $rc->mAttribs['ts_tags'], 'changeslist' );
345348 $classes = array_merge( $classes, $newClasses );
346349 $s .= ' ' . $tagSummary;
Index: trunk/extensions/AbuseFilter/AbuseFilter.php
@@ -39,6 +39,7 @@
4040 $wgAutoloadClasses['AbuseFilterViewTools'] = "$dir/Views/AbuseFilterViewTools.php";
4141 $wgAutoloadClasses['AbuseFilterViewHistory'] = "$dir/Views/AbuseFilterViewHistory.php";
4242 $wgAutoloadClasses['AbuseFilterViewRevert'] = "$dir/Views/AbuseFilterViewRevert.php";
 43+$wgAutoloadClasses['AbuseFilterViewTest'] = "$dir/Views/AbuseFilterViewTest.php";
4344
4445 $wgSpecialPages['AbuseLog'] = 'SpecialAbuseLog';
4546 $wgSpecialPages['AbuseFilter'] = 'SpecialAbuseFilter';
@@ -81,6 +82,7 @@
8283 $wgAjaxExportList[] = 'AbuseFilter::ajaxCheckSyntax';
8384 $wgAjaxExportList[] = 'AbuseFilter::ajaxEvaluateExpression';
8485 $wgAjaxExportList[] = 'AbuseFilter::ajaxReAutoconfirm';
 86+$wgAjaxExportList[] = 'AbuseFilter::ajaxGetFilter';
8587
8688 // Bump the version number every time you change any of the .css/.js files
8789 $wgAbuseFilterStyleVersion = 3;
Index: trunk/extensions/AbuseFilter/SpecialAbuseFilter.php
@@ -49,6 +49,10 @@
5050 $this->mFilter = $params[1];
5151 $view = 'AbuseFilterViewRevert';
5252 }
 53+
 54+ if ( !empty($params[0]) && $params[0] == 'test' ) {
 55+ $view = 'AbuseFilterViewTest';
 56+ }
5357
5458 if (!empty($params[0]) && ($params[0] == 'history' || $params[0] == 'log') ) {
5559 if (count($params) == 1) {
Index: trunk/extensions/AbuseFilter/Views/AbuseFilterViewList.php
@@ -14,7 +14,7 @@
1515
1616 // Quick links
1717 $wgOut->addWikiMsg( 'abusefilter-links' );
18 - $lists = array( 'tools' );
 18+ $lists = array( 'tools', 'test' );
1919 if ($this->canEdit())
2020 $lists[] = 'new';
2121 $links = '';
Index: trunk/extensions/AbuseFilter/Views/AbuseFilterViewTest.php
@@ -0,0 +1,84 @@
 2+<?php
 3+
 4+if (!defined( 'MEDIAWIKI' ))
 5+ die();
 6+
 7+class AbuseFilterViewTest extends AbuseFilterView {
 8+ // Hard-coded for now.
 9+ static $mChangeLimit = 100;
 10+
 11+ function show( ) {
 12+ global $wgOut, $wgUser, $wgRequest;
 13+
 14+ AbuseFilter::disableConditionLimit();
 15+
 16+ $this->loadParameters();
 17+
 18+ $wgOut->setPageTitle( wfMsg( 'abusefilter-test' ) );
 19+ $wgOut->addWikiMsg( 'abusefilter-test-intro', self::$mChangeLimit );
 20+
 21+ $output = '';
 22+ $output .= AbuseFilter::buildEditBox( $this->mFilter, 'wpTestFilter' ) . "\n";
 23+ $output .= Xml::inputLabel( wfMsg( 'abusefilter-test-load-filter' ), 'wpInsertFilter', 'mw-abusefilter-load-filter', 10, '' ) . '&nbsp;' .
 24+ Xml::element( 'input', array( 'type' => 'button', 'value' => wfMsg( 'abusefilter-test-load' ), 'id' => 'mw-abusefilter-load' ) );
 25+ $output = Xml::tags( 'div', array( 'id' => 'mw-abusefilter-test-editor' ), $output );
 26+
 27+ // Removed until I can distinguish between positives and negatives :)
 28+// $output .= Xml::tags( 'p', null, Xml::checkLabel( wfMsg( 'abusefilter-test-shownegative' ), 'wpShowNegative', 'wpShowNegative', $this->mShowNegative ) );
 29+ $output .= Xml::tags( 'p', null, Xml::submitButton( wfMsg( 'abusefilter-test-submit' ) ) );
 30+ $output .= Xml::hidden( 'title', $this->getTitle("test")->getPrefixedText() );
 31+ $output = Xml::tags( 'form', array( 'action' => $this->getTitle("test")->getLocalURL(), 'method' => 'POST' ), $output );
 32+
 33+ $output = Xml::fieldset( wfMsg( 'abusefilter-test-legend' ), $output );
 34+
 35+ $wgOut->addHTML( $output );
 36+
 37+ if ($wgRequest->wasPosted()) {
 38+ $this->doTest();
 39+ }
 40+ }
 41+
 42+ function doTest() {
 43+ // Quick syntax check.
 44+ if ( ($result = AbuseFilter::checkSyntax( $this->mFilter )) !== true ) {
 45+ $wgOut->addWikiMsg( 'abusefilter-test-syntaxerr' );
 46+ return;
 47+ }
 48+
 49+ // Get our ChangesList
 50+ global $wgUser, $wgOut;
 51+ $changesList = ChangesList::newFromUser( $wgUser );
 52+ $output = $changesList->beginRecentChangesList();
 53+
 54+ $dbr = wfGetDB( DB_SLAVE );
 55+ $res = $dbr->select( 'recentchanges', '*', array(), __METHOD__, array( 'LIMIT' => self::$mChangeLimit, 'ORDER BY' => 'rc_timestamp asc' ) );
 56+
 57+ $counter = 1;
 58+
 59+ while ( $row = $dbr->fetchObject( $res ) ) {
 60+ $vars = AbuseFilter::getVarsFromRCRow( $row );
 61+
 62+ if (!$vars)
 63+ continue;
 64+
 65+ $result = AbuseFilter::checkConditions( $this->mFilter, $vars );
 66+
 67+ if ($result || $this->mShowNegative) {
 68+ $rc = RecentChange::newFromRow( $row );
 69+ $rc->counter = $counter++;
 70+ $output .= $changesList->recentChangesLine( $rc, false );
 71+ }
 72+ }
 73+
 74+ $output .= $changesList->endRecentChangesList();
 75+
 76+ $wgOut->addHTML( $output );
 77+ }
 78+
 79+ function loadParameters() {
 80+ global $wgRequest;
 81+
 82+ $this->mFilter = $wgRequest->getText( 'wpTestFilter' );
 83+ $this->mShowNegative = $wgRequest->getBool( 'wpShowNegative' );
 84+ }
 85+}
\ No newline at end of file
Index: trunk/extensions/AbuseFilter/AbuseFilter.class.php
@@ -52,6 +52,22 @@
5353 }
5454 }
5555
 56+ public static function ajaxGetFilter( $filter ) {
 57+ global $wgUser;
 58+ if ( !$wgUser->isAllowed( 'abusefilter-view' ) ) {
 59+ return false;
 60+ }
 61+
 62+ $dbr = wfGetDB( DB_SLAVE );
 63+ $row = $dbr->selectRow( 'abuse_filter', '*', array( 'af_id' => $filter ), __METHOD__ );
 64+
 65+ if ( $row->af_hidden && !$wgUser->isAllowed( 'abusefilter-modify' ) ) {
 66+ return false;
 67+ }
 68+
 69+ return strval($row->af_pattern);
 70+ }
 71+
5672 public static function triggerLimiter( $val = 1 ) {
5773 self::$condCount += $val;
5874
@@ -759,4 +775,111 @@
760776 $display = wfEmptyMsg( "abusefilter-action-$action", $display ) ? $action : $display;
761777 return $display;
762778 }
 779+
 780+ public static function getVarsFromRCRow( $row ) {
 781+ if ($row->rc_this_oldid) {
 782+ // It's an edit.
 783+ return self::getEditVarsFromRCRow( $row );
 784+ } elseif ( $row->rc_log_type == 'move' ) {
 785+ return self::getMoveVarsFromRCRow( $row );
 786+ } elseif ( $row->rc_log_type == 'newusers' ) {
 787+ return self::getCreateVarsFromRCRow( $row );
 788+ }
 789+ }
 790+
 791+ public static function getCreateVarsFromRCRow( $row ) {
 792+ $vars = array('ACTION' => 'createaccount');
 793+ $vars['USER_NAME'] = $vars['ACCOUNTNAME'] = Title::makeTitle( $row->rc_namespace, $row->rc_title )->getText();
 794+ return $vars;
 795+ }
 796+
 797+ public static function getEditVarsFromRCRow( $row ) {
 798+ $vars = array();
 799+ $title = Title::makeTitle( $row->rc_namespace, $row->rc_title );
 800+
 801+ $vars = array_merge( $vars, self::generateUserVars( User::newFromId( $row->rc_user ) ) );
 802+ $vars = array_merge( $vars, self::generateTitleVars( $title, 'ARTICLE_' ) );
 803+ $vars['ACTION'] = 'edit';
 804+ $vars['SUMMARY'] = $row->rc_comment;
 805+
 806+ $newRev = Revision::newFromId( $row->rc_this_oldid );
 807+ $new_text = $newRev->getText();
 808+
 809+ if ($row->rc_last_oldid) {
 810+ $oldRev = Revision::newFromId( $row->rc_last_oldid );
 811+ $old_text = $oldRev->getText();
 812+ } else {
 813+ $old_text = '';
 814+ }
 815+
 816+ $vars = array_merge( $vars, self::getEditVars( $title, $old_text, $new_text, null, $row->rc_this_oldid, $row->rc_last_oldid ) );
 817+
 818+ return $vars;
 819+ }
 820+
 821+ public static function getMoveVarsFromRCRow( $row ) {
 822+ $vars = array();
 823+
 824+ $user = User::newFromId( $row->rc_user );
 825+ $oldTitle = Title::makeTitle( $row->rc_namespace, $row->rc_title );
 826+ $newTitle = Title::newFromText( trim($row->rc_params) );
 827+
 828+ $vars = array_merge( $vars, AbuseFilter::generateUserVars( $user ),
 829+ AbuseFilter::generateTitleVars( $oldTitle, 'MOVED_FROM' ),
 830+ AbuseFilter::generateTitleVars( $newTitle, 'MOVED_TO' ) );
 831+
 832+ $vars['SUMMARY'] = $row->rc_comment;
 833+ $vars['ACTION'] = 'move';
 834+
 835+ return $vars;
 836+ }
 837+
 838+ public static function getEditVars( $title, $old_text, $new_text, $oldLinks = null, $revid=null, $oldid=null ) {
 839+ $vars = array();
 840+ $article = new Article( $title );
 841+
 842+ $vars['EDIT_DELTA'] = strlen($new_text) - strlen($old_text);
 843+ $vars['OLD_SIZE'] = strlen($old_text);
 844+ $diff = wfDiff( $old_text, $new_text );
 845+ $diff = trim( str_replace( '\No newline at end of file', '', $diff ) );
 846+ $vars['EDIT_DIFF'] = $diff;
 847+ $vars['NEW_SIZE'] = strlen($new_text);
 848+
 849+ $vars['OLD_WIKITEXT'] = $old_text;
 850+ $vars['NEW_WIKITEXT'] = $new_text;
 851+
 852+ // Some more specific/useful details about the changes.
 853+ $diff_lines = explode( "\n", $diff );
 854+ $added_lines = array();
 855+ $removed_lines = array();
 856+ foreach( $diff_lines as $line ) {
 857+ if (strpos( $line, '-' )===0) {
 858+ $removed_lines[] = substr($line,1);
 859+ } elseif (strpos( $line, '+' )===0) {
 860+ $added_lines[] = substr($line,1);
 861+ }
 862+ }
 863+ $vars['ADDED_LINES'] = implode( "\n", $added_lines );
 864+ $vars['REMOVED_LINES'] = implode( "\n", $removed_lines );
 865+
 866+ if ($oldLinks === null && $oldid) {
 867+ $oldInfo = $article->prepareTextForEdit( $old_text, $oldid );
 868+ $oldLinks = $oldInfo->output->getExternalLinks();
 869+ } elseif ($oldLinks === null) {
 870+ $oldLinks = array();
 871+ }
 872+
 873+ // Added links...
 874+ $editInfo = $article->prepareTextForEdit( $old_text, $revid );
 875+ $newLinks = array_keys( $editInfo->output->getExternalLinks() );
 876+ $vars['ALL_LINKS'] = implode( "\n", $newLinks );
 877+ $vars['ADDED_LINKS'] = implode( "\n", array_diff( $newLinks, array_intersect( $newLinks, $oldLinks ) ) );
 878+ $vars['REMOVED_LINKS'] = implode( "\n", array_diff( $oldLinks, array_intersect( $newLinks, $oldLinks ) ) );
 879+
 880+ // Pull other useful stuff from $editInfo.
 881+ $newHTML = $vars['NEW_HTML'] = $editInfo->output->getText();
 882+ $newText = $vars['NEW_TEXT'] = preg_replace( '/<[^>]+>/', '', $newHTML );
 883+
 884+ return $vars;
 885+ }
763886 }
Index: trunk/extensions/AbuseFilter/edit.js
@@ -49,6 +49,15 @@
5050 document.getElementById('wpFilterBuilder').selectedIndex = 0;
5151 }
5252
 53+function fetchFilter() {
 54+ var filter = document.getElementById( 'mw-abusefilter-load-filter' ).value;
 55+
 56+ sajax_do_call( 'AbuseFilter::ajaxGetFilter', [filter], function(request) {
 57+ var filter = request.responseText;
 58+ document.getElementById( wgFilterBoxName ).value = filter;
 59+ } );
 60+}
 61+
5362 //From http://clipmarks.com/clipmark/CEFC94CB-94D6-4495-A7AA-791B7355E284/
5463 function insertAtCursor(myField, myValue) {
5564 //IE support
@@ -107,5 +116,10 @@
108117 }
109118 } );
110119
 120+ var loader = document.getElementById( 'mw-abusefilter-load' );
 121+ if (loader) {
 122+ addHandler( loader, 'click', fetchFilter );
 123+ }
 124+
111125 setupActions();
112126 } );
\ No newline at end of file
Index: trunk/extensions/AbuseFilter/AbuseFilter.i18n.php
@@ -315,6 +315,13 @@
316316 'abusefilter-revert-reason' => 'Automatic revert of all actions taken by the abuse filter due to filter $1.
317317 Reason given: $2',
318318 'abusefilter-revert-reasonfield' => 'Reason for revert:',
 319+
 320+ 'abusefilter-test' => 'Test a filter against previous edits',
 321+ 'abusefilter-test-intro' => 'This page allows you to check a filter entered in the box below against the last $1 changes. To load an existing filter, type its filter ID into the box below the edit textbox, and click the "Load" button.',
 322+ 'abusefilter-test-legend' => 'Filter testing',
 323+ 'abusefilter-test-load-filter' => 'Load filter ID:',
 324+ 'abusefilter-test-submit' => 'Test',
 325+ 'abusefilter-test-load' => 'Load',
319326 );
320327
321328 /** Message documentation (Message documentation)
Index: trunk/extensions/AbuseFilter/AbuseFilter.hooks.php
@@ -19,44 +19,11 @@
2020
2121 $old_text = $editor->getBaseRevision() ? $editor->getBaseRevision()->getText() : '';
2222 $new_text = $editor->textbox1;
23 -
24 - $vars['EDIT_DELTA'] = strlen($new_text) - strlen($old_text);
25 - $vars['OLD_SIZE'] = strlen($old_text);
26 - $diff = wfDiff( $old_text, $new_text );
27 - $diff = trim( str_replace( '\No newline at end of file', '', $diff ) );
28 - $vars['EDIT_DIFF'] = $diff;
29 - $vars['NEW_SIZE'] = strlen($new_text);
30 -
31 - $vars['OLD_WIKITEXT'] = $old_text;
32 - $vars['NEW_WIKITEXT'] = $new_text;
33 -
34 - // Some more specific/useful details about the changes.
35 - $diff_lines = explode( "\n", $diff );
36 - $added_lines = array();
37 - $removed_lines = array();
38 - foreach( $diff_lines as $line ) {
39 - if (strpos( $line, '-' )===0) {
40 - $removed_lines[] = substr($line,1);
41 - } elseif (strpos( $line, '+' )===0) {
42 - $added_lines[] = substr($line,1);
43 - }
44 - }
45 - $vars['ADDED_LINES'] = implode( "\n", $added_lines );
46 - $vars['REMOVED_LINES'] = implode( "\n", $removed_lines );
47 -
48 - // Added links...
4923 $oldLinks = self::getOldLinks( $editor->mTitle );
50 - $editInfo = $editor->mArticle->prepareTextForEdit( $text );
51 - $newLinks = array_keys( $editInfo->output->getExternalLinks() );
52 - $vars['ALL_LINKS'] = implode( "\n", $newLinks );
53 - $vars['ADDED_LINKS'] = implode( "\n", array_diff( $newLinks, array_intersect( $newLinks, $oldLinks ) ) );
54 - $vars['REMOVED_LINKS'] = implode( "\n", array_diff( $oldLinks, array_intersect( $newLinks, $oldLinks ) ) );
5524
56 - // Pull other useful stuff from $editInfo.
57 - $newHTML = $vars['NEW_HTML'] = $editInfo->output->getText();
58 - $newText = $vars['NEW_TEXT'] = preg_replace( '/<[^>]+>/', '', $newHTML );
 25+ $vars = array_merge( $vars, AbuseFilter::getEditVars( $editor->mTitle, $old_text, $new_text, $oldLinks ) );
5926
60 - $filter_result = AbuseFilter::filterAction( $vars, $editor->mTitle );
 27+ $filter_result = AbuseFilter::filterAction( $vars, $editor->mTitle, $oldLinks );
6128
6229 if( $filter_result !== true ){
6330 $error = $filter_result;

Status & tagging log