2020-09-16 12:07:27 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace MediaWiki\Extension\DiscussionTools;
|
|
|
|
|
2021-01-06 17:06:27 +00:00
|
|
|
use Language;
|
2023-12-11 15:38:02 +00:00
|
|
|
use MediaWiki\Config\ConfigException;
|
2024-06-08 22:02:35 +00:00
|
|
|
use MediaWiki\Context\IContextSource;
|
2023-09-28 11:00:36 +00:00
|
|
|
use MediaWiki\Extension\DiscussionTools\Hooks\HookRunner;
|
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;
|
2023-09-29 15:32:45 +00:00
|
|
|
use MediaWiki\Extension\DiscussionTools\ThreadItem\ThreadItem;
|
2023-12-11 15:38:02 +00:00
|
|
|
use MediaWiki\Html\Html;
|
2021-01-29 18:31:27 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2024-06-08 22:02:35 +00:00
|
|
|
use MediaWiki\Parser\ParserOutput;
|
2023-12-11 15:38:02 +00:00
|
|
|
use MediaWiki\Parser\Sanitizer;
|
|
|
|
use MediaWiki\Request\WebRequest;
|
2023-08-19 04:14:36 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2021-04-19 19:14:07 +00:00
|
|
|
use MediaWiki\User\UserIdentity;
|
2023-12-11 15:38:02 +00:00
|
|
|
use MediaWiki\Utils\MWTimestamp;
|
2021-01-29 18:31:27 +00:00
|
|
|
use MWExceptionHandler;
|
|
|
|
use Throwable;
|
2023-09-29 15:32:45 +00:00
|
|
|
use Wikimedia\Parsoid\DOM\Document;
|
2022-02-19 03:25:14 +00:00
|
|
|
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.
|
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
|
2023-09-28 11:00:36 +00:00
|
|
|
protected static function getHookRunner(): HookRunner {
|
|
|
|
return new HookRunner( MediaWikiServices::getInstance()->getHookContainer() );
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2024-05-05 04:16:02 +00:00
|
|
|
MediaWikiServices::getInstance()->getStatsFactory()
|
|
|
|
->getTiming( 'discussiontools_addreplylinks_seconds' )
|
|
|
|
->copyToStatsdAt( 'discussiontools.addReplyLinks' )
|
|
|
|
->observe( $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(
|
2023-11-02 16:29:54 +00:00
|
|
|
// The following messages can be generated upstream
|
|
|
|
// * discussiontools-limitreport-timeusage-value
|
|
|
|
// * discussiontools-limitreport-timeusage-value-text
|
|
|
|
// * discussiontools-limitreport-timeusage-value-html
|
2021-10-07 15:58:17 +00:00
|
|
|
'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
|
|
|
/**
|
2023-12-24 07:33:27 +00:00
|
|
|
* Add a wrapper, topic container, and subscribe link around a heading element
|
2021-01-28 17:19:52 +00:00
|
|
|
*
|
|
|
|
* @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
|
|
|
*/
|
2023-12-24 07:33:27 +00:00
|
|
|
protected static function handleHeading(
|
2022-04-07 00:00:05 +00:00
|
|
|
Element $headingElement,
|
|
|
|
?ContentHeadingItem $headingItem = null,
|
2023-09-28 11:00:36 +00:00
|
|
|
?array &$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' )
|
|
|
|
) ) {
|
2023-12-15 22:01:40 +00:00
|
|
|
// Do not add the wrapper if the heading has attributes generated from wikitext (T353489).
|
|
|
|
// Only allow reserved attributes (e.g. 'data-mw', which can't be used in wikitext, but which
|
|
|
|
// are used internally by our own code and by Parsoid) and the 'id' attribute used by Parsoid.
|
|
|
|
foreach ( $headingElement->attributes as $attr ) {
|
|
|
|
if ( $attr->name !== 'id' && !Sanitizer::isReservedDataAttribute( $attr->name ) ) {
|
|
|
|
return $headingElement;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-15 12:48:50 +00:00
|
|
|
$wrapperNode = $doc->createElement( 'div' );
|
|
|
|
$headingElement->parentNode->insertBefore( $wrapperNode, $headingElement );
|
|
|
|
$wrapperNode->appendChild( $headingElement );
|
2022-10-18 00:27:35 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-10-11 15:53:55 +00:00
|
|
|
$uneditable = DOMCompat::querySelector( $wrapperNode, 'mw\\:editsection' ) === null;
|
|
|
|
$headingItem->setUneditableSection( $uneditable );
|
|
|
|
self::addOverflowMenuButton( $headingItem, $doc, $wrapperNode );
|
2022-10-02 13:32:55 +00:00
|
|
|
|
2022-07-05 23:21:34 +00:00
|
|
|
$latestReplyItem = $headingItem->getLatestReply();
|
2023-12-24 07:33:27 +00:00
|
|
|
|
|
|
|
$bar = null;
|
2022-07-05 23:21:34 +00:00
|
|
|
if ( $latestReplyItem ) {
|
2023-12-24 07:33:27 +00:00
|
|
|
$bar = $doc->createElement( 'div' );
|
|
|
|
$bar->setAttribute(
|
|
|
|
'class',
|
|
|
|
'ext-discussiontools-init-section-bar'
|
2021-01-28 17:19:52 +00:00
|
|
|
);
|
2023-12-24 07:33:27 +00:00
|
|
|
}
|
2021-01-28 17:19:52 +00:00
|
|
|
|
2023-12-24 07:33:27 +00:00
|
|
|
self::addTopicContainer(
|
|
|
|
$wrapperNode, $latestReplyItem, $doc, $headingItem, $bar, $tocInfo
|
|
|
|
);
|
2021-01-28 17:19:52 +00:00
|
|
|
|
2023-12-24 07:33:27 +00:00
|
|
|
self::addSubscribeLink(
|
2024-03-08 19:44:54 +00:00
|
|
|
$headingItem, $doc, $wrapperNode, $latestReplyItem, $bar
|
2023-12-24 07:33:27 +00:00
|
|
|
);
|
2021-01-28 17:19:52 +00:00
|
|
|
|
2023-12-24 07:33:27 +00:00
|
|
|
if ( $latestReplyItem ) {
|
|
|
|
// The check for if ( $latestReplyItem ) prevents $bar from being null
|
|
|
|
// @phan-suppress-next-line PhanTypeMismatchArgumentNullable
|
|
|
|
$wrapperNode->appendChild( $bar );
|
|
|
|
}
|
2021-01-28 17:19:52 +00:00
|
|
|
|
2023-12-24 07:33:27 +00:00
|
|
|
return $wrapperNode;
|
|
|
|
}
|
2021-01-28 17:19:52 +00:00
|
|
|
|
2023-12-24 07:33:27 +00:00
|
|
|
/**
|
|
|
|
* Add a topic container around a heading element.
|
|
|
|
*
|
|
|
|
* A topic container is the information displayed when the "Show discusion activity" user
|
|
|
|
* preference is selected. This displays information such as the latest comment time, number
|
|
|
|
* of comments, and number of editors in the discussion.
|
|
|
|
*/
|
|
|
|
protected static function addTopicContainer(
|
2024-03-08 19:44:54 +00:00
|
|
|
Element $wrapperNode,
|
2024-03-10 22:40:27 +00:00
|
|
|
?ContentCommentItem $latestReplyItem,
|
2024-03-08 19:44:54 +00:00
|
|
|
Document $doc,
|
|
|
|
ContentHeadingItem $headingItem,
|
|
|
|
?Element $bar,
|
2024-03-10 22:40:27 +00:00
|
|
|
array &$tocInfo
|
2023-12-24 07:33:27 +00:00
|
|
|
) {
|
2024-03-10 22:40:27 +00:00
|
|
|
if ( !DOMCompat::getClassList( $wrapperNode )->contains( 'mw-heading' ) ) {
|
2023-12-24 07:33:27 +00:00
|
|
|
DOMCompat::getClassList( $wrapperNode )->add( 'mw-heading' );
|
|
|
|
DOMCompat::getClassList( $wrapperNode )->add( 'mw-heading2' );
|
|
|
|
}
|
|
|
|
DOMCompat::getClassList( $wrapperNode )->add( 'ext-discussiontools-init-section' );
|
2021-01-28 17:19:52 +00:00
|
|
|
|
2023-12-24 07:33:27 +00:00
|
|
|
if ( !$latestReplyItem ) {
|
|
|
|
return;
|
|
|
|
}
|
2021-01-28 17:19:52 +00:00
|
|
|
|
2023-12-24 07:33:27 +00:00
|
|
|
$latestReplyJSON = json_encode( static::getJsonArrayForCommentMarker( $latestReplyItem ) );
|
|
|
|
$latestReply = $doc->createComment(
|
|
|
|
// Timestamp output varies by user timezone, so is formatted later
|
|
|
|
'__DTLATESTCOMMENTTHREAD__' . htmlspecialchars( $latestReplyJSON, ENT_NOQUOTES ) . '__'
|
|
|
|
);
|
|
|
|
|
|
|
|
$commentCount = $doc->createComment(
|
|
|
|
'__DTCOMMENTCOUNT__' . $headingItem->getCommentCount() . '__'
|
|
|
|
);
|
2021-01-28 17:19:52 +00:00
|
|
|
|
2023-12-24 07:33:27 +00:00
|
|
|
$authorCount = $doc->createComment(
|
|
|
|
'__DTAUTHORCOUNT__' . count( $headingItem->getAuthorsBelow() ) . '__'
|
|
|
|
);
|
2021-01-28 17:19:52 +00:00
|
|
|
|
2023-12-24 07:33:27 +00:00
|
|
|
$metadata = $doc->createElement( 'div' );
|
|
|
|
$metadata->setAttribute(
|
|
|
|
'class',
|
|
|
|
'ext-discussiontools-init-section-metadata'
|
|
|
|
);
|
|
|
|
$metadata->appendChild( $latestReply );
|
|
|
|
$metadata->appendChild( $commentCount );
|
|
|
|
$metadata->appendChild( $authorCount );
|
|
|
|
$bar->appendChild( $metadata );
|
|
|
|
|
|
|
|
$tocInfo[ $headingItem->getLinkableTitle() ] = [
|
|
|
|
'commentCount' => $headingItem->getCommentCount(),
|
|
|
|
];
|
|
|
|
}
|
2022-04-07 00:00:05 +00:00
|
|
|
|
2023-12-24 07:33:27 +00:00
|
|
|
/**
|
|
|
|
* Add a subscribe/unsubscribe link to the right of a heading element
|
|
|
|
*/
|
|
|
|
protected static function addSubscribeLink(
|
2024-03-10 22:40:27 +00:00
|
|
|
ContentHeadingItem $headingItem,
|
|
|
|
Document $doc,
|
2024-03-08 19:44:54 +00:00
|
|
|
Element $wrapperNode,
|
2024-03-10 22:40:27 +00:00
|
|
|
?ContentCommentItem $latestReplyItem,
|
2024-03-08 19:44:54 +00:00
|
|
|
?Element $bar
|
2023-12-24 07:33:27 +00:00
|
|
|
) {
|
|
|
|
$headingJSONEscaped = htmlspecialchars(
|
|
|
|
json_encode( static::getJsonForHeadingMarker( $headingItem ) )
|
|
|
|
);
|
|
|
|
|
|
|
|
// Replaced in ::postprocessTopicSubscription() as the text depends on user state
|
|
|
|
if ( $headingItem->isSubscribable() ) {
|
|
|
|
$subscribeButton = $doc->createComment( '__DTSUBSCRIBEBUTTONDESKTOP__' . $headingJSONEscaped );
|
|
|
|
$wrapperNode->insertBefore( $subscribeButton, $wrapperNode->firstChild );
|
2022-09-06 20:53:51 +00:00
|
|
|
}
|
2022-11-15 22:34:16 +00:00
|
|
|
|
2023-12-24 07:33:27 +00:00
|
|
|
if ( !$latestReplyItem ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$actions = $doc->createElement( 'div' );
|
|
|
|
$actions->setAttribute(
|
|
|
|
'class',
|
|
|
|
'ext-discussiontools-init-section-actions'
|
|
|
|
);
|
|
|
|
if ( $headingItem->isSubscribable() ) {
|
|
|
|
$subscribeButton = $doc->createComment( '__DTSUBSCRIBEBUTTONMOBILE__' . $headingJSONEscaped );
|
|
|
|
$actions->appendChild( $subscribeButton );
|
|
|
|
}
|
|
|
|
$bar->appendChild( $actions );
|
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
|
|
|
|
2022-07-30 22:02:17 +00:00
|
|
|
$url = $title->getCanonicalURL();
|
|
|
|
$dtConfig = MediaWikiServices::getInstance()->getConfigFactory()->makeConfig( 'discussiontools' );
|
2023-06-26 15:13:09 +00:00
|
|
|
$enablePermalinksFrontend = $dtConfig->get( 'DiscussionToolsEnablePermalinksFrontend' );
|
2022-07-30 22:02:17 +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 );
|
2022-07-30 22:02:17 +00:00
|
|
|
// Start marker is added after reply link to keep reverse DOM order
|
2020-09-16 12:07:27 +00:00
|
|
|
|
2022-03-18 03:28:06 +00:00
|
|
|
if ( $threadItem instanceof ContentHeadingItem ) {
|
2024-04-20 00:08:35 +00:00
|
|
|
$headline = $threadItem->getHeadlineNode();
|
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 ) {
|
2024-04-20 00:08:35 +00:00
|
|
|
// Hack for tests (T363031), $headline should already be a <h2>
|
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 ) {
|
2023-12-24 07:33:27 +00:00
|
|
|
static::handleHeading( $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 );
|
2022-07-30 22:02:17 +00:00
|
|
|
|
2023-06-26 15:13:09 +00:00
|
|
|
if ( $enablePermalinksFrontend ) {
|
2022-07-30 22:02:17 +00:00
|
|
|
$timestampRanges = $threadItem->getTimestampRanges();
|
|
|
|
$lastTimestamp = end( $timestampRanges );
|
|
|
|
$existingLink = CommentUtils::closestElement( $lastTimestamp->startContainer, [ 'a' ] ) ??
|
|
|
|
CommentUtils::closestElement( $lastTimestamp->endContainer, [ 'a' ] );
|
|
|
|
|
|
|
|
if ( !$existingLink ) {
|
|
|
|
$link = $doc->createElement( 'a' );
|
|
|
|
$link->setAttribute( 'href', $url . '#' . Sanitizer::escapeIdForLink( $threadItem->getId() ) );
|
|
|
|
$link->setAttribute( 'class', 'ext-discussiontools-init-timestamplink' );
|
|
|
|
$lastTimestamp->surroundContents( $link );
|
|
|
|
}
|
|
|
|
}
|
2023-10-11 15:55:46 +00:00
|
|
|
self::addOverflowMenuButton( $threadItem, $doc, $replyButtons );
|
2020-09-16 12:07:27 +00:00
|
|
|
}
|
2022-07-30 22:02:17 +00:00
|
|
|
|
|
|
|
$range->insertNode( $startMarker );
|
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;
|
|
|
|
}
|
2023-12-24 07:33:27 +00:00
|
|
|
$headingElement = static::handleHeading( $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()
|
2023-11-02 16:29:54 +00:00
|
|
|
// The following messages are generated upstream:
|
|
|
|
// * discussiontools-comments-before-first-heading-category-desc
|
2022-12-07 21:42:41 +00:00
|
|
|
->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() );
|
|
|
|
|
2023-11-27 04:04:06 +00:00
|
|
|
// Temporary hack to deal with T351461#9358034: this should be a
|
|
|
|
// call to `setJsConfigVar` but Parsoid is currently reprocessing
|
|
|
|
// content from extensions.
|
|
|
|
$pout->addJsConfigVars( 'wgDiscussionToolsPageThreads', $threadsJSON );
|
2022-08-12 17:13:48 +00:00
|
|
|
|
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
|
|
|
}
|
2023-09-29 15:32:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add an overflow menu button to an element.
|
|
|
|
*
|
|
|
|
* @param ThreadItem $threadItem The heading or comment item
|
|
|
|
* @param Document $document Retrieved by parsing page HTML
|
|
|
|
* @param Element $element The element to add the overflow menu button to
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected static function addOverflowMenuButton(
|
2023-10-11 15:53:55 +00:00
|
|
|
ThreadItem $threadItem, Document $document, Element $element
|
2023-09-29 15:32:45 +00:00
|
|
|
): void {
|
2023-10-11 15:53:55 +00:00
|
|
|
$overflowMenuDataJSON = json_encode( [ 'threadItem' => $threadItem ] );
|
2023-09-29 15:32:45 +00:00
|
|
|
|
|
|
|
$overflowMenuButton = $document->createComment(
|
|
|
|
'__DTELLIPSISBUTTON__' . htmlspecialchars( $overflowMenuDataJSON, ENT_NOQUOTES )
|
|
|
|
);
|
|
|
|
$element->appendChild( $overflowMenuButton );
|
|
|
|
}
|
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.
|
|
|
|
*/
|
2024-03-10 22:40:27 +00:00
|
|
|
public static function removeInteractiveTools( string $text ): string {
|
2022-06-01 21:43:42 +00:00
|
|
|
$text = strtr( $text, [
|
2022-08-06 13:49:36 +00:00
|
|
|
'<!--__DTREPLYBUTTONSCONTENT__-->' => '',
|
2022-06-01 21:43:42 +00:00
|
|
|
] );
|
|
|
|
|
2023-08-25 15:22:31 +00:00
|
|
|
$text = preg_replace( '/<!--__DTELLIPSISBUTTON__(.*?)-->/', '', $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.
|
|
|
|
*/
|
|
|
|
public static function postprocessTopicSubscription(
|
2023-10-09 14:41:28 +00:00
|
|
|
string $text, IContextSource $contextSource,
|
2024-02-20 00:57:43 +00:00
|
|
|
SubscriptionStore $subscriptionStore, bool $isMobile, bool $useButtons
|
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 = [];
|
2024-03-08 19:35:10 +00:00
|
|
|
preg_match_all( '/<!--__DTSUBSCRIBEBUTTONDESKTOP__(.*?)-->/', $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
|
|
|
|
2023-10-09 14:41:28 +00:00
|
|
|
$user = $contextSource->getUser();
|
2021-04-23 18:54:49 +00:00
|
|
|
$items = $subscriptionStore->getSubscriptionItemsForUser(
|
|
|
|
$user,
|
|
|
|
$itemNames
|
|
|
|
);
|
|
|
|
$itemsByName = [];
|
|
|
|
foreach ( $items as $item ) {
|
|
|
|
$itemsByName[ $item->getItemName() ] = $item;
|
|
|
|
}
|
|
|
|
|
2023-10-09 14:41:28 +00:00
|
|
|
$lang = $contextSource->getLanguage();
|
|
|
|
$title = $contextSource->getTitle();
|
2021-04-19 19:14:07 +00:00
|
|
|
$text = preg_replace_callback(
|
2024-03-08 19:35:10 +00:00
|
|
|
'/<!--__(DTSUBSCRIBEBUTTON(?:DESKTOP|MOBILE))__(.*?)-->/',
|
2024-02-20 00:57:43 +00:00
|
|
|
static function ( $matches ) use (
|
|
|
|
$doc, $itemsByName, $itemDataByName, $lang, $title, $isMobile, $useButtons
|
|
|
|
) {
|
2022-12-08 16:15:02 +00:00
|
|
|
$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
|
|
|
|
2024-03-08 19:35:10 +00:00
|
|
|
if ( $buttonIsMobile !== $isMobile ) {
|
2024-02-20 00:57:43 +00:00
|
|
|
return '';
|
|
|
|
}
|
2024-03-08 19:35:10 +00:00
|
|
|
|
|
|
|
if ( !$useButtons ) {
|
2022-12-08 16:15:02 +00:00
|
|
|
$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 {
|
|
|
|
$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.
|
|
|
|
*/
|
|
|
|
public static function postprocessReplyTool(
|
2024-02-20 00:57:43 +00:00
|
|
|
string $text, IContextSource $contextSource, bool $isMobile, bool $useButtons
|
2021-01-28 17:19:52 +00:00
|
|
|
): string {
|
2022-04-21 13:08:59 +00:00
|
|
|
$doc = DOMCompat::newDocument( true );
|
2023-10-09 14:41:28 +00:00
|
|
|
|
|
|
|
$lang = $contextSource->getLanguage();
|
2022-04-21 13:08:59 +00:00
|
|
|
$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__-->/',
|
2024-02-20 00:57:43 +00:00
|
|
|
static function ( $matches ) use ( $doc, $replyLinkText, $replyButtonText, $isMobile, $useButtons, $lang ) {
|
2022-04-21 13:08:59 +00:00
|
|
|
$replyLinkButtons = $doc->createElement( 'span' );
|
|
|
|
|
2024-02-20 00:57:43 +00:00
|
|
|
if ( $useButtons ) {
|
|
|
|
// Visual enhancements button
|
|
|
|
$useIcon = $isMobile || static::isLanguageRequiringReplyIcon( $lang );
|
|
|
|
$replyLinkButton = new \OOUI\ButtonWidget( [
|
|
|
|
'classes' => [ 'ext-discussiontools-init-replybutton' ],
|
|
|
|
'framed' => false,
|
|
|
|
'label' => $replyButtonText,
|
|
|
|
'icon' => $useIcon ? 'share' : null,
|
|
|
|
'flags' => [ 'progressive' ],
|
|
|
|
'infusable' => true,
|
|
|
|
] );
|
2022-04-21 13:08:59 +00:00
|
|
|
|
2024-02-20 00:57:43 +00:00
|
|
|
DOMCompat::setInnerHTML( $replyLinkButtons, $replyLinkButton->toString() );
|
|
|
|
} else {
|
|
|
|
// Reply link
|
|
|
|
$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 = ']';
|
|
|
|
|
|
|
|
$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
|
|
|
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".
|
|
|
|
*/
|
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
|
|
|
*/
|
|
|
|
public static function postprocessVisualEnhancements(
|
2023-10-09 14:41:28 +00:00
|
|
|
string $text, IContextSource $contextSource, bool $isMobile
|
2021-01-28 17:19:52 +00:00
|
|
|
): string {
|
2023-10-09 14:41:28 +00:00
|
|
|
$lang = $contextSource->getLanguage();
|
|
|
|
$user = $contextSource->getUser();
|
2021-01-28 17:19:52 +00:00
|
|
|
$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
|
|
|
|
);
|
2023-10-11 12:54:20 +00:00
|
|
|
$text = preg_replace_callback(
|
|
|
|
'/<!--__DTELLIPSISBUTTON__(.*?)-->/',
|
|
|
|
static function ( $matches ) use ( $contextSource, $isMobile ) {
|
|
|
|
$overflowMenuData = json_decode( htmlspecialchars_decode( $matches[1] ), true ) ?? [];
|
|
|
|
|
|
|
|
// TODO: Remove the fallback to empty array after the parser cache is updated.
|
|
|
|
$threadItem = $overflowMenuData['threadItem'] ?? [];
|
2023-10-11 15:53:55 +00:00
|
|
|
// TODO: Remove $overflowMenuData['editable'] after caches clear
|
|
|
|
if ( isset( $overflowMenuData['editable'] ) ) {
|
|
|
|
$threadItem['uneditableSection'] = !$overflowMenuData['editable'];
|
|
|
|
}
|
2023-10-11 12:54:20 +00:00
|
|
|
$threadItemType = $threadItem['type'] ?? null;
|
|
|
|
if ( !$isMobile && $threadItemType === 'heading' ) {
|
|
|
|
// Displaying the overflow menu next to a topic heading is a bit more
|
|
|
|
// complicated on desktop, so leaving it out for now.
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
$overflowMenuItems = [];
|
|
|
|
$resourceLoaderModules = [];
|
|
|
|
|
|
|
|
self::getHookRunner()->onDiscussionToolsAddOverflowMenuItems(
|
|
|
|
$overflowMenuItems,
|
|
|
|
$resourceLoaderModules,
|
|
|
|
$threadItem,
|
|
|
|
$contextSource
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( $overflowMenuItems ) {
|
|
|
|
usort(
|
2023-09-28 11:00:36 +00:00
|
|
|
$overflowMenuItems,
|
2023-10-11 12:54:20 +00:00
|
|
|
static function ( OverflowMenuItem $itemA, OverflowMenuItem $itemB ): int {
|
|
|
|
return $itemB->getWeight() - $itemA->getWeight();
|
|
|
|
}
|
2023-09-28 11:00:36 +00:00
|
|
|
);
|
2022-07-27 15:12:07 +00:00
|
|
|
|
2023-10-11 12:54:20 +00:00
|
|
|
$overflowButton = new ButtonMenuSelectWidget( [
|
|
|
|
'classes' => [
|
|
|
|
'ext-discussiontools-init-section-overflowMenuButton'
|
|
|
|
],
|
|
|
|
'framed' => false,
|
|
|
|
'icon' => 'ellipsis',
|
|
|
|
'infusable' => true,
|
|
|
|
'data' => [
|
|
|
|
'itemConfigs' => $overflowMenuItems,
|
|
|
|
'resourceLoaderModules' => $resourceLoaderModules
|
|
|
|
]
|
|
|
|
] );
|
|
|
|
return $overflowButton->toString();
|
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
},
|
|
|
|
$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
|
|
|
|
*/
|
|
|
|
public static function postprocessVisualEnhancementsSubtitle(
|
2023-10-09 14:41:28 +00:00
|
|
|
ParserOutput $pout, IContextSource $contextSource
|
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'] ) {
|
2023-10-09 14:41:28 +00:00
|
|
|
$lang = $contextSource->getLanguage();
|
|
|
|
$user = $contextSource->getUser();
|
2023-02-07 00:31:33 +00:00
|
|
|
$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
|
|
|
|
*/
|
|
|
|
public static function postprocessTableOfContents(
|
2023-10-09 14:41:28 +00:00
|
|
|
ParserOutput $pout, IContextSource $contextSource
|
2023-01-27 12:29:17 +00:00
|
|
|
): 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] ) ) {
|
2023-10-09 14:41:28 +00:00
|
|
|
$lang = $contextSource->getLanguage();
|
2023-01-27 12:29:17 +00:00
|
|
|
$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
|
|
|
public static function isEmptyTalkPage( ParserOutput $pout ): bool {
|
|
|
|
return $pout->getExtensionData( 'DiscussionTools-isEmptyTalkPage' ) === true;
|
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
|
|
|
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
|
|
|
public static function hasCommentsInLedeContent( ParserOutput $pout ): bool {
|
|
|
|
return $pout->getExtensionData( 'DiscussionTools-hasCommentsInLedeContent' ) === true;
|
2022-11-30 19:01:42 +00:00
|
|
|
}
|
|
|
|
|
2023-11-29 13:51:18 +00:00
|
|
|
/**
|
|
|
|
* Check if the language requires an icon for the reply button
|
|
|
|
*
|
|
|
|
* @param Language $userLang Language
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function isLanguageRequiringReplyIcon( Language $userLang ): bool {
|
|
|
|
$services = MediaWikiServices::getInstance();
|
|
|
|
|
|
|
|
$dtConfig = $services->getConfigFactory()->makeConfig( 'discussiontools' );
|
2022-12-13 05:49:51 +00:00
|
|
|
$languages = $dtConfig->get( 'DiscussionTools_visualenhancements_reply_icon_languages' );
|
2023-11-29 13:51:18 +00:00
|
|
|
|
|
|
|
if ( array_is_list( $languages ) ) {
|
|
|
|
// Detect legacy list format
|
|
|
|
throw new ConfigException(
|
|
|
|
'DiscussionTools_visualenhancements_reply_icon_languages must be an associative array'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// User language matched exactly and is explicitly set to true or false
|
|
|
|
if ( isset( $languages[ $userLang->getCode() ] ) ) {
|
|
|
|
return (bool)$languages[ $userLang->getCode() ];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check fallback languages
|
|
|
|
$fallbackLanguages = $userLang->getFallbackLanguages();
|
2024-03-27 12:45:21 +00:00
|
|
|
foreach ( $fallbackLanguages as $fallbackLanguage ) {
|
|
|
|
if ( isset( $languages[ $fallbackLanguage ] ) ) {
|
|
|
|
return (bool)$languages[ $fallbackLanguage ];
|
2023-11-29 13:51:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Language not listed, default is to show no icon
|
|
|
|
return false;
|
2022-12-13 05:49:51 +00:00
|
|
|
}
|
|
|
|
|
2020-09-16 12:07:27 +00:00
|
|
|
}
|