Index: trunk/extensions/SpamRegex/SpamRegex.php |
— | — | @@ -21,23 +21,29 @@ |
22 | 22 | define('SPAMREGEX_TEXTBOX', 0); |
23 | 23 | define('SPAMREGEX_SUMMARY', 1); |
24 | 24 | |
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 | +); |
34 | 34 | |
| 35 | +//New user right |
| 36 | +$wgAvailableRights[] = 'spamregex'; |
| 37 | +$wgGroupPermissions['staff']['spamregex'] = true; |
| 38 | + |
35 | 39 | /* 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 ); |
40 | 46 | } else { |
41 | | - return $wgDBname; |
| 47 | + return call_user_func_array( 'wfMemcKey', $args ); |
42 | 48 | } |
43 | 49 | } |
44 | 50 | |
— | — | @@ -45,22 +51,11 @@ |
46 | 52 | $wgExtensionMessagesFiles['SpamRegex'] = $dir . 'SpamRegex.i18n.php'; |
47 | 53 | $wgExtensionAliasesFiles['SpamRegex'] = $dir . 'SpamRegex.alias.php'; |
48 | 54 | $wgAutoloadClasses['SpamRegex'] = $dir . 'SpecialSpamRegex.php'; |
| 55 | +$wgAutoloadClasses['SpamRegexHooks'] = $dir . 'SpamRegexCore.php'; |
49 | 56 | $wgSpecialPages['SpamRegex'] = 'SpamRegex'; |
50 | 57 | $wgSpecialPageGroups['SpamRegex'] = 'pagetools'; |
51 | 58 | |
52 | | -//New user right |
53 | | -$wgAvailableRights[] = 'spamregex'; |
54 | | -$wgGroupPermissions['staff']['spamregex'] = true; |
| 59 | +$wgHooks['EditFilter'][] = 'SpamRegexHooks::onEditFilter'; |
| 60 | +$wgHooks['AbortMove'][] = 'SpamRegexHooks::onAbortMove'; |
55 | 61 | |
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"); |
67 | 62 | require_once("$IP/extensions/SimplifiedRegex/SimplifiedRegex.php"); |
\ No newline at end of file |
Index: trunk/extensions/SpamRegex/SpecialSpamRegex.php |
— | — | @@ -13,7 +13,7 @@ |
14 | 14 | */ |
15 | 15 | function __construct() { |
16 | 16 | parent::__construct( 'SpamRegex', 'spamregex', true, 'execute', false ); |
17 | | - wfLoadExtensionMessages('SpamRegex'); |
| 17 | + wfLoadExtensionMessages( 'SpamRegex' ); |
18 | 18 | } |
19 | 19 | |
20 | 20 | /** |
— | — | @@ -27,9 +27,9 @@ |
28 | 28 | if ( $wgUser->isBlocked() ) { |
29 | 29 | $wgOut->blockedPage(); |
30 | 30 | return; |
31 | | - } |
| 31 | + } |
32 | 32 | if ( wfReadOnly() ) { |
33 | | - $wgOut->readOnlyPage(); |
| 33 | + $wgOut->readOnlyPage(); |
34 | 34 | return; |
35 | 35 | } |
36 | 36 | if ( !$wgUser->isAllowed( 'spamregex' ) ) { |
— | — | @@ -37,9 +37,9 @@ |
38 | 38 | return; |
39 | 39 | } |
40 | 40 | |
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 ); |
44 | 44 | |
45 | 45 | $action = $wgRequest->getVal( 'action' ); |
46 | 46 | if ( 'success_block' == $action ) { |
— | — | @@ -49,17 +49,17 @@ |
50 | 50 | $sRL->showSuccess(); |
51 | 51 | $sRF->showForm(''); |
52 | 52 | } 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 ) ); |
55 | 55 | } else if ( $wgRequest->wasPosted() && 'submit' == $action && |
56 | | - $wgUser->matchEditToken( $wgRequest->getVal('wpEditToken') ) ) { |
| 56 | + $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) ) { |
57 | 57 | $sRF->doSubmit(); |
58 | | - } else if ('delete' == $action) { |
| 58 | + } else if ( 'delete' == $action ) { |
59 | 59 | $sRL->deleteFromList(); |
60 | 60 | } else { |
61 | | - $sRF->showForm(''); |
| 61 | + $sRF->showForm( '' ); |
62 | 62 | } |
63 | | - $sRL->showList('', $offset ); |
| 63 | + $sRL->showList( '' ); |
64 | 64 | } |
65 | 65 | } |
66 | 66 | |
— | — | @@ -73,7 +73,7 @@ |
74 | 74 | } |
75 | 75 | |
76 | 76 | /* wrapper for GET values */ |
77 | | - function wfSpamRegexGetListBits() { |
| 77 | + public static function getListBits() { |
78 | 78 | global $wgRequest; |
79 | 79 | $pieces = array(); |
80 | 80 | list( $limit, $offset ) = $wgRequest->getLimitOffset(); |
— | — | @@ -84,11 +84,11 @@ |
85 | 85 | } |
86 | 86 | |
87 | 87 | /* 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' ) ); |
93 | 93 | } |
94 | 94 | |
95 | 95 | /** |
— | — | @@ -98,93 +98,88 @@ |
99 | 99 | */ |
100 | 100 | function showList( $err ) { |
101 | 101 | wfProfileIn( __METHOD__ ); |
102 | | - global $wgOut, $wgRequest, $wgMemc, $wgLang; |
| 102 | + global $wgOut, $wgRequest, $wgLang; |
103 | 103 | |
104 | 104 | /* on error, display error */ |
105 | 105 | if ( "" != $err ) { |
106 | | - $wgOut->addHTML("<p class='error'>{$err}</p>\n"); |
| 106 | + $wgOut->addHTML( "<p class='error'>{$err}</p>\n" ); |
107 | 107 | } |
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(); |
112 | 108 | |
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(); |
115 | 119 | |
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}\">" ); |
119 | 122 | |
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' ); |
138 | 130 | } |
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>" ); |
140 | 140 | } |
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 ); |
144 | 144 | } |
145 | | - $dbr->freeResult($res); |
146 | | - $wgOut->addHTML("</form>"); |
147 | | - $this->showPrevNext($wgOut); |
148 | 145 | wfProfileOut( __METHOD__ ); |
149 | 146 | } |
150 | 147 | |
151 | 148 | /* remove from list - without confirmation */ |
152 | 149 | function deleteFromList() { |
153 | 150 | 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' ) ); |
156 | 153 | /* delete */ |
157 | 154 | $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' ); |
161 | 157 | if ( $dbw->affectedRows() ) { |
162 | 158 | /* success */ |
163 | | - $this->wfSpamRegexUnsetKeys(); |
| 159 | + self::unsetKeys(); |
164 | 160 | 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() ) ); |
166 | 162 | } else { |
167 | 163 | 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() ) ); |
169 | 165 | } |
170 | 166 | } |
171 | 167 | |
172 | | - /* fetch number of all rows */ |
| 168 | + /** |
| 169 | + * fetch number of all rows |
| 170 | + * Use memcached if possible |
| 171 | + */ |
173 | 172 | function fetchNumResults() { |
174 | 173 | wfProfileIn( __METHOD__ ); |
175 | | - global $wgMemc, $wgSharedDB; |
| 174 | + global $wgMemc; |
176 | 175 | |
177 | 176 | /* we use memcached here */ |
178 | | - $key = wfSpamRegexGetMemcDB() . ":spamRegexCore:numResults"; |
179 | | - $cached = $wgMemc->get($key); |
| 177 | + $key = wfSpamRegexCacheKey( 'spamRegexCore', 'numResults' ); |
| 178 | + $cached = $wgMemc->get( $key ); |
180 | 179 | $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 ); |
189 | 184 | } else { |
190 | 185 | $results = $cached; |
191 | 186 | } |
— | — | @@ -193,22 +188,23 @@ |
194 | 189 | return $results; |
195 | 190 | } |
196 | 191 | |
| 192 | + |
197 | 193 | /* draws one option for select */ |
198 | 194 | function makeOption($blocker, $current) { |
199 | 195 | global $wgOut; |
200 | 196 | if ($blocker == $current) { |
201 | | - $wgOut->addHTML("<option selected=\"selected\" value=\"{$blocker}\">{$blocker}</option>"); |
| 197 | + $wgOut->addHTML( "<option selected=\"selected\" value=\"{$blocker}\">{$blocker}</option>" ); |
202 | 198 | } else { |
203 | | - $wgOut->addHTML("<option value=\"{$blocker}\">{$blocker}</option>"); |
| 199 | + $wgOut->addHTML( "<option value=\"{$blocker}\">{$blocker}</option>" ); |
204 | 200 | } |
205 | 201 | } |
206 | 202 | |
207 | 203 | /* on success */ |
208 | 204 | function showSuccess() { |
209 | 205 | 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' ) ); |
213 | 209 | } |
214 | 210 | |
215 | 211 | /* init for showprevnext */ |
— | — | @@ -236,8 +232,8 @@ |
237 | 233 | function spamRegexForm( $par ) { |
238 | 234 | global $wgRequest; |
239 | 235 | $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; |
242 | 238 | } |
243 | 239 | |
244 | 240 | /* output */ |
— | — | @@ -245,17 +241,20 @@ |
246 | 242 | global $wgOut, $wgUser, $wgRequest; |
247 | 243 | |
248 | 244 | $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() ); |
251 | 247 | |
252 | 248 | if ( "" != $err ) { |
253 | 249 | $wgOut->setSubtitle( wfMsgHtml( 'formerror' ) ); |
254 | 250 | $wgOut->addHTML( "<p class='error'>{$err}</p>\n" ); |
255 | 251 | } |
256 | 252 | |
257 | | - $wgOut->addWikiMsg('spamregex-intro'); |
| 253 | + $wgOut->addWikiMsg( 'spamregex-intro' ); |
258 | 254 | |
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 = ''; |
260 | 259 | |
261 | 260 | $wgOut->addScript(" |
262 | 261 | <script type=\"text/javascript\"> |
— | — | @@ -283,10 +282,10 @@ |
284 | 283 | addOnloadHook (SpamRegexEnhanceControls); |
285 | 284 | </script>" |
286 | 285 | ); |
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' ) ); |
291 | 290 | $wgOut->addHTML(" |
292 | 291 | <form name=\"spamregex\" method=\"post\" action=\"{$action}\"> |
293 | 292 | <table border=\"0\"> |
— | — | @@ -324,8 +323,8 @@ |
325 | 324 | /* on success */ |
326 | 325 | function showSuccess() { |
327 | 326 | 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' ) ); |
330 | 329 | |
331 | 330 | $wgOut->addWikiMsg('spamregex-block-message', $this->mBlockedPhrase); |
332 | 331 | } |
— | — | @@ -337,13 +336,13 @@ |
338 | 337 | |
339 | 338 | /* empty name */ |
340 | 339 | if ( strlen($this->mBlockedPhrase) == 0 ) { |
341 | | - $this->showForm(wfMsgHtml('spamregex-warning-1')); |
| 340 | + $this->showForm( wfMsgHtml( 'spamregex-warning-1' ) ); |
342 | 341 | wfProfileOut( __METHOD__ ); |
343 | 342 | return; |
344 | 343 | } |
345 | 344 | /* 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' ) ); |
348 | 347 | wfProfileOut( __METHOD__ ); |
349 | 348 | return; |
350 | 349 | } |
— | — | @@ -354,33 +353,35 @@ |
355 | 354 | $timestamp = wfTimestampNow(); |
356 | 355 | |
357 | 356 | /* 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' ) ); |
360 | 359 | wfProfileOut( __METHOD__ ); |
361 | 360 | return; |
362 | 361 | } |
363 | 362 | |
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 | + ); |
374 | 375 | |
375 | 376 | /* 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 ) ); |
378 | 379 | wfProfileOut( __METHOD__ ); |
379 | 380 | return; |
380 | 381 | } |
381 | | - spamRegexList::wfSpamRegexUnsetKeys($name); |
| 382 | + spamRegexList::unsetKeys(); |
382 | 383 | /* redirect */ |
383 | | - $titleObj = Title::makeTitle( NS_SPECIAL, 'SpamRegex' ); |
| 384 | + $titleObj = SpecialPage::getTitleFor( 'SpamRegex' ); |
384 | 385 | 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() ) ); |
386 | 387 | } |
387 | 388 | } |
\ 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 |
1 | 14 | + native |
Index: trunk/extensions/SpamRegex/SpamRegexCore.php |
— | — | @@ -1,110 +1,105 @@ |
2 | 2 | <?php |
| 3 | + |
3 | 4 | /** |
4 | 5 | * Fetch phrases to block, and fill $wgSpamRegex with them, rather than scribble that into the variable at startup |
5 | 6 | */ |
| 7 | +class SpamRegexHooks { |
6 | 8 | |
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__ ); |
11 | 12 | |
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?) |
16 | 20 | |
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 ); |
30 | 27 | |
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' ); |
34 | 31 | |
35 | | - $wgOut->returnToMain( false, $wgTitle ); |
36 | | - wfProfileOut( __METHOD__ ); |
37 | | - return false; |
| 32 | + $wgOut->returnToMain( false, $wgTitle ); |
| 33 | + wfProfileOut( __METHOD__ ); |
| 34 | + return false; |
| 35 | + } |
38 | 36 | } |
39 | 37 | } |
40 | | - } |
41 | 38 | |
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 | + } |
51 | 49 | } |
52 | 50 | } |
53 | | - } |
54 | | - wfProfileOut( __METHOD__ ); |
55 | | - return true; |
56 | | -} |
| 51 | + wfProfileOut( __METHOD__ ); |
| 52 | + return true; |
| 53 | + } |
57 | 54 | |
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__ ); |
62 | 59 | |
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' ); |
72 | 63 | |
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 | + } |
75 | 73 | } |
76 | 74 | } |
| 75 | + wfProfileOut( __METHOD__ ); |
| 76 | + return true; |
77 | 77 | } |
78 | | - wfProfileOut( __METHOD__ ); |
79 | | - return true; |
80 | | -} |
81 | 78 | |
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__ ); |
85 | 82 | |
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; |
102 | 102 | } |
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; |
108 | 105 | } |
109 | | - wfProfileOut( __METHOD__ ); |
110 | | - return $phrases; |
111 | 106 | } |
\ No newline at end of file |