r74698 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r74697‎ | r74698 | r74699 >
Date:23:48, 12 October 2010
Author:platonides
Status:deferred
Tags:
Comment:
Change the way in which comments are added by enclosing them into a new <comment> tag. This provides isolation between comments.
Replaces the old article-comments-new-comment message.
Include three different comment modes (needs ui).
The heading could have also been provided by <comment/>, but keeping it outside allows to use the edit section feature for comments.
Forbidden new lines in names. This should help protect against wiki-injection. In the default, article-comments-new-comment-heading outputs the name outside the <comment> in a header.
Added new lines after erroboxes in case wiki syntax beginning by new line is used on those messages.
Modified paths:
  • /trunk/extensions/ArticleComments/ArticleComments.i18n.php (modified) (history)
  • /trunk/extensions/ArticleComments/ArticleComments.php (modified) (history)

Diff [purge]

Index: trunk/extensions/ArticleComments/ArticleComments.php
@@ -3,7 +3,7 @@
44 * ArticleComments.php - A MediaWiki extension for adding comment sections to articles.
55 * @author Jim R. Wilson
66 * @author Platonides
7 - * @version 0.5.1
 7+ * @version 0.6
88 * @copyright Copyright (C) 2007 Jim R. Wilson
99 * @license The MIT License - http://www.opensource.org/licenses/mit-license.php
1010 * -----------------------------------------------------------------------
@@ -22,6 +22,8 @@
2323 * <comments />
2424 * Note: Typically this would be placed at the end of the article text.
2525 * Version Notes:
 26+ * version 0.6:
 27+ * Added comments inside <comment> tags instead of article-comments-new-comment message.
2628 * version 0.5.1:
2729 * Removed the base64 pass
2830 * version 0.5:
@@ -83,7 +85,7 @@
8486 'author' => 'Jim R. Wilson - wilson.jim.r <at> gmail.com',
8587 'url' => 'http://jimbojw.com/wiki/index.php?title=ArticleComments',
8688 'descriptionmsg' => 'article-comments-desc',
87 - 'version' => '0.5.1'
 89+ 'version' => '0.6'
8890 );
8991
9092 # Add Extension Functions
@@ -102,7 +104,8 @@
103105 $wgArticleCommentDefaults = array(
104106 'showurlfield' => true, # Provide an URL field ?
105107 'noscript' => false, # Set to true to not include any ArticleComments related JavaScript
106 - 'hideform' => true # Should the comment field be hidden by default?
 108+ 'hideform' => true, # Should the comment field be hidden by default?
 109+ 'defaultMode' => 'normal', # Which mode should be preselected for comments? Values are: plain, normal and wiki
107110 );
108111
109112 /**
@@ -122,6 +125,7 @@
123126 # Sets up the ArticleComments Parser hook for <comments />
124127 function wfArticleCommentsParserSetup( &$parser ) {
125128 $parser->setHook( 'comments', 'wfArticleCommentsParserHook' );
 129+ $parser->setHook( 'comment', 'wfArticleCommentsParserHookComment' );
126130 return true;
127131 }
128132
@@ -130,6 +134,64 @@
131135 return wfArticleCommentForm( $parser->mTitle, $params );
132136 }
133137
 138+function wfArticleCommentsParserHookComment( $text, $args, $parser, $frame ) {
 139+ global $wgArticleCommentDefaults, $wgParser, $wgParserConf;
 140+
 141+ if ( $parser === $wgParser ) # Workaround bug 25506
 142+ $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'] ) )
 148+ $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
 153+ $args['date'] = $parser->getFunctionLang()->date( wfTimestamp( TS_MW, $args['date'] ) );
 154+
 155+ if ( !isset( $args['signature'] ) )
 156+ $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
 158+ $args['signature'] = $parser->recursiveTagParse( $args['signature'], $frame );
 159+
 160+ if ( !isset( $args['mode'] ) )
 161+ $args['mode'] = $wgArticleCommentDefaults['defaultMode'];
 162+
 163+ $args['mode'] = strtolower( $args['mode'] );
 164+ if ( $args['mode'] == 'plain' ) {
 165+ // Don't perform any formatting
 166+
 167+ $text = htmlspecialchars( $text );
 168+
 169+ // But make new line generate new paragraphs
 170+ $text = str_replace( "\n", "</p><p>", $text );
 171+
 172+ return "<p>$text</p>";
 173+ } elseif ( $args['mode'] == 'normal' ) {
 174+ // Convert some wikitext oddities to wiki markup
 175+
 176+ # Need only a newline for new paragraph
 177+ $text = str_replace( "\n", "\n\n", $text );
 178+
 179+ # Disable <pre> on space // TODO: Enable space indenting.
 180+ $text = str_replace( "\n ", "\n&#32;", $text );
 181+
 182+ } elseif ( $args['mode'] == 'wiki' ) {
 183+ /* Full wikitext */
 184+
 185+ } else {
 186+ return wfMsgExt( 'article-comments-comment-bad-mode', array( 'parsemag', 'language' => $parser->getFunctionLang() ) ) ;
 187+ }
 188+
 189+ # Parse the content, this is later kept as-is since we do a replaceafter there.
 190+ $text = $parser->recursiveTagParse( $text, $frame );
 191+
 192+ return wfMsgExt( 'article-comments-comment-contents', array( 'parse', 'replaceafter', 'content' ),
 193+ $args['name'], $args['url'], $args['signature'], $args['date'], $text );
 194+}
 195+
134196 /**
135197 * Echos out a comment form depending on the page action and namespace.
136198 * @param Title $title The title of the article on which the form will appear.
@@ -298,7 +360,7 @@
299361 $messages[] = wfMsg( 'article-comments-invalid-field', wfMsgForContent( 'article-comments-title-string' ), $titleText );
300362 }
301363
302 - if ( !$commenterName ) {
 364+ if ( !$commenterName || strpos( $commenterName, "\n" ) !== false ) {
303365 $messages[] = wfMsg( 'article-comments-required-field', wfMsgForContent( 'article-comments-name-string' ) );
304366 }
305367
@@ -312,7 +374,7 @@
313375
314376 if ( !empty( $messages ) ) {
315377 $wgOut->setPageTitle( wfMsg( 'article-comments-submission-failed' ) );
316 - $wikiText = "<div class='errorbox'>";
 378+ $wikiText = "<div class='errorbox'>\n";
317379 $wikiText .= wfMsg( 'article-comments-failure-reasons' ) . "\n\n";
318380 foreach ( $messages as $message ) {
319381 $wikiText .= "* $message\n";
@@ -330,7 +392,7 @@
331393 # Check whether user is blocked from editing the talk page
332394 if ( $wgUser->isBlockedFrom( $talkTitle ) ) {
333395 $wgOut->setPageTitle( wfMsg( 'article-comments-submission-failed' ) );
334 - $wikiText = "<div class='errorbox'>";
 396+ $wikiText = "<div class='errorbox'>\n";
335397 $wikiText .= wfMsg( 'article-comments-failure-reasons' ) . "\n\n";
336398 $wikiText .= '* ' . wfMsg( 'article-comments-user-is-blocked', $talkTitle->getPrefixedText() ) . "\n";
337399 $wgOut->addWikiText( $wikiText . "</div>" );
@@ -393,20 +455,13 @@
394456 $talkContent = wfMsgForContent( 'article-comments-talk-page-starter', $title->getPrefixedText() );
395457 }
396458
397 - # Determine signature components
398 - if ( $commenterURL != '' ) $sigText = "[$commenterURL $commenterName]";
399 - else if ( $wgUser->isLoggedIn() ) $sigText = $wgParser->getUserSig( $wgUser );
400 - else $sigText = $commenterName;
401 -
402459 # Search for insertion point, or append most recent comment.
403 - $commentText = wfMsgForContent(
404 - 'article-comments-new-comment',
405 - wfMsgForContent( 'article-comments-commenter-said', $commenterName ),
406 - $comment,
407 - $sigText,
408 - '~~~~~'
409 - );
410 -
 460+ $commentText = wfMsgForContent( 'article-comments-new-comment-heading', $commenterName, $commenterURL, '~~~~', $comment );
 461+ $commentText .= '<comment date="' . htmlspecialchars( wfTimestamp( TS_ISO_8601 ) ) . '" name="' . htmlspecialchars( $commenterName ) . '"';
 462+ if ( $commenterURL != '' ) $commentText .= ' url="' . htmlspecialchars( $commenterURL ) . '"';
 463+ if ( $wgUser->isLoggedIn() ) $commentText .= ' signature="' . htmlspecialchars( $wgParser->getUserSig( $wgUser ) ) . '"';
 464+ $commentText .= ">\n" . str_replace( '</comment', '&lt;/comment', $comment ) . "\n</comment>";
 465+
411466 $posAbove = stripos( $talkContent, '<!--COMMENTS_ABOVE-->' );
412467 if ( $posAbove === false ) $posBelow = stripos( $talkContent, '<!--COMMENTS_BELOW-->' );
413468 if ( $posAbove !== false ) {
Index: trunk/extensions/ArticleComments/ArticleComments.i18n.php
@@ -32,7 +32,11 @@
3333 'article-comments-submission-view-all' => 'You may view all comments on that article [[$1|here]]',
3434 'article-comments-prefilled-comment-text' => '',
3535 'article-comments-user-is-blocked' => 'Your user account is currently blocked from editing [[$1]].',
36 - 'article-comments-new-comment' => "\n== \$1 ==\n\n<div class='commentBlock'>\n\$2\n\n--\$3 \$4\n</div>\n",
 36+ 'article-comments-new-comment-heading' => "\n== {{int:article-comments-commenter-said|\$1}} ==\n\n",
 37+ 'article-comments-comment-bad-mode' => '<div class="error">Invalid mode given for comment. Available ones are plain, normal and wiki.</div>',
 38+ 'article-comments-comment-contents' => "<div class='commentBlock'><small>$4</small>$5--\$3</div>\n",
 39+ 'article-comments-comment-missing-name-parameter' => 'Missing name',
 40+ 'article-comments-comment-missing-date-parameter' => 'Missing comment date',
3741 'article-comments-no-spam' => 'At least one of the submitted fields was flagged as spam.',
3842 'processcomment' => 'Process article comment',
3943 );
@@ -41,10 +45,20 @@
4246 'article-comments-required-field' => 'Shown as a list below article-comments-failure-reasons. With $1 being one of article-comments-*-string messages.',
4347 'article-comments-submission-failed' => 'Page title when there are errors in the comment submission',
4448 'article-comments-invalid-field' => 'Shown as a list below article-comments-failure-reasons. With $1 being article-comments-title-string or article-comments-url-string messages, and $2 the wrong value.',
45 - 'article-comments-new-comment' => 'Text to add in the new comment.
46 -* $1 - Expansion of article-comments-commenter-said.
47 -* $2 - Comment text.
48 -* $3 - Commenter name, possibly linking to its web.
49 -* $4 - Datetime.',
 49+ 'article-comments-new-comment-heading' => 'Wiki text which will appear above the &lt;comment&gt; tags.
 50+
 51+Available variables:
 52+* $1 - Commenter name.
 53+* $2 - Commenter url (may be empty).
 54+* $3 - Datetime.
 55+* $4 - Comment text.',
 56+ 'article-comments-comment-contents' => 'Way in which &lt;comment&gt; tags are parsed.
 57+Note it is importat not to place the $5 between new-lines in order to get new lines correctly converted into new paragraphs in normal mode (wfMsgExt can\'t place it inside a <p/>).
 58+
 59+* $1 - Commenter name.
 60+* $2 - Comment url.
 61+* $3 - User signature, if an URL is available, name linking to its web.
 62+* $4 - Parsed datetime.
 63+* $5 - Comment text.',
5064 );
5165

Status & tagging log