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;
|
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;
|
2020-09-16 12:07:27 +00:00
|
|
|
|
2020-11-16 15:50:46 +00:00
|
|
|
class CommentFormatter {
|
2021-02-17 17:16:17 +00:00
|
|
|
// List of features which, when enabled, cause the comment formatter to run
|
|
|
|
public const USE_WITH_FEATURES = [
|
2021-04-08 12:30:28 +00:00
|
|
|
HookUtils::REPLYTOOL,
|
|
|
|
HookUtils::TOPICSUBSCRIPTION,
|
2021-01-28 17:19:52 +00:00
|
|
|
HookUtils::VISUALENHANCEMENTS
|
2021-02-17 17:16:17 +00:00
|
|
|
];
|
|
|
|
|
2021-01-08 22:19:20 +00:00
|
|
|
/**
|
|
|
|
* Get a comment parser object for a DOM element
|
|
|
|
*
|
|
|
|
* This method exists so it can mocked in tests.
|
|
|
|
*
|
|
|
|
* @return CommentParser
|
|
|
|
*/
|
Change CommentParser into a service
Goal:
-----
To have a method like CommentParser::parse(), which just takes a node
to parse and a title and returns plain data, so that we don't need to
keep track of the config to construct a CommentParser object (the
required config like content language is provided by services) and
we don't need to keep that object around after parsing.
Changes:
--------
CommentParser.php:
* …is now a service. Constructor only takes services as arguments.
The node and title are passed to a new parse() method.
* parse() should return plain data, but I split this part to a separate
patch for ease of review: I49bfe019aa460651447fd383f73eafa9d7180a92.
* CommentParser still cheats and accesses global state in a few places,
e.g. calling Title::makeTitleSafe or CommentUtils::getTitleFromUrl,
so we can't turn its tests into true unit tests. This work is left
for future commits.
LanguageData.php:
* …is now a service, instead of a static class.
Parser.js:
* …is not a real service, but it's changed to behave in a similar way.
Constructor takes only the required config as argument,
and node and title are instead passed to a new parse() method.
CommentParserTest.php:
parser.test.js:
* Can be simplified, now that we don't need a useless node and title
to test internal methods that don't use them.
testUtils.js:
* Can be simplified, now that we don't need to override internal
ResourceLoader stuff just to change the parser config.
Change-Id: Iadb7757debe000025e52770ca51ebcf24ca8ee66
2022-02-19 02:43:21 +00:00
|
|
|
protected static function getParser(): CommentParser {
|
|
|
|
return MediaWikiServices::getInstance()->getService( 'DiscussionTools.CommentParser' );
|
2021-01-08 22:19:20 +00:00
|
|
|
}
|
|
|
|
|
2021-01-29 18:31:27 +00:00
|
|
|
/**
|
2021-04-19 18:34:55 +00:00
|
|
|
* Add discussion tools to some HTML
|
2021-01-29 18:31:27 +00:00
|
|
|
*
|
2021-09-27 19:38:09 +00:00
|
|
|
* @param string &$text Parser text output (modified by reference)
|
2021-10-07 15:58:17 +00:00
|
|
|
* @param ParserOutput $pout ParserOutput object for metadata, e.g. parser limit report
|
2022-01-11 15:50:44 +00:00
|
|
|
* @param Title $title
|
2021-01-29 18:31:27 +00:00
|
|
|
*/
|
2022-01-11 15:50:44 +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 {
|
2022-01-11 15:50:44 +00:00
|
|
|
$text = static::addDiscussionToolsInternal( $text, $title );
|
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
|
2021-01-28 17:19:52 +00:00
|
|
|
*/
|
2022-03-18 03:28:06 +00:00
|
|
|
protected static function addTopicContainer( Element $headingElement, ?ContentHeadingItem $headingItem = null ) {
|
2021-01-28 17:19:52 +00:00
|
|
|
$doc = $headingElement->ownerDocument;
|
|
|
|
|
|
|
|
DOMCompat::getClassList( $headingElement )->add( 'ext-discussiontools-init-section' );
|
|
|
|
|
|
|
|
if ( !$headingItem ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$headingNameEscaped = htmlspecialchars( $headingItem->getName(), ENT_NOQUOTES );
|
|
|
|
|
|
|
|
// Replaced in ::postprocessTopicSubscription() as the text depends on user state
|
2022-07-20 12:58:35 +00:00
|
|
|
$subscribeLink = $doc->createComment( '__DTSUBSCRIBELINK__' . $headingNameEscaped );
|
2022-07-22 21:46:41 +00:00
|
|
|
if ( $headingItem->isSubscribable() ) {
|
2022-07-20 12:58:35 +00:00
|
|
|
$headingElement->insertBefore( $subscribeLink, $headingElement->firstChild );
|
2022-07-22 21:46:41 +00:00
|
|
|
}
|
2021-01-28 17:19:52 +00:00
|
|
|
|
2022-07-20 13:05:21 +00:00
|
|
|
$subscribeButton = $doc->createComment( '__DTSUBSCRIBEBUTTONDESKTOP__' . $headingNameEscaped );
|
|
|
|
$headingElement->insertBefore( $subscribeButton, $headingElement->firstChild );
|
|
|
|
|
2021-01-28 17:19:52 +00:00
|
|
|
// Visual enhancements: topic containers
|
|
|
|
$summary = $headingItem->getThreadSummary();
|
|
|
|
if ( $summary['commentCount'] ) {
|
2022-06-23 16:18:00 +00:00
|
|
|
$latestReplyJSON = static::getJsonForCommentMarker( $summary['latestReply'] );
|
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(
|
|
|
|
'__DTCOMMENTCOUNT__' . $summary['commentCount'] . '__'
|
|
|
|
);
|
|
|
|
|
|
|
|
$authorCount = $doc->createComment(
|
|
|
|
'__DTAUTHORCOUNT__' . count( $summary['authors'] ) . '__'
|
|
|
|
);
|
|
|
|
|
|
|
|
// Topic subscriptions
|
2022-07-20 13:05:21 +00:00
|
|
|
$subscribeButton = $doc->createComment( '__DTSUBSCRIBEBUTTONMOBILE__' . $headingNameEscaped );
|
2021-01-28 17:19:52 +00:00
|
|
|
$ellipsisButton = $doc->createComment( '__DTELLIPSISBUTTON__' );
|
|
|
|
|
|
|
|
$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() ) {
|
|
|
|
$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-06-24 16:25:06 +00:00
|
|
|
$headingElement->appendChild( $ellipsisButton );
|
2021-01-28 17:19:52 +00:00
|
|
|
$headingElement->appendChild( $bar );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-01-11 15:50:44 +00:00
|
|
|
* @param Title $title
|
2021-04-19 18:34:55 +00:00
|
|
|
* @return string HTML with discussion tools
|
2020-11-16 15:50:46 +00:00
|
|
|
*/
|
2022-01-11 15:50:44 +00:00
|
|
|
protected static function addDiscussionToolsInternal( string $html, 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
|
|
|
|
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
|
|
|
// TODO: Consider not attaching JSON data to the DOM.
|
|
|
|
// 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
|
|
|
|
|
|
|
$itemData = $threadItem->jsonSerialize();
|
|
|
|
$itemJSON = json_encode( $itemData );
|
|
|
|
|
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' );
|
|
|
|
$headline->setAttribute( 'data-mw-comment', $itemJSON );
|
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 ) {
|
|
|
|
static::addTopicContainer( $headingElement, $threadItem );
|
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' );
|
|
|
|
$replyButtons->setAttribute( 'data-mw-comment', $itemJSON );
|
|
|
|
$replyButtons->appendChild( $doc->createComment( '__DTREPLYBUTTONSCONTENT__' ) );
|
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
|
|
|
|
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 ) {
|
|
|
|
static::addTopicContainer( $headingElement );
|
|
|
|
}
|
|
|
|
|
2022-05-21 05:09:25 +00:00
|
|
|
if ( count( $threadItems ) === 0 ) {
|
|
|
|
$container->appendChild( $doc->createComment( '__DTEMPTYTALKPAGE__' ) );
|
|
|
|
}
|
|
|
|
|
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)
|
2021-08-02 13:45:39 +00:00
|
|
|
return XMLSerializer::serialize( $container, [ 'innerXML' => true, 'smartQuote' => false ] )['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, [
|
|
|
|
'<!--__DTREPLY__-->' => '',
|
|
|
|
'<!--__DTREPLYBRACKETOPEN__-->' => '',
|
|
|
|
'<!--__DTREPLYBRACKETCLOSE__-->' => '',
|
2022-08-06 13:49:36 +00:00
|
|
|
'<!--__DTREPLYBUTTONSCONTENT__-->' => '',
|
2022-06-01 21:43:42 +00:00
|
|
|
'<!--__DTELLIPSISBUTTON__-->' => '',
|
|
|
|
'<!--__DTEMPTYTALKPAGE__-->' => '',
|
|
|
|
] );
|
|
|
|
|
|
|
|
$text = preg_replace( '/<!--__DTSUBSCRIBE__(.*?)-->/', '', $text );
|
|
|
|
$text = preg_replace( '/<!--__DTSUBSCRIBELINK__(.*?)-->/', '', $text );
|
2022-07-20 13:05:21 +00:00
|
|
|
// (DESKTOP|MOBILE)? can be made unconditional once the un-suffixed buttons have cleared from the cache
|
|
|
|
$text = preg_replace( '/<!--__DTSUBSCRIBEBUTTON(DESKTOP|MOBILE)?__(.*?)-->/', '', $text );
|
2022-08-06 13:49:36 +00:00
|
|
|
// To be removed once the old version has cleared from the cache
|
2022-04-21 13:08:59 +00:00
|
|
|
$text = preg_replace( '/<!--__DTREPLYBUTTONS__(.*?)-->/', '', $text );
|
2022-06-01 21:43:42 +00:00
|
|
|
|
|
|
|
return $text;
|
|
|
|
}
|
|
|
|
|
2021-04-19 19:14:07 +00:00
|
|
|
/**
|
|
|
|
* Replace placeholders for topic subscription buttons with the real thing.
|
|
|
|
*
|
|
|
|
* @param string $text
|
|
|
|
* @param Language $lang
|
|
|
|
* @param SubscriptionStore $subscriptionStore
|
|
|
|
* @param UserIdentity $user
|
2022-07-20 13:05:21 +00:00
|
|
|
* @param bool $isMobile
|
2021-04-19 19:14:07 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function postprocessTopicSubscription(
|
2022-07-20 13:05:21 +00:00
|
|
|
string $text, Language $lang, SubscriptionStore $subscriptionStore, UserIdentity $user, bool $isMobile
|
2021-07-22 07:25:13 +00:00
|
|
|
): string {
|
2021-07-29 02:13:09 +00:00
|
|
|
$doc = DOMCompat::newDocument( true );
|
2021-04-23 18:54:49 +00:00
|
|
|
|
|
|
|
$matches = [];
|
2022-06-20 11:04:27 +00:00
|
|
|
preg_match_all( '/<!--__DTSUBSCRIBE(LINK)?__(.*?)-->/', $text, $matches );
|
|
|
|
$itemNames = array_map(
|
|
|
|
static function ( string $itemName, string $link ): string {
|
|
|
|
return $link ? htmlspecialchars_decode( $itemName ) : $itemName;
|
|
|
|
},
|
|
|
|
$matches[2], $matches[1]
|
|
|
|
);
|
|
|
|
|
|
|
|
// TODO: Remove (LINK)? from regex once parser cache has expired (a few weeks):
|
|
|
|
// preg_match_all( '/<!--__DTSUBSCRIBELINK__(.*?)-->/', $text, $matches );
|
|
|
|
// $itemNames = array_map(
|
|
|
|
// static function ( string $itemName ): string {
|
|
|
|
// return htmlspecialchars_decode( $itemName );
|
|
|
|
// },
|
|
|
|
// $matches[1]
|
|
|
|
// );
|
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-06-20 11:04:27 +00:00
|
|
|
'/<!--__DTSUBSCRIBE(LINK)?__(.*?)-->/',
|
2021-05-05 11:44:51 +00:00
|
|
|
static function ( $matches ) use ( $doc, $itemsByName, $lang ) {
|
2022-06-20 11:04:27 +00:00
|
|
|
// TODO: Remove (LINK)? from regex
|
|
|
|
$itemName = $matches[1] ? htmlspecialchars_decode( $matches[2] ) : $matches[2];
|
2021-04-23 18:54:49 +00:00
|
|
|
$isSubscribed = isset( $itemsByName[ $itemName ] ) && !$itemsByName[ $itemName ]->isMuted();
|
2021-08-19 20:35:32 +00:00
|
|
|
$subscribedState = isset( $itemsByName[ $itemName ] ) ? $itemsByName[ $itemName ]->getState() : null;
|
2021-05-05 11:44:51 +00:00
|
|
|
|
2021-04-19 19:14:07 +00:00
|
|
|
$subscribe = $doc->createElement( 'span' );
|
|
|
|
$subscribe->setAttribute(
|
|
|
|
'class',
|
2021-05-05 11:44:51 +00:00
|
|
|
'ext-discussiontools-init-section-subscribe mw-editsection-like'
|
2021-04-19 19:14:07 +00:00
|
|
|
);
|
2021-05-05 11:44:51 +00:00
|
|
|
|
|
|
|
$subscribeLink = $doc->createElement( 'a' );
|
|
|
|
// Set empty 'href' to avoid a:not([href]) selector in MobileFrontend
|
|
|
|
$subscribeLink->setAttribute( 'href', '' );
|
|
|
|
$subscribeLink->setAttribute( 'class', 'ext-discussiontools-init-section-subscribe-link' );
|
|
|
|
$subscribeLink->setAttribute( 'role', 'button' );
|
|
|
|
$subscribeLink->setAttribute( 'tabindex', '0' );
|
|
|
|
$subscribeLink->setAttribute( 'title', wfMessage(
|
|
|
|
$isSubscribed ?
|
|
|
|
'discussiontools-topicsubscription-button-unsubscribe-tooltip' :
|
|
|
|
'discussiontools-topicsubscription-button-subscribe-tooltip'
|
|
|
|
)->inLanguage( $lang )->text() );
|
|
|
|
$subscribeLink->nodeValue = wfMessage(
|
|
|
|
$isSubscribed ?
|
|
|
|
'discussiontools-topicsubscription-button-unsubscribe' :
|
|
|
|
'discussiontools-topicsubscription-button-subscribe'
|
|
|
|
)->inLanguage( $lang )->text();
|
|
|
|
|
2021-08-19 20:35:32 +00:00
|
|
|
if ( $subscribedState !== null ) {
|
|
|
|
$subscribeLink->setAttribute( 'data-mw-subscribed', (string)$subscribedState );
|
2021-05-05 11:44:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$bracket = $doc->createElement( 'span' );
|
|
|
|
$bracket->setAttribute( 'class', 'ext-discussiontools-init-section-subscribe-bracket' );
|
2021-10-26 15:42:50 +00:00
|
|
|
$bracketOpen = $bracket->cloneNode( false );
|
|
|
|
$bracketOpen->nodeValue = '[';
|
|
|
|
$bracketClose = $bracket->cloneNode( false );
|
|
|
|
$bracketClose->nodeValue = ']';
|
2021-05-05 11:44:51 +00:00
|
|
|
|
2021-10-26 15:42:50 +00:00
|
|
|
$subscribe->appendChild( $bracketOpen );
|
2021-05-05 11:44:51 +00:00
|
|
|
$subscribe->appendChild( $subscribeLink );
|
2021-10-26 15:42:50 +00:00
|
|
|
$subscribe->appendChild( $bracketClose );
|
2021-05-05 11:44:51 +00:00
|
|
|
|
2021-04-19 19:14:07 +00:00
|
|
|
return DOMCompat::getOuterHTML( $subscribe );
|
|
|
|
},
|
|
|
|
$text
|
|
|
|
);
|
2021-01-28 17:19:52 +00:00
|
|
|
|
|
|
|
$text = preg_replace_callback(
|
2022-07-20 13:05:21 +00:00
|
|
|
// (DESKTOP|MOBILE)? can be made unconditional once the un-suffixed buttons have cleared from the cache
|
|
|
|
'/<!--__DTSUBSCRIBEBUTTON(DESKTOP|MOBILE)?__(.*?)-->/',
|
|
|
|
static function ( $matches ) use ( $doc, $itemsByName, $lang, $isMobile ) {
|
|
|
|
if ( $matches[1] ) {
|
|
|
|
$buttonIsMobile = $matches[1] === 'MOBILE';
|
|
|
|
if ( $buttonIsMobile !== $isMobile ) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$itemName = htmlspecialchars_decode( $matches[2] );
|
2021-01-28 17:19:52 +00:00
|
|
|
$isSubscribed = isset( $itemsByName[ $itemName ] ) && !$itemsByName[ $itemName ]->isMuted();
|
|
|
|
$subscribedState = isset( $itemsByName[ $itemName ] ) ? $itemsByName[ $itemName ]->getState() : null;
|
|
|
|
|
|
|
|
$subscribe = new \OOUI\ButtonWidget( [
|
|
|
|
'classes' => [ 'ext-discussiontools-init-section-subscribeButton' ],
|
|
|
|
'framed' => false,
|
|
|
|
'icon' => $isSubscribed ? 'bell' : 'bellOutline',
|
|
|
|
'flags' => [ 'progressive' ],
|
|
|
|
'label' => wfMessage( $isSubscribed ?
|
|
|
|
'discussiontools-topicsubscription-button-unsubscribe-label' :
|
|
|
|
'discussiontools-topicsubscription-button-subscribe-label'
|
|
|
|
)->inLanguage( $lang )->text(),
|
|
|
|
'title' => wfMessage( $isSubscribed ?
|
|
|
|
'discussiontools-topicsubscription-button-unsubscribe-tooltip' :
|
|
|
|
'discussiontools-topicsubscription-button-subscribe-tooltip'
|
|
|
|
)->inLanguage( $lang )->text(),
|
|
|
|
'infusable' => true,
|
|
|
|
] );
|
|
|
|
|
|
|
|
if ( $subscribedState !== null ) {
|
|
|
|
$subscribe->setAttributes( [ 'data-mw-subscribed' => (string)$subscribedState ] );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $subscribe->toString();
|
|
|
|
},
|
|
|
|
$text
|
|
|
|
);
|
|
|
|
|
2021-04-19 19:14:07 +00:00
|
|
|
return $text;
|
|
|
|
}
|
|
|
|
|
2021-04-15 21:09:55 +00:00
|
|
|
/**
|
|
|
|
* Replace placeholders for reply links with the real thing.
|
|
|
|
*
|
|
|
|
* @param string $text
|
|
|
|
* @param Language $lang
|
2022-04-21 13:08:59 +00:00
|
|
|
* @param bool $isMobile
|
2021-04-15 21:09:55 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function postprocessReplyTool(
|
2022-07-27 14:19:31 +00:00
|
|
|
string $text, Language $lang, bool $isMobile
|
2021-01-28 17:19:52 +00:00
|
|
|
): string {
|
2022-04-21 13:08:59 +00:00
|
|
|
$doc = DOMCompat::newDocument( true );
|
|
|
|
$replyLinkText = wfMessage( 'discussiontools-replylink' )->inLanguage( $lang )->escaped();
|
|
|
|
$replyButtonText = wfMessage( 'discussiontools-replybutton' )->inLanguage( $lang )->escaped();
|
|
|
|
|
2022-08-06 13:49:36 +00:00
|
|
|
// Remove __DTREPLYBUTTONS__ once it has cleared from the cache
|
2022-04-21 13:08:59 +00:00
|
|
|
$text = preg_replace_callback(
|
2022-08-06 13:49:36 +00:00
|
|
|
'/<!--__DTREPLYBUTTONS__(.*?)__-->|<!--__DTREPLYBUTTONSCONTENT__-->/',
|
2022-04-21 13:08:59 +00:00
|
|
|
static function ( $matches ) use ( $doc, $replyLinkText, $replyButtonText, $isMobile ) {
|
2022-08-06 13:49:36 +00:00
|
|
|
$itemJSON = $matches[1] ?? null;
|
2022-04-21 13:08:59 +00:00
|
|
|
|
|
|
|
$replyLinkButtons = $doc->createElement( 'span' );
|
2022-08-06 13:49:36 +00:00
|
|
|
if ( $itemJSON ) {
|
|
|
|
$replyLinkButtons->setAttribute( 'class', 'ext-discussiontools-init-replylink-buttons' );
|
|
|
|
$replyLinkButtons->setAttribute( 'data-mw-comment', $itemJSON );
|
|
|
|
}
|
2022-04-21 13:08:59 +00:00
|
|
|
|
|
|
|
// Reply
|
|
|
|
$replyLink = $doc->createElement( 'a' );
|
|
|
|
$replyLink->setAttribute( 'class', 'ext-discussiontools-init-replylink-reply' );
|
|
|
|
$replyLink->setAttribute( 'role', 'button' );
|
|
|
|
$replyLink->setAttribute( 'tabindex', '0' );
|
|
|
|
// Set empty 'href' to avoid a:not([href]) selector in MobileFrontend
|
|
|
|
$replyLink->setAttribute( 'href', '' );
|
|
|
|
$replyLink->textContent = $replyLinkText;
|
|
|
|
|
|
|
|
$bracket = $doc->createElement( 'span' );
|
|
|
|
$bracket->setAttribute( 'class', 'ext-discussiontools-init-replylink-bracket' );
|
|
|
|
$bracketOpen = $bracket->cloneNode( false );
|
|
|
|
$bracketClose = $bracket->cloneNode( false );
|
|
|
|
$bracketOpen->textContent = '[';
|
|
|
|
$bracketClose->textContent = ']';
|
|
|
|
|
|
|
|
// Visual enhancements button
|
|
|
|
$replyLinkButton = new \OOUI\ButtonWidget( [
|
|
|
|
'classes' => [ 'ext-discussiontools-init-replybutton' ],
|
|
|
|
'framed' => false,
|
|
|
|
'label' => $replyButtonText,
|
|
|
|
'icon' => $isMobile ? 'share' : null,
|
|
|
|
'flags' => [ 'progressive' ],
|
|
|
|
'infusable' => true,
|
|
|
|
] );
|
|
|
|
|
2022-08-01 21:08:05 +00:00
|
|
|
DOMCompat::setInnerHTML( $replyLinkButtons, $replyLinkButton->toString() );
|
|
|
|
$replyLinkButtons->appendChild( $bracketOpen );
|
|
|
|
$replyLinkButtons->appendChild( $replyLink );
|
|
|
|
$replyLinkButtons->appendChild( $bracketClose );
|
2022-04-21 13:08:59 +00:00
|
|
|
|
2022-08-06 13:49:36 +00:00
|
|
|
return $itemJSON ?
|
|
|
|
DOMCompat::getOuterHTML( $replyLinkButtons ) :
|
|
|
|
DOMCompat::getInnerHTML( $replyLinkButtons );
|
2022-04-21 13:08:59 +00:00
|
|
|
},
|
|
|
|
$text
|
|
|
|
);
|
2021-10-26 15:42:50 +00:00
|
|
|
|
2022-04-21 13:08:59 +00:00
|
|
|
// Old style replacements for content still in parser cache
|
2021-10-26 15:42:50 +00:00
|
|
|
$text = strtr( $text, [
|
2022-04-21 13:08:59 +00:00
|
|
|
'<!--__DTREPLY__-->' => $replyLinkText,
|
2021-10-26 15:42:50 +00:00
|
|
|
'<!--__DTREPLYBRACKETOPEN__-->' => '[',
|
|
|
|
'<!--__DTREPLYBRACKETCLOSE__-->' => ']',
|
|
|
|
] );
|
|
|
|
|
2021-04-15 21:09:55 +00:00
|
|
|
return $text;
|
|
|
|
}
|
|
|
|
|
2021-01-28 17:19:52 +00:00
|
|
|
/**
|
|
|
|
* Create a meta item label
|
|
|
|
*
|
|
|
|
* @param string $className
|
2022-06-23 16:18:00 +00:00
|
|
|
* @param string|\OOUI\HtmlSnippet $label Label
|
2021-01-28 17:19:52 +00:00
|
|
|
* @return \OOUI\Tag
|
|
|
|
*/
|
2022-06-23 16:18:00 +00:00
|
|
|
private static function metaLabel( string $className, $label ): \OOUI\Tag {
|
2021-01-28 17:19:52 +00:00
|
|
|
return ( new \OOUI\Tag( 'span' ) )
|
|
|
|
->addClasses( [ 'ext-discussiontools-init-section-metaitem', $className ] )
|
|
|
|
->appendContent( $label );
|
|
|
|
}
|
|
|
|
|
2022-06-23 16:18:00 +00:00
|
|
|
/**
|
|
|
|
* Get JSON for a commentItem that can be inserted into a comment marker
|
|
|
|
*
|
2022-03-18 03:28:06 +00:00
|
|
|
* @param ContentCommentItem $commentItem Comment item
|
2022-06-23 16:18:00 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2022-03-18 03:28:06 +00:00
|
|
|
private static function getJsonForCommentMarker( ContentCommentItem $commentItem ): string {
|
2022-06-23 16:18:00 +00:00
|
|
|
$JSON = [
|
|
|
|
'id' => $commentItem->getId(),
|
|
|
|
'timestamp' => $commentItem->getTimestampString()
|
|
|
|
];
|
|
|
|
return json_encode( $JSON );
|
|
|
|
}
|
|
|
|
|
2021-01-28 17:19:52 +00:00
|
|
|
/**
|
|
|
|
* Get a relative timestamp from a signature timestamp.
|
|
|
|
*
|
|
|
|
* Signature timestamps don't have seconds-level accuracy, so any
|
|
|
|
* time difference of less than 120 seconds is treated as being
|
|
|
|
* posted "just now".
|
|
|
|
*
|
|
|
|
* @param MWTimestamp $timestamp
|
|
|
|
* @param Language $lang
|
|
|
|
* @param UserIdentity $user
|
|
|
|
* @return string
|
|
|
|
*/
|
2022-06-28 17:34:17 +00:00
|
|
|
public static function getSignatureRelativeTime(
|
|
|
|
MWTimestamp $timestamp, Language $lang, UserIdentity $user
|
|
|
|
): string {
|
2021-01-28 17:19:52 +00:00
|
|
|
if ( time() - intval( $timestamp->getTimestamp() ) < 120 ) {
|
|
|
|
$timestamp = new MWTimestamp();
|
|
|
|
}
|
|
|
|
return $lang->getHumanTimestamp( $timestamp, null, $user );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Post-process timestamps
|
|
|
|
*
|
|
|
|
* @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-05-21 05:09:25 +00:00
|
|
|
/**
|
|
|
|
* Check if the talk page had no comments or headings.
|
|
|
|
*
|
|
|
|
* @param string $text
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function isEmptyTalkPage( string $text ): bool {
|
|
|
|
return strpos( $text, '<!--__DTEMPTYTALKPAGE__-->' ) !== false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Append content to an empty talk page
|
|
|
|
*
|
|
|
|
* @param string $text
|
|
|
|
* @param string $content
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function appendToEmptyTalkPage( string $text, string $content ): string {
|
|
|
|
return str_replace( '<!--__DTEMPTYTALKPAGE__-->', $content, $text );
|
|
|
|
}
|
|
|
|
|
2020-09-16 12:07:27 +00:00
|
|
|
}
|