http://www.mediawiki.org/wiki/Special:Code/MediaWiki/55132
Revision: 55132 Author: tbleher Date: 2009-08-16 09:52:10 +0000 (Sun, 16 Aug 2009) Log Message: ----------- Add initial version of Commentbox extension Added Paths: ----------- trunk/extensions/Commentbox/ trunk/extensions/Commentbox/Commentbox.i18n.php trunk/extensions/Commentbox/Commentbox.php trunk/extensions/Commentbox/SpecialAddComment_body.php Added: trunk/extensions/Commentbox/Commentbox.i18n.php =================================================================== --- trunk/extensions/Commentbox/Commentbox.i18n.php (rev 0) +++ trunk/extensions/Commentbox/Commentbox.i18n.php 2009-08-16 09:52:10 UTC (rev 55132) @@ -0,0 +1,45 @@ +<?php +/** + * Internationalisation file for extension Commentbox. + * + * @addtogroup Extensions + */ + +if (!defined('MEDIAWIKI')) die(); + +$messages = array(); + +$messages['en'] = array( + 'commentbox-desc' => 'Adds a commentbox to certain pages', + 'commentbox-prefill' => '', + 'commentbox-intro' => '== Add a comment... == +You have a comment on this page? Add it here or <span class="plainlinks">[{{fullurl:{{FULLPAGENAME}}|action=edit}} edit the page directly]</span>.', + 'commentbox-savebutton' => 'Save comment', + 'commentbox-name' => 'Name:', + 'commentbox-name-explanation' => '<small>(Tip: If you [[Special:Userlogin|log in]], you won\'t have to fill in your name here manually)</small>', + 'commentbox-log' => 'New Comments', + 'commentbox-first-comment-heading' => '== Comments ==', + 'commentbox-regex' => '/\n==\s*Comments\s*==\s*\n/i', # should match the above heading + 'commentbox-errorpage-title' => 'Error while creating comment', + 'commentbox-error-page-nonexistent' => 'This page does not exist!', + 'commentbox-error-namespace' => 'Comments are not allowed in this namespace!', + 'commentbox-error-empty-comment' => 'Empty comments are not allowed!', +); + +$messages['de'] = array( + 'commentbox-desc' => 'Fügt in bestimmte Seiten ein Kommentarfeld ein', + 'commentbox-prefill' => '', + 'commentbox-intro' => '== Kommentar hinzufügen... == +Du hast einen Kommentar zu dieser Seite? Trag ihn hier ein oder <span class="plainlinks">[{{fullurl:{{FULLPAGENAME}}|action=edit}} bearbeite die Seite direkt]</span>.', + 'commentbox-savebutton' => 'Kommentar speichern', + 'commentbox-name' => 'Name:', + 'commentbox-name-explanation' => '<small>(Tipp: Wenn Du Dich [[Spezial:Anmelden|anmeldest]], musst Du nicht mehr hier Deinen Namen angeben)</small>', + 'commentbox-log' => 'Neuer Kommentar', + 'commentbox-first-comment-heading' => '== Kommentare ==', + 'commentbox-regex' => '/\n==\s*Kommentare\s*==\s*\n/i', + 'commentbox-errorpage-title' => 'Fehler bei der Erzeugung des Kommentars', + 'commentbox-error-page-nonexistent' => 'Die Seite existiert nicht!', + 'commentbox-error-namespace' => 'Kommentare sind in diesem Namensraum nicht erlaubt!', + 'commentbox-error-empty-comment' => 'Leere Kommentare sind nicht erlaubt!', +); + Added: trunk/extensions/Commentbox/Commentbox.php =================================================================== --- trunk/extensions/Commentbox/Commentbox.php (rev 0) +++ trunk/extensions/Commentbox/Commentbox.php 2009-08-16 09:52:10 UTC (rev 55132) @@ -0,0 +1,91 @@ +<?php +/*...@+ + * Adds a comment box to the bottom of wiki pages in predefined namespaces + * @addtogroup Extensions + * + * @link http://www.mediawiki.org/wiki/Extension:Commentbox Documentation + * + * + * @author Thomas Bleher <thomasble...@gmx.de> + * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later + */ +if (!defined('MEDIAWIKI')) die(); + +$wgExtensionCredits['other'][] = array( + 'name' => 'Commentbox', + 'path' => __FILE__, + 'author' => '[http://spiele.j-crew.de Thomas Bleher]', + 'version' => '0.1', + 'url' => 'http://www.mediawiki.org/wiki/Extension:Commentbox', + 'description' => 'Adds a commentbox to certain pages', + 'descriptionmsg' => 'commentbox-desc', +); + +# Configuration parameters +$wgCommentboxNamespaces = array ( + NS_MAIN => true +); +$wgCommentboxRows = 5; +$wgCommentboxColumns = 80; + +$wgExtensionMessagesFiles['Commentbox'] = dirname(__FILE__) . '/Commentbox.i18n.php'; +$wgSpecialPages['AddComment'] = 'SpecialAddComment'; +$wgAutoloadClasses['SpecialAddComment'] = dirname( __FILE__ ) . '/SpecialAddComment_body.php'; +$wgHooks['OutputPageBeforeHTML'][] = 'wfExtensionCommentbox_Add'; + +function wfExtensionCommentbox_Add( &$op, &$text ) { + global $wgUser, $wgArticle, $wgRequest, $action, $wgTitle, + $wgCommentboxNamespaces, $wgCommentboxRows, + $wgCommentboxColumns; + + if( !$wgTitle->exists() ) + return true; + + if( !$wgTitle->userCan('edit', true) ) + return true; + if( !array_key_exists( $wgTitle->getNamespace(), $wgCommentboxNamespaces ) + || !$wgCommentboxNamespaces[ $wgTitle->getNamespace() ] ) + return true; + if( !( $action == 'view' || $action == 'purge' || $action == 'submit' ) ) + return true; + if( $wgRequest->getCheck( 'wpPreview' ) + || $wgRequest->getCheck( 'wpLivePreview' ) + || $wgRequest->getCheck( 'wpDiff' ) ) + return true; + if( !is_null( $wgRequest->getVal( 'preview' ) ) ) + return true; + if( !is_null( $wgRequest->getVal( 'diff' ) ) ) + return true; + + $newaction = Title::newFromText( 'AddComment', NS_SPECIAL )->escapeFullURL(); + $name = ''; + if ( !$wgUser->isLoggedIn() ) { + $namecomment = wfMsgExt( 'commentbox-name-explanation', 'parseinline' ); + $namelabel = wfMsgExt( 'commentbox-name', 'parseinline' ); + $name = '<br />' . $namelabel; + $name .= ' <input name="wpAuthor" tabindex="2" type="text" size="30" maxlength="50" /> '; + $name .= $namecomment; + } + $inhalt = wfMsgNoTrans( 'commentbox-prefill' ); + $save = wfMsgExt( 'commentbox-savebutton', 'escapenoentities' ); + $texttitle = htmlspecialchars( Title::makeName( $wgTitle->getNamespace(), $wgTitle->getText() ) ); + + $intro = wfMsgExt( 'commentbox-intro', 'parse' ); + + $text .= <<<END + <form id="commentform" name="commentform" method="post" + action="$newaction" enctype="multipart/form-data"> + $intro + <textarea tabindex='1' accesskey="," name="wpComment" id="wpComment" + rows='$wgCommentboxRows' cols='$wpCommentboxColumns' + >$inhalt</textarea> + $name + <br /> + <input type="hidden" name="wpPageName" value="$texttitle" /> + <input id="wpSave" name="wpSave" type="submit" tabindex="3" value="$save" + accesskey="s" title="$save [alt-s]" /> + </form> +END; + return true; +} + Added: trunk/extensions/Commentbox/SpecialAddComment_body.php =================================================================== --- trunk/extensions/Commentbox/SpecialAddComment_body.php (rev 0) +++ trunk/extensions/Commentbox/SpecialAddComment_body.php 2009-08-16 09:52:10 UTC (rev 55132) @@ -0,0 +1,120 @@ +<?php +/** + * Specialpage for the Commentbox extension. + * + * @addtogroup Extensions + */ + +if (!defined('MEDIAWIKI')) die(); + +class SpecialAddComment extends UnlistedSpecialPage { + public function __construct() { + parent::__construct( 'AddComment', 'edit' ); + } + + public function execute( $par ) { + global $wgUser, $wgRequest, $wgOut, $wgCommentboxNamespaces; + if( !$wgRequest->wasPosted() ) { + $wgOut->redirect( Title::newMainPage()->getFullURL() ); + return; + } + $this->setHeaders(); + wfLoadExtensionMessages( 'Commentbox' ); + + if( !$this->userCanExecute( $wgUser ) ){ + $this->displayRestrictionError(); + return; + } + $Pagename = $wgRequest->getText( 'wpPageName' ); + $Author = $wgRequest->getText( 'wpAuthor', '' ); + $Comment = $wgRequest->getText( 'wpComment', '' ); + $title = Title::newFromText( $Pagename ); + if( $title == NULL || !$title->exists() ) { + $this->fail( 'commentbox-error-page-nonexistent' ); + return; + } + + if( !array_key_exists( $title->getNamespace(), $wgCommentboxNamespaces ) + || !$wgCommentboxNamespaces[ $title->getNamespace() ] ) { + $this->fail( 'commentbox-error-namespace', $title ); + return; + } + + if( $Comment == '' || $Comment == wfMsgNoTrans( 'commentbox-prefill' ) ) { + $this->fail( 'commentbox-error-empty-comment', $title ); + return; + } + + if( !$title->userCan( 'edit' ) ) { + $this->displayRestrictionError(); + return; + } + + // TODO: Integrate with SpamBlacklist etc. + // Currently, no http/https-links are allowed at all + $matches = array(); + if( preg_match( '@https?://[-.\w]+@', $Comment, $matches ) || + preg_match( '@https?://[-.\w]+@', $Author, $matches ) ) { + $wgOut->setPageTitle( wfMsg( 'spamprotectiontitle' ) ); + $wgOut->setRobotPolicy( 'noindex,nofollow' ); + $wgOut->setArticleRelated( false ); + + $wgOut->addWikiText( wfMsg( 'spamprotectiontext' ) ); + $wgOut->addWikiText( wfMsg( 'spamprotectionmatch', "<nowiki>{$matches[0]}</nowiki>" ) ); + $wgOut->returnToMain( false, $wgTitle ); + return; + } + + $article = new Article( $title ); + $text = $article->getContent(); + $subject = ''; + if( !preg_match( wfMsgForContentNoTrans( 'commentbox-regex' ), $text ) ) + $subject = wfMsgForContent( 'commentbox-first-comment-heading' )."\n"; + $sig = $wgUser->isLoggedIn() ? "-- ~~~~" : "-- $Author ~~~~~"; + // Append <br/> after each newline, except if the user started a new paragraph + $Comment = preg_replace( '/(?<!\n)\n(?!\n)/', "<br/>\n", $Comment ); + $text .= "\n\n".$subject.$Comment."\n<br/>".$sig; + try { + $req = new FauxRequest(array( + 'action' => 'edit', + 'title' => $title->getPrefixedText(), + 'text' => $text, + 'summary' => wfMsgForContent( 'commentbox-log' ), + 'token' => $wgUser->editToken(), + ), true); + $api = new ApiMain($req, true); + $api->execute(); + wfDebug("Completed API-Save\n"); + // we only reach this point if Api doesn't throw an exception + $data = $api->getResultData(); + if ($data['edit']['result'] == 'Failure') { + $spamurl = $data['edit']['spamblacklist']; + if ($spamurl != '') + throw new Exception("Die Seite enthaelt die Spam-Url ``{$spamurl}''"); + else + throw new Exception("Unbekannter Fehler"); + } + } catch (Exception $e) { + global $wgOut; + $wgOut->setPageTitle( wfMsg( 'commentbox-errorpage-title' ) ); + $wgOut->addHTML("<div class='errorbox'>". htmlspecialchars($e->getMessage()). "</div><br clear='both' />" ); + if( $title != NULL ) + $wgOut->returnToMain( false, $title ); + return; + } + + $wgOut->redirect( $title->getFullURL() ); + return; + } + + function fail( $str, $title = NULL ) { + global $wgOut; + $wgOut->setPageTitle( wfMsg( 'commentbox-errorpage-title' ) ); + $wgOut->addWikiText("<div class='errorbox'>". wfMsg( $str ). "</div><br clear='both' />" ); + if( $title != NULL ) + $wgOut->returnToMain( false, $title ); + return; + } + +} + _______________________________________________ MediaWiki-CVS mailing list MediaWiki-CVS@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs