r73985 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r73984‎ | r73985 | r73986 >
Date:21:36, 29 September 2010
Author:platonides
Status:resolved (Comments)
Tags:
Comment:
Updated to work with MediaWiki 1.16+ (may also work with previous versions)
Moved i18n to its own file.
Initialised and documented extension variables.
Removed mentions to $wgContentLang
Updated the regex which now need new lines between the base64 text and the <pre> tags
Renamed displayArticleCommentForm() to wfArticleCommentsAfterContent() and made it work, which makes $wgArticleCommentsNSDisplayList feature useful.
Namespace global comment form appear below the categories while those in <comments/> appear in place. Not sure what approach is the best one.
Fixed html injections.
Modified paths:
  • /trunk/extensions/ArticleComments/ArticleComments.i18n.php (added) (history)
  • /trunk/extensions/ArticleComments/ArticleComments.php (modified) (history)

Diff [purge]

Index: trunk/extensions/ArticleComments/ArticleComments.php
@@ -1,8 +1,9 @@
2 -<?php //{{MediaWikiExtension}}<source lang="php">
 2+<?php
33 /*
44 * ArticleComments.php - A MediaWiki extension for adding comment sections to articles.
55 * @author Jim R. Wilson
6 - * @version 0.4.4
 6+ * @author Platonides
 7+ * @version 0.5
78 * @copyright Copyright (C) 2007 Jim R. Wilson
89 * @license The MIT License - http://www.opensource.org/licenses/mit-license.php
910 * -----------------------------------------------------------------------
@@ -10,8 +11,7 @@
1112 * This is a MediaWiki (http://www.mediawiki.org/) extension which adds support
1213 * for comment sections within article pages, or directly into all pages.
1314 * Requirements:
14 - * MediaWiki 1.6.x, 1.8.x, 1.9.x or higher
15 - * PHP 4.x, 5.x or higher
 15+ * MediaWiki 1.16.x or higher
1616 * Installation:
1717 * 1. Drop this script (ArticleComments.php) in $IP/extensions
1818 * Note: $IP is your MediaWiki install dir.
@@ -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.5:
 27+ * Updated to work with MediaWiki 1.16+
2628 * version 0.4.3:
2729 * Added new insertion feature, comments will now be inserted before <!--COMMENTS_ABOVE--> if present
2830 * Or, after <!--COMMENTS_BELOW--> if present (the latter causes reverse chronological comment ordering).
@@ -78,22 +80,52 @@
7981 'author'=>'Jim R. Wilson - wilson.jim.r &lt;at&gt; gmail.com',
8082 'url'=>'http://jimbojw.com/wiki/index.php?title=ArticleComments',
8183 'description'=>'Enables comment sections on article pages.',
82 - 'version'=>'0.4.1'
 84+ 'version'=>'0.5'
8385 );
8486
8587 # Add Extension Functions
86 -$wgExtensionFunctions[] = 'wfArticleCommentsParserSetup';
 88+$wgExtensionMessagesFiles['ArticleComments'] = dirname( __FILE__ ) . "/ArticleComments.i18n.php";
8789
 90+
 91+# Attach Hooks
 92+$wgHooks['ParserFirstCallInit'][] = 'wfArticleCommentsParserSetup';
 93+$wgHooks['SkinAfterContent'][] = 'wfArticleCommentsAfterContent';
 94+$wgHooks['ParserAfterTidy'][] = 'wfProcessEncodedContent';
 95+$wgHooks['ArticleCommentsSpamCheck'][] = 'defaultArticleCommentSpamCheck';
 96+
 97+/**
 98+ * Comment options
 99+ * May be overriden as parameters to the <comment> tag
 100+ */
 101+$wgArticleCommentDefaults = array(
 102+ 'showurlfield' => true, # Provide an URL field ?
 103+ 'noscript' => false, # Set to true to not include any ArticleComments related JavaScript
 104+ 'hideform' => true # Should the comment field be hidden by default?
 105+ );
 106+
 107+/**
 108+ * List of namespaces on which a comment field is automatically added.
 109+ * Eg. $wgArticleCommentsNSDisplayList[] = NS_MAIN;
 110+ */
 111+$wgArticleCommentsNSDisplayList = array();
 112+
 113+# Sets up special page to handle comment submission
 114+$wgExtensionFunctions[] = 'setupSpecialProcessComment';
 115+
 116+function setupSpecialProcessComment() {
 117+ SpecialPage::addPage( new SpecialPage( 'ProcessComment', '', true, 'specialProcessComment', false ) );
 118+}
 119+
88120 # Sets up the ArticleComments Parser hook for <comments />
89 -function wfArticleCommentsParserSetup() {
90 - global $wgParser;
91 - $wgParser->setHook( 'comments', 'wfArticleCommentsParserHook' );
 121+function wfArticleCommentsParserSetup( &$parser ) {
 122+ $parser->setHook( 'comments', 'wfArticleCommentsParserHook' );
 123+ return true;
92124 }
93 -function wfArticleCommentsParserHook( $text, $params = array(), $parser ) {
94125
 126+function wfArticleCommentsParserHook( $text, $params = array(), $parser ) {
95127 # Generate a comment form for display
96128 $commentForm = wfArticleCommentForm( $parser->mTitle, $params );
97 -
 129+
98130 # Hide content from the Parser using base64 to avoid mangling.
99131 # Note: Content will be decoded after Tidy has finished its processing of the page.
100132 return '<pre>@ENCODED@'.base64_encode($commentForm).'@ENCODED@</pre>';
@@ -104,7 +136,7 @@
105137 * @param Title $title The title of the article on which the form will appear.
106138 * @param Array $params A hash of parameters containing rendering options.
107139 */
108 -function displayArticleCommentForm( $title = null, $params = array() ) {
 140+function wfArticleCommentsAfterContent( $data, $skin ) {
109141
110142 global $wgRequest, $wgArticleCommentsNSDisplayList;
111143
@@ -112,16 +144,16 @@
113145 if ($wgRequest->getVal('action') &&
114146 $wgRequest->getVal('action')!='view' &&
115147 $wgRequest->getVal('action')!='purge'
116 - ) return;
 148+ ) return true;
117149
118 - # Short-circuit if displayl ist is undefined or null
119 - if ($wgArticleCommentsNSDisplayList==null) return;
 150+ # Short-circuit if displaylist is undefined, empty or null
 151+ if ($wgArticleCommentsNSDisplayList == null)
 152+ return true;
120153
121 - # Use wgTitle if title is not specified
122 - if ($title==null) {
123 - global $wgTitle;
124 - $title = $wgTitle;
125 - }
 154+ $title = $skin->getTitle();
 155+ if ( !$title->exists() ) {
 156+ return true;
 157+ }
126158
127159 # Ensure that the namespace list is an actual list
128160 $nsList = $wgArticleCommentsNSDisplayList;
@@ -129,20 +161,20 @@
130162
131163 # Display the form
132164 if (in_array($title->getNamespace(), $nsList)) {
133 - echo(wfArticleCommentForm($title, $params));
 165+ $data .= wfArticleCommentForm($title, $params);
134166 }
135167
 168+ return true;
136169 }
137170
138171 /**
139 - * Generates and returns an ArticleComment form.
 172+ * Generates and returns an ArticleComment form html.
140173 * @param Title $title The title of the article on which the form will appear.
141174 * @param Array $params A hash of parameters containing rendering options.
142175 */
143 -function wfArticleCommentForm( $title = null, $params = array() ) {
 176+function wfArticleCommentForm( $title, $params = array() ) {
144177
145 - global $wgScript, $wgArticleCommentDefaults, $wgContentLang, $wgContLang;
146 - $wcl = ($wgContentLang ? $wgContentLang : $wgContLang);
 178+ global $wgScript, $wgArticleCommentDefaults, $wgContLang;
147179
148180 # Merge in global defaults if specified
149181 if (is_array($wgArticleCommentDefaults) &&
@@ -154,34 +186,28 @@
155187 $params = array_merge($tmp, $params);
156188 }
157189
158 - # Use wgTitle if title is not specified
159 - if ($title==null) {
160 - global $wgTitle;
161 - $title = $wgTitle;
162 - }
163 -
164190 $ac = 'article-comments-';
165 - $formAction = $wgScript.'?title='.$wcl->getNsText(NS_SPECIAL).':ProcessComment';
 191+ $formAction = $wgScript.'?title='.$wgContLang->getNsText(NS_SPECIAL).':ProcessComment';
166192
167193 # Build out the comment form.
168194 $content =
169195 '<div id="commentForm">'.
170 - '<form method="post" action="'.$formAction.'">'.
 196+ '<form method="post" action="'. htmlspecialchars( $formAction ) .'">'.
171197 '<input type="hidden" id="titleKey" name="titleKey" '.
172 - 'value="'.$title->getDBKey().'" />'.
 198+ 'value="'. htmlspecialchars( $title->getDBKey() ) . '" />'.
173199 '<input type="hidden" id="titleNS" name="titleNS" '.
174 - 'value="'.$title->getNamespace().'" />'.
175 - '<p>'.wfMsgForContent($ac.'name-field').'<br />'.
 200+ 'value="'. htmlspecialchars( $title->getNamespace() ) .'" />'.
 201+ '<p>'.wfMsgExt($ac.'name-field', array( 'parseinline', 'content' ) ).'<br />'.
176202 '<input type="text" id="commenterName" name="commenterName" /></p>'.
177203 ($params['showurlfield']=='false' || $params['showurlfield']===false?'':
178 - '<p>'.wfMsgForContent($ac.'url-field').'<br />'.
 204+ '<p>'.wfMsgExt($ac.'url-field', array( 'parseinline', 'content' ) ).'<br />'.
179205 '<input type="text" id="commenterURL" name="commenterURL" value="http://" /></p>'
180206 ).
181 - '<p>'.wfMsgForContent($ac.'comment-field').'<br />'.
 207+ '<p>'.wfMsgExt($ac.'comment-field', array( 'parseinline', 'content' ) ).'<br />'.
182208 '<textarea id="comment" name="comment" style="width:30em" rows="5">'.
183209 '</textarea></p>'.
184210 '<p><input id="submit" type="submit" '.
185 - 'value="'.wfMsgForContent($ac.'submit-button').'" /></p>'.
 211+ 'value="'.htmlspecialchars( wfMsgForContent($ac.'submit-button') ) .'" /></p>'.
186212 '</form></div>';
187213
188214 # Short-circuit if noScript has been set to anything other than false
@@ -246,10 +272,6 @@
247273 return $content;
248274 }
249275
250 -# Attach Hooks
251 -$wgHooks['ParserAfterTidy'][] = 'wfProcessEncodedContent';
252 -$wgHooks['ArticleCommentsSpamCheck'][] = 'defaultArticleCommentSpamCheck';
253 -
254276 /**
255277 * Processes HTML comments with encoded content.
256278 * Usage: $wgHooks['OutputPageBeforeHTML'][] = 'wfProcessEncodedContent';
@@ -259,54 +281,19 @@
260282 */
261283 function wfProcessEncodedContent($out, $text) {
262284 $text = preg_replace(
263 - '/<pre>@ENCODED@([0-9a-zA-Z\\+\\/]+=*)@ENCODED@<\\/pre>/e',
 285+ '/<pre>\n@ENCODED@([0-9a-zA-Z\\+\\/]+=*)@ENCODED@\n<\\/pre>/e',
264286 'base64_decode("$1")',
265287 $text
266288 );
267289 return true;
268290 }
269291
270 -# Sets up special page to handle comment submission
271 -$wgExtensionFunctions[] = 'setupSpecialProcessComment';
272 -function setupSpecialProcessComment() {
273 - global $IP, $wgMessageCache;
274 - require_once($IP.'/includes/SpecialPage.php');
275 - SpecialPage::addPage(new SpecialPage('ProcessComment', '', true, 'specialProcessComment', false));
276 -
277 - # Messages used in this extension
278 - $wgMessageCache->addMessage('article-comments-title-field', 'Title');
279 - $wgMessageCache->addMessage('article-comments-name-string', 'Name');
280 - $wgMessageCache->addMessage('article-comments-name-field', 'Name (required): ');
281 - $wgMessageCache->addMessage('article-comments-url-field', 'Website: ');
282 - $wgMessageCache->addMessage('article-comments-comment-string', 'Comment');
283 - $wgMessageCache->addMessage('article-comments-comment-field', 'Comment: ');
284 - $wgMessageCache->addMessage('article-comments-submit-button', 'Submit');
285 - $wgMessageCache->addMessage('article-comments-leave-comment-link', 'Leave a comment ...');
286 - $wgMessageCache->addMessage('article-comments-invalid-field', 'The $1 provided <nowiki>[$2]</nowiki> is invalid.');
287 - $wgMessageCache->addMessage('article-comments-required-field', '$1 field is required.');
288 - $wgMessageCache->addMessage('article-comments-submission-failed', 'Comment Submission Failed');
289 - $wgMessageCache->addMessage('article-comments-failure-reasons', 'Sorry, your comment submission failed for the following reason(s):');
290 - $wgMessageCache->addMessage('article-comments-no-comments', 'Sorry, the article &quot;[[$1]]&quot; is not accepting comments at this time.');
291 - $wgMessageCache->addMessage('article-comments-talk-page-starter', "<noinclude>Comments on [[$1]]\n<comments />\n----- __NOEDITSECTION__</noinclude>\n");
292 - $wgMessageCache->addMessage('article-comments-commenter-said', '$1 said ...');
293 - $wgMessageCache->addMessage('article-comments-summary', 'Comment provided by $1 - via ArticleComments extension');
294 - $wgMessageCache->addMessage('article-comments-submission-succeeded', 'Comment submission succeeded');
295 - $wgMessageCache->addMessage('article-comments-submission-success', 'You have successfully submitted a comment for [[$1]]');
296 - $wgMessageCache->addMessage('article-comments-submission-view-all', 'You may view all comments on that article [[$1|here]]');
297 - $wgMessageCache->addMessage('article-comments-prefilled-comment-text', '');
298 - $wgMessageCache->addMessage('article-comments-user-is-blocked', 'Your user account is currently blocked from editing [[$1]].');
299 - $wgMessageCache->addMessage('article-comments-new-comment', "\n== \$1 ==\n\n<div class='commentBlock'>\n\$2\n\n--\$3 \$4\n</div>\n");
300 - $wgMessageCache->addMessage('article-comments-no-spam', "At least one of the submitted fields was flagged as spam.");
301 - $wgMessageCache->addMessage('processcomment', 'Process Article Comment');
302 -}
303 -
304292 /**
305293 * Special page for comment processing.
306294 */
307295 function specialProcessComment() {
308296
309 - global $wgOut, $wgParser, $wgUser, $wgContentLang, $wgContLang;
310 - $wcl = ($wgContentLang ? $wgContentLang : $wgContLang);
 297+ global $wgOut, $wgParser, $wgUser, $wgContLang;
311298
312299 # Retrieve submitted values
313300 $titleKey = $_POST['titleKey'];
@@ -412,7 +399,7 @@
413400 }
414401
415402 # Determine signature components
416 - $d = $wcl->timeanddate( date( 'YmdHis' ), false, false) . ' (' . date( 'T' ) . ')';
 403+ $d = $wgContLang->timeanddate( date( 'YmdHis' ), false, false) . ' (' . date( 'T' ) . ')';
417404 if ($commenterURL && $commenterURL!='http://') $sigText = "[$commenterURL $commenterName]";
418405 else if ($wgUser->isLoggedIn()) $sigText = $wgParser->getUserSig( $wgUser );
419406 else $sigText = $commenterName;
@@ -425,6 +412,7 @@
426413 $sigText,
427414 $d
428415 );
 416+
429417 $posAbove = stripos( $talkContent, '<!--COMMENTS_ABOVE-->' );
430418 if ($posAbove===false) $posBelow = stripos( $talkContent, '<!--COMMENTS_BELOW-->' );
431419 if ($posAbove!==false) {
@@ -501,5 +489,3 @@
502490 # We made it this far, leave $isspam alone and give other implementors a chance.
503491 return true;
504492 }
505 -
506 -//</source>
Index: trunk/extensions/ArticleComments/ArticleComments.i18n.php
@@ -0,0 +1,37 @@
 2+<?php
 3+/**
 4+ * Internationalisation file for ArticleComments extension.
 5+ *
 6+ * @file
 7+ * @ingroup Extensions
 8+ */
 9+
 10+$messages = array();
 11+
 12+$messages['en'] = array(
 13+ 'article-comments-title-field' => 'Title',
 14+ 'article-comments-name-string' => 'Name',
 15+ 'article-comments-name-field' => 'Name (required): ',
 16+ 'article-comments-url-field' => 'Website: ',
 17+ 'article-comments-comment-string' => 'Comment',
 18+ 'article-comments-comment-field' => 'Comment: ',
 19+ 'article-comments-submit-button' => 'Submit',
 20+ 'article-comments-leave-comment-link' => 'Leave a comment ...',
 21+ 'article-comments-invalid-field' => 'The $1 provided <nowiki>[$2]</nowiki> is invalid.',
 22+ 'article-comments-required-field' => '$1 field is required.',
 23+ 'article-comments-submission-failed' => 'Comment Submission Failed',
 24+ 'article-comments-failure-reasons' => 'Sorry, your comment submission failed for the following reason(s):',
 25+ 'article-comments-no-comments' => 'Sorry, the article &quot;[[$1]]&quot; is not accepting comments at this time.',
 26+ 'article-comments-talk-page-starter' => "<noinclude>Comments on [[$1]]\n<comments />\n----- __NOEDITSECTION__</noinclude>\n",
 27+ 'article-comments-commenter-said' => '$1 said ...',
 28+ 'article-comments-summary' => 'Comment provided by $1 - via ArticleComments extension',
 29+ 'article-comments-submission-succeeded' => 'Comment submission succeeded',
 30+ 'article-comments-submission-success' => 'You have successfully submitted a comment for [[$1]]',
 31+ 'article-comments-submission-view-all' => 'You may view all comments on that article [[$1|here]]',
 32+ 'article-comments-prefilled-comment-text' => '',
 33+ 'article-comments-user-is-blocked' => 'Your user account is currently blocked from editing [[$1]].',
 34+ 'article-comments-new-comment' => "\n== \$1 ==\n\n<div class='commentBlock'>\n\$2\n\n--\$3 \$4\n</div>\n",
 35+ 'article-comments-no-spam' => 'At least one of the submitted fields was flagged as spam.',
 36+ 'processcomment' => 'Process Article Comment',
 37+);
 38+
Property changes on: trunk/extensions/ArticleComments/ArticleComments.i18n.php
___________________________________________________________________
Added: svn:eol-style
139 + native

Follow-up revisions

RevisionCommit summaryAuthorDate
r74152Always use doEdit()....platonides21:13, 2 October 2010
r74161&quot; -> "...platonides21:53, 2 October 2010

Comments

#Comment by Raymond (talk | contribs)   11:40, 30 September 2010

A short i18n review from Translatewiki view:

+    'article-comments-name-field' => 'Name (required): ',
+    'article-comments-url-field' => 'Website: ',
+    'article-comments-comment-field' => 'Comment: ',

Trailing whitespaces are trimmed automatically.

+    'article-comments-invalid-field' => 'The $1 provided [$2] is invalid.',

(and more messages) What is $1, $2? Please add explanations in 'qqq' pseudo language section.

+    'article-comments-no-comments' => 'Sorry, the article "$1" is not accepting comments at this time.',

Please use UTF-8 characters.

+    'article-comments-talk-page-starter' => "Comments on $1\n<comments />\n----- \n",

The wiki syntax should be added to the programm code. I suppose that it should not be changed for individual languages.


#Comment by Nikerabbit (talk | contribs)   14:46, 30 September 2010
+    $d = $wgContLang->timeanddate( date( 'YmdHis' ), false, false) . ' (' . date( 'T' ) . ')';

wfTimestampNow() instead of date( 'YmdHis' )?

#Comment by Platonides (talk | contribs)   21:55, 2 October 2010

Most of this has been addressed in r74152.

The &quot; to " was done in r74161 (since CR resolved the entity, it took me a while to realise the problem)

Raymond, article-comments-talk-page-starter should be customizable. Note that it is a content message.

#Comment by Raymond (talk | contribs)   08:35, 3 October 2010

The text content of 'article-comments-talk-page-starter' ("Comments on $1") should be translated, sure. But what about the surrounding wiki syntax? noinclude, comments /, NOEDITSECTION etc? I think this should not be changed by the translators? And therefore it can be moved to the program code?

#Comment by Platonides (talk | contribs)   18:12, 3 October 2010

Maybe not by the translators, but would certainly be useful for customization via the MediaWiki namespace. We may need more fine grained messages here.

#Comment by Nikerabbit (talk | contribs)   11:03, 25 December 2010

At least the trailing whitespace must be removed.

Status & tagging log