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;
|
Change CommentParser into a service
Goal:
-----
To have a method like CommentParser::parse(), which just takes a node
to parse and a title and returns plain data, so that we don't need to
keep track of the config to construct a CommentParser object (the
required config like content language is provided by services) and
we don't need to keep that object around after parsing.
Changes:
--------
CommentParser.php:
* …is now a service. Constructor only takes services as arguments.
The node and title are passed to a new parse() method.
* parse() should return plain data, but I split this part to a separate
patch for ease of review: I49bfe019aa460651447fd383f73eafa9d7180a92.
* CommentParser still cheats and accesses global state in a few places,
e.g. calling Title::makeTitleSafe or CommentUtils::getTitleFromUrl,
so we can't turn its tests into true unit tests. This work is left
for future commits.
LanguageData.php:
* …is now a service, instead of a static class.
Parser.js:
* …is not a real service, but it's changed to behave in a similar way.
Constructor takes only the required config as argument,
and node and title are instead passed to a new parse() method.
CommentParserTest.php:
parser.test.js:
* Can be simplified, now that we don't need a useless node and title
to test internal methods that don't use them.
testUtils.js:
* Can be simplified, now that we don't need to override internal
ResourceLoader stuff just to change the parser config.
Change-Id: Iadb7757debe000025e52770ca51ebcf24ca8ee66
2022-02-19 02:43:21 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
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-01-31 15:01:32 +00:00
|
|
|
trait ApiDiscussionToolsTrait {
|
|
|
|
/**
|
|
|
|
* @param RevisionRecord $revision
|
2022-02-19 06:31:34 +00:00
|
|
|
* @return ThreadItemSet
|
2022-01-31 15:01:32 +00:00
|
|
|
*/
|
2022-02-19 06:31:34 +00:00
|
|
|
protected function parseRevision( RevisionRecord $revision ): ThreadItemSet {
|
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
|
|
|
|
Change CommentParser into a service
Goal:
-----
To have a method like CommentParser::parse(), which just takes a node
to parse and a title and returns plain data, so that we don't need to
keep track of the config to construct a CommentParser object (the
required config like content language is provided by services) and
we don't need to keep that object around after parsing.
Changes:
--------
CommentParser.php:
* …is now a service. Constructor only takes services as arguments.
The node and title are passed to a new parse() method.
* parse() should return plain data, but I split this part to a separate
patch for ease of review: I49bfe019aa460651447fd383f73eafa9d7180a92.
* CommentParser still cheats and accesses global state in a few places,
e.g. calling Title::makeTitleSafe or CommentUtils::getTitleFromUrl,
so we can't turn its tests into true unit tests. This work is left
for future commits.
LanguageData.php:
* …is now a service, instead of a static class.
Parser.js:
* …is not a real service, but it's changed to behave in a similar way.
Constructor takes only the required config as argument,
and node and title are instead passed to a new parse() method.
CommentParserTest.php:
parser.test.js:
* Can be simplified, now that we don't need a useless node and title
to test internal methods that don't use them.
testUtils.js:
* Can be simplified, now that we don't need to override internal
ResourceLoader stuff just to change the parser config.
Change-Id: Iadb7757debe000025e52770ca51ebcf24ca8ee66
2022-02-19 02:43:21 +00:00
|
|
|
$parser = MediaWikiServices::getInstance()->getService( 'DiscussionTools.CommentParser' );
|
|
|
|
return $parser->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 ) {
|
|
|
|
$wikitext = CommentUtils::htmlTrim( $wikitext );
|
|
|
|
if ( $signature !== null ) {
|
|
|
|
$wikitext .= $signature;
|
|
|
|
}
|
|
|
|
} 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-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
|
|
|
}
|