2022-01-31 15:01:32 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace MediaWiki\Extension\DiscussionTools;
|
|
|
|
|
2022-02-02 00:13:32 +00:00
|
|
|
use ApiMain;
|
|
|
|
use ApiResult;
|
|
|
|
use DerivativeContext;
|
|
|
|
use DerivativeRequest;
|
|
|
|
use IContextSource;
|
2022-09-08 00:29:35 +00:00
|
|
|
use MediaWiki\Extension\VisualEditor\ParsoidClient;
|
|
|
|
use MediaWiki\Extension\VisualEditor\VisualEditorParsoidClientFactory;
|
2022-01-31 15:01:32 +00:00
|
|
|
use MediaWiki\Revision\RevisionRecord;
|
|
|
|
use Title;
|
2022-02-21 22:07:38 +00:00
|
|
|
use TitleValue;
|
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
|
2022-02-02 00:13:32 +00:00
|
|
|
*/
|
2022-01-31 15:01:32 +00:00
|
|
|
trait ApiDiscussionToolsTrait {
|
|
|
|
/**
|
|
|
|
* @param RevisionRecord $revision
|
2022-03-18 03:28:06 +00:00
|
|
|
* @return ContentThreadItemSet
|
2022-01-31 15:01:32 +00:00
|
|
|
*/
|
2022-03-18 03:28:06 +00:00
|
|
|
protected function parseRevision( RevisionRecord $revision ): ContentThreadItemSet {
|
2022-01-31 15:01:32 +00:00
|
|
|
$response = $this->requestRestbasePageHtml( $revision );
|
|
|
|
|
|
|
|
$doc = DOMUtils::parseHTML( $response['body'] );
|
|
|
|
$container = DOMCompat::getBody( $doc );
|
|
|
|
|
|
|
|
CommentUtils::unwrapParsoidSections( $container );
|
|
|
|
|
2022-02-21 22:07:38 +00:00
|
|
|
$title = TitleValue::newFromPage( $revision->getPage() );
|
2022-01-31 15:01:32 +00:00
|
|
|
|
2022-09-02 02:05:02 +00:00
|
|
|
return $this->commentParser->parse( $container, $title );
|
2022-01-31 15:01:32 +00:00
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
/**
|
|
|
|
* @param RevisionRecord $revision
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
abstract protected function requestRestbasePageHtml( RevisionRecord $revision ): array;
|
2022-02-02 00:13:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Title $title
|
|
|
|
* @param string $html
|
|
|
|
* @param int|null $oldid
|
|
|
|
* @param string|null $etag
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
abstract protected function transformHTML(
|
|
|
|
Title $title, string $html, int $oldid = null, string $etag = null
|
|
|
|
): array;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return IContextSource
|
|
|
|
*/
|
|
|
|
abstract public function getContext();
|
|
|
|
|
2022-01-31 15:01:32 +00:00
|
|
|
}
|