
bartek at svn
Aug 3, 2007, 5:08 AM
Views: 216
Permalink
|
|
SVN: [24564] trunk/extensions/SpamRegex
|
|
Revision: 24564 Author: bartek Date: 2007-08-03 12:08:50 +0000 (Fri, 03 Aug 2007) Log Message: ----------- SpamRegex code update Modified Paths: -------------- trunk/extensions/SpamRegex/README trunk/extensions/SpamRegex/SpamRegex.php trunk/extensions/SpamRegex/SpamRegexCore.php trunk/extensions/SpamRegex/SpecialSpamRegex.php Modified: trunk/extensions/SpamRegex/README =================================================================== --- trunk/extensions/SpamRegex/README 2007-08-03 12:05:05 UTC (rev 24563) +++ trunk/extensions/SpamRegex/README 2007-08-03 12:08:50 UTC (rev 24564) @@ -7,6 +7,8 @@ `spam_text` varchar(255) NOT NULL, `spam_timestamp` char(14) NOT NULL, `spam_user` varchar(255) NOT NULL, + `spam_textbox` int(1) NOT NULL DEFAULT 1, + `spam_summary` int(1) NOT NULL DEFAULT 0, PRIMARY KEY (`spam_id`), UNIQUE KEY `spam_text` (`spam_text`), KEY `spam_timestamp` (`spam_timestamp`), Modified: trunk/extensions/SpamRegex/SpamRegex.php =================================================================== --- trunk/extensions/SpamRegex/SpamRegex.php 2007-08-03 12:05:05 UTC (rev 24563) +++ trunk/extensions/SpamRegex/SpamRegex.php 2007-08-03 12:08:50 UTC (rev 24564) @@ -8,6 +8,10 @@ /* for memcached - expiration time */ define ('SPAMREGEX_EXPIRE', 0) ; +/* two modes for two kinds of blocks */ +define ('SPAMREGEX_TEXTBOX', 0) ; +define ('SPAMREGEX_SUMMARY', 1) ; + /* return the name of the table */ function wfSpamRegexGetTable() { global $wgSharedDB ; @@ -20,5 +24,5 @@ require_once ($IP.SPAMREGEX_PATH."extensions/SpamRegex/SpecialSpamRegex.php") ; //will need more, maybe Core? require_once ($IP.SPAMREGEX_PATH."extensions/SpamRegex/SpamRegexCore.php") ; - require_once ($IP.SPAMREGEX_PATH."extensions/SimplifiedRegex.php") ; - + require_once ($IP.SPAMREGEX_PATH."extensions/SimplifiedRegex/SimplifiedRegex.php") ; +?> Modified: trunk/extensions/SpamRegex/SpamRegexCore.php =================================================================== --- trunk/extensions/SpamRegex/SpamRegexCore.php 2007-08-03 12:05:05 UTC (rev 24563) +++ trunk/extensions/SpamRegex/SpamRegexCore.php 2007-08-03 12:08:50 UTC (rev 24564) @@ -6,23 +6,66 @@ global $wgHooks; /* initialize hook, FilterEdit is too far in code */ -$wgHooks['AlternateEdit'][] = 'wfGetSpamRegex'; +$wgHooks['AlternateEdit'][] = 'wfGetSpamRegex' ; +$wgHooks['EditFilter'][] = 'wfGetSummarySpamRegex' ; function wfGetSpamRegex () { - global $wgMemc, $wgUser, $wgSpamRegex, $wgSharedDB ; + global $wgSpamRegex; if (!wfSimplifiedRegexCheckSharedDB()) return true ; + /* get here only the phrases for blocking in textbox */ + $phrases = wfFetchSpamRegexData (SPAMREGEX_TEXTBOX) ; + ("" != $phrases) ? $wgSpamRegex = "/".$phrases."/i" : $wgSpamRegex = false ; + return true ; +} + +function wfGetSummarySpamRegex ($editpage) { + global $wgOut; + if (!wfSimplifiedRegexCheckSharedDB()) + return true ; + + $matches = array() ; + /* here we get only the phrases for blocking in summaries... */ + $phrases = wfFetchSpamRegexData (SPAMREGEX_SUMMARY) ; + ("" != $phrases) ? $m_phrases = "/".$phrases."/i" : $m_phrases = false ; + if ( $m_phrases && ($editpage->summary != '') && preg_match( $m_phrases, $editpage->summary, $matches ) ) { + /* ...so let's rock with our custom spamPage to indicate that + (since some phrases can be safely in the text and not in a summary + and we do not want to confuse the good users, right?) + */ + $wgOut->setPageTitle( wfMsg( 'spamprotectiontitle' ) ); + $wgOut->setRobotPolicy( 'noindex,nofollow' ); + $wgOut->setArticleRelated( false ); + + $wgOut->addWikiText( wfMsg( 'spamprotectiontext' ) ); + if ( $matches[0] ) { + $wgOut->addWikiText( wfMsg( 'spamprotectionmatch', "<nowiki>{$matches[0]}</nowiki>" ) ); + } + $wgOut->addWikiText ( wfMsg ('spamregex_summary') ) ; + + $wgOut->returnToMain( false ); + + return false ; + } + return true ; +} + +function wfFetchSpamRegexData ($mode) { + global $wgMemc, $wgUser, $wgSpamRegex, $wgSharedDB ; + $phrases = "" ; $first = true ; /* first, check if regex string is already stored in memcache */ - $key = "$wgSharedDB:spamRegexCore:spamRegex" ; + ( $mode == SPAMREGEX_SUMMARY ) ? $key_clause = ":Summary" : $key_clause = ":Textbox" ; + $key = "$wgSharedDB:spamRegexCore:spamRegex" . $key_clause ; $cached = $wgMemc->get ($key) ; if ( !$cached ) { /* fetch data from db, concatenate into one string, then fill cache */ + ( $mode == SPAMREGEX_SUMMARY ) ? $clause = " WHERE spam_summary = 1" : $clause = " WHERE spam_textbox = 1" ; $dbr =& wfGetDB( DB_SLAVE ) ; - $query = "SELECT spam_text FROM ".wfSpamRegexGetTable() ; + $query = "SELECT spam_text FROM " . wfSpamRegexGetTable() . $clause ; $res = $dbr->query ($query) ; while ( $row = $dbr->fetchObject( $res ) ) { $concat = $row->spam_text ; @@ -39,8 +82,7 @@ /* take from cache */ $phrases = $cached ; } - ("" != $phrases) ? $wgSpamRegex = "/".$phrases."/i" : $wgSpamRegex = false ; - return true ; + return $phrases ; } - +?> Modified: trunk/extensions/SpamRegex/SpecialSpamRegex.php =================================================================== --- trunk/extensions/SpamRegex/SpecialSpamRegex.php 2007-08-03 12:05:05 UTC (rev 24563) +++ trunk/extensions/SpamRegex/SpecialSpamRegex.php 2007-08-03 12:08:50 UTC (rev 24564) @@ -26,6 +26,7 @@ require_once($IP. '/includes/SpecialPage.php'); SpecialPage::addPage(new SpecialPage('Spamregex', 'spamregex', true, 'wfSpamRegexSpecial', false)); $wgMessageCache->addMessage('spamregex', 'spamRegex'); + $wgMessageCache->addMessage('spamregex_summary', 'The text was found in the article\'s summary.'); } /* wrapper for GET values */ @@ -70,7 +71,8 @@ /* useful for cleaning the memcached keys */ function wfSpamRegexUnsetKeys () { global $wgMemc, $wgSharedDB ; - $wgMemc->delete ("$wgSharedDB:spamRegexCore:spamRegex") ; + $wgMemc->delete ("$wgSharedDB:spamRegexCore:spamRegex:Textbox") ; + $wgMemc->delete ("$wgSharedDB:spamRegexCore:spamRegex:Summary") ; $wgMemc->delete ("$wgSharedDB:spamRegexCore:numResults") ; } @@ -115,8 +117,18 @@ while ( $row = $dbr->fetchObject( $res ) ) { $time = $wgLang->timeanddate( wfTimestamp( TS_MW, $row->spam_timestamp ), true ) ; $ublock_ip = urlencode ($row->spam_text) ; + $desc = "" ; + if ($row->spam_textbox == 1) { + $desc .= "(Text)" ; + } + if ($row->spam_summary == 1) { + if ($row->spam_textbox == 1) { + $desc .= " " ; + } + $desc .= "(Summary)" ; + } $wgOut->addHTML (" - <li><b>".htmlspecialchars($row->spam_text)."</b> (<a href=\"{$action_unblock}&text={$ublock_ip}\">remove</a>) added by {$row->spam_user} on {$time}</li> + <li><b>".htmlspecialchars($row->spam_text)."</b> $desc (<a href=\"{$action_unblock}&text={$ublock_ip}\">remove</a>) added by {$row->spam_user} on {$time}</li> ") ; } $dbr->freeResult ($res) ; @@ -201,11 +213,15 @@ /* the form for blocking names and addresses */ class spamRegexForm { var $mBlockedPhrase ; + var $mBlockedText ; + var $mBlockedSummary ; /* constructor */ function spamRegexForm ( $par ) { global $wgRequest ; $this->mBlockedPhrase = $wgRequest->getVal( 'wpBlockedPhrase', $wgRequest->getVal( 'text', $par ) ); + ($wgRequest->getVal ('wpBlockedTextbox') ) ? $this->mBlockedTextbox = 1 : $this->mBlockedTextbox = 0 ; + ($wgRequest->getVal ('wpBlockedSummary') ) ? $this->mBlockedSummary = 1 : $this->mBlockedSummary = 0 ; } /* output */ @@ -225,6 +241,33 @@ ( 'submit' == $wgRequest->getVal( 'action' )) ? $scBlockedPhrase = htmlspecialchars ($this->mBlockedPhrase) : $scBlockedPhrase = '' ; + $wgOut->addScript(" + <script type=\"text/javascript\"> + function SpamRegexEnhanceControls () { + var SRTextboxControl = document.getElementById ('wpBlockedTextbox') ; + var SRSummaryControl = document.getElementById ('wpBlockedSummary') ; + + SRTextboxControl.onclick = function () { + if (!SRTextboxControl.checked) { + if (!SRSummaryControl.checked) { + SRSummaryControl.checked = true ; + } + } + } + + SRSummaryControl.onclick = function () { + if (!SRSummaryControl.checked) { + if (!SRTextboxControl.checked) { + SRTextboxControl.checked = true ; + } + } + } + } + + addOnloadHook (SpamRegexEnhanceControls) ; + </script>" + ) ; + $wgOut->addHtml(" <form name=\"spamregex\" method=\"post\" action=\"{$action}\"> <table border=\"0\"> @@ -234,10 +277,24 @@ <input tabindex=\"1\" name=\"wpBlockedPhrase\" value=\"{$scBlockedPhrase}\" /> </td> </tr> + <tr> + <td align=\"right\"> </td> + <td align=\"left\"> + <input type=\"checkbox\" tabindex=\"2\" name=\"wpBlockedTextbox\" id=\"wpBlockedTextbox\" value=\"1\" checked=\"checked\" /> + <label for=\"wpBlockedTextbox\">block phrase in article text</label> + </td> + </tr> + <tr> + <td align=\"right\"> </td> + <td align=\"left\"> + <input type=\"checkbox\" tabindex=\"3\" name=\"wpBlockedSummary\" id=\"wpBlockedSummary\" value=\"1\" /> + <label for=\"wpBlockedSummary\">block phrase in summary</label> + </td> + </tr> <tr> <td align=\"right\"> </td> <td align=\"left\"> - <input tabindex=\"2\" name=\"wpSpamRegexBlockedSubmit\" type=\"submit\" value=\"Block this phrase\" /> + <input tabindex=\"4\" name=\"wpSpamRegexBlockedSubmit\" type=\"submit\" value=\"Block this phrase\" /> </td> </tr> </table> @@ -273,12 +330,21 @@ $dbw =& wfGetDB( DB_MASTER ); $name = $wgUser->getName () ; $timestamp = wfTimestampNow() ; + + /* we need at least one block mode specified... we can have them both, of course */ + if ( ($this->mBlockedTextbox == 0) && ($this->mBlockedSummary == 0) ) { + $this->showForm ("Please check at least one blocking mode.") ; + return ; + } + $query = "INSERT IGNORE INTO ".wfSpamRegexGetTable() - ." (spam_id, spam_text, spam_timestamp, spam_user) + ." (spam_id, spam_text, spam_timestamp, spam_user, spam_textbox, spam_summary) VALUES (null, {$dbw->addQuotes($this->mBlockedPhrase)}, {$timestamp}, - {$dbw->addQuotes($name)} + {$dbw->addQuotes($name)}, + {$this->mBlockedTextbox}, + {$this->mBlockedSummary} )" ; $dbw->query ($query) ; @@ -294,4 +360,4 @@ } } - +?> _______________________________________________ MediaWiki-CVS mailing list MediaWiki-CVS[at]lists.wikimedia.org http://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs
|