2022-02-02 00:13:32 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace MediaWiki\Extension\DiscussionTools;
|
|
|
|
|
2024-10-19 21:39:18 +00:00
|
|
|
use MediaWiki\Api\ApiBase;
|
|
|
|
use MediaWiki\Api\ApiMain;
|
|
|
|
use MediaWiki\Api\ApiUsageException;
|
2022-03-29 19:29:46 +00:00
|
|
|
use MediaWiki\Extension\VisualEditor\ApiParsoidTrait;
|
2022-09-08 00:29:35 +00:00
|
|
|
use MediaWiki\Extension\VisualEditor\VisualEditorParsoidClientFactory;
|
2023-08-19 04:14:36 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2023-07-18 02:19:45 +00:00
|
|
|
use MediaWiki\User\TempUser\TempUserCreator;
|
|
|
|
use MediaWiki\User\UserFactory;
|
2022-06-27 13:40:41 +00:00
|
|
|
use SkinFactory;
|
2022-02-02 00:13:32 +00:00
|
|
|
use Wikimedia\ParamValidator\ParamValidator;
|
2022-02-04 21:58:20 +00:00
|
|
|
use Wikimedia\Parsoid\Utils\DOMCompat;
|
|
|
|
use Wikimedia\Parsoid\Utils\DOMUtils;
|
2022-02-02 00:13:32 +00:00
|
|
|
|
|
|
|
class ApiDiscussionToolsPreview extends ApiBase {
|
|
|
|
use ApiDiscussionToolsTrait;
|
|
|
|
use ApiParsoidTrait;
|
|
|
|
|
2022-10-21 19:34:18 +00:00
|
|
|
private CommentParser $commentParser;
|
|
|
|
private VisualEditorParsoidClientFactory $parsoidClientFactory;
|
2023-07-18 02:19:45 +00:00
|
|
|
private TempUserCreator $tempUserCreator;
|
|
|
|
private UserFactory $userFactory;
|
2022-10-21 19:34:18 +00:00
|
|
|
private SkinFactory $skinFactory;
|
2022-06-27 13:40:41 +00:00
|
|
|
|
2022-02-04 21:58:20 +00:00
|
|
|
public function __construct(
|
|
|
|
ApiMain $main,
|
2022-09-02 02:05:02 +00:00
|
|
|
string $name,
|
2022-09-08 00:29:35 +00:00
|
|
|
VisualEditorParsoidClientFactory $parsoidClientFactory,
|
2022-06-27 13:40:41 +00:00
|
|
|
CommentParser $commentParser,
|
2023-07-18 02:19:45 +00:00
|
|
|
TempUserCreator $tempUserCreator,
|
|
|
|
UserFactory $userFactory,
|
2022-06-27 13:40:41 +00:00
|
|
|
SkinFactory $skinFactory
|
2022-02-04 21:58:20 +00:00
|
|
|
) {
|
2022-02-02 00:13:32 +00:00
|
|
|
parent::__construct( $main, $name );
|
2022-09-08 00:29:35 +00:00
|
|
|
$this->parsoidClientFactory = $parsoidClientFactory;
|
2022-02-04 21:58:20 +00:00
|
|
|
$this->commentParser = $commentParser;
|
2023-07-18 02:19:45 +00:00
|
|
|
$this->tempUserCreator = $tempUserCreator;
|
|
|
|
$this->userFactory = $userFactory;
|
2022-06-27 13:40:41 +00:00
|
|
|
$this->skinFactory = $skinFactory;
|
2022-02-02 00:13:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
2022-10-28 18:24:02 +00:00
|
|
|
* @throws ApiUsageException
|
2022-02-02 00:13:32 +00:00
|
|
|
*/
|
|
|
|
public function execute() {
|
|
|
|
$params = $this->extractRequestParams();
|
|
|
|
$title = Title::newFromText( $params['page'] );
|
|
|
|
|
|
|
|
if ( !$title ) {
|
|
|
|
$this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['page'] ) ] );
|
|
|
|
}
|
|
|
|
if ( $params['type'] === 'topic' ) {
|
|
|
|
$this->requireAtLeastOneParameter( $params, 'sectiontitle' );
|
|
|
|
}
|
|
|
|
|
2022-02-04 21:58:20 +00:00
|
|
|
// Try without adding a signature
|
2022-06-27 11:50:10 +00:00
|
|
|
$result = $this->previewMessage(
|
|
|
|
$params['type'],
|
|
|
|
$title,
|
|
|
|
[
|
|
|
|
'wikitext' => $params['wikitext'],
|
|
|
|
'sectiontitle' => $params['sectiontitle']
|
2022-06-27 13:40:41 +00:00
|
|
|
],
|
|
|
|
$params
|
2022-06-27 11:50:10 +00:00
|
|
|
);
|
2022-02-04 21:58:20 +00:00
|
|
|
$resultHtml = $result->getResultData( [ 'parse', 'text' ] );
|
|
|
|
|
|
|
|
// Check if there was a signature in a proper place
|
|
|
|
$container = DOMCompat::getBody( DOMUtils::parseHTML( $resultHtml ) );
|
2022-02-21 22:07:38 +00:00
|
|
|
$threadItemSet = $this->commentParser->parse( $container, $title->getTitleValue() );
|
2023-07-18 02:19:45 +00:00
|
|
|
if ( !CommentUtils::isSingleCommentSignedBy(
|
|
|
|
$threadItemSet, $this->getUserForPreview()->getName(), $container
|
|
|
|
) ) {
|
2022-02-04 21:58:20 +00:00
|
|
|
// If not, add the signature and re-render
|
2022-02-02 00:13:32 +00:00
|
|
|
$signature = $this->msg( 'discussiontools-signature-prefix' )->inContentLanguage()->text() . '~~~~';
|
|
|
|
// Drop opacity of signature in preview to make message body preview clearer.
|
|
|
|
// Extract any leading spaces outside the <span> markup to ensure accurate previews.
|
2023-10-30 08:12:21 +00:00
|
|
|
$signature = preg_replace( '/([^ ].*)/', '<span style="opacity: 0.6;">$1</span>',
|
|
|
|
$signature );
|
2022-02-02 00:13:32 +00:00
|
|
|
|
2022-06-27 11:50:10 +00:00
|
|
|
$result = $this->previewMessage(
|
|
|
|
$params['type'],
|
|
|
|
$title,
|
|
|
|
[
|
|
|
|
'wikitext' => $params['wikitext'],
|
|
|
|
'sectiontitle' => $params['sectiontitle'],
|
|
|
|
'signature' => $signature
|
2022-06-27 13:40:41 +00:00
|
|
|
],
|
|
|
|
$params
|
2022-06-27 11:50:10 +00:00
|
|
|
);
|
2022-02-04 21:58:20 +00:00
|
|
|
}
|
2022-02-02 00:13:32 +00:00
|
|
|
|
|
|
|
$this->getResult()->addValue( null, $this->getModuleName(), $result->serializeForApiResult() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function getAllowedParams() {
|
|
|
|
return [
|
|
|
|
'type' => [
|
|
|
|
ParamValidator::PARAM_REQUIRED => true,
|
|
|
|
ParamValidator::PARAM_TYPE => [
|
|
|
|
'reply',
|
|
|
|
'topic',
|
|
|
|
],
|
2023-08-04 18:55:13 +00:00
|
|
|
ApiBase::PARAM_HELP_MSG_PER_VALUE => [
|
|
|
|
'reply' => 'apihelp-discussiontoolsedit-paramvalue-paction-addcomment',
|
|
|
|
'topic' => 'apihelp-discussiontoolsedit-paramvalue-paction-addtopic',
|
|
|
|
],
|
2022-02-02 00:13:32 +00:00
|
|
|
],
|
|
|
|
'page' => [
|
|
|
|
ParamValidator::PARAM_REQUIRED => true,
|
|
|
|
ApiBase::PARAM_HELP_MSG => 'apihelp-visualeditoredit-param-page',
|
|
|
|
],
|
|
|
|
'wikitext' => [
|
|
|
|
ParamValidator::PARAM_REQUIRED => true,
|
|
|
|
ParamValidator::PARAM_TYPE => 'text',
|
|
|
|
],
|
|
|
|
'sectiontitle' => [
|
|
|
|
ParamValidator::PARAM_TYPE => 'string',
|
|
|
|
ApiBase::PARAM_HELP_MSG => 'apihelp-edit-param-sectiontitle',
|
|
|
|
],
|
2022-06-27 13:40:41 +00:00
|
|
|
'useskin' => [
|
|
|
|
ParamValidator::PARAM_TYPE => array_keys( $this->skinFactory->getInstalledSkins() ),
|
|
|
|
ApiBase::PARAM_HELP_MSG => 'apihelp-parse-param-useskin',
|
|
|
|
],
|
2022-02-02 00:13:32 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function isInternal() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|