diff options
Diffstat (limited to 'CommentStreams/includes/ApiCSPostComment.php')
-rw-r--r-- | CommentStreams/includes/ApiCSPostComment.php | 218 |
1 files changed, 79 insertions, 139 deletions
diff --git a/CommentStreams/includes/ApiCSPostComment.php b/CommentStreams/includes/ApiCSPostComment.php index c37d6a3d..bbede380 100644 --- a/CommentStreams/includes/ApiCSPostComment.php +++ b/CommentStreams/includes/ApiCSPostComment.php @@ -24,95 +24,116 @@ namespace MediaWiki\Extension\CommentStreams; use ApiBase; -use ApiMessage; -use EchoEvent; -use ExtensionRegistry; +use ApiMain; +use ApiUsageException; use ManualLogEntry; -use WikiPage; +use MediaWiki\Linker\LinkTarget; +use MediaWiki\MediaWikiServices; +use MWException; +use Title; class ApiCSPostComment extends ApiBase { + /** + * @var CommentStreamsFactory + */ + private $commentStreamsFactory; + + /** + * @var CommentStreamsEchoInterface + */ + private $echoInterface; /** * @param ApiMain $main main module * @param string $action name of this module */ - public function __construct( $main, $action ) { + public function __construct( ApiMain $main, string $action ) { parent::__construct( $main, $action ); + $services = MediaWikiServices::getInstance(); + $this->commentStreamsFactory = $services->getService( 'CommentStreamsFactory' ); + $this->echoInterface = $services->getService( 'CommentStreamsEchoInterface' ); } /** * execute the API request + * @throws ApiUsageException + * @throws MWException */ public function execute() { - if ( !in_array( 'cs-comment', $this->getUser()->getRights() ) || - $this->getUser()->isBlocked() ) { - $this->dieCustomUsageMessage( - 'commentstreams-api-error-post-permissions' ); + if ( !CommentStreamsUtils::userHasRight( $this->getUser(), 'cs-comment' ) ) { + $this->dieWithError( 'commentstreams-api-error-post-permissions' ); } $associatedid = $this->getMain()->getVal( 'associatedid' ); $parentid = $this->getMain()->getVal( 'parentid' ); $comment_title = $this->getMain()->getVal( 'commenttitle' ); $wikitext = $this->getMain()->getVal( 'wikitext' ); + $commentblockid = $this->getMain()->getVal( 'commentblockid' ); if ( $parentid === null && $comment_title === null ) { - $this->dieCustomUsageMessage( - 'commentstreams-api-error-missingcommenttitle' ); + $this->dieWithError( 'commentstreams-api-error-missingcommenttitle' ); } if ( $parentid !== null && $comment_title !== null ) { - $this->dieCustomUsageMessage( - 'commentstreams-api-error-post-parentandtitle' ); + $this->dieWithError( 'commentstreams-api-error-post-parentandtitle' ); } + $parent_comment_title = null; if ( $parentid !== null ) { - $parent_page = WikiPage::newFromId( $parentid ); + $parentid = (int)$parentid; + $parent_page = CommentStreamsUtils::newWikiPageFromId( $parentid ); if ( $parent_page === null || !$parent_page->getTitle()->exists() ) { - $this->dieCustomUsageMessage( - 'commentstreams-api-error-post-parentpagedoesnotexist' ); - } - $parent_comment = Comment::newFromWikiPage( $parent_page ); - if ( $parent_comment->getAssociatedId() !== (int)$associatedid ) { - $this->dieCustomUsageMessage( - 'commentstreams-api-error-post-associatedpageidmismatch' ); + $this->dieWithError( 'commentstreams-api-error-post-parentpagedoesnotexist' ); + } else { + $parent_comment = $this->commentStreamsFactory->newFromWikiPage( $parent_page ); + if ( $parent_comment->getAssociatedId() !== (int)$associatedid ) { + $this->dieWithError( 'commentstreams-api-error-post-associatedpageidmismatch' ); + } + $parent_comment_title = $parent_comment->getCommentTitle(); } } - $associated_page = WikiPage::newFromId( $associatedid ); + $associatedid = (int)$associatedid; + $associated_page = CommentStreamsUtils::newWikiPageFromId( $associatedid ); if ( $associated_page === null || !$associated_page->getTitle()->exists() ) { - $this->dieCustomUsageMessage( - 'commentstreams-api-error-post-associatedpagedoesnotexist' ); - } - - $comment = Comment::newFromValues( $associatedid, $parentid, - $comment_title, $wikitext, $this->getUser() ); - if ( !$comment ) { - $this->dieCustomUsageMessage( 'commentstreams-api-error-post' ); - } - - $title = $comment->getWikiPage()->getTitle(); - if ( $comment->getParentId() === null ) { - $this->logAction( 'comment-create', $title ); + $this->dieWithError( 'commentstreams-api-error-post-associatedpagedoesnotexist' ); } else { - $this->logAction( 'reply-create', $title ); - } - - $json = $comment->getJSON(); - if ( ExtensionRegistry::getInstance()->isLoaded( 'Echo' ) && - $comment->getParentId() === null - ) { - $json['watching'] = 1; + $comment = $this->commentStreamsFactory->newFromValues( + $commentblockid, + $associatedid, + $parentid, + $comment_title, + $wikitext, + $this->getUser() + ); + + if ( !$comment ) { + $this->dieWithError( 'commentstreams-api-error-post' ); + } else { + $title = $comment->getTitle(); + if ( $comment->getParentId() === null ) { + $this->logAction( 'comment-create', $title ); + } else { + $this->logAction( 'reply-create', $title ); + } + + $this->getResult()->addValue( null, $this->getModuleName(), $comment->getId() ); + + $this->echoInterface->sendNotifications( + $comment, + $associated_page, + $this->getUser(), + $parent_comment_title ?: $comment_title + ); + } } - $this->getResult()->addValue( null, $this->getModuleName(), $json ); - - $this->sendNotifications( $comment, $associated_page ); } /** * @return array allowed parameters */ - public function getAllowedParams() { + public function getAllowedParams() : array { return [ 'commenttitle' => [ ApiBase::PARAM_TYPE => 'string', @@ -129,6 +150,10 @@ class ApiCSPostComment extends ApiBase { 'parentid' => [ ApiBase::PARAM_TYPE => 'integer', ApiBase::PARAM_REQUIRED => false + ], + 'commentblockid' => [ + ApiBase::PARAM_TYPE => 'string', + ApiBase::PARAM_REQUIRED => false ] ]; } @@ -136,106 +161,21 @@ class ApiCSPostComment extends ApiBase { /** * @return string indicates that this API module requires a CSRF token */ - public function needstoken() { + public function needstoken() : string { return 'csrf'; } /** - * Send Echo notifications if Echo is installed. - * - * @param Comment $comment the comment to send notifications for - * @param WikiPage $associated_page the associated page for the comment - * @return not used - */ - private function sendNotifications( $comment, $associated_page ) { - if ( !ExtensionRegistry::getInstance()->isLoaded( 'Echo' ) ) { - return; - } - - $parent_id = $comment->getParentId(); - if ( $parent_id === null ) { - $comment_title = $comment->getCommentTitle(); - } else { - $parent_page = WikiPage::newFromId( $parent_id ); - if ( $parent_page === null ) { - return; - } - $parent_comment = Comment::newFromWikiPage( $parent_page ); - if ( $parent_comment === null ) { - return; - } else { - $comment_title = $parent_comment->getCommentTitle(); - } - } - - $associated_page_display_title = - $associated_page->getTitle()->getPrefixedText(); - if ( class_exists( 'PageProps' ) ) { - $associated_title = $associated_page->getTitle(); - $values = \PageProps::getInstance()->getProperties( $associated_title, - 'displaytitle' ); - if ( array_key_exists( $associated_title->getArticleID(), $values ) ) { - $associated_page_display_title = - $values[$associated_title->getArticleID()]; - } - } - - $extra = [ - 'comment_id' => $comment->getId(), - 'parent_id' => $comment->getParentId(), - 'comment_author_username' => $comment->getUsername(), - 'comment_author_display_name' => $comment->getUserDisplayNameUnlinked(), - 'comment_title' => $comment_title, - 'associated_page_display_title' => $associated_page_display_title, - 'comment_wikitext' => $comment->getWikitext() - ]; - - if ( $parent_id !== null ) { - EchoEvent::create( [ - 'type' => 'commentstreams-reply-on-watched-page', - 'title' => $associated_page->getTitle(), - 'extra' => $extra, - 'agent' => $this->getUser() - ] ); - EchoEvent::create( [ - 'type' => 'commentstreams-reply-to-watched-comment', - 'title' => $associated_page->getTitle(), - 'extra' => $extra, - 'agent' => $this->getUser() - ] ); - } else { - EchoEvent::create( [ - 'type' => 'commentstreams-comment-on-watched-page', - 'title' => $associated_page->getTitle(), - 'extra' => $extra, - 'agent' => $this->getUser() - ] ); - } - } - - /** * log action * @param string $action the name of the action to be logged - * @param string|null $title the title of the page for the comment that the - * action was performed upon + * @param LinkTarget|Title $target the title of the page for the comment that the + * action was performed upon, if different from the current comment + * @throws MWException */ - protected function logAction( $action, $title ) { + protected function logAction( string $action, $target ) { $logEntry = new ManualLogEntry( 'commentstreams', $action ); $logEntry->setPerformer( $this->getUser() ); - $logEntry->setTarget( $title ); - $logid = $logEntry->insert(); - } - - /** - * die with a custom usage message - * @param string $message_name the name of the custom message - */ - private function dieCustomUsageMessage( $message_name ) { - $error_message = wfMessage( $message_name ); - $this->dieUsageMsg( - [ - ApiMessage::create( $error_message ) - ] - ); + $logEntry->setTarget( $target ); + $logEntry->insert(); } } |