2022-02-04 23:21:29 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace MediaWiki\Extension\DiscussionTools;
|
|
|
|
|
|
|
|
use ApiBase;
|
|
|
|
use ApiMain;
|
2022-11-21 13:49:21 +00:00
|
|
|
use MediaWiki\Extension\DiscussionTools\Hooks\HookUtils;
|
2022-08-29 03:44:12 +00:00
|
|
|
use MediaWiki\Extension\DiscussionTools\ThreadItem\CommentItem;
|
2022-03-18 03:28:06 +00:00
|
|
|
use MediaWiki\Extension\DiscussionTools\ThreadItem\ContentHeadingItem;
|
|
|
|
use MediaWiki\Extension\DiscussionTools\ThreadItem\ContentThreadItem;
|
2022-09-08 00:29:35 +00:00
|
|
|
use MediaWiki\Extension\VisualEditor\VisualEditorParsoidClientFactory;
|
2022-09-02 01:49:44 +00:00
|
|
|
use MediaWiki\Revision\RevisionLookup;
|
2022-02-04 23:21:29 +00:00
|
|
|
use Title;
|
|
|
|
use Wikimedia\ParamValidator\ParamValidator;
|
2022-07-27 16:05:06 +00:00
|
|
|
use Wikimedia\Parsoid\DOM\Element;
|
|
|
|
use Wikimedia\Parsoid\DOM\Text;
|
2022-03-28 21:39:13 +00:00
|
|
|
use Wikimedia\Parsoid\Utils\DOMUtils;
|
2022-02-04 23:21:29 +00:00
|
|
|
|
|
|
|
class ApiDiscussionToolsPageInfo extends ApiBase {
|
|
|
|
|
2022-10-21 19:34:18 +00:00
|
|
|
private CommentParser $commentParser;
|
|
|
|
private VisualEditorParsoidClientFactory $parsoidClientFactory;
|
|
|
|
private RevisionLookup $revisionLookup;
|
2022-09-02 01:49:44 +00:00
|
|
|
|
2022-09-02 02:05:02 +00:00
|
|
|
public function __construct(
|
|
|
|
ApiMain $main,
|
|
|
|
string $name,
|
2022-09-08 00:29:35 +00:00
|
|
|
VisualEditorParsoidClientFactory $parsoidClientFactory,
|
2022-09-02 01:49:44 +00:00
|
|
|
CommentParser $commentParser,
|
|
|
|
RevisionLookup $revisionLookup
|
2022-09-02 02:05:02 +00:00
|
|
|
) {
|
2022-02-04 23:21:29 +00:00
|
|
|
parent::__construct( $main, $name );
|
2022-09-08 00:29:35 +00:00
|
|
|
$this->parsoidClientFactory = $parsoidClientFactory;
|
2022-09-02 02:05:02 +00:00
|
|
|
$this->commentParser = $commentParser;
|
2022-09-02 01:49:44 +00:00
|
|
|
$this->revisionLookup = $revisionLookup;
|
2022-02-04 23:21:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function execute() {
|
|
|
|
$params = $this->extractRequestParams();
|
2022-09-02 01:49:44 +00:00
|
|
|
$this->requireAtLeastOneParameter( $params, 'page', 'oldid' );
|
|
|
|
|
2022-12-19 07:24:33 +00:00
|
|
|
if ( isset( $params['page'] ) ) {
|
|
|
|
$title = Title::newFromText( $params['page'] );
|
|
|
|
if ( !$title ) {
|
|
|
|
$this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['page'] ) ] );
|
|
|
|
}
|
|
|
|
if ( !HookUtils::isAvailableForTitle( $title ) ) {
|
|
|
|
$this->getResult()->addValue(
|
|
|
|
null,
|
|
|
|
$this->getModuleName(),
|
|
|
|
[ 'transcludedfrom' => [] ]
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-02 01:49:44 +00:00
|
|
|
if ( isset( $params['oldid'] ) ) {
|
|
|
|
$revision = $this->revisionLookup->getRevisionById( $params['oldid'] );
|
|
|
|
if ( !$revision ) {
|
|
|
|
$this->dieWithError( [ 'apierror-nosuchrevid', $params['oldid'] ] );
|
|
|
|
}
|
2022-02-04 23:21:29 +00:00
|
|
|
|
2022-09-02 01:49:44 +00:00
|
|
|
} else {
|
|
|
|
$title = Title::newFromText( $params['page'] );
|
|
|
|
if ( !$title ) {
|
|
|
|
$this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['page'] ) ] );
|
|
|
|
}
|
|
|
|
$revision = $this->revisionLookup->getRevisionByTitle( $title );
|
|
|
|
if ( !$revision ) {
|
|
|
|
$this->dieWithError(
|
|
|
|
[ 'apierror-missingrev-title', wfEscapeWikiText( $title->getPrefixedText() ) ],
|
|
|
|
'nosuchrevid'
|
|
|
|
);
|
|
|
|
}
|
2022-02-04 23:21:29 +00:00
|
|
|
}
|
|
|
|
|
2022-11-21 13:49:21 +00:00
|
|
|
$threadItemSet = HookUtils::parseRevisionParsoidHtml( $revision );
|
2022-02-04 23:21:29 +00:00
|
|
|
|
2022-03-21 21:14:47 +00:00
|
|
|
$result = [];
|
2022-09-02 01:49:44 +00:00
|
|
|
$prop = array_fill_keys( $params['prop'], true );
|
2022-02-04 23:21:29 +00:00
|
|
|
|
2022-03-21 21:14:47 +00:00
|
|
|
if ( isset( $prop['transcludedfrom'] ) ) {
|
2022-06-09 13:51:33 +00:00
|
|
|
$result['transcludedfrom'] = static::getTranscludedFrom( $threadItemSet );
|
2022-06-06 14:13:16 +00:00
|
|
|
}
|
2022-02-04 23:21:29 +00:00
|
|
|
|
2022-06-06 14:13:16 +00:00
|
|
|
if ( isset( $prop['threaditemshtml'] ) ) {
|
2022-06-09 13:51:33 +00:00
|
|
|
$result['threaditemshtml'] = static::getThreadItemsHtml( $threadItemSet );
|
2022-06-06 14:13:16 +00:00
|
|
|
}
|
2022-02-04 23:21:29 +00:00
|
|
|
|
2022-06-06 14:13:16 +00:00
|
|
|
$this->getResult()->addValue( null, $this->getModuleName(), $result );
|
|
|
|
}
|
2022-03-21 21:14:47 +00:00
|
|
|
|
2022-06-06 14:13:16 +00:00
|
|
|
/**
|
2022-03-18 03:28:06 +00:00
|
|
|
* Get transcluded=from data for a ContentThreadItemSet
|
2022-06-06 14:13:16 +00:00
|
|
|
*
|
2022-03-18 03:28:06 +00:00
|
|
|
* @param ContentThreadItemSet $threadItemSet
|
2022-06-06 14:13:16 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
2022-03-18 03:28:06 +00:00
|
|
|
private static function getTranscludedFrom( ContentThreadItemSet $threadItemSet ): array {
|
2022-06-06 14:13:16 +00:00
|
|
|
$threadItems = $threadItemSet->getThreadItems();
|
|
|
|
$transcludedFrom = [];
|
|
|
|
foreach ( $threadItems as $threadItem ) {
|
|
|
|
$from = $threadItem->getTranscludedFrom();
|
2022-02-04 23:21:29 +00:00
|
|
|
|
2022-08-23 23:01:09 +00:00
|
|
|
// Key by IDs and names. This assumes that they can never conflict.
|
2022-06-06 14:13:16 +00:00
|
|
|
|
|
|
|
$transcludedFrom[ $threadItem->getId() ] = $from;
|
|
|
|
|
|
|
|
$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;
|
|
|
|
}
|
2022-02-04 23:21:29 +00:00
|
|
|
}
|
|
|
|
|
2022-06-06 14:13:16 +00:00
|
|
|
return $transcludedFrom;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-03-18 03:28:06 +00:00
|
|
|
* Get thread items HTML for a ContentThreadItemSet
|
2022-06-06 14:13:16 +00:00
|
|
|
*
|
2022-03-18 03:28:06 +00:00
|
|
|
* @param ContentThreadItemSet $threadItemSet
|
2022-06-06 14:13:16 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
2022-03-18 03:28:06 +00:00
|
|
|
private static function getThreadItemsHtml( ContentThreadItemSet $threadItemSet ): array {
|
2022-07-27 16:05:06 +00:00
|
|
|
// This function assumes that the start of the ranges associated with
|
|
|
|
// HeadingItems are going to be at the start of their associated
|
|
|
|
// heading node (`<h2>^heading</h2>`), i.e. in the position generated
|
|
|
|
// by getHeadlineNodeAndOffset.
|
2022-06-06 14:13:16 +00:00
|
|
|
$threads = $threadItemSet->getThreads();
|
2022-07-27 16:05:06 +00:00
|
|
|
if ( count( $threads ) > 0 && !$threads[0]->isPlaceholderHeading() ) {
|
2022-06-06 14:13:16 +00:00
|
|
|
$firstHeading = $threads[0];
|
2022-07-27 16:05:06 +00:00
|
|
|
$firstRange = $firstHeading->getRange();
|
|
|
|
$rootNode = $firstHeading->getRootNode();
|
|
|
|
// We need a placeholder if there's content between the beginning
|
|
|
|
// of rootnode and the start of firstHeading. An ancestor of the
|
|
|
|
// first heading with a previousSibling is evidence that there's
|
|
|
|
// probably content. If this is giving false positives we could
|
|
|
|
// perhaps use linearWalkBackwards and DomUtils::isContentNode.
|
|
|
|
$closest = CommentUtils::closestElementWithSibling( $firstRange->startContainer, 'previous' );
|
|
|
|
if ( $closest && !$rootNode->isSameNode( $closest ) ) {
|
|
|
|
$range = new ImmutableRange( $rootNode, 0, $rootNode, 0 );
|
2022-03-18 03:28:06 +00:00
|
|
|
$fakeHeading = new ContentHeadingItem( $range, null );
|
2022-07-27 16:05:06 +00:00
|
|
|
$fakeHeading->setRootNode( $rootNode );
|
2022-06-29 00:43:48 +00:00
|
|
|
$fakeHeading->setName( 'h-' );
|
|
|
|
$fakeHeading->setId( 'h-' );
|
2022-06-06 14:13:16 +00:00
|
|
|
array_unshift( $threads, $fakeHeading );
|
2022-05-17 00:36:05 +00:00
|
|
|
}
|
2022-06-06 14:13:16 +00:00
|
|
|
}
|
2022-03-18 03:28:06 +00:00
|
|
|
$output = array_map( static function ( ContentThreadItem $item ) {
|
|
|
|
return $item->jsonSerialize( true, static function ( array &$array, ContentThreadItem $item ) {
|
2022-06-06 14:13:16 +00:00
|
|
|
$array['html'] = $item->getHtml();
|
2022-08-29 03:44:12 +00:00
|
|
|
if ( $item instanceof CommentItem ) {
|
|
|
|
// We want timestamps to be consistently formatted in API
|
|
|
|
// output instead of varying based on comment time
|
|
|
|
// (T315400). The format used here is equivalent to 'Y-m-d\TH:i:s\Z'
|
|
|
|
$array['timestamp'] = wfTimestamp( TS_ISO_8601, $item->getTimestamp()->getTimestamp() );
|
|
|
|
}
|
2022-06-06 14:13:16 +00:00
|
|
|
} );
|
|
|
|
}, $threads );
|
|
|
|
foreach ( $threads as $index => $item ) {
|
|
|
|
// need to loop over this to fix up empty sections, because we
|
|
|
|
// need context that's not available inside the array map
|
2022-03-18 03:28:06 +00:00
|
|
|
if ( $item instanceof ContentHeadingItem && count( $item->getReplies() ) === 0 ) {
|
2022-07-27 16:05:06 +00:00
|
|
|
// If there are no replies we want to include whatever's
|
|
|
|
// inside this section as "othercontent". We create a range
|
|
|
|
// that's between the end of this section's heading and the
|
|
|
|
// start of next section's heading. The main difficulty here
|
|
|
|
// is avoiding catching any of the heading's tags within the
|
|
|
|
// range.
|
2022-06-06 14:13:16 +00:00
|
|
|
$nextItem = $threads[ $index + 1 ] ?? false;
|
|
|
|
$startRange = $item->getRange();
|
2022-07-27 16:05:06 +00:00
|
|
|
if ( $item->isPlaceholderHeading() ) {
|
|
|
|
// Placeholders don't have any heading to avoid
|
|
|
|
$startNode = $startRange->startContainer;
|
|
|
|
$startOffset = $startRange->startOffset;
|
|
|
|
} else {
|
|
|
|
$startNode = CommentUtils::closestElementWithSibling( $startRange->endContainer, 'next' );
|
2022-12-01 17:44:28 +00:00
|
|
|
if ( !$startNode ) {
|
|
|
|
// If there's no siblings here this means we're on a
|
|
|
|
// heading that is the final heading on a page and
|
|
|
|
// which has no contents at all. We can skip the rest.
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
$startNode = $startNode->nextSibling;
|
|
|
|
$startOffset = 0;
|
|
|
|
}
|
2022-07-27 16:05:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( !$startNode ) {
|
|
|
|
$startNode = $startRange->endContainer;
|
|
|
|
$startOffset = $startRange->endOffset;
|
|
|
|
}
|
|
|
|
|
2022-06-06 14:13:16 +00:00
|
|
|
if ( $nextItem ) {
|
2022-07-27 16:05:06 +00:00
|
|
|
$nextStart = $nextItem->getRange()->startContainer;
|
|
|
|
$endContainer = CommentUtils::closestElementWithSibling( $nextStart, 'previous' );
|
|
|
|
$endContainer = $endContainer && $endContainer->previousSibling ?
|
|
|
|
$endContainer->previousSibling : $nextStart;
|
|
|
|
$endOffset = CommentUtils::childIndexOf( $endContainer );
|
|
|
|
if ( $endContainer instanceof Text ) {
|
|
|
|
// This probably means that there's a wrapping node
|
|
|
|
// e.g. <div>foo\n==heading==\nbar</div>
|
|
|
|
$endOffset += $endContainer->length;
|
|
|
|
} elseif ( $endContainer instanceof Element && $endContainer->tagName === 'section' ) {
|
|
|
|
// if we're in sections, make sure we're selecting the
|
|
|
|
// end of the previous section
|
|
|
|
$endOffset = $endContainer->childNodes->length;
|
|
|
|
} elseif ( $endContainer->parentNode ) {
|
|
|
|
$endContainer = $endContainer->parentNode;
|
|
|
|
}
|
2022-06-06 14:13:16 +00:00
|
|
|
$betweenRange = new ImmutableRange(
|
2022-07-27 16:05:06 +00:00
|
|
|
$startNode, $startOffset,
|
|
|
|
$endContainer ?: $nextStart, $endOffset
|
2022-06-06 14:13:16 +00:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
// This is the last section, so we want to go to the end of the rootnode
|
|
|
|
$betweenRange = new ImmutableRange(
|
2022-07-27 16:05:06 +00:00
|
|
|
$startNode, $startOffset,
|
2022-06-06 14:13:16 +00:00
|
|
|
$item->getRootNode(), $item->getRootNode()->childNodes->length
|
|
|
|
);
|
2022-03-28 21:39:13 +00:00
|
|
|
}
|
2022-06-06 14:13:16 +00:00
|
|
|
$fragment = $betweenRange->cloneContents();
|
|
|
|
CommentModifier::unwrapFragment( $fragment );
|
2022-12-01 17:44:28 +00:00
|
|
|
$otherContent = trim( DOMUtils::getFragmentInnerHTML( $fragment ) );
|
|
|
|
if ( $otherContent ) {
|
|
|
|
// A completely empty section will result in otherContent
|
|
|
|
// being an empty string. In this case we should just not include it.
|
|
|
|
$output[$index]['othercontent'] = $otherContent;
|
|
|
|
}
|
|
|
|
|
2022-03-28 21:39:13 +00:00
|
|
|
}
|
2022-03-21 21:14:47 +00:00
|
|
|
}
|
2022-06-06 14:13:16 +00:00
|
|
|
return $output;
|
2022-02-04 23:21:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function getAllowedParams() {
|
|
|
|
return [
|
|
|
|
'page' => [
|
|
|
|
ApiBase::PARAM_HELP_MSG => 'apihelp-visualeditoredit-param-page',
|
|
|
|
],
|
2022-09-02 01:49:44 +00:00
|
|
|
'oldid' => [
|
|
|
|
ParamValidator::PARAM_TYPE => 'integer',
|
|
|
|
],
|
2022-03-21 21:14:47 +00:00
|
|
|
'prop' => [
|
2022-04-03 23:26:15 +00:00
|
|
|
ParamValidator::PARAM_DEFAULT => 'transcludedfrom',
|
|
|
|
ParamValidator::PARAM_ISMULTI => true,
|
|
|
|
ParamValidator::PARAM_TYPE => [
|
2022-03-21 21:14:47 +00:00
|
|
|
'transcludedfrom',
|
|
|
|
'threaditemshtml'
|
|
|
|
],
|
|
|
|
ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
|
|
|
|
],
|
2022-02-04 23:21:29 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function needsToken() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function isInternal() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function isWriteMode() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|