mediawiki-extensions-Discus.../includes/ApiDiscussionTools.php
Bartosz Dziewoński 7ad6328223 Include 'false' results in 'transcludedfrom' API response
Skipping them could result in incorrect handling when RESTBase HTML is
outdated.

When a result for a given comment is not found, display an error
instead of assuming it is not transcluded.

Bug: T262065
Change-Id: I14a7a0a25d5181b5c49bd5677f0c002dce5a3cb9
2020-10-22 22:25:35 +02:00

110 lines
2.3 KiB
PHP

<?php
namespace MediaWiki\Extension\DiscussionTools;
use ApiBase;
use ApiMain;
use ApiParsoidTrait;
use DOMElement;
use Title;
use Wikimedia\ParamValidator\ParamValidator;
use Wikimedia\Parsoid\Utils\DOMUtils;
class ApiDiscussionTools extends ApiBase {
use ApiParsoidTrait;
/**
* @inheritDoc
*/
public function __construct( ApiMain $main, string $name ) {
parent::__construct( $main, $name );
}
/**
* @inheritDoc
*/
public function execute() {
$params = $this->extractRequestParams();
$title = Title::newFromText( $params['page'] );
$result = null;
if ( !$title ) {
$this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['page'] ) ] );
return;
}
switch ( $params['paction'] ) {
case 'transcludedfrom':
$response = $this->requestRestbasePageHtml(
$this->getValidRevision( $title, $params['oldid'] ?? null )
);
$doc = DOMUtils::parseHTML( $response['body'] );
$container = $doc->getElementsByTagName( 'body' )->item( 0 );
'@phan-var DOMElement $container';
CommentUtils::unwrapParsoidSections( $container );
$parser = CommentParser::newFromGlobalState( $container );
$comments = $parser->getCommentItems();
$transcludedFrom = [];
foreach ( $comments as $comment ) {
$from = $comment->getTranscludedFrom();
$transcludedFrom[ $comment->getId() ] = $from;
$legacyId = $comment->getLegacyId();
if ( $legacyId ) {
$transcludedFrom[ $legacyId ] = $from;
}
}
$result = $transcludedFrom;
break;
}
$this->getResult()->addValue( null, $this->getModuleName(), $result );
}
/**
* @inheritDoc
*/
public function getAllowedParams() {
return [
'paction' => [
ParamValidator::PARAM_REQUIRED => true,
ParamValidator::PARAM_TYPE => [
'transcludedfrom',
],
ApiBase::PARAM_HELP_MSG => 'apihelp-visualeditoredit-param-paction',
],
'page' => [
ParamValidator::PARAM_REQUIRED => true,
ApiBase::PARAM_HELP_MSG => 'apihelp-visualeditoredit-param-page',
],
'oldid' => null,
];
}
/**
* @inheritDoc
*/
public function needsToken() {
return false;
}
/**
* @inheritDoc
*/
public function isInternal() {
return true;
}
/**
* @inheritDoc
*/
public function isWriteMode() {
return false;
}
}