ApiDiscussionToolsPageInfo: Allow excluding signatures

Special:DiscussionToolsDebug falsely promised that the API delivers
the same information, but in fact the API included the signatures in
each comment's HTML unconditionally. Allow excluding them.

Change-Id: Ie1e38d28bed0b6d5713d9051b84cc08a23da94c2
This commit is contained in:
Bartosz Dziewoński 2023-05-20 17:18:13 +03:00
parent 18edf9ca61
commit 2d40cbb6d5
6 changed files with 32 additions and 9 deletions

View file

@ -24,6 +24,7 @@
"apihelp-discussiontoolsgetsubscriptions-summary": "Get the subscription statuses of given topics.", "apihelp-discussiontoolsgetsubscriptions-summary": "Get the subscription statuses of given topics.",
"apihelp-discussiontoolspageinfo-param-oldid": "The revision number to use (defaults to latest revision).", "apihelp-discussiontoolspageinfo-param-oldid": "The revision number to use (defaults to latest revision).",
"apihelp-discussiontoolspageinfo-param-prop": "Which properties to get:", "apihelp-discussiontoolspageinfo-param-prop": "Which properties to get:",
"apihelp-discussiontoolspageinfo-param-excludesignatures": "Exclude user signatures from the comments (when using <var>prop=threaditemshtml</var>).",
"apihelp-discussiontoolspageinfo-paramvalue-prop-threaditemshtml": "Representation of the comment threads parsed from the page", "apihelp-discussiontoolspageinfo-paramvalue-prop-threaditemshtml": "Representation of the comment threads parsed from the page",
"apihelp-discussiontoolspageinfo-paramvalue-prop-transcludedfrom": "Which other pages comments have been transcluded from", "apihelp-discussiontoolspageinfo-paramvalue-prop-transcludedfrom": "Which other pages comments have been transcluded from",
"apihelp-discussiontoolspageinfo-summary": "Returns metadata required to initialize the discussion tools.", "apihelp-discussiontoolspageinfo-summary": "Returns metadata required to initialize the discussion tools.",

View file

@ -26,6 +26,7 @@
"apihelp-discussiontoolsgetsubscriptions-summary": "{{doc-apihelp-summary|discussiontoolsgetsubscriptions}}", "apihelp-discussiontoolsgetsubscriptions-summary": "{{doc-apihelp-summary|discussiontoolsgetsubscriptions}}",
"apihelp-discussiontoolspageinfo-param-oldid": "{{doc-apihelp-param|discussiontoolspageinfo|oldid}}", "apihelp-discussiontoolspageinfo-param-oldid": "{{doc-apihelp-param|discussiontoolspageinfo|oldid}}",
"apihelp-discussiontoolspageinfo-param-prop": "{{doc-apihelp-param|discussiontoolspageinfo|prop}}", "apihelp-discussiontoolspageinfo-param-prop": "{{doc-apihelp-param|discussiontoolspageinfo|prop}}",
"apihelp-discussiontoolspageinfo-param-excludesignatures": "{{doc-apihelp-param|discussiontoolspageinfo|excludesignatures}}",
"apihelp-discussiontoolspageinfo-paramvalue-prop-threaditemshtml": "{{doc-apihelp-paramvalue|discussiontoolspageinfo|prop|threaditemshtml}}", "apihelp-discussiontoolspageinfo-paramvalue-prop-threaditemshtml": "{{doc-apihelp-paramvalue|discussiontoolspageinfo|prop|threaditemshtml}}",
"apihelp-discussiontoolspageinfo-paramvalue-prop-transcludedfrom": "{{doc-apihelp-paramvalue|discussiontoolspageinfo|prop|transcludedfrom}}", "apihelp-discussiontoolspageinfo-paramvalue-prop-transcludedfrom": "{{doc-apihelp-paramvalue|discussiontoolspageinfo|prop|transcludedfrom}}",
"apihelp-discussiontoolspageinfo-summary": "{{doc-apihelp-summary|discussiontoolspageinfo}}", "apihelp-discussiontoolspageinfo-summary": "{{doc-apihelp-summary|discussiontoolspageinfo}}",

View file

@ -10,7 +10,7 @@
"discussiontools-defaultsummary-reply": "Reply", "discussiontools-defaultsummary-reply": "Reply",
"discussiontoolsdebug-title": "Discussion tools data structure", "discussiontoolsdebug-title": "Discussion tools data structure",
"discussiontoolsdebug-pagetitle": "Page title", "discussiontoolsdebug-pagetitle": "Page title",
"discussiontoolsdebug-intro": "This page demonstrates how the [https://www.mediawiki.org/wiki/Special:MyLanguage/Extension:DiscussionTools DiscussionTools extension] recognizes the threads and comments present on the page '''[[:$1]]'''. You may find it useful in debugging issues with the extension or understanding how it works. The same information is available in the [[Special:ApiSandbox#action=discussiontoolspageinfo&prop=threaditemshtml&page=$1|discussiontoolspageinfo API]].", "discussiontoolsdebug-intro": "This page demonstrates how the [https://www.mediawiki.org/wiki/Special:MyLanguage/Extension:DiscussionTools DiscussionTools extension] recognizes the threads and comments present on the page '''[[:$1]]'''. You may find it useful in debugging issues with the extension or understanding how it works. The same information is available in the [[$2|discussiontoolspageinfo API]].",
"discussiontools-desc": "Tools to enhance discussion pages.", "discussiontools-desc": "Tools to enhance discussion pages.",
"discussiontools-emptystate-button": "Start a discussion", "discussiontools-emptystate-button": "Start a discussion",
"discussiontools-emptystate-desc": "[[{{MediaWiki:discussiontools-emptystate-link-talkpages}}|Talk pages]] are where people discuss how to make content on {{SITENAME}} the best that it can be. {{GENDER:|You}} can use this page to start a discussion with others about how to improve [[:{{SUBJECTPAGENAME}}]].", "discussiontools-emptystate-desc": "[[{{MediaWiki:discussiontools-emptystate-link-talkpages}}|Talk pages]] are where people discuss how to make content on {{SITENAME}} the best that it can be. {{GENDER:|You}} can use this page to start a discussion with others about how to improve [[:{{SUBJECTPAGENAME}}]].",

View file

@ -7,6 +7,7 @@ use ApiMain;
use ApiUsageException; use ApiUsageException;
use MediaWiki\Extension\DiscussionTools\Hooks\HookUtils; use MediaWiki\Extension\DiscussionTools\Hooks\HookUtils;
use MediaWiki\Extension\DiscussionTools\ThreadItem\CommentItem; use MediaWiki\Extension\DiscussionTools\ThreadItem\CommentItem;
use MediaWiki\Extension\DiscussionTools\ThreadItem\ContentCommentItem;
use MediaWiki\Extension\DiscussionTools\ThreadItem\ContentHeadingItem; use MediaWiki\Extension\DiscussionTools\ThreadItem\ContentHeadingItem;
use MediaWiki\Extension\DiscussionTools\ThreadItem\ContentThreadItem; use MediaWiki\Extension\DiscussionTools\ThreadItem\ContentThreadItem;
use MediaWiki\Extension\VisualEditor\VisualEditorParsoidClientFactory; use MediaWiki\Extension\VisualEditor\VisualEditorParsoidClientFactory;
@ -53,7 +54,8 @@ class ApiDiscussionToolsPageInfo extends ApiBase {
} }
if ( isset( $prop['threaditemshtml'] ) ) { if ( isset( $prop['threaditemshtml'] ) ) {
$result['threaditemshtml'] = static::getThreadItemsHtml( $threadItemSet ); $excludeSignatures = $params['excludesignatures'];
$result['threaditemshtml'] = static::getThreadItemsHtml( $threadItemSet, $excludeSignatures );
} }
$this->getResult()->addValue( null, $this->getModuleName(), $result ); $this->getResult()->addValue( null, $this->getModuleName(), $result );
@ -145,9 +147,10 @@ class ApiDiscussionToolsPageInfo extends ApiBase {
* Get thread items HTML for a ContentThreadItemSet * Get thread items HTML for a ContentThreadItemSet
* *
* @param ContentThreadItemSet $threadItemSet * @param ContentThreadItemSet $threadItemSet
* @param bool $excludeSignatures
* @return array * @return array
*/ */
private static function getThreadItemsHtml( ContentThreadItemSet $threadItemSet ): array { private static function getThreadItemsHtml( ContentThreadItemSet $threadItemSet, bool $excludeSignatures ): array {
// This function assumes that the start of the ranges associated with // This function assumes that the start of the ranges associated with
// HeadingItems are going to be at the start of their associated // HeadingItems are going to be at the start of their associated
// heading node (`<h2>^heading</h2>`), i.e. in the position generated // heading node (`<h2>^heading</h2>`), i.e. in the position generated
@ -172,9 +175,16 @@ class ApiDiscussionToolsPageInfo extends ApiBase {
array_unshift( $threads, $fakeHeading ); array_unshift( $threads, $fakeHeading );
} }
} }
$output = array_map( static function ( ContentThreadItem $item ) { $output = array_map( static function ( ContentThreadItem $item ) use ( $excludeSignatures ) {
return $item->jsonSerialize( true, static function ( array &$array, ContentThreadItem $item ) { return $item->jsonSerialize( true, static function ( array &$array, ContentThreadItem $item ) use (
$array['html'] = $item->getHtml(); $excludeSignatures
) {
if ( $item instanceof ContentCommentItem && $excludeSignatures ) {
$array['html'] = $item->getBodyHTML( true );
} else {
$array['html'] = $item->getHTML();
}
if ( $item instanceof CommentItem ) { if ( $item instanceof CommentItem ) {
// We want timestamps to be consistently formatted in API // We want timestamps to be consistently formatted in API
// output instead of varying based on comment time // output instead of varying based on comment time
@ -279,6 +289,7 @@ class ApiDiscussionToolsPageInfo extends ApiBase {
], ],
ApiBase::PARAM_HELP_MSG_PER_VALUE => [], ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
], ],
'excludesignatures' => false,
]; ];
} }

View file

@ -11,6 +11,7 @@ use MediaWiki\Linker\Linker;
use MediaWiki\Page\ParserOutputAccess; use MediaWiki\Page\ParserOutputAccess;
use MWTimestamp; use MWTimestamp;
use ParserOptions; use ParserOptions;
use SpecialPage;
use Title; use Title;
use Wikimedia\Assert\Assert; use Wikimedia\Assert\Assert;
use Wikimedia\Parsoid\Utils\DOMCompat; use Wikimedia\Parsoid\Utils\DOMCompat;
@ -95,7 +96,16 @@ class SpecialDiscussionToolsDebug extends FormSpecialPage {
$out = $this->getOutput(); $out = $this->getOutput();
$out->addHTML( $this->msg( 'discussiontoolsdebug-intro', $title->getPrefixedText() )->parseAsBlock() ); $out->addHTML( $this->msg(
'discussiontoolsdebug-intro',
$title->getPrefixedText(),
SpecialPage::getTitleFor(
'ApiSandbox',
false,
'action=discussiontoolspageinfo&prop=threaditemshtml&excludesignatures=1&page='
. urlencode( $title->getPrefixedText() )
)->getFullText()
)->parseAsBlock() );
$pageLang = $title->getPageViewLanguage(); $pageLang = $title->getPageViewLanguage();
$pageLangAttribs = [ $pageLangAttribs = [
@ -138,7 +148,7 @@ class SpecialDiscussionToolsDebug extends FormSpecialPage {
'</span>' . '</span>' .
'</span>' . '</span>' .
Html::rawElement( 'div', $pageLangAttribs, Html::rawElement( 'div', $pageLangAttribs,
'<div class="mw-dt-comment-body mw-parser-output">' . $comment->getBodyHtml( true ) . '</div>' '<div class="mw-dt-comment-body mw-parser-output">' . $comment->getBodyHTML( true ) . '</div>'
); );
} }
$level = $comment->getLevel(); $level = $comment->getLevel();

View file

@ -60,7 +60,7 @@ class ApiDiscussionToolsPageInfoTest extends ApiTestCase {
$pageInfo = TestingAccessWrapper::newFromClass( ApiDiscussionToolsPageInfo::class ); $pageInfo = TestingAccessWrapper::newFromClass( ApiDiscussionToolsPageInfo::class );
$threadItemsHtml = $pageInfo->getThreadItemsHtml( $threadItemSet ); $threadItemsHtml = $pageInfo->getThreadItemsHtml( $threadItemSet, false );
// Optionally write updated content to the JSON files // Optionally write updated content to the JSON files
if ( getenv( 'DISCUSSIONTOOLS_OVERWRITE_TESTS' ) ) { if ( getenv( 'DISCUSSIONTOOLS_OVERWRITE_TESTS' ) ) {