Index: trunk/extensions/ArticleComments/ArticleComments.php |
— | — | @@ -1,10 +1,10 @@ |
2 | 2 | <?php |
3 | | -/* |
| 3 | +/** |
4 | 4 | * ArticleComments.php - A MediaWiki extension for adding comment sections to articles. |
5 | 5 | * @author Jim R. Wilson |
6 | 6 | * @author Platonides |
7 | 7 | * @version 0.6 |
8 | | - * @copyright Copyright (C) 2007 Jim R. Wilson |
| 8 | + * @copyright Copyright © 2007 Jim R. Wilson |
9 | 9 | * @license The MIT License - http://www.opensource.org/licenses/mit-license.php |
10 | 10 | * ----------------------------------------------------------------------- |
11 | 11 | * Description: |
— | — | @@ -52,7 +52,7 @@ |
53 | 53 | * version 0.1: |
54 | 54 | * Initial release. |
55 | 55 | * ----------------------------------------------------------------------- |
56 | | - * Copyright (c) 2007 Jim R. Wilson |
| 56 | + * Copyright © 2007 Jim R. Wilson |
57 | 57 | * |
58 | 58 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
59 | 59 | * of this software and associated documentation files (the "Software"), to deal |
— | — | @@ -76,23 +76,24 @@ |
77 | 77 | */ |
78 | 78 | |
79 | 79 | # Confirm MW environment |
80 | | -if ( !defined( 'MEDIAWIKI' ) ) die(); |
| 80 | +if ( !defined( 'MEDIAWIKI' ) ) { |
| 81 | + die(); |
| 82 | +} |
81 | 83 | |
82 | 84 | # Credits |
83 | 85 | $wgExtensionCredits['other'][] = array( |
84 | 86 | 'path' => __FILE__, |
85 | 87 | 'name' => 'ArticleComments', |
86 | | - 'author' => 'Jim R. Wilson - wilson.jim.r <at> gmail.com', |
87 | | - 'url' => 'http://jimbojw.com/wiki/index.php?title=ArticleComments', |
| 88 | + 'author' => array( 'Jim R. Wilson', 'Platonides' ), |
| 89 | + 'version' => '0.6', |
| 90 | + 'url' => 'http://www.mediawiki.org/wiki/Extension:ArticleComments', |
88 | 91 | 'descriptionmsg' => 'article-comments-desc', |
89 | | - 'version' => '0.6' |
90 | 92 | ); |
91 | 93 | |
92 | | -# Add Extension Functions |
93 | | -$wgExtensionMessagesFiles['ArticleComments'] = dirname( __FILE__ ) . "/ArticleComments.i18n.php"; |
| 94 | +# Add extension internationalization messages |
| 95 | +$wgExtensionMessagesFiles['ArticleComments'] = dirname( __FILE__ ) . '/ArticleComments.i18n.php'; |
94 | 96 | |
95 | | - |
96 | | -# Attach Hooks |
| 97 | +# Attach hooks |
97 | 98 | $wgHooks['ParserFirstCallInit'][] = 'wfArticleCommentsParserSetup'; |
98 | 99 | $wgHooks['SkinAfterContent'][] = 'wfArticleCommentsAfterContent'; |
99 | 100 | $wgHooks['ArticleCommentsSpamCheck'][] = 'defaultArticleCommentSpamCheck'; |
— | — | @@ -106,7 +107,7 @@ |
107 | 108 | 'noscript' => false, # Set to true to not include any ArticleComments related JavaScript |
108 | 109 | 'hideform' => true, # Should the comment field be hidden by default? |
109 | 110 | 'defaultMode' => 'normal', # Which mode should be preselected for comments? Values are: plain, normal and wiki |
110 | | - ); |
| 111 | +); |
111 | 112 | |
112 | 113 | /** |
113 | 114 | * List of namespaces on which a comment field is automatically added. |
— | — | @@ -137,81 +138,93 @@ |
138 | 139 | function wfArticleCommentsParserHookComment( $text, $args, $parser, $frame ) { |
139 | 140 | global $wgArticleCommentDefaults, $wgParser, $wgParserConf; |
140 | 141 | |
141 | | - if ( $parser === $wgParser ) # Workaround bug 25506 |
| 142 | + if ( $parser === $wgParser ) { # Workaround bug 25506 |
142 | 143 | $wgParser = new StubObject( 'wgParser', $wgParserConf['class'], array( $wgParserConf ) ); |
143 | | - |
144 | | - if ( !isset( $args['name'] ) ) |
145 | | - $args['name'] = wfMsgExt( 'article-comments-comment-missing-name-parameter', array( 'language' => $parser->getFunctionLang() ) ) ; |
146 | | - |
147 | | - if ( !isset( $args['url'] ) ) |
| 144 | + } |
| 145 | + |
| 146 | + if ( !isset( $args['name'] ) ) { |
| 147 | + $args['name'] = wfMsgExt( 'article-comments-comment-missing-name-parameter', array( 'language' => $parser->getFunctionLang() ) ); |
| 148 | + } |
| 149 | + |
| 150 | + if ( !isset( $args['url'] ) ) { |
148 | 151 | $args['url'] = ''; # This one can be empty |
149 | | - |
150 | | - if ( !isset( $args['date'] ) ) |
151 | | - $args['date'] = wfMsgExt( 'article-comments-comment-missing-date-parameter', array( 'language' => $parser->getFunctionLang() ) ) ; |
152 | | - else |
| 152 | + } |
| 153 | + |
| 154 | + if ( !isset( $args['date'] ) ) { |
| 155 | + $args['date'] = wfMsgExt( 'article-comments-comment-missing-date-parameter', array( 'language' => $parser->getFunctionLang() ) ); |
| 156 | + } else { |
153 | 157 | $args['date'] = $parser->getFunctionLang()->date( wfTimestamp( TS_MW, $args['date'] ) ); |
154 | | - |
155 | | - if ( !isset( $args['signature'] ) ) |
| 158 | + } |
| 159 | + |
| 160 | + if ( !isset( $args['signature'] ) ) { |
156 | 161 | $args['signature'] = $args['url'] == '' ? $args['name'] : $parser->getOptions()->getSkin( $parser->getTitle() )->makeExternalLink( $args['url'], $args['name'] ); |
157 | | - else // The signature is wikitext, so it may need parsing |
| 162 | + } else { // The signature is wikitext, so it may need parsing |
158 | 163 | $args['signature'] = $parser->recursiveTagParse( $args['signature'], $frame ); |
159 | | - |
160 | | - if ( !isset( $args['mode'] ) ) |
| 164 | + } |
| 165 | + |
| 166 | + if ( !isset( $args['mode'] ) ) { |
161 | 167 | $args['mode'] = $wgArticleCommentDefaults['defaultMode']; |
| 168 | + } |
162 | 169 | |
163 | | - $args['mode'] = strtolower( $args['mode'] ); |
| 170 | + $args['mode'] = strtolower( $args['mode'] ); |
164 | 171 | if ( $args['mode'] == 'plain' ) { |
165 | 172 | // Don't perform any formatting |
166 | | - |
167 | 173 | $text = htmlspecialchars( $text ); |
168 | | - |
| 174 | + |
169 | 175 | // But make new line generate new paragraphs |
170 | 176 | $text = str_replace( "\n", "</p><p>", $text ); |
171 | | - |
172 | | - return "<p>$text</p>"; |
| 177 | + |
| 178 | + return "<p>$text</p>"; |
173 | 179 | } elseif ( $args['mode'] == 'normal' ) { |
174 | 180 | // Convert some wikitext oddities to wiki markup |
175 | | - |
| 181 | + |
176 | 182 | # Need only a newline for new paragraph |
177 | 183 | $text = str_replace( "\n", "\n\n", $text ); |
178 | | - |
| 184 | + |
179 | 185 | # Disable <pre> on space // TODO: Enable space indenting. |
180 | 186 | $text = str_replace( "\n ", "\n ", $text ); |
181 | | - |
| 187 | + |
182 | 188 | } elseif ( $args['mode'] == 'wiki' ) { |
183 | 189 | /* Full wikitext */ |
184 | | - |
| 190 | + |
185 | 191 | } else { |
186 | 192 | return Html::rawElement( 'div', array( 'class' => 'error' ), |
187 | 193 | wfMsgExt( 'article-comments-comment-bad-mode', array( 'parsemag', 'language' => $parser->getFunctionLang() ) ) |
188 | 194 | ); |
189 | 195 | } |
190 | | - |
| 196 | + |
191 | 197 | # Parse the content, this is later kept as-is since we do a replaceafter there. |
192 | 198 | $text = $parser->recursiveTagParse( $text, $frame ); |
193 | | - |
194 | | - return wfMsgExt( 'article-comments-comment-contents', array( 'parse', 'replaceafter', 'content' ), |
195 | | - $args['name'], $args['url'], $args['signature'], $args['date'], $text ); |
| 199 | + |
| 200 | + return wfMsgExt( |
| 201 | + 'article-comments-comment-contents', |
| 202 | + array( 'parse', 'replaceafter', 'content' ), |
| 203 | + $args['name'], $args['url'], $args['signature'], $args['date'], $text |
| 204 | + ); |
196 | 205 | } |
197 | 206 | |
198 | 207 | /** |
199 | 208 | * Echos out a comment form depending on the page action and namespace. |
200 | | - * @param Title $title The title of the article on which the form will appear. |
201 | | - * @param Array $params A hash of parameters containing rendering options. |
| 209 | + * |
| 210 | + * @param $data |
| 211 | + * @param $skin Object: Skin object |
202 | 212 | */ |
203 | 213 | function wfArticleCommentsAfterContent( $data, $skin ) { |
204 | | - |
205 | 214 | global $wgRequest, $wgArticleCommentsNSDisplayList; |
206 | 215 | |
207 | | - # Short circuit for anything other than action=view or action=purge |
| 216 | + # Short-circuit for anything other than action=view or action=purge |
208 | 217 | if ( $wgRequest->getVal( 'action' ) && |
209 | 218 | $wgRequest->getVal( 'action' ) != 'view' && |
210 | 219 | $wgRequest->getVal( 'action' ) != 'purge' |
211 | | - ) return true; |
| 220 | + ) |
| 221 | + { |
| 222 | + return true; |
| 223 | + } |
212 | 224 | |
213 | 225 | # Short-circuit if displaylist is undefined, empty or null |
214 | | - if ( $wgArticleCommentsNSDisplayList == null ) |
| 226 | + if ( $wgArticleCommentsNSDisplayList == null ) { |
215 | 227 | return true; |
| 228 | + } |
216 | 229 | |
217 | 230 | $title = $skin->getTitle(); |
218 | 231 | if ( !$title->exists() ) { |
— | — | @@ -220,7 +233,9 @@ |
221 | 234 | |
222 | 235 | # Ensure that the namespace list is an actual list |
223 | 236 | $nsList = $wgArticleCommentsNSDisplayList; |
224 | | - if ( !is_array( $nsList ) ) $nsList = array( $nsList ); |
| 237 | + if ( !is_array( $nsList ) ) { |
| 238 | + $nsList = array( $nsList ); |
| 239 | + } |
225 | 240 | |
226 | 241 | # Display the form |
227 | 242 | if ( in_array( $title->getNamespace(), $nsList ) ) { |
— | — | @@ -231,9 +246,9 @@ |
232 | 247 | } |
233 | 248 | |
234 | 249 | /** |
235 | | - * Generates and returns an ArticleComment form html. |
236 | | - * @param Title $title The title of the article on which the form will appear. |
237 | | - * @param Array $params A hash of parameters containing rendering options. |
| 250 | + * Generates and returns an ArticleComment form HTML. |
| 251 | + * @param $title Object: the title of the article on which the form will appear. |
| 252 | + * @param $params Array: a hash of parameters containing rendering options. |
238 | 253 | */ |
239 | 254 | function wfArticleCommentForm( $title, $params = array() ) { |
240 | 255 | global $wgArticleCommentDefaults, $wgOut; |
— | — | @@ -252,17 +267,27 @@ |
253 | 268 | $content .= '<p>'; |
254 | 269 | $content .= Html::hidden( 'commentArticle', $title->getPrefixedDBkey() ); |
255 | 270 | |
256 | | - $content .= '<label for="commenterName">' . wfMsgExt( 'article-comments-name-field', array( 'parseinline', 'content' ) ) . Html::element( 'br' ) . '</label>'; |
| 271 | + $content .= '<label for="commenterName">' . |
| 272 | + wfMsgExt( |
| 273 | + 'article-comments-name-field', |
| 274 | + array( 'parseinline', 'content' ) |
| 275 | + ) . Html::element( 'br' ) . '</label>'; |
257 | 276 | $content .= Html::input( 'commenterName', '', 'text', array( 'id' => 'commenterName' ) ); |
258 | 277 | $content .= '</p>'; |
259 | 278 | |
260 | 279 | if ( $params['showurlfield'] ) { |
261 | | - $content .= '<p><label for="commenterURL">' . wfMsgExt( 'article-comments-url-field', array( 'parseinline', 'content' ) ) . Html::element( 'br' ) . '</label>'; |
| 280 | + $content .= '<p><label for="commenterURL">' . |
| 281 | + wfMsgExt( |
| 282 | + 'article-comments-url-field', |
| 283 | + array( 'parseinline', 'content' ) |
| 284 | + ) . Html::element( 'br' ) . '</label>'; |
262 | 285 | $content .= Html::input( 'commenterURL', 'http://', 'text', array( 'id' => 'commenterURL' ) ); |
263 | 286 | $content .= '</p>'; |
264 | 287 | } |
265 | 288 | |
266 | | - $content .= '<p><label for="comment">' . wfMsgExt( 'article-comments-comment-field', array( 'parseinline', 'content' ) ) . Html::element( 'br' ) . '</label>'; |
| 289 | + $content .= '<p><label for="comment">' . |
| 290 | + wfMsgExt( 'article-comments-comment-field', array( 'parseinline', 'content' ) ) . |
| 291 | + Html::element( 'br' ) . '</label>'; |
267 | 292 | |
268 | 293 | $content .= '<textarea id="comment" name="comment" style="width:30em" rows="5">' . '</textarea></p>'; |
269 | 294 | |
— | — | @@ -327,15 +352,14 @@ |
328 | 353 | |
329 | 354 | $js .= "})();\n//]]></script>"; |
330 | 355 | $wgOut->addScript( $js ); |
331 | | - |
| 356 | + |
332 | 357 | return $content; |
333 | 358 | } |
334 | 359 | |
335 | 360 | /** |
336 | | -* Special page for comment processing. |
337 | | -*/ |
| 361 | + * Special page for comment processing. |
| 362 | + */ |
338 | 363 | function specialProcessComment() { |
339 | | - |
340 | 364 | global $wgOut, $wgParser, $wgUser, $wgContLang, $wgRequest; |
341 | 365 | |
342 | 366 | # Retrieve submitted values |
— | — | @@ -371,7 +395,7 @@ |
372 | 396 | } |
373 | 397 | |
374 | 398 | if ( !$comment ) { |
375 | | - $messages[] = wfMsg('article-comments-required-field', wfMsg( 'article-comments-comment-string' ) ); |
| 399 | + $messages[] = wfMsg( 'article-comments-required-field', wfMsg( 'article-comments-comment-string' ) ); |
376 | 400 | } |
377 | 401 | |
378 | 402 | if ( !empty( $messages ) ) { |
— | — | @@ -381,7 +405,7 @@ |
382 | 406 | foreach ( $messages as $message ) { |
383 | 407 | $wikiText .= "* $message\n"; |
384 | 408 | } |
385 | | - $wgOut->addWikiText( $wikiText . "</div>" ); |
| 409 | + $wgOut->addWikiText( $wikiText . '</div>' ); |
386 | 410 | return; |
387 | 411 | } |
388 | 412 | |
— | — | @@ -398,7 +422,7 @@ |
399 | 423 | # 1 error only but message is used above for n errors too |
400 | 424 | $wikiText .= wfMsgExt( 'article-comments-failure-reasons', 'parsemag', 1 ) . "\n\n"; |
401 | 425 | $wikiText .= '* ' . wfMsg( 'article-comments-user-is-blocked', $talkTitle->getPrefixedText() ) . "\n"; |
402 | | - $wgOut->addWikiText( $wikiText . "</div>" ); |
| 426 | + $wgOut->addWikiText( $wikiText . '</div>' ); |
403 | 427 | return; |
404 | 428 | } |
405 | 429 | |
— | — | @@ -414,8 +438,7 @@ |
415 | 439 | $talkContent = $talkArticle->getContent(); |
416 | 440 | } |
417 | 441 | |
418 | | - |
419 | | - # Check if talk NS is in the Namespace display list |
| 442 | + # Check if talk NS is in the namespace display list |
420 | 443 | # Note: if so, then there's no need to confirm that <comments /> appears in the article or talk page. |
421 | 444 | global $wgArticleCommentsNSDisplayList; |
422 | 445 | $skipCheck = ( |
— | — | @@ -431,24 +454,24 @@ |
432 | 455 | ) { |
433 | 456 | $wgOut->setPageTitle( wfMsgForContent( 'article-comments-submission-failed' ) ); |
434 | 457 | $wgOut->addWikiText( |
435 | | - "<div class='errorbox'>" . |
| 458 | + '<div class="errorbox">' . |
436 | 459 | wfMsg( 'article-comments-no-comments', $title->getPrefixedText() ) . |
437 | | - "</div>" |
| 460 | + '</div>' |
438 | 461 | ); |
439 | 462 | return; |
440 | 463 | } |
441 | 464 | |
442 | 465 | # Run spam checks |
443 | | - $isspam = false; |
444 | | - wfRunHooks( 'ArticleCommentsSpamCheck', array( $comment , $commenterName, $commenterURL, &$isspam ) ); |
| 466 | + $isSpam = false; |
| 467 | + wfRunHooks( 'ArticleCommentsSpamCheck', array( $comment, $commenterName, $commenterURL, &$isSpam ) ); |
445 | 468 | |
446 | 469 | # If it's spam - it's gone! |
447 | | - if ( $isspam ) { |
| 470 | + if ( $isSpam ) { |
448 | 471 | $wgOut->setPageTitle( wfMsg( 'article-comments-submission-failed' ) ); |
449 | 472 | $wgOut->addWikiText( |
450 | | - "<div class='errorbox'>" . |
| 473 | + '<div class="errorbox">' . |
451 | 474 | wfMsg( 'article-comments-no-spam' ) . |
452 | | - "</div>" |
| 475 | + '</div>' |
453 | 476 | ); |
454 | 477 | return; |
455 | 478 | } |
— | — | @@ -461,16 +484,22 @@ |
462 | 485 | # Search for insertion point, or append most recent comment. |
463 | 486 | $commentText = wfMsgForContent( 'article-comments-new-comment-heading', $commenterName, $commenterURL, '~~~~', $comment ); |
464 | 487 | $commentText .= '<comment date="' . htmlspecialchars( wfTimestamp( TS_ISO_8601 ) ) . '" name="' . htmlspecialchars( $commenterName ) . '"'; |
465 | | - if ( $commenterURL != '' ) $commentText .= ' url="' . htmlspecialchars( $commenterURL ) . '"'; |
466 | | - if ( $wgUser->isLoggedIn() ) $commentText .= ' signature="' . htmlspecialchars( $wgParser->getUserSig( $wgUser ) ) . '"'; |
| 488 | + if ( $commenterURL != '' ) { |
| 489 | + $commentText .= ' url="' . htmlspecialchars( $commenterURL ) . '"'; |
| 490 | + } |
| 491 | + if ( $wgUser->isLoggedIn() ) { |
| 492 | + $commentText .= ' signature="' . htmlspecialchars( $wgParser->getUserSig( $wgUser ) ) . '"'; |
| 493 | + } |
467 | 494 | $commentText .= ">\n" . str_replace( '</comment', '</comment', $comment ) . "\n</comment>"; |
468 | | - |
| 495 | + |
469 | 496 | $posAbove = stripos( $talkContent, '<!--COMMENTS_ABOVE-->' ); |
470 | | - if ( $posAbove === false ) $posBelow = stripos( $talkContent, '<!--COMMENTS_BELOW-->' ); |
| 497 | + if ( $posAbove === false ) { |
| 498 | + $posBelow = stripos( $talkContent, '<!--COMMENTS_BELOW-->' ); |
| 499 | + } |
471 | 500 | if ( $posAbove !== false ) { |
472 | 501 | # Insert comments above HTML marker |
473 | 502 | $talkContent = substr( $talkContent, 0, $posAbove ) . $commentText . substr( $talkContent, $posAbove ); |
474 | | - } else if ( $posBelow !== false ) { |
| 503 | + } elseif ( $posBelow !== false ) { |
475 | 504 | # Insert comments below HTML marker |
476 | 505 | $talkContent = substr( $talkContent, 0, $posBelow + 21 ) . $commentText . substr( $talkContent, $posBelow + 21 ); |
477 | 506 | } else { |
— | — | @@ -483,22 +512,21 @@ |
484 | 513 | $talkArticle->doEdit( $talkContent, $summary ); |
485 | 514 | |
486 | 515 | $wgOut->setPageTitle( wfMsg( 'article-comments-submission-succeeded' ) ); |
487 | | - $wgOut->addWikiText( wfMsg( 'article-comments-submission-success', $title->getPrefixedText() ) ); |
488 | | - $wgOut->addWikiText( wfMsg( 'article-comments-submission-view-all', $talkTitle->getPrefixedText() ) ); |
| 516 | + $wgOut->addWikiMsg( 'article-comments-submission-success', $title->getPrefixedText() ); |
| 517 | + $wgOut->addWikiMsg( 'article-comments-submission-view-all', $talkTitle->getPrefixedText() ); |
489 | 518 | } |
490 | 519 | |
491 | 520 | /** |
492 | | - * Checks ArticleComment fields for SPAM. |
| 521 | + * Checks ArticleComment fields for spam. |
493 | 522 | * Usage: $wgHooks['ArticleCommentsSpamCheck'][] = 'defaultArticleCommentSpamCheck'; |
494 | 523 | * @param String $comment The comment body submitted (passed by value) |
495 | 524 | * @param String $commenterName Name of commenter (passed by value) |
496 | 525 | * @param String $commenterURL Website URL provided for comment (passed by value) |
497 | | - * @param Boolean $isspam Whether the comment is spam (passed by reference) |
| 526 | + * @param Boolean $isSpam Whether the comment is spam (passed by reference) |
498 | 527 | * @return Boolean Always true to indicate other hooking methods may continue to check for spam. |
499 | 528 | */ |
500 | | -function defaultArticleCommentSpamCheck( $comment, $commenterName, $commenterURL, &$isspam ) { |
501 | | - |
502 | | - if ( $isspam ) { |
| 529 | +function defaultArticleCommentSpamCheck( $comment, $commenterName, $commenterURL, &$isSpam ) { |
| 530 | + if ( $isSpam ) { |
503 | 531 | # This module only marks comments as spam (other modules may unspam) |
504 | 532 | return true; |
505 | 533 | } |
— | — | @@ -510,7 +538,7 @@ |
511 | 539 | if ( $wgSpamRegex ) { |
512 | 540 | foreach ( $fields as $field ) { |
513 | 541 | if ( preg_match( $wgSpamRegex, $field ) ) { |
514 | | - $isspam = true; |
| 542 | + $isSpam = true; |
515 | 543 | return true; |
516 | 544 | } |
517 | 545 | } |
— | — | @@ -524,7 +552,7 @@ |
525 | 553 | foreach ( $spampatterns as $sp ) { |
526 | 554 | foreach ( $fields as $field ) { |
527 | 555 | if ( preg_match( $sp, $field ) ) { |
528 | | - $isspam = true; |
| 556 | + $isSpam = true; |
529 | 557 | return true; |
530 | 558 | } |
531 | 559 | } |
— | — | @@ -539,17 +567,17 @@ |
540 | 568 | ); |
541 | 569 | foreach ( $spampatterns as $sp ) { |
542 | 570 | if ( preg_match( $sp, $commenterName ) ) { |
543 | | - $isspam = true; |
| 571 | + $isSpam = true; |
544 | 572 | return true; |
545 | 573 | } |
546 | 574 | } |
547 | 575 | |
548 | 576 | # Fail for length violations |
549 | 577 | if ( strlen( $commenterName ) > 255 || strlen( $commenterURL ) > 300 ) { |
550 | | - $isspam = true; |
| 578 | + $isSpam = true; |
551 | 579 | return true; |
552 | 580 | } |
553 | 581 | |
554 | | - # We made it this far, leave $isspam alone and give other implementors a chance. |
| 582 | + # We made it this far, leave $isSpam alone and give other implementors a chance. |
555 | 583 | return true; |
556 | 584 | } |