Index: trunk/extensions/AkismetKlik/AkismetKlik.php |
— | — | @@ -3,9 +3,7 @@ |
4 | 4 | exit; |
5 | 5 | } |
6 | 6 | |
7 | | -# |
8 | 7 | # Include PHP5 Akismet class from http://www.achingbrain.net/stuff/akismet (GPL) |
9 | | -# |
10 | 8 | require_once('Akismet.class.php'); |
11 | 9 | |
12 | 10 | #Extension credits |
— | — | @@ -13,42 +11,30 @@ |
14 | 12 | 'name' => 'AkismetKlik', |
15 | 13 | 'author' => 'Carl Austin Bennett', |
16 | 14 | 'url' => 'http://www.mediawiki.org/wiki/Extension:AkismetKlik', |
17 | | - 'description' => 'Rejects edits from suspected comment spammers on Akismet\'s blacklist.', |
| 15 | + 'description' => "Rejects edits from suspected comment spammers on Akismet's blacklist.", |
18 | 16 | ); |
19 | 17 | |
20 | 18 | # Set site-specific configuration values |
21 | | -#$wgAKkey='867-5309'; |
22 | | -#$siteURL='http://wiki.example.org'; |
| 19 | +$wgAKkey = ''; |
| 20 | +$wgAKSiteUrl = ''; |
23 | 21 | |
24 | 22 | # |
25 | 23 | # MediaWiki hooks |
26 | 24 | # |
27 | 25 | # Loader for spam blacklist feature |
28 | 26 | # Include this from LocalSettings.php |
| 27 | +$wgHooks['EditFilterMerged'][] = 'wfAkismetFilterMerged'; |
29 | 28 | |
30 | | -global $wgAkismetFilterCallback, $wgPreAkismetFilterCallback, $wgUser; |
31 | | -$wgPreAkismetFilterCallback = false; |
32 | | - |
33 | | -if ( defined( 'MW_SUPPORTS_EDITFILTERMERGED' ) ) { |
34 | | - $wgHooks['EditFilterMerged'][] = 'wfAkismetFilterMerged'; |
35 | | -} else { |
36 | | - if ( $wgFilterCallback ) { |
37 | | - $wgPreAkismetFilterCallback = $wgFilterCallback; |
38 | | - } |
39 | | - $wgFilterCallback = 'wfAkismetFilter'; |
40 | | -} |
41 | | - |
42 | | -#$wgHooks['EditFilter'][] = 'wfAkismetFilter'; |
43 | | - |
44 | 29 | /** |
45 | 30 | * Get an instance of AkismetKlik and do some first-call initialisation. |
46 | 31 | * All actual functionality is implemented in that object |
| 32 | + * @return AkismetKlik |
47 | 33 | */ |
48 | 34 | function wfAkismetKlikObject() { |
49 | 35 | global $wgSpamBlacklistSettings, $wgPreSpamFilterCallback; |
50 | 36 | static $spamObj; |
51 | 37 | if ( !$spamObj ) { |
52 | | - $spamObj = new AkismetKlik ( $wgSpamBlacklistSettings ); |
| 38 | + $spamObj = new AkismetKlik( $wgSpamBlacklistSettings ); |
53 | 39 | $spamObj->previousFilter = $wgPreSpamFilterCallback; |
54 | 40 | } |
55 | 41 | return $spamObj; |
— | — | @@ -56,6 +42,10 @@ |
57 | 43 | |
58 | 44 | /** |
59 | 45 | * Hook function for $wgFilterCallback |
| 46 | + * @param $title Title |
| 47 | + * @param $text string |
| 48 | + * @param $section |
| 49 | + * @return bool |
60 | 50 | */ |
61 | 51 | function wfAkismetFilter( &$title, $text, $section ) { |
62 | 52 | $spamObj = wfAkismetKlikObject(); |
— | — | @@ -64,11 +54,13 @@ |
65 | 55 | |
66 | 56 | /** |
67 | 57 | * Hook function for EditFilterMerged, replaces wfAkismetFilter |
| 58 | + * @param $editPage EditPage |
| 59 | + * @param $text string |
| 60 | + * @return bool |
68 | 61 | */ |
69 | 62 | function wfAkismetFilterMerged( $editPage, $text ) { |
70 | 63 | $spamObj = new AkismetKlik(); |
71 | | - $ret = $spamObj->filter( $editPage->mArticle->getTitle(), $text, '', $editPage ); |
72 | | - // Return convention for hooks is the inverse of $wgAkismetFilterCallback |
| 64 | + $ret = $spamObj->filter( $editPage->getArticle()->getTitle(), $text, '', $editPage ); |
73 | 65 | return !$ret; |
74 | 66 | } |
75 | 67 | |
— | — | @@ -77,10 +69,12 @@ |
78 | 70 | # |
79 | 71 | class AkismetKlik { |
80 | 72 | |
81 | | - function AkismetKlik( $settings = array() ) { |
| 73 | + /** |
| 74 | + * @param $settings array |
| 75 | + */ |
| 76 | + function __construct( $settings = array() ) { |
82 | 77 | foreach ( $settings as $name => $value ) { |
83 | 78 | $this->$name = $value; |
84 | | - echo $value; |
85 | 79 | } |
86 | 80 | } |
87 | 81 | |
— | — | @@ -88,34 +82,28 @@ |
89 | 83 | * @param Title $title |
90 | 84 | * @param string $text Text of section, or entire text if $editPage!=false |
91 | 85 | * @param string $section Section number or name |
92 | | - * @param EditPage $editPage EditPage if EditFilterMerged was called, false otherwise |
93 | | - * @return True if the edit should not be allowed, false otherwise |
| 86 | + * @param EditPage|bool $editPage EditPage if EditFilterMerged was called, false otherwise |
| 87 | + * @throws MWException |
| 88 | + * @return bool True if the edit should not be allowed, false otherwise |
94 | 89 | * If the return value is true, an error will have been sent to $wgOut |
95 | 90 | */ |
96 | 91 | function filter( &$title, $text, $section, $editPage = false ) { |
97 | | - global $wgArticle, $wgVersion, $wgOut, $wgParser, $wgUser; |
98 | | - global $siteURL, $wgAKkey; |
| 92 | + global $wgParser, $wgUser, $wgAKSiteUrl, $wgAKkey, $IP; |
99 | 93 | |
100 | | - $fname = 'wfAkismetKlikFilter'; |
101 | | - wfProfileIn( $fname ); |
102 | | - |
103 | | - # Call the rest of the hook chain first |
104 | | - if ( $this->previousFilter ) { |
105 | | - $f = $this->previousFilter; |
106 | | - if ( $f( $title, $text, $section ) ) { |
107 | | - wfProfileOut( $fname ); |
108 | | - return true; |
109 | | - } |
| 94 | + if ( strlen( $wgAKkey ) == 0 ) { |
| 95 | + throw new MWException( "Set $wgAKkey" ); |
110 | 96 | } |
| 97 | + if ( strlen( $wgAKkey ) == 0 ) { |
| 98 | + throw new MWException( "Set $wgAKkey" ); |
| 99 | + } |
111 | 100 | |
112 | | - $this->title = $title; |
113 | | - $this->text = $text; |
114 | | - $this->section = $section; |
| 101 | + wfProfileIn( __METHOD__ ); |
| 102 | + |
115 | 103 | $text = str_replace( '.', '.', $text ); |
116 | 104 | |
117 | 105 | # Run parser to strip SGML comments and such out of the markup |
118 | 106 | if ( $editPage ) { |
119 | | - $editInfo = $editPage->mArticle->prepareTextForEdit( $text ); |
| 107 | + $editInfo = $editPage->getArticle()->prepareTextForEdit( $text ); |
120 | 108 | $out = $editInfo->output; |
121 | 109 | $pgtitle = $title; |
122 | 110 | } else { |
— | — | @@ -124,28 +112,28 @@ |
125 | 113 | $out = $wgParser->parse( $text, $title, $options ); |
126 | 114 | $pgtitle = ""; |
127 | 115 | } |
128 | | - $links = implode( "\n", array_keys( $out->getExternalLinks())); |
| 116 | + $links = implode( "\n", array_keys( $out->getExternalLinks() ) ); |
129 | 117 | |
130 | 118 | # Do the match |
131 | | - if ($wgUser->mName == "") $user = $IP; |
132 | | - else $user = $wgUser->mName; |
133 | | - $akismet = new Akismet($siteURL, $wgAKkey); |
134 | | - $akismet->setCommentAuthor($user); |
135 | | - $akismet->setCommentAuthorEmail($wgUser->mEmail); |
136 | | - $akismet->setCommentAuthorURL($links); |
137 | | - $akismet->setCommentContent($text); |
138 | | - $akismet->setCommentType("wiki"); |
139 | | - $akismet->setPermalink($siteURL . '/wiki/' . $pgtitle); |
140 | | - if($akismet->isCommentSpam()&&!$wgUser->isAllowed( 'bypassakismet' )) |
141 | | - { |
| 119 | + if ( $wgUser->mName == "" ) { |
| 120 | + $user = $IP; |
| 121 | + } else { |
| 122 | + $user = $wgUser->mName; |
| 123 | + } |
| 124 | + $akismet = new Akismet( $wgAKSiteUrl, $wgAKkey ); |
| 125 | + $akismet->setCommentAuthor( $user ); |
| 126 | + $akismet->setCommentAuthorEmail( $wgUser->mEmail ); |
| 127 | + $akismet->setCommentAuthorURL( $links ); |
| 128 | + $akismet->setCommentContent( $text ); |
| 129 | + $akismet->setCommentType( "wiki" ); |
| 130 | + $akismet->setPermalink( $wgAKSiteUrl . '/wiki/' . $pgtitle ); |
| 131 | + if( $akismet->isCommentSpam() && !$wgUser->isAllowed( 'bypassakismet' ) ) { |
142 | 132 | wfDebugLog( 'AkismetKlik', "Match!\n" ); |
143 | | - if ( $editPage ) { |
144 | | - $editPage->spamPage( "http://akismet.com blacklist error" ); |
145 | | - } else { |
146 | | - EditPage::spamPage( "http://akismet.com blacklist error" ); |
147 | | - } |
| 133 | + EditPage::spamPage( "http://akismet.com blacklist error" ); |
| 134 | + wfProfileOut( __METHOD__ ); |
148 | 135 | return true; |
149 | 136 | } |
| 137 | + wfProfileOut( __METHOD__ ); |
150 | 138 | return false; |
151 | 139 | } |
152 | 140 | } |