2015-10-28 07:23:38 +00:00
|
|
|
<?php
|
2018-11-10 21:06:01 +00:00
|
|
|
|
2021-03-14 03:43:32 +00:00
|
|
|
namespace MediaWiki\Extension\Thanks;
|
|
|
|
|
|
|
|
use DatabaseLogEntry;
|
|
|
|
use LogEntry;
|
|
|
|
use LogPage;
|
2023-06-09 16:39:56 +00:00
|
|
|
use MediaWiki\Extension\Notifications\Formatters\EchoEventPresentationModel;
|
2023-05-19 10:31:13 +00:00
|
|
|
use MediaWiki\Language\RawMessage;
|
2022-12-10 21:57:25 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2024-01-04 21:22:56 +00:00
|
|
|
use MediaWiki\Parser\Sanitizer;
|
2019-10-21 14:12:06 +00:00
|
|
|
use MediaWiki\Revision\RevisionRecord;
|
2024-01-04 21:22:56 +00:00
|
|
|
use MediaWiki\SpecialPage\SpecialPage;
|
2019-10-21 14:12:06 +00:00
|
|
|
|
2018-02-21 03:46:30 +00:00
|
|
|
class EchoCoreThanksPresentationModel extends EchoEventPresentationModel {
|
2018-04-09 07:12:28 +00:00
|
|
|
/** @var LogEntry|bool|null */
|
2018-03-03 04:00:40 +00:00
|
|
|
private $logEntry;
|
|
|
|
|
2015-10-28 07:23:38 +00:00
|
|
|
public function canRender() {
|
2018-04-23 21:59:13 +00:00
|
|
|
$hasTitle = (bool)$this->event->getTitle();
|
2019-08-02 10:57:45 +00:00
|
|
|
if ( $hasTitle && $this->getThankType() === 'log' ) {
|
2018-03-03 04:00:40 +00:00
|
|
|
$logEntry = $this->getLogEntry();
|
2019-08-02 10:57:45 +00:00
|
|
|
return $logEntry && !(
|
|
|
|
// the notification renders the message on Special:Log without the comment,
|
|
|
|
// so check $logEntry is not deleted, or only its comment is deleted
|
|
|
|
$logEntry->getDeleted() & ~LogPage::DELETED_COMMENT
|
|
|
|
);
|
2018-03-03 04:00:40 +00:00
|
|
|
}
|
2018-04-23 21:59:13 +00:00
|
|
|
return $hasTitle;
|
2015-10-28 07:23:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getIconType() {
|
|
|
|
return 'thanks';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getHeaderMessage() {
|
2018-08-29 22:02:06 +00:00
|
|
|
$type = $this->getThankType();
|
2016-06-16 16:12:38 +00:00
|
|
|
if ( $this->isBundled() ) {
|
2018-04-23 21:59:13 +00:00
|
|
|
// Message is either notification-bundle-header-rev-thank
|
|
|
|
// or notification-bundle-header-log-thank.
|
2018-03-03 04:00:40 +00:00
|
|
|
$msg = $this->msg( "notification-bundle-header-$type-thank" );
|
2016-06-16 16:12:38 +00:00
|
|
|
$msg->params( $this->getBundleCount() );
|
|
|
|
$msg->params( $this->getTruncatedTitleText( $this->event->getTitle(), true ) );
|
|
|
|
$msg->params( $this->getViewingUserForGender() );
|
|
|
|
return $msg;
|
|
|
|
} else {
|
2018-09-04 18:57:59 +00:00
|
|
|
if ( $this->event->getExtraParam( 'revcreation', null ) ) {
|
|
|
|
// This is a thank on a page creation revision.
|
|
|
|
$msg = $this->getMessageWithAgent( "notification-header-creation-thank" );
|
|
|
|
} else {
|
|
|
|
// Message is either notification-header-rev-thank or notification-header-log-thank.
|
|
|
|
$msg = $this->getMessageWithAgent( "notification-header-$type-thank" );
|
|
|
|
}
|
2016-06-16 16:12:38 +00:00
|
|
|
$msg->params( $this->getTruncatedTitleText( $this->event->getTitle(), true ) );
|
|
|
|
$msg->params( $this->getViewingUserForGender() );
|
|
|
|
return $msg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-29 10:42:53 +00:00
|
|
|
public function getCompactHeaderMessage() {
|
2023-11-30 11:52:11 +00:00
|
|
|
// The following message is used here:
|
|
|
|
// * notification-compact-header-edit-thank
|
2016-06-29 10:42:53 +00:00
|
|
|
$msg = parent::getCompactHeaderMessage();
|
|
|
|
$msg->params( $this->getViewingUserForGender() );
|
|
|
|
return $msg;
|
|
|
|
}
|
|
|
|
|
2016-06-16 16:12:38 +00:00
|
|
|
public function getBodyMessage() {
|
2018-03-03 04:00:40 +00:00
|
|
|
$comment = $this->getRevOrLogComment();
|
2021-11-22 13:00:00 +00:00
|
|
|
if ( $comment !== '' ) {
|
2016-06-16 16:12:38 +00:00
|
|
|
$msg = new RawMessage( '$1' );
|
|
|
|
$msg->plaintextParams( $comment );
|
|
|
|
return $msg;
|
|
|
|
}
|
2021-11-22 13:00:00 +00:00
|
|
|
return false;
|
2016-06-16 16:12:38 +00:00
|
|
|
}
|
|
|
|
|
2021-11-22 13:00:00 +00:00
|
|
|
/**
|
|
|
|
* @return string|bool The comment or false if it could not be retrieved.
|
|
|
|
*/
|
2016-06-16 16:12:38 +00:00
|
|
|
private function getRevisionEditSummary() {
|
2019-10-21 14:12:06 +00:00
|
|
|
if ( !$this->userCan( RevisionRecord::DELETED_COMMENT ) ) {
|
2016-06-16 16:12:38 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-08-02 10:57:45 +00:00
|
|
|
$revision = $this->event->getRevision();
|
2016-06-16 16:12:38 +00:00
|
|
|
if ( !$revision ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-10-21 14:12:06 +00:00
|
|
|
$summary = $revision->getComment( RevisionRecord::RAW );
|
2019-08-02 10:57:45 +00:00
|
|
|
return $summary ? $summary->text : false;
|
2016-06-16 16:12:38 +00:00
|
|
|
}
|
|
|
|
|
2018-03-03 04:00:40 +00:00
|
|
|
/**
|
|
|
|
* Get the comment/summary/excerpt of the log entry or revision,
|
|
|
|
* for use in the notification body.
|
2021-11-22 13:00:00 +00:00
|
|
|
* @return string
|
2018-03-03 04:00:40 +00:00
|
|
|
*/
|
2021-11-22 13:00:00 +00:00
|
|
|
protected function getRevOrLogComment(): string {
|
2018-03-03 04:00:40 +00:00
|
|
|
if ( $this->event->getExtraParam( 'logid' ) ) {
|
|
|
|
$logEntry = $this->getLogEntry();
|
|
|
|
if ( !$logEntry ) {
|
|
|
|
return '';
|
|
|
|
}
|
2024-07-14 12:39:57 +00:00
|
|
|
$services = MediaWikiServices::getInstance();
|
|
|
|
$formatter = $services->getLogFormatterFactory()->newFromEntry( $logEntry );
|
2018-03-03 04:00:40 +00:00
|
|
|
$excerpt = $formatter->getPlainActionText();
|
|
|
|
// Turn wikitext into plaintext
|
2024-07-14 12:39:57 +00:00
|
|
|
$excerpt = $services->getCommentFormatter()->format( $excerpt );
|
2021-03-14 03:43:32 +00:00
|
|
|
return Sanitizer::stripAllTags( $excerpt );
|
2018-03-03 04:00:40 +00:00
|
|
|
} else {
|
|
|
|
// Try to get edit summary.
|
|
|
|
$summary = $this->getRevisionEditSummary();
|
2021-11-22 13:00:00 +00:00
|
|
|
if ( $summary !== false && $summary !== '' ) {
|
2018-03-03 04:00:40 +00:00
|
|
|
return $summary;
|
|
|
|
}
|
|
|
|
// Fallback on edit excerpt.
|
2019-10-21 14:12:06 +00:00
|
|
|
if ( $this->userCan( RevisionRecord::DELETED_TEXT ) ) {
|
2021-11-22 13:00:00 +00:00
|
|
|
return $this->event->getExtraParam( 'excerpt', '' );
|
2018-03-03 04:00:40 +00:00
|
|
|
}
|
2021-11-22 13:00:00 +00:00
|
|
|
return '';
|
2016-06-16 16:12:38 +00:00
|
|
|
}
|
2015-10-28 07:23:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getPrimaryLink() {
|
2018-04-23 04:40:54 +00:00
|
|
|
$logId = $this->event->getExtraParam( 'logid' );
|
|
|
|
if ( $logId ) {
|
|
|
|
$url = SpecialPage::getTitleFor( 'Log' )->getLocalURL( [ 'logid' => $logId ] );
|
2018-03-03 04:00:40 +00:00
|
|
|
$label = 'notification-link-text-view-logentry';
|
|
|
|
} else {
|
|
|
|
$url = $this->event->getTitle()->getLocalURL( [
|
2015-10-28 07:23:38 +00:00
|
|
|
'oldid' => 'prev',
|
|
|
|
'diff' => $this->event->getExtraParam( 'revid' )
|
2018-03-03 04:00:40 +00:00
|
|
|
] );
|
|
|
|
$label = 'notification-link-text-view-edit';
|
|
|
|
}
|
|
|
|
return [
|
|
|
|
'url' => $url,
|
|
|
|
// Label is only used for non-JS clients.
|
|
|
|
'label' => $this->msg( $label )->text(),
|
2016-04-22 20:13:56 +00:00
|
|
|
];
|
2015-10-28 07:23:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getSecondaryLinks() {
|
2019-12-22 07:25:15 +00:00
|
|
|
$pageLink = $this->getPageLink( $this->event->getTitle(), '', true );
|
2016-06-16 16:12:38 +00:00
|
|
|
if ( $this->isBundled() ) {
|
|
|
|
return [ $pageLink ];
|
|
|
|
} else {
|
|
|
|
return [ $this->getAgentLink(), $pageLink ];
|
|
|
|
}
|
2015-10-28 07:23:38 +00:00
|
|
|
}
|
2018-03-03 04:00:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return LogEntry|false
|
|
|
|
*/
|
|
|
|
private function getLogEntry() {
|
|
|
|
if ( $this->logEntry !== null ) {
|
|
|
|
return $this->logEntry;
|
|
|
|
}
|
|
|
|
$logId = $this->event->getExtraParam( 'logid' );
|
|
|
|
if ( !$logId ) {
|
|
|
|
$this->logEntry = false;
|
|
|
|
} else {
|
2024-02-28 12:55:05 +00:00
|
|
|
$dbr = MediaWikiServices::getInstance()->getConnectionProvider()->getReplicaDatabase();
|
2023-11-07 20:40:06 +00:00
|
|
|
$this->logEntry = DatabaseLogEntry::newFromId( $logId, $dbr ) ?: false;
|
2018-03-03 04:00:40 +00:00
|
|
|
}
|
|
|
|
return $this->logEntry;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns thank type
|
|
|
|
*
|
|
|
|
* @return string 'log' or 'rev'
|
|
|
|
*/
|
|
|
|
private function getThankType() {
|
|
|
|
return $this->event->getExtraParam( 'logid' ) ? 'log' : 'rev';
|
|
|
|
}
|
2015-10-28 07:23:38 +00:00
|
|
|
}
|