2019-12-10 02:38:17 +00:00
|
|
|
<?php
|
|
|
|
|
2020-05-14 22:44:49 +00:00
|
|
|
namespace MediaWiki\Extension\DiscussionTools;
|
|
|
|
|
|
|
|
use DateInterval;
|
|
|
|
use DateTime;
|
|
|
|
use DateTimeImmutable;
|
|
|
|
use DateTimeZone;
|
2022-10-28 18:24:02 +00:00
|
|
|
use InvalidArgumentException;
|
2020-05-14 22:44:49 +00:00
|
|
|
use Language;
|
2022-10-28 18:24:02 +00:00
|
|
|
use LogicException;
|
2023-12-11 15:38:02 +00:00
|
|
|
use MediaWiki\Config\Config;
|
2022-03-18 03:28:06 +00:00
|
|
|
use MediaWiki\Extension\DiscussionTools\ThreadItem\ContentCommentItem;
|
|
|
|
use MediaWiki\Extension\DiscussionTools\ThreadItem\ContentHeadingItem;
|
|
|
|
use MediaWiki\Extension\DiscussionTools\ThreadItem\ContentThreadItem;
|
2022-02-21 17:39:36 +00:00
|
|
|
use MediaWiki\Languages\LanguageConverterFactory;
|
2023-12-11 15:38:02 +00:00
|
|
|
use MediaWiki\Title\MalformedTitleException;
|
|
|
|
use MediaWiki\Title\TitleParser;
|
|
|
|
use MediaWiki\Title\TitleValue;
|
|
|
|
use MediaWiki\Utils\MWTimestamp;
|
2022-10-28 18:24:02 +00:00
|
|
|
use RuntimeException;
|
2022-02-19 03:25:14 +00:00
|
|
|
use Wikimedia\Assert\Assert;
|
2021-09-14 20:19:05 +00:00
|
|
|
use Wikimedia\IPUtils;
|
2021-07-29 02:16:15 +00:00
|
|
|
use Wikimedia\Parsoid\DOM\Element;
|
|
|
|
use Wikimedia\Parsoid\DOM\Node;
|
|
|
|
use Wikimedia\Parsoid\DOM\Text;
|
2022-01-24 16:16:20 +00:00
|
|
|
use Wikimedia\Parsoid\Utils\DOMCompat;
|
2023-11-30 17:37:38 +00:00
|
|
|
use Wikimedia\Timestamp\TimestampException;
|
2019-12-10 02:38:17 +00:00
|
|
|
|
|
|
|
// TODO consider making timestamp parsing not a returned function
|
|
|
|
|
2020-05-14 22:44:49 +00:00
|
|
|
class CommentParser {
|
2022-10-21 19:34:18 +00:00
|
|
|
|
2022-02-21 21:42:14 +00:00
|
|
|
/**
|
|
|
|
* How far backwards we look for a signature associated with a timestamp before giving up.
|
|
|
|
* Note that this is not a hard limit on the length of signatures we detect.
|
|
|
|
*/
|
2019-12-10 02:38:17 +00:00
|
|
|
private const SIGNATURE_SCAN_LIMIT = 100;
|
|
|
|
|
2022-10-21 19:34:18 +00:00
|
|
|
private Config $config;
|
|
|
|
private Language $language;
|
|
|
|
private LanguageConverterFactory $languageConverterFactory;
|
|
|
|
private TitleParser $titleParser;
|
2020-09-03 20:59:33 +00:00
|
|
|
|
2023-09-16 00:31:47 +00:00
|
|
|
/** @var string[] */
|
|
|
|
private array $dateFormat;
|
|
|
|
/** @var string[][] */
|
|
|
|
private array $digits;
|
2020-09-03 20:59:33 +00:00
|
|
|
/** @var string[][] */
|
2020-06-10 20:20:05 +00:00
|
|
|
private $contLangMessages;
|
2023-09-16 00:31:47 +00:00
|
|
|
private string $localTimezone;
|
|
|
|
/** @var string[][] */
|
|
|
|
private array $timezones;
|
|
|
|
private string $specialContributionsName;
|
2020-06-10 20:20:05 +00:00
|
|
|
|
2022-10-21 19:34:18 +00:00
|
|
|
private Element $rootNode;
|
|
|
|
private TitleValue $title;
|
2022-02-21 17:39:36 +00:00
|
|
|
|
2019-12-10 02:38:17 +00:00
|
|
|
/**
|
|
|
|
* @param Config $config
|
2022-02-21 17:39:36 +00:00
|
|
|
* @param Language $language Content language
|
|
|
|
* @param LanguageConverterFactory $languageConverterFactory
|
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
|
|
|
* @param LanguageData $languageData
|
2022-02-21 22:07:38 +00:00
|
|
|
* @param TitleParser $titleParser
|
2019-12-10 02:38:17 +00:00
|
|
|
*/
|
2022-01-11 15:50:44 +00:00
|
|
|
public function __construct(
|
2022-02-21 17:39:36 +00:00
|
|
|
Config $config,
|
|
|
|
Language $language,
|
|
|
|
LanguageConverterFactory $languageConverterFactory,
|
2022-02-21 22:07:38 +00:00
|
|
|
LanguageData $languageData,
|
|
|
|
TitleParser $titleParser
|
2022-01-11 15:50:44 +00:00
|
|
|
) {
|
2019-12-10 02:38:17 +00:00
|
|
|
$this->config = $config;
|
2020-09-03 20:59:33 +00:00
|
|
|
$this->language = $language;
|
2022-02-21 17:39:36 +00:00
|
|
|
$this->languageConverterFactory = $languageConverterFactory;
|
2022-02-21 22:07:38 +00:00
|
|
|
$this->titleParser = $titleParser;
|
2020-05-19 18:40:05 +00:00
|
|
|
|
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
|
|
|
$data = $languageData->getLocalData();
|
2020-05-19 18:40:05 +00:00
|
|
|
$this->dateFormat = $data['dateFormat'];
|
|
|
|
$this->digits = $data['digits'];
|
|
|
|
$this->contLangMessages = $data['contLangMessages'];
|
|
|
|
$this->localTimezone = $data['localTimezone'];
|
|
|
|
$this->timezones = $data['timezones'];
|
2022-02-21 22:07:38 +00:00
|
|
|
$this->specialContributionsName = $data['specialContributionsName'];
|
2019-12-10 02:38:17 +00:00
|
|
|
}
|
|
|
|
|
2020-07-20 21:15:03 +00:00
|
|
|
/**
|
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
|
|
|
* Parse a discussion page.
|
|
|
|
*
|
|
|
|
* @param Element $rootNode Root node of content to parse
|
2022-02-21 22:07:38 +00:00
|
|
|
* @param TitleValue $title Title of the page being parsed
|
2022-03-18 03:28:06 +00:00
|
|
|
* @return ContentThreadItemSet
|
2020-07-20 21:15:03 +00:00
|
|
|
*/
|
2022-03-18 03:28:06 +00:00
|
|
|
public function parse( Element $rootNode, TitleValue $title ): ContentThreadItemSet {
|
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
|
|
|
$this->rootNode = $rootNode;
|
|
|
|
$this->title = $title;
|
2022-02-19 06:31:34 +00:00
|
|
|
|
|
|
|
$result = $this->buildThreadItems();
|
|
|
|
$this->buildThreads( $result );
|
|
|
|
$this->computeIdsAndNames( $result );
|
|
|
|
|
|
|
|
return $result;
|
2019-12-10 02:38:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-09-30 18:41:38 +00:00
|
|
|
* Return the next leaf node in the tree order that is likely a part of a discussion comment,
|
|
|
|
* rather than some boring "separator" element.
|
2019-12-10 02:38:17 +00:00
|
|
|
*
|
2020-12-14 20:13:55 +00:00
|
|
|
* Currently, this can return a Text node with content other than whitespace, or an Element node
|
|
|
|
* that is a "void element" or "text element", except some special cases that we treat as comment
|
|
|
|
* separators (isCommentSeparator()).
|
2019-12-10 02:38:17 +00:00
|
|
|
*
|
2024-02-08 15:19:53 +00:00
|
|
|
* @param ?Node $node Node after which to start searching
|
|
|
|
* (if null, start at the beginning of the document).
|
2021-07-29 02:16:15 +00:00
|
|
|
* @return Node
|
2019-12-10 02:38:17 +00:00
|
|
|
*/
|
2024-02-08 15:19:53 +00:00
|
|
|
private function nextInterestingLeafNode( ?Node $node ): Node {
|
2020-07-20 21:15:03 +00:00
|
|
|
$rootNode = $this->rootNode;
|
2020-07-15 14:53:11 +00:00
|
|
|
$treeWalker = new TreeWalker(
|
|
|
|
$rootNode,
|
|
|
|
NodeFilter::SHOW_ELEMENT | NodeFilter::SHOW_TEXT,
|
2021-12-04 02:06:57 +00:00
|
|
|
static function ( $n ) use ( $node, $rootNode ) {
|
2024-02-08 15:19:53 +00:00
|
|
|
// Skip past the starting node and its descendants
|
|
|
|
if ( $n === $node || $n->parentNode === $node ) {
|
2020-07-15 14:53:11 +00:00
|
|
|
return NodeFilter::FILTER_REJECT;
|
2019-12-10 02:38:17 +00:00
|
|
|
}
|
2020-12-14 20:13:55 +00:00
|
|
|
// Ignore some elements usually used as separators or headers (and their descendants)
|
2021-12-04 02:06:57 +00:00
|
|
|
if ( CommentUtils::isCommentSeparator( $n ) ) {
|
2020-12-14 20:13:55 +00:00
|
|
|
return NodeFilter::FILTER_REJECT;
|
|
|
|
}
|
2021-01-26 03:36:27 +00:00
|
|
|
// Ignore nodes with no rendering that mess up our indentation detection
|
|
|
|
if ( CommentUtils::isRenderingTransparentNode( $n ) ) {
|
|
|
|
return NodeFilter::FILTER_REJECT;
|
|
|
|
}
|
2021-12-04 02:06:57 +00:00
|
|
|
if ( CommentUtils::isCommentContent( $n ) ) {
|
2020-07-15 14:53:11 +00:00
|
|
|
return NodeFilter::FILTER_ACCEPT;
|
|
|
|
}
|
|
|
|
return NodeFilter::FILTER_SKIP;
|
2019-12-10 02:38:17 +00:00
|
|
|
}
|
2020-07-15 14:53:11 +00:00
|
|
|
);
|
2024-02-08 15:19:53 +00:00
|
|
|
if ( $node ) {
|
|
|
|
$treeWalker->currentNode = $node;
|
|
|
|
}
|
2020-07-15 14:53:11 +00:00
|
|
|
$treeWalker->nextNode();
|
|
|
|
if ( !$treeWalker->currentNode ) {
|
2022-10-28 18:24:02 +00:00
|
|
|
throw new RuntimeException( 'nextInterestingLeafNode not found' );
|
2020-07-15 14:53:11 +00:00
|
|
|
}
|
|
|
|
return $treeWalker->currentNode;
|
2019-12-10 02:38:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string[] $values Values to match
|
|
|
|
* @return string Regular expression
|
|
|
|
*/
|
2021-07-22 07:25:13 +00:00
|
|
|
private static function regexpAlternateGroup( array $values ): string {
|
2021-05-05 06:59:38 +00:00
|
|
|
return '(' . implode( '|', array_map( static function ( string $x ) {
|
2019-12-10 02:38:17 +00:00
|
|
|
return preg_quote( $x, '/' );
|
|
|
|
}, $values ) ) . ')';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-09-03 20:59:33 +00:00
|
|
|
* Get text of localisation messages in content language.
|
|
|
|
*
|
|
|
|
* @param string $contLangVariant Content language variant
|
|
|
|
* @param string[] $messages Message keys
|
2019-12-10 02:38:17 +00:00
|
|
|
* @return string[] Message values
|
|
|
|
*/
|
2021-07-22 07:25:13 +00:00
|
|
|
private function getMessages( string $contLangVariant, array $messages ): array {
|
2020-09-03 20:59:33 +00:00
|
|
|
return array_map( function ( string $key ) use ( $contLangVariant ) {
|
|
|
|
return $this->contLangMessages[$contLangVariant][$key];
|
|
|
|
}, $messages );
|
2019-12-10 02:38:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a regexp that matches timestamps generated using the given date format.
|
|
|
|
*
|
|
|
|
* This only supports format characters that are used by the default date format in any of
|
|
|
|
* MediaWiki's languages, namely: D, d, F, G, H, i, j, l, M, n, Y, xg, xkY (and escape characters),
|
|
|
|
* and only dates when MediaWiki existed, let's say 2000 onwards (Thai dates before 1941 are
|
|
|
|
* complicated).
|
|
|
|
*
|
2020-09-03 20:59:33 +00:00
|
|
|
* @param string $contLangVariant Content language variant
|
2019-12-10 02:38:17 +00:00
|
|
|
* @param string $format Date format
|
2020-05-12 11:51:20 +00:00
|
|
|
* @param string $digitsRegexp Regular expression matching a single localised digit, e.g. '[0-9]'
|
|
|
|
* @param array $tzAbbrs Associative array mapping localised timezone abbreviations to
|
|
|
|
* IANA abbreviations, for the local timezone, e.g. [ 'EDT' => 'EDT', 'EST' => 'EST' ]
|
2019-12-10 02:38:17 +00:00
|
|
|
* @return string Regular expression
|
|
|
|
*/
|
|
|
|
private function getTimestampRegexp(
|
2020-09-03 20:59:33 +00:00
|
|
|
string $contLangVariant, string $format, string $digitsRegexp, array $tzAbbrs
|
2021-07-22 07:25:13 +00:00
|
|
|
): string {
|
2019-12-10 02:38:17 +00:00
|
|
|
$formatLength = strlen( $format );
|
|
|
|
$s = '';
|
2023-06-04 18:16:56 +00:00
|
|
|
$raw = false;
|
2019-12-10 02:38:17 +00:00
|
|
|
// Adapted from Language::sprintfDate()
|
|
|
|
for ( $p = 0; $p < $formatLength; $p++ ) {
|
|
|
|
$num = false;
|
|
|
|
$code = $format[ $p ];
|
|
|
|
if ( $code === 'x' && $p < $formatLength - 1 ) {
|
|
|
|
$code .= $format[++$p];
|
|
|
|
}
|
|
|
|
if ( $code === 'xk' && $p < $formatLength - 1 ) {
|
|
|
|
$code .= $format[++$p];
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ( $code ) {
|
2020-10-29 10:52:56 +00:00
|
|
|
case 'xx':
|
2019-12-10 02:38:17 +00:00
|
|
|
$s .= 'x';
|
|
|
|
break;
|
|
|
|
case 'xg':
|
2022-06-09 13:51:33 +00:00
|
|
|
$s .= static::regexpAlternateGroup(
|
2020-09-03 20:59:33 +00:00
|
|
|
$this->getMessages( $contLangVariant, Language::MONTH_GENITIVE_MESSAGES )
|
2019-12-10 02:38:17 +00:00
|
|
|
);
|
|
|
|
break;
|
2023-06-04 18:16:56 +00:00
|
|
|
case 'xn':
|
|
|
|
$raw = true;
|
|
|
|
break;
|
2019-12-10 02:38:17 +00:00
|
|
|
case 'd':
|
|
|
|
$num = '2';
|
|
|
|
break;
|
|
|
|
case 'D':
|
2022-06-09 13:51:33 +00:00
|
|
|
$s .= static::regexpAlternateGroup(
|
2020-09-03 20:59:33 +00:00
|
|
|
$this->getMessages( $contLangVariant, Language::WEEKDAY_ABBREVIATED_MESSAGES )
|
2019-12-10 02:38:17 +00:00
|
|
|
);
|
|
|
|
break;
|
|
|
|
case 'j':
|
|
|
|
$num = '1,2';
|
|
|
|
break;
|
|
|
|
case 'l':
|
2022-06-09 13:51:33 +00:00
|
|
|
$s .= static::regexpAlternateGroup(
|
2020-09-03 20:59:33 +00:00
|
|
|
$this->getMessages( $contLangVariant, Language::WEEKDAY_MESSAGES )
|
2019-12-10 02:38:17 +00:00
|
|
|
);
|
|
|
|
break;
|
|
|
|
case 'F':
|
2022-06-09 13:51:33 +00:00
|
|
|
$s .= static::regexpAlternateGroup(
|
2020-09-03 20:59:33 +00:00
|
|
|
$this->getMessages( $contLangVariant, Language::MONTH_MESSAGES )
|
2019-12-10 02:38:17 +00:00
|
|
|
);
|
|
|
|
break;
|
|
|
|
case 'M':
|
2022-06-09 13:51:33 +00:00
|
|
|
$s .= static::regexpAlternateGroup(
|
2020-09-03 20:59:33 +00:00
|
|
|
$this->getMessages( $contLangVariant, Language::MONTH_ABBREVIATED_MESSAGES )
|
2019-12-10 02:38:17 +00:00
|
|
|
);
|
|
|
|
break;
|
2023-06-04 18:16:56 +00:00
|
|
|
case 'm':
|
|
|
|
$num = '2';
|
|
|
|
break;
|
2019-12-10 02:38:17 +00:00
|
|
|
case 'n':
|
|
|
|
$num = '1,2';
|
|
|
|
break;
|
|
|
|
case 'Y':
|
|
|
|
$num = '4';
|
|
|
|
break;
|
|
|
|
case 'xkY':
|
|
|
|
$num = '4';
|
|
|
|
break;
|
|
|
|
case 'G':
|
|
|
|
$num = '1,2';
|
|
|
|
break;
|
|
|
|
case 'H':
|
|
|
|
$num = '2';
|
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
$num = '2';
|
|
|
|
break;
|
2023-06-04 18:16:56 +00:00
|
|
|
case 's':
|
|
|
|
$num = '2';
|
|
|
|
break;
|
2019-12-10 02:38:17 +00:00
|
|
|
case '\\':
|
|
|
|
// Backslash escaping
|
|
|
|
if ( $p < $formatLength - 1 ) {
|
|
|
|
$s .= preg_quote( $format[++$p], '/' );
|
|
|
|
} else {
|
|
|
|
$s .= preg_quote( '\\', '/' );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '"':
|
|
|
|
// Quoted literal
|
|
|
|
if ( $p < $formatLength - 1 ) {
|
|
|
|
$endQuote = strpos( $format, '"', $p + 1 );
|
|
|
|
if ( $endQuote === false ) {
|
|
|
|
// No terminating quote, assume literal "
|
|
|
|
$s .= '"';
|
|
|
|
} else {
|
|
|
|
$s .= preg_quote( substr( $format, $p + 1, $endQuote - $p - 1 ), '/' );
|
|
|
|
$p = $endQuote;
|
|
|
|
}
|
|
|
|
} else {
|
2020-05-12 23:21:02 +00:00
|
|
|
// Quote at end of string, assume literal "
|
2019-12-10 02:38:17 +00:00
|
|
|
$s .= '"';
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2022-05-16 22:48:24 +00:00
|
|
|
// Copy whole characters together, instead of single bytes
|
|
|
|
$char = mb_substr( mb_strcut( $format, $p, 4 ), 0, 1 );
|
|
|
|
$s .= preg_quote( $char, '/' );
|
|
|
|
$p += strlen( $char ) - 1;
|
2019-12-10 02:38:17 +00:00
|
|
|
}
|
|
|
|
if ( $num !== false ) {
|
2023-06-04 18:16:56 +00:00
|
|
|
if ( $raw ) {
|
|
|
|
$s .= '([0-9]{' . $num . '})';
|
|
|
|
$raw = false;
|
|
|
|
} else {
|
|
|
|
$s .= '(' . $digitsRegexp . '{' . $num . '})';
|
|
|
|
}
|
2019-12-10 02:38:17 +00:00
|
|
|
}
|
2022-05-16 22:48:24 +00:00
|
|
|
// Ignore some invisible Unicode characters that often sneak into copy-pasted timestamps (T308448)
|
|
|
|
$s .= '[\\x{200E}\\x{200F}]?';
|
2019-12-10 02:38:17 +00:00
|
|
|
}
|
|
|
|
|
2022-06-09 13:51:33 +00:00
|
|
|
$tzRegexp = static::regexpAlternateGroup( array_keys( $tzAbbrs ) );
|
2019-12-10 02:38:17 +00:00
|
|
|
|
2020-05-12 11:51:20 +00:00
|
|
|
// Hard-coded parentheses and space like in Parser::pstPass2
|
|
|
|
// Ignore some invisible Unicode characters that often sneak into copy-pasted timestamps (T245784)
|
2019-12-10 02:38:17 +00:00
|
|
|
// \uNNNN syntax can only be used from PHP 7.3
|
2022-05-16 22:48:24 +00:00
|
|
|
return '/' . $s . ' [\\x{200E}\\x{200F}]?\\(' . $tzRegexp . '\\)/u';
|
2019-12-10 02:38:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a function that parses timestamps generated using the given date format, based on the result
|
|
|
|
* of matching the regexp returned by getTimestampRegexp()
|
|
|
|
*
|
2020-09-03 20:59:33 +00:00
|
|
|
* @param string $contLangVariant Content language variant
|
2019-12-10 02:38:17 +00:00
|
|
|
* @param string $format Date format, as used by MediaWiki
|
2023-10-30 08:20:27 +00:00
|
|
|
* @param array<int,string>|null $digits Localised digits from 0 to 9, e.g. `[ '0', '1', ..., '9' ]`
|
2019-12-10 02:38:17 +00:00
|
|
|
* @param string $localTimezone Local timezone IANA name, e.g. `America/New_York`
|
|
|
|
* @param array $tzAbbrs Map of localised timezone abbreviations to IANA abbreviations
|
|
|
|
* for the local timezone, e.g. [ 'EDT' => 'EDT', 'EST' => 'EST' ]
|
2020-05-15 20:59:36 +00:00
|
|
|
* @return callable Parser function
|
2019-12-10 02:38:17 +00:00
|
|
|
*/
|
|
|
|
private function getTimestampParser(
|
2020-09-03 20:59:33 +00:00
|
|
|
string $contLangVariant, string $format, ?array $digits, string $localTimezone, array $tzAbbrs
|
2021-07-22 07:25:13 +00:00
|
|
|
): callable {
|
2023-10-30 08:20:27 +00:00
|
|
|
$untransformDigits = static function ( string $text ) use ( $digits ): int {
|
|
|
|
return (int)( $digits ? strtr( $text, array_flip( $digits ) ) : $text );
|
2019-12-10 02:38:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
$formatLength = strlen( $format );
|
|
|
|
$matchingGroups = [];
|
|
|
|
for ( $p = 0; $p < $formatLength; $p++ ) {
|
|
|
|
$code = $format[$p];
|
|
|
|
if ( $code === 'x' && $p < $formatLength - 1 ) {
|
|
|
|
$code .= $format[++$p];
|
|
|
|
}
|
|
|
|
if ( $code === 'xk' && $p < $formatLength - 1 ) {
|
|
|
|
$code .= $format[++$p];
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ( $code ) {
|
|
|
|
case 'xx':
|
2023-06-04 18:16:56 +00:00
|
|
|
case 'xn':
|
2019-12-10 02:38:17 +00:00
|
|
|
break;
|
|
|
|
case 'xg':
|
|
|
|
case 'd':
|
|
|
|
case 'j':
|
|
|
|
case 'D':
|
|
|
|
case 'l':
|
|
|
|
case 'F':
|
|
|
|
case 'M':
|
2023-06-04 18:16:56 +00:00
|
|
|
case 'm':
|
2019-12-10 02:38:17 +00:00
|
|
|
case 'n':
|
|
|
|
case 'Y':
|
|
|
|
case 'xkY':
|
|
|
|
case 'G':
|
|
|
|
case 'H':
|
|
|
|
case 'i':
|
2023-06-04 18:16:56 +00:00
|
|
|
case 's':
|
2019-12-10 02:38:17 +00:00
|
|
|
$matchingGroups[] = $code;
|
|
|
|
break;
|
|
|
|
case '\\':
|
|
|
|
// Backslash escaping
|
|
|
|
if ( $p < $formatLength - 1 ) {
|
|
|
|
$p++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '"':
|
|
|
|
// Quoted literal
|
|
|
|
if ( $p < $formatLength - 1 ) {
|
|
|
|
$endQuote = strpos( $format, '"', $p + 1 );
|
|
|
|
if ( $endQuote !== false ) {
|
|
|
|
$p = $endQuote;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return function ( array $match ) use (
|
2020-09-03 20:59:33 +00:00
|
|
|
$matchingGroups, $untransformDigits, $localTimezone, $tzAbbrs, $contLangVariant
|
2019-12-10 02:38:17 +00:00
|
|
|
) {
|
|
|
|
if ( is_array( $match[0] ) ) {
|
|
|
|
// Strip PREG_OFFSET_CAPTURE data
|
2020-08-11 04:22:57 +00:00
|
|
|
unset( $match['offset'] );
|
2021-05-05 06:59:38 +00:00
|
|
|
$match = array_map( static function ( array $tuple ) {
|
2019-12-10 02:38:17 +00:00
|
|
|
return $tuple[0];
|
|
|
|
}, $match );
|
|
|
|
}
|
|
|
|
$year = 0;
|
|
|
|
$monthIdx = 0;
|
|
|
|
$day = 0;
|
|
|
|
$hour = 0;
|
|
|
|
$minute = 0;
|
|
|
|
foreach ( $matchingGroups as $i => $code ) {
|
|
|
|
$text = $match[$i + 1];
|
|
|
|
switch ( $code ) {
|
|
|
|
case 'xg':
|
2020-09-03 20:59:33 +00:00
|
|
|
$monthIdx = array_search(
|
|
|
|
$text,
|
2023-07-24 17:50:00 +00:00
|
|
|
$this->getMessages( $contLangVariant, Language::MONTH_GENITIVE_MESSAGES ),
|
|
|
|
true
|
2020-09-03 20:59:33 +00:00
|
|
|
);
|
2019-12-10 02:38:17 +00:00
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
case 'j':
|
2023-10-30 08:20:27 +00:00
|
|
|
$day = $untransformDigits( $text );
|
2019-12-10 02:38:17 +00:00
|
|
|
break;
|
|
|
|
case 'D':
|
|
|
|
case 'l':
|
|
|
|
// Day of the week - unused
|
|
|
|
break;
|
|
|
|
case 'F':
|
2020-09-03 20:59:33 +00:00
|
|
|
$monthIdx = array_search(
|
|
|
|
$text,
|
2023-07-24 17:50:00 +00:00
|
|
|
$this->getMessages( $contLangVariant, Language::MONTH_MESSAGES ),
|
|
|
|
true
|
2020-09-03 20:59:33 +00:00
|
|
|
);
|
2019-12-10 02:38:17 +00:00
|
|
|
break;
|
|
|
|
case 'M':
|
2020-09-03 20:59:33 +00:00
|
|
|
$monthIdx = array_search(
|
|
|
|
$text,
|
2023-07-24 17:50:00 +00:00
|
|
|
$this->getMessages( $contLangVariant, Language::MONTH_ABBREVIATED_MESSAGES ),
|
|
|
|
true
|
2020-09-03 20:59:33 +00:00
|
|
|
);
|
2019-12-10 02:38:17 +00:00
|
|
|
break;
|
2023-06-04 18:16:56 +00:00
|
|
|
case 'm':
|
2019-12-10 02:38:17 +00:00
|
|
|
case 'n':
|
2023-10-30 08:20:27 +00:00
|
|
|
$monthIdx = $untransformDigits( $text ) - 1;
|
2019-12-10 02:38:17 +00:00
|
|
|
break;
|
|
|
|
case 'Y':
|
2023-10-30 08:20:27 +00:00
|
|
|
$year = $untransformDigits( $text );
|
2019-12-10 02:38:17 +00:00
|
|
|
break;
|
|
|
|
case 'xkY':
|
|
|
|
// Thai year
|
2023-10-30 08:20:27 +00:00
|
|
|
$year = $untransformDigits( $text ) - 543;
|
2019-12-10 02:38:17 +00:00
|
|
|
break;
|
|
|
|
case 'G':
|
|
|
|
case 'H':
|
2023-10-30 08:20:27 +00:00
|
|
|
$hour = $untransformDigits( $text );
|
2019-12-10 02:38:17 +00:00
|
|
|
break;
|
|
|
|
case 'i':
|
2023-10-30 08:20:27 +00:00
|
|
|
$minute = $untransformDigits( $text );
|
2019-12-10 02:38:17 +00:00
|
|
|
break;
|
2023-06-04 18:16:56 +00:00
|
|
|
case 's':
|
|
|
|
// Seconds - unused, because most timestamp formats omit them
|
|
|
|
break;
|
2019-12-10 02:38:17 +00:00
|
|
|
default:
|
2022-10-28 18:24:02 +00:00
|
|
|
throw new LogicException( 'Not implemented' );
|
2019-12-10 02:38:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The last matching group is the timezone abbreviation
|
|
|
|
$tzAbbr = $tzAbbrs[ end( $match ) ];
|
|
|
|
|
|
|
|
// Most of the time, the timezone abbreviation is not necessary to parse the date, since we
|
|
|
|
// can assume all times are in the wiki's local timezone.
|
|
|
|
$date = new DateTime();
|
|
|
|
// setTimezone must be called before setDate/setTime
|
|
|
|
$date->setTimezone( new DateTimeZone( $localTimezone ) );
|
|
|
|
$date->setDate( $year, $monthIdx + 1, $day );
|
|
|
|
$date->setTime( $hour, $minute, 0 );
|
|
|
|
|
|
|
|
// But during the "fall back" at the end of DST, some times will happen twice.
|
|
|
|
// Since the timezone abbreviation disambiguates the DST/non-DST times, we can detect
|
|
|
|
// when PHP chose the wrong one, and then try the other one. It appears that PHP always
|
|
|
|
// uses the later (non-DST) hour, but that behavior isn't documented, so we account for both.
|
2022-03-03 16:29:39 +00:00
|
|
|
$dateWarning = null;
|
2019-12-10 02:38:17 +00:00
|
|
|
if ( $date->format( 'T' ) !== $tzAbbr ) {
|
|
|
|
$altDate = clone $date;
|
|
|
|
if ( $date->format( 'I' ) ) {
|
|
|
|
// Parsed time is DST, try non-DST by advancing one hour
|
|
|
|
$altDate->add( new DateInterval( 'PT1H' ) );
|
|
|
|
} else {
|
|
|
|
// Parsed time is non-DST, try DST by going back one hour
|
|
|
|
$altDate->sub( new DateInterval( 'PT1H' ) );
|
|
|
|
}
|
|
|
|
if ( $altDate->format( 'T' ) === $tzAbbr ) {
|
|
|
|
$date = $altDate;
|
2022-03-03 16:29:39 +00:00
|
|
|
$dateWarning = 'Timestamp has timezone abbreviation for the wrong time';
|
2020-05-13 18:23:48 +00:00
|
|
|
} else {
|
2022-03-03 16:29:39 +00:00
|
|
|
$dateWarning = 'Ambiguous time at DST switchover was parsed';
|
2019-12-10 02:38:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now set the timezone back to UTC for formatting
|
|
|
|
$date->setTimezone( new DateTimeZone( 'UTC' ) );
|
|
|
|
$date = DateTimeImmutable::createFromMutable( $date );
|
|
|
|
|
2023-11-30 17:37:38 +00:00
|
|
|
// We require the date to be compatible with our libraries, for example zero or negative years (T352455)
|
|
|
|
// In PHP we need to check with MWTimestamp.
|
|
|
|
// In JS we need to check with Moment.
|
|
|
|
try {
|
|
|
|
// @phan-suppress-next-line PhanNoopNew
|
|
|
|
new MWTimestamp( $date->format( 'c' ) );
|
|
|
|
} catch ( TimestampException $ex ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-03-03 16:29:39 +00:00
|
|
|
return [
|
|
|
|
'date' => $date,
|
|
|
|
'warning' => $dateWarning,
|
|
|
|
];
|
2019-12-10 02:38:17 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-09-03 20:59:33 +00:00
|
|
|
* Get a regexp that matches timestamps in the local date format, for each language variant.
|
2019-12-10 02:38:17 +00:00
|
|
|
*
|
|
|
|
* This calls getTimestampRegexp() with predefined data for the current wiki.
|
|
|
|
*
|
2020-09-03 20:59:33 +00:00
|
|
|
* @return string[] Regular expressions
|
2019-12-10 02:38:17 +00:00
|
|
|
*/
|
2021-07-22 07:25:13 +00:00
|
|
|
public function getLocalTimestampRegexps(): array {
|
2022-02-21 17:39:36 +00:00
|
|
|
$langConv = $this->languageConverterFactory->getLanguageConverter( $this->language );
|
2020-09-03 20:59:33 +00:00
|
|
|
return array_map( function ( $contLangVariant ) {
|
|
|
|
return $this->getTimestampRegexp(
|
|
|
|
$contLangVariant,
|
|
|
|
$this->dateFormat[$contLangVariant],
|
|
|
|
'[' . implode( '', $this->digits[$contLangVariant] ) . ']',
|
|
|
|
$this->timezones[$contLangVariant]
|
|
|
|
);
|
2021-02-27 14:46:23 +00:00
|
|
|
}, $langConv->getVariants() );
|
2019-12-10 02:38:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-09-03 20:59:33 +00:00
|
|
|
* Get a function that parses timestamps in the local date format, for each language variant,
|
|
|
|
* based on the result of matching the regexp returned by getLocalTimestampRegexp().
|
2019-12-10 02:38:17 +00:00
|
|
|
*
|
|
|
|
* This calls getTimestampParser() with predefined data for the current wiki.
|
|
|
|
*
|
2020-09-03 20:59:33 +00:00
|
|
|
* @return callable[] Parser functions
|
2019-12-10 02:38:17 +00:00
|
|
|
*/
|
2021-07-22 07:25:13 +00:00
|
|
|
private function getLocalTimestampParsers(): array {
|
2022-02-21 17:39:36 +00:00
|
|
|
$langConv = $this->languageConverterFactory->getLanguageConverter( $this->language );
|
2020-09-03 20:59:33 +00:00
|
|
|
return array_map( function ( $contLangVariant ) {
|
|
|
|
return $this->getTimestampParser(
|
|
|
|
$contLangVariant,
|
|
|
|
$this->dateFormat[$contLangVariant],
|
|
|
|
$this->digits[$contLangVariant],
|
|
|
|
$this->localTimezone,
|
|
|
|
$this->timezones[$contLangVariant]
|
|
|
|
);
|
2021-02-27 14:46:23 +00:00
|
|
|
}, $langConv->getVariants() );
|
2019-12-10 02:38:17 +00:00
|
|
|
}
|
|
|
|
|
2021-01-26 18:58:58 +00:00
|
|
|
/**
|
|
|
|
* Given a link node (`<a>`), if it's a link to a user-related page, return their username.
|
|
|
|
*
|
2021-07-29 02:16:15 +00:00
|
|
|
* @param Element $link
|
2022-02-04 18:16:24 +00:00
|
|
|
* @return array|null Array, or null:
|
|
|
|
* - string 'username' Username
|
|
|
|
* - string|null 'displayName' Display name (link text if link target was in the user namespace)
|
2021-01-26 18:58:58 +00:00
|
|
|
*/
|
2022-02-04 18:16:24 +00:00
|
|
|
private function getUsernameFromLink( Element $link ): ?array {
|
2022-01-11 15:52:16 +00:00
|
|
|
// Selflink: use title of current page
|
2022-01-24 16:16:20 +00:00
|
|
|
if ( DOMCompat::getClassList( $link )->contains( 'mw-selflink' ) ) {
|
2022-01-11 15:52:16 +00:00
|
|
|
$title = $this->title;
|
|
|
|
} else {
|
2022-02-21 22:07:38 +00:00
|
|
|
$titleString = CommentUtils::getTitleFromUrl( $link->getAttribute( 'href' ) ?? '', $this->config ) ?? '';
|
2023-10-30 16:55:42 +00:00
|
|
|
// Performance optimization, skip strings that obviously don't contain a namespace
|
|
|
|
if ( $titleString === '' || !str_contains( $titleString, ':' ) ) {
|
|
|
|
return null;
|
|
|
|
}
|
2023-12-07 23:59:19 +00:00
|
|
|
$title = $this->parseTitle( $titleString );
|
|
|
|
if ( !$title ) {
|
2022-02-21 22:07:38 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-01-26 18:58:58 +00:00
|
|
|
}
|
2022-02-04 15:50:20 +00:00
|
|
|
|
|
|
|
$username = null;
|
2022-02-04 18:16:24 +00:00
|
|
|
$displayName = null;
|
2022-02-04 15:50:20 +00:00
|
|
|
$mainText = $title->getText();
|
|
|
|
|
2023-11-28 08:49:00 +00:00
|
|
|
if ( $title->inNamespace( NS_USER ) || $title->inNamespace( NS_USER_TALK ) ) {
|
2022-02-04 15:50:20 +00:00
|
|
|
$username = $mainText;
|
2023-05-19 07:36:50 +00:00
|
|
|
if ( str_contains( $username, '/' ) ) {
|
2021-01-26 18:58:58 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-11-28 08:49:00 +00:00
|
|
|
if ( $title->inNamespace( NS_USER ) ) {
|
2022-02-04 18:16:24 +00:00
|
|
|
// Use regex trim for consistency with JS implementation
|
|
|
|
$text = preg_replace( [ '/^[\s]+/u', '/[\s]+$/u' ], '', $link->textContent ?? '' );
|
|
|
|
// Record the display name if it has been customised beyond changing case
|
|
|
|
if ( $text && mb_strtolower( $text ) !== mb_strtolower( $username ) ) {
|
|
|
|
$displayName = $text;
|
|
|
|
}
|
|
|
|
}
|
2023-11-28 08:49:00 +00:00
|
|
|
} elseif ( $title->inNamespace( NS_SPECIAL ) ) {
|
2022-02-21 22:07:38 +00:00
|
|
|
$parts = explode( '/', $mainText );
|
|
|
|
if ( count( $parts ) === 2 && $parts[0] === $this->specialContributionsName ) {
|
|
|
|
// Normalize the username: users may link to their contributions with an unnormalized name
|
|
|
|
$userpage = $this->titleParser->makeTitleValueSafe( NS_USER, $parts[1] );
|
|
|
|
if ( !$userpage ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$username = $userpage->getText();
|
2021-01-26 18:58:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( !$username ) {
|
|
|
|
return null;
|
|
|
|
}
|
2021-09-14 20:19:05 +00:00
|
|
|
if ( IPUtils::isIPv6( $username ) ) {
|
2021-01-26 18:58:58 +00:00
|
|
|
// Bot-generated links "Preceding unsigned comment added by" have non-standard case
|
|
|
|
$username = strtoupper( $username );
|
|
|
|
}
|
2022-02-04 18:16:24 +00:00
|
|
|
return [
|
|
|
|
'username' => $username,
|
|
|
|
'displayName' => $displayName,
|
|
|
|
];
|
2021-01-26 18:58:58 +00:00
|
|
|
}
|
|
|
|
|
2019-12-10 02:38:17 +00:00
|
|
|
/**
|
|
|
|
* Find a user signature preceding a timestamp.
|
|
|
|
*
|
|
|
|
* The signature includes the timestamp node.
|
|
|
|
*
|
|
|
|
* A signature must contain at least one link to the user's userpage, discussion page or
|
|
|
|
* contributions (and may contain other links). The link may be nested in other elements.
|
|
|
|
*
|
2021-07-29 02:16:15 +00:00
|
|
|
* @param Text $timestampNode
|
|
|
|
* @param Node|null $until Node to stop searching at
|
2022-02-04 15:44:14 +00:00
|
|
|
* @return array Result, an associative array with the following keys:
|
|
|
|
* - Node[] `nodes` Sibling nodes comprising the signature, in reverse order (with
|
|
|
|
* $timestampNode or its parent node as the first element)
|
|
|
|
* - string|null `username` Username, null for unsigned comments
|
2019-12-10 02:38:17 +00:00
|
|
|
*/
|
2021-07-29 02:16:15 +00:00
|
|
|
private function findSignature( Text $timestampNode, ?Node $until = null ): array {
|
2019-12-10 02:38:17 +00:00
|
|
|
$sigUsername = null;
|
2022-02-04 18:16:24 +00:00
|
|
|
$sigDisplayName = null;
|
2019-12-10 02:38:17 +00:00
|
|
|
$length = 0;
|
|
|
|
$lastLinkNode = $timestampNode;
|
2020-05-27 17:42:19 +00:00
|
|
|
|
2021-01-26 18:58:58 +00:00
|
|
|
CommentUtils::linearWalkBackwards(
|
|
|
|
$timestampNode,
|
2021-07-29 02:16:15 +00:00
|
|
|
function ( string $event, Node $node ) use (
|
2022-02-04 18:16:24 +00:00
|
|
|
&$sigUsername, &$sigDisplayName, &$lastLinkNode, &$length,
|
2021-01-26 18:58:58 +00:00
|
|
|
$until, $timestampNode
|
|
|
|
) {
|
|
|
|
if ( $event === 'enter' && $node === $until ) {
|
|
|
|
return true;
|
2019-12-10 02:38:17 +00:00
|
|
|
}
|
2022-06-09 13:51:33 +00:00
|
|
|
if ( $length >= static::SIGNATURE_SCAN_LIMIT ) {
|
2021-01-26 18:58:58 +00:00
|
|
|
return true;
|
2019-12-10 02:38:17 +00:00
|
|
|
}
|
2021-01-26 18:58:58 +00:00
|
|
|
if ( CommentUtils::isBlockElement( $node ) ) {
|
|
|
|
// Don't allow reaching into preceding paragraphs
|
|
|
|
return true;
|
2019-12-10 02:38:17 +00:00
|
|
|
}
|
|
|
|
|
2021-01-26 18:58:58 +00:00
|
|
|
if ( $event === 'leave' && $node !== $timestampNode ) {
|
2022-02-04 16:25:42 +00:00
|
|
|
$length += $node instanceof Text ?
|
|
|
|
mb_strlen( CommentUtils::htmlTrim( $node->textContent ?? '' ) ) : 0;
|
2019-12-10 02:38:17 +00:00
|
|
|
}
|
2021-01-26 18:58:58 +00:00
|
|
|
|
|
|
|
// Find the closest link before timestamp that links to the user's user page.
|
|
|
|
//
|
|
|
|
// Support timestamps being linked to the diff introducing the comment:
|
|
|
|
// if the timestamp node is the only child of a link node, use the link node instead
|
|
|
|
//
|
|
|
|
// Handle links nested in formatting elements.
|
2022-02-21 18:54:15 +00:00
|
|
|
if ( $event === 'leave' && $node instanceof Element && strtolower( $node->tagName ) === 'a' ) {
|
2024-01-30 10:28:36 +00:00
|
|
|
$classList = DOMCompat::getClassList( $node );
|
|
|
|
// Generated timestamp links sometimes look like username links (e.g. on user talk pages)
|
|
|
|
// so ignore these.
|
|
|
|
if ( !$classList->contains( 'ext-discussiontools-init-timestamplink' ) ) {
|
|
|
|
$user = $this->getUsernameFromLink( $node );
|
|
|
|
if ( $user ) {
|
|
|
|
// Accept the first link to the user namespace, then only accept links to that user
|
|
|
|
if ( $sigUsername === null ) {
|
|
|
|
$sigUsername = $user['username'];
|
|
|
|
}
|
|
|
|
if ( $user['username'] === $sigUsername ) {
|
|
|
|
$lastLinkNode = $node;
|
|
|
|
if ( $user['displayName'] ) {
|
|
|
|
$sigDisplayName = $user['displayName'];
|
|
|
|
}
|
2022-02-04 18:16:24 +00:00
|
|
|
}
|
2021-01-26 18:58:58 +00:00
|
|
|
}
|
2024-01-30 10:28:36 +00:00
|
|
|
// Keep looking if a node with links wasn't a link to a user page
|
|
|
|
// "Doc James (talk · contribs · email)"
|
2021-01-26 18:58:58 +00:00
|
|
|
}
|
2019-12-10 02:38:17 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-26 18:58:58 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$range = new ImmutableRange(
|
|
|
|
$lastLinkNode->parentNode,
|
|
|
|
CommentUtils::childIndexOf( $lastLinkNode ),
|
|
|
|
$timestampNode->parentNode,
|
|
|
|
CommentUtils::childIndexOf( $timestampNode ) + 1
|
|
|
|
);
|
|
|
|
|
|
|
|
// Expand the range so that it covers sibling nodes.
|
|
|
|
// This will include any wrapping formatting elements as part of the signature.
|
|
|
|
//
|
|
|
|
// Helpful accidental feature: users whose signature is not detected in full (due to
|
|
|
|
// text formatting) can just wrap it in a <span> to fix that.
|
|
|
|
// "Ten Pound Hammer • (What did I screw up now?)"
|
|
|
|
// "« Saper // dyskusja »"
|
|
|
|
//
|
|
|
|
// TODO Not sure if this is actually good, might be better to just use the range...
|
|
|
|
$sigNodes = array_reverse( CommentUtils::getCoveredSiblings( $range ) );
|
|
|
|
|
2022-02-04 15:44:14 +00:00
|
|
|
return [
|
|
|
|
'nodes' => $sigNodes,
|
2022-02-04 18:16:24 +00:00
|
|
|
'username' => $sigUsername,
|
|
|
|
'displayName' => $sigDisplayName,
|
2022-02-04 15:44:14 +00:00
|
|
|
];
|
2019-12-10 02:38:17 +00:00
|
|
|
}
|
|
|
|
|
2020-07-15 14:53:11 +00:00
|
|
|
/**
|
|
|
|
* Callback for TreeWalker that will skip over nodes where we don't want to detect
|
|
|
|
* comments (or section headings).
|
|
|
|
*
|
2021-07-29 02:16:15 +00:00
|
|
|
* @param Node $node
|
2020-07-15 14:53:11 +00:00
|
|
|
* @return int Appropriate NodeFilter constant
|
|
|
|
*/
|
2021-12-01 14:53:20 +00:00
|
|
|
public static function acceptOnlyNodesAllowingComments( Node $node ): int {
|
2022-02-21 18:04:31 +00:00
|
|
|
if ( $node instanceof Element ) {
|
|
|
|
$tagName = strtolower( $node->tagName );
|
|
|
|
// The table of contents has a heading that gets erroneously detected as a section
|
|
|
|
if ( $node->getAttribute( 'id' ) === 'toc' ) {
|
|
|
|
return NodeFilter::FILTER_REJECT;
|
|
|
|
}
|
2023-03-27 12:50:45 +00:00
|
|
|
// Don't detect comments within quotes (T275881)
|
|
|
|
if (
|
|
|
|
$tagName === 'blockquote' ||
|
|
|
|
$tagName === 'cite' ||
|
|
|
|
$tagName === 'q'
|
|
|
|
) {
|
|
|
|
return NodeFilter::FILTER_REJECT;
|
|
|
|
}
|
|
|
|
$classList = DOMCompat::getClassList( $node );
|
|
|
|
// Don't attempt to parse blocks marked 'mw-notalk'
|
|
|
|
if ( $classList->contains( 'mw-notalk' ) ) {
|
|
|
|
return NodeFilter::FILTER_REJECT;
|
|
|
|
}
|
2022-02-21 18:04:31 +00:00
|
|
|
// Don't detect comments within references. We can't add replies to them without bungling up
|
|
|
|
// the structure in some cases (T301213), and you're not supposed to do that anyway…
|
|
|
|
if (
|
|
|
|
// <ol class="references"> is the only reliably consistent thing between the two parsers
|
|
|
|
$tagName === 'ol' &&
|
|
|
|
DOMCompat::getClassList( $node )->contains( 'references' )
|
|
|
|
) {
|
|
|
|
return NodeFilter::FILTER_REJECT;
|
|
|
|
}
|
2021-02-26 20:05:40 +00:00
|
|
|
}
|
2022-02-21 18:04:31 +00:00
|
|
|
$parentNode = $node->parentNode;
|
2020-11-04 15:17:39 +00:00
|
|
|
// Don't detect comments within headings (but don't reject the headings themselves)
|
2022-02-21 18:04:31 +00:00
|
|
|
if ( $parentNode instanceof Element && preg_match( '/^h([1-6])$/i', $parentNode->tagName ) ) {
|
2020-11-04 15:17:39 +00:00
|
|
|
return NodeFilter::FILTER_REJECT;
|
|
|
|
}
|
2020-07-15 14:53:11 +00:00
|
|
|
return NodeFilter::FILTER_ACCEPT;
|
|
|
|
}
|
|
|
|
|
2022-10-04 12:50:57 +00:00
|
|
|
/**
|
|
|
|
* Convert a byte offset within a text node to a unicode codepoint offset
|
|
|
|
*
|
|
|
|
* @param Text $node Text node
|
|
|
|
* @param int $byteOffset Byte offset
|
|
|
|
* @return int Codepoint offset
|
|
|
|
*/
|
|
|
|
private static function getCodepointOffset( Text $node, int $byteOffset ): int {
|
|
|
|
return mb_strlen( substr( $node->nodeValue ?? '', 0, $byteOffset ) );
|
|
|
|
}
|
|
|
|
|
2019-12-10 02:38:17 +00:00
|
|
|
/**
|
2020-07-10 14:16:49 +00:00
|
|
|
* Find a timestamps in a given text node
|
2019-12-10 02:38:17 +00:00
|
|
|
*
|
2021-07-29 02:16:15 +00:00
|
|
|
* @param Text $node
|
2020-11-11 08:31:59 +00:00
|
|
|
* @param string[] $timestampRegexps
|
2020-09-03 20:59:33 +00:00
|
|
|
* @return array|null Array with the following keys:
|
2021-08-02 15:16:32 +00:00
|
|
|
* - int 'offset' Length of extra text preceding the node that was used for matching (in bytes)
|
2020-09-03 20:59:33 +00:00
|
|
|
* - int 'parserIndex' Which of the regexps matched
|
|
|
|
* - array 'matchData' Regexp match data, which specifies the location of the match,
|
2021-08-02 15:16:32 +00:00
|
|
|
* and which can be parsed using getLocalTimestampParsers() (offsets are in bytes)
|
2022-10-04 12:50:57 +00:00
|
|
|
* - ImmutableRange 'range' Range covering the timestamp
|
2019-12-10 02:38:17 +00:00
|
|
|
*/
|
2021-07-29 02:16:15 +00:00
|
|
|
public function findTimestamp( Text $node, array $timestampRegexps ): ?array {
|
2020-08-11 03:53:38 +00:00
|
|
|
$nodeText = '';
|
2020-08-11 04:22:57 +00:00
|
|
|
$offset = 0;
|
2022-10-04 12:50:57 +00:00
|
|
|
// Searched nodes (reverse order)
|
|
|
|
$nodes = [];
|
2019-12-10 02:38:17 +00:00
|
|
|
|
2020-08-11 03:53:38 +00:00
|
|
|
while ( $node ) {
|
2020-08-11 04:22:57 +00:00
|
|
|
$nodeText = $node->nodeValue . $nodeText;
|
2022-10-04 12:50:57 +00:00
|
|
|
$nodes[] = $node;
|
2019-12-10 02:38:17 +00:00
|
|
|
|
2020-08-11 03:53:38 +00:00
|
|
|
// In Parsoid HTML, entities are represented as a 'mw:Entity' node, rather than normal HTML
|
|
|
|
// entities. On Arabic Wikipedia, the "UTC" timezone name contains some non-breaking spaces,
|
|
|
|
// which apparently are often turned into entities by buggy editing tools. To handle
|
|
|
|
// this, we must piece together the text, so that our regexp can match those timestamps.
|
|
|
|
if (
|
2020-08-11 04:22:57 +00:00
|
|
|
( $previousSibling = $node->previousSibling ) &&
|
2021-07-29 02:16:15 +00:00
|
|
|
$previousSibling instanceof Element &&
|
2020-08-11 04:22:57 +00:00
|
|
|
$previousSibling->getAttribute( 'typeof' ) === 'mw:Entity'
|
2020-08-11 03:53:38 +00:00
|
|
|
) {
|
2020-08-11 04:22:57 +00:00
|
|
|
$nodeText = $previousSibling->firstChild->nodeValue . $nodeText;
|
2021-07-29 02:16:15 +00:00
|
|
|
$offset += strlen( $previousSibling->firstChild->nodeValue ?? '' );
|
2022-10-04 12:50:57 +00:00
|
|
|
$nodes[] = $previousSibling->firstChild;
|
2019-12-10 02:38:17 +00:00
|
|
|
|
2020-08-11 04:22:57 +00:00
|
|
|
// If the entity is preceded by more text, do this again
|
2020-08-11 03:53:38 +00:00
|
|
|
if (
|
2020-08-11 04:22:57 +00:00
|
|
|
$previousSibling->previousSibling &&
|
2021-07-29 02:16:15 +00:00
|
|
|
$previousSibling->previousSibling instanceof Text
|
2020-08-11 03:53:38 +00:00
|
|
|
) {
|
2021-07-29 02:16:15 +00:00
|
|
|
$offset += strlen( $previousSibling->previousSibling->nodeValue ?? '' );
|
2020-08-11 04:22:57 +00:00
|
|
|
$node = $previousSibling->previousSibling;
|
2019-12-10 02:38:17 +00:00
|
|
|
} else {
|
|
|
|
$node = null;
|
|
|
|
}
|
2020-08-11 03:53:38 +00:00
|
|
|
} else {
|
|
|
|
$node = null;
|
2019-12-10 02:38:17 +00:00
|
|
|
}
|
2020-08-11 03:53:38 +00:00
|
|
|
}
|
2019-12-10 02:38:17 +00:00
|
|
|
|
2020-09-03 20:59:33 +00:00
|
|
|
foreach ( $timestampRegexps as $i => $timestampRegexp ) {
|
|
|
|
$matchData = null;
|
|
|
|
// Allows us to mimic match.index in #getComments
|
|
|
|
if ( preg_match( $timestampRegexp, $nodeText, $matchData, PREG_OFFSET_CAPTURE ) ) {
|
2022-10-04 12:50:57 +00:00
|
|
|
$timestampLength = strlen( $matchData[0][0] );
|
|
|
|
// Bytes at the end of the last node which aren't part of the match
|
|
|
|
$tailLength = strlen( $nodeText ) - $timestampLength - $matchData[0][1];
|
|
|
|
// We are moving right to left, but we start to the right of the end of
|
|
|
|
// the timestamp if there is trailing garbage, so that is a negative offset.
|
|
|
|
$count = -$tailLength;
|
|
|
|
$endNode = $nodes[0];
|
|
|
|
$endOffset = strlen( $endNode->nodeValue ?? '' ) - $tailLength;
|
|
|
|
|
|
|
|
foreach ( $nodes as $n ) {
|
|
|
|
$count += strlen( $n->nodeValue ?? '' );
|
|
|
|
// If we have counted to beyond the start of the timestamp, we are in the
|
|
|
|
// start node of the timestamp
|
|
|
|
if ( $count >= $timestampLength ) {
|
|
|
|
$startNode = $n;
|
|
|
|
// Offset is how much we overshot the start by
|
|
|
|
$startOffset = $count - $timestampLength;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Assert::precondition( $endNode instanceof Node, 'endNode of timestamp is a Node' );
|
|
|
|
Assert::precondition( $startNode instanceof Node, 'startNode of timestamp range found' );
|
|
|
|
Assert::precondition( is_int( $startOffset ), 'startOffset of timestamp range found' );
|
|
|
|
|
|
|
|
$startOffset = static::getCodepointOffset( $startNode, $startOffset );
|
|
|
|
$endOffset = static::getCodepointOffset( $endNode, $endOffset );
|
|
|
|
|
|
|
|
$range = new ImmutableRange( $startNode, $startOffset, $endNode, $endOffset );
|
|
|
|
|
2020-09-03 20:59:33 +00:00
|
|
|
return [
|
|
|
|
'matchData' => $matchData,
|
2022-10-04 12:50:57 +00:00
|
|
|
// Bytes at the start of the first node which aren't part of the match
|
|
|
|
// TODO: Remove this and use 'range' instead
|
2020-09-03 20:59:33 +00:00
|
|
|
'offset' => $offset,
|
2022-10-04 12:50:57 +00:00
|
|
|
'range' => $range,
|
2020-09-03 20:59:33 +00:00
|
|
|
'parserIndex' => $i,
|
|
|
|
];
|
|
|
|
}
|
2020-08-11 03:53:38 +00:00
|
|
|
}
|
2020-07-10 14:16:49 +00:00
|
|
|
return null;
|
2019-12-10 02:38:17 +00:00
|
|
|
}
|
|
|
|
|
2021-02-26 20:57:22 +00:00
|
|
|
/**
|
2021-07-29 02:16:15 +00:00
|
|
|
* @param Node[] $sigNodes
|
2021-02-26 20:57:22 +00:00
|
|
|
* @param array $match
|
2021-07-29 02:16:15 +00:00
|
|
|
* @param Text $node
|
2021-02-26 20:57:22 +00:00
|
|
|
* @return ImmutableRange
|
|
|
|
*/
|
2021-12-01 14:53:20 +00:00
|
|
|
private function adjustSigRange( array $sigNodes, array $match, Text $node ): ImmutableRange {
|
2021-02-26 20:57:22 +00:00
|
|
|
$firstSigNode = end( $sigNodes );
|
|
|
|
$lastSigNode = $sigNodes[0];
|
|
|
|
|
|
|
|
// TODO Document why this needs to be so complicated
|
2021-08-02 15:16:32 +00:00
|
|
|
$lastSigNodeOffsetByteOffset =
|
|
|
|
$match['matchData'][0][1] + strlen( $match['matchData'][0][0] ) - $match['offset'];
|
2021-02-26 20:57:22 +00:00
|
|
|
$lastSigNodeOffset = $lastSigNode === $node ?
|
2022-10-04 12:50:57 +00:00
|
|
|
static::getCodepointOffset( $node, $lastSigNodeOffsetByteOffset ) :
|
2021-02-26 20:57:22 +00:00
|
|
|
CommentUtils::childIndexOf( $lastSigNode ) + 1;
|
|
|
|
$sigRange = new ImmutableRange(
|
|
|
|
$firstSigNode->parentNode,
|
|
|
|
CommentUtils::childIndexOf( $firstSigNode ),
|
|
|
|
$lastSigNode === $node ? $node : $lastSigNode->parentNode,
|
|
|
|
$lastSigNodeOffset
|
|
|
|
);
|
|
|
|
|
|
|
|
return $sigRange;
|
|
|
|
}
|
|
|
|
|
2022-03-18 03:28:06 +00:00
|
|
|
private function buildThreadItems(): ContentThreadItemSet {
|
|
|
|
$result = new ContentThreadItemSet();
|
2022-02-19 06:31:34 +00:00
|
|
|
|
2020-09-03 20:59:33 +00:00
|
|
|
$timestampRegexps = $this->getLocalTimestampRegexps();
|
|
|
|
$dfParsers = $this->getLocalTimestampParsers();
|
2019-12-10 02:38:17 +00:00
|
|
|
|
2024-02-08 15:19:53 +00:00
|
|
|
$curCommentEnd = null;
|
2019-12-10 02:38:17 +00:00
|
|
|
|
2020-07-15 14:53:11 +00:00
|
|
|
$treeWalker = new TreeWalker(
|
2020-07-20 21:15:03 +00:00
|
|
|
$this->rootNode,
|
2020-07-15 14:53:11 +00:00
|
|
|
NodeFilter::SHOW_ELEMENT | NodeFilter::SHOW_TEXT,
|
2022-06-09 13:51:33 +00:00
|
|
|
[ static::class, 'acceptOnlyNodesAllowingComments' ]
|
2020-07-15 14:53:11 +00:00
|
|
|
);
|
|
|
|
while ( $node = $treeWalker->nextNode() ) {
|
2021-07-29 02:16:15 +00:00
|
|
|
if ( $node instanceof Element && preg_match( '/^h([1-6])$/i', $node->tagName, $match ) ) {
|
2024-04-19 22:34:32 +00:00
|
|
|
$headingNode = CommentUtils::getHeadlineNode( $node );
|
2020-10-27 12:18:36 +00:00
|
|
|
$range = new ImmutableRange(
|
2024-04-19 22:34:32 +00:00
|
|
|
$headingNode, 0, $headingNode, $headingNode->childNodes->length
|
2020-10-27 12:18:36 +00:00
|
|
|
);
|
2023-12-07 22:57:21 +00:00
|
|
|
$transcludedFrom = $this->computeTranscludedFrom( $range );
|
|
|
|
$curComment = new ContentHeadingItem( $range, $transcludedFrom, (int)( $match[ 1 ] ) );
|
2020-07-29 23:57:51 +00:00
|
|
|
$curComment->setRootNode( $this->rootNode );
|
2022-02-19 06:31:34 +00:00
|
|
|
$result->addThreadItem( $curComment );
|
2020-11-16 00:35:51 +00:00
|
|
|
$curCommentEnd = $node;
|
2021-07-29 02:16:15 +00:00
|
|
|
} elseif ( $node instanceof Text && ( $match = $this->findTimestamp( $node, $timestampRegexps ) ) ) {
|
2020-05-13 18:23:48 +00:00
|
|
|
$warnings = [];
|
2024-02-08 15:19:53 +00:00
|
|
|
$foundSignature = $this->findSignature( $node, $curCommentEnd );
|
2022-02-04 15:44:14 +00:00
|
|
|
$author = $foundSignature['username'];
|
2019-12-10 02:38:17 +00:00
|
|
|
|
|
|
|
if ( !$author ) {
|
|
|
|
// Ignore timestamps for which we couldn't find a signature. It's probably not a real
|
|
|
|
// comment, but just a false match due to a copypasted timestamp.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-02-26 20:57:22 +00:00
|
|
|
$sigRanges = [];
|
2022-10-04 12:50:57 +00:00
|
|
|
$timestampRanges = [];
|
|
|
|
|
2022-02-04 15:44:14 +00:00
|
|
|
$sigRanges[] = $this->adjustSigRange( $foundSignature['nodes'], $match, $node );
|
2022-10-04 12:50:57 +00:00
|
|
|
$timestampRanges[] = $match['range'];
|
2019-12-10 02:38:17 +00:00
|
|
|
|
2020-08-11 04:22:55 +00:00
|
|
|
// Everything from the last comment up to here is the next comment
|
2020-11-16 00:35:51 +00:00
|
|
|
$startNode = $this->nextInterestingLeafNode( $curCommentEnd );
|
2024-02-08 15:19:53 +00:00
|
|
|
$endNode = $foundSignature['nodes'][0];
|
2021-01-08 19:20:33 +00:00
|
|
|
|
|
|
|
// Skip to the end of the "paragraph". This only looks at tag names and can be fooled by CSS, but
|
|
|
|
// avoiding that would be more difficult and slower.
|
2021-02-26 20:57:22 +00:00
|
|
|
//
|
|
|
|
// If this skips over another potential signature, also skip it in the main TreeWalker loop, to
|
|
|
|
// avoid generating multiple comments when there is more than one signature on a single "line".
|
|
|
|
// Often this is done when someone edits their comment later and wants to add a note about that.
|
|
|
|
// (Or when another person corrects a typo, or strikes out a comment, etc.) Multiple comments
|
|
|
|
// within one paragraph/list-item result in a confusing double "Reply" button, and we also have
|
|
|
|
// no way to indicate which one you're replying to (this might matter in the future for
|
|
|
|
// notifications or something).
|
2021-01-08 19:20:33 +00:00
|
|
|
CommentUtils::linearWalk(
|
2024-02-08 15:19:53 +00:00
|
|
|
$endNode,
|
2021-07-29 02:16:15 +00:00
|
|
|
function ( string $event, Node $n ) use (
|
2022-10-04 12:50:57 +00:00
|
|
|
&$endNode, &$sigRanges, &$timestampRanges,
|
2021-02-26 20:57:22 +00:00
|
|
|
$treeWalker, $timestampRegexps, $node
|
|
|
|
) {
|
2022-11-30 16:02:39 +00:00
|
|
|
if ( CommentUtils::isBlockElement( $n ) || CommentUtils::isCommentSeparator( $n ) ) {
|
2021-01-08 19:20:33 +00:00
|
|
|
// Stop when entering or leaving a block node
|
|
|
|
return true;
|
|
|
|
}
|
2021-02-26 20:57:22 +00:00
|
|
|
if (
|
|
|
|
$event === 'leave' &&
|
2021-07-29 02:16:15 +00:00
|
|
|
$n instanceof Text && $n !== $node &&
|
2021-02-26 20:57:22 +00:00
|
|
|
( $match2 = $this->findTimestamp( $n, $timestampRegexps ) )
|
|
|
|
) {
|
|
|
|
// If this skips over another potential signature, also skip it in the main TreeWalker loop
|
|
|
|
$treeWalker->currentNode = $n;
|
|
|
|
// …and add it as another signature to this comment (regardless of the author and timestamp)
|
|
|
|
$foundSignature2 = $this->findSignature( $n, $node );
|
2022-02-04 15:44:14 +00:00
|
|
|
if ( $foundSignature2['username'] ) {
|
|
|
|
$sigRanges[] = $this->adjustSigRange( $foundSignature2['nodes'], $match2, $n );
|
2022-10-04 12:50:57 +00:00
|
|
|
$timestampRanges[] = $match2['range'];
|
2021-02-26 20:57:22 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-08 19:20:33 +00:00
|
|
|
if ( $event === 'leave' ) {
|
|
|
|
// Take the last complete node which we skipped past
|
2021-02-26 20:57:22 +00:00
|
|
|
$endNode = $n;
|
2021-01-08 19:20:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2020-08-11 04:22:55 +00:00
|
|
|
|
2022-02-21 18:42:36 +00:00
|
|
|
$length = ( $endNode instanceof Text ) ?
|
|
|
|
mb_strlen( rtrim( $endNode->nodeValue ?? '', "\t\n\f\r " ) ) :
|
2020-11-24 20:58:34 +00:00
|
|
|
// PHP bug: childNodes can be null for comment nodes
|
2021-07-29 02:16:15 +00:00
|
|
|
// (it should always be a NodeList, even if the node can't have children)
|
2020-11-24 20:58:34 +00:00
|
|
|
( $endNode->childNodes ? $endNode->childNodes->length : 0 );
|
|
|
|
$range = new ImmutableRange(
|
|
|
|
$startNode->parentNode,
|
|
|
|
CommentUtils::childIndexOf( $startNode ),
|
|
|
|
$endNode,
|
|
|
|
$length
|
|
|
|
);
|
2023-12-07 22:57:21 +00:00
|
|
|
$transcludedFrom = $this->computeTranscludedFrom( $range );
|
2020-08-11 04:22:55 +00:00
|
|
|
|
2020-07-29 23:57:51 +00:00
|
|
|
$startLevel = CommentUtils::getIndentLevel( $startNode, $this->rootNode ) + 1;
|
|
|
|
$endLevel = CommentUtils::getIndentLevel( $node, $this->rootNode ) + 1;
|
2019-12-10 02:38:17 +00:00
|
|
|
if ( $startLevel !== $endLevel ) {
|
2020-05-13 18:23:48 +00:00
|
|
|
$warnings[] = 'Comment starts and ends with different indentation';
|
2019-12-10 02:38:17 +00:00
|
|
|
}
|
2020-08-20 18:38:25 +00:00
|
|
|
// Should this use the indent level of $startNode or $node?
|
|
|
|
$level = min( $startLevel, $endLevel );
|
|
|
|
|
2023-11-30 17:37:38 +00:00
|
|
|
$parserResult = $dfParsers[ $match['parserIndex'] ]( $match['matchData'] );
|
|
|
|
if ( !$parserResult ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
[ 'date' => $dateTime, 'warning' => $dateWarning ] = $parserResult;
|
2022-03-03 16:29:39 +00:00
|
|
|
|
|
|
|
if ( $dateWarning ) {
|
|
|
|
$warnings[] = $dateWarning;
|
2020-08-20 18:38:25 +00:00
|
|
|
}
|
2019-12-10 02:38:17 +00:00
|
|
|
|
2022-03-18 03:28:06 +00:00
|
|
|
$curComment = new ContentCommentItem(
|
2020-08-20 18:38:25 +00:00
|
|
|
$level,
|
2020-05-22 16:26:05 +00:00
|
|
|
$range,
|
2023-12-07 22:57:21 +00:00
|
|
|
$transcludedFrom,
|
2021-02-26 20:57:22 +00:00
|
|
|
$sigRanges,
|
2022-10-04 12:50:57 +00:00
|
|
|
$timestampRanges,
|
2021-11-08 17:47:03 +00:00
|
|
|
$dateTime,
|
2022-02-04 18:16:24 +00:00
|
|
|
$author,
|
|
|
|
$foundSignature['displayName']
|
2020-05-22 16:26:05 +00:00
|
|
|
);
|
2020-07-29 23:57:51 +00:00
|
|
|
$curComment->setRootNode( $this->rootNode );
|
2020-05-13 18:23:48 +00:00
|
|
|
if ( $warnings ) {
|
2020-05-22 16:26:05 +00:00
|
|
|
$curComment->addWarnings( $warnings );
|
2020-05-13 18:23:48 +00:00
|
|
|
}
|
2022-02-19 06:31:34 +00:00
|
|
|
if ( $result->isEmpty() ) {
|
|
|
|
// Add a fake placeholder heading if there are any comments in the 0th section
|
|
|
|
// (before the first real heading)
|
|
|
|
$range = new ImmutableRange( $this->rootNode, 0, $this->rootNode, 0 );
|
2023-12-07 22:57:21 +00:00
|
|
|
$fakeHeading = new ContentHeadingItem( $range, false, null );
|
2022-02-19 06:31:34 +00:00
|
|
|
$fakeHeading->setRootNode( $this->rootNode );
|
|
|
|
$result->addThreadItem( $fakeHeading );
|
|
|
|
}
|
|
|
|
$result->addThreadItem( $curComment );
|
2020-11-16 00:35:51 +00:00
|
|
|
$curCommentEnd = $curComment->getRange()->endContainer;
|
2019-12-10 02:38:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-19 06:31:34 +00:00
|
|
|
return $result;
|
2020-07-20 21:15:03 +00:00
|
|
|
}
|
|
|
|
|
2023-12-07 22:57:21 +00:00
|
|
|
/**
|
|
|
|
* Get the name of the page from which this thread item is transcluded (if any). Replies to
|
|
|
|
* transcluded items must be posted on that page, instead of the current one.
|
|
|
|
*
|
|
|
|
* This is tricky, because we don't want to mark items as trancluded when they're just using a
|
|
|
|
* template (e.g. {{ping|…}} or a non-substituted signature template). Sometimes the whole comment
|
|
|
|
* can be template-generated (e.g. when using some wrapper templates), but as long as a reply can
|
|
|
|
* be added outside of that template, we should not treat it as transcluded.
|
|
|
|
*
|
|
|
|
* The start/end boundary points of comment ranges and Parsoid transclusion ranges don't line up
|
|
|
|
* exactly, even when to a human it's obvious that they cover the same content, making this more
|
|
|
|
* complicated.
|
|
|
|
*
|
|
|
|
* @return string|bool `false` if this item is not transcluded. A string if it's transcluded
|
|
|
|
* from a single page (the page title, in text form with spaces). `true` if it's transcluded, but
|
|
|
|
* we can't determine the source.
|
|
|
|
*/
|
|
|
|
public function computeTranscludedFrom( ImmutableRange $commentRange ) {
|
|
|
|
// Collapsed ranges should otherwise be impossible, but they're not (T299583)
|
|
|
|
// TODO: See if we can fix the root cause, and remove this?
|
|
|
|
if ( $commentRange->collapsed ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// General approach:
|
|
|
|
//
|
|
|
|
// Compare the comment range to each transclusion range on the page, and if it overlaps any of
|
|
|
|
// them, examine the overlap. There are a few cases:
|
|
|
|
//
|
|
|
|
// * Comment and transclusion do not overlap:
|
|
|
|
// → Not transcluded.
|
|
|
|
// * Comment contains the transclusion:
|
|
|
|
// → Not transcluded (just a template).
|
|
|
|
// * Comment is contained within the transclusion:
|
|
|
|
// → Transcluded, we can determine the source page (unless it's a complex transclusion).
|
|
|
|
// * Comment and transclusion overlap partially:
|
|
|
|
// → Transcluded, but we can't determine the source page.
|
|
|
|
// * Comment (almost) exactly matches the transclusion:
|
|
|
|
// → Maybe transcluded (it could be that the source page only contains that single comment),
|
|
|
|
// maybe not transcluded (it could be a wrapper template that covers a single comment).
|
|
|
|
// This is very sad, and we decide based on the namespace.
|
|
|
|
//
|
|
|
|
// Most transclusion ranges on the page trivially fall in the "do not overlap" or "contains"
|
|
|
|
// cases, and we only have to carefully examine the two transclusion ranges that contain the
|
|
|
|
// first and last node of the comment range.
|
|
|
|
//
|
|
|
|
// To check for almost exact matches, we walk between the relevant boundary points, and if we
|
|
|
|
// only find uninteresting nodes (that would be ignored when detecting comments), we treat them
|
|
|
|
// like exact matches.
|
|
|
|
|
|
|
|
$startTransclNode = CommentUtils::getTranscludedFromElement(
|
|
|
|
CommentUtils::getRangeFirstNode( $commentRange )
|
|
|
|
);
|
|
|
|
$endTransclNode = CommentUtils::getTranscludedFromElement(
|
|
|
|
CommentUtils::getRangeLastNode( $commentRange )
|
|
|
|
);
|
|
|
|
|
|
|
|
// We only have to examine the two transclusion ranges that contain the first/last node of the
|
|
|
|
// comment range (if they exist). Ignore ranges outside the comment or in the middle of it.
|
|
|
|
$transclNodes = [];
|
|
|
|
if ( $startTransclNode ) {
|
|
|
|
$transclNodes[] = $startTransclNode;
|
|
|
|
}
|
|
|
|
if ( $endTransclNode && $endTransclNode !== $startTransclNode ) {
|
|
|
|
$transclNodes[] = $endTransclNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ( $transclNodes as $transclNode ) {
|
|
|
|
$transclRange = static::getTransclusionRange( $transclNode );
|
|
|
|
$compared = CommentUtils::compareRanges( $commentRange, $transclRange );
|
|
|
|
$transclTitles = $this->getTransclusionTitles( $transclNode );
|
2023-12-07 23:59:19 +00:00
|
|
|
$simpleTransclTitle = count( $transclTitles ) === 1 && $transclTitles[0] !== null ?
|
|
|
|
$this->parseTitle( $transclTitles[0] ) : null;
|
2023-12-07 22:57:21 +00:00
|
|
|
|
|
|
|
switch ( $compared ) {
|
|
|
|
case 'equal':
|
|
|
|
// Comment (almost) exactly matches the transclusion
|
|
|
|
if ( $simpleTransclTitle === null ) {
|
|
|
|
// Allow replying to some accidental complex transclusions consisting of only templates
|
|
|
|
// and wikitext (T313093)
|
|
|
|
if ( count( $transclTitles ) > 1 ) {
|
2023-12-07 23:59:19 +00:00
|
|
|
foreach ( $transclTitles as $transclTitleString ) {
|
|
|
|
if ( $transclTitleString !== null ) {
|
|
|
|
$transclTitle = $this->parseTitle( $transclTitleString );
|
|
|
|
if ( $transclTitle && !$transclTitle->inNamespace( NS_TEMPLATE ) ) {
|
|
|
|
return true;
|
|
|
|
}
|
2023-12-07 22:57:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Continue examining the other ranges.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Multi-template transclusion, or a parser function call, or template-affected wikitext outside
|
|
|
|
// of a template call, or a mix of the above
|
|
|
|
return true;
|
|
|
|
|
|
|
|
} elseif ( $simpleTransclTitle->inNamespace( NS_TEMPLATE ) ) {
|
|
|
|
// Is that a subpage transclusion with a single comment, or a wrapper template
|
|
|
|
// transclusion on this page? We don't know, but let's guess based on the namespace.
|
|
|
|
// (T289873)
|
|
|
|
// Continue examining the other ranges.
|
|
|
|
break;
|
2023-12-07 23:59:19 +00:00
|
|
|
} elseif ( !$this->titleCanExist( $simpleTransclTitle ) ) {
|
|
|
|
// Special page transclusion (T344622) or something else weird. Don't return the title,
|
2023-12-07 22:57:21 +00:00
|
|
|
// since it's useless for replying, and can't be stored in the permalink database.
|
|
|
|
return true;
|
|
|
|
} else {
|
2023-12-07 23:59:19 +00:00
|
|
|
Assert::precondition( $transclTitles[0] !== null, "Simple transclusion found" );
|
|
|
|
return strtr( $transclTitles[0], '_', ' ' );
|
2023-12-07 22:57:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case 'contains':
|
|
|
|
// Comment contains the transclusion
|
|
|
|
|
|
|
|
// If the entire transclusion is contained within the comment range, that's just a
|
|
|
|
// template. This is the same as a transclusion in the middle of the comment, which we
|
|
|
|
// ignored earlier, it just takes us longer to get here in this case.
|
|
|
|
|
|
|
|
// Continue examining the other ranges.
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'contained':
|
|
|
|
// Comment is contained within the transclusion
|
|
|
|
if ( $simpleTransclTitle === null ) {
|
|
|
|
return true;
|
2023-12-07 23:59:19 +00:00
|
|
|
} elseif ( !$this->titleCanExist( $simpleTransclTitle ) ) {
|
|
|
|
// Special page transclusion (T344622) or something else weird. Don't return the title,
|
2023-12-07 22:57:21 +00:00
|
|
|
// since it's useless for replying, and can't be stored in the permalink database.
|
|
|
|
return true;
|
|
|
|
} else {
|
2023-12-07 23:59:19 +00:00
|
|
|
Assert::precondition( $transclTitles[0] !== null, "Simple transclusion found" );
|
|
|
|
return strtr( $transclTitles[0], '_', ' ' );
|
2023-12-07 22:57:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case 'after':
|
|
|
|
case 'before':
|
|
|
|
// Comment and transclusion do not overlap
|
|
|
|
|
|
|
|
// This should be impossible, because we ignored these ranges earlier.
|
|
|
|
throw new LogicException( 'Unexpected transclusion or comment range' );
|
|
|
|
|
|
|
|
case 'overlapstart':
|
|
|
|
case 'overlapend':
|
|
|
|
// Comment and transclusion overlap partially
|
|
|
|
return true;
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw new LogicException( 'Unexpected return value from compareRanges()' );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we got here, the comment range was not contained by or overlapping any of the transclusion
|
|
|
|
// ranges. Comment is not transcluded.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-12-07 23:59:19 +00:00
|
|
|
private function titleCanExist( TitleValue $title ): bool {
|
|
|
|
return $title->getNamespace() >= NS_MAIN &&
|
|
|
|
!$title->isExternal() &&
|
|
|
|
$title->getText() !== '';
|
|
|
|
}
|
|
|
|
|
|
|
|
private function parseTitle( string $titleString ): ?TitleValue {
|
|
|
|
try {
|
|
|
|
return $this->titleParser->parseTitle( $titleString );
|
|
|
|
} catch ( MalformedTitleException $err ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-07 22:57:21 +00:00
|
|
|
/**
|
|
|
|
* Return the page titles for each part of the transclusion, or nulls for each part that isn't
|
|
|
|
* transcluded from another page.
|
|
|
|
*
|
|
|
|
* If the node represents a single-page transclusion, this will return an array containing a
|
2023-12-07 23:59:19 +00:00
|
|
|
* single string.
|
2023-12-07 22:57:21 +00:00
|
|
|
*
|
|
|
|
* @param Element $node
|
2023-12-07 23:59:19 +00:00
|
|
|
* @return array<string|null>
|
2023-12-07 22:57:21 +00:00
|
|
|
*/
|
|
|
|
private function getTransclusionTitles( Element $node ): array {
|
|
|
|
$dataMw = json_decode( $node->getAttribute( 'data-mw' ) ?? '', true );
|
|
|
|
$out = [];
|
|
|
|
|
|
|
|
foreach ( $dataMw['parts'] ?? [] as $part ) {
|
|
|
|
if (
|
|
|
|
!is_string( $part ) &&
|
|
|
|
// 'href' will be unset if this is a parser function rather than a template
|
|
|
|
isset( $part['template']['target']['href'] )
|
|
|
|
) {
|
|
|
|
$parsoidHref = $part['template']['target']['href'];
|
|
|
|
Assert::precondition( substr( $parsoidHref, 0, 2 ) === './', "href has valid format" );
|
2023-12-07 23:59:19 +00:00
|
|
|
$out[] = urldecode( substr( $parsoidHref, 2 ) );
|
2023-12-07 22:57:21 +00:00
|
|
|
} else {
|
|
|
|
$out[] = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a transclusion's first node (e.g. returned by CommentUtils::getTranscludedFromElement()),
|
|
|
|
* return a range starting before the node and ending after the transclusion's last node.
|
|
|
|
*
|
|
|
|
* @param Element $startNode
|
|
|
|
* @return ImmutableRange
|
|
|
|
*/
|
|
|
|
private function getTransclusionRange( Element $startNode ): ImmutableRange {
|
|
|
|
$endNode = $startNode;
|
|
|
|
while (
|
|
|
|
// Phan doesn't realize that the conditions on $nextSibling can terminate the loop
|
|
|
|
// @phan-suppress-next-line PhanInfiniteLoop
|
|
|
|
$endNode &&
|
|
|
|
( $nextSibling = $endNode->nextSibling ) &&
|
|
|
|
$nextSibling instanceof Element &&
|
|
|
|
$nextSibling->getAttribute( 'about' ) === $endNode->getAttribute( 'about' )
|
|
|
|
) {
|
|
|
|
$endNode = $nextSibling;
|
|
|
|
}
|
|
|
|
|
|
|
|
$range = new ImmutableRange(
|
|
|
|
$startNode->parentNode,
|
|
|
|
CommentUtils::childIndexOf( $startNode ),
|
|
|
|
$endNode->parentNode,
|
|
|
|
CommentUtils::childIndexOf( $endNode ) + 1
|
|
|
|
);
|
|
|
|
|
|
|
|
return $range;
|
|
|
|
}
|
|
|
|
|
2021-02-02 17:40:18 +00:00
|
|
|
/**
|
|
|
|
* Truncate user generated parts of IDs so full ID always fits within a database field of length 255
|
|
|
|
*
|
2024-02-02 16:06:30 +00:00
|
|
|
* nb: Text should already have had spaces replaced with underscores by this point.
|
|
|
|
*
|
2021-02-02 17:40:18 +00:00
|
|
|
* @param string $text Text
|
2024-02-02 16:06:30 +00:00
|
|
|
* @param bool $legacy Generate legacy ID, not needed in JS implementation
|
2021-02-02 17:40:18 +00:00
|
|
|
* @return string Truncated text
|
|
|
|
*/
|
2024-02-02 16:06:30 +00:00
|
|
|
private function truncateForId( string $text, bool $legacy = false ): string {
|
|
|
|
$truncated = $this->language->truncateForDatabase( $text, 80, '' );
|
|
|
|
if ( !$legacy ) {
|
|
|
|
$truncated = trim( $truncated, '_' );
|
|
|
|
}
|
|
|
|
return $truncated;
|
2021-02-02 17:40:18 +00:00
|
|
|
}
|
|
|
|
|
2020-10-21 15:52:04 +00:00
|
|
|
/**
|
|
|
|
* Given a thread item, return an identifier for it that is unique within the page.
|
2024-02-02 16:06:30 +00:00
|
|
|
*
|
|
|
|
* @param ContentThreadItem $threadItem
|
|
|
|
* @param ContentThreadItemSet $previousItems
|
|
|
|
* @param bool $legacy Generate legacy ID, not needed in JS implementation
|
|
|
|
* @return string
|
2020-10-21 15:52:04 +00:00
|
|
|
*/
|
2024-02-02 16:06:30 +00:00
|
|
|
private function computeId(
|
|
|
|
ContentThreadItem $threadItem, ContentThreadItemSet $previousItems, bool $legacy = false
|
|
|
|
): string {
|
2020-09-22 23:05:25 +00:00
|
|
|
$id = null;
|
|
|
|
|
2022-03-18 03:28:06 +00:00
|
|
|
if ( $threadItem instanceof ContentHeadingItem && $threadItem->isPlaceholderHeading() ) {
|
2020-09-22 23:05:25 +00:00
|
|
|
// The range points to the root note, using it like below results in silly values
|
2021-03-17 14:39:41 +00:00
|
|
|
$id = 'h-';
|
2022-03-18 03:28:06 +00:00
|
|
|
} elseif ( $threadItem instanceof ContentHeadingItem ) {
|
2024-02-02 16:06:30 +00:00
|
|
|
$id = 'h-' . $this->truncateForId( $threadItem->getLinkableId(), $legacy );
|
2022-03-18 03:28:06 +00:00
|
|
|
} elseif ( $threadItem instanceof ContentCommentItem ) {
|
2024-02-02 16:06:30 +00:00
|
|
|
$id = 'c-' . $this->truncateForId( str_replace( ' ', '_', $threadItem->getAuthor() ), $legacy ) .
|
2021-11-08 17:47:03 +00:00
|
|
|
'-' . $threadItem->getTimestampString();
|
2020-10-01 20:13:47 +00:00
|
|
|
} else {
|
2022-10-28 18:24:02 +00:00
|
|
|
throw new InvalidArgumentException( 'Unknown ThreadItem type' );
|
2020-10-01 20:13:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If there would be multiple comments with the same ID (i.e. the user left multiple comments
|
|
|
|
// in one edit, or within a minute), add the parent ID to disambiguate them.
|
|
|
|
$threadItemParent = $threadItem->getParent();
|
2022-03-18 03:28:06 +00:00
|
|
|
if ( $threadItemParent instanceof ContentHeadingItem && !$threadItemParent->isPlaceholderHeading() ) {
|
2024-02-02 16:06:30 +00:00
|
|
|
$id .= '-' . $this->truncateForId( $threadItemParent->getLinkableId(), $legacy );
|
2022-03-18 03:28:06 +00:00
|
|
|
} elseif ( $threadItemParent instanceof ContentCommentItem ) {
|
2024-02-02 16:06:30 +00:00
|
|
|
$id .= '-' . $this->truncateForId( str_replace( ' ', '_', $threadItemParent->getAuthor() ), $legacy ) .
|
2021-11-08 17:47:03 +00:00
|
|
|
'-' . $threadItemParent->getTimestampString();
|
2020-10-01 20:13:47 +00:00
|
|
|
}
|
2020-10-21 15:52:04 +00:00
|
|
|
|
2022-03-18 03:28:06 +00:00
|
|
|
if ( $threadItem instanceof ContentHeadingItem ) {
|
2020-10-07 15:48:20 +00:00
|
|
|
// To avoid old threads re-appearing on popular pages when someone uses a vague title
|
|
|
|
// (e.g. dozens of threads titled "question" on [[Wikipedia:Help desk]]: https://w.wiki/fbN),
|
|
|
|
// include the oldest timestamp in the thread (i.e. date the thread was started) in the
|
|
|
|
// heading ID.
|
2022-07-05 23:21:34 +00:00
|
|
|
$oldestComment = $threadItem->getOldestReply();
|
2021-02-22 20:31:15 +00:00
|
|
|
if ( $oldestComment ) {
|
2021-11-08 17:47:03 +00:00
|
|
|
$id .= '-' . $oldestComment->getTimestampString();
|
2020-10-07 15:48:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-19 06:31:34 +00:00
|
|
|
if ( $previousItems->findCommentById( $id ) ) {
|
2020-10-01 20:13:47 +00:00
|
|
|
// Well, that's tough
|
2024-02-02 16:06:30 +00:00
|
|
|
if ( !$legacy ) {
|
|
|
|
$threadItem->addWarning( 'Duplicate comment ID' );
|
|
|
|
}
|
2020-10-01 20:13:47 +00:00
|
|
|
// Finally, disambiguate by adding sequential numbers, to allow replying to both comments
|
|
|
|
$number = 1;
|
2022-02-19 06:31:34 +00:00
|
|
|
while ( $previousItems->findCommentById( "$id-$number" ) ) {
|
2020-10-21 15:52:04 +00:00
|
|
|
$number++;
|
|
|
|
}
|
2021-03-17 14:39:41 +00:00
|
|
|
$id = "$id-$number";
|
2020-10-21 15:52:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $id;
|
|
|
|
}
|
|
|
|
|
2021-02-12 19:16:13 +00:00
|
|
|
/**
|
|
|
|
* Given a thread item, return an identifier for it that is consistent across all pages and
|
|
|
|
* revisions where this comment might appear.
|
|
|
|
*
|
|
|
|
* Multiple comments on a page can have the same name; use ID to distinguish them.
|
|
|
|
*/
|
2022-03-18 03:28:06 +00:00
|
|
|
private function computeName( ContentThreadItem $threadItem ): string {
|
2021-02-12 19:16:13 +00:00
|
|
|
$name = null;
|
|
|
|
|
2022-03-18 03:28:06 +00:00
|
|
|
if ( $threadItem instanceof ContentHeadingItem ) {
|
2021-02-12 19:16:13 +00:00
|
|
|
$name = 'h-';
|
2022-07-05 23:21:34 +00:00
|
|
|
$mainComment = $threadItem->getOldestReply();
|
2022-03-18 03:28:06 +00:00
|
|
|
} elseif ( $threadItem instanceof ContentCommentItem ) {
|
2021-02-12 19:16:13 +00:00
|
|
|
$name = 'c-';
|
|
|
|
$mainComment = $threadItem;
|
|
|
|
} else {
|
2022-10-28 18:24:02 +00:00
|
|
|
throw new InvalidArgumentException( 'Unknown ThreadItem type' );
|
2021-02-12 19:16:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( $mainComment ) {
|
|
|
|
$name .= $this->truncateForId( str_replace( ' ', '_', $mainComment->getAuthor() ) ) .
|
2021-11-08 17:47:03 +00:00
|
|
|
'-' . $mainComment->getTimestampString();
|
2021-02-12 19:16:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $name;
|
|
|
|
}
|
|
|
|
|
2022-03-18 03:28:06 +00:00
|
|
|
private function buildThreads( ContentThreadItemSet $result ): void {
|
2022-02-19 06:31:34 +00:00
|
|
|
$lastHeading = null;
|
2019-12-10 02:38:17 +00:00
|
|
|
$replies = [];
|
|
|
|
|
2022-02-19 06:31:34 +00:00
|
|
|
foreach ( $result->getThreadItems() as $threadItem ) {
|
2020-07-20 21:15:03 +00:00
|
|
|
if ( count( $replies ) < $threadItem->getLevel() ) {
|
2019-12-10 02:38:17 +00:00
|
|
|
// Someone skipped an indentation level (or several). Pretend that the previous reply
|
|
|
|
// covers multiple indentation levels, so that following comments get connected to it.
|
2020-07-20 21:15:03 +00:00
|
|
|
$threadItem->addWarning( 'Comment skips indentation level' );
|
|
|
|
while ( count( $replies ) < $threadItem->getLevel() ) {
|
2019-12-10 02:38:17 +00:00
|
|
|
$replies[] = end( $replies );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-18 03:28:06 +00:00
|
|
|
if ( $threadItem instanceof ContentHeadingItem ) {
|
2019-12-10 02:38:17 +00:00
|
|
|
// New root (thread)
|
2020-10-01 19:36:11 +00:00
|
|
|
// Attach as a sub-thread to preceding higher-level heading.
|
|
|
|
// Any replies will appear in the tree twice, under the main-thread and the sub-thread.
|
2022-02-19 06:31:34 +00:00
|
|
|
$maybeParent = $lastHeading;
|
2020-10-01 19:36:11 +00:00
|
|
|
while ( $maybeParent && $maybeParent->getHeadingLevel() >= $threadItem->getHeadingLevel() ) {
|
|
|
|
$maybeParent = $maybeParent->getParent();
|
|
|
|
}
|
|
|
|
if ( $maybeParent ) {
|
|
|
|
$threadItem->setParent( $maybeParent );
|
|
|
|
$maybeParent->addReply( $threadItem );
|
|
|
|
}
|
2022-02-19 06:31:34 +00:00
|
|
|
$lastHeading = $threadItem;
|
2020-07-20 21:15:03 +00:00
|
|
|
} elseif ( isset( $replies[ $threadItem->getLevel() - 1 ] ) ) {
|
2019-12-10 02:38:17 +00:00
|
|
|
// Add as a reply to the closest less-nested comment
|
2020-07-20 21:15:03 +00:00
|
|
|
$threadItem->setParent( $replies[ $threadItem->getLevel() - 1 ] );
|
|
|
|
$threadItem->getParent()->addReply( $threadItem );
|
2019-12-10 02:38:17 +00:00
|
|
|
} else {
|
2020-07-20 21:15:03 +00:00
|
|
|
$threadItem->addWarning( 'Comment could not be connected to a thread' );
|
2019-12-10 02:38:17 +00:00
|
|
|
}
|
|
|
|
|
2020-07-20 21:15:03 +00:00
|
|
|
$replies[ $threadItem->getLevel() ] = $threadItem;
|
2019-12-10 02:38:17 +00:00
|
|
|
// Cut off more deeply nested replies
|
2020-07-20 21:15:03 +00:00
|
|
|
array_splice( $replies, $threadItem->getLevel() + 1 );
|
2020-10-07 15:48:20 +00:00
|
|
|
}
|
2022-02-19 06:31:34 +00:00
|
|
|
}
|
2020-10-01 20:13:47 +00:00
|
|
|
|
2022-02-19 06:31:34 +00:00
|
|
|
/**
|
|
|
|
* Set the IDs and names used to refer to comments and headings.
|
|
|
|
* This has to be a separate pass because we don't have the list of replies before
|
|
|
|
* this point.
|
|
|
|
*/
|
2022-03-18 03:28:06 +00:00
|
|
|
private function computeIdsAndNames( ContentThreadItemSet $result ): void {
|
2022-02-19 06:31:34 +00:00
|
|
|
foreach ( $result->getThreadItems() as $threadItem ) {
|
2021-02-12 19:16:13 +00:00
|
|
|
$name = $this->computeName( $threadItem );
|
|
|
|
$threadItem->setName( $name );
|
|
|
|
|
2022-02-19 06:31:34 +00:00
|
|
|
$id = $this->computeId( $threadItem, $result );
|
2020-10-01 20:13:47 +00:00
|
|
|
$threadItem->setId( $id );
|
2024-02-02 16:06:30 +00:00
|
|
|
$legacyId = $this->computeId( $threadItem, $result, true );
|
|
|
|
if ( $legacyId !== $id ) {
|
|
|
|
$threadItem->setLegacyId( $legacyId );
|
|
|
|
}
|
2022-02-19 06:31:34 +00:00
|
|
|
|
|
|
|
$result->updateIdAndNameMaps( $threadItem );
|
2019-12-10 02:38:17 +00:00
|
|
|
}
|
2020-10-07 15:48:20 +00:00
|
|
|
}
|
2019-12-10 02:38:17 +00:00
|
|
|
}
|