r75431 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r75430‎ | r75431 | r75432 >
Date:15:45, 26 October 2010
Author:ashley
Status:ok
Tags:
Comment:
ArticleComments: coding style tweaks etc.
Modified paths:
  • /trunk/extensions/ArticleComments/ArticleComments.php (modified) (history)

Diff [purge]

Index: trunk/extensions/ArticleComments/ArticleComments.php
@@ -1,10 +1,10 @@
22 <?php
3 -/*
 3+/**
44 * ArticleComments.php - A MediaWiki extension for adding comment sections to articles.
55 * @author Jim R. Wilson
66 * @author Platonides
77 * @version 0.6
8 - * @copyright Copyright (C) 2007 Jim R. Wilson
 8+ * @copyright Copyright © 2007 Jim R. Wilson
99 * @license The MIT License - http://www.opensource.org/licenses/mit-license.php
1010 * -----------------------------------------------------------------------
1111 * Description:
@@ -52,7 +52,7 @@
5353 * version 0.1:
5454 * Initial release.
5555 * -----------------------------------------------------------------------
56 - * Copyright (c) 2007 Jim R. Wilson
 56+ * Copyright © 2007 Jim R. Wilson
5757 *
5858 * Permission is hereby granted, free of charge, to any person obtaining a copy
5959 * of this software and associated documentation files (the "Software"), to deal
@@ -76,23 +76,24 @@
7777 */
7878
7979 # Confirm MW environment
80 -if ( !defined( 'MEDIAWIKI' ) ) die();
 80+if ( !defined( 'MEDIAWIKI' ) ) {
 81+ die();
 82+}
8183
8284 # Credits
8385 $wgExtensionCredits['other'][] = array(
8486 'path' => __FILE__,
8587 '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',
8891 'descriptionmsg' => 'article-comments-desc',
89 - 'version' => '0.6'
9092 );
9193
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';
9496
95 -
96 -# Attach Hooks
 97+# Attach hooks
9798 $wgHooks['ParserFirstCallInit'][] = 'wfArticleCommentsParserSetup';
9899 $wgHooks['SkinAfterContent'][] = 'wfArticleCommentsAfterContent';
99100 $wgHooks['ArticleCommentsSpamCheck'][] = 'defaultArticleCommentSpamCheck';
@@ -106,7 +107,7 @@
107108 'noscript' => false, # Set to true to not include any ArticleComments related JavaScript
108109 'hideform' => true, # Should the comment field be hidden by default?
109110 'defaultMode' => 'normal', # Which mode should be preselected for comments? Values are: plain, normal and wiki
110 - );
 111+);
111112
112113 /**
113114 * List of namespaces on which a comment field is automatically added.
@@ -137,81 +138,93 @@
138139 function wfArticleCommentsParserHookComment( $text, $args, $parser, $frame ) {
139140 global $wgArticleCommentDefaults, $wgParser, $wgParserConf;
140141
141 - if ( $parser === $wgParser ) # Workaround bug 25506
 142+ if ( $parser === $wgParser ) { # Workaround bug 25506
142143 $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'] ) ) {
148151 $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 {
153157 $args['date'] = $parser->getFunctionLang()->date( wfTimestamp( TS_MW, $args['date'] ) );
154 -
155 - if ( !isset( $args['signature'] ) )
 158+ }
 159+
 160+ if ( !isset( $args['signature'] ) ) {
156161 $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
158163 $args['signature'] = $parser->recursiveTagParse( $args['signature'], $frame );
159 -
160 - if ( !isset( $args['mode'] ) )
 164+ }
 165+
 166+ if ( !isset( $args['mode'] ) ) {
161167 $args['mode'] = $wgArticleCommentDefaults['defaultMode'];
 168+ }
162169
163 - $args['mode'] = strtolower( $args['mode'] );
 170+ $args['mode'] = strtolower( $args['mode'] );
164171 if ( $args['mode'] == 'plain' ) {
165172 // Don't perform any formatting
166 -
167173 $text = htmlspecialchars( $text );
168 -
 174+
169175 // But make new line generate new paragraphs
170176 $text = str_replace( "\n", "</p><p>", $text );
171 -
172 - return "<p>$text</p>";
 177+
 178+ return "<p>$text</p>";
173179 } elseif ( $args['mode'] == 'normal' ) {
174180 // Convert some wikitext oddities to wiki markup
175 -
 181+
176182 # Need only a newline for new paragraph
177183 $text = str_replace( "\n", "\n\n", $text );
178 -
 184+
179185 # Disable <pre> on space // TODO: Enable space indenting.
180186 $text = str_replace( "\n ", "\n&#32;", $text );
181 -
 187+
182188 } elseif ( $args['mode'] == 'wiki' ) {
183189 /* Full wikitext */
184 -
 190+
185191 } else {
186192 return Html::rawElement( 'div', array( 'class' => 'error' ),
187193 wfMsgExt( 'article-comments-comment-bad-mode', array( 'parsemag', 'language' => $parser->getFunctionLang() ) )
188194 );
189195 }
190 -
 196+
191197 # Parse the content, this is later kept as-is since we do a replaceafter there.
192198 $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+ );
196205 }
197206
198207 /**
199208 * 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
202212 */
203213 function wfArticleCommentsAfterContent( $data, $skin ) {
204 -
205214 global $wgRequest, $wgArticleCommentsNSDisplayList;
206215
207 - # Short circuit for anything other than action=view or action=purge
 216+ # Short-circuit for anything other than action=view or action=purge
208217 if ( $wgRequest->getVal( 'action' ) &&
209218 $wgRequest->getVal( 'action' ) != 'view' &&
210219 $wgRequest->getVal( 'action' ) != 'purge'
211 - ) return true;
 220+ )
 221+ {
 222+ return true;
 223+ }
212224
213225 # Short-circuit if displaylist is undefined, empty or null
214 - if ( $wgArticleCommentsNSDisplayList == null )
 226+ if ( $wgArticleCommentsNSDisplayList == null ) {
215227 return true;
 228+ }
216229
217230 $title = $skin->getTitle();
218231 if ( !$title->exists() ) {
@@ -220,7 +233,9 @@
221234
222235 # Ensure that the namespace list is an actual list
223236 $nsList = $wgArticleCommentsNSDisplayList;
224 - if ( !is_array( $nsList ) ) $nsList = array( $nsList );
 237+ if ( !is_array( $nsList ) ) {
 238+ $nsList = array( $nsList );
 239+ }
225240
226241 # Display the form
227242 if ( in_array( $title->getNamespace(), $nsList ) ) {
@@ -231,9 +246,9 @@
232247 }
233248
234249 /**
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.
238253 */
239254 function wfArticleCommentForm( $title, $params = array() ) {
240255 global $wgArticleCommentDefaults, $wgOut;
@@ -252,17 +267,27 @@
253268 $content .= '<p>';
254269 $content .= Html::hidden( 'commentArticle', $title->getPrefixedDBkey() );
255270
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>';
257276 $content .= Html::input( 'commenterName', '', 'text', array( 'id' => 'commenterName' ) );
258277 $content .= '</p>';
259278
260279 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>';
262285 $content .= Html::input( 'commenterURL', 'http://', 'text', array( 'id' => 'commenterURL' ) );
263286 $content .= '</p>';
264287 }
265288
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>';
267292
268293 $content .= '<textarea id="comment" name="comment" style="width:30em" rows="5">' . '</textarea></p>';
269294
@@ -327,15 +352,14 @@
328353
329354 $js .= "})();\n//]]></script>";
330355 $wgOut->addScript( $js );
331 -
 356+
332357 return $content;
333358 }
334359
335360 /**
336 -* Special page for comment processing.
337 -*/
 361+ * Special page for comment processing.
 362+ */
338363 function specialProcessComment() {
339 -
340364 global $wgOut, $wgParser, $wgUser, $wgContLang, $wgRequest;
341365
342366 # Retrieve submitted values
@@ -371,7 +395,7 @@
372396 }
373397
374398 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' ) );
376400 }
377401
378402 if ( !empty( $messages ) ) {
@@ -381,7 +405,7 @@
382406 foreach ( $messages as $message ) {
383407 $wikiText .= "* $message\n";
384408 }
385 - $wgOut->addWikiText( $wikiText . "</div>" );
 409+ $wgOut->addWikiText( $wikiText . '</div>' );
386410 return;
387411 }
388412
@@ -398,7 +422,7 @@
399423 # 1 error only but message is used above for n errors too
400424 $wikiText .= wfMsgExt( 'article-comments-failure-reasons', 'parsemag', 1 ) . "\n\n";
401425 $wikiText .= '* ' . wfMsg( 'article-comments-user-is-blocked', $talkTitle->getPrefixedText() ) . "\n";
402 - $wgOut->addWikiText( $wikiText . "</div>" );
 426+ $wgOut->addWikiText( $wikiText . '</div>' );
403427 return;
404428 }
405429
@@ -414,8 +438,7 @@
415439 $talkContent = $talkArticle->getContent();
416440 }
417441
418 -
419 - # Check if talk NS is in the Namespace display list
 442+ # Check if talk NS is in the namespace display list
420443 # Note: if so, then there's no need to confirm that <comments /> appears in the article or talk page.
421444 global $wgArticleCommentsNSDisplayList;
422445 $skipCheck = (
@@ -431,24 +454,24 @@
432455 ) {
433456 $wgOut->setPageTitle( wfMsgForContent( 'article-comments-submission-failed' ) );
434457 $wgOut->addWikiText(
435 - "<div class='errorbox'>" .
 458+ '<div class="errorbox">' .
436459 wfMsg( 'article-comments-no-comments', $title->getPrefixedText() ) .
437 - "</div>"
 460+ '</div>'
438461 );
439462 return;
440463 }
441464
442465 # 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 ) );
445468
446469 # If it's spam - it's gone!
447 - if ( $isspam ) {
 470+ if ( $isSpam ) {
448471 $wgOut->setPageTitle( wfMsg( 'article-comments-submission-failed' ) );
449472 $wgOut->addWikiText(
450 - "<div class='errorbox'>" .
 473+ '<div class="errorbox">' .
451474 wfMsg( 'article-comments-no-spam' ) .
452 - "</div>"
 475+ '</div>'
453476 );
454477 return;
455478 }
@@ -461,16 +484,22 @@
462485 # Search for insertion point, or append most recent comment.
463486 $commentText = wfMsgForContent( 'article-comments-new-comment-heading', $commenterName, $commenterURL, '~~~~', $comment );
464487 $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+ }
467494 $commentText .= ">\n" . str_replace( '</comment', '&lt;/comment', $comment ) . "\n</comment>";
468 -
 495+
469496 $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+ }
471500 if ( $posAbove !== false ) {
472501 # Insert comments above HTML marker
473502 $talkContent = substr( $talkContent, 0, $posAbove ) . $commentText . substr( $talkContent, $posAbove );
474 - } else if ( $posBelow !== false ) {
 503+ } elseif ( $posBelow !== false ) {
475504 # Insert comments below HTML marker
476505 $talkContent = substr( $talkContent, 0, $posBelow + 21 ) . $commentText . substr( $talkContent, $posBelow + 21 );
477506 } else {
@@ -483,22 +512,21 @@
484513 $talkArticle->doEdit( $talkContent, $summary );
485514
486515 $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() );
489518 }
490519
491520 /**
492 - * Checks ArticleComment fields for SPAM.
 521+ * Checks ArticleComment fields for spam.
493522 * Usage: $wgHooks['ArticleCommentsSpamCheck'][] = 'defaultArticleCommentSpamCheck';
494523 * @param String $comment The comment body submitted (passed by value)
495524 * @param String $commenterName Name of commenter (passed by value)
496525 * @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)
498527 * @return Boolean Always true to indicate other hooking methods may continue to check for spam.
499528 */
500 -function defaultArticleCommentSpamCheck( $comment, $commenterName, $commenterURL, &$isspam ) {
501 -
502 - if ( $isspam ) {
 529+function defaultArticleCommentSpamCheck( $comment, $commenterName, $commenterURL, &$isSpam ) {
 530+ if ( $isSpam ) {
503531 # This module only marks comments as spam (other modules may unspam)
504532 return true;
505533 }
@@ -510,7 +538,7 @@
511539 if ( $wgSpamRegex ) {
512540 foreach ( $fields as $field ) {
513541 if ( preg_match( $wgSpamRegex, $field ) ) {
514 - $isspam = true;
 542+ $isSpam = true;
515543 return true;
516544 }
517545 }
@@ -524,7 +552,7 @@
525553 foreach ( $spampatterns as $sp ) {
526554 foreach ( $fields as $field ) {
527555 if ( preg_match( $sp, $field ) ) {
528 - $isspam = true;
 556+ $isSpam = true;
529557 return true;
530558 }
531559 }
@@ -539,17 +567,17 @@
540568 );
541569 foreach ( $spampatterns as $sp ) {
542570 if ( preg_match( $sp, $commenterName ) ) {
543 - $isspam = true;
 571+ $isSpam = true;
544572 return true;
545573 }
546574 }
547575
548576 # Fail for length violations
549577 if ( strlen( $commenterName ) > 255 || strlen( $commenterURL ) > 300 ) {
550 - $isspam = true;
 578+ $isSpam = true;
551579 return true;
552580 }
553581
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.
555583 return true;
556584 }

Follow-up revisions

RevisionCommit summaryAuthorDate
r75444Followup r75431, bug 24853. Add to RELEASE-NOTESreedy17:12, 26 October 2010

Status & tagging log