r55132 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r55131‎ | r55132 | r55133 >
Date:09:52, 16 August 2009
Author:tbleher
Status:deferred
Tags:
Comment:
Add initial version of Commentbox extension
Modified paths:
  • /trunk/extensions/Commentbox (added) (history)
  • /trunk/extensions/Commentbox/Commentbox.i18n.php (added) (history)
  • /trunk/extensions/Commentbox/Commentbox.php (added) (history)
  • /trunk/extensions/Commentbox/SpecialAddComment_body.php (added) (history)

Diff [purge]

Index: trunk/extensions/Commentbox/Commentbox.i18n.php
@@ -0,0 +1,45 @@
 2+<?php
 3+/**
 4+ * Internationalisation file for extension Commentbox.
 5+ *
 6+ * @addtogroup Extensions
 7+ */
 8+
 9+if (!defined('MEDIAWIKI')) die();
 10+
 11+$messages = array();
 12+
 13+$messages['en'] = array(
 14+ 'commentbox-desc' => 'Adds a commentbox to certain pages',
 15+ 'commentbox-prefill' => '',
 16+ 'commentbox-intro' => '== Add a comment... ==
 17+You have a comment on this page? Add it here or <span class="plainlinks">[{{fullurl:{{FULLPAGENAME}}|action=edit}} edit the page directly]</span>.',
 18+ 'commentbox-savebutton' => 'Save comment',
 19+ 'commentbox-name' => 'Name:',
 20+ 'commentbox-name-explanation' => '<small>(Tip: If you [[Special:Userlogin|log in]], you won\'t have to fill in your name here manually)</small>',
 21+ 'commentbox-log' => 'New Comments',
 22+ 'commentbox-first-comment-heading' => '== Comments ==',
 23+ 'commentbox-regex' => '/\n==\s*Comments\s*==\s*\n/i', # should match the above heading
 24+ 'commentbox-errorpage-title' => 'Error while creating comment',
 25+ 'commentbox-error-page-nonexistent' => 'This page does not exist!',
 26+ 'commentbox-error-namespace' => 'Comments are not allowed in this namespace!',
 27+ 'commentbox-error-empty-comment' => 'Empty comments are not allowed!',
 28+);
 29+
 30+$messages['de'] = array(
 31+ 'commentbox-desc' => 'Fügt in bestimmte Seiten ein Kommentarfeld ein',
 32+ 'commentbox-prefill' => '',
 33+ 'commentbox-intro' => '== Kommentar hinzufügen... ==
 34+Du hast einen Kommentar zu dieser Seite? Trag ihn hier ein oder <span class="plainlinks">[{{fullurl:{{FULLPAGENAME}}|action=edit}} bearbeite die Seite direkt]</span>.',
 35+ 'commentbox-savebutton' => 'Kommentar speichern',
 36+ 'commentbox-name' => 'Name:',
 37+ 'commentbox-name-explanation' => '<small>(Tipp: Wenn Du Dich [[Spezial:Anmelden|anmeldest]], musst Du nicht mehr hier Deinen Namen angeben)</small>',
 38+ 'commentbox-log' => 'Neuer Kommentar',
 39+ 'commentbox-first-comment-heading' => '== Kommentare ==',
 40+ 'commentbox-regex' => '/\n==\s*Kommentare\s*==\s*\n/i',
 41+ 'commentbox-errorpage-title' => 'Fehler bei der Erzeugung des Kommentars',
 42+ 'commentbox-error-page-nonexistent' => 'Die Seite existiert nicht!',
 43+ 'commentbox-error-namespace' => 'Kommentare sind in diesem Namensraum nicht erlaubt!',
 44+ 'commentbox-error-empty-comment' => 'Leere Kommentare sind nicht erlaubt!',
 45+);
 46+
Index: trunk/extensions/Commentbox/SpecialAddComment_body.php
@@ -0,0 +1,120 @@
 2+<?php
 3+/**
 4+ * Specialpage for the Commentbox extension.
 5+ *
 6+ * @addtogroup Extensions
 7+ */
 8+
 9+if (!defined('MEDIAWIKI')) die();
 10+
 11+class SpecialAddComment extends UnlistedSpecialPage {
 12+ public function __construct() {
 13+ parent::__construct( 'AddComment', 'edit' );
 14+ }
 15+
 16+ public function execute( $par ) {
 17+ global $wgUser, $wgRequest, $wgOut, $wgCommentboxNamespaces;
 18+ if( !$wgRequest->wasPosted() ) {
 19+ $wgOut->redirect( Title::newMainPage()->getFullURL() );
 20+ return;
 21+ }
 22+ $this->setHeaders();
 23+ wfLoadExtensionMessages( 'Commentbox' );
 24+
 25+ if( !$this->userCanExecute( $wgUser ) ){
 26+ $this->displayRestrictionError();
 27+ return;
 28+ }
 29+ $Pagename = $wgRequest->getText( 'wpPageName' );
 30+ $Author = $wgRequest->getText( 'wpAuthor', '' );
 31+ $Comment = $wgRequest->getText( 'wpComment', '' );
 32+ $title = Title::newFromText( $Pagename );
 33+ if( $title == NULL || !$title->exists() ) {
 34+ $this->fail( 'commentbox-error-page-nonexistent' );
 35+ return;
 36+ }
 37+
 38+ if( !array_key_exists( $title->getNamespace(), $wgCommentboxNamespaces )
 39+ || !$wgCommentboxNamespaces[ $title->getNamespace() ] ) {
 40+ $this->fail( 'commentbox-error-namespace', $title );
 41+ return;
 42+ }
 43+
 44+ if( $Comment == '' || $Comment == wfMsgNoTrans( 'commentbox-prefill' ) ) {
 45+ $this->fail( 'commentbox-error-empty-comment', $title );
 46+ return;
 47+ }
 48+
 49+ if( !$title->userCan( 'edit' ) ) {
 50+ $this->displayRestrictionError();
 51+ return;
 52+ }
 53+
 54+ // TODO: Integrate with SpamBlacklist etc.
 55+ // Currently, no http/https-links are allowed at all
 56+ $matches = array();
 57+ if( preg_match( '@https?://[-.\w]+@', $Comment, $matches ) ||
 58+ preg_match( '@https?://[-.\w]+@', $Author, $matches ) ) {
 59+ $wgOut->setPageTitle( wfMsg( 'spamprotectiontitle' ) );
 60+ $wgOut->setRobotPolicy( 'noindex,nofollow' );
 61+ $wgOut->setArticleRelated( false );
 62+
 63+ $wgOut->addWikiText( wfMsg( 'spamprotectiontext' ) );
 64+ $wgOut->addWikiText( wfMsg( 'spamprotectionmatch', "<nowiki>{$matches[0]}</nowiki>" ) );
 65+ $wgOut->returnToMain( false, $wgTitle );
 66+ return;
 67+ }
 68+
 69+ $article = new Article( $title );
 70+ $text = $article->getContent();
 71+ $subject = '';
 72+ if( !preg_match( wfMsgForContentNoTrans( 'commentbox-regex' ), $text ) )
 73+ $subject = wfMsgForContent( 'commentbox-first-comment-heading' )."\n";
 74+ $sig = $wgUser->isLoggedIn() ? "-- ~~~~" : "-- $Author ~~~~~";
 75+ // Append <br/> after each newline, except if the user started a new paragraph
 76+ $Comment = preg_replace( '/(?<!\n)\n(?!\n)/', "<br/>\n", $Comment );
 77+ $text .= "\n\n".$subject.$Comment."\n<br/>".$sig;
 78+ try {
 79+ $req = new FauxRequest(array(
 80+ 'action' => 'edit',
 81+ 'title' => $title->getPrefixedText(),
 82+ 'text' => $text,
 83+ 'summary' => wfMsgForContent( 'commentbox-log' ),
 84+ 'token' => $wgUser->editToken(),
 85+ ), true);
 86+ $api = new ApiMain($req, true);
 87+ $api->execute();
 88+ wfDebug("Completed API-Save\n");
 89+ // we only reach this point if Api doesn't throw an exception
 90+ $data = $api->getResultData();
 91+ if ($data['edit']['result'] == 'Failure') {
 92+ $spamurl = $data['edit']['spamblacklist'];
 93+ if ($spamurl != '')
 94+ throw new Exception("Die Seite enthaelt die Spam-Url ``{$spamurl}''");
 95+ else
 96+ throw new Exception("Unbekannter Fehler");
 97+ }
 98+ } catch (Exception $e) {
 99+ global $wgOut;
 100+ $wgOut->setPageTitle( wfMsg( 'commentbox-errorpage-title' ) );
 101+ $wgOut->addHTML("<div class='errorbox'>". htmlspecialchars($e->getMessage()). "</div><br clear='both' />" );
 102+ if( $title != NULL )
 103+ $wgOut->returnToMain( false, $title );
 104+ return;
 105+ }
 106+
 107+ $wgOut->redirect( $title->getFullURL() );
 108+ return;
 109+ }
 110+
 111+ function fail( $str, $title = NULL ) {
 112+ global $wgOut;
 113+ $wgOut->setPageTitle( wfMsg( 'commentbox-errorpage-title' ) );
 114+ $wgOut->addWikiText("<div class='errorbox'>". wfMsg( $str ). "</div><br clear='both' />" );
 115+ if( $title != NULL )
 116+ $wgOut->returnToMain( false, $title );
 117+ return;
 118+ }
 119+
 120+}
 121+
Index: trunk/extensions/Commentbox/Commentbox.php
@@ -0,0 +1,91 @@
 2+<?php
 3+/**#@+
 4+ * Adds a comment box to the bottom of wiki pages in predefined namespaces
 5+ * @addtogroup Extensions
 6+ *
 7+ * @link http://www.mediawiki.org/wiki/Extension:Commentbox Documentation
 8+ *
 9+ *
 10+ * @author Thomas Bleher <ThomasBleher@gmx.de>
 11+ * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
 12+ */
 13+if (!defined('MEDIAWIKI')) die();
 14+
 15+$wgExtensionCredits['other'][] = array(
 16+ 'name' => 'Commentbox',
 17+ 'path' => __FILE__,
 18+ 'author' => '[http://spiele.j-crew.de Thomas Bleher]',
 19+ 'version' => '0.1',
 20+ 'url' => 'http://www.mediawiki.org/wiki/Extension:Commentbox',
 21+ 'description' => 'Adds a commentbox to certain pages',
 22+ 'descriptionmsg' => 'commentbox-desc',
 23+);
 24+
 25+# Configuration parameters
 26+$wgCommentboxNamespaces = array (
 27+ NS_MAIN => true
 28+);
 29+$wgCommentboxRows = 5;
 30+$wgCommentboxColumns = 80;
 31+
 32+$wgExtensionMessagesFiles['Commentbox'] = dirname(__FILE__) . '/Commentbox.i18n.php';
 33+$wgSpecialPages['AddComment'] = 'SpecialAddComment';
 34+$wgAutoloadClasses['SpecialAddComment'] = dirname( __FILE__ ) . '/SpecialAddComment_body.php';
 35+$wgHooks['OutputPageBeforeHTML'][] = 'wfExtensionCommentbox_Add';
 36+
 37+function wfExtensionCommentbox_Add( &$op, &$text ) {
 38+ global $wgUser, $wgArticle, $wgRequest, $action, $wgTitle,
 39+ $wgCommentboxNamespaces, $wgCommentboxRows,
 40+ $wgCommentboxColumns;
 41+
 42+ if( !$wgTitle->exists() )
 43+ return true;
 44+
 45+ if( !$wgTitle->userCan('edit', true) )
 46+ return true;
 47+ if( !array_key_exists( $wgTitle->getNamespace(), $wgCommentboxNamespaces )
 48+ || !$wgCommentboxNamespaces[ $wgTitle->getNamespace() ] )
 49+ return true;
 50+ if( !( $action == 'view' || $action == 'purge' || $action == 'submit' ) )
 51+ return true;
 52+ if( $wgRequest->getCheck( 'wpPreview' )
 53+ || $wgRequest->getCheck( 'wpLivePreview' )
 54+ || $wgRequest->getCheck( 'wpDiff' ) )
 55+ return true;
 56+ if( !is_null( $wgRequest->getVal( 'preview' ) ) )
 57+ return true;
 58+ if( !is_null( $wgRequest->getVal( 'diff' ) ) )
 59+ return true;
 60+
 61+ $newaction = Title::newFromText( 'AddComment', NS_SPECIAL )->escapeFullURL();
 62+ $name = '';
 63+ if ( !$wgUser->isLoggedIn() ) {
 64+ $namecomment = wfMsgExt( 'commentbox-name-explanation', 'parseinline' );
 65+ $namelabel = wfMsgExt( 'commentbox-name', 'parseinline' );
 66+ $name = '<br />' . $namelabel;
 67+ $name .= ' <input name="wpAuthor" tabindex="2" type="text" size="30" maxlength="50" /> ';
 68+ $name .= $namecomment;
 69+ }
 70+ $inhalt = wfMsgNoTrans( 'commentbox-prefill' );
 71+ $save = wfMsgExt( 'commentbox-savebutton', 'escapenoentities' );
 72+ $texttitle = htmlspecialchars( Title::makeName( $wgTitle->getNamespace(), $wgTitle->getText() ) );
 73+
 74+ $intro = wfMsgExt( 'commentbox-intro', 'parse' );
 75+
 76+ $text .= <<<END
 77+ <form id="commentform" name="commentform" method="post"
 78+ action="$newaction" enctype="multipart/form-data">
 79+ $intro
 80+ <textarea tabindex='1' accesskey="," name="wpComment" id="wpComment"
 81+ rows='$wgCommentboxRows' cols='$wpCommentboxColumns'
 82+ >$inhalt</textarea>
 83+ $name
 84+ <br />
 85+ <input type="hidden" name="wpPageName" value="$texttitle" />
 86+ <input id="wpSave" name="wpSave" type="submit" tabindex="3" value="$save"
 87+ accesskey="s" title="$save [alt-s]" />
 88+ </form>
 89+END;
 90+ return true;
 91+}
 92+

Status & tagging log