2022-02-02 00:13:32 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace MediaWiki\Extension\DiscussionTools;
|
|
|
|
|
|
|
|
use ApiBase;
|
|
|
|
use ApiMain;
|
2022-10-28 18:24:02 +00:00
|
|
|
use 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;
|
2022-06-27 13:40:41 +00:00
|
|
|
use SkinFactory;
|
2022-02-02 00:13:32 +00:00
|
|
|
use Title;
|
|
|
|
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;
|
|
|
|
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,
|
|
|
|
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;
|
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() );
|
2022-02-04 21:58:20 +00:00
|
|
|
if ( !CommentUtils::isSingleCommentSignedBy( $threadItemSet, $this->getUser()->getName(), $container ) ) {
|
|
|
|
// 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.
|
|
|
|
$signature = preg_replace_callback( '/^( *)(.+)$/', static function ( $matches ) {
|
|
|
|
list( , $leadingSpaces, $sig ) = $matches;
|
|
|
|
return $leadingSpaces . '<span style="opacity: 0.6;">' . $sig . '</span>';
|
|
|
|
}, $signature );
|
|
|
|
|
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',
|
|
|
|
],
|
|
|
|
ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
|
|
|
|
],
|
|
|
|
'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;
|
|
|
|
}
|
|
|
|
}
|