2019-12-10 02:38:17 +00:00
|
|
|
<?php
|
|
|
|
|
2020-05-14 22:44:49 +00:00
|
|
|
namespace MediaWiki\Extension\DiscussionTools;
|
|
|
|
|
|
|
|
use Config;
|
|
|
|
use DateInterval;
|
|
|
|
use DateTime;
|
|
|
|
use DateTimeImmutable;
|
|
|
|
use DateTimeZone;
|
|
|
|
use DOMElement;
|
|
|
|
use DOMNode;
|
|
|
|
use DOMText;
|
|
|
|
use DOMXPath;
|
|
|
|
use IP;
|
|
|
|
use Language;
|
2019-12-10 02:38:17 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2020-05-15 21:00:30 +00:00
|
|
|
use MWException;
|
2020-05-14 22:44:49 +00:00
|
|
|
use Title;
|
2019-12-10 02:38:17 +00:00
|
|
|
|
|
|
|
// TODO clean up static vs non-static
|
|
|
|
|
|
|
|
// TODO consider rewriting as single traversal, without XPath
|
|
|
|
// TODO consider making timestamp parsing not a returned function
|
|
|
|
|
2020-05-14 22:44:49 +00:00
|
|
|
class CommentParser {
|
2019-12-10 02:38:17 +00:00
|
|
|
private const SIGNATURE_SCAN_LIMIT = 100;
|
|
|
|
|
2020-06-10 20:20:05 +00:00
|
|
|
/** @var Config */
|
|
|
|
private $config;
|
|
|
|
|
|
|
|
private $dateFormat;
|
|
|
|
private $digits;
|
|
|
|
/** @var string[] */
|
|
|
|
private $contLangMessages;
|
|
|
|
private $localTimezone;
|
|
|
|
private $timezones;
|
|
|
|
|
2019-12-10 02:38:17 +00:00
|
|
|
/**
|
|
|
|
* @param Language $language Content language
|
|
|
|
* @param Config $config
|
|
|
|
* @param array $data
|
|
|
|
*/
|
|
|
|
public function __construct( Language $language, Config $config, array $data = [] ) {
|
|
|
|
$this->config = $config;
|
2020-05-19 18:40:05 +00:00
|
|
|
|
|
|
|
if ( !$data ) {
|
|
|
|
// TODO: Instead of passing data used for mocking, mock the methods that fetch the data.
|
|
|
|
$data = Data::getLocalData( null, $config, $language );
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->dateFormat = $data['dateFormat'];
|
|
|
|
$this->digits = $data['digits'];
|
|
|
|
$this->contLangMessages = $data['contLangMessages'];
|
|
|
|
$this->localTimezone = $data['localTimezone'];
|
|
|
|
$this->timezones = $data['timezones'];
|
2019-12-10 02:38:17 +00:00
|
|
|
}
|
|
|
|
|
2020-05-14 22:44:49 +00:00
|
|
|
public static function newFromGlobalState() : CommentParser {
|
2019-12-10 02:38:17 +00:00
|
|
|
return new static(
|
|
|
|
MediaWikiServices::getInstance()->getContentLanguage(),
|
|
|
|
MediaWikiServices::getInstance()->getMainConfig()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a MediaWiki page title from a URL
|
|
|
|
* @param string $url
|
|
|
|
* @return Title|null
|
|
|
|
*/
|
|
|
|
private function getTitleFromUrl( string $url ) : ?Title {
|
|
|
|
// TODO: Set the correct base in the document?
|
|
|
|
if ( strpos( $url, './' ) === 0 ) {
|
2020-07-01 16:04:02 +00:00
|
|
|
$url = 'https://local' . str_replace( '$1', substr( $url, 2 ), $this->config->get( 'ArticlePath' ) );
|
2019-12-10 02:38:17 +00:00
|
|
|
} elseif ( strpos( $url, '://' ) === false ) {
|
|
|
|
$url = 'https://local' . $url;
|
|
|
|
}
|
|
|
|
$bits = wfParseUrl( $url );
|
|
|
|
$query = wfCgiToArray( $bits['query'] ?? '' );
|
|
|
|
if ( isset( $query['title'] ) ) {
|
|
|
|
return Title::newFromText( $query['title'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
$articlePathRegexp = '/' . str_replace(
|
|
|
|
preg_quote( '$1', '/' ),
|
|
|
|
'(.*)',
|
|
|
|
preg_quote( $this->config->get( 'ArticlePath' ), '/' )
|
|
|
|
) . '/';
|
|
|
|
$matches = null;
|
|
|
|
if ( preg_match( $articlePathRegexp, $url, $matches ) ) {
|
|
|
|
return Title::newFromText( urldecode( $matches[1] ) );
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the next leaf node in the tree order that is not an empty or whitespace-only text node.
|
|
|
|
*
|
|
|
|
* In other words, this returns a text node with content other than whitespace, or an element node
|
|
|
|
* with no children, that follows the given node.
|
|
|
|
*
|
|
|
|
* @param DOMNode $node Node to start searching at. This node's children are ignored.
|
2020-05-11 19:56:23 +00:00
|
|
|
* @param DOMElement $rootNode Node to stop searching at
|
2020-06-04 18:48:58 +00:00
|
|
|
* @return DOMNode
|
2019-12-10 02:38:17 +00:00
|
|
|
*/
|
2020-06-04 18:48:58 +00:00
|
|
|
private function nextInterestingLeafNode( DOMNode $node, DOMElement $rootNode ) : DOMNode {
|
2020-07-15 14:53:11 +00:00
|
|
|
$treeWalker = new TreeWalker(
|
|
|
|
$rootNode,
|
|
|
|
NodeFilter::SHOW_ELEMENT | NodeFilter::SHOW_TEXT,
|
|
|
|
function ( $n ) use ( $node, $rootNode ) {
|
|
|
|
// Ignore this node and its descendants
|
|
|
|
// (unless it's the root node, this is a special case for "fakeHeading" handling)
|
|
|
|
if ( $node !== $rootNode && ( $n === $node || $n->parentNode === $node ) ) {
|
|
|
|
return NodeFilter::FILTER_REJECT;
|
2019-12-10 02:38:17 +00:00
|
|
|
}
|
2020-07-15 14:53:11 +00:00
|
|
|
if (
|
2020-05-11 20:07:14 +00:00
|
|
|
(
|
|
|
|
$n->nodeType === XML_TEXT_NODE &&
|
2020-05-14 22:44:49 +00:00
|
|
|
CommentUtils::htmlTrim( $n->nodeValue ) !== ''
|
2020-05-11 20:07:14 +00:00
|
|
|
) ||
|
|
|
|
(
|
|
|
|
$n->nodeType === XML_CDATA_SECTION_NODE &&
|
2020-05-14 22:44:49 +00:00
|
|
|
CommentUtils::htmlTrim( $n->nodeValue ) !== ''
|
2020-05-11 20:07:14 +00:00
|
|
|
) ||
|
2019-12-10 02:38:17 +00:00
|
|
|
( $n->nodeType === XML_ELEMENT_NODE && !$n->firstChild )
|
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
|
|
|
);
|
|
|
|
$treeWalker->currentNode = $node;
|
|
|
|
$treeWalker->nextNode();
|
|
|
|
if ( !$treeWalker->currentNode ) {
|
|
|
|
throw new MWException( 'nextInterestingLeafNode not found' );
|
|
|
|
}
|
|
|
|
return $treeWalker->currentNode;
|
2019-12-10 02:38:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string[] $values Values to match
|
|
|
|
* @return string Regular expression
|
|
|
|
*/
|
|
|
|
private static function regexpAlternateGroup( array $values ) : string {
|
2020-05-16 15:46:25 +00:00
|
|
|
return '(' . implode( '|', array_map( function ( string $x ) {
|
2019-12-10 02:38:17 +00:00
|
|
|
return preg_quote( $x, '/' );
|
|
|
|
}, $values ) ) . ')';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string[] $messageKeys Message keys
|
|
|
|
* @return string[] Message values
|
|
|
|
*/
|
|
|
|
private function getMessages( array $messageKeys ) : array {
|
2020-05-16 15:46:25 +00:00
|
|
|
return array_map( function ( string $key ) {
|
2020-05-19 18:40:05 +00:00
|
|
|
return $this->contLangMessages[$key];
|
2019-12-10 02:38:17 +00:00
|
|
|
}, $messageKeys );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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).
|
|
|
|
*
|
|
|
|
* @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(
|
|
|
|
string $format, string $digitsRegexp, array $tzAbbrs
|
|
|
|
) : string {
|
|
|
|
$formatLength = strlen( $format );
|
|
|
|
$s = '';
|
|
|
|
// 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 ) {
|
|
|
|
case 'xx' :
|
|
|
|
$s .= 'x';
|
|
|
|
break;
|
|
|
|
case 'xg':
|
|
|
|
$s .= self::regexpAlternateGroup(
|
|
|
|
$this->getMessages( Language::MONTH_GENITIVE_MESSAGES )
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
$num = '2';
|
|
|
|
break;
|
|
|
|
case 'D':
|
|
|
|
$s .= self::regexpAlternateGroup(
|
|
|
|
$this->getMessages( Language::WEEKDAY_ABBREVIATED_MESSAGES )
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
case 'j':
|
|
|
|
$num = '1,2';
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
$s .= self::regexpAlternateGroup(
|
|
|
|
$this->getMessages( Language::WEEKDAY_MESSAGES )
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
case 'F':
|
|
|
|
$s .= self::regexpAlternateGroup(
|
|
|
|
$this->getMessages( Language::MONTH_MESSAGES )
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
case 'M':
|
|
|
|
$s .= self::regexpAlternateGroup(
|
|
|
|
$this->getMessages( Language::MONTH_ABBREVIATED_MESSAGES )
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
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;
|
|
|
|
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:
|
|
|
|
$s .= preg_quote( $format[$p], '/' );
|
|
|
|
}
|
|
|
|
if ( $num !== false ) {
|
|
|
|
$s .= '(' . $digitsRegexp . '{' . $num . '})';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$tzRegexp = self::regexpAlternateGroup( array_keys( $tzAbbrs ) );
|
|
|
|
|
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
|
|
|
|
return '/' . $s . '[\\x{200E}\\x{200F}]? [\\x{200E}\\x{200F}]?\\(' . $tzRegexp . '\\)/u';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a function that parses timestamps generated using the given date format, based on the result
|
|
|
|
* of matching the regexp returned by getTimestampRegexp()
|
|
|
|
*
|
|
|
|
* @param string $format Date format, as used by MediaWiki
|
|
|
|
* @param string|null $digits Localised digits from 0 to 9, e.g. `0123456789`
|
|
|
|
* @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(
|
|
|
|
string $format, ?string $digits, string $localTimezone, array $tzAbbrs
|
|
|
|
) : callable {
|
|
|
|
$untransformDigits = function ( string $text ) use ( $digits ) {
|
|
|
|
if ( !$digits ) {
|
|
|
|
return $text;
|
|
|
|
}
|
|
|
|
return preg_replace_callback(
|
|
|
|
'/[' . $digits . ']/',
|
2020-05-16 15:46:25 +00:00
|
|
|
function ( array $m ) use ( $digits ) {
|
2020-06-10 20:20:05 +00:00
|
|
|
return (string)strpos( $digits, $m[0] );
|
2019-12-10 02:38:17 +00:00
|
|
|
},
|
|
|
|
$text
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
$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':
|
|
|
|
break;
|
|
|
|
case 'xg':
|
|
|
|
case 'd':
|
|
|
|
case 'j':
|
|
|
|
case 'D':
|
|
|
|
case 'l':
|
|
|
|
case 'F':
|
|
|
|
case 'M':
|
|
|
|
case 'n':
|
|
|
|
case 'Y':
|
|
|
|
case 'xkY':
|
|
|
|
case 'G':
|
|
|
|
case 'H':
|
|
|
|
case 'i':
|
|
|
|
$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 (
|
|
|
|
$matchingGroups, $untransformDigits, $localTimezone, $tzAbbrs
|
|
|
|
) {
|
|
|
|
if ( is_array( $match[0] ) ) {
|
|
|
|
// Strip PREG_OFFSET_CAPTURE data
|
2020-05-16 15:46:25 +00:00
|
|
|
$match = array_map( 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':
|
|
|
|
$monthIdx = array_search( $text, $this->getMessages( Language::MONTH_GENITIVE_MESSAGES ) );
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
case 'j':
|
|
|
|
$day = intval( $untransformDigits( $text ) );
|
|
|
|
break;
|
|
|
|
case 'D':
|
|
|
|
case 'l':
|
|
|
|
// Day of the week - unused
|
|
|
|
break;
|
|
|
|
case 'F':
|
|
|
|
$monthIdx = array_search( $text, $this->getMessages( Language::MONTH_MESSAGES ) );
|
|
|
|
break;
|
|
|
|
case 'M':
|
|
|
|
$monthIdx = array_search( $text, $this->getMessages( Language::MONTH_ABBREVIATED_MESSAGES ) );
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
$monthIdx = intval( $untransformDigits( $text ) ) - 1;
|
|
|
|
break;
|
|
|
|
case 'Y':
|
|
|
|
$year = intval( $untransformDigits( $text ) );
|
|
|
|
break;
|
|
|
|
case 'xkY':
|
|
|
|
// Thai year
|
|
|
|
$year = intval( $untransformDigits( $text ) ) - 543;
|
|
|
|
break;
|
|
|
|
case 'G':
|
|
|
|
case 'H':
|
|
|
|
$hour = intval( $untransformDigits( $text ) );
|
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
$minute = intval( $untransformDigits( $text ) );
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// TODO throw NotImplementedException or whatever it's called
|
|
|
|
throw new MWException( 'Not implemented' );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
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;
|
2020-05-13 18:23:48 +00:00
|
|
|
$discussionToolsWarning = 'Timestamp has timezone abbreviation for the wrong time';
|
|
|
|
} else {
|
|
|
|
$discussionToolsWarning = '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 );
|
2020-05-13 18:23:48 +00:00
|
|
|
if ( isset( $discussionToolsWarning ) ) {
|
2020-06-10 20:20:05 +00:00
|
|
|
// @phan-suppress-next-line PhanUndeclaredProperty
|
2020-05-13 18:23:48 +00:00
|
|
|
$date->discussionToolsWarning = $discussionToolsWarning;
|
|
|
|
}
|
2019-12-10 02:38:17 +00:00
|
|
|
|
|
|
|
return $date;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a regular expression that matches timestamps in the local date format.
|
|
|
|
*
|
|
|
|
* This calls getTimestampRegexp() with predefined data for the current wiki.
|
|
|
|
*
|
|
|
|
* @return string Regular expression
|
|
|
|
*/
|
|
|
|
public function getLocalTimestampRegexp() : string {
|
2020-05-15 21:02:57 +00:00
|
|
|
return $this->getTimestampRegexp(
|
2019-12-10 02:38:17 +00:00
|
|
|
$this->dateFormat,
|
2020-05-19 18:40:05 +00:00
|
|
|
$this->digits ? "[$this->digits]" : '\\d',
|
|
|
|
$this->timezones
|
2019-12-10 02:38:17 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a function that parses timestamps in the local date format, based on the result
|
|
|
|
* of matching the regexp returned by getLocalTimestampRegexp().
|
|
|
|
*
|
|
|
|
* This calls getTimestampParser() with predefined data for the current wiki.
|
|
|
|
*
|
2020-05-15 20:59:36 +00:00
|
|
|
* @return callable Parser function
|
2019-12-10 02:38:17 +00:00
|
|
|
*/
|
|
|
|
private function getLocalTimestampParser() : callable {
|
|
|
|
return $this->getTimestampParser(
|
|
|
|
$this->dateFormat,
|
|
|
|
$this->digits,
|
|
|
|
$this->localTimezone,
|
2020-05-19 18:40:05 +00:00
|
|
|
$this->timezones
|
2019-12-10 02:38:17 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the indent level of $node, relative to $rootNode.
|
|
|
|
*
|
|
|
|
* The indent level is the number of lists inside of which it is nested.
|
|
|
|
*
|
|
|
|
* @param DOMNode $node
|
2020-05-11 19:56:23 +00:00
|
|
|
* @param DOMElement $rootNode
|
2019-12-10 02:38:17 +00:00
|
|
|
* @return int
|
|
|
|
*/
|
2020-05-11 19:56:23 +00:00
|
|
|
private function getIndentLevel( DOMNode $node, DOMElement $rootNode ) : int {
|
2019-12-10 02:38:17 +00:00
|
|
|
$indent = 0;
|
|
|
|
while ( $node ) {
|
|
|
|
if ( $node === $rootNode ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$nodeName = strtolower( $node->nodeName );
|
|
|
|
if ( $nodeName === 'li' || $nodeName === 'dd' ) {
|
|
|
|
$indent++;
|
|
|
|
}
|
|
|
|
$node = $node->parentNode;
|
|
|
|
}
|
|
|
|
return $indent;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* This function returns a two-element array. The first element is an array of sibling nodes
|
2020-05-27 17:42:19 +00:00
|
|
|
* comprising the signature, in reverse order (with $timestampNode or its parent node as the last
|
|
|
|
* element). The second element is the username (null for unsigned comments).
|
2019-12-10 02:38:17 +00:00
|
|
|
*
|
|
|
|
* @param DOMText $timestampNode Text node
|
|
|
|
* @param DOMNode|null $until Node to stop searching at
|
|
|
|
* @return array [ nodes, username ]
|
|
|
|
*/
|
2020-05-14 23:09:20 +00:00
|
|
|
private function findSignature( DOMText $timestampNode, ?DOMNode $until = null ) : array {
|
2020-05-27 17:42:19 +00:00
|
|
|
// 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
|
|
|
|
if (
|
|
|
|
!$timestampNode->previousSibling && !$timestampNode->nextSibling &&
|
|
|
|
strtolower( $timestampNode->parentNode->nodeName ) === 'a'
|
|
|
|
) {
|
|
|
|
$timestampNode = $timestampNode->parentNode;
|
|
|
|
}
|
|
|
|
|
2019-12-10 02:38:17 +00:00
|
|
|
$node = $timestampNode;
|
|
|
|
$sigNodes = [ $node ];
|
|
|
|
$sigUsername = null;
|
|
|
|
$length = 0;
|
|
|
|
$lastLinkNode = $timestampNode;
|
2020-05-27 17:42:19 +00:00
|
|
|
|
2019-12-10 02:38:17 +00:00
|
|
|
while (
|
|
|
|
( $node = $node->previousSibling ) && $length < self::SIGNATURE_SCAN_LIMIT && $node !== $until
|
|
|
|
) {
|
|
|
|
$sigNodes[] = $node;
|
|
|
|
$length += $node->textContent ? strlen( $node->textContent ) : 0;
|
2020-06-04 18:48:58 +00:00
|
|
|
if ( !( $node instanceof DOMElement ) ) {
|
2019-12-10 02:38:17 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$links = [];
|
|
|
|
if ( strtolower( $node->nodeName ) === 'a' ) {
|
|
|
|
$links = [ $node ];
|
2020-06-04 18:48:58 +00:00
|
|
|
} else {
|
2019-12-10 02:38:17 +00:00
|
|
|
// Handle links nested in formatting elements.
|
|
|
|
// 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 »"
|
|
|
|
$links = $node->getElementsByTagName( 'a' );
|
|
|
|
}
|
|
|
|
if ( !count( $links ) ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the earliest link that links to the user's user page
|
|
|
|
foreach ( $links as $link ) {
|
|
|
|
$username = null;
|
|
|
|
$title = $this->getTitleFromUrl( $link->getAttribute( 'href' ) );
|
|
|
|
if ( !$title ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ( $title->getNamespace() === NS_USER || $title->getNamespace() === NS_USER_TALK ) {
|
|
|
|
$username = $title->getText();
|
|
|
|
if ( strpos( $username, '/' ) !== false ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} elseif ( $title->isSpecial( 'Contributions' ) ) {
|
|
|
|
$parts = explode( '/', $title->getText(), 2 );
|
|
|
|
if ( !isset( $parts[1] ) ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Normalize the username: users may link to their contributions with an unnormalized name
|
|
|
|
$userpage = Title::makeTitleSafe( NS_USER, $parts[1] );
|
|
|
|
if ( !$userpage ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$username = $userpage->getText();
|
|
|
|
}
|
|
|
|
if ( !$username ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ( IP::isIPv6( $username ) ) {
|
|
|
|
// Bot-generated links "Preceding unsigned comment added by" have non-standard case
|
|
|
|
$username = strtoupper( $username );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Accept the first link to the user namespace, then only accept links to that user
|
|
|
|
if ( $sigUsername === null ) {
|
|
|
|
$sigUsername = $username;
|
|
|
|
}
|
|
|
|
if ( $username === $sigUsername ) {
|
|
|
|
$lastLinkNode = $node;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Keep looking if a node with links wasn't a link to a user page
|
|
|
|
// "Doc James (talk · contribs · email)"
|
|
|
|
}
|
|
|
|
// Pop excess text nodes
|
|
|
|
while ( end( $sigNodes ) !== $lastLinkNode ) {
|
|
|
|
array_pop( $sigNodes );
|
|
|
|
}
|
|
|
|
return [ $sigNodes, $sigUsername ];
|
|
|
|
}
|
|
|
|
|
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).
|
|
|
|
*
|
|
|
|
* @param DOMNode $node
|
|
|
|
* @return int Appropriate NodeFilter constant
|
|
|
|
*/
|
|
|
|
public static function acceptOnlyNodesAllowingComments( DOMNode $node ) {
|
|
|
|
// The table of contents has a heading that gets erroneously detected as a section
|
|
|
|
if ( $node instanceof DOMElement && $node->getAttribute( 'id' ) === 'toc' ) {
|
|
|
|
return NodeFilter::FILTER_REJECT;
|
|
|
|
}
|
|
|
|
return NodeFilter::FILTER_ACCEPT;
|
|
|
|
}
|
|
|
|
|
2019-12-10 02:38:17 +00:00
|
|
|
/**
|
|
|
|
* Find all timestamps within a DOM subtree.
|
|
|
|
*
|
2020-05-11 19:56:23 +00:00
|
|
|
* @param DOMElement $rootNode
|
2019-12-10 02:38:17 +00:00
|
|
|
* @return array Array of [node, matchData] pairs
|
|
|
|
*/
|
2020-05-11 19:56:23 +00:00
|
|
|
public function findTimestamps( DOMElement $rootNode ) : array {
|
2019-12-10 02:38:17 +00:00
|
|
|
$xpath = new DOMXPath( $rootNode->ownerDocument );
|
|
|
|
$textNodes = $xpath->query( '//text()', $rootNode );
|
|
|
|
$matches = [];
|
2020-05-15 21:02:57 +00:00
|
|
|
$timestampRegex = $this->getLocalTimestampRegexp();
|
2019-12-10 02:38:17 +00:00
|
|
|
foreach ( $textNodes as $node ) {
|
|
|
|
$startNode = $node;
|
|
|
|
$nodeText = '';
|
|
|
|
|
|
|
|
while ( $node ) {
|
|
|
|
$nodeText .= $node->nodeValue;
|
|
|
|
|
|
|
|
// 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 (
|
|
|
|
$node->nextSibling &&
|
|
|
|
$node->nextSibling->nodeType === XML_ELEMENT_NODE &&
|
|
|
|
$node->nextSibling->getAttribute( 'typeof' ) === 'mw:Entity'
|
|
|
|
) {
|
|
|
|
$nodeText .= $node->nextSibling->firstChild->nodeValue;
|
|
|
|
|
|
|
|
// If the entity is followed by more text, do this again
|
|
|
|
if (
|
|
|
|
$node->nextSibling->nextSibling &&
|
|
|
|
$node->nextSibling->nextSibling->nodeType === XML_TEXT_NODE
|
|
|
|
) {
|
|
|
|
$node = $node->nextSibling->nextSibling;
|
|
|
|
} else {
|
|
|
|
$node = null;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$node = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$matchData = null;
|
|
|
|
// Allows us to mimic match.index in #getComments
|
|
|
|
if ( preg_match( $timestampRegex, $nodeText, $matchData, PREG_OFFSET_CAPTURE ) ) {
|
|
|
|
$matches[] = [ $startNode, $matchData ];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $matches;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all discussion comments (and headings) within a DOM subtree.
|
|
|
|
*
|
|
|
|
* This returns a flat list, use groupThreads() to associate replies to original messages and
|
|
|
|
* get a tree structure starting at section headings.
|
|
|
|
*
|
|
|
|
* For example, for a MediaWiki discussion like this (we're dealing with HTML DOM here,
|
|
|
|
* the wikitext syntax is just for illustration):
|
|
|
|
*
|
|
|
|
* == A ==
|
|
|
|
* B. ~~~~
|
|
|
|
* : C.
|
|
|
|
* : C. ~~~~
|
|
|
|
* :: D. ~~~~
|
|
|
|
* ::: E. ~~~~
|
|
|
|
* ::: F. ~~~~
|
|
|
|
* : G. ~~~~
|
|
|
|
* H. ~~~~
|
|
|
|
* : I. ~~~~
|
|
|
|
*
|
|
|
|
* This function would return a structure like:
|
|
|
|
*
|
|
|
|
* [
|
2020-06-25 12:23:17 +00:00
|
|
|
* HeadingItem( { level: 0, range: (h2: A) } ),
|
|
|
|
* CommentItem( { level: 1, range: (p: B) } ),
|
|
|
|
* CommentItem( { level: 2, range: (li: C, li: C) } ),
|
|
|
|
* CommentItem( { level: 3, range: (li: D) } ),
|
|
|
|
* CommentItem( { level: 4, range: (li: E) } ),
|
|
|
|
* CommentItem( { level: 4, range: (li: F) } ),
|
|
|
|
* CommentItem( { level: 2, range: (li: G) } ),
|
|
|
|
* CommentItem( { level: 1, range: (p: H) } ),
|
|
|
|
* CommentItem( { level: 2, range: (li: I) } )
|
2019-12-10 02:38:17 +00:00
|
|
|
* ]
|
|
|
|
*
|
2020-05-11 19:56:23 +00:00
|
|
|
* @param DOMElement $rootNode
|
2020-05-22 16:26:05 +00:00
|
|
|
* @return ThreadItem[] Thread items
|
2019-12-10 02:38:17 +00:00
|
|
|
*/
|
2020-05-11 19:56:23 +00:00
|
|
|
public function getComments( DOMElement $rootNode ) : array {
|
2019-12-10 02:38:17 +00:00
|
|
|
$timestamps = $this->findTimestamps( $rootNode );
|
|
|
|
|
|
|
|
$comments = [];
|
|
|
|
$dfParser = $this->getLocalTimestampParser();
|
|
|
|
|
|
|
|
// Placeholder heading in case there are comments in the 0th section
|
2020-05-22 13:47:21 +00:00
|
|
|
$range = new ImmutableRange( $rootNode, 0, $rootNode, 0 );
|
2020-05-22 16:26:05 +00:00
|
|
|
$fakeHeading = new HeadingItem( $range, true );
|
2019-12-10 02:38:17 +00:00
|
|
|
|
|
|
|
$curComment = $fakeHeading;
|
|
|
|
|
|
|
|
$nextTimestamp = 0;
|
2020-07-15 14:53:11 +00:00
|
|
|
$treeWalker = new TreeWalker(
|
|
|
|
$rootNode,
|
|
|
|
NodeFilter::SHOW_ELEMENT | NodeFilter::SHOW_TEXT,
|
|
|
|
[ self::class, 'acceptOnlyNodesAllowingComments' ]
|
|
|
|
);
|
|
|
|
while ( $node = $treeWalker->nextNode() ) {
|
2019-12-10 02:38:17 +00:00
|
|
|
if ( $node->nodeType === XML_ELEMENT_NODE && preg_match( '/^h[1-6]$/i', $node->nodeName ) ) {
|
2020-05-22 13:47:21 +00:00
|
|
|
$range = new ImmutableRange( $node, 0, $node, $node->childNodes->length );
|
2020-05-22 16:26:05 +00:00
|
|
|
$curComment = new HeadingItem( $range );
|
2019-12-10 02:38:17 +00:00
|
|
|
$comments[] = $curComment;
|
2020-07-15 14:53:11 +00:00
|
|
|
} elseif (
|
|
|
|
$node instanceof DOMText &&
|
|
|
|
isset( $timestamps[$nextTimestamp] ) && $node === $timestamps[$nextTimestamp][0]
|
|
|
|
) {
|
2020-05-13 18:23:48 +00:00
|
|
|
$warnings = [];
|
2020-05-22 16:26:05 +00:00
|
|
|
$foundSignature = $this->findSignature( $node, $curComment->getRange()->endContainer );
|
2019-12-10 02:38:17 +00:00
|
|
|
$author = $foundSignature[1];
|
|
|
|
$firstSigNode = end( $foundSignature[0] );
|
2020-05-27 17:42:19 +00:00
|
|
|
$lastSigNode = $foundSignature[0][0];
|
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.
|
|
|
|
$nextTimestamp++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Everything from the last comment up to here is the next comment
|
2020-05-22 16:26:05 +00:00
|
|
|
$startNode = $this->nextInterestingLeafNode( $curComment->getRange()->endContainer, $rootNode );
|
2019-12-10 02:38:17 +00:00
|
|
|
$match = $timestamps[$nextTimestamp][1];
|
2020-05-27 17:42:19 +00:00
|
|
|
$offset = $lastSigNode === $node ?
|
|
|
|
$match[0][1] + strlen( $match[0][0] ) :
|
|
|
|
CommentUtils::childIndexOf( $lastSigNode ) + 1;
|
2020-05-22 13:47:21 +00:00
|
|
|
$range = new ImmutableRange(
|
|
|
|
$startNode->parentNode,
|
|
|
|
CommentUtils::childIndexOf( $startNode ),
|
2020-05-27 17:42:19 +00:00
|
|
|
$lastSigNode === $node ? $node : $lastSigNode->parentNode,
|
|
|
|
$offset
|
2020-05-22 13:47:21 +00:00
|
|
|
);
|
|
|
|
$sigRange = new ImmutableRange(
|
|
|
|
$firstSigNode->parentNode,
|
|
|
|
CommentUtils::childIndexOf( $firstSigNode ),
|
2020-05-27 17:42:19 +00:00
|
|
|
$lastSigNode === $node ? $node : $lastSigNode->parentNode,
|
|
|
|
$offset
|
2020-05-22 13:47:21 +00:00
|
|
|
);
|
2019-12-10 02:38:17 +00:00
|
|
|
|
|
|
|
$startLevel = $this->getIndentLevel( $startNode, $rootNode ) + 1;
|
|
|
|
$endLevel = $this->getIndentLevel( $node, $rootNode ) + 1;
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2020-05-12 11:51:20 +00:00
|
|
|
// within one paragraph/list-item result in a confusing double "Reply" button, and we also have
|
2019-12-10 02:38:17 +00:00
|
|
|
// no way to indicate which one you're replying to (this might matter in the future for
|
|
|
|
// notifications or something).
|
|
|
|
if (
|
2020-05-22 16:26:05 +00:00
|
|
|
$curComment instanceof CommentItem &&
|
2019-12-10 02:38:17 +00:00
|
|
|
(
|
2020-05-14 22:44:49 +00:00
|
|
|
CommentUtils::closestElement(
|
2020-05-11 20:07:14 +00:00
|
|
|
$node, [ 'li', 'dd', 'p' ]
|
|
|
|
) ?? $node->parentNode
|
|
|
|
) ===
|
|
|
|
(
|
2020-05-14 22:44:49 +00:00
|
|
|
CommentUtils::closestElement(
|
2020-05-22 16:26:05 +00:00
|
|
|
$curComment->getRange()->endContainer, [ 'li', 'dd', 'p' ]
|
|
|
|
) ?? $curComment->getRange()->endContainer->parentNode
|
2019-12-10 02:38:17 +00:00
|
|
|
)
|
|
|
|
) {
|
|
|
|
// Merge this with the previous comment. Use that comment's author and timestamp.
|
2020-05-22 16:26:05 +00:00
|
|
|
$curComment->setRange(
|
|
|
|
$curComment->getRange()->setEnd( $range->endContainer, $range->endOffset )
|
|
|
|
);
|
|
|
|
$curComment->addSignatureRange( $sigRange );
|
|
|
|
$curComment->setLevel( min( min( $startLevel, $endLevel ), $curComment->getLevel() ) );
|
2019-12-10 02:38:17 +00:00
|
|
|
|
|
|
|
$nextTimestamp++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-05-13 18:23:48 +00:00
|
|
|
$dateTime = $dfParser( $match );
|
|
|
|
if ( isset( $dateTime->discussionToolsWarning ) ) {
|
|
|
|
$warnings[] = $dateTime->discussionToolsWarning;
|
|
|
|
}
|
|
|
|
|
2020-05-22 16:26:05 +00:00
|
|
|
$curComment = new CommentItem(
|
|
|
|
// Should this use the indent level of $startNode or $node?
|
|
|
|
min( $startLevel, $endLevel ),
|
|
|
|
$range,
|
|
|
|
[ $sigRange ],
|
2020-05-12 23:21:02 +00:00
|
|
|
// ISO 8601 date. Almost DateTimeInterface::RFC3339_EXTENDED, but ending with 'Z' instead
|
|
|
|
// of '+00:00', like Date#toISOString in JavaScript.
|
2020-05-22 16:26:05 +00:00
|
|
|
$dateTime->format( 'Y-m-d\TH:i:s.v\Z' ),
|
|
|
|
$author
|
|
|
|
);
|
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
|
|
|
}
|
2019-12-10 02:38:17 +00:00
|
|
|
$comments[] = $curComment;
|
|
|
|
$nextTimestamp++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert the fake placeholder heading if there are any comments in the 0th section
|
|
|
|
// (before the first real heading)
|
2020-05-22 16:26:05 +00:00
|
|
|
if ( count( $comments ) && !( $comments[ 0 ] instanceof HeadingItem ) ) {
|
2019-12-10 02:38:17 +00:00
|
|
|
array_unshift( $comments, $fakeHeading );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $comments;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Group discussion comments into threads and associate replies to original messages.
|
|
|
|
*
|
|
|
|
* Each thread must begin with a heading. Original messages in the thread are treated as replies to
|
|
|
|
* its heading. Other replies are associated based on the order and indentation level.
|
|
|
|
*
|
|
|
|
* Note that the objects in `comments` are extended in-place with the additional data.
|
|
|
|
*
|
|
|
|
* For example, for a MediaWiki discussion like this (we're dealing with HTML DOM here,
|
|
|
|
* the wikitext syntax is just for illustration):
|
|
|
|
*
|
|
|
|
* == A ==
|
|
|
|
* B. ~~~~
|
|
|
|
* : C.
|
|
|
|
* : C. ~~~~
|
|
|
|
* :: D. ~~~~
|
|
|
|
* ::: E. ~~~~
|
|
|
|
* ::: F. ~~~~
|
|
|
|
* : G. ~~~~
|
|
|
|
* H. ~~~~
|
|
|
|
* : I. ~~~~
|
|
|
|
*
|
|
|
|
* This function would return a structure like:
|
|
|
|
*
|
|
|
|
* [
|
2020-06-25 12:23:17 +00:00
|
|
|
* HeadingItem( { level: 0, range: (h2: A), replies: [
|
|
|
|
* CommentItem( { level: 1, range: (p: B), replies: [
|
|
|
|
* CommentItem( { level: 2, range: (li: C, li: C), replies: [
|
|
|
|
* CommentItem( { level: 3, range: (li: D), replies: [
|
|
|
|
* CommentItem( { level: 4, range: (li: E), replies: [] },
|
|
|
|
* CommentItem( { level: 4, range: (li: F), replies: [] },
|
|
|
|
* ] },
|
|
|
|
* ] },
|
|
|
|
* CommentItem( { level: 2, range: (li: G), replies: [] },
|
|
|
|
* ] },
|
|
|
|
* CommentItem( { level: 1, range: (p: H), replies: [
|
|
|
|
* CommentItem( { level: 2, range: (li: I), replies: [] },
|
|
|
|
* ] },
|
|
|
|
* ] } )
|
2019-12-10 02:38:17 +00:00
|
|
|
* ]
|
|
|
|
*
|
2020-05-22 16:26:05 +00:00
|
|
|
* @param ThreadItem[] &$comments Result of #getComments, will be modified to add more properties
|
2020-06-25 12:23:17 +00:00
|
|
|
* @return HeadingItem[] Tree structure of comments, top-level items are the headings.
|
2019-12-10 02:38:17 +00:00
|
|
|
*/
|
|
|
|
public function groupThreads( array &$comments ) : array {
|
|
|
|
$threads = [];
|
|
|
|
$replies = [];
|
|
|
|
$commentsById = [];
|
|
|
|
|
|
|
|
foreach ( $comments as &$comment ) {
|
2020-05-22 16:26:05 +00:00
|
|
|
if ( $comment instanceof HeadingItem ) {
|
2019-12-10 02:38:17 +00:00
|
|
|
// We don't need ids for section headings right now, but we might in the future
|
|
|
|
// e.g. if we allow replying directly to sections (adding top-level comments)
|
|
|
|
$id = null;
|
2020-05-22 16:26:05 +00:00
|
|
|
} elseif ( $comment instanceof CommentItem ) {
|
|
|
|
$id = ( $comment->getAuthor() ?? '' ) . '|' . $comment->getTimestamp();
|
2019-12-10 02:38:17 +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), append sequential numbers
|
|
|
|
$number = 0;
|
|
|
|
while ( isset( $commentsById["$id|$number"] ) ) {
|
|
|
|
$number++;
|
|
|
|
}
|
|
|
|
$id = "$id|$number";
|
2020-05-22 16:26:05 +00:00
|
|
|
} else {
|
|
|
|
throw new MWException( 'Unknown ThreadItem type' );
|
2019-12-10 02:38:17 +00:00
|
|
|
}
|
2020-05-12 23:21:02 +00:00
|
|
|
|
2019-12-10 02:38:17 +00:00
|
|
|
if ( $id !== null ) {
|
|
|
|
$commentsById[$id] = $comment;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This modifies the original objects in $comments!
|
2020-05-22 16:26:05 +00:00
|
|
|
$comment->setId( $id );
|
2019-12-10 02:38:17 +00:00
|
|
|
|
2020-05-22 16:26:05 +00:00
|
|
|
if ( count( $replies ) < $comment->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-05-22 16:26:05 +00:00
|
|
|
$comment->addWarning( 'Comment skips indentation level' );
|
|
|
|
while ( count( $replies ) < $comment->getLevel() ) {
|
2019-12-10 02:38:17 +00:00
|
|
|
// FIXME this will clone the reply, not just set a reference
|
|
|
|
$replies[] = end( $replies );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-22 16:26:05 +00:00
|
|
|
if ( $comment instanceof HeadingItem ) {
|
2019-12-10 02:38:17 +00:00
|
|
|
// New root (thread)
|
|
|
|
$threads[] = $comment;
|
2020-05-22 16:26:05 +00:00
|
|
|
} elseif ( isset( $replies[ $comment->getLevel() - 1 ] ) ) {
|
2019-12-10 02:38:17 +00:00
|
|
|
// Add as a reply to the closest less-nested comment
|
2020-05-22 16:26:05 +00:00
|
|
|
$comment->setParent( $replies[ $comment->getLevel() - 1 ] );
|
|
|
|
$comment->getParent()->addReply( $comment );
|
2019-12-10 02:38:17 +00:00
|
|
|
} else {
|
2020-05-22 16:26:05 +00:00
|
|
|
$comment->addWarning( 'Comment could not be connected to a thread' );
|
2019-12-10 02:38:17 +00:00
|
|
|
}
|
|
|
|
|
2020-05-22 16:26:05 +00:00
|
|
|
$replies[ $comment->getLevel() ] = $comment;
|
2019-12-10 02:38:17 +00:00
|
|
|
// Cut off more deeply nested replies
|
2020-05-22 16:26:05 +00:00
|
|
|
array_splice( $replies, $comment->getLevel() + 1 );
|
2019-12-10 02:38:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $threads;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the list of authors involved in a comment and its replies.
|
|
|
|
*
|
2020-05-22 16:26:05 +00:00
|
|
|
* @param HeadingItem $heading Heading object, as returned by #groupThreads
|
2019-12-10 02:38:17 +00:00
|
|
|
* @return string[] Author usernames
|
|
|
|
*/
|
2020-05-22 16:26:05 +00:00
|
|
|
public function getAuthors( HeadingItem $heading ) : array {
|
2019-12-10 02:38:17 +00:00
|
|
|
$authors = [];
|
2020-05-22 16:26:05 +00:00
|
|
|
$getAuthorSet = function ( CommentItem $comment ) use ( &$authors, &$getAuthorSet ) {
|
|
|
|
$author = $comment->getAuthor();
|
|
|
|
if ( $author ) {
|
|
|
|
$authors[ $author ] = true;
|
2019-12-10 02:38:17 +00:00
|
|
|
}
|
|
|
|
// Get the set of authors in the same format from each reply
|
2020-05-22 16:26:05 +00:00
|
|
|
array_map( $getAuthorSet, $comment->getReplies() );
|
2019-12-10 02:38:17 +00:00
|
|
|
};
|
|
|
|
|
2020-05-22 16:26:05 +00:00
|
|
|
array_map( $getAuthorSet, $heading->getReplies() );
|
2019-12-10 02:38:17 +00:00
|
|
|
|
|
|
|
ksort( $authors );
|
|
|
|
return array_keys( $authors );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the name of the page from which this comment is transcluded (if any).
|
|
|
|
*
|
2020-05-22 16:26:05 +00:00
|
|
|
* @param CommentItem $comment Comment object, as returned by #groupThreads
|
2019-12-10 02:38:17 +00:00
|
|
|
* @return string|bool `false` if this comment 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.
|
|
|
|
*/
|
2020-05-22 16:26:05 +00:00
|
|
|
public function getTranscludedFrom( CommentItem $comment ) {
|
2020-05-27 17:31:31 +00:00
|
|
|
// If some template is used within the comment (e.g. {{ping|…}} or {{tl|…}}, or a
|
|
|
|
// non-substituted signature template), that *does not* mean the comment is transcluded.
|
|
|
|
// We only want to consider comments to be transcluded if the wrapper element (usually
|
|
|
|
// <li> or <p>) is marked as part of a transclusion. If we can't find a wrapper, using
|
|
|
|
// endContainer should avoid false negatives (although may have false positives).
|
2020-05-31 15:17:59 +00:00
|
|
|
$node = CommentUtils::getTranscludedFromElement(
|
2020-05-22 16:26:05 +00:00
|
|
|
CommentUtils::getFullyCoveredWrapper( $comment ) ?: $comment->getRange()->endContainer
|
2020-05-31 15:17:59 +00:00
|
|
|
);
|
2019-12-10 02:38:17 +00:00
|
|
|
|
|
|
|
if ( !$node ) {
|
|
|
|
// No mw:Transclusion node found, this comment is not transcluded
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$dataMw = json_decode( $node->getAttribute( 'data-mw' ), true );
|
|
|
|
|
|
|
|
// Only return a page name if this is a simple single-template transclusion.
|
2020-06-04 18:48:58 +00:00
|
|
|
if (
|
|
|
|
is_array( $dataMw ) &&
|
2019-12-10 02:38:17 +00:00
|
|
|
$dataMw['parts'] &&
|
|
|
|
count( $dataMw['parts'] ) === 1 &&
|
|
|
|
$dataMw['parts'][0]['template'] &&
|
|
|
|
$dataMw['parts'][0]['template']['target']['href']
|
|
|
|
) {
|
2020-05-15 00:51:36 +00:00
|
|
|
$title = self::getTitleFromUrl( $dataMw['parts'][0]['template']['target']['href'] );
|
|
|
|
return $title->getPrefixedText();
|
2019-12-10 02:38:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
}
|