2020-09-16 12:07:27 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace MediaWiki\Extension\DiscussionTools;
|
|
|
|
|
2022-06-23 16:18:00 +00:00
|
|
|
use Html;
|
2021-01-06 17:06:27 +00:00
|
|
|
use Language;
|
2021-04-08 12:30:28 +00:00
|
|
|
use MediaWiki\Extension\DiscussionTools\Hooks\HookUtils;
|
2022-03-18 03:28:06 +00:00
|
|
|
use MediaWiki\Extension\DiscussionTools\ThreadItem\ContentCommentItem;
|
|
|
|
use MediaWiki\Extension\DiscussionTools\ThreadItem\ContentHeadingItem;
|
2022-08-12 17:13:48 +00:00
|
|
|
use MediaWiki\Extension\DiscussionTools\ThreadItem\ContentThreadItem;
|
2021-01-29 18:31:27 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2021-04-19 19:14:07 +00:00
|
|
|
use MediaWiki\User\UserIdentity;
|
2021-01-29 18:31:27 +00:00
|
|
|
use MWExceptionHandler;
|
2021-01-28 17:19:52 +00:00
|
|
|
use MWTimestamp;
|
2022-04-07 00:00:05 +00:00
|
|
|
use Parser;
|
2021-09-27 19:38:09 +00:00
|
|
|
use ParserOutput;
|
2022-08-04 13:16:07 +00:00
|
|
|
use Sanitizer;
|
2021-01-29 18:31:27 +00:00
|
|
|
use Throwable;
|
2022-01-11 15:50:44 +00:00
|
|
|
use Title;
|
2021-01-29 18:31:27 +00:00
|
|
|
use WebRequest;
|
2022-02-19 03:25:14 +00:00
|
|
|
use Wikimedia\Assert\Assert;
|
|
|
|
use Wikimedia\Parsoid\DOM\Element;
|
2021-04-19 19:14:07 +00:00
|
|
|
use Wikimedia\Parsoid\Utils\DOMCompat;
|
2020-11-16 15:50:46 +00:00
|
|
|
use Wikimedia\Parsoid\Utils\DOMUtils;
|
2021-02-13 19:01:58 +00:00
|
|
|
use Wikimedia\Parsoid\Wt2Html\XMLSerializer;
|
2020-09-16 12:07:27 +00:00
|
|
|
|
2020-11-16 15:50:46 +00:00
|
|
|
class CommentFormatter {
|
2021-02-17 17:16:17 +00:00
|
|
|
// List of features which, when enabled, cause the comment formatter to run
|
|
|
|
public const USE_WITH_FEATURES = [
|
2021-04-08 12:30:28 +00:00
|
|
|
HookUtils::REPLYTOOL,
|
|
|
|
HookUtils::TOPICSUBSCRIPTION,
|
2021-01-28 17:19:52 +00:00
|
|
|
HookUtils::VISUALENHANCEMENTS
|
2021-02-17 17:16:17 +00:00
|
|
|
];
|
|
|
|
|
2021-01-08 22:19:20 +00:00
|
|
|
/**
|
|
|
|
* Get a comment parser object for a DOM element
|
|
|
|
*
|
|
|
|
* This method exists so it can mocked in tests.
|
|
|
|
*
|
|
|
|
* @return CommentParser
|
|
|
|
*/
|
Change CommentParser into a service
Goal:
-----
To have a method like CommentParser::parse(), which just takes a node
to parse and a title and returns plain data, so that we don't need to
keep track of the config to construct a CommentParser object (the
required config like content language is provided by services) and
we don't need to keep that object around after parsing.
Changes:
--------
CommentParser.php:
* …is now a service. Constructor only takes services as arguments.
The node and title are passed to a new parse() method.
* parse() should return plain data, but I split this part to a separate
patch for ease of review: I49bfe019aa460651447fd383f73eafa9d7180a92.
* CommentParser still cheats and accesses global state in a few places,
e.g. calling Title::makeTitleSafe or CommentUtils::getTitleFromUrl,
so we can't turn its tests into true unit tests. This work is left
for future commits.
LanguageData.php:
* …is now a service, instead of a static class.
Parser.js:
* …is not a real service, but it's changed to behave in a similar way.
Constructor takes only the required config as argument,
and node and title are instead passed to a new parse() method.
CommentParserTest.php:
parser.test.js:
* Can be simplified, now that we don't need a useless node and title
to test internal methods that don't use them.
testUtils.js:
* Can be simplified, now that we don't need to override internal
ResourceLoader stuff just to change the parser config.
Change-Id: Iadb7757debe000025e52770ca51ebcf24ca8ee66
2022-02-19 02:43:21 +00:00
|
|
|
protected static function getParser(): CommentParser {
|
|
|
|
return MediaWikiServices::getInstance()->getService( 'DiscussionTools.CommentParser' );
|
2021-01-08 22:19:20 +00:00
|
|
|
}
|
|
|
|
|
2021-01-29 18:31:27 +00:00
|
|
|
/**
|
2021-04-19 18:34:55 +00:00
|
|
|
* Add discussion tools to some HTML
|
2021-01-29 18:31:27 +00:00
|
|
|
*
|
2021-09-27 19:38:09 +00:00
|
|
|
* @param string &$text Parser text output (modified by reference)
|
2021-10-07 15:58:17 +00:00
|
|
|
* @param ParserOutput $pout ParserOutput object for metadata, e.g. parser limit report
|
2022-04-07 00:00:05 +00:00
|
|
|
* @param Parser $parser
|
2021-01-29 18:31:27 +00:00
|
|
|
*/
|
2022-04-07 00:00:05 +00:00
|
|
|
public static function addDiscussionTools( string &$text, ParserOutput $pout, Parser $parser ): void {
|
|
|
|
$title = $parser->getTitle();
|
2021-01-29 18:31:27 +00:00
|
|
|
$start = microtime( true );
|
2021-09-27 19:38:09 +00:00
|
|
|
$requestId = null;
|
2021-01-29 18:31:27 +00:00
|
|
|
|
|
|
|
try {
|
2022-04-07 00:00:05 +00:00
|
|
|
[ 'html' => $text, 'tocInfo' => $tocInfo ] =
|
2022-08-12 17:13:48 +00:00
|
|
|
static::addDiscussionToolsInternal( $text, $pout, $title );
|
2022-04-07 00:00:05 +00:00
|
|
|
|
|
|
|
// Enhance the table of contents in supporting skins (vector-2022)
|
|
|
|
|
|
|
|
// Only do the work if the HTML would be shown. It looks like we can only check this
|
|
|
|
// by checking whether the HTML for the normal TOC has been generated. Code in
|
|
|
|
// OutputPage::addParserOutputMetadata does the same.
|
|
|
|
if ( $pout->getTOCHTML() ) {
|
|
|
|
// If the TOC HTML has been generated, then the parser cache is already split by user
|
|
|
|
// language (because of the "Contents" header in the TOC), so we can render text in user
|
|
|
|
// language as well. If that behavior in core changes, then we'll have to change this to
|
|
|
|
// happen in a post-processing step (like all other transformations) to avoid splitting it.
|
|
|
|
$lang = $parser->getOptions()->getUserLangObj();
|
|
|
|
$sections = $pout->getSections();
|
|
|
|
foreach ( $sections as &$item ) {
|
|
|
|
$key = str_replace( '_', ' ', $item['anchor'] );
|
|
|
|
// Unset if we did not format this section as a topic container
|
|
|
|
if ( isset( $tocInfo[$key] ) ) {
|
|
|
|
$count = $lang->formatNum( $tocInfo[$key]['commentCount'] );
|
|
|
|
$commentCount = wfMessage(
|
|
|
|
'discussiontools-topicheader-commentcount',
|
|
|
|
$count
|
|
|
|
)->inLanguage( $lang )->text();
|
|
|
|
|
|
|
|
$summary = Html::element( 'span', [
|
|
|
|
'class' => 'ext-discussiontools-init-sidebar-meta'
|
2022-09-02 13:43:19 +00:00
|
|
|
], $commentCount );
|
2022-04-07 00:00:05 +00:00
|
|
|
|
|
|
|
// This also shows up in API action=parse&prop=sections output.
|
|
|
|
$item['html-summary'] = $summary;
|
2022-09-06 20:53:51 +00:00
|
|
|
} else {
|
|
|
|
$item['html-summary'] = '';
|
2022-04-07 00:00:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$pout->setSections( $sections );
|
|
|
|
}
|
|
|
|
|
2021-01-29 18:31:27 +00:00
|
|
|
} catch ( Throwable $e ) {
|
|
|
|
// Catch errors, so that they don't cause the entire page to not display.
|
2021-09-27 19:38:09 +00:00
|
|
|
// Log it and report the request ID to make it easier to find in the logs.
|
2021-01-29 18:31:27 +00:00
|
|
|
MWExceptionHandler::logException( $e );
|
2021-09-27 19:38:09 +00:00
|
|
|
$requestId = WebRequest::getRequestId();
|
2021-01-29 18:31:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$duration = microtime( true ) - $start;
|
|
|
|
|
|
|
|
$stats = MediaWikiServices::getInstance()->getStatsdDataFactory();
|
|
|
|
$stats->timing( 'discussiontools.addReplyLinks', $duration * 1000 );
|
2021-09-27 19:38:09 +00:00
|
|
|
|
2021-10-07 15:58:17 +00:00
|
|
|
// How long this method took, in seconds
|
|
|
|
$pout->setLimitReportData(
|
|
|
|
'discussiontools-limitreport-timeusage',
|
|
|
|
sprintf( '%.3f', $duration )
|
|
|
|
);
|
|
|
|
if ( $requestId ) {
|
|
|
|
// Request ID where errors were logged (only if an error occurred)
|
2021-09-27 19:38:09 +00:00
|
|
|
$pout->setLimitReportData(
|
2021-10-07 15:58:17 +00:00
|
|
|
'discussiontools-limitreport-errorreqid',
|
|
|
|
$requestId
|
2021-09-27 19:38:09 +00:00
|
|
|
);
|
|
|
|
}
|
2021-01-29 18:31:27 +00:00
|
|
|
}
|
|
|
|
|
2021-01-28 17:19:52 +00:00
|
|
|
/**
|
|
|
|
* Add a topic container around a heading element
|
|
|
|
*
|
|
|
|
* @param Element $headingElement Heading element
|
2022-03-18 03:28:06 +00:00
|
|
|
* @param ContentHeadingItem|null $headingItem Heading item
|
2022-04-07 00:00:05 +00:00
|
|
|
* @param array|null &$tocInfo TOC info
|
2022-11-15 22:34:16 +00:00
|
|
|
* @return Element Wrapper element (either found or newly added), or the heading element if not using wrappers
|
2021-01-28 17:19:52 +00:00
|
|
|
*/
|
2022-04-07 00:00:05 +00:00
|
|
|
protected static function addTopicContainer(
|
|
|
|
Element $headingElement,
|
|
|
|
?ContentHeadingItem $headingItem = null,
|
|
|
|
&$tocInfo = null
|
2022-11-15 22:34:16 +00:00
|
|
|
): Element {
|
2022-10-18 00:27:35 +00:00
|
|
|
$legacyMarkup = MediaWikiServices::getInstance()->getMainConfig()->get( 'DiscussionToolsLegacyHeadingMarkup' );
|
2021-01-28 17:19:52 +00:00
|
|
|
$doc = $headingElement->ownerDocument;
|
|
|
|
|
2022-10-18 00:27:35 +00:00
|
|
|
if ( $legacyMarkup ) {
|
|
|
|
$wrapperNode = $headingElement;
|
|
|
|
} else {
|
|
|
|
$wrapperNode = $headingElement->parentNode;
|
|
|
|
if ( !(
|
|
|
|
$wrapperNode instanceof Element &&
|
|
|
|
DOMCompat::getClassList( $wrapperNode )->contains( 'mw-heading' )
|
|
|
|
) ) {
|
|
|
|
$wrapperNode = $doc->createElement( 'div' );
|
|
|
|
$wrapperNode->setAttribute( 'class', 'mw-heading mw-heading2' );
|
|
|
|
$headingElement->parentNode->insertBefore( $wrapperNode, $headingElement );
|
|
|
|
$wrapperNode->appendChild( $headingElement );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DOMCompat::getClassList( $wrapperNode )->add( 'ext-discussiontools-init-section' );
|
2021-01-28 17:19:52 +00:00
|
|
|
|
|
|
|
if ( !$headingItem ) {
|
2022-11-15 22:34:16 +00:00
|
|
|
return $wrapperNode;
|
2021-01-28 17:19:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$headingNameEscaped = htmlspecialchars( $headingItem->getName(), ENT_NOQUOTES );
|
|
|
|
|
|
|
|
// Replaced in ::postprocessTopicSubscription() as the text depends on user state
|
2022-07-22 21:46:41 +00:00
|
|
|
if ( $headingItem->isSubscribable() ) {
|
2022-08-22 22:43:41 +00:00
|
|
|
$subscribeLink = $doc->createComment( '__DTSUBSCRIBELINK__' . $headingNameEscaped );
|
2022-07-20 12:58:35 +00:00
|
|
|
$headingElement->insertBefore( $subscribeLink, $headingElement->firstChild );
|
2021-01-28 17:19:52 +00:00
|
|
|
|
2022-08-22 22:43:41 +00:00
|
|
|
$subscribeButton = $doc->createComment( '__DTSUBSCRIBEBUTTONDESKTOP__' . $headingNameEscaped );
|
2022-10-18 00:27:35 +00:00
|
|
|
$wrapperNode->insertBefore( $subscribeButton, $wrapperNode->firstChild );
|
2022-08-22 22:43:41 +00:00
|
|
|
}
|
2022-07-20 13:05:21 +00:00
|
|
|
|
2022-10-02 13:32:55 +00:00
|
|
|
$ellipsisButton = $doc->createComment( '__DTELLIPSISBUTTON__' );
|
2022-10-18 00:27:35 +00:00
|
|
|
$wrapperNode->appendChild( $ellipsisButton );
|
2022-10-02 13:32:55 +00:00
|
|
|
|
2021-01-28 17:19:52 +00:00
|
|
|
// Visual enhancements: topic containers
|
2022-07-05 23:21:34 +00:00
|
|
|
$latestReplyItem = $headingItem->getLatestReply();
|
|
|
|
if ( $latestReplyItem ) {
|
|
|
|
$latestReplyJSON = static::getJsonForCommentMarker( $latestReplyItem );
|
2021-01-28 17:19:52 +00:00
|
|
|
$latestReply = $doc->createComment(
|
|
|
|
// Timestamp output varies by user timezone, so is formatted later
|
2022-06-23 16:18:00 +00:00
|
|
|
'__DTLATESTCOMMENTTHREAD__' . htmlspecialchars( $latestReplyJSON, ENT_NOQUOTES ) . '__'
|
2021-01-28 17:19:52 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$commentCount = $doc->createComment(
|
2022-07-05 23:21:34 +00:00
|
|
|
'__DTCOMMENTCOUNT__' . $headingItem->getCommentCount() . '__'
|
2021-01-28 17:19:52 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$authorCount = $doc->createComment(
|
2022-07-05 23:21:34 +00:00
|
|
|
'__DTAUTHORCOUNT__' . count( $headingItem->getAuthorsBelow() ) . '__'
|
2021-01-28 17:19:52 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// Topic subscriptions
|
|
|
|
$metadata = $doc->createElement( 'div' );
|
|
|
|
$metadata->setAttribute(
|
|
|
|
'class',
|
|
|
|
'ext-discussiontools-init-section-metadata'
|
|
|
|
);
|
|
|
|
|
|
|
|
$metadata->appendChild( $latestReply );
|
|
|
|
$metadata->appendChild( $commentCount );
|
|
|
|
$metadata->appendChild( $authorCount );
|
|
|
|
|
|
|
|
$actions = $doc->createElement( 'div' );
|
|
|
|
$actions->setAttribute(
|
|
|
|
'class',
|
|
|
|
'ext-discussiontools-init-section-actions'
|
|
|
|
);
|
|
|
|
|
2022-07-22 21:46:41 +00:00
|
|
|
if ( $headingItem->isSubscribable() ) {
|
2022-08-22 22:43:41 +00:00
|
|
|
$subscribeButton = $doc->createComment( '__DTSUBSCRIBEBUTTONMOBILE__' . $headingNameEscaped );
|
2022-07-22 21:46:41 +00:00
|
|
|
$actions->appendChild( $subscribeButton );
|
|
|
|
}
|
2021-01-28 17:19:52 +00:00
|
|
|
|
|
|
|
$bar = $doc->createElement( 'div' );
|
|
|
|
$bar->setAttribute(
|
|
|
|
'class',
|
|
|
|
'ext-discussiontools-init-section-bar'
|
|
|
|
);
|
|
|
|
|
|
|
|
$bar->appendChild( $metadata );
|
|
|
|
$bar->appendChild( $actions );
|
|
|
|
|
2022-10-18 00:27:35 +00:00
|
|
|
$wrapperNode->appendChild( $bar );
|
2022-04-07 00:00:05 +00:00
|
|
|
|
2022-09-06 20:53:51 +00:00
|
|
|
$tocInfo[ $headingItem->getLinkableTitle() ] = [
|
|
|
|
'commentCount' => $headingItem->getCommentCount(),
|
|
|
|
];
|
|
|
|
}
|
2022-11-15 22:34:16 +00:00
|
|
|
|
|
|
|
return $wrapperNode;
|
2021-01-28 17:19:52 +00:00
|
|
|
}
|
|
|
|
|
2020-11-16 15:50:46 +00:00
|
|
|
/**
|
2021-04-19 18:34:55 +00:00
|
|
|
* Add discussion tools to some HTML
|
2020-11-16 15:50:46 +00:00
|
|
|
*
|
|
|
|
* @param string $html HTML
|
2022-08-12 17:13:48 +00:00
|
|
|
* @param ParserOutput $pout
|
2022-01-11 15:50:44 +00:00
|
|
|
* @param Title $title
|
2022-04-07 00:00:05 +00:00
|
|
|
* @return array HTML with discussion tools and TOC info
|
2020-11-16 15:50:46 +00:00
|
|
|
*/
|
2022-08-12 17:13:48 +00:00
|
|
|
protected static function addDiscussionToolsInternal( string $html, ParserOutput $pout, Title $title ): array {
|
2020-10-21 15:52:04 +00:00
|
|
|
// The output of this method can end up in the HTTP cache (Varnish). Avoid changing it;
|
|
|
|
// and when doing so, ensure that frontend code can handle both the old and new outputs.
|
|
|
|
// See controller#init in JS.
|
|
|
|
|
2020-11-16 15:50:46 +00:00
|
|
|
$doc = DOMUtils::parseHTML( $html );
|
2021-08-02 13:45:39 +00:00
|
|
|
$container = DOMCompat::getBody( $doc );
|
2020-09-16 12:07:27 +00:00
|
|
|
|
2022-02-21 22:07:38 +00:00
|
|
|
$threadItemSet = static::getParser()->parse( $container, $title->getTitleValue() );
|
2022-02-19 06:31:34 +00:00
|
|
|
$threadItems = $threadItemSet->getThreadItems();
|
2020-09-16 12:07:27 +00:00
|
|
|
|
2022-04-07 00:00:05 +00:00
|
|
|
$tocInfo = [];
|
|
|
|
|
2022-04-21 14:29:56 +00:00
|
|
|
$newestComment = null;
|
|
|
|
$newestCommentJSON = null;
|
|
|
|
|
2021-12-21 14:20:18 +00:00
|
|
|
// Iterate in reverse order, because adding the range markers for a thread item
|
|
|
|
// can invalidate the ranges of subsequent thread items (T298096)
|
|
|
|
foreach ( array_reverse( $threadItems ) as $threadItem ) {
|
2020-09-16 12:07:27 +00:00
|
|
|
// Create a dummy node to attach data to.
|
2022-03-18 03:28:06 +00:00
|
|
|
if ( $threadItem instanceof ContentHeadingItem && $threadItem->isPlaceholderHeading() ) {
|
2020-09-16 12:07:27 +00:00
|
|
|
$node = $doc->createElement( 'span' );
|
2021-02-22 22:02:41 +00:00
|
|
|
$container->insertBefore( $node, $container->firstChild );
|
2020-09-16 12:07:27 +00:00
|
|
|
$threadItem->setRange( new ImmutableRange( $node, 0, $node, 0 ) );
|
|
|
|
}
|
|
|
|
|
2021-12-21 14:20:18 +00:00
|
|
|
// Add start and end markers to range
|
2020-10-27 12:18:36 +00:00
|
|
|
$id = $threadItem->getId();
|
|
|
|
$range = $threadItem->getRange();
|
|
|
|
$startMarker = $doc->createElement( 'span' );
|
2021-03-10 17:25:52 +00:00
|
|
|
$startMarker->setAttribute( 'data-mw-comment-start', '' );
|
|
|
|
$startMarker->setAttribute( 'id', $id );
|
2020-10-27 12:18:36 +00:00
|
|
|
$endMarker = $doc->createElement( 'span' );
|
|
|
|
$endMarker->setAttribute( 'data-mw-comment-end', $id );
|
2020-12-14 16:57:51 +00:00
|
|
|
|
|
|
|
// Extend the range if the start or end is inside an element which can't have element children.
|
|
|
|
// (There may be other problematic elements... but this seems like a good start.)
|
2022-02-14 19:21:35 +00:00
|
|
|
while ( CommentUtils::cantHaveElementChildren( $range->startContainer ) ) {
|
2020-12-14 16:57:51 +00:00
|
|
|
$range = $range->setStart(
|
|
|
|
$range->startContainer->parentNode,
|
|
|
|
CommentUtils::childIndexOf( $range->startContainer )
|
|
|
|
);
|
|
|
|
}
|
2022-02-14 19:21:35 +00:00
|
|
|
while ( CommentUtils::cantHaveElementChildren( $range->endContainer ) ) {
|
2020-12-14 16:57:51 +00:00
|
|
|
$range = $range->setEnd(
|
|
|
|
$range->endContainer->parentNode,
|
|
|
|
CommentUtils::childIndexOf( $range->endContainer ) + 1
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-10-27 12:18:36 +00:00
|
|
|
$range->setStart( $range->endContainer, $range->endOffset )->insertNode( $endMarker );
|
|
|
|
$range->insertNode( $startMarker );
|
2020-09-16 12:07:27 +00:00
|
|
|
|
2022-03-18 03:28:06 +00:00
|
|
|
if ( $threadItem instanceof ContentHeadingItem ) {
|
2022-02-19 03:25:14 +00:00
|
|
|
// <span class="mw-headline" …>, or <hN …> in Parsoid HTML
|
|
|
|
$headline = $threadItem->getRange()->endContainer;
|
|
|
|
Assert::precondition( $headline instanceof Element, 'HeadingItem refers to an element node' );
|
2022-08-12 17:13:48 +00:00
|
|
|
$headline->setAttribute( 'data-mw-thread-id', $threadItem->getId() );
|
2022-07-22 21:46:41 +00:00
|
|
|
if ( $threadItem->getHeadingLevel() === 2 ) {
|
2021-01-28 17:19:52 +00:00
|
|
|
$headingElement = CommentUtils::closestElement( $headline, [ 'h2' ] );
|
2022-06-20 11:04:27 +00:00
|
|
|
|
2021-01-28 17:19:52 +00:00
|
|
|
if ( $headingElement ) {
|
2022-04-07 00:00:05 +00:00
|
|
|
static::addTopicContainer( $headingElement, $threadItem, $tocInfo );
|
2021-02-17 22:34:02 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-18 03:28:06 +00:00
|
|
|
} elseif ( $threadItem instanceof ContentCommentItem ) {
|
2022-08-06 13:49:36 +00:00
|
|
|
$replyButtons = $doc->createElement( 'span' );
|
|
|
|
$replyButtons->setAttribute( 'class', 'ext-discussiontools-init-replylink-buttons' );
|
2022-08-12 17:13:48 +00:00
|
|
|
$replyButtons->setAttribute( 'data-mw-thread-id', $threadItem->getId() );
|
2022-08-06 13:49:36 +00:00
|
|
|
$replyButtons->appendChild( $doc->createComment( '__DTREPLYBUTTONSCONTENT__' ) );
|
2022-04-21 14:29:56 +00:00
|
|
|
|
|
|
|
if ( !$newestComment || $threadItem->getTimestamp() > $newestComment->getTimestamp() ) {
|
|
|
|
$newestComment = $threadItem;
|
|
|
|
// Needs to calculated before DOM modifications change ranges
|
|
|
|
$newestCommentJSON = static::getJsonForCommentMarker( $threadItem, true );
|
|
|
|
}
|
|
|
|
|
2022-04-21 13:08:59 +00:00
|
|
|
CommentModifier::addReplyLink( $threadItem, $replyButtons );
|
2020-09-16 12:07:27 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-16 15:50:46 +00:00
|
|
|
|
2022-04-21 14:29:56 +00:00
|
|
|
if ( $newestCommentJSON ) {
|
|
|
|
$newestCommentMarker = $doc->createComment(
|
|
|
|
'__DTLATESTCOMMENTPAGE__' . htmlspecialchars( $newestCommentJSON, ENT_NOQUOTES ) . '__'
|
|
|
|
);
|
|
|
|
$container->appendChild( $newestCommentMarker );
|
|
|
|
}
|
|
|
|
|
2022-09-27 14:48:25 +00:00
|
|
|
$firstHeading = null;
|
|
|
|
|
2021-01-28 17:19:52 +00:00
|
|
|
// Enhance other <h2>'s which aren't part of a thread
|
|
|
|
$headings = DOMCompat::querySelectorAll( $container, 'h2' );
|
|
|
|
foreach ( $headings as $headingElement ) {
|
2022-10-18 04:00:31 +00:00
|
|
|
$wrapper = $headingElement->parentNode;
|
|
|
|
if ( $wrapper instanceof Element && DOMCompat::getClassList( $wrapper )->contains( 'toctitle' ) ) {
|
|
|
|
continue;
|
|
|
|
}
|
2022-11-15 22:34:16 +00:00
|
|
|
$headingElement = static::addTopicContainer( $headingElement );
|
2022-09-27 14:48:25 +00:00
|
|
|
if ( !$firstHeading ) {
|
|
|
|
$firstHeading = $headingElement;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
// Page has no headings but some content
|
|
|
|
( !$firstHeading && $container->childNodes->length ) ||
|
|
|
|
// Page has content before the first heading
|
|
|
|
( $firstHeading && $firstHeading->previousSibling !== null )
|
|
|
|
) {
|
|
|
|
$container->appendChild( $doc->createComment( '__DTHASLEDECONTENT__' ) );
|
2021-01-28 17:19:52 +00:00
|
|
|
}
|
|
|
|
|
2022-05-21 05:09:25 +00:00
|
|
|
if ( count( $threadItems ) === 0 ) {
|
|
|
|
$container->appendChild( $doc->createComment( '__DTEMPTYTALKPAGE__' ) );
|
|
|
|
}
|
|
|
|
|
2022-08-12 17:13:48 +00:00
|
|
|
$threadsJSON = array_map( static function ( ContentThreadItem $item ) {
|
|
|
|
return $item->jsonSerialize( true );
|
|
|
|
}, $threadItemSet->getThreadsStructured() );
|
|
|
|
|
|
|
|
$pout->setJsConfigVar( 'wgDiscussionToolsPageThreads', $threadsJSON );
|
|
|
|
|
2021-02-13 19:01:58 +00:00
|
|
|
// Like DOMCompat::getInnerHTML(), but disable 'smartQuote' for compatibility with
|
|
|
|
// ParserOutput::EDITSECTION_REGEX matching 'mw:editsection' tags (T274709)
|
2022-04-07 00:00:05 +00:00
|
|
|
$html = XMLSerializer::serialize( $container, [ 'innerXML' => true, 'smartQuote' => false ] )['html'];
|
|
|
|
|
|
|
|
return [ 'html' => $html, 'tocInfo' => $tocInfo ];
|
2020-09-16 12:07:27 +00:00
|
|
|
}
|
|
|
|
|
2022-06-01 21:43:42 +00:00
|
|
|
/**
|
|
|
|
* Replace placeholders for all interactive tools with nothing. This is intended for cases where
|
|
|
|
* interaction is unexpected, e.g. reply links while previewing an edit.
|
|
|
|
*
|
|
|
|
* @param string $text
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function removeInteractiveTools( string $text ) {
|
|
|
|
$text = strtr( $text, [
|
2022-08-06 13:49:36 +00:00
|
|
|
'<!--__DTREPLYBUTTONSCONTENT__-->' => '',
|
2022-06-01 21:43:42 +00:00
|
|
|
'<!--__DTELLIPSISBUTTON__-->' => '',
|
|
|
|
'<!--__DTEMPTYTALKPAGE__-->' => '',
|
|
|
|
] );
|
|
|
|
|
|
|
|
$text = preg_replace( '/<!--__DTSUBSCRIBELINK__(.*?)-->/', '', $text );
|
2022-08-01 23:09:41 +00:00
|
|
|
$text = preg_replace( '/<!--__DTSUBSCRIBEBUTTON(DESKTOP|MOBILE)__(.*?)-->/', '', $text );
|
2022-06-01 21:43:42 +00:00
|
|
|
|
|
|
|
return $text;
|
|
|
|
}
|
|
|
|
|
2021-04-19 19:14:07 +00:00
|
|
|
/**
|
|
|
|
* Replace placeholders for topic subscription buttons with the real thing.
|
|
|
|
*
|
|
|
|
* @param string $text
|
|
|
|
* @param Language $lang
|
|
|
|
* @param SubscriptionStore $subscriptionStore
|
|
|
|
* @param UserIdentity $user
|
2022-07-20 13:05:21 +00:00
|
|
|
* @param bool $isMobile
|
2021-04-19 19:14:07 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function postprocessTopicSubscription(
|
2022-07-20 13:05:21 +00:00
|
|
|
string $text, Language $lang, SubscriptionStore $subscriptionStore, UserIdentity $user, bool $isMobile
|
2021-07-22 07:25:13 +00:00
|
|
|
): string {
|
2021-07-29 02:13:09 +00:00
|
|
|
$doc = DOMCompat::newDocument( true );
|
2021-04-23 18:54:49 +00:00
|
|
|
|
|
|
|
$matches = [];
|
2022-08-01 23:09:41 +00:00
|
|
|
preg_match_all( '/<!--__DTSUBSCRIBELINK__(.*?)-->/', $text, $matches );
|
2022-06-20 11:04:27 +00:00
|
|
|
$itemNames = array_map(
|
2022-08-01 23:09:41 +00:00
|
|
|
static function ( string $itemName ): string {
|
|
|
|
return htmlspecialchars_decode( $itemName );
|
2022-06-20 11:04:27 +00:00
|
|
|
},
|
2022-08-01 23:09:41 +00:00
|
|
|
$matches[1]
|
2022-06-20 11:04:27 +00:00
|
|
|
);
|
|
|
|
|
2021-04-23 18:54:49 +00:00
|
|
|
$items = $subscriptionStore->getSubscriptionItemsForUser(
|
|
|
|
$user,
|
|
|
|
$itemNames
|
|
|
|
);
|
|
|
|
$itemsByName = [];
|
|
|
|
foreach ( $items as $item ) {
|
|
|
|
$itemsByName[ $item->getItemName() ] = $item;
|
|
|
|
}
|
|
|
|
|
2021-04-19 19:14:07 +00:00
|
|
|
$text = preg_replace_callback(
|
2022-08-01 23:09:41 +00:00
|
|
|
'/<!--__DTSUBSCRIBELINK__(.*?)-->/',
|
2021-05-05 11:44:51 +00:00
|
|
|
static function ( $matches ) use ( $doc, $itemsByName, $lang ) {
|
2022-08-01 23:09:41 +00:00
|
|
|
$itemName = htmlspecialchars_decode( $matches[1] );
|
2021-04-23 18:54:49 +00:00
|
|
|
$isSubscribed = isset( $itemsByName[ $itemName ] ) && !$itemsByName[ $itemName ]->isMuted();
|
2021-08-19 20:35:32 +00:00
|
|
|
$subscribedState = isset( $itemsByName[ $itemName ] ) ? $itemsByName[ $itemName ]->getState() : null;
|
2021-05-05 11:44:51 +00:00
|
|
|
|
2021-04-19 19:14:07 +00:00
|
|
|
$subscribe = $doc->createElement( 'span' );
|
|
|
|
$subscribe->setAttribute(
|
|
|
|
'class',
|
2021-05-05 11:44:51 +00:00
|
|
|
'ext-discussiontools-init-section-subscribe mw-editsection-like'
|
2021-04-19 19:14:07 +00:00
|
|
|
);
|
2021-05-05 11:44:51 +00:00
|
|
|
|
|
|
|
$subscribeLink = $doc->createElement( 'a' );
|
|
|
|
// Set empty 'href' to avoid a:not([href]) selector in MobileFrontend
|
|
|
|
$subscribeLink->setAttribute( 'href', '' );
|
|
|
|
$subscribeLink->setAttribute( 'class', 'ext-discussiontools-init-section-subscribe-link' );
|
|
|
|
$subscribeLink->setAttribute( 'role', 'button' );
|
|
|
|
$subscribeLink->setAttribute( 'tabindex', '0' );
|
|
|
|
$subscribeLink->setAttribute( 'title', wfMessage(
|
|
|
|
$isSubscribed ?
|
|
|
|
'discussiontools-topicsubscription-button-unsubscribe-tooltip' :
|
|
|
|
'discussiontools-topicsubscription-button-subscribe-tooltip'
|
|
|
|
)->inLanguage( $lang )->text() );
|
|
|
|
$subscribeLink->nodeValue = wfMessage(
|
|
|
|
$isSubscribed ?
|
|
|
|
'discussiontools-topicsubscription-button-unsubscribe' :
|
|
|
|
'discussiontools-topicsubscription-button-subscribe'
|
|
|
|
)->inLanguage( $lang )->text();
|
|
|
|
|
2021-08-19 20:35:32 +00:00
|
|
|
if ( $subscribedState !== null ) {
|
|
|
|
$subscribeLink->setAttribute( 'data-mw-subscribed', (string)$subscribedState );
|
2021-05-05 11:44:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$bracket = $doc->createElement( 'span' );
|
|
|
|
$bracket->setAttribute( 'class', 'ext-discussiontools-init-section-subscribe-bracket' );
|
2021-10-26 15:42:50 +00:00
|
|
|
$bracketOpen = $bracket->cloneNode( false );
|
|
|
|
$bracketOpen->nodeValue = '[';
|
|
|
|
$bracketClose = $bracket->cloneNode( false );
|
|
|
|
$bracketClose->nodeValue = ']';
|
2021-05-05 11:44:51 +00:00
|
|
|
|
2021-10-26 15:42:50 +00:00
|
|
|
$subscribe->appendChild( $bracketOpen );
|
2021-05-05 11:44:51 +00:00
|
|
|
$subscribe->appendChild( $subscribeLink );
|
2021-10-26 15:42:50 +00:00
|
|
|
$subscribe->appendChild( $bracketClose );
|
2021-05-05 11:44:51 +00:00
|
|
|
|
2021-04-19 19:14:07 +00:00
|
|
|
return DOMCompat::getOuterHTML( $subscribe );
|
|
|
|
},
|
|
|
|
$text
|
|
|
|
);
|
2021-01-28 17:19:52 +00:00
|
|
|
|
|
|
|
$text = preg_replace_callback(
|
2022-08-01 23:09:41 +00:00
|
|
|
'/<!--__DTSUBSCRIBEBUTTON(DESKTOP|MOBILE)__(.*?)-->/',
|
2022-07-20 13:05:21 +00:00
|
|
|
static function ( $matches ) use ( $doc, $itemsByName, $lang, $isMobile ) {
|
2022-08-01 23:09:41 +00:00
|
|
|
$buttonIsMobile = $matches[1] === 'MOBILE';
|
|
|
|
if ( $buttonIsMobile !== $isMobile ) {
|
|
|
|
return '';
|
2022-07-20 13:05:21 +00:00
|
|
|
}
|
|
|
|
$itemName = htmlspecialchars_decode( $matches[2] );
|
2021-01-28 17:19:52 +00:00
|
|
|
$isSubscribed = isset( $itemsByName[ $itemName ] ) && !$itemsByName[ $itemName ]->isMuted();
|
|
|
|
$subscribedState = isset( $itemsByName[ $itemName ] ) ? $itemsByName[ $itemName ]->getState() : null;
|
|
|
|
|
|
|
|
$subscribe = new \OOUI\ButtonWidget( [
|
|
|
|
'classes' => [ 'ext-discussiontools-init-section-subscribeButton' ],
|
|
|
|
'framed' => false,
|
|
|
|
'icon' => $isSubscribed ? 'bell' : 'bellOutline',
|
|
|
|
'flags' => [ 'progressive' ],
|
|
|
|
'label' => wfMessage( $isSubscribed ?
|
|
|
|
'discussiontools-topicsubscription-button-unsubscribe-label' :
|
|
|
|
'discussiontools-topicsubscription-button-subscribe-label'
|
|
|
|
)->inLanguage( $lang )->text(),
|
|
|
|
'title' => wfMessage( $isSubscribed ?
|
|
|
|
'discussiontools-topicsubscription-button-unsubscribe-tooltip' :
|
|
|
|
'discussiontools-topicsubscription-button-subscribe-tooltip'
|
|
|
|
)->inLanguage( $lang )->text(),
|
|
|
|
'infusable' => true,
|
|
|
|
] );
|
|
|
|
|
|
|
|
if ( $subscribedState !== null ) {
|
|
|
|
$subscribe->setAttributes( [ 'data-mw-subscribed' => (string)$subscribedState ] );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $subscribe->toString();
|
|
|
|
},
|
|
|
|
$text
|
|
|
|
);
|
|
|
|
|
2021-04-19 19:14:07 +00:00
|
|
|
return $text;
|
|
|
|
}
|
|
|
|
|
2021-04-15 21:09:55 +00:00
|
|
|
/**
|
|
|
|
* Replace placeholders for reply links with the real thing.
|
|
|
|
*
|
|
|
|
* @param string $text
|
|
|
|
* @param Language $lang
|
2022-04-21 13:08:59 +00:00
|
|
|
* @param bool $isMobile
|
2021-04-15 21:09:55 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function postprocessReplyTool(
|
2022-07-27 14:19:31 +00:00
|
|
|
string $text, Language $lang, bool $isMobile
|
2021-01-28 17:19:52 +00:00
|
|
|
): string {
|
2022-04-21 13:08:59 +00:00
|
|
|
$doc = DOMCompat::newDocument( true );
|
|
|
|
$replyLinkText = wfMessage( 'discussiontools-replylink' )->inLanguage( $lang )->escaped();
|
|
|
|
$replyButtonText = wfMessage( 'discussiontools-replybutton' )->inLanguage( $lang )->escaped();
|
|
|
|
|
|
|
|
$text = preg_replace_callback(
|
2022-08-01 23:09:41 +00:00
|
|
|
'/<!--__DTREPLYBUTTONSCONTENT__-->/',
|
2022-04-21 13:08:59 +00:00
|
|
|
static function ( $matches ) use ( $doc, $replyLinkText, $replyButtonText, $isMobile ) {
|
|
|
|
$replyLinkButtons = $doc->createElement( 'span' );
|
|
|
|
|
|
|
|
// Reply
|
|
|
|
$replyLink = $doc->createElement( 'a' );
|
|
|
|
$replyLink->setAttribute( 'class', 'ext-discussiontools-init-replylink-reply' );
|
|
|
|
$replyLink->setAttribute( 'role', 'button' );
|
|
|
|
$replyLink->setAttribute( 'tabindex', '0' );
|
|
|
|
// Set empty 'href' to avoid a:not([href]) selector in MobileFrontend
|
|
|
|
$replyLink->setAttribute( 'href', '' );
|
|
|
|
$replyLink->textContent = $replyLinkText;
|
|
|
|
|
|
|
|
$bracket = $doc->createElement( 'span' );
|
|
|
|
$bracket->setAttribute( 'class', 'ext-discussiontools-init-replylink-bracket' );
|
|
|
|
$bracketOpen = $bracket->cloneNode( false );
|
|
|
|
$bracketClose = $bracket->cloneNode( false );
|
|
|
|
$bracketOpen->textContent = '[';
|
|
|
|
$bracketClose->textContent = ']';
|
|
|
|
|
|
|
|
// Visual enhancements button
|
|
|
|
$replyLinkButton = new \OOUI\ButtonWidget( [
|
|
|
|
'classes' => [ 'ext-discussiontools-init-replybutton' ],
|
|
|
|
'framed' => false,
|
|
|
|
'label' => $replyButtonText,
|
|
|
|
'icon' => $isMobile ? 'share' : null,
|
|
|
|
'flags' => [ 'progressive' ],
|
|
|
|
'infusable' => true,
|
|
|
|
] );
|
|
|
|
|
2022-08-01 21:08:05 +00:00
|
|
|
DOMCompat::setInnerHTML( $replyLinkButtons, $replyLinkButton->toString() );
|
|
|
|
$replyLinkButtons->appendChild( $bracketOpen );
|
|
|
|
$replyLinkButtons->appendChild( $replyLink );
|
|
|
|
$replyLinkButtons->appendChild( $bracketClose );
|
2022-04-21 13:08:59 +00:00
|
|
|
|
2022-08-01 23:09:41 +00:00
|
|
|
return DOMCompat::getInnerHTML( $replyLinkButtons );
|
2022-04-21 13:08:59 +00:00
|
|
|
},
|
|
|
|
$text
|
|
|
|
);
|
2021-10-26 15:42:50 +00:00
|
|
|
|
2021-04-15 21:09:55 +00:00
|
|
|
return $text;
|
|
|
|
}
|
|
|
|
|
2021-01-28 17:19:52 +00:00
|
|
|
/**
|
|
|
|
* Create a meta item label
|
|
|
|
*
|
|
|
|
* @param string $className
|
2022-06-23 16:18:00 +00:00
|
|
|
* @param string|\OOUI\HtmlSnippet $label Label
|
2021-01-28 17:19:52 +00:00
|
|
|
* @return \OOUI\Tag
|
|
|
|
*/
|
2022-06-23 16:18:00 +00:00
|
|
|
private static function metaLabel( string $className, $label ): \OOUI\Tag {
|
2021-01-28 17:19:52 +00:00
|
|
|
return ( new \OOUI\Tag( 'span' ) )
|
|
|
|
->addClasses( [ 'ext-discussiontools-init-section-metaitem', $className ] )
|
|
|
|
->appendContent( $label );
|
|
|
|
}
|
|
|
|
|
2022-06-23 16:18:00 +00:00
|
|
|
/**
|
|
|
|
* Get JSON for a commentItem that can be inserted into a comment marker
|
|
|
|
*
|
2022-03-18 03:28:06 +00:00
|
|
|
* @param ContentCommentItem $commentItem Comment item
|
2022-04-21 14:29:56 +00:00
|
|
|
* @param bool $includeTopicAndAuthor Include metadata about topic and author
|
2022-06-23 16:18:00 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2022-04-21 14:29:56 +00:00
|
|
|
private static function getJsonForCommentMarker(
|
|
|
|
ContentCommentItem $commentItem,
|
|
|
|
bool $includeTopicAndAuthor = false
|
|
|
|
): string {
|
2022-06-23 16:18:00 +00:00
|
|
|
$JSON = [
|
|
|
|
'id' => $commentItem->getId(),
|
|
|
|
'timestamp' => $commentItem->getTimestampString()
|
|
|
|
];
|
2022-04-21 14:29:56 +00:00
|
|
|
if ( $includeTopicAndAuthor ) {
|
|
|
|
$JSON['author'] = $commentItem->getAuthor();
|
|
|
|
$heading = $commentItem->getSubscribableHeading();
|
|
|
|
if ( $heading ) {
|
|
|
|
$JSON['heading'] = $heading->jsonSerialize();
|
|
|
|
$JSON['heading']['text'] = $heading->getText();
|
|
|
|
$JSON['heading']['linkableTitle'] = $heading->getLinkableTitle();
|
|
|
|
}
|
|
|
|
}
|
2022-06-23 16:18:00 +00:00
|
|
|
return json_encode( $JSON );
|
|
|
|
}
|
|
|
|
|
2021-01-28 17:19:52 +00:00
|
|
|
/**
|
|
|
|
* Get a relative timestamp from a signature timestamp.
|
|
|
|
*
|
|
|
|
* Signature timestamps don't have seconds-level accuracy, so any
|
|
|
|
* time difference of less than 120 seconds is treated as being
|
|
|
|
* posted "just now".
|
|
|
|
*
|
|
|
|
* @param MWTimestamp $timestamp
|
|
|
|
* @param Language $lang
|
|
|
|
* @param UserIdentity $user
|
|
|
|
* @return string
|
|
|
|
*/
|
2022-06-28 17:34:17 +00:00
|
|
|
public static function getSignatureRelativeTime(
|
|
|
|
MWTimestamp $timestamp, Language $lang, UserIdentity $user
|
|
|
|
): string {
|
2021-01-28 17:19:52 +00:00
|
|
|
if ( time() - intval( $timestamp->getTimestamp() ) < 120 ) {
|
|
|
|
$timestamp = new MWTimestamp();
|
|
|
|
}
|
|
|
|
return $lang->getHumanTimestamp( $timestamp, null, $user );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-04-21 14:29:56 +00:00
|
|
|
* Post-process visual enhancements features (topic containers)
|
2021-01-28 17:19:52 +00:00
|
|
|
*
|
|
|
|
* @param string $text
|
|
|
|
* @param Language $lang
|
|
|
|
* @param UserIdentity $user
|
2022-07-27 15:12:07 +00:00
|
|
|
* @param bool $isMobile
|
2021-01-28 17:19:52 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function postprocessVisualEnhancements(
|
2022-07-27 15:12:07 +00:00
|
|
|
string $text, Language $lang, UserIdentity $user, bool $isMobile
|
2021-01-28 17:19:52 +00:00
|
|
|
): string {
|
|
|
|
$text = preg_replace_callback(
|
2022-06-23 16:18:00 +00:00
|
|
|
'/<!--__DTLATESTCOMMENTTHREAD__(.*?)__-->/',
|
2021-01-28 17:19:52 +00:00
|
|
|
static function ( $matches ) use ( $lang, $user ) {
|
2022-06-23 16:18:00 +00:00
|
|
|
$itemData = json_decode( htmlspecialchars_decode( $matches[1] ), true );
|
|
|
|
if ( $itemData && $itemData['timestamp'] && $itemData['id'] ) {
|
|
|
|
$relativeTime = static::getSignatureRelativeTime(
|
|
|
|
new MWTimestamp( $itemData['timestamp'] ),
|
|
|
|
$lang,
|
|
|
|
$user
|
|
|
|
);
|
|
|
|
$commentLink = Html::element( 'a', [
|
2022-08-04 13:16:07 +00:00
|
|
|
'href' => '#' . Sanitizer::escapeIdForLink( $itemData['id'] )
|
2022-06-23 16:18:00 +00:00
|
|
|
], $relativeTime );
|
|
|
|
|
|
|
|
$label = wfMessage( 'discussiontools-topicheader-latestcomment' )
|
|
|
|
->rawParams( $commentLink )
|
|
|
|
->inLanguage( $lang )->escaped();
|
|
|
|
|
|
|
|
return CommentFormatter::metaLabel(
|
|
|
|
'ext-discussiontools-init-section-timestampLabel',
|
|
|
|
new \OOUI\HtmlSnippet( $label )
|
|
|
|
);
|
|
|
|
}
|
2021-01-28 17:19:52 +00:00
|
|
|
},
|
|
|
|
$text
|
|
|
|
);
|
|
|
|
$text = preg_replace_callback(
|
|
|
|
'/<!--__DTCOMMENTCOUNT__([0-9]+)__-->/',
|
|
|
|
static function ( $matches ) use ( $lang, $user ) {
|
|
|
|
$count = $lang->formatNum( $matches[1] );
|
|
|
|
$label = wfMessage(
|
|
|
|
'discussiontools-topicheader-commentcount',
|
|
|
|
$count
|
|
|
|
)->inLanguage( $lang )->text();
|
|
|
|
return CommentFormatter::metaLabel(
|
|
|
|
'ext-discussiontools-init-section-commentCountLabel',
|
|
|
|
$label
|
|
|
|
);
|
|
|
|
},
|
|
|
|
$text
|
|
|
|
);
|
|
|
|
$text = preg_replace_callback(
|
|
|
|
'/<!--__DTAUTHORCOUNT__([0-9]+)__-->/',
|
|
|
|
static function ( $matches ) use ( $lang, $user ) {
|
|
|
|
$count = $lang->formatNum( $matches[1] );
|
|
|
|
$label = wfMessage(
|
|
|
|
'discussiontools-topicheader-authorcount',
|
|
|
|
$count
|
|
|
|
)->inLanguage( $lang )->text();
|
|
|
|
return CommentFormatter::metaLabel(
|
|
|
|
'ext-discussiontools-init-section-authorCountLabel',
|
|
|
|
$label
|
|
|
|
);
|
|
|
|
},
|
|
|
|
$text
|
|
|
|
);
|
2022-07-27 15:12:07 +00:00
|
|
|
if ( $isMobile ) {
|
|
|
|
$text = preg_replace_callback(
|
|
|
|
'/<!--__DTELLIPSISBUTTON__-->/',
|
|
|
|
static function ( $matches ) {
|
|
|
|
$ellipsis = new ButtonMenuSelectWidget( [
|
|
|
|
'classes' => [ 'ext-discussiontools-init-section-ellipsisButton' ],
|
|
|
|
'framed' => false,
|
|
|
|
'icon' => 'ellipsis',
|
|
|
|
'infusable' => true,
|
|
|
|
] );
|
|
|
|
|
|
|
|
return $ellipsis->toString();
|
|
|
|
},
|
|
|
|
$text
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$text = preg_replace(
|
|
|
|
'/<!--__DTELLIPSISBUTTON__-->/',
|
|
|
|
'',
|
|
|
|
$text
|
|
|
|
);
|
|
|
|
}
|
2021-01-28 17:19:52 +00:00
|
|
|
return $text;
|
|
|
|
}
|
|
|
|
|
2022-04-21 14:29:56 +00:00
|
|
|
/**
|
|
|
|
* Post-process visual enhancements features for page subtitle
|
|
|
|
*
|
|
|
|
* @param string $text
|
|
|
|
* @param Language $lang
|
|
|
|
* @param UserIdentity $user
|
|
|
|
* @return ?string
|
|
|
|
*/
|
|
|
|
public static function postprocessVisualEnhancementsSubtitle(
|
|
|
|
string $text, Language $lang, UserIdentity $user
|
|
|
|
): ?string {
|
|
|
|
preg_match( '/<!--__DTLATESTCOMMENTPAGE__(.*?)__-->/', $text, $matches );
|
|
|
|
if ( count( $matches ) ) {
|
|
|
|
$itemData = json_decode( htmlspecialchars_decode( $matches[1] ), true );
|
|
|
|
|
|
|
|
if ( $itemData && $itemData['timestamp'] && $itemData['id'] ) {
|
|
|
|
$relativeTime = static::getSignatureRelativeTime(
|
|
|
|
new MWTimestamp( $itemData['timestamp'] ),
|
|
|
|
$lang,
|
|
|
|
$user
|
|
|
|
);
|
|
|
|
$commentLink = Html::element( 'a', [
|
|
|
|
'href' => '#' . Sanitizer::escapeIdForLink( $itemData['id'] )
|
|
|
|
], $relativeTime );
|
|
|
|
|
|
|
|
if ( isset( $itemData['heading'] ) ) {
|
|
|
|
$headingLink = Html::element( 'a', [
|
|
|
|
'href' => '#' . Sanitizer::escapeIdForLink( $itemData['heading']['linkableTitle'] )
|
|
|
|
], $itemData['heading']['text'] );
|
|
|
|
$label = wfMessage( 'discussiontools-pageframe-latestcomment' )
|
|
|
|
->rawParams( $commentLink )
|
|
|
|
->params( $itemData['author'] )
|
|
|
|
->rawParams( $headingLink )
|
|
|
|
->inLanguage( $lang )->escaped();
|
|
|
|
} else {
|
|
|
|
$label = wfMessage( 'discussiontools-pageframe-latestcomment-notopic' )
|
|
|
|
->rawParams( $commentLink )
|
|
|
|
->params( $itemData['author'] )
|
|
|
|
->inLanguage( $lang )->escaped();
|
|
|
|
}
|
|
|
|
|
|
|
|
return Html::rawElement(
|
|
|
|
'div',
|
|
|
|
[ 'class' => 'ext-discussiontools-init-pageframe-latestcomment' ],
|
|
|
|
$label
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-05-21 05:09:25 +00:00
|
|
|
/**
|
|
|
|
* Check if the talk page had no comments or headings.
|
|
|
|
*
|
|
|
|
* @param string $text
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function isEmptyTalkPage( string $text ): bool {
|
|
|
|
return strpos( $text, '<!--__DTEMPTYTALKPAGE__-->' ) !== false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Append content to an empty talk page
|
|
|
|
*
|
|
|
|
* @param string $text
|
|
|
|
* @param string $content
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function appendToEmptyTalkPage( string $text, string $content ): string {
|
|
|
|
return str_replace( '<!--__DTEMPTYTALKPAGE__-->', $content, $text );
|
|
|
|
}
|
|
|
|
|
2022-09-27 14:48:25 +00:00
|
|
|
/**
|
|
|
|
* Check if the talk page has content above the first heading, in the lede section.
|
|
|
|
*
|
|
|
|
* @param string $text
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function hasLedeContent( string $text ): bool {
|
|
|
|
return strpos( $text, '<!--__DTHASLEDECONTENT__-->' ) !== false;
|
|
|
|
}
|
|
|
|
|
2020-09-16 12:07:27 +00:00
|
|
|
}
|