2022-01-31 15:01:32 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace MediaWiki\Extension\DiscussionTools;
|
|
|
|
|
2024-10-19 21:39:18 +00:00
|
|
|
use MediaWiki\Api\ApiMain;
|
|
|
|
use MediaWiki\Api\ApiResult;
|
2024-06-08 22:02:35 +00:00
|
|
|
use MediaWiki\Context\DerivativeContext;
|
|
|
|
use MediaWiki\Context\IContextSource;
|
2022-09-08 00:29:35 +00:00
|
|
|
use MediaWiki\Extension\VisualEditor\ParsoidClient;
|
|
|
|
use MediaWiki\Extension\VisualEditor\VisualEditorParsoidClientFactory;
|
2023-08-19 23:39:56 +00:00
|
|
|
use MediaWiki\Request\DerivativeRequest;
|
2023-12-11 15:38:02 +00:00
|
|
|
use MediaWiki\Request\WebRequest;
|
2022-01-31 15:01:32 +00:00
|
|
|
use MediaWiki\Revision\RevisionRecord;
|
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;
|
2023-12-11 15:38:02 +00:00
|
|
|
use MediaWiki\User\User;
|
2023-07-18 02:19:45 +00:00
|
|
|
use MediaWiki\User\UserFactory;
|
2022-01-31 15:01:32 +00:00
|
|
|
use Wikimedia\Parsoid\Utils\DOMCompat;
|
|
|
|
use Wikimedia\Parsoid\Utils\DOMUtils;
|
|
|
|
|
2022-02-02 00:13:32 +00:00
|
|
|
/**
|
|
|
|
* Random methods we want to share between API modules.
|
2022-09-02 02:05:02 +00:00
|
|
|
*
|
2022-09-08 00:29:35 +00:00
|
|
|
* @property VisualEditorParsoidClientFactory $parsoidClientFactory
|
2022-09-02 02:05:02 +00:00
|
|
|
* @property CommentParser $commentParser
|
2023-07-18 02:19:45 +00:00
|
|
|
* @property TempUserCreator $tempUserCreator
|
|
|
|
* @property UserFactory $userFactory
|
2022-02-02 00:13:32 +00:00
|
|
|
*/
|
2022-01-31 15:01:32 +00:00
|
|
|
trait ApiDiscussionToolsTrait {
|
|
|
|
|
2022-02-02 00:13:32 +00:00
|
|
|
/**
|
|
|
|
* Given parameters describing a reply or new topic, transform them into wikitext using Parsoid,
|
|
|
|
* then preview the wikitext using the legacy parser.
|
|
|
|
*
|
2022-06-27 11:50:10 +00:00
|
|
|
* @param string $type 'topic' or 'reply'
|
|
|
|
* @param Title $title Context title for wikitext transformations
|
2022-02-02 00:13:32 +00:00
|
|
|
* @param array $params Associative array with the following keys:
|
2022-02-04 21:58:20 +00:00
|
|
|
* - `wikitext` (string|null) Content of the message, mutually exclusive with `html`
|
|
|
|
* - `html` (string|null) Content of the message, mutually exclusive with `wikitext`
|
2022-02-02 00:13:32 +00:00
|
|
|
* - `sectiontitle` (string) Content of the title, when `type` is 'topic'
|
|
|
|
* - `signature` (string|null) Wikitext signature to add to the message
|
2022-06-27 13:40:41 +00:00
|
|
|
* @param array $originalParams Original params from the source API call
|
2022-02-02 00:13:32 +00:00
|
|
|
* @return ApiResult action=parse API result
|
|
|
|
*/
|
2022-06-27 13:40:41 +00:00
|
|
|
protected function previewMessage(
|
|
|
|
string $type, Title $title, array $params, array $originalParams = []
|
|
|
|
): ApiResult {
|
2022-02-04 21:58:20 +00:00
|
|
|
$wikitext = $params['wikitext'] ?? null;
|
|
|
|
$html = $params['html'] ?? null;
|
2022-02-02 00:13:32 +00:00
|
|
|
$signature = $params['signature'] ?? null;
|
|
|
|
|
2022-06-27 11:50:10 +00:00
|
|
|
switch ( $type ) {
|
2022-02-02 00:13:32 +00:00
|
|
|
case 'topic':
|
2022-02-04 21:58:20 +00:00
|
|
|
if ( $wikitext !== null ) {
|
|
|
|
if ( $signature !== null ) {
|
2022-08-06 12:51:02 +00:00
|
|
|
$wikitext = CommentModifier::appendSignatureWikitext( $wikitext, $signature );
|
2022-02-04 21:58:20 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$doc = DOMUtils::parseHTML( '' );
|
|
|
|
$container = DOMUtils::parseHTMLToFragment( $doc, $html );
|
|
|
|
if ( $signature !== null ) {
|
|
|
|
CommentModifier::appendSignature( $container, $signature );
|
|
|
|
}
|
|
|
|
$html = DOMUtils::getFragmentInnerHTML( $container );
|
|
|
|
$wikitext = $this->transformHTML( $title, $html )[ 'body' ];
|
2022-02-02 00:13:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( $params['sectiontitle'] ) {
|
|
|
|
$wikitext = "== " . $params['sectiontitle'] . " ==\n" . $wikitext;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'reply':
|
|
|
|
$doc = DOMUtils::parseHTML( '' );
|
|
|
|
|
2022-02-04 21:58:20 +00:00
|
|
|
if ( $wikitext !== null ) {
|
|
|
|
$container = CommentModifier::prepareWikitextReply( $doc, $wikitext );
|
|
|
|
} else {
|
|
|
|
$container = CommentModifier::prepareHtmlReply( $doc, $html );
|
|
|
|
}
|
2022-02-02 00:13:32 +00:00
|
|
|
|
|
|
|
if ( $signature !== null ) {
|
|
|
|
CommentModifier::appendSignature( $container, $signature );
|
|
|
|
}
|
|
|
|
$list = CommentModifier::transferReply( $container );
|
|
|
|
$html = DOMCompat::getOuterHTML( $list );
|
|
|
|
|
|
|
|
$wikitext = $this->transformHTML( $title, $html )[ 'body' ];
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$apiParams = [
|
|
|
|
'action' => 'parse',
|
|
|
|
'title' => $title->getPrefixedText(),
|
|
|
|
'text' => $wikitext,
|
|
|
|
'pst' => '1',
|
|
|
|
'preview' => '1',
|
|
|
|
'disableeditsection' => '1',
|
|
|
|
'prop' => 'text|modules|jsconfigvars',
|
|
|
|
];
|
2022-06-27 13:40:41 +00:00
|
|
|
if ( isset( $originalParams['useskin'] ) ) {
|
|
|
|
$apiParams['useskin'] = $originalParams['useskin'];
|
|
|
|
}
|
|
|
|
if ( isset( $originalParams['mobileformat'] ) && $originalParams['mobileformat'] ) {
|
|
|
|
$apiParams['mobileformat'] = '1';
|
|
|
|
}
|
2022-02-02 00:13:32 +00:00
|
|
|
|
|
|
|
$context = new DerivativeContext( $this->getContext() );
|
|
|
|
$context->setRequest(
|
|
|
|
new DerivativeRequest(
|
|
|
|
$context->getRequest(),
|
|
|
|
$apiParams,
|
|
|
|
/* was posted? */ true
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$api = new ApiMain(
|
|
|
|
$context,
|
|
|
|
/* enable write? */ false
|
|
|
|
);
|
|
|
|
|
|
|
|
$api->execute();
|
|
|
|
return $api->getResult();
|
|
|
|
}
|
|
|
|
|
2023-07-18 02:19:45 +00:00
|
|
|
/**
|
|
|
|
* @see ApiParse::getUserForPreview
|
|
|
|
* @return User
|
|
|
|
*/
|
|
|
|
private function getUserForPreview() {
|
|
|
|
$user = $this->getUser();
|
|
|
|
if ( $this->tempUserCreator->shouldAutoCreate( $user, 'edit' ) ) {
|
|
|
|
return $this->userFactory->newUnsavedTempUser(
|
|
|
|
$this->tempUserCreator->getStashedName( $this->getRequest()->getSession() )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
2022-09-08 00:29:35 +00:00
|
|
|
/**
|
|
|
|
* @see VisualEditorParsoidClientFactory
|
|
|
|
* @return ParsoidClient
|
|
|
|
*/
|
|
|
|
protected function getParsoidClient(): ParsoidClient {
|
|
|
|
return $this->parsoidClientFactory->createParsoidClient(
|
|
|
|
$this->getContext()->getRequest()->getHeader( 'Cookie' )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-01-31 15:01:32 +00:00
|
|
|
/**
|
2022-11-21 11:00:08 +00:00
|
|
|
* @warning (T323357) - Calling this method writes to stash, so it should be called
|
|
|
|
* only when we are fetching page HTML for editing.
|
2022-01-31 15:01:32 +00:00
|
|
|
*/
|
|
|
|
abstract protected function requestRestbasePageHtml( RevisionRecord $revision ): array;
|
2022-02-02 00:13:32 +00:00
|
|
|
|
|
|
|
abstract protected function transformHTML(
|
2024-11-02 01:22:27 +00:00
|
|
|
Title $title, string $html, ?int $oldid = null, ?string $etag = null
|
2022-02-02 00:13:32 +00:00
|
|
|
): array;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return IContextSource
|
|
|
|
*/
|
|
|
|
abstract public function getContext();
|
|
|
|
|
2023-07-18 02:19:45 +00:00
|
|
|
/**
|
|
|
|
* @return User
|
|
|
|
*/
|
|
|
|
abstract public function getUser();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return WebRequest
|
|
|
|
*/
|
|
|
|
abstract public function getRequest();
|
|
|
|
|
2022-01-31 15:01:32 +00:00
|
|
|
}
|