r41319 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r41318‎ | r41319 | r41320 >
Date:22:23, 27 September 2008
Author:ialex
Status:old
Tags:
Comment:
Code cleanup for SpamRegex extension:
* Use AbortMove hook rather than SpecialMovePageBeforeMove, the latter doesn't exist in trunk
* Removed wfSpamRegexGetTable(), use $wgSharedTables to use a global table, updated memcached keys in that way
* Put hooks in a class, it will be autoloaded when needed
* Put the SQL statement in a file, taken from http://www.mediawiki.org/wiki/Extension:SpamRegex
* Fixed some E_NOTICE
* Fixed some XHTML errors ("&" not escaped in urls)
* Whitespaces fixes
Modified paths:
  • /trunk/extensions/SpamRegex/SpamRegex.php (modified) (history)
  • /trunk/extensions/SpamRegex/SpamRegex.sql (added) (history)
  • /trunk/extensions/SpamRegex/SpamRegexCore.php (modified) (history)
  • /trunk/extensions/SpamRegex/SpecialSpamRegex.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SpamRegex/SpamRegex.php
@@ -21,23 +21,29 @@
2222 define('SPAMREGEX_TEXTBOX', 0);
2323 define('SPAMREGEX_SUMMARY', 1);
2424
25 -/* return the name of the table */
26 -function wfSpamRegexGetTable() {
27 - global $wgSharedDB;
28 - if ("" != $wgSharedDB) {
29 - return "{$wgSharedDB}.spam_regex";
30 - } else {
31 - return "spam_regex";
32 - }
33 -}
 25+//Extension credits
 26+$wgExtensionCredits['specialpage'][] = array(
 27+ 'name' => 'Regular Expression Spam Block',
 28+ 'version' => '1.0',
 29+ 'author' => 'Bartek Łapiński',
 30+ 'url' => 'http://www.mediawiki.org/wiki/Extension:SpamRegex',
 31+ 'description' => 'Filters out unwanted phrases in edited pages, based on regular expressions',
 32+ 'descriptionmsg' => 'spamregex-desc',
 33+);
3434
 35+//New user right
 36+$wgAvailableRights[] = 'spamregex';
 37+$wgGroupPermissions['staff']['spamregex'] = true;
 38+
3539 /* return the proper db key for Memc */
36 -function wfSpamRegexGetMemcDB() {
37 - global $wgSharedDB, $wgDBname;
38 - if (!empty( $wgSharedDB ) ) {
39 - return $wgSharedDB;
 40+function wfSpamRegexCacheKey( /*...*/ ) {
 41+ global $wgSharedDB, $wgSharedTables, $wgSharedPrefix;
 42+ $args = func_get_args();
 43+ if( in_array( 'spam_regex', $wgSharedTables ) ) {
 44+ $args = array_merge( array( $wgSharedDB, $wgSharedPrefix ), $args );
 45+ return call_user_func_array( 'wfForeignMemcKey', $args );
4046 } else {
41 - return $wgDBname;
 47+ return call_user_func_array( 'wfMemcKey', $args );
4248 }
4349 }
4450
@@ -45,22 +51,11 @@
4652 $wgExtensionMessagesFiles['SpamRegex'] = $dir . 'SpamRegex.i18n.php';
4753 $wgExtensionAliasesFiles['SpamRegex'] = $dir . 'SpamRegex.alias.php';
4854 $wgAutoloadClasses['SpamRegex'] = $dir . 'SpecialSpamRegex.php';
 55+$wgAutoloadClasses['SpamRegexHooks'] = $dir . 'SpamRegexCore.php';
4956 $wgSpecialPages['SpamRegex'] = 'SpamRegex';
5057 $wgSpecialPageGroups['SpamRegex'] = 'pagetools';
5158
52 -//New user right
53 -$wgAvailableRights[] = 'spamregex';
54 -$wgGroupPermissions['staff']['spamregex'] = true;
 59+$wgHooks['EditFilter'][] = 'SpamRegexHooks::onEditFilter';
 60+$wgHooks['AbortMove'][] = 'SpamRegexHooks::onAbortMove';
5561
56 -//Extension credits
57 -$wgExtensionCredits['specialpage'][] = array(
58 - 'name' => 'Regular Expression Spam Block',
59 - 'version' => '1.0',
60 - 'author' => 'Bartek Łapiński',
61 - 'url' => 'http://www.mediawiki.org/wiki/Extension:SpamRegex',
62 - 'description' => 'Filters out unwanted phrases in edited pages, based on regular expressions',
63 - 'descriptionmsg' => 'spamregex-desc',
64 -);
65 -
66 -require_once("$IP/extensions/SpamRegex/SpamRegexCore.php");
6762 require_once("$IP/extensions/SimplifiedRegex/SimplifiedRegex.php");
\ No newline at end of file
Index: trunk/extensions/SpamRegex/SpecialSpamRegex.php
@@ -13,7 +13,7 @@
1414 */
1515 function __construct() {
1616 parent::__construct( 'SpamRegex', 'spamregex', true, 'execute', false );
17 - wfLoadExtensionMessages('SpamRegex');
 17+ wfLoadExtensionMessages( 'SpamRegex' );
1818 }
1919
2020 /**
@@ -27,9 +27,9 @@
2828 if ( $wgUser->isBlocked() ) {
2929 $wgOut->blockedPage();
3030 return;
31 - }
 31+ }
3232 if ( wfReadOnly() ) {
33 - $wgOut->readOnlyPage();
 33+ $wgOut->readOnlyPage();
3434 return;
3535 }
3636 if ( !$wgUser->isAllowed( 'spamregex' ) ) {
@@ -37,9 +37,9 @@
3838 return;
3939 }
4040
41 - $wgOut->setPageTitle(wfMsgHtml('spamregex-page-title'));
42 - $sRF = new spamRegexForm($par);
43 - $sRL = new spamRegexList($par);
 41+ $wgOut->setPageTitle( wfMsgHtml( 'spamregex-page-title' ) );
 42+ $sRF = new spamRegexForm( $par );
 43+ $sRL = new spamRegexList( $par );
4444
4545 $action = $wgRequest->getVal( 'action' );
4646 if ( 'success_block' == $action ) {
@@ -49,17 +49,17 @@
5050 $sRL->showSuccess();
5151 $sRF->showForm('');
5252 } else if ( 'failure_unblock' == $action ) {
53 - $text = htmlspecialchars ($wgRequest->getVal('text'));
54 - $sRF->showForm( wfMsg('spamregex-error-unblocking', $text) );
 53+ $text = htmlspecialchars ($wgRequest->getVal( 'text' ) );
 54+ $sRF->showForm( wfMsg( 'spamregex-error-unblocking', $text ) );
5555 } else if ( $wgRequest->wasPosted() && 'submit' == $action &&
56 - $wgUser->matchEditToken( $wgRequest->getVal('wpEditToken') ) ) {
 56+ $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) ) {
5757 $sRF->doSubmit();
58 - } else if ('delete' == $action) {
 58+ } else if ( 'delete' == $action ) {
5959 $sRL->deleteFromList();
6060 } else {
61 - $sRF->showForm('');
 61+ $sRF->showForm( '' );
6262 }
63 - $sRL->showList('', $offset );
 63+ $sRL->showList( '' );
6464 }
6565 }
6666
@@ -73,7 +73,7 @@
7474 }
7575
7676 /* wrapper for GET values */
77 - function wfSpamRegexGetListBits() {
 77+ public static function getListBits() {
7878 global $wgRequest;
7979 $pieces = array();
8080 list( $limit, $offset ) = $wgRequest->getLimitOffset();
@@ -84,11 +84,11 @@
8585 }
8686
8787 /* useful for cleaning the memcached keys */
88 - function wfSpamRegexUnsetKeys() {
89 - global $wgMemc, $wgSharedDB;
90 - $wgMemc->delete("$wgSharedDB:spamRegexCore:spamRegex:Textbox");
91 - $wgMemc->delete("$wgSharedDB:spamRegexCore:spamRegex:Summary");
92 - $wgMemc->delete("$wgSharedDB:spamRegexCore:numResults");
 88+ public static function unsetKeys() {
 89+ global $wgMemc;
 90+ $wgMemc->delete( wfSpamRegexCacheKey( 'spamRegexCore', 'spamRegex', 'Textbox' ) );
 91+ $wgMemc->delete( wfSpamRegexCacheKey( 'spamRegexCore', 'spamRegex', 'Summary' ) );
 92+ $wgMemc->delete( wfSpamRegexCacheKey( 'spamRegexCore', 'numResults' ) );
9393 }
9494
9595 /**
@@ -98,93 +98,88 @@
9999 */
100100 function showList( $err ) {
101101 wfProfileIn( __METHOD__ );
102 - global $wgOut, $wgRequest, $wgMemc, $wgLang;
 102+ global $wgOut, $wgRequest, $wgLang;
103103
104104 /* on error, display error */
105105 if ( "" != $err ) {
106 - $wgOut->addHTML("<p class='error'>{$err}</p>\n");
 106+ $wgOut->addHTML( "<p class='error'>{$err}</p>\n" );
107107 }
108 - $titleObj = Title::makeTitle( NS_SPECIAL, 'SpamRegex' );
109 - $action = $titleObj->escapeLocalURL("") ."?".$this->wfSpamRegexGetListBits();
110 - $action_unblock = $titleObj->escapeLocalURL("action=delete") ."&".$this->wfSpamRegexGetListBits();
111 - list( $limit, $offset ) = $wgRequest->getLimitOffset();
112108
113 - $wgOut->addWikiText('<br /><br />'.wfMsg('spamregex-currently-blocked'));
114 - $this->showPrevNext($wgOut);
 109+ $wgOut->addWikiMsg( 'spamregex-currently-blocked' );
 110+ /* get data and play with data */
 111+ $dbr = wfGetDB( DB_SLAVE );
 112+ if ( !$this->fetchNumResults() ) {
 113+ $wgOut->addWikiMsg( 'spamregex-no-currently-blocked' );
 114+ } else {
 115+ $titleObj = SpecialPage::getTitleFor( 'SpamRegex' );
 116+ $action = $titleObj->escapeLocalURL( self::getListBits() );
 117+ $action_unblock = $titleObj->escapeLocalURL( 'action=delete&'.self::getListBits() );
 118+ list( $limit, $offset ) = $wgRequest->getLimitOffset();
115119
116 - if ( 0 == $this->fetchNumResults() ) {
117 - $wgOut->addWikiText('<br />'.wfMsg('spamregex-no-currently-blocked').'<br /><br />');
118 - }
 120+ $this->showPrevNext( $wgOut );
 121+ $wgOut->addHTML( "<form name=\"spamregexlist\" method=\"get\" action=\"{$action}\">" );
119122
120 - $wgOut->addHTML("<form name=\"spamregexlist\" method=\"get\" action=\"{$action}\">");
121 - /* get data and play with data */
122 - $dbr = wfGetDB(DB_SLAVE);
123 - $query = "SELECT * FROM ".wfSpamRegexGetTable();
124 - $query .= " order by spam_text";
125 -
126 - $query = $dbr->limitResult($query, $limit, $offset);
127 - $res = $dbr->query($query);
128 - while ( $row = $dbr->fetchObject( $res ) ) {
129 - $time = $wgLang->timeanddate( wfTimestamp( TS_MW, $row->spam_timestamp ), true );
130 - $ublock_ip = urlencode($row->spam_text);
131 - $desc = "";
132 - if ($row->spam_textbox == 1) {
133 - $desc .= wfMsg('spamregex-text');
134 - }
135 - if ($row->spam_summary == 1) {
136 - if ($row->spam_textbox == 1) {
137 - $desc .= " ";
 123+ $res = $dbr->select( 'spam_regex', '*', array(), __METHOD__, array( 'LIMIT' => $limit, 'OFFSET' => $offset ) );
 124+ while ( $row = $res->fetchObject() ) {
 125+ $time = $wgLang->timeanddate( wfTimestamp( TS_MW, $row->spam_timestamp ), true );
 126+ $ublock_ip = urlencode($row->spam_text);
 127+ $desc = '';
 128+ if ( $row->spam_textbox == 1 ) {
 129+ $desc .= wfMsg( 'spamregex-text' );
138130 }
139 - $desc .= wfMsg('spamregex-summary-log');
 131+ if ( $row->spam_summary == 1 ) {
 132+ if ( $row->spam_textbox == 1 ) {
 133+ $desc .= " ";
 134+ }
 135+ $desc .= wfMsg( 'spamregex-summary-log' );
 136+ }
 137+ $wgOut->addHTML( "<ul>" );
 138+ $wgOut->addWikiText( wfMsg( 'spamregex-log-1', $row->spam_text, $desc, $action_unblock, $ublock_ip ) . $row->spam_user . wfMsg( 'spamregex-log-2', $time ) );
 139+ $wgOut->addHTML( "</ul>" );
140140 }
141 - $wgOut->addHTML("<ul>");
142 - $wgOut->addWikiText(wfMsg('spamregex-log-1', $row->spam_text, $desc, $action_unblock, $ublock_ip) .$row->spam_user . wfMsg('spamregex-log-2', $time));
143 - $wgOut->addHTML("</ul>");
 141+ $res->free();
 142+ $wgOut->addHTML( "</form>" );
 143+ $this->showPrevNext( $wgOut );
144144 }
145 - $dbr->freeResult($res);
146 - $wgOut->addHTML("</form>");
147 - $this->showPrevNext($wgOut);
148145 wfProfileOut( __METHOD__ );
149146 }
150147
151148 /* remove from list - without confirmation */
152149 function deleteFromList() {
153150 wfProfileIn( __METHOD__ );
154 - global $wgOut, $wgRequest, $wgMemc, $wgUser;
155 - $text = urldecode( $wgRequest->getVal('text') );
 151+ global $wgOut, $wgRequest, $wgUser;
 152+ $text = urldecode( $wgRequest->getVal( 'text' ) );
156153 /* delete */
157154 $dbw = wfGetDB( DB_MASTER );
158 - $query = "DELETE FROM ".wfSpamRegexGetTable()." WHERE spam_text = ".$dbw->addQuotes($text);
159 - $dbw->query($query);
160 - $titleObj = Title::makeTitle( NS_SPECIAL, 'SpamRegex' );
 155+ $dbw->delete( 'spam_regex', array( 'spam_text' => $text ), __METHOD__ );
 156+ $titleObj = SpecialPage::getTitleFor( 'SpamRegex' );
161157 if ( $dbw->affectedRows() ) {
162158 /* success */
163 - $this->wfSpamRegexUnsetKeys();
 159+ self::unsetKeys();
164160 wfProfileOut( __METHOD__ );
165 - $wgOut->redirect( $titleObj->getFullURL( 'action=success_unblock&text='.urlencode($text).'&'.$this->wfSpamRegexGetListBits() ) );
 161+ $wgOut->redirect( $titleObj->getFullURL( 'action=success_unblock&text='.urlencode($text).'&'.self::getListBits() ) );
166162 } else {
167163 wfProfileOut( __METHOD__ );
168 - $wgOut->redirect( $titleObj->getFullURL( 'action=failure_unblock&text='.urlencode($text).'&'.$this->wfSpamRegexGetListBits() ) );
 164+ $wgOut->redirect( $titleObj->getFullURL( 'action=failure_unblock&text='.urlencode($text).'&'.self::getListBits() ) );
169165 }
170166 }
171167
172 - /* fetch number of all rows */
 168+ /**
 169+ * fetch number of all rows
 170+ * Use memcached if possible
 171+ */
173172 function fetchNumResults() {
174173 wfProfileIn( __METHOD__ );
175 - global $wgMemc, $wgSharedDB;
 174+ global $wgMemc;
176175
177176 /* we use memcached here */
178 - $key = wfSpamRegexGetMemcDB() . ":spamRegexCore:numResults";
179 - $cached = $wgMemc->get($key);
 177+ $key = wfSpamRegexCacheKey( 'spamRegexCore', 'numResults' );
 178+ $cached = $wgMemc->get( $key );
180179 $results = 0;
181 - if ( is_null($cached) ) {
182 - $dbr = wfGetDB(DB_SLAVE);
183 - $query_count = "SELECT COUNT(*) as n FROM ".wfSpamRegexGetTable();
184 - $res_count = $dbr->query($query_count);
185 - $row_count = $dbr->fetchObject($res_count);
186 - $results = $row_count->n;
187 - $wgMemc->set($key, $results, REGEXBLOCK_EXPIRE);
188 - $dbr->freeResult($res_count);
 180+ if ( true || is_null( $cached ) ) {
 181+ $dbr = wfGetDB( DB_SLAVE );
 182+ $results = $dbr->selectField( 'spam_regex', 'COUNT(*)', '', __METHOD__ );
 183+ $wgMemc->set( $key, $results, SPAMREGEX_EXPIRE );
189184 } else {
190185 $results = $cached;
191186 }
@@ -193,22 +188,23 @@
194189 return $results;
195190 }
196191
 192+
197193 /* draws one option for select */
198194 function makeOption($blocker, $current) {
199195 global $wgOut;
200196 if ($blocker == $current) {
201 - $wgOut->addHTML("<option selected=\"selected\" value=\"{$blocker}\">{$blocker}</option>");
 197+ $wgOut->addHTML( "<option selected=\"selected\" value=\"{$blocker}\">{$blocker}</option>" );
202198 } else {
203 - $wgOut->addHTML("<option value=\"{$blocker}\">{$blocker}</option>");
 199+ $wgOut->addHTML( "<option value=\"{$blocker}\">{$blocker}</option>" );
204200 }
205201 }
206202
207203 /* on success */
208204 function showSuccess() {
209205 global $wgOut, $wgRequest;
210 - $wgOut->setPageTitle(wfMsg('spamregex-page-title-1'));
211 - $wgOut->setSubTitle(wfMsg('spamregex-unblock-success'));
212 - $wgOut->addWikiText(wfMsg('spamregex-unblock-message', $wgRequest->getVal('text', $par)));
 206+ $wgOut->setPageTitle( wfMsg( 'spamregex-page-title-1' ) );
 207+ $wgOut->setSubTitle( wfMsg( 'spamregex-unblock-success' ) );
 208+ $wgOut->addWikiMsg( 'spamregex-unblock-message', $wgRequest->getVal( 'text' ) );
213209 }
214210
215211 /* init for showprevnext */
@@ -236,8 +232,8 @@
237233 function spamRegexForm( $par ) {
238234 global $wgRequest;
239235 $this->mBlockedPhrase = $wgRequest->getVal( 'wpBlockedPhrase', $wgRequest->getVal( 'text', $par ) );
240 - ($wgRequest->getVal('wpBlockedTextbox') ) ? $this->mBlockedTextbox = 1 : $this->mBlockedTextbox = 0;
241 - ($wgRequest->getVal('wpBlockedSummary') ) ? $this->mBlockedSummary = 1 : $this->mBlockedSummary = 0;
 236+ $this->mBlockedTextbox = $wgRequest->getCheck( 'wpBlockedTextbox' ) ? 1 : 0;
 237+ $this->mBlockedSummary = $wgRequest->getCheck( 'wpBlockedSummary' ) ? 1 : 0;
242238 }
243239
244240 /* output */
@@ -245,17 +241,20 @@
246242 global $wgOut, $wgUser, $wgRequest;
247243
248244 $token = htmlspecialchars( $wgUser->editToken() );
249 - $titleObj = Title::makeTitle( NS_SPECIAL, 'SpamRegex' );
250 - $action = $titleObj->escapeLocalURL( "action=submit" )."&".spamRegexList::wfSpamRegexGetListBits();
 245+ $titleObj = SpecialPage::getTitleFor( 'SpamRegex' );
 246+ $action = $titleObj->escapeLocalURL( "action=submit&".spamRegexList::getListBits() );
251247
252248 if ( "" != $err ) {
253249 $wgOut->setSubtitle( wfMsgHtml( 'formerror' ) );
254250 $wgOut->addHTML( "<p class='error'>{$err}</p>\n" );
255251 }
256252
257 - $wgOut->addWikiMsg('spamregex-intro');
 253+ $wgOut->addWikiMsg( 'spamregex-intro' );
258254
259 - ( 'submit' == $wgRequest->getVal( 'action' )) ? $scBlockedPhrase = htmlspecialchars ($this->mBlockedPhrase) : $scBlockedPhrase = '';
 255+ if( 'submit' == $wgRequest->getVal( 'action' ) )
 256+ $scBlockedPhrase = htmlspecialchars( $this->mBlockedPhrase );
 257+ else
 258+ $scBlockedPhrase = '';
260259
261260 $wgOut->addScript("
262261 <script type=\"text/javascript\">
@@ -283,10 +282,10 @@
284283 addOnloadHook (SpamRegexEnhanceControls);
285284 </script>"
286285 );
287 - $phraseblock = wfMsg('spamregex-phrase-block');
288 - $phraseblocktext = wfMsg('spamregex-phrase-block-text');
289 - $phraseblocksummary = wfMsg('spamregex-phrase-block-summary');
290 - $blockphrase = wfMsg('spamregex-block-submit');
 286+ $phraseblock = wfMsgExt( 'spamregex-phrase-block', array( 'parseinline' ) );
 287+ $phraseblocktext = wfMsgExt( 'spamregex-phrase-block-text', array( 'parseinline' ) );
 288+ $phraseblocksummary = wfMsgExt( 'spamregex-phrase-block-summary', array( 'parseinline' ) );
 289+ $blockphrase = wfMsgExt('spamregex-block-submit', array( 'escapenoentities' ) );
291290 $wgOut->addHTML("
292291 <form name=\"spamregex\" method=\"post\" action=\"{$action}\">
293292 <table border=\"0\">
@@ -324,8 +323,8 @@
325324 /* on success */
326325 function showSuccess() {
327326 global $wgOut, $wgRequest;
328 - $wgOut->setPageTitle(wfMsg('spamregex-page-title-2'));
329 - $wgOut->setSubTitle(wfMsg('spamregex-block-success'));
 327+ $wgOut->setPageTitle( wfMsg( 'spamregex-page-title-2' ) );
 328+ $wgOut->setSubTitle( wfMsg( 'spamregex-block-success' ) );
330329
331330 $wgOut->addWikiMsg('spamregex-block-message', $this->mBlockedPhrase);
332331 }
@@ -337,13 +336,13 @@
338337
339338 /* empty name */
340339 if ( strlen($this->mBlockedPhrase) == 0 ) {
341 - $this->showForm(wfMsgHtml('spamregex-warning-1'));
 340+ $this->showForm( wfMsgHtml( 'spamregex-warning-1' ) );
342341 wfProfileOut( __METHOD__ );
343342 return;
344343 }
345344 /* validate expression */
346 - if ( !$simple_regex = wfValidRegex($this->mBlockedPhrase) ) {
347 - $this->showForm(wfMsgHtml('spamregex-error-1'));
 345+ if ( !$simple_regex = wfValidRegex( $this->mBlockedPhrase ) ) {
 346+ $this->showForm( wfMsgHtml( 'spamregex-error-1' ) );
348347 wfProfileOut( __METHOD__ );
349348 return;
350349 }
@@ -354,33 +353,35 @@
355354 $timestamp = wfTimestampNow();
356355
357356 /* we need at least one block mode specified... we can have them both, of course */
358 - if ( ($this->mBlockedTextbox == 0) && ($this->mBlockedSummary == 0) ) {
359 - $this->showForm(wfMsgHtml('spamregex-warning-2'));
 357+ if ( !$this->mBlockedTextbox && !$this->mBlockedSummary ) {
 358+ $this->showForm( wfMsgHtml( 'spamregex-warning-2' ) );
360359 wfProfileOut( __METHOD__ );
361360 return;
362361 }
363362
364 - $query = "INSERT IGNORE INTO ".wfSpamRegexGetTable()
365 - ." (spam_id, spam_text, spam_timestamp, spam_user, spam_textbox, spam_summary)
366 - VALUES (null,
367 - {$dbw->addQuotes($this->mBlockedPhrase)},
368 - {$timestamp},
369 - {$dbw->addQuotes($name)},
370 - {$this->mBlockedTextbox},
371 - {$this->mBlockedSummary}
372 - )";
373 - $dbw->query($query);
 363+ $dbw->insert(
 364+ 'spam_regex',
 365+ array(
 366+ 'spam_text' => $this->mBlockedPhrase,
 367+ 'spam_timestamp' => $timestamp,
 368+ 'spam_user' => $name,
 369+ 'spam_textbox' => $this->mBlockedTextbox,
 370+ 'spam_summary' => $this->mBlockedSummary
 371+ ),
 372+ __METHOD__,
 373+ array( 'IGNORE' )
 374+ );
374375
375376 /* duplicate entry */
376 - if (!$dbw->affectedRows()) {
377 - $this->showForm(wfMsgHtml('spamregex-already-blocked', $this->mBlockedPhrase));
 377+ if ( !$dbw->affectedRows() ) {
 378+ $this->showForm( wfMsgHtml( 'spamregex-already-blocked', $this->mBlockedPhrase ) );
378379 wfProfileOut( __METHOD__ );
379380 return;
380381 }
381 - spamRegexList::wfSpamRegexUnsetKeys($name);
 382+ spamRegexList::unsetKeys();
382383 /* redirect */
383 - $titleObj = Title::makeTitle( NS_SPECIAL, 'SpamRegex' );
 384+ $titleObj = SpecialPage::getTitleFor( 'SpamRegex' );
384385 wfProfileOut( __METHOD__ );
385 - $wgOut->redirect( $titleObj->getFullURL( 'action=success_block&text=' .urlencode( $this->mBlockedPhrase )."&".spamRegexList::wfSpamRegexGetListBits() ) );
 386+ $wgOut->redirect( $titleObj->getFullURL( 'action=success_block&text=' .urlencode( $this->mBlockedPhrase ).'&'.spamRegexList::getListBits() ) );
386387 }
387388 }
\ No newline at end of file
Index: trunk/extensions/SpamRegex/SpamRegex.sql
@@ -0,0 +1,12 @@
 2+CREATE TABLE /*$wgDBprefix*/spam_regex (
 3+ spam_id int(5) NOT NULL AUTO_INCREMENT,
 4+ spam_text varchar(255) NOT NULL,
 5+ spam_timestamp char(14) NOT NULL,
 6+ spam_user varchar(255) NOT NULL,
 7+ spam_textbox int(1) NOT NULL DEFAULT 1,
 8+ spam_summary int(1) NOT NULL DEFAULT 0,
 9+ PRIMARY KEY( spam_id ),
 10+ UNIQUE KEY spam_text( spam_text ),
 11+ KEY spam_timestamp ( spam_timestamp ),
 12+ KEY spam_user ( spam_user )
 13+) /*$wgDBTableOptions*/;
\ No newline at end of file
Property changes on: trunk/extensions/SpamRegex/SpamRegex.sql
___________________________________________________________________
Added: svn:eol-style
114 + native
Index: trunk/extensions/SpamRegex/SpamRegexCore.php
@@ -1,110 +1,105 @@
22 <?php
 3+
34 /**
45 * Fetch phrases to block, and fill $wgSpamRegex with them, rather than scribble that into the variable at startup
56 */
 7+class SpamRegexHooks {
68
7 -global $wgHooks;
8 -/* initialize hook, FilterEdit is too far in code */
9 -$wgHooks['EditFilter'][] = 'wfGetSummarySpamRegex';
10 -$wgHooks['SpecialMovepageBeforeMove'][] = 'wfGetMoveSpamRegex';
 9+ public static function onEditFilter( $editpage ) {
 10+ global $wgOut, $wgTitle;
 11+ wfProfileIn( __METHOD__ );
1112
12 -function wfGetSummarySpamRegex($editpage) {
13 - wfProfileIn( __METHOD__ );
14 - wfLoadExtensionMessages('SpamRegex');
15 - global $wgOut, $wgTitle;
 13+ // here we get only the phrases for blocking in summaries...
 14+ $s_phrases = array();
 15+ $s_phrases = self::fetchRegexData( SPAMREGEX_SUMMARY );
 16+ if ( $s_phrases && ( $editpage->summary != '' ) ) {
 17+ // ...so let's rock with our custom spamPage to indicate that
 18+ // (since some phrases can be safely in the text and not in a summary
 19+ // and we do not want to confuse the good users, right?)
1620
17 - // here we get only the phrases for blocking in summaries...
18 - $s_phrases = array();
19 - $s_phrases = wfFetchSpamRegexData(SPAMREGEX_SUMMARY);
20 - if ( $s_phrases && ($editpage->summary != '') ) {
21 - // ...so let's rock with our custom spamPage to indicate that
22 - // (since some phrases can be safely in the text and not in a summary
23 - // and we do not want to confuse the good users, right?)
24 -
25 - foreach ($s_phrases as $s_phrase) {
26 - if ( preg_match($s_phrase, $editpage->summary, $s_matches) ) {
27 - $wgOut->setPageTitle( wfMsg( 'spamprotectiontitle' ) );
28 - $wgOut->setRobotPolicy( 'noindex,nofollow' );
29 - $wgOut->setArticleRelated( false );
 21+ foreach( $s_phrases as $s_phrase ) {
 22+ if ( preg_match( $s_phrase, $editpage->summary, $s_matches ) ) {
 23+ wfLoadExtensionMessages( 'SpamRegex' );
 24+ $wgOut->setPageTitle( wfMsg( 'spamprotectiontitle' ) );
 25+ $wgOut->setRobotPolicy( 'noindex,nofollow' );
 26+ $wgOut->setArticleRelated( false );
3027
31 - $wgOut->addWikiMsg( 'spamprotectiontext' );
32 - $wgOut->addWikiMsg( 'spamprotectionmatch', "<nowiki>{$s_matches[0]}</nowiki>" );
33 - $wgOut->addWikiMsg('spamregex-summary');
 28+ $wgOut->addWikiMsg( 'spamprotectiontext' );
 29+ $wgOut->addWikiMsg( 'spamprotectionmatch', "<nowiki>{$s_matches[0]}</nowiki>" );
 30+ $wgOut->addWikiMsg( 'spamregex-summary' );
3431
35 - $wgOut->returnToMain( false, $wgTitle );
36 - wfProfileOut( __METHOD__ );
37 - return false;
 32+ $wgOut->returnToMain( false, $wgTitle );
 33+ wfProfileOut( __METHOD__ );
 34+ return false;
 35+ }
3836 }
3937 }
40 - }
4138
42 - $t_phrases = array();
43 - // and here we check for phrases within the text itself
44 - $t_phrases = wfFetchSpamRegexData(SPAMREGEX_TEXTBOX);
45 - if ($t_phrases && is_array($t_phrases)) {
46 - foreach ($t_phrases as $t_phrase) {
47 - if (preg_match ($t_phrase, $editpage->textbox1, $t_matches)) {
48 - $editpage->spamPage($t_matches[0]);
49 - wfProfileOut( __METHOD__ );
50 - return false;
 39+ $t_phrases = array();
 40+ // and here we check for phrases within the text itself
 41+ $t_phrases = self::fetchRegexData( SPAMREGEX_TEXTBOX );
 42+ if ( $t_phrases && is_array( $t_phrases ) ) {
 43+ foreach( $t_phrases as $t_phrase ) {
 44+ if ( preg_match( $t_phrase, $editpage->textbox1, $t_matches ) ) {
 45+ $editpage->spamPage( $t_matches[0] );
 46+ wfProfileOut( __METHOD__ );
 47+ return false;
 48+ }
5149 }
5250 }
53 - }
54 - wfProfileOut( __METHOD__ );
55 - return true;
56 -}
 51+ wfProfileOut( __METHOD__ );
 52+ return true;
 53+ }
5754
58 -// this is for page move
59 -function wfGetMoveSpamRegex($movepage) {
60 - wfProfileIn( __METHOD__ );
61 - wfLoadExtensionMessages('SpamRegex');
 55+ // this is for page move
 56+ public static function onAbortMove( $oldtitle, $newtitle, $user, &$error ) {
 57+ global $wgRequest;
 58+ wfProfileIn( __METHOD__ );
6259
63 - // here we get only the phrases for blocking in summaries...
64 - $s_phrases = array();
65 - $s_phrases = wfFetchSpamRegexData(SPAMREGEX_SUMMARY);
66 - if ( $s_phrases && ($movepage->reason != '') ) {
67 - foreach ($s_phrases as $s_phrase) {
68 - if (preg_match ($s_phrase, $movepage->reason, $s_matches)) {
69 - $spammessage = wfMsg('spamregex-move');
70 - $spammessage .= wfMsg('spamprotectionmatch', "<nowiki>{$s_matches[0]}</nowiki>");
71 - $movepage->showForm('hookaborted', $spammessage);
 60+ // here we get only the phrases for blocking in summaries...
 61+ $s_phrases = self::fetchRegexData( SPAMREGEX_SUMMARY );
 62+ $reason = $wgRequest->getText( 'wpReason' );
7263
73 - wfProfileOut( __METHOD__ );
74 - return false;
 64+ if ( $s_phrases && $reason ) {
 65+ foreach( $s_phrases as $s_phrase ) {
 66+ if ( preg_match( $s_phrase, $reason, $s_matches ) ) {
 67+ wfLoadExtensionMessages( 'SpamRegex' );
 68+ $error .= wfMsg( 'spamregex-move' );
 69+ $error .= wfMsg( 'spamprotectionmatch', "<nowiki>{$s_matches[0]}</nowiki>" );
 70+ wfProfileOut( __METHOD__ );
 71+ return false;
 72+ }
7573 }
7674 }
 75+ wfProfileOut( __METHOD__ );
 76+ return true;
7777 }
78 - wfProfileOut( __METHOD__ );
79 - return true;
80 -}
8178
82 -function wfFetchSpamRegexData($mode) {
83 - wfProfileIn( __METHOD__ );
84 - global $wgMemc, $wgUser, $wgSpamRegex, $wgSharedDB;
 79+ protected static function fetchRegexData( $mode ) {
 80+ global $wgMemc;
 81+ wfProfileIn( __METHOD__ );
8582
86 - $phrases = array();
87 - $first = true;
88 -
89 - /* first, check if regex string is already stored in memcache */
90 - ( $mode == SPAMREGEX_SUMMARY ) ? $key_clause = ":Summary" : $key_clause = ":Textbox";
91 - $key = wfSpamRegexGetMemcDB() . ":spamRegexCore:spamRegex" . $key_clause;
92 - $cached = $wgMemc->get($key);
93 - if ( !$cached ) {
94 - /* fetch data from db, concatenate into one string, then fill cache */
95 - ( $mode == SPAMREGEX_SUMMARY ) ? $clause = " WHERE spam_summary = 1" : $clause = " WHERE spam_textbox = 1";
96 - $dbr = wfGetDB( DB_SLAVE );
97 - $query = "SELECT spam_text FROM " . wfSpamRegexGetTable() . $clause;
98 - $res = $dbr->query($query);
99 - while ( $row = $dbr->fetchObject( $res ) ) {
100 - $concat = $row->spam_text ;
101 - $phrases [] = "/" . $concat . "/i" ;
 83+ $phrases = array();
 84+ /* first, check if regex string is already stored in memcache */
 85+ $key_clause = $mode == SPAMREGEX_SUMMARY ? 'Summary' : 'Textbox';
 86+ $key = wfSpamRegexCacheKey( 'spamRegexCore', 'spamRegex', $key_clause );
 87+ $cached = $wgMemc->get( $key );
 88+ if ( !$cached ) {
 89+ /* fetch data from db, concatenate into one string, then fill cache */
 90+ $field = $mode == SPAMREGEX_SUMMARY ? 'spam_summary' : 'spam_textbox';
 91+ $dbr = wfGetDB( DB_SLAVE );
 92+ $res = $dbr->select( 'spam_regex', 'spam_text', array( $field => 1 ), __METHOD__ );
 93+ while ( $row = $res->fetchObject() ) {
 94+ $concat = $row->spam_text;
 95+ $phrases [] = "/" . $concat . "/i" ;
 96+ }
 97+ $wgMemc->set( $key, $phrases, 0 );
 98+ $res->free();
 99+ } else {
 100+ /* take from cache */
 101+ $phrases = $cached;
102102 }
103 - $wgMemc->set($key, $phrases, 0);
104 - $dbr->freeResult($res);
105 - } else {
106 - /* take from cache */
107 - $phrases = $cached;
 103+ wfProfileOut( __METHOD__ );
 104+ return $phrases;
108105 }
109 - wfProfileOut( __METHOD__ );
110 - return $phrases;
111106 }
\ No newline at end of file