diff options
Diffstat (limited to 'CommentStreams/includes/Comment.php')
-rw-r--r-- | CommentStreams/includes/Comment.php | 180 |
1 files changed, 114 insertions, 66 deletions
diff --git a/CommentStreams/includes/Comment.php b/CommentStreams/includes/Comment.php index 27a8300c..755c24c3 100644 --- a/CommentStreams/includes/Comment.php +++ b/CommentStreams/includes/Comment.php @@ -21,6 +21,21 @@ * DEALINGS IN THE SOFTWARE. */ +namespace MediaWiki\Extension\CommentStreams; + +use Html; +use MediaWiki\MediaWikiServices; +use MWTimestamp; +use Parser; +use ParserOptions; +use SMWDataItem; +use SMWUpdateJob; +use Title; +use User; +use wAvatar; +use WikiPage; +use WikitextContent; + class Comment { // wiki page object for this comment wiki page @@ -73,7 +88,7 @@ class Comment { * error */ public static function newFromWikiPage( $wikipage ) { - if ( !is_null( $wikipage ) && + if ( $wikipage !== null && $wikipage->getTitle()->getNamespace() === NS_COMMENTSTREAMS ) { $comment = new Comment( $wikipage ); if ( $wikipage->exists() ) { @@ -100,10 +115,10 @@ class Comment { */ public static function newFromValues( $assoc_page_id, $parent_page_id, $comment_title, $wikitext, $user ) { - if ( is_null( $comment_title ) && is_null( $parent_page_id ) ) { + if ( $comment_title === null && $parent_page_id === null ) { return null; } - if ( !is_null( $comment_title ) && !is_null( $parent_page_id ) ) { + if ( $comment_title !== null && $parent_page_id !== null ) { return null; } $annotated_wikitext = self::addAnnotations( $wikitext, $comment_title, @@ -114,9 +129,20 @@ class Comment { $index = wfRandomString(); $title = Title::newFromText( (string)$index, NS_COMMENTSTREAMS ); if ( !$title->isDeletedQuick() && !$title->exists() ) { - if ( !$title->userCan( 'cs-comment' ) ) { - return null; + if ( class_exists( 'MediaWiki\Permissions\PermissionManager' ) ) { + // MW 1.33+ + if ( !MediaWikiServices::getInstance() + ->getPermissionManager() + ->userCan( 'cs-comment', $user, $title ) + ) { + return null; + } + } else { + if ( !$title->userCan( 'cs-comment' ) ) { + return null; + } } + $wikipage = new WikiPage( $title ); $status = $wikipage->doEditContent( $content, '', EDIT_NEW | EDIT_SUPPRESS_RC, false, $user, null ); @@ -152,15 +178,15 @@ class Comment { } $comment->loadFromValues( $assoc_page_id, $parent_page_id, $comment_title ); - if ( is_null( $parent_page_id ) ) { + if ( $parent_page_id === null ) { $comment->watch( $user ); } else { self::watchComment( $parent_page_id, $user ); } if ( defined( 'SMW_VERSION' ) ) { - $job = new SMWUpdateJob( $title ); - JobQueueGroup::singleton()->push( $job ); + $job = new SMWUpdateJob( $title, [] ); + \JobQueueGroup::singleton()->push( $job ); } return $comment; @@ -195,7 +221,7 @@ class Comment { if ( $result ) { $this->assoc_page_id = (int)$result->cst_assoc_page_id; $this->parent_page_id = $result->cst_parent_page_id; - if ( !is_null( $this->parent_page_id ) ) { + if ( $this->parent_page_id !== null ) { $this->parent_page_id = (int)$this->parent_page_id; } $this->comment_title = $result->cst_comment_title; @@ -215,7 +241,7 @@ class Comment { $comment_title ) { $this->assoc_page_id = (int)$assoc_page_id; $this->parent_page_id = $parent_page_id; - if ( !is_null( $this->parent_page_id ) ) { + if ( $this->parent_page_id !== null ) { $this->parent_page_id = (int)$this->parent_page_id; } $this->comment_title = $comment_title; @@ -271,9 +297,9 @@ class Comment { * @return string wikitext of the comment */ public function getWikiText() { - if ( is_null( $this->wikitext ) ) { - $wikitext = ContentHandler::getContentText( $this->wikipage->getContent( - Revision::RAW ) ); + if ( $this->wikitext === null ) { + $wikitext = \ContentHandler::getContentText( $this->wikipage->getContent( + \Revision::RAW ) ); $wikitext = $this->removeAnnotations( $wikitext ); $this->wikitext = $wikitext; } @@ -284,10 +310,16 @@ class Comment { * @return string parsed HTML of the comment */ public function getHTML() { - if ( is_null( $this->html ) ) { + if ( $this->html === null ) { $this->getWikiText(); - if ( !is_null( $this->wikitext ) ) { - $parser = new Parser; + if ( $this->wikitext !== null ) { + if ( class_exists( \ParserFactory::class ) ) { + // @requires MediaWiki >= 1.32.0 + $parser = MediaWikiServices::getInstance()->getParserFactory()->create(); + } else { + $parser = new Parser(); + } + $this->html = $parser->parse( $this->wikitext, $this->wikipage->getTitle(), new ParserOptions )->getText(); } @@ -299,7 +331,7 @@ class Comment { * @return User the author of this comment */ public function getUser() { - if ( is_null( $this->user ) ) { + if ( $this->user === null ) { $user_id = $this->wikipage->getOldestRevision()->getUser(); $this->user = User::newFromId( $user_id ); } @@ -342,7 +374,7 @@ class Comment { * @return string the URL of the avatar of the author of this comment */ public function getAvatar() { - if ( is_null( $this->avatar ) ) { + if ( $this->avatar === null ) { if ( class_exists( 'wAvatar' ) ) { // from Extension:SocialProfile $avatar = new wAvatar( $this->getUser()->getId(), 'l' ); @@ -359,7 +391,7 @@ class Comment { * @return MWTimestamp the earliest revision date for this */ public function getCreationTimestamp() { - if ( is_null( $this->creation_timestamp ) ) { + if ( $this->creation_timestamp === null ) { $this->creation_timestamp = MWTimestamp::getLocalInstance( $this->wikipage->getTitle()->getEarliestRevTime() ); } @@ -370,11 +402,7 @@ class Comment { * @return MWTimestamp the earliest revision date for this */ public function getCreationDate() { - if ( !is_null( $this->getCreationTimestamp() ) ) { - $user = RequestContext::getMain()->getUser(); - if ($user && !$user->isAnon()) { - $this->creation_timestamp->offsetForUser($user); - } + if ( $this->getCreationTimestamp() !== null ) { return $this->creation_timestamp->format( "M j \a\\t g:i a" ); } return ""; @@ -384,13 +412,20 @@ class Comment { * @return MWTimestamp the latest revision date for this */ public function getModificationTimestamp() { - if ( is_null( $this->modification_timestamp ) ) { + if ( $this->modification_timestamp === null ) { $title = $this->wikipage->getTitle(); if ( $title->getFirstRevision()->getId() === $title->getLatestRevID() ) { return null; } - $timestamp = Revision::getTimestampFromId( $title, - $title->getLatestRevID() ); + + $revStore = MediaWikiServices::getInstance()->getRevisionStore(); + $latestRev = $title->getLatestRevId(); + if ( version_compare( MW_VERSION, '1.34', '<' ) ) { + $timestamp = $revStore->getTimestampFromId( $title, $latestRev ); + } else { + $timestamp = $revStore->getTimestampFromId( $latestRev ); + } + $this->modification_timestamp = MWTimestamp::getLocalInstance( $timestamp ); } @@ -401,11 +436,7 @@ class Comment { * @return MWTimestamp the earliest revision date for this */ public function getModificationDate() { - if ( !is_null( $this->getModificationTimestamp() ) ) { - $user = RequestContext::getMain()->getUser(); - if ($user && !$user->isAnon()) { - $this->modification_timestamp->offsetForUser($user); - } + if ( $this->getModificationTimestamp() !== null ) { return $this->modification_timestamp->format( "M j \a\\t g:i a" ); } return null; @@ -415,7 +446,7 @@ class Comment { * @return int number of replies */ public function getNumReplies() { - if ( is_null( $this->num_replies ) ) { + if ( $this->num_replies === null ) { $dbr = wfGetDB( DB_REPLICA ); $this->num_replies = $dbr->selectRowCount( 'cs_comment_data', @@ -491,7 +522,7 @@ class Comment { * @return int number of up votes */ public function getNumUpVotes() { - if ( is_null( $this->num_up_votes ) ) { + if ( $this->num_up_votes === null ) { $dbr = wfGetDB( DB_REPLICA ); $this->num_up_votes = $dbr->selectRowCount( 'cs_votes', @@ -510,7 +541,7 @@ class Comment { * @return int number of down votes */ public function getNumDownVotes() { - if ( is_null( $this->num_down_votes ) ) { + if ( $this->num_down_votes === null ) { $dbr = wfGetDB( DB_REPLICA ); $this->num_down_votes = $dbr->selectRowCount( 'cs_votes', @@ -530,7 +561,7 @@ class Comment { * * @param string $vote 1 for up vote, -1 for down vote, 0 for no vote * @param User $user the user voting on the comment - * @return database status code + * @return bool database status code */ public function vote( $vote, $user ) { if ( $vote !== "-1" && $vote !== "0" && $vote !== "1" ) { @@ -599,7 +630,7 @@ class Comment { * watch a comment (get page ID from this comment) * * @param User $user the user watching the comment - * @return database true for OK, false for error + * @return bool database true for OK, false for error */ public function watch( $user ) { return self::watchComment( $this->getID(), $user ); @@ -608,9 +639,9 @@ class Comment { /** * watch a comment (get page ID from parameter) * - * @param $pageid the page ID of the comment to watch + * @param int $pageid the page ID of the comment to watch * @param User $user the user watching the comment - * @return database true for OK, false for error + * @return bool database true for OK, false for error */ private static function watchComment( $pageid, $user ) { if ( self::isWatchingComment( $pageid, $user ) ) { @@ -632,7 +663,7 @@ class Comment { * unwatch a comment * * @param User $user the user unwatching the comment - * @return database true for OK, false for error + * @return bool database true for OK, false for error */ public function unwatch( $user ) { if ( !$this->isWatching( $user ) ) { @@ -654,7 +685,7 @@ class Comment { * Check if a particular user is watching this comment * * @param User $user the user watching the comment - * @return database true for OK, false for error + * @return bool database true for OK, false for error */ public function isWatching( $user ) { return self::isWatchingComment( $this->getId(), $user ); @@ -663,9 +694,9 @@ class Comment { /** * Check if a particular user is watching a comment * - * @param $pageid the page ID of the comment to check + * @param int $pageid the page ID of the comment to check * @param User $user the user watching the comment - * @return database true for OK, false for error + * @return bool database true for OK, false for error */ private static function isWatchingComment( $pageid, $user ) { $dbr = wfGetDB( DB_REPLICA ); @@ -724,10 +755,10 @@ class Comment { * @return bool true if successful */ public function update( $comment_title, $wikitext, $user ) { - if ( is_null( $comment_title ) && is_null( $this->getParentId() ) ) { + if ( $comment_title === null && $this->getParentId() === null ) { return false; } - if ( !is_null( $comment_title ) && !is_null( $this->getParentId() ) ) { + if ( $comment_title !== null && $this->getParentId() !== null ) { return false; } $annotated_wikitext = @@ -765,17 +796,28 @@ class Comment { /** * delete comment from database * + * @param User $deleter * @return bool true if successful */ - public function delete() { - $pageid = $this->getId(); + public function delete( User $deleter ) { + if ( version_compare( MW_VERSION, '1.35', '<' ) ) { + $status = $this->getWikiPage()->doDeleteArticleReal( + 'comment deleted', + true + ); + } else { + $status = $this->getWikiPage()->doDeleteArticleReal( + 'comment deleted', + $deleter, + true + ); + } - $status = $this->getWikiPage()->doDeleteArticleReal( 'comment deleted', - true, 0 ); if ( !$status->isOK() && !$status->isGood() ) { return false; } + $pageid = $this->getId(); $dbw = wfGetDB( DB_MASTER ); $result = $dbw->delete( 'cs_comment_data', @@ -797,7 +839,7 @@ class Comment { */ public static function addAnnotations( $wikitext, $comment_title, $assoc_page_id ) { - if ( !is_null( $comment_title ) ) { + if ( $comment_title !== null ) { $wikitext .= <<<EOT {{DISPLAYTITLE: $comment_title @@ -815,7 +857,7 @@ EOT; */ public function removeAnnotations( $wikitext ) { $comment_title = $this->getCommentTitle(); - if ( !is_null( $comment_title ) ) { + if ( $comment_title !== null ) { $strip = <<<EOT {{DISPLAYTITLE: $comment_title @@ -849,7 +891,7 @@ EOT; $page_id = $row->cst_page_id; $wikipage = WikiPage::newFromId( $page_id ); $comment = self::newFromWikiPage( $wikipage ); - if ( !is_null( $comment ) ) { + if ( $comment !== null ) { $comments[] = $comment; } } @@ -879,7 +921,7 @@ EOT; $page_id = $row->cst_page_id; $wikipage = WikiPage::newFromId( $page_id ); $comment = self::newFromWikiPage( $wikipage ); - if ( !is_null( $comment ) ) { + if ( $comment !== null ) { $comments[] = $comment; } } @@ -905,23 +947,23 @@ EOT; } $userpage = $user->getUserPage(); $displayname = null; - if ( !is_null( $GLOBALS['wgCommentStreamsUserRealNamePropertyName'] ) ) { + if ( $GLOBALS['wgCommentStreamsUserRealNamePropertyName'] !== null ) { $displayname = self::getUserProperty( $user, $GLOBALS['wgCommentStreamsUserRealNamePropertyName'] ); } - if ( is_null( $displayname ) || strlen( $displayname ) == 0 ) { + if ( $displayname === null || strlen( $displayname ) == 0 ) { if ( class_exists( 'PageProps' ) ) { - $values = PageProps::getInstance()->getProperties( $userpage, + $values = \PageProps::getInstance()->getProperties( $userpage, 'displaytitle' ); if ( array_key_exists( $userpage->getArticleID(), $values ) ) { $displayname = $values[$userpage->getArticleID()]; } } } - if ( is_null( $displayname ) || strlen( $displayname ) == 0 ) { + if ( $displayname === null || strlen( $displayname ) == 0 ) { $displayname = $user->getRealName(); } - if ( is_null( $displayname ) || strlen( $displayname ) == 0 ) { + if ( $displayname === null || strlen( $displayname ) == 0 ) { $displayname = $user->getName(); } if ( $linked && $userpage->exists() ) { @@ -938,13 +980,13 @@ EOT; */ public static function getAvatarFromUser( $user ) { $avatar = null; - if ( !is_null( $GLOBALS['wgCommentStreamsUserAvatarPropertyName'] ) ) { + if ( $GLOBALS['wgCommentStreamsUserAvatarPropertyName'] !== null ) { $avatar = self::getUserProperty( $user, $GLOBALS['wgCommentStreamsUserAvatarPropertyName'] ); - if ( !is_null( $avatar ) ) { + if ( $avatar !== null ) { if ( gettype( $avatar ) === 'string' ) { $avatar = Title::newFromText( $avatar ); - if ( is_null( $avatar ) ) { + if ( $avatar === null ) { return null; } } @@ -952,7 +994,13 @@ EOT; return null; } if ( $avatar->isKnown() && $avatar->getNamespace() === NS_FILE ) { - $file = wfFindFile( $avatar ); + if ( method_exists( MediaWikiServices::class, 'getRepoGroup' ) ) { + // MediaWiki 1.34+ + $file = MediaWikiServices::getInstance()->getRepoGroup() + ->findFile( $avatar ); + } else { + $file = wfFindFile( $avatar ); + } if ( $file ) { return $file->getFullUrl(); } @@ -974,9 +1022,9 @@ EOT; $userpage = $user->getUserPage(); if ( $userpage->exists() ) { $store = \SMW\StoreFactory::getStore(); - $subject = SMWDIWikiPage::newFromTitle( $userpage ); + $subject = \SMWDIWikiPage::newFromTitle( $userpage ); $data = $store->getSemanticData( $subject ); - $property = SMWDIProperty::newFromUserLabel( $propertyName ); + $property = \SMWDIProperty::newFromUserLabel( $propertyName ); $values = $data->getPropertyValues( $property ); if ( count( $values ) > 0 ) { // this property should only have one value so pick the first one @@ -1002,9 +1050,9 @@ EOT; public static function locateUsersWatchingComment( $event ) { $id = $event->getExtraParam( 'parent_id' ); $wikipage = WikiPage::newFromId( $id ); - if ( !is_null( $wikipage ) ) { + if ( $wikipage !== null ) { $comment = self::newFromWikiPage( $wikipage ); - if ( !is_null( $comment ) ) { + if ( $comment !== null ) { return $comment->getWatchers(); } } |