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;
|
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;
|
2022-10-28 18:24:02 +00:00
|
|
|
use Wikimedia\Timestamp\TimestampException;
|
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
|
2023-07-20 14:27:06 +00:00
|
|
|
* @param Title $title
|
2021-01-29 18:31:27 +00:00
|
|
|
*/
|
2023-07-20 14:27:06 +00:00
|
|
|
public static function addDiscussionTools( string &$text, ParserOutput $pout, Title $title ): void {
|
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 {
|
2023-01-27 12:29:17 +00:00
|
|
|
$text = static::addDiscussionToolsInternal( $text, $pout, $title );
|
2022-04-07 00:00:05 +00:00
|
|
|
|
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 12:48:50 +00:00
|
|
|
* @return Element Wrapper element (either found or newly added)
|
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 {
|
2021-01-28 17:19:52 +00:00
|
|
|
$doc = $headingElement->ownerDocument;
|
2022-11-15 12:48:50 +00:00
|
|
|
$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 );
|
2022-10-18 00:27:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-12-08 16:15:02 +00:00
|
|
|
$headingJSONEscaped = htmlspecialchars(
|
|
|
|
json_encode( static::getJsonForHeadingMarker( $headingItem ) )
|
|
|
|
);
|
2021-01-28 17:19:52 +00:00
|
|
|
|
|
|
|
// Replaced in ::postprocessTopicSubscription() as the text depends on user state
|
2022-07-22 21:46:41 +00:00
|
|
|
if ( $headingItem->isSubscribable() ) {
|
2022-12-08 16:15:02 +00:00
|
|
|
$subscribeLink = $doc->createComment( '__DTSUBSCRIBELINK__' . $headingJSONEscaped );
|
2022-07-20 12:58:35 +00:00
|
|
|
$headingElement->insertBefore( $subscribeLink, $headingElement->firstChild );
|
2021-01-28 17:19:52 +00:00
|
|
|
|
2022-12-08 16:15:02 +00:00
|
|
|
$subscribeButton = $doc->createComment( '__DTSUBSCRIBEBUTTONDESKTOP__' . $headingJSONEscaped );
|
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 ) {
|
2023-02-07 00:04:15 +00:00
|
|
|
$latestReplyJSON = json_encode( static::getJsonArrayForCommentMarker( $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-12-08 16:15:02 +00:00
|
|
|
$subscribeButton = $doc->createComment( '__DTSUBSCRIBEBUTTONMOBILE__' . $headingJSONEscaped );
|
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
|
2023-01-27 12:29:17 +00:00
|
|
|
* @return string HTML with discussion tools
|
2020-11-16 15:50:46 +00:00
|
|
|
*/
|
2023-01-27 12:29:17 +00:00
|
|
|
protected static function addDiscussionToolsInternal( string $html, ParserOutput $pout, Title $title ): string {
|
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;
|
2023-02-07 00:04:15 +00:00
|
|
|
$newestCommentData = null;
|
2022-04-21 14:29:56 +00:00
|
|
|
|
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
|
2023-02-07 00:04:15 +00:00
|
|
|
$newestCommentData = static::getJsonArrayForCommentMarker( $threadItem, true );
|
2022-04-21 14:29:56 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2023-01-27 12:29:17 +00:00
|
|
|
$pout->setExtensionData( 'DiscussionTools-tocInfo', $tocInfo );
|
|
|
|
|
2023-02-07 00:42:00 +00:00
|
|
|
if ( $newestCommentData ) {
|
2023-02-07 00:04:15 +00:00
|
|
|
$pout->setExtensionData( 'DiscussionTools-newestComment', $newestCommentData );
|
2022-04-21 14:29:56 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 21:01:17 +00:00
|
|
|
$startOfSections = DOMCompat::querySelector( $container, 'meta[property="mw:PageProp/toc"]' );
|
2022-09-27 14:48:25 +00:00
|
|
|
|
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-11-16 21:01:17 +00:00
|
|
|
if ( !$startOfSections ) {
|
|
|
|
$startOfSections = $headingElement;
|
2022-09-27 14:48:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
// Page has no headings but some content
|
2022-11-16 21:01:17 +00:00
|
|
|
( !$startOfSections && $container->childNodes->length ) ||
|
|
|
|
// Page has content before the first heading / TOC
|
|
|
|
( $startOfSections && $startOfSections->previousSibling !== null )
|
2022-09-27 14:48:25 +00:00
|
|
|
) {
|
2023-02-07 00:04:15 +00:00
|
|
|
$pout->setExtensionData( 'DiscussionTools-hasLedeContent', true );
|
2021-01-28 17:19:52 +00:00
|
|
|
}
|
2022-11-30 19:01:42 +00:00
|
|
|
if (
|
|
|
|
// Placeholder heading indicates that there are comments in the lede section (T324139).
|
|
|
|
// We can't really separate them from the lede content.
|
|
|
|
isset( $threadItems[0] ) &&
|
|
|
|
$threadItems[0] instanceof ContentHeadingItem &&
|
|
|
|
$threadItems[0]->isPlaceholderHeading()
|
|
|
|
) {
|
2023-02-07 00:04:15 +00:00
|
|
|
$pout->setExtensionData( 'DiscussionTools-hasCommentsInLedeContent', true );
|
2022-12-07 21:42:41 +00:00
|
|
|
MediaWikiServices::getInstance()->getTrackingCategories()
|
|
|
|
->addTrackingCategory( $pout, 'discussiontools-comments-before-first-heading-category', $title );
|
2022-11-30 19:01:42 +00:00
|
|
|
}
|
2021-01-28 17:19:52 +00:00
|
|
|
|
2022-05-21 05:09:25 +00:00
|
|
|
if ( count( $threadItems ) === 0 ) {
|
2023-02-07 00:04:15 +00:00
|
|
|
$pout->setExtensionData( 'DiscussionTools-isEmptyTalkPage', true );
|
2022-05-21 05:09:25 +00:00
|
|
|
}
|
|
|
|
|
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'];
|
|
|
|
|
2023-01-27 12:29:17 +00:00
|
|
|
return $html;
|
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__-->' => '',
|
|
|
|
] );
|
|
|
|
|
|
|
|
$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
|
2022-12-08 16:15:02 +00:00
|
|
|
* @param Title $title
|
2021-04-19 19:14:07 +00:00
|
|
|
* @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-12-08 16:15:02 +00:00
|
|
|
string $text, Language $lang, Title $title,
|
|
|
|
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-12-08 16:15:02 +00:00
|
|
|
$itemDataByName = [];
|
2022-08-01 23:09:41 +00:00
|
|
|
preg_match_all( '/<!--__DTSUBSCRIBELINK__(.*?)-->/', $text, $matches );
|
2022-12-08 16:15:02 +00:00
|
|
|
foreach ( $matches[1] as $itemData ) {
|
2023-02-09 17:54:40 +00:00
|
|
|
$itemDataByName[ $itemData ] = json_decode( htmlspecialchars_decode( $itemData ), true );
|
2022-12-08 16:15:02 +00:00
|
|
|
}
|
|
|
|
$itemNames = array_column( $itemDataByName, 'name' );
|
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-12-08 16:15:02 +00:00
|
|
|
'/<!--__(DTSUBSCRIBELINK|DTSUBSCRIBEBUTTON(?:DESKTOP|MOBILE))__(.*?)-->/',
|
|
|
|
static function ( $matches ) use ( $doc, $itemsByName, $itemDataByName, $lang, $title, $isMobile ) {
|
|
|
|
$isLink = $matches[1] === 'DTSUBSCRIBELINK';
|
|
|
|
$buttonIsMobile = $matches[1] === 'DTSUBSCRIBEBUTTONMOBILE';
|
|
|
|
|
|
|
|
$itemData = $itemDataByName[ $matches[2] ];
|
|
|
|
'@phan-var array $itemData';
|
|
|
|
$itemName = $itemData['name'];
|
|
|
|
|
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
|
|
|
|
2023-02-09 17:54:40 +00:00
|
|
|
$href = $title->getLinkURL( [
|
|
|
|
'action' => $isSubscribed ? 'dtunsubscribe' : 'dtsubscribe',
|
|
|
|
'commentname' => $itemName,
|
|
|
|
'section' => $itemData['linkableTitle'],
|
|
|
|
] );
|
2021-05-05 11:44:51 +00:00
|
|
|
|
2022-12-08 16:15:02 +00:00
|
|
|
if ( $isLink ) {
|
|
|
|
$subscribe = $doc->createElement( 'span' );
|
|
|
|
$subscribe->setAttribute(
|
|
|
|
'class',
|
|
|
|
'ext-discussiontools-init-section-subscribe mw-editsection-like'
|
|
|
|
);
|
|
|
|
|
|
|
|
$subscribeLink = $doc->createElement( 'a' );
|
|
|
|
$subscribeLink->setAttribute( 'href', $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();
|
|
|
|
|
|
|
|
if ( $subscribedState !== null ) {
|
|
|
|
$subscribeLink->setAttribute( 'data-mw-subscribed', (string)$subscribedState );
|
|
|
|
}
|
2021-01-28 17:19:52 +00:00
|
|
|
|
2022-12-08 16:15:02 +00:00
|
|
|
$bracket = $doc->createElement( 'span' );
|
|
|
|
$bracket->setAttribute( 'class', 'ext-discussiontools-init-section-subscribe-bracket' );
|
|
|
|
$bracketOpen = $bracket->cloneNode( false );
|
|
|
|
$bracketOpen->nodeValue = '[';
|
|
|
|
$bracketClose = $bracket->cloneNode( false );
|
|
|
|
$bracketClose->nodeValue = ']';
|
|
|
|
|
|
|
|
$subscribe->appendChild( $bracketOpen );
|
|
|
|
$subscribe->appendChild( $subscribeLink );
|
|
|
|
$subscribe->appendChild( $bracketClose );
|
|
|
|
|
|
|
|
return DOMCompat::getOuterHTML( $subscribe );
|
|
|
|
} else {
|
|
|
|
if ( $buttonIsMobile !== $isMobile ) {
|
|
|
|
return '';
|
|
|
|
}
|
2021-01-28 17:19:52 +00:00
|
|
|
|
2022-12-08 16:15:02 +00:00
|
|
|
$subscribe = new \OOUI\ButtonWidget( [
|
|
|
|
'classes' => [ 'ext-discussiontools-init-section-subscribeButton' ],
|
|
|
|
'framed' => false,
|
|
|
|
'icon' => $isSubscribed ? 'bell' : 'bellOutline',
|
|
|
|
'flags' => [ 'progressive' ],
|
|
|
|
'href' => $href,
|
|
|
|
'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 ] );
|
|
|
|
}
|
2021-01-28 17:19:52 +00:00
|
|
|
|
2022-12-08 16:15:02 +00:00
|
|
|
return $subscribe->toString();
|
2021-01-28 17:19:52 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
$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-12-13 05:49:51 +00:00
|
|
|
static function ( $matches ) use ( $doc, $replyLinkText, $replyButtonText, $isMobile, $lang ) {
|
2022-04-21 13:08:59 +00:00
|
|
|
$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
|
2022-12-13 05:49:51 +00:00
|
|
|
$useIcon = $isMobile || static::isLanguageRequiringReplyIcon( $lang );
|
2022-04-21 13:08:59 +00:00
|
|
|
$replyLinkButton = new \OOUI\ButtonWidget( [
|
|
|
|
'classes' => [ 'ext-discussiontools-init-replybutton' ],
|
|
|
|
'framed' => false,
|
|
|
|
'label' => $replyButtonText,
|
2022-12-13 05:49:51 +00:00
|
|
|
'icon' => $useIcon ? 'share' : null,
|
2022-04-21 13:08:59 +00:00
|
|
|
'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
|
|
|
/**
|
2023-02-07 00:04:15 +00:00
|
|
|
* Get JSON data for a commentItem that can be inserted into a comment marker
|
2022-06-23 16:18:00 +00:00
|
|
|
*
|
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
|
2023-02-07 00:04:15 +00:00
|
|
|
* @return array
|
2022-06-23 16:18:00 +00:00
|
|
|
*/
|
2023-02-07 00:04:15 +00:00
|
|
|
private static function getJsonArrayForCommentMarker(
|
2022-04-21 14:29:56 +00:00
|
|
|
ContentCommentItem $commentItem,
|
|
|
|
bool $includeTopicAndAuthor = false
|
2023-02-07 00:04:15 +00:00
|
|
|
): array {
|
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 ) {
|
2022-12-08 16:15:02 +00:00
|
|
|
$JSON['heading'] = static::getJsonForHeadingMarker( $heading );
|
2022-04-21 14:29:56 +00:00
|
|
|
}
|
|
|
|
}
|
2023-02-07 00:04:15 +00:00
|
|
|
return $JSON;
|
2022-06-23 16:18:00 +00:00
|
|
|
}
|
|
|
|
|
2022-12-08 16:15:02 +00:00
|
|
|
/**
|
|
|
|
* @param ContentHeadingItem $heading
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private static function getJsonForHeadingMarker( ContentHeadingItem $heading ): array {
|
|
|
|
$JSON = $heading->jsonSerialize();
|
|
|
|
$JSON['text'] = $heading->getText();
|
|
|
|
$JSON['linkableTitle'] = $heading->getLinkableTitle();
|
|
|
|
return $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 {
|
2022-10-28 18:24:02 +00:00
|
|
|
try {
|
|
|
|
$diff = time() - intval( $timestamp->getTimestamp() );
|
|
|
|
} catch ( TimestampException $ex ) {
|
|
|
|
// Can't happen
|
|
|
|
$diff = 0;
|
|
|
|
}
|
|
|
|
if ( $diff < 120 ) {
|
2021-01-28 17:19:52 +00:00
|
|
|
$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
|
|
|
|
*
|
2023-02-07 00:31:33 +00:00
|
|
|
* @param ParserOutput $pout
|
2022-04-21 14:29:56 +00:00
|
|
|
* @param Language $lang
|
|
|
|
* @param UserIdentity $user
|
|
|
|
* @return ?string
|
|
|
|
*/
|
|
|
|
public static function postprocessVisualEnhancementsSubtitle(
|
2023-02-07 00:31:33 +00:00
|
|
|
ParserOutput $pout, Language $lang, UserIdentity $user
|
2022-04-21 14:29:56 +00:00
|
|
|
): ?string {
|
2023-02-07 00:31:33 +00:00
|
|
|
$itemData = $pout->getExtensionData( 'DiscussionTools-newestComment' );
|
|
|
|
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();
|
2023-02-12 02:57:31 +00:00
|
|
|
}
|
2023-02-07 00:31:33 +00:00
|
|
|
|
|
|
|
return Html::rawElement(
|
|
|
|
'div',
|
|
|
|
[ 'class' => 'ext-discussiontools-init-pageframe-latestcomment' ],
|
|
|
|
$label
|
|
|
|
);
|
2022-04-21 14:29:56 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-01-27 12:29:17 +00:00
|
|
|
/**
|
|
|
|
* Post-process visual enhancements features for table of contents
|
|
|
|
*
|
|
|
|
* @param ParserOutput $pout
|
|
|
|
* @param Language $lang
|
|
|
|
*/
|
|
|
|
public static function postprocessTableOfContents(
|
|
|
|
ParserOutput $pout, Language $lang
|
|
|
|
): void {
|
|
|
|
$tocInfo = $pout->getExtensionData( 'DiscussionTools-tocInfo' );
|
|
|
|
|
|
|
|
if ( $tocInfo && $pout->getTOCData() ) {
|
|
|
|
$sections = $pout->getTOCData()->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'
|
|
|
|
], $commentCount );
|
|
|
|
} else {
|
|
|
|
$summary = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
// This also shows up in API action=parse&prop=sections output.
|
|
|
|
$item->setExtensionData( 'DiscussionTools-html-summary', $summary );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-21 05:09:25 +00:00
|
|
|
/**
|
|
|
|
* Check if the talk page had no comments or headings.
|
|
|
|
*
|
2023-02-07 00:31:33 +00:00
|
|
|
* @param ParserOutput $pout
|
2022-05-21 05:09:25 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2023-02-07 00:31:33 +00:00
|
|
|
public static function isEmptyTalkPage( ParserOutput $pout ): bool {
|
|
|
|
return $pout->getExtensionData( 'DiscussionTools-isEmptyTalkPage' ) === true;
|
2022-05-21 05:09:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Append content to an empty talk page
|
|
|
|
*
|
2023-02-07 00:31:33 +00:00
|
|
|
* @param ParserOutput $pout
|
2022-05-21 05:09:25 +00:00
|
|
|
* @param string $content
|
|
|
|
*/
|
2023-02-07 00:31:33 +00:00
|
|
|
public static function appendToEmptyTalkPage( ParserOutput $pout, string $content ): void {
|
|
|
|
$text = $pout->getRawText();
|
|
|
|
$text .= $content;
|
|
|
|
$pout->setText( $text );
|
2022-05-21 05:09:25 +00:00
|
|
|
}
|
|
|
|
|
2022-09-27 14:48:25 +00:00
|
|
|
/**
|
|
|
|
* Check if the talk page has content above the first heading, in the lede section.
|
|
|
|
*
|
2023-02-07 00:31:33 +00:00
|
|
|
* @param ParserOutput $pout
|
2022-09-27 14:48:25 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2023-02-07 00:31:33 +00:00
|
|
|
public static function hasLedeContent( ParserOutput $pout ): bool {
|
|
|
|
return $pout->getExtensionData( 'DiscussionTools-hasLedeContent' ) === true;
|
2022-09-27 14:48:25 +00:00
|
|
|
}
|
|
|
|
|
2022-11-30 19:01:42 +00:00
|
|
|
/**
|
|
|
|
* Check if the talk page has comments above the first heading, in the lede section.
|
|
|
|
*
|
2023-02-07 00:31:33 +00:00
|
|
|
* @param ParserOutput $pout
|
2022-11-30 19:01:42 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2023-02-07 00:31:33 +00:00
|
|
|
public static function hasCommentsInLedeContent( ParserOutput $pout ): bool {
|
|
|
|
return $pout->getExtensionData( 'DiscussionTools-hasCommentsInLedeContent' ) === true;
|
2022-11-30 19:01:42 +00:00
|
|
|
}
|
|
|
|
|
2022-12-13 05:49:51 +00:00
|
|
|
public static function isLanguageRequiringReplyIcon( Language $lang ): bool {
|
|
|
|
$dtConfig = MediaWikiServices::getInstance()->getConfigFactory()->makeConfig( 'discussiontools' );
|
|
|
|
$languages = $dtConfig->get( 'DiscussionTools_visualenhancements_reply_icon_languages' );
|
2023-06-06 12:08:00 +00:00
|
|
|
return in_array( $lang->getCode(), $languages, true );
|
2022-12-13 05:49:51 +00:00
|
|
|
}
|
|
|
|
|
2020-09-16 12:07:27 +00:00
|
|
|
}
|