2020-08-19 20:03:41 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace MediaWiki\Extension\DiscussionTools;
|
|
|
|
|
|
|
|
use ApiBase;
|
|
|
|
use ApiMain;
|
|
|
|
use ApiParsoidTrait;
|
|
|
|
use Title;
|
|
|
|
use Wikimedia\ParamValidator\ParamValidator;
|
2021-08-02 13:45:39 +00:00
|
|
|
use Wikimedia\Parsoid\Utils\DOMCompat;
|
2020-08-19 20:03:41 +00:00
|
|
|
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'] ) ] );
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ( $params['paction'] ) {
|
|
|
|
case 'transcludedfrom':
|
|
|
|
$response = $this->requestRestbasePageHtml(
|
|
|
|
$this->getValidRevision( $title, $params['oldid'] ?? null )
|
|
|
|
);
|
|
|
|
|
|
|
|
$doc = DOMUtils::parseHTML( $response['body'] );
|
2021-08-02 13:45:39 +00:00
|
|
|
$container = DOMCompat::getBody( $doc );
|
2020-08-19 20:03:41 +00:00
|
|
|
|
|
|
|
CommentUtils::unwrapParsoidSections( $container );
|
|
|
|
|
|
|
|
$parser = CommentParser::newFromGlobalState( $container );
|
2021-02-25 17:02:19 +00:00
|
|
|
$threadItems = $parser->getThreadItems();
|
2020-08-19 20:03:41 +00:00
|
|
|
|
|
|
|
$transcludedFrom = [];
|
2021-02-25 17:02:19 +00:00
|
|
|
foreach ( $threadItems as $threadItem ) {
|
|
|
|
$from = $threadItem->getTranscludedFrom();
|
2021-04-07 20:46:36 +00:00
|
|
|
|
|
|
|
// Key by IDs, legacy IDs, and names. This assumes that they can never conflict.
|
|
|
|
|
2021-02-25 17:02:19 +00:00
|
|
|
$transcludedFrom[ $threadItem->getId() ] = $from;
|
2021-04-07 20:46:36 +00:00
|
|
|
|
2021-02-25 17:02:19 +00:00
|
|
|
$legacyId = $threadItem->getLegacyId();
|
2020-10-22 20:25:29 +00:00
|
|
|
if ( $legacyId ) {
|
|
|
|
$transcludedFrom[ $legacyId ] = $from;
|
2020-08-19 20:03:41 +00:00
|
|
|
}
|
2021-04-07 20:46:36 +00:00
|
|
|
|
|
|
|
$name = $threadItem->getName();
|
|
|
|
if ( isset( $transcludedFrom[ $name ] ) && $transcludedFrom[ $name ] !== $from ) {
|
|
|
|
// Two or more items with the same name, transcluded from different pages.
|
|
|
|
// Consider them both to be transcluded from unknown source.
|
|
|
|
$transcludedFrom[ $name ] = true;
|
|
|
|
} else {
|
|
|
|
$transcludedFrom[ $name ] = $from;
|
|
|
|
}
|
2020-08-19 20:03:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$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',
|
2021-02-10 23:05:38 +00:00
|
|
|
ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
|
2020-08-19 20:03:41 +00:00
|
|
|
],
|
|
|
|
'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;
|
|
|
|
}
|
|
|
|
}
|