From 2d40cbb6d58328d6a9c7256dde3192adaccfa62c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Dziewo=C5=84ski?= Date: Sat, 20 May 2023 17:18:13 +0300 Subject: [PATCH] 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 --- i18n/api/en.json | 1 + i18n/api/qqq.json | 1 + i18n/en.json | 2 +- includes/ApiDiscussionToolsPageInfo.php | 21 ++++++++++++++----- includes/SpecialDiscussionToolsDebug.php | 14 +++++++++++-- .../ApiDiscussionToolsPageInfoTest.php | 2 +- 6 files changed, 32 insertions(+), 9 deletions(-) diff --git a/i18n/api/en.json b/i18n/api/en.json index 7920ff86c..ba4f53813 100644 --- a/i18n/api/en.json +++ b/i18n/api/en.json @@ -24,6 +24,7 @@ "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-prop": "Which properties to get:", + "apihelp-discussiontoolspageinfo-param-excludesignatures": "Exclude user signatures from the comments (when using prop=threaditemshtml).", "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-summary": "Returns metadata required to initialize the discussion tools.", diff --git a/i18n/api/qqq.json b/i18n/api/qqq.json index df1773c31..6d27359b2 100644 --- a/i18n/api/qqq.json +++ b/i18n/api/qqq.json @@ -26,6 +26,7 @@ "apihelp-discussiontoolsgetsubscriptions-summary": "{{doc-apihelp-summary|discussiontoolsgetsubscriptions}}", "apihelp-discussiontoolspageinfo-param-oldid": "{{doc-apihelp-param|discussiontoolspageinfo|oldid}}", "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-transcludedfrom": "{{doc-apihelp-paramvalue|discussiontoolspageinfo|prop|transcludedfrom}}", "apihelp-discussiontoolspageinfo-summary": "{{doc-apihelp-summary|discussiontoolspageinfo}}", diff --git a/i18n/en.json b/i18n/en.json index e75b22c43..aaf8f7358 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -10,7 +10,7 @@ "discussiontools-defaultsummary-reply": "Reply", "discussiontoolsdebug-title": "Discussion tools data structure", "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-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}}]].", diff --git a/includes/ApiDiscussionToolsPageInfo.php b/includes/ApiDiscussionToolsPageInfo.php index 2c668b530..310431247 100644 --- a/includes/ApiDiscussionToolsPageInfo.php +++ b/includes/ApiDiscussionToolsPageInfo.php @@ -7,6 +7,7 @@ use ApiMain; use ApiUsageException; use MediaWiki\Extension\DiscussionTools\Hooks\HookUtils; use MediaWiki\Extension\DiscussionTools\ThreadItem\CommentItem; +use MediaWiki\Extension\DiscussionTools\ThreadItem\ContentCommentItem; use MediaWiki\Extension\DiscussionTools\ThreadItem\ContentHeadingItem; use MediaWiki\Extension\DiscussionTools\ThreadItem\ContentThreadItem; use MediaWiki\Extension\VisualEditor\VisualEditorParsoidClientFactory; @@ -53,7 +54,8 @@ class ApiDiscussionToolsPageInfo extends ApiBase { } 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 ); @@ -145,9 +147,10 @@ class ApiDiscussionToolsPageInfo extends ApiBase { * Get thread items HTML for a ContentThreadItemSet * * @param ContentThreadItemSet $threadItemSet + * @param bool $excludeSignatures * @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 // HeadingItems are going to be at the start of their associated // heading node (`

^heading

`), i.e. in the position generated @@ -172,9 +175,16 @@ class ApiDiscussionToolsPageInfo extends ApiBase { array_unshift( $threads, $fakeHeading ); } } - $output = array_map( static function ( ContentThreadItem $item ) { - return $item->jsonSerialize( true, static function ( array &$array, ContentThreadItem $item ) { - $array['html'] = $item->getHtml(); + $output = array_map( static function ( ContentThreadItem $item ) use ( $excludeSignatures ) { + return $item->jsonSerialize( true, static function ( array &$array, ContentThreadItem $item ) use ( + $excludeSignatures + ) { + if ( $item instanceof ContentCommentItem && $excludeSignatures ) { + $array['html'] = $item->getBodyHTML( true ); + } else { + $array['html'] = $item->getHTML(); + } + if ( $item instanceof CommentItem ) { // We want timestamps to be consistently formatted in API // output instead of varying based on comment time @@ -279,6 +289,7 @@ class ApiDiscussionToolsPageInfo extends ApiBase { ], ApiBase::PARAM_HELP_MSG_PER_VALUE => [], ], + 'excludesignatures' => false, ]; } diff --git a/includes/SpecialDiscussionToolsDebug.php b/includes/SpecialDiscussionToolsDebug.php index 901967507..19ae4964a 100644 --- a/includes/SpecialDiscussionToolsDebug.php +++ b/includes/SpecialDiscussionToolsDebug.php @@ -11,6 +11,7 @@ use MediaWiki\Linker\Linker; use MediaWiki\Page\ParserOutputAccess; use MWTimestamp; use ParserOptions; +use SpecialPage; use Title; use Wikimedia\Assert\Assert; use Wikimedia\Parsoid\Utils\DOMCompat; @@ -95,7 +96,16 @@ class SpecialDiscussionToolsDebug extends FormSpecialPage { $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(); $pageLangAttribs = [ @@ -138,7 +148,7 @@ class SpecialDiscussionToolsDebug extends FormSpecialPage { '' . '' . Html::rawElement( 'div', $pageLangAttribs, - '
' . $comment->getBodyHtml( true ) . '
' + '
' . $comment->getBodyHTML( true ) . '
' ); } $level = $comment->getLevel(); diff --git a/tests/phpunit/integration/ApiDiscussionToolsPageInfoTest.php b/tests/phpunit/integration/ApiDiscussionToolsPageInfoTest.php index c89b91b18..956b4605c 100644 --- a/tests/phpunit/integration/ApiDiscussionToolsPageInfoTest.php +++ b/tests/phpunit/integration/ApiDiscussionToolsPageInfoTest.php @@ -60,7 +60,7 @@ class ApiDiscussionToolsPageInfoTest extends ApiTestCase { $pageInfo = TestingAccessWrapper::newFromClass( ApiDiscussionToolsPageInfo::class ); - $threadItemsHtml = $pageInfo->getThreadItemsHtml( $threadItemSet ); + $threadItemsHtml = $pageInfo->getThreadItemsHtml( $threadItemSet, false ); // Optionally write updated content to the JSON files if ( getenv( 'DISCUSSIONTOOLS_OVERWRITE_TESTS' ) ) {