r73892 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r73891‎ | r73892 | r73893 >
Date:15:16, 28 September 2010
Author:jimbojw
Status:deferred (Comments)
Tags:
Comment:
Importing (with permssion) ArticleComments Extension by Jim R. Wilson

Import of ArticleComments v0.1 alpha from http://jimbojw.com/wiki/index.php?title=ArticleComments&oldid=1564

svn:author jimbojw
Modified paths:
  • /trunk/extensions/ArticleComments (added) (history)
  • /trunk/extensions/ArticleComments/ArticleComments.php (added) (history)

Diff [purge]

Index: trunk/extensions/ArticleComments/ArticleComments.php
@@ -0,0 +1,268 @@
 2+<?php //{{MediaWikiExtension}}<source lang="php">
 3+/*
 4+ * ArticleComments.php - A MediaWiki extension for adding comment sections to articles.
 5+ * @author Jim R. Wilson
 6+ * @version 0.1
 7+ * @copyright Copyright (C) 2007 Jim R. Wilson
 8+ * @license The MIT License - http://www.opensource.org/licenses/mit-license.php
 9+ * -----------------------------------------------------------------------
 10+ * Description:
 11+ * This is a MediaWiki (http://www.mediawiki.org/) extension which adds support
 12+ * for comment sections within article pages
 13+ * Requirements:
 14+ * This extension is made to work with MediaWiki 1.6.x, 1.8.x or 1.9.x running against
 15+ * PHP 4.3.x, 5.x or higher.
 16+ * Installation:
 17+ * 1. Drop this script (ArticleComments.php) in $IP/extensions
 18+ * Note: $IP is your MediaWiki install dir.
 19+ * 2. Enable the extension by adding this line to your LocalSettings.php:
 20+ * require_once('extensions/ArticleComments.php');
 21+ * Usage:
 22+ * Once installed, you may utilize ArticleComments by adding the following flag in the article text:
 23+ * <comments />
 24+ * Note: Typically this would be placed at the end of the article text.
 25+ * -----------------------------------------------------------------------
 26+ * Copyright (c) 2007 Jim R. Wilson
 27+ *
 28+ * Permission is hereby granted, free of charge, to any person obtaining a copy
 29+ * of this software and associated documentation files (the "Software"), to deal
 30+ * in the Software without restriction, including without limitation the rights to
 31+ * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 32+ * the Software, and to permit persons to whom the Software is furnished to do
 33+ * so, subject to the following conditions:
 34+ *
 35+ * The above copyright notice and this permission notice shall be included in all
 36+ * copies or substantial portions of the Software.
 37+ *
 38+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 39+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 40+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 41+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 42+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 43+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 44+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 45+ * OTHER DEALINGS IN THE SOFTWARE.
 46+ * -----------------------------------------------------------------------
 47+ */
 48+
 49+# Confirm MW environment
 50+if (!defined('MEDIAWIKI')) die();
 51+
 52+# Credits
 53+$wgExtensionCredits['other'][] = array(
 54+ 'name'=>'ArticleComments',
 55+ 'author'=>'Jim R. Wilson - wilson.jim.r &lt;at&gt; gmail.com',
 56+ 'url'=>'http://jimbojw.com/wiki/index.php?title=ArticleComments',
 57+ 'description'=>'Enables comment sections on article pages.',
 58+ 'version'=>'0.1'
 59+);
 60+
 61+# Add Extension Functions
 62+$wgExtensionFunctions[] = 'wfArticleCommentsParserSetup';
 63+
 64+# Sets up the ArticleComments Parser hook for <comments />
 65+function wfArticleCommentsParserSetup() {
 66+ global $wgParser;
 67+ $wgParser->setHook( 'comments', 'wfArticleCommentsParserHook' );
 68+}
 69+function wfArticleCommentsParserHook( $text, $params = array(), &$parser ) {
 70+
 71+ global $wgScript;
 72+
 73+ $articleTitle = $parser->mTitle;
 74+
 75+ # Build out the comment form.
 76+ $content = '<div id="commentForm">';
 77+ $content .= '<form method="post" action="'.$wgScript.'?title=Special:ProcessComment">';
 78+ $content .= '<input type="hidden" id="titleKey" name="titleKey" value="'.$articleTitle->getDBKey().'" />';
 79+ $content .= '<input type="hidden" id="titleNS" name="titleNS" value="'.$articleTitle->getNamespace().'" />';
 80+ $content .= '<p>'.wfMsgForContent('article-comments-name-field').'<br /><input type="text" id="commenterName" name="commenterName" /></p>';
 81+ $content .= '<p>'.wfMsgForContent('article-comments-url-field').'<br /><input type="text" id="commenterURL" name="commenterURL" /></p>';
 82+ $content .= '<p>'.wfMsgForContent('article-comments-comment-field').'<br /><textarea id="comment" name="comment" style="width:30em" rows="5"></textarea></p>';
 83+ $content .= '<p><input id="submit" type="submit" value="'.wfMsgForContent('article-comments-submit-button').'" /></p>';
 84+ $content .= '</form>';
 85+ $content .= '</div>';
 86+
 87+ # Inline JavaScript to make form behavior more rich (must degrade gracefully in JS-disabled browsers)
 88+ $content .= '<script type="text/javascript">//<![CDATA['."\n";
 89+ $content .= '(function(){'."\n";
 90+
 91+ # Prefill the name field if the user is logged in.
 92+ $content .= 'var prefillUserName = function(){'."\n";
 93+ $content .= 'var ptu=document.getElementById("pt-userpage");'."\n";
 94+ $content .= 'if (ptu) document.getElementById("commenterName").value=';
 95+ $content .= 'ptu.getElementsByTagName("a")[0].innerHTML;'."\n";
 96+ $content .= '};'."\n";
 97+ $content .= 'if (window.addEventListener) window.addEventListener("load",prefillUserName,false);'."\n";
 98+ $content .= 'else if (window.attachEvent) window.attachEvent("onload",prefillUserName);'."\n";
 99+
 100+ # Hides the commentForm until the "Make a comment" link is clicked
 101+ $content .= 'var cf=document.getElementById("commentForm");'."\n";
 102+ $content .= 'cf.style.display="none";'."\n";
 103+ $content .= 'var p=document.createElement("p");'."\n";
 104+ $content .= 'p.innerHTML="<a href=\'javascript:void(0)\' onclick=\'';
 105+ $content .= 'document.getElementById(\\"commentForm\\").style.display=\\"block\\";';
 106+ $content .= 'this.style.display=\\"none\\";false';
 107+ $content .= '\'>'.wfMsgForContent('article-comments-leave-comment-link').'</a>";'."\n";
 108+ $content .= 'cf.parentNode.insertBefore(p,cf);'."\n";
 109+
 110+ $content .= '})();';
 111+ $content .= '//]]></script>';
 112+
 113+ # Hide content from the Parser using base64 to avoid mangling.
 114+ # Note: Content will be decoded after Tidy has finished it's processing of the page.
 115+ return '<pre>@ENCODED@'.base64_encode($content).'@ENCODED@</pre>';
 116+}
 117+
 118+# Attach Hooks
 119+$wgHooks['ParserAfterTidy'][] = 'wfProcessEncodedContent';
 120+
 121+/**
 122+ * Processes HTML comments with encoded content.
 123+ * Usage: $wgHooks['OutputPageBeforeHTML'][] = 'wfProcessEncodedContent';
 124+ * @param $out Handle to an OutputPage object (presumably $wgOut).
 125+ * @param $test Article/Output text.
 126+ */
 127+function wfProcessEncodedContent($out, $text) {
 128+ $text = preg_replace(
 129+ '/<pre>@ENCODED@([0-9a-zA-Z\\+\\/]+=*)@ENCODED@<\\/pre>/e',
 130+ 'base64_decode("$1")',
 131+ $text
 132+ );
 133+ return true;
 134+}
 135+
 136+# Sets up special page to handle comment submission
 137+$wgExtensionFunctions[] = 'setupSpecialProcessComment';
 138+function setupSpecialProcessComment() {
 139+ global $IP, $wgMessageCache;
 140+ require_once($IP . '/includes/SpecialPage.php');
 141+ SpecialPage::addPage(new SpecialPage('ProcessComment', '', true, 'specialProcessComment', false));
 142+
 143+ # Messages used in this extension
 144+ $wgMessageCache->addMessage('article-comments-title-field', 'Title');
 145+ $wgMessageCache->addMessage('article-comments-name-string', 'Name');
 146+ $wgMessageCache->addMessage('article-comments-name-field', 'Name (required): ');
 147+ $wgMessageCache->addMessage('article-comments-url-field', 'Website: ');
 148+ $wgMessageCache->addMessage('article-comments-comment-string', 'Comment');
 149+ $wgMessageCache->addMessage('article-comments-comment-field', 'Comment: ');
 150+ $wgMessageCache->addMessage('article-comments-submit-button', 'Submit');
 151+ $wgMessageCache->addMessage('article-comments-leave-comment-link', 'Leave a comment ...');
 152+ $wgMessageCache->addMessage('article-comments-invalid-field', 'The $1 provided <nowiki>[$2]</nowiki> is invalid.');
 153+ $wgMessageCache->addMessage('article-comments-required-field', '$1 field is required.');
 154+ $wgMessageCache->addMessage('article-comments-submission-failed', 'Comment Submission Failed');
 155+ $wgMessageCache->addMessage('article-comments-failure-reasons', 'Sorry, your comment submission failed for the following reason(s):');
 156+ $wgMessageCache->addMessage('article-comments-no-comments', 'Sorry, the article &quot;[[$1]]&quot; is not accepting comments at this time.');
 157+ $wgMessageCache->addMessage('article-comments-talk-page-starter', "<noinclude>Comments on [[$1]]\n<comments />\n----- __NOEDITSECTION__</noinclude>\n");
 158+ $wgMessageCache->addMessage('article-comments-commenter-said', '$1 said ...');
 159+ $wgMessageCache->addMessage('article-comments-summary', 'Comment provided by $1 - via ArticleComments extension');
 160+ $wgMessageCache->addMessage('processcomment', 'Process Article Comment');
 161+}
 162+
 163+/**
 164+* Special page for comment processing.
 165+*/
 166+function specialProcessComment() {
 167+
 168+ global $wgOut, $wgContLang, $wgParser, $wgUser;
 169+
 170+ # Check whether user is allowed to add a comment
 171+ # $wgUser->getBlockedStatus()
 172+
 173+ # Retrieve submitted values
 174+ $titleKey = $_POST['titleKey'];
 175+ $titleNS = intval($_POST['titleNS']);
 176+ $commenterName = $_POST['commenterName'];
 177+ $commenterURL = $_POST['commenterURL'];
 178+ $comment = $_POST['comment'];
 179+
 180+ # Perform validation checks on supplied fields
 181+ $ac = 'article-comments-';
 182+ $messages = array();
 183+ if (!$titleKey) $messages[] = wfMsgForContent(
 184+ $ac.'invalid-field', wfMsgForContent($ac.'title-field'), $titleKey
 185+ );
 186+ if (!$commenterName) $messages[] = wfMsgForContent(
 187+ $ac.'required-field', wfMsgForContent($ac.'name-string'));
 188+ if (!$comment) $messages[] = wfMsgForContent(
 189+ $ac.'required-field', wfMsgForContent($ac.'comment-string'));
 190+ if (!empty($messages)) {
 191+ $wgOut->setPageTitle(wfMsgForContent($ac.'submission-failed'));
 192+ $wikiText = "<div class='errorbox'>";
 193+ $wikiText .= wfMsgForContent($ac.'failure-reasons')."\n\n";
 194+ foreach ($messages as $message) {
 195+ $wikiText .= "* $message\n";
 196+ }
 197+ $wgOut->addWikiText($wikiText . "</div>");
 198+ return;
 199+ }
 200+
 201+ # Setup title and talkTitle object
 202+ $title = Title::newFromDBkey($titleKey);
 203+ $title->mNamespace = $titleNS - ($titleNS % 2);
 204+ $article = new Article($title);
 205+
 206+ $talkTitle = Title::newFromDBkey($titleKey);
 207+ $talkTitle->mNamespace = $titleNS + 1 - ($titleNS % 2);
 208+ $talkArticle = new Article($talkTitle);
 209+
 210+ # Retrieve article content
 211+ $articleContent = '';
 212+ if ( $article->exists() ) {
 213+ $articleContent = $article->getContent();
 214+ }
 215+
 216+ # Retrieve existing talk content
 217+ $talkContent = '';
 218+ if ( $talkTitle->exists() ) {
 219+ $talkContent = $talkArticle->getContent();
 220+ }
 221+
 222+ # Check whether the article or its talk page contains a <comments /> flag
 223+ if (
 224+ strpos($articleContent, '<comments />')===false
 225+ && strpos($talkContent, '<comments />')===false
 226+ ) {
 227+ $wgOut->setPageTitle(wfMsgForContent($ac.'submission-failed'));
 228+ $wgOut->addWikiText(
 229+ "<div class='errorbox'>".
 230+ wfMsgForContent($ac.'no-comments', $title->getPrefixedText()).
 231+ "</div>"
 232+ );
 233+ return;
 234+ }
 235+
 236+ # Initialize the talk page's content.
 237+ if ( $talkContent == '' ) {
 238+ $talkContent = wfMsgForContent($ac.'talk-page-starter', $title->getPrefixedText() );
 239+ }
 240+
 241+ # Determine signature components
 242+ $d = $wgContLang->timeanddate( date( 'YmdHis' ), false, false) . ' (' . date( 'T' ) . ')';
 243+ if ($commenterURL) $sigText = "[$commenterURL $commenterName]";
 244+ else $sigText = $wgParser->getUserSig( $wgUser );
 245+
 246+ # Append most recent comment
 247+ $talkContent .= "\n== ".wfMsgForContent($ac.'commenter-said', $commenterName)." ==\n\n";
 248+ $talkContent .= "<div class='commentBlock'>\n";
 249+ $talkContent .= $comment."\n\n";
 250+ $talkContent .= "--$sigText $d\n";
 251+ $talkContent .= "</div>";
 252+
 253+ # Update article
 254+ $summary = wfMsgForContent($ac.'summary', $commenterName);
 255+ if (method_exists($talkArticle, 'doEdit')) {
 256+ $talkArticle->doEdit($talkContent, $summary);
 257+ } else {
 258+ $method = ($talkArticle->exists() ? 'updateArticle' : 'insertNewArticle' );
 259+ $talkArticle->$method($talkContent, $summary, false, false);
 260+ return;
 261+ }
 262+
 263+ $wgOut->setPageTitle("Comment Submission Success!");
 264+ $wgOut->addWikiText("You have successfully commented on [[".$title->getPrefixedText()."]].\n\n");
 265+ $wgOut->addWikiText("You may view all comments on that article [[".$talkTitle->getPrefixedText()."|here]]");
 266+}
 267+
 268+//</source>
 269+?>
\ No newline at end of file
Property changes on: trunk/extensions/ArticleComments/ArticleComments.php
___________________________________________________________________
Added: svn:eol-style
1270 + native

Comments

#Comment by Platonides (talk | contribs)   16:19, 28 September 2010

Revisions 73892, 73894-73909 were committed by me and then reattributed to the original author (jimbojw).

Status & tagging log