2019-09-26 07:06:56 +00:00
|
|
|
'use strict';
|
2020-05-22 16:26:05 +00:00
|
|
|
/* global $:off */
|
|
|
|
|
2024-05-24 12:31:30 +00:00
|
|
|
const
|
2020-03-08 14:32:38 +00:00
|
|
|
utils = require( './utils.js' ),
|
2022-05-16 22:48:24 +00:00
|
|
|
charAt = require( 'mediawiki.String' ).charAt,
|
2020-09-08 00:23:53 +00:00
|
|
|
codePointLength = require( 'mediawiki.String' ).codePointLength,
|
2021-02-02 17:40:18 +00:00
|
|
|
trimByteLength = require( 'mediawiki.String' ).trimByteLength,
|
2020-05-22 16:26:05 +00:00
|
|
|
CommentItem = require( './CommentItem.js' ),
|
|
|
|
HeadingItem = require( './HeadingItem.js' ),
|
2021-01-26 18:58:58 +00:00
|
|
|
ThreadItem = require( './ThreadItem.js' ),
|
2022-02-19 06:31:34 +00:00
|
|
|
ThreadItemSet = require( './ThreadItemSet.js' ),
|
2019-10-18 12:12:55 +00:00
|
|
|
moment = require( './lib/moment-timezone/moment-timezone-with-data-1970-2030.js' );
|
2019-09-26 07:06:56 +00:00
|
|
|
|
2019-10-20 16:36:43 +00:00
|
|
|
/**
|
|
|
|
* Utilities for detecting and parsing components of discussion pages: signatures, timestamps,
|
|
|
|
* comments and threads.
|
|
|
|
*
|
2020-07-20 21:15:03 +00:00
|
|
|
* @class mw.dt.Parser
|
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 {Array} data Language-specific data to be used for parsing
|
2022-01-11 15:50:44 +00:00
|
|
|
* @constructor
|
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
|
|
|
*/
|
|
|
|
function Parser( data ) {
|
|
|
|
this.data = data;
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*
|
|
|
|
* @constant {number}
|
|
|
|
*/
|
2024-05-24 12:31:30 +00:00
|
|
|
const SIGNATURE_SCAN_LIMIT = 100;
|
2022-02-21 21:42:14 +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.
|
|
|
|
*
|
2022-01-11 15:50:44 +00:00
|
|
|
* @param {HTMLElement} rootNode Root node of content to parse
|
|
|
|
* @param {mw.Title} title Title of the page being parsed
|
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
|
|
|
* @chainable
|
|
|
|
* @return {Parser}
|
2019-10-20 16:36:43 +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
|
|
|
Parser.prototype.parse = function ( rootNode, title ) {
|
2020-07-20 21:15:03 +00:00
|
|
|
this.rootNode = rootNode;
|
2022-01-11 15:50:44 +00:00
|
|
|
this.title = title;
|
2022-02-19 06:31:34 +00:00
|
|
|
|
2024-05-24 12:20:50 +00:00
|
|
|
const result = this.buildThreadItems();
|
2022-02-19 06:31:34 +00:00
|
|
|
this.buildThreads( result );
|
|
|
|
this.computeIdsAndNames( result );
|
|
|
|
|
|
|
|
return result;
|
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
|
|
|
};
|
2020-07-20 21:15:03 +00:00
|
|
|
|
|
|
|
OO.initClass( Parser );
|
|
|
|
|
2019-10-20 16:36:43 +00:00
|
|
|
/**
|
|
|
|
* Get text of localisation messages in content language.
|
|
|
|
*
|
|
|
|
* @private
|
2020-09-03 20:59:33 +00:00
|
|
|
* @param {string} contLangVariant Content language variant
|
|
|
|
* @param {string[]} messages Message keys
|
|
|
|
* @return {string[]} Message values
|
2019-10-20 16:36:43 +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
|
|
|
Parser.prototype.getMessages = function ( contLangVariant, messages ) {
|
2024-04-19 22:07:35 +00:00
|
|
|
return messages.map( ( code ) => this.data.contLangMessages[ contLangVariant ][ code ] );
|
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
|
|
|
};
|
2019-09-26 07:06:56 +00:00
|
|
|
|
2019-10-20 16:36:43 +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-07-20 21:15:03 +00:00
|
|
|
* @private
|
2020-09-03 20:59:33 +00:00
|
|
|
* @param {string} contLangVariant Content language variant
|
2019-10-20 16:36:43 +00:00
|
|
|
* @param {string} format Date format, as used by MediaWiki
|
2020-05-08 19:21:53 +00:00
|
|
|
* @param {string} digitsRegexp Regular expression matching a single localised digit, e.g. `[0-9]`
|
2021-12-20 17:25:54 +00:00
|
|
|
* @param {Object.<string,string>} tzAbbrs Map of localised timezone abbreviations to IANA abbreviations
|
2019-10-20 16:36:43 +00:00
|
|
|
* for the local timezone, e.g. `{EDT: "EDT", EST: "EST"}`
|
|
|
|
* @return {string} Regular expression
|
|
|
|
*/
|
2020-09-03 20:59:33 +00:00
|
|
|
Parser.prototype.getTimestampRegexp = function ( contLangVariant, format, digitsRegexp, tzAbbrs ) {
|
2020-08-26 22:46:34 +00:00
|
|
|
function regexpGroup( r ) {
|
|
|
|
return '(' + r + ')';
|
2019-10-20 16:26:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function regexpAlternateGroup( array ) {
|
|
|
|
return '(' + array.map( mw.util.escapeRegExp ).join( '|' ) + ')';
|
|
|
|
}
|
2019-09-26 07:06:56 +00:00
|
|
|
|
2024-05-24 12:20:50 +00:00
|
|
|
let s = '';
|
|
|
|
let raw = false;
|
2019-10-20 16:26:00 +00:00
|
|
|
// Adapted from Language::sprintfDate()
|
2024-05-24 12:20:50 +00:00
|
|
|
for ( let p = 0; p < format.length; p++ ) {
|
|
|
|
let num = false;
|
|
|
|
let code = format[ p ];
|
2019-09-26 07:06:56 +00:00
|
|
|
if ( code === 'x' && p < format.length - 1 ) {
|
|
|
|
code += format[ ++p ];
|
|
|
|
}
|
|
|
|
if ( code === 'xk' && p < format.length - 1 ) {
|
|
|
|
code += format[ ++p ];
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ( code ) {
|
|
|
|
case 'xx':
|
|
|
|
s += 'x';
|
|
|
|
break;
|
|
|
|
case 'xg':
|
2024-04-18 18:37:58 +00:00
|
|
|
s += regexpAlternateGroup( this.getMessages( contLangVariant, [
|
2019-09-26 07:06:56 +00:00
|
|
|
'january-gen', 'february-gen', 'march-gen', 'april-gen', 'may-gen', 'june-gen',
|
|
|
|
'july-gen', 'august-gen', 'september-gen', 'october-gen', 'november-gen',
|
|
|
|
'december-gen'
|
|
|
|
] ) );
|
|
|
|
break;
|
2023-06-04 18:16:56 +00:00
|
|
|
case 'xn':
|
|
|
|
raw = true;
|
|
|
|
break;
|
2019-09-26 07:06:56 +00:00
|
|
|
case 'd':
|
|
|
|
num = '2';
|
|
|
|
break;
|
|
|
|
case 'D':
|
2024-04-18 18:37:58 +00:00
|
|
|
s += regexpAlternateGroup( this.getMessages( contLangVariant, [
|
2019-09-26 07:06:56 +00:00
|
|
|
'sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'
|
|
|
|
] ) );
|
|
|
|
break;
|
|
|
|
case 'j':
|
|
|
|
num = '1,2';
|
|
|
|
break;
|
|
|
|
case 'l':
|
2024-04-18 18:37:58 +00:00
|
|
|
s += regexpAlternateGroup( this.getMessages( contLangVariant, [
|
2019-09-26 07:06:56 +00:00
|
|
|
'sunday', 'monday', 'tuesday', 'wednesday', 'thursday',
|
|
|
|
'friday', 'saturday'
|
|
|
|
] ) );
|
|
|
|
break;
|
|
|
|
case 'F':
|
2024-04-18 18:37:58 +00:00
|
|
|
s += regexpAlternateGroup( this.getMessages( contLangVariant, [
|
2019-09-26 07:06:56 +00:00
|
|
|
'january', 'february', 'march', 'april', 'may_long', 'june',
|
|
|
|
'july', 'august', 'september', 'october', 'november',
|
|
|
|
'december'
|
|
|
|
] ) );
|
|
|
|
break;
|
|
|
|
case 'M':
|
2024-04-18 18:37:58 +00:00
|
|
|
s += regexpAlternateGroup( this.getMessages( contLangVariant, [
|
2019-09-26 07:06:56 +00:00
|
|
|
'jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug',
|
|
|
|
'sep', 'oct', 'nov', 'dec'
|
|
|
|
] ) );
|
|
|
|
break;
|
2023-06-04 18:16:56 +00:00
|
|
|
case 'm':
|
|
|
|
num = '2';
|
|
|
|
break;
|
2019-09-26 07:06:56 +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-09-26 07:06:56 +00:00
|
|
|
case '\\':
|
|
|
|
// Backslash escaping
|
|
|
|
if ( p < format.length - 1 ) {
|
2019-10-20 16:34:44 +00:00
|
|
|
s += mw.util.escapeRegExp( format[ ++p ] );
|
2019-09-26 07:06:56 +00:00
|
|
|
} else {
|
2019-10-20 16:34:44 +00:00
|
|
|
s += mw.util.escapeRegExp( '\\' );
|
2019-09-26 07:06:56 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '"':
|
|
|
|
// Quoted literal
|
|
|
|
if ( p < format.length - 1 ) {
|
2024-05-24 12:20:50 +00:00
|
|
|
const endQuote = format.indexOf( '"', p + 1 );
|
2019-09-26 07:06:56 +00:00
|
|
|
if ( endQuote === -1 ) {
|
|
|
|
// No terminating quote, assume literal "
|
|
|
|
s += '"';
|
|
|
|
} else {
|
2021-11-08 19:03:40 +00:00
|
|
|
s += mw.util.escapeRegExp( format.slice( p + 1, endQuote ) );
|
2019-09-26 07:06:56 +00:00
|
|
|
p = endQuote;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Quote at end of string, assume literal "
|
|
|
|
s += '"';
|
|
|
|
}
|
|
|
|
break;
|
2024-05-24 12:31:30 +00:00
|
|
|
default: {
|
2022-05-16 22:48:24 +00:00
|
|
|
// Copy whole characters together, instead of single UTF-16 surrogates
|
2024-05-24 12:31:30 +00:00
|
|
|
const char = charAt( format, p );
|
2022-05-16 22:48:24 +00:00
|
|
|
s += mw.util.escapeRegExp( char );
|
|
|
|
p += char.length - 1;
|
2024-05-24 12:31:30 +00:00
|
|
|
}
|
2019-09-26 07:06:56 +00:00
|
|
|
}
|
|
|
|
if ( num !== false ) {
|
2023-06-04 18:16:56 +00:00
|
|
|
if ( raw ) {
|
|
|
|
s += regexpGroup( '[0-9]{' + num + '}' );
|
|
|
|
raw = false;
|
|
|
|
} else {
|
|
|
|
s += regexpGroup( digitsRegexp + '{' + num + '}' );
|
|
|
|
}
|
2019-09-26 07:06:56 +00:00
|
|
|
}
|
2022-05-16 22:48:24 +00:00
|
|
|
// Ignore some invisible Unicode characters that often sneak into copy-pasted timestamps (T308448)
|
|
|
|
s += '[\\u200E\\u200F]?';
|
2019-09-26 07:06:56 +00:00
|
|
|
}
|
|
|
|
|
2024-05-24 12:20:50 +00:00
|
|
|
const tzRegexp = regexpAlternateGroup( Object.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)
|
2024-05-24 12:20:50 +00:00
|
|
|
const regexp = s + ' [\\u200E\\u200F]?\\(' + tzRegexp + '\\)';
|
2019-10-18 12:12:55 +00:00
|
|
|
|
|
|
|
return regexp;
|
2020-07-20 21:15:03 +00:00
|
|
|
};
|
2019-09-26 07:06:56 +00:00
|
|
|
|
2019-10-20 16:36:43 +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-07-20 21:15:03 +00:00
|
|
|
* @private
|
2020-09-03 20:59:33 +00:00
|
|
|
* @param {string} contLangVariant Content language variant
|
2019-10-20 16:36:43 +00:00
|
|
|
* @param {string} format Date format, as used by MediaWiki
|
2020-09-14 21:31:53 +00:00
|
|
|
* @param {string[]|null} digits Localised digits from 0 to 9, e.g. `[ '0', '1', ..., '9' ]`
|
2019-10-20 16:36:43 +00:00
|
|
|
* @param {string} localTimezone Local timezone IANA name, e.g. `America/New_York`
|
2021-12-20 17:25:54 +00:00
|
|
|
* @param {Object.<string,string>} tzAbbrs Map of localised timezone abbreviations to IANA abbreviations
|
2019-10-20 16:36:43 +00:00
|
|
|
* for the local timezone, e.g. `{EDT: "EDT", EST: "EST"}`
|
2020-06-25 12:23:17 +00:00
|
|
|
* @return {TimestampParser} Timestamp parser function
|
2019-10-20 16:36:43 +00:00
|
|
|
*/
|
2020-09-03 20:59:33 +00:00
|
|
|
Parser.prototype.getTimestampParser = function ( contLangVariant, format, digits, localTimezone, tzAbbrs ) {
|
2024-05-24 12:20:50 +00:00
|
|
|
const matchingGroups = [];
|
|
|
|
for ( let p = 0; p < format.length; p++ ) {
|
|
|
|
let code = format[ p ];
|
2019-09-26 07:06:56 +00:00
|
|
|
if ( code === 'x' && p < format.length - 1 ) {
|
|
|
|
code += format[ ++p ];
|
|
|
|
}
|
|
|
|
if ( code === 'xk' && p < format.length - 1 ) {
|
|
|
|
code += format[ ++p ];
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ( code ) {
|
|
|
|
case 'xx':
|
2023-06-04 18:16:56 +00:00
|
|
|
case 'xn':
|
2019-09-26 07:06:56 +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-09-26 07:06:56 +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-09-26 07:06:56 +00:00
|
|
|
matchingGroups.push( code );
|
|
|
|
break;
|
|
|
|
case '\\':
|
|
|
|
// Backslash escaping
|
|
|
|
if ( p < format.length - 1 ) {
|
|
|
|
++p;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '"':
|
|
|
|
// Quoted literal
|
|
|
|
if ( p < format.length - 1 ) {
|
2024-05-24 12:20:50 +00:00
|
|
|
const endQuote = format.indexOf( '"', p + 1 );
|
2019-09-26 07:06:56 +00:00
|
|
|
if ( endQuote !== -1 ) {
|
|
|
|
p = endQuote;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-30 08:20:27 +00:00
|
|
|
/**
|
|
|
|
* @param {string} text
|
|
|
|
* @return {number}
|
|
|
|
*/
|
2019-09-26 07:06:56 +00:00
|
|
|
function untransformDigits( text ) {
|
2023-10-30 08:20:27 +00:00
|
|
|
return Number( digits ? text.replace(
|
2023-11-28 16:10:47 +00:00
|
|
|
// digits list comes from site config so is trusted
|
2020-08-31 22:13:00 +00:00
|
|
|
new RegExp( '[' + digits.join( '' ) + ']', 'g' ),
|
2023-10-30 08:20:27 +00:00
|
|
|
( m ) => digits.indexOf( m )
|
|
|
|
) : text );
|
2019-09-26 07:06:56 +00:00
|
|
|
}
|
|
|
|
|
2020-06-25 12:23:17 +00:00
|
|
|
/**
|
|
|
|
* @typedef {function(Array):moment} TimestampParser
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Timestamp parser
|
|
|
|
*
|
|
|
|
* @param {Array} match RegExp match data
|
2023-11-30 17:37:38 +00:00
|
|
|
* @return {Object} Result, an object with the following keys (or null if the date is invalid):
|
2022-03-03 16:29:39 +00:00
|
|
|
* - {moment} date Moment date object
|
|
|
|
* - {string|null} warning Warning message if the input wasn't correctly formed
|
2020-06-25 12:23:17 +00:00
|
|
|
*/
|
2024-04-18 18:37:58 +00:00
|
|
|
return ( match ) => {
|
2024-05-24 12:20:50 +00:00
|
|
|
let
|
2019-09-26 07:06:56 +00:00
|
|
|
year = 0,
|
|
|
|
monthIdx = 0,
|
|
|
|
day = 0,
|
|
|
|
hour = 0,
|
2021-04-08 13:46:09 +00:00
|
|
|
minute = 0;
|
|
|
|
|
2024-05-24 12:20:50 +00:00
|
|
|
for ( let i = 0; i < matchingGroups.length; i++ ) {
|
|
|
|
const code2 = matchingGroups[ i ];
|
|
|
|
const text = match[ i + 1 ];
|
2019-09-26 07:06:56 +00:00
|
|
|
|
2020-08-26 22:46:34 +00:00
|
|
|
switch ( code2 ) {
|
2019-09-26 07:06:56 +00:00
|
|
|
case 'xg':
|
2024-04-18 18:37:58 +00:00
|
|
|
monthIdx = this.getMessages( contLangVariant, [
|
2019-09-26 07:06:56 +00:00
|
|
|
'january-gen', 'february-gen', 'march-gen', 'april-gen', 'may-gen', 'june-gen',
|
|
|
|
'july-gen', 'august-gen', 'september-gen', 'october-gen', 'november-gen',
|
|
|
|
'december-gen'
|
|
|
|
] ).indexOf( text );
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
case 'j':
|
2023-10-30 08:20:27 +00:00
|
|
|
day = untransformDigits( text );
|
2019-09-26 07:06:56 +00:00
|
|
|
break;
|
|
|
|
case 'D':
|
|
|
|
case 'l':
|
|
|
|
// Day of the week - unused
|
|
|
|
break;
|
|
|
|
case 'F':
|
2024-04-18 18:37:58 +00:00
|
|
|
monthIdx = this.getMessages( contLangVariant, [
|
2019-09-26 07:06:56 +00:00
|
|
|
'january', 'february', 'march', 'april', 'may_long', 'june',
|
|
|
|
'july', 'august', 'september', 'october', 'november',
|
|
|
|
'december'
|
|
|
|
] ).indexOf( text );
|
|
|
|
break;
|
|
|
|
case 'M':
|
2024-04-18 18:37:58 +00:00
|
|
|
monthIdx = this.getMessages( contLangVariant, [
|
2019-09-26 07:06:56 +00:00
|
|
|
'jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug',
|
|
|
|
'sep', 'oct', 'nov', 'dec'
|
|
|
|
] ).indexOf( text );
|
|
|
|
break;
|
2023-06-04 18:16:56 +00:00
|
|
|
case 'm':
|
2019-09-26 07:06:56 +00:00
|
|
|
case 'n':
|
2023-10-30 08:20:27 +00:00
|
|
|
monthIdx = untransformDigits( text ) - 1;
|
2019-09-26 07:06:56 +00:00
|
|
|
break;
|
|
|
|
case 'Y':
|
2023-10-30 08:20:27 +00:00
|
|
|
year = untransformDigits( text );
|
2019-09-26 07:06:56 +00:00
|
|
|
break;
|
|
|
|
case 'xkY':
|
|
|
|
// Thai year
|
2023-10-30 08:20:27 +00:00
|
|
|
year = untransformDigits( text ) - 543;
|
2019-09-26 07:06:56 +00:00
|
|
|
break;
|
|
|
|
case 'G':
|
|
|
|
case 'H':
|
2023-10-30 08:20:27 +00:00
|
|
|
hour = untransformDigits( text );
|
2019-09-26 07:06:56 +00:00
|
|
|
break;
|
|
|
|
case 'i':
|
2023-10-30 08:20:27 +00:00
|
|
|
minute = untransformDigits( text );
|
2019-09-26 07:06:56 +00:00
|
|
|
break;
|
2023-06-04 18:16:56 +00:00
|
|
|
case 's':
|
|
|
|
// Seconds - unused, because most timestamp formats omit them
|
|
|
|
break;
|
2019-09-26 07:06:56 +00:00
|
|
|
default:
|
|
|
|
throw new Error( 'Not implemented' );
|
|
|
|
}
|
|
|
|
}
|
2019-10-18 12:12:55 +00:00
|
|
|
// The last matching group is the timezone abbreviation
|
2024-05-24 12:20:50 +00:00
|
|
|
const tzAbbr = tzAbbrs[ match[ match.length - 1 ] ];
|
2019-10-18 12:12:55 +00:00
|
|
|
|
|
|
|
// 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.
|
2024-05-24 12:20:50 +00:00
|
|
|
let date = moment.tz( [ year, monthIdx, day, hour, minute ], localTimezone );
|
2019-09-26 07:06:56 +00:00
|
|
|
|
2019-10-18 12:12:55 +00:00
|
|
|
// But during the "fall back" at the end of DST, some times will happen twice. Per the docs,
|
|
|
|
// "Moment Timezone handles this by always using the earlier instance of a duplicated hour."
|
|
|
|
// https://momentjs.com/timezone/docs/#/using-timezones/parsing-ambiguous-inputs/
|
|
|
|
|
|
|
|
// Since the timezone abbreviation disambiguates the DST/non-DST times, we can detect when
|
|
|
|
// that behavior was incorrect...
|
2024-05-24 12:20:50 +00:00
|
|
|
let dateWarning = null;
|
2019-10-18 12:12:55 +00:00
|
|
|
if ( date.zoneAbbr() !== tzAbbr ) {
|
|
|
|
// ...and force the correct parsing. I can't find proper documentation for this feature,
|
|
|
|
// but this pull request explains it: https://github.com/moment/moment-timezone/pull/101
|
|
|
|
moment.tz.moveAmbiguousForward = true;
|
|
|
|
date = moment.tz( [ year, monthIdx, day, hour, minute ], localTimezone );
|
|
|
|
moment.tz.moveAmbiguousForward = false;
|
|
|
|
if ( date.zoneAbbr() !== tzAbbr ) {
|
|
|
|
// This should not be possible for "genuine" timestamps generated by MediaWiki.
|
|
|
|
// But bots and humans get it wrong when marking up unsigned comments…
|
|
|
|
// https://pl.wikipedia.org/w/index.php?title=Wikipedia:Kawiarenka/Artykuły&diff=prev&oldid=54772606
|
2022-03-03 16:29:39 +00:00
|
|
|
dateWarning = 'Timestamp has timezone abbreviation for the wrong time';
|
2019-10-18 12:12:55 +00:00
|
|
|
} else {
|
2022-03-03 16:29:39 +00:00
|
|
|
dateWarning = 'Ambiguous time at DST switchover was parsed';
|
2019-10-18 12:12:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
if ( !date.isValid() ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-03-03 16:29:39 +00:00
|
|
|
return {
|
|
|
|
date: date,
|
|
|
|
warning: dateWarning
|
|
|
|
};
|
2019-09-26 07:06:56 +00:00
|
|
|
};
|
2020-07-20 21:15:03 +00:00
|
|
|
};
|
2019-09-26 07:06:56 +00:00
|
|
|
|
2019-10-20 16:36:43 +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-10-20 16:36:43 +00:00
|
|
|
*
|
|
|
|
* This calls #getTimestampRegexp with predefined data for the current wiki.
|
|
|
|
*
|
|
|
|
* @private
|
2020-09-03 20:59:33 +00:00
|
|
|
* @return {string[]} Regular expressions
|
2019-10-20 16:36:43 +00:00
|
|
|
*/
|
2020-09-03 20:59:33 +00:00
|
|
|
Parser.prototype.getLocalTimestampRegexps = function () {
|
2024-04-19 22:07:35 +00:00
|
|
|
return Object.keys( this.data.dateFormat ).map( ( contLangVariant ) => this.getTimestampRegexp(
|
|
|
|
contLangVariant,
|
|
|
|
this.data.dateFormat[ contLangVariant ],
|
|
|
|
'[' + this.data.digits[ contLangVariant ].join( '' ) + ']',
|
|
|
|
this.data.timezones[ contLangVariant ]
|
|
|
|
) );
|
2020-07-20 21:15:03 +00:00
|
|
|
};
|
2019-09-26 07:06:56 +00:00
|
|
|
|
2019-10-20 16:36:43 +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 regexps returned by #getLocalTimestampRegexps.
|
2019-10-20 16:36:43 +00:00
|
|
|
*
|
|
|
|
* This calls #getTimestampParser with predefined data for the current wiki.
|
|
|
|
*
|
|
|
|
* @private
|
2020-09-03 20:59:33 +00:00
|
|
|
* @return {TimestampParser[]} Timestamp parser functions
|
2019-10-20 16:36:43 +00:00
|
|
|
*/
|
2020-09-03 20:59:33 +00:00
|
|
|
Parser.prototype.getLocalTimestampParsers = function () {
|
2024-04-19 22:07:35 +00:00
|
|
|
return Object.keys( this.data.dateFormat ).map( ( contLangVariant ) => this.getTimestampParser(
|
|
|
|
contLangVariant,
|
|
|
|
this.data.dateFormat[ contLangVariant ],
|
|
|
|
this.data.digits[ contLangVariant ],
|
|
|
|
this.data.localTimezone,
|
|
|
|
this.data.timezones[ contLangVariant ]
|
|
|
|
) );
|
2020-07-20 21:15:03 +00:00
|
|
|
};
|
2019-09-26 07:06:56 +00:00
|
|
|
|
2019-10-24 16:24:59 +00:00
|
|
|
/**
|
|
|
|
* Callback for document.createTreeWalker that will skip over nodes where we don't want to detect
|
|
|
|
* comments (or section headings).
|
|
|
|
*
|
|
|
|
* @param {Node} node
|
|
|
|
* @return {number} Appropriate NodeFilter constant
|
|
|
|
*/
|
|
|
|
function acceptOnlyNodesAllowingComments( node ) {
|
2022-02-21 18:04:31 +00:00
|
|
|
if ( node instanceof HTMLElement ) {
|
2024-05-24 12:20:50 +00:00
|
|
|
const tagName = node.tagName.toLowerCase();
|
2022-02-21 18:04:31 +00:00
|
|
|
// The table of contents has a heading that gets erroneously detected as a section
|
|
|
|
if ( node.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;
|
|
|
|
}
|
|
|
|
// Don't attempt to parse blocks marked 'mw-notalk'
|
|
|
|
if ( node.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' &&
|
|
|
|
node.classList.contains( 'references' )
|
|
|
|
) {
|
|
|
|
return NodeFilter.FILTER_REJECT;
|
|
|
|
}
|
2021-02-26 20:05:40 +00:00
|
|
|
}
|
2024-05-24 12:20:50 +00:00
|
|
|
const 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 HTMLElement && parentNode.tagName.match( /^h([1-6])$/i ) ) {
|
2020-11-04 15:17:39 +00:00
|
|
|
return NodeFilter.FILTER_REJECT;
|
|
|
|
}
|
2019-10-24 16:24:59 +00:00
|
|
|
return NodeFilter.FILTER_ACCEPT;
|
|
|
|
}
|
|
|
|
|
2019-10-20 16:36:43 +00:00
|
|
|
/**
|
2020-09-03 20:59:33 +00:00
|
|
|
* Find a timestamp in a given text node
|
2019-10-20 16:36:43 +00:00
|
|
|
*
|
2020-07-20 21:15:03 +00:00
|
|
|
* @private
|
2020-07-10 14:16:49 +00:00
|
|
|
* @param {Text} node Text node
|
2020-09-03 20:59:33 +00:00
|
|
|
* @param {string[]} timestampRegexps Timestamp regexps
|
|
|
|
* @return {Object|null} Object with the following keys:
|
|
|
|
* - {number} offset Length of extra text preceding the node that was used for matching
|
|
|
|
* - {number} parserIndex Which of the regexps matched
|
|
|
|
* - {Array} matchData Regexp match data, which specifies the location of the match,
|
|
|
|
* and which can be parsed using #getLocalTimestampParsers
|
2022-10-04 12:50:57 +00:00
|
|
|
* - {Object} range Range-like object covering the timestamp
|
2019-10-20 16:36:43 +00:00
|
|
|
*/
|
2020-09-03 20:59:33 +00:00
|
|
|
Parser.prototype.findTimestamp = function ( node, timestampRegexps ) {
|
2024-05-24 12:43:07 +00:00
|
|
|
let nodeText = '';
|
|
|
|
let offset = 0;
|
|
|
|
// Searched nodes (reverse order)
|
|
|
|
const nodes = [];
|
2022-10-04 12:50:57 +00:00
|
|
|
|
2020-07-10 14:16:49 +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.push( node );
|
2020-07-10 14:16:49 +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
|
|
|
node.previousSibling &&
|
|
|
|
node.previousSibling.nodeType === Node.ELEMENT_NODE &&
|
|
|
|
node.previousSibling.getAttribute( 'typeof' ) === 'mw:Entity'
|
2020-07-10 14:16:49 +00:00
|
|
|
) {
|
2020-08-11 04:22:57 +00:00
|
|
|
nodeText = node.previousSibling.firstChild.nodeValue + nodeText;
|
|
|
|
offset += node.previousSibling.firstChild.nodeValue.length;
|
2022-10-04 12:50:57 +00:00
|
|
|
nodes.push( node.previousSibling.firstChild );
|
2020-07-10 14:16:49 +00:00
|
|
|
|
|
|
|
// If the entity is followed by more text, do this again
|
2020-05-05 20:19:04 +00:00
|
|
|
if (
|
2020-08-11 04:22:57 +00:00
|
|
|
node.previousSibling.previousSibling &&
|
|
|
|
node.previousSibling.previousSibling.nodeType === Node.TEXT_NODE
|
2020-05-05 20:19:04 +00:00
|
|
|
) {
|
2020-08-11 04:22:57 +00:00
|
|
|
offset += node.previousSibling.previousSibling.nodeValue.length;
|
|
|
|
node = node.previousSibling.previousSibling;
|
2020-05-05 20:19:04 +00:00
|
|
|
} else {
|
|
|
|
node = null;
|
|
|
|
}
|
2020-07-10 14:16:49 +00:00
|
|
|
} else {
|
|
|
|
node = null;
|
2019-09-26 07:06:56 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-10 14:16:49 +00:00
|
|
|
|
2024-05-24 12:43:07 +00:00
|
|
|
for ( let i = 0; i < timestampRegexps.length; i++ ) {
|
2020-09-03 20:59:33 +00:00
|
|
|
// Technically, there could be multiple matches in a single text node. However, the ultimate
|
|
|
|
// point of this is to find the signatures which precede the timestamps, and any later
|
|
|
|
// timestamps in the text node can't be directly preceded by a signature (as we require them to
|
|
|
|
// have links), so we only concern ourselves with the first match.
|
2024-05-24 12:43:07 +00:00
|
|
|
const matchData = nodeText.match( timestampRegexps[ i ] );
|
2020-09-03 20:59:33 +00:00
|
|
|
if ( matchData ) {
|
2024-05-24 12:31:30 +00:00
|
|
|
const timestampLength = matchData[ 0 ].length;
|
2022-10-04 12:50:57 +00:00
|
|
|
// Bytes at the end of the last node which aren't part of the match
|
2024-05-24 12:20:50 +00:00
|
|
|
const tailLength = nodeText.length - timestampLength - matchData.index;
|
2022-10-04 12:50:57 +00:00
|
|
|
// 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.
|
2024-05-24 12:31:30 +00:00
|
|
|
let count = -tailLength;
|
2024-05-24 12:20:50 +00:00
|
|
|
const endContainer = nodes[ 0 ];
|
|
|
|
const endOffset = endContainer.nodeValue.length - tailLength;
|
2022-10-04 12:50:57 +00:00
|
|
|
|
2024-05-24 12:31:30 +00:00
|
|
|
let startContainer, startOffset;
|
2024-04-18 18:37:58 +00:00
|
|
|
nodes.some( ( n ) => {
|
2022-10-04 12:50:57 +00:00
|
|
|
count += n.nodeValue.length;
|
|
|
|
// If we have counted to beyond the start of the timestamp, we are in the
|
|
|
|
// start node of the timestamp
|
|
|
|
if ( count >= timestampLength ) {
|
|
|
|
startContainer = n;
|
|
|
|
// Offset is how much we overshot the start by
|
|
|
|
startOffset = count - timestampLength;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
} );
|
|
|
|
|
2024-05-24 12:20:50 +00:00
|
|
|
const range = {
|
2022-10-04 12:50:57 +00:00
|
|
|
startContainer: startContainer,
|
|
|
|
startOffset: startOffset,
|
|
|
|
endContainer: endContainer,
|
|
|
|
endOffset: 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 04:22:57 +00:00
|
|
|
}
|
|
|
|
return null;
|
2020-07-20 21:15:03 +00:00
|
|
|
};
|
2019-09-26 07:06:56 +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.
|
|
|
|
*
|
|
|
|
* @param {HTMLElement} link
|
2022-02-04 18:16:24 +00:00
|
|
|
* @return {Object|null} Object, 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-01-11 15:52:16 +00:00
|
|
|
Parser.prototype.getUsernameFromLink = function ( link ) {
|
2024-05-24 12:20:50 +00:00
|
|
|
let title;
|
2022-01-11 15:52:16 +00:00
|
|
|
// Selflink: use title of current page
|
|
|
|
if ( link.classList.contains( 'mw-selflink' ) ) {
|
|
|
|
title = this.title;
|
|
|
|
} else {
|
2024-05-24 12:20:50 +00:00
|
|
|
const titleString = utils.getTitleFromUrl( link.href ) || '';
|
2023-10-30 16:55:42 +00:00
|
|
|
// Performance optimization, skip strings that obviously don't contain a namespace
|
|
|
|
if ( !titleString || titleString.indexOf( ':' ) === -1 ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
title = mw.Title.newFromText( titleString );
|
2022-01-11 15:52:16 +00:00
|
|
|
}
|
2021-01-26 18:58:58 +00:00
|
|
|
if ( !title ) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-02-04 15:50:20 +00:00
|
|
|
|
2024-05-24 12:20:50 +00:00
|
|
|
let username;
|
|
|
|
let displayName = null;
|
|
|
|
const namespaceId = title.getNamespaceId();
|
|
|
|
const mainText = title.getMainText();
|
|
|
|
const namespaceIds = mw.config.get( 'wgNamespaceIds' );
|
2022-02-04 15:50:20 +00:00
|
|
|
|
2021-01-26 18:58:58 +00:00
|
|
|
if (
|
2022-02-04 15:50:20 +00:00
|
|
|
namespaceId === namespaceIds.user ||
|
|
|
|
namespaceId === namespaceIds.user_talk
|
2021-01-26 18:58:58 +00:00
|
|
|
) {
|
2022-02-04 15:50:20 +00:00
|
|
|
username = mainText;
|
2021-01-26 18:58:58 +00:00
|
|
|
if ( username.indexOf( '/' ) !== -1 ) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-02-04 18:16:24 +00:00
|
|
|
if ( namespaceId === namespaceIds.user ) {
|
|
|
|
// Use regex trim for consistency with PHP implementation
|
2024-05-24 12:20:50 +00:00
|
|
|
const text = link.textContent.replace( /^[\s]+/, '' ).replace( /[\s]+$/, '' );
|
2022-02-04 18:16:24 +00:00
|
|
|
// Record the display name if it has been customised beyond changing case
|
|
|
|
if ( text && text.toLowerCase() !== username.toLowerCase() ) {
|
|
|
|
displayName = text;
|
|
|
|
}
|
|
|
|
}
|
2022-02-21 22:07:38 +00:00
|
|
|
} else if ( namespaceId === namespaceIds.special ) {
|
2024-05-24 12:20:50 +00:00
|
|
|
const parts = mainText.split( '/' );
|
2022-02-21 22:07:38 +00:00
|
|
|
if ( parts.length === 2 && parts[ 0 ] === this.data.specialContributionsName ) {
|
|
|
|
// Normalize the username: users may link to their contributions with an unnormalized name
|
2024-05-24 12:20:50 +00:00
|
|
|
const userpage = mw.Title.makeTitle( namespaceIds.user, parts[ 1 ] );
|
2022-02-21 22:07:38 +00:00
|
|
|
if ( !userpage ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
username = userpage.getMainText();
|
2021-01-26 18:58:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( !username ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if ( mw.util.isIPv6Address( username ) ) {
|
|
|
|
// Bot-generated links "Preceding unsigned comment added by" have non-standard case
|
|
|
|
username = username.toUpperCase();
|
|
|
|
}
|
2022-02-04 18:16:24 +00:00
|
|
|
return {
|
|
|
|
username: username,
|
|
|
|
displayName: displayName
|
|
|
|
};
|
2022-01-11 15:52:16 +00:00
|
|
|
};
|
2021-01-26 18:58:58 +00:00
|
|
|
|
2019-10-20 16:36:43 +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.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @param {Text} timestampNode Text node
|
2020-03-06 16:51:59 +00:00
|
|
|
* @param {Node} [until] Node to stop searching at
|
2022-02-04 15:44:14 +00:00
|
|
|
* @return {Object} Result, an object 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-10-20 16:36:43 +00:00
|
|
|
*/
|
2020-07-20 21:15:03 +00:00
|
|
|
Parser.prototype.findSignature = function ( timestampNode, until ) {
|
2024-05-24 12:20:50 +00:00
|
|
|
let sigUsername = null;
|
|
|
|
let sigDisplayName = null;
|
|
|
|
let length = 0;
|
|
|
|
let lastLinkNode = timestampNode;
|
2020-05-27 17:42:19 +00:00
|
|
|
|
2021-01-26 18:58:58 +00:00
|
|
|
utils.linearWalkBackwards(
|
|
|
|
timestampNode,
|
2024-04-18 18:37:58 +00:00
|
|
|
( event, node ) => {
|
2021-01-26 18:58:58 +00:00
|
|
|
if ( event === 'enter' && node === until ) {
|
|
|
|
return true;
|
2019-09-26 07:06:56 +00:00
|
|
|
}
|
2022-02-21 21:42:14 +00:00
|
|
|
if ( length >= SIGNATURE_SCAN_LIMIT ) {
|
2021-01-26 18:58:58 +00:00
|
|
|
return true;
|
2019-09-26 07:06:56 +00:00
|
|
|
}
|
2021-01-26 18:58:58 +00:00
|
|
|
if ( utils.isBlockElement( node ) ) {
|
|
|
|
// Don't allow reaching into preceding paragraphs
|
|
|
|
return true;
|
2019-09-26 07:06:56 +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.nodeType === Node.TEXT_NODE ?
|
|
|
|
codePointLength( utils.htmlTrim( node.textContent ) ) : 0;
|
2019-09-26 07:06:56 +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.
|
|
|
|
if ( event === 'leave' && node.nodeType === Node.ELEMENT_NODE && node.tagName.toLowerCase() === 'a' ) {
|
2024-01-30 10:28:36 +00:00
|
|
|
if ( !node.classList.contains( 'ext-discussiontools-init-timestamplink' ) ) {
|
2024-05-24 12:20:50 +00:00
|
|
|
const user = this.getUsernameFromLink( node );
|
2024-01-30 10:28:36 +00:00
|
|
|
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-09-26 07:06:56 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-26 18:58:58 +00:00
|
|
|
);
|
|
|
|
|
2024-05-24 12:20:50 +00:00
|
|
|
const range = {
|
2021-01-26 18:58:58 +00:00
|
|
|
startContainer: lastLinkNode.parentNode,
|
|
|
|
startOffset: utils.childIndexOf( lastLinkNode ),
|
|
|
|
endContainer: timestampNode.parentNode,
|
|
|
|
endOffset: utils.childIndexOf( timestampNode ) + 1
|
|
|
|
};
|
2024-05-24 12:20:50 +00:00
|
|
|
const nativeRange = ThreadItem.prototype.getRange.call( { range: range } );
|
2021-01-26 18:58:58 +00:00
|
|
|
|
|
|
|
// 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...
|
2024-05-24 12:20:50 +00:00
|
|
|
const sigNodes = utils.getCoveredSiblings( nativeRange ).reverse();
|
2021-01-26 18:58:58 +00:00
|
|
|
|
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
|
|
|
};
|
2020-07-20 21:15:03 +00:00
|
|
|
};
|
2019-09-26 07:06:56 +00:00
|
|
|
|
2019-10-20 16:36:43 +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-10-20 16:36:43 +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-10-20 16:36:43 +00:00
|
|
|
*
|
|
|
|
* @private
|
2024-02-08 15:19:53 +00:00
|
|
|
* @param {Node|null} node Node after which to start searching
|
|
|
|
* (if null, start at the beginning of the document).
|
2020-06-04 18:48:58 +00:00
|
|
|
* @return {Node}
|
2019-10-20 16:36:43 +00:00
|
|
|
*/
|
2020-07-20 21:15:03 +00:00
|
|
|
Parser.prototype.nextInterestingLeafNode = function ( node ) {
|
2024-05-24 12:20:50 +00:00
|
|
|
const rootNode = this.rootNode;
|
2020-07-20 21:15:03 +00:00
|
|
|
|
2024-05-24 12:20:50 +00:00
|
|
|
const treeWalker = rootNode.ownerDocument.createTreeWalker(
|
2019-09-26 07:06:56 +00:00
|
|
|
rootNode,
|
|
|
|
// eslint-disable-next-line no-bitwise
|
|
|
|
NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_TEXT,
|
2024-04-18 18:37:58 +00:00
|
|
|
( n ) => {
|
2024-02-08 15:19:53 +00:00
|
|
|
// Skip past the starting node and its descendants
|
|
|
|
if ( n === node || n.parentNode === node ) {
|
2019-09-26 07:06:56 +00:00
|
|
|
return NodeFilter.FILTER_REJECT;
|
|
|
|
}
|
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 ( utils.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 ( utils.isRenderingTransparentNode( n ) ) {
|
|
|
|
return NodeFilter.FILTER_REJECT;
|
|
|
|
}
|
2021-12-04 02:06:57 +00:00
|
|
|
if ( utils.isCommentContent( n ) ) {
|
2019-09-26 07:06:56 +00:00
|
|
|
return NodeFilter.FILTER_ACCEPT;
|
|
|
|
}
|
|
|
|
return NodeFilter.FILTER_SKIP;
|
|
|
|
},
|
|
|
|
false
|
|
|
|
);
|
2024-02-08 15:19:53 +00:00
|
|
|
if ( node ) {
|
|
|
|
treeWalker.currentNode = node;
|
|
|
|
}
|
2019-09-26 07:06:56 +00:00
|
|
|
treeWalker.nextNode();
|
2020-06-04 18:48:58 +00:00
|
|
|
if ( !treeWalker.currentNode ) {
|
|
|
|
throw new Error( 'nextInterestingLeafNode not found' );
|
|
|
|
}
|
2019-09-26 07:06:56 +00:00
|
|
|
return treeWalker.currentNode;
|
2020-07-20 21:15:03 +00:00
|
|
|
};
|
2019-09-26 07:06:56 +00:00
|
|
|
|
2021-02-26 20:57:22 +00:00
|
|
|
/**
|
|
|
|
* @param {Node[]} sigNodes
|
|
|
|
* @param {Object} match
|
|
|
|
* @param {Text} node
|
2022-10-04 12:50:57 +00:00
|
|
|
* @return {Object} Range-like object
|
2021-02-26 20:57:22 +00:00
|
|
|
*/
|
|
|
|
function adjustSigRange( sigNodes, match, node ) {
|
2024-05-24 12:20:50 +00:00
|
|
|
const firstSigNode = sigNodes[ sigNodes.length - 1 ];
|
|
|
|
const lastSigNode = sigNodes[ 0 ];
|
2021-02-26 20:57:22 +00:00
|
|
|
|
|
|
|
// TODO Document why this needs to be so complicated
|
2024-05-24 12:20:50 +00:00
|
|
|
const lastSigNodeOffset = lastSigNode === node ?
|
2021-02-26 20:57:22 +00:00
|
|
|
match.matchData.index + match.matchData[ 0 ].length - match.offset :
|
|
|
|
utils.childIndexOf( lastSigNode ) + 1;
|
2024-05-24 12:20:50 +00:00
|
|
|
const sigRange = {
|
2021-02-26 20:57:22 +00:00
|
|
|
startContainer: firstSigNode.parentNode,
|
|
|
|
startOffset: utils.childIndexOf( firstSigNode ),
|
|
|
|
endContainer: lastSigNode === node ? node : lastSigNode.parentNode,
|
|
|
|
endOffset: lastSigNodeOffset
|
|
|
|
};
|
|
|
|
return sigRange;
|
|
|
|
}
|
|
|
|
|
2022-02-19 06:31:34 +00:00
|
|
|
/**
|
|
|
|
* @return {ThreadItemSet}
|
|
|
|
*/
|
2020-07-20 21:15:03 +00:00
|
|
|
Parser.prototype.buildThreadItems = function () {
|
2024-05-24 12:20:50 +00:00
|
|
|
const result = new ThreadItemSet();
|
2022-02-19 06:31:34 +00:00
|
|
|
|
2024-05-24 12:20:50 +00:00
|
|
|
const
|
2020-09-03 20:59:33 +00:00
|
|
|
dfParsers = this.getLocalTimestampParsers(),
|
2022-02-19 06:31:34 +00:00
|
|
|
timestampRegexps = this.getLocalTimestampRegexps();
|
2019-09-26 07:06:56 +00:00
|
|
|
|
2024-05-24 12:20:50 +00:00
|
|
|
const treeWalker = this.rootNode.ownerDocument.createTreeWalker(
|
2020-07-20 21:15:03 +00:00
|
|
|
this.rootNode,
|
2019-09-26 07:06:56 +00:00
|
|
|
// eslint-disable-next-line no-bitwise
|
|
|
|
NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_TEXT,
|
2019-10-24 16:24:59 +00:00
|
|
|
acceptOnlyNodesAllowingComments,
|
2019-09-26 07:06:56 +00:00
|
|
|
false
|
|
|
|
);
|
|
|
|
|
2024-05-24 12:20:50 +00:00
|
|
|
let curComment, range;
|
|
|
|
let curCommentEnd = null;
|
2020-01-28 23:35:26 +00:00
|
|
|
|
2024-05-24 12:20:50 +00:00
|
|
|
let node;
|
2019-09-26 07:06:56 +00:00
|
|
|
while ( ( node = treeWalker.nextNode() ) ) {
|
2024-05-24 12:31:30 +00:00
|
|
|
let match;
|
2020-10-01 19:36:11 +00:00
|
|
|
if ( node.tagName && ( match = node.tagName.match( /^h([1-6])$/i ) ) ) {
|
2024-05-24 12:20:50 +00:00
|
|
|
const headingNode = utils.getHeadlineNode( node );
|
2020-02-05 18:30:21 +00:00
|
|
|
range = {
|
2020-10-27 12:18:36 +00:00
|
|
|
startContainer: headingNode,
|
2024-04-19 22:34:32 +00:00
|
|
|
startOffset: 0,
|
2020-10-27 12:18:36 +00:00
|
|
|
endContainer: headingNode,
|
|
|
|
endOffset: headingNode.childNodes.length
|
2020-02-05 18:30:21 +00:00
|
|
|
};
|
2020-10-01 19:36:11 +00:00
|
|
|
curComment = new HeadingItem( range, +match[ 1 ] );
|
2020-07-29 23:57:51 +00:00
|
|
|
curComment.rootNode = this.rootNode;
|
2022-02-19 06:31:34 +00:00
|
|
|
result.addThreadItem( curComment );
|
2020-11-16 00:35:51 +00:00
|
|
|
curCommentEnd = node;
|
2020-09-03 20:59:33 +00:00
|
|
|
} else if ( node.nodeType === Node.TEXT_NODE && ( match = this.findTimestamp( node, timestampRegexps ) ) ) {
|
2024-05-24 12:20:50 +00:00
|
|
|
const warnings = [];
|
|
|
|
const foundSignature = this.findSignature( node, curCommentEnd );
|
|
|
|
const author = foundSignature.username;
|
Only detect comments with real signatures
Consequences of this are visible in the test cases:
* (en) Tech News posts are not detected.
Examples: "21:22, 1 July 2019 (UTC)", "21:42, 29 July 2019 (UTC)"
* (en) Comments by users who customize the timestamp are not detected.
Examples: "10:49, 28 June 2019 (UTC)", "21:34, 14 July 2019 (UTC)"
* (en) Comments with signatures missing a username are not detected.
This sometimes happens if a comment is accidentally signed with
'~~~~~' (five tildes), which only inserts the timestamp.
Examples: "17:17, 27 July 2019 (UTC)", "10:25, 29 July 2019 (UTC)"
* (pl) A lone timestamp at the beginning of a thread is not detected.
It's not part of a post, it was added to aid automatic archiving.
Example: "21:03, 18 paź 2018 (CET)"
Bug: T245692
Change-Id: I0767bb239a1800f2e538917b5995fc4f0fa4d043
2020-02-20 22:06: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;
|
|
|
|
}
|
|
|
|
|
2024-05-24 12:31:30 +00:00
|
|
|
const sigRanges = [];
|
|
|
|
const timestampRanges = [];
|
2022-10-04 12:50:57 +00:00
|
|
|
|
2022-02-04 15:44:14 +00:00
|
|
|
sigRanges.push( adjustSigRange( foundSignature.nodes, match, node ) );
|
2022-10-04 12:50:57 +00:00
|
|
|
timestampRanges.push( match.range );
|
2019-09-26 07:06:56 +00:00
|
|
|
|
2020-08-11 04:22:55 +00:00
|
|
|
// Everything from the last comment up to here is the next comment
|
2024-05-24 12:20:50 +00:00
|
|
|
const startNode = this.nextInterestingLeafNode( curCommentEnd );
|
2024-05-24 12:31:30 +00:00
|
|
|
let 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
|
|
|
utils.linearWalk(
|
2024-02-08 15:19:53 +00:00
|
|
|
endNode,
|
2021-01-08 19:20:33 +00:00
|
|
|
// eslint-disable-next-line no-loop-func
|
2024-04-18 18:37:58 +00:00
|
|
|
( event, n ) => {
|
2024-05-24 12:20:50 +00:00
|
|
|
let match2, foundSignature2;
|
2022-11-30 16:02:39 +00:00
|
|
|
if ( utils.isBlockElement( n ) || utils.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' &&
|
|
|
|
n.nodeType === Node.TEXT_NODE && n !== node &&
|
|
|
|
( 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.push( adjustSigRange( foundSignature2.nodes, match2, n ) );
|
2022-10-04 12:50:57 +00:00
|
|
|
timestampRanges.push( 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
|
|
|
}
|
2024-04-18 18:37:58 +00:00
|
|
|
}
|
2021-01-08 19:20:33 +00:00
|
|
|
);
|
2020-08-11 04:22:55 +00:00
|
|
|
|
2024-05-24 12:20:50 +00:00
|
|
|
const length = endNode.nodeType === Node.TEXT_NODE ?
|
2020-11-24 20:58:34 +00:00
|
|
|
endNode.textContent.replace( /[\t\n\f\r ]+$/, '' ).length :
|
|
|
|
endNode.childNodes.length;
|
|
|
|
range = {
|
|
|
|
startContainer: startNode.parentNode,
|
|
|
|
startOffset: utils.childIndexOf( startNode ),
|
|
|
|
endContainer: endNode,
|
|
|
|
endOffset: length
|
|
|
|
};
|
2020-08-11 04:22:55 +00:00
|
|
|
|
2024-05-24 12:20:50 +00:00
|
|
|
const startLevel = utils.getIndentLevel( startNode, this.rootNode ) + 1;
|
|
|
|
const endLevel = utils.getIndentLevel( node, this.rootNode ) + 1;
|
2019-09-26 07:06:56 +00:00
|
|
|
if ( startLevel !== endLevel ) {
|
2020-05-13 18:23:48 +00:00
|
|
|
warnings.push( 'Comment starts and ends with different indentation' );
|
2019-09-26 07:06:56 +00:00
|
|
|
}
|
2020-08-20 18:38:25 +00:00
|
|
|
// Should this use the indent level of `startNode` or `node`?
|
2024-05-24 12:20:50 +00:00
|
|
|
const level = Math.min( startLevel, endLevel );
|
2020-08-20 18:38:25 +00:00
|
|
|
|
2024-05-24 12:20:50 +00:00
|
|
|
const parserResult = dfParsers[ match.parserIndex ]( match.matchData );
|
2023-11-30 17:37:38 +00:00
|
|
|
if ( !parserResult ) {
|
|
|
|
continue;
|
|
|
|
}
|
2024-05-24 12:20:50 +00:00
|
|
|
const dateTime = parserResult.date;
|
2022-03-03 16:29:39 +00:00
|
|
|
if ( parserResult.warning ) {
|
|
|
|
warnings.push( parserResult.warning );
|
2020-08-20 18:38:25 +00:00
|
|
|
}
|
2019-09-26 07:06:56 +00:00
|
|
|
|
2020-05-22 16:26:05 +00:00
|
|
|
curComment = new CommentItem(
|
2020-08-20 18:38:25 +00:00
|
|
|
level,
|
2020-05-22 16:26:05 +00:00
|
|
|
range,
|
2021-02-26 20:57:22 +00:00
|
|
|
sigRanges,
|
2022-10-04 12:50:57 +00:00
|
|
|
timestampRanges,
|
2020-05-22 16:26:05 +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.rootNode = this.rootNode;
|
2020-05-13 18:23:48 +00:00
|
|
|
if ( warnings.length ) {
|
|
|
|
curComment.warnings = warnings;
|
|
|
|
}
|
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 = {
|
|
|
|
startContainer: this.rootNode,
|
|
|
|
startOffset: 0,
|
|
|
|
endContainer: this.rootNode,
|
|
|
|
endOffset: 0
|
|
|
|
};
|
2024-05-24 12:20:50 +00:00
|
|
|
const fakeHeading = new HeadingItem( range, null );
|
2022-02-19 06:31:34 +00:00
|
|
|
fakeHeading.rootNode = this.rootNode;
|
|
|
|
result.addThreadItem( fakeHeading );
|
|
|
|
}
|
|
|
|
result.addThreadItem( curComment );
|
2020-11-16 00:35:51 +00:00
|
|
|
curCommentEnd = curComment.range.endContainer;
|
2019-09-26 07:06:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-19 06:31:34 +00:00
|
|
|
return result;
|
2020-07-20 21:15:03 +00:00
|
|
|
};
|
|
|
|
|
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
|
|
|
|
* @return {string} Truncated text
|
|
|
|
*/
|
|
|
|
Parser.prototype.truncateForId = function ( text ) {
|
2024-02-02 16:06:30 +00:00
|
|
|
return trimByteLength( '', text, 80 ).newVal.replace( /^_+|_+$/g, '' );
|
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.
|
|
|
|
*
|
|
|
|
* @param {ThreadItem} threadItem
|
2022-02-19 06:31:34 +00:00
|
|
|
* @param {ThreadItemSet} previousItems
|
2021-02-12 23:02:51 +00:00
|
|
|
* @return {string}
|
2020-10-21 15:52:04 +00:00
|
|
|
*/
|
2022-02-19 06:31:34 +00:00
|
|
|
Parser.prototype.computeId = function ( threadItem, previousItems ) {
|
2024-05-24 12:20:50 +00:00
|
|
|
let id, headline;
|
2020-10-21 15:52:04 +00:00
|
|
|
|
2020-09-22 23:05:25 +00:00
|
|
|
if ( threadItem instanceof HeadingItem && threadItem.placeholderHeading ) {
|
|
|
|
// 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-';
|
2020-09-22 23:05:25 +00:00
|
|
|
} else if ( threadItem instanceof HeadingItem ) {
|
2022-02-22 09:36:45 +00:00
|
|
|
headline = threadItem.range.startContainer;
|
2021-03-17 14:39:41 +00:00
|
|
|
id = 'h-' + this.truncateForId( headline.getAttribute( 'id' ) || '' );
|
2020-09-22 23:05:25 +00:00
|
|
|
} else if ( threadItem instanceof CommentItem ) {
|
2022-03-24 16:48:25 +00:00
|
|
|
id = 'c-' + this.truncateForId( threadItem.author || '' ).replace( / /g, '_' ) + '-' + threadItem.getTimestampString();
|
2020-10-01 20:13:47 +00:00
|
|
|
} else {
|
|
|
|
throw new Error( 'Unknown ThreadItem type' );
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2024-05-24 12:20:50 +00:00
|
|
|
const threadItemParent = threadItem.parent;
|
2020-10-01 20:13:47 +00:00
|
|
|
if ( threadItemParent instanceof HeadingItem && !threadItemParent.placeholderHeading ) {
|
2022-02-22 09:36:45 +00:00
|
|
|
headline = threadItemParent.range.startContainer;
|
2021-03-17 14:39:41 +00:00
|
|
|
id += '-' + this.truncateForId( headline.getAttribute( 'id' ) || '' );
|
2020-10-01 20:13:47 +00:00
|
|
|
} else if ( threadItemParent instanceof CommentItem ) {
|
2022-03-24 16:48:25 +00:00
|
|
|
id += '-' + this.truncateForId( threadItemParent.author || '' ).replace( / /g, '_' ) + '-' + threadItemParent.getTimestampString();
|
2020-10-01 20:13:47 +00:00
|
|
|
}
|
2020-10-21 15:52:04 +00:00
|
|
|
|
2020-10-07 15:48:20 +00:00
|
|
|
if ( threadItem instanceof HeadingItem ) {
|
|
|
|
// 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.
|
2024-05-24 12:20:50 +00:00
|
|
|
const oldestComment = threadItem.getOldestReply();
|
2021-02-22 20:31:15 +00:00
|
|
|
if ( oldestComment ) {
|
2022-03-24 16:48:25 +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
|
|
|
|
threadItem.warnings.push( 'Duplicate comment ID' );
|
|
|
|
// Finally, disambiguate by adding sequential numbers, to allow replying to both comments
|
2024-05-24 12:20:50 +00:00
|
|
|
let 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.
|
|
|
|
*
|
|
|
|
* @param {ThreadItem} threadItem
|
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
Parser.prototype.computeName = function ( threadItem ) {
|
2024-05-24 12:20:50 +00:00
|
|
|
let name, mainComment;
|
2021-02-12 19:16:13 +00:00
|
|
|
|
|
|
|
if ( threadItem instanceof HeadingItem ) {
|
|
|
|
name = 'h-';
|
2022-07-05 23:21:34 +00:00
|
|
|
mainComment = threadItem.getOldestReply();
|
2021-02-12 19:16:13 +00:00
|
|
|
} else if ( threadItem instanceof CommentItem ) {
|
|
|
|
name = 'c-';
|
|
|
|
mainComment = threadItem;
|
|
|
|
} else {
|
|
|
|
throw new Error( 'Unknown ThreadItem type' );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( mainComment ) {
|
|
|
|
name += this.truncateForId( mainComment.author || '' ).replace( / /g, '_' ) +
|
2022-03-24 16:48:25 +00:00
|
|
|
'-' + mainComment.getTimestampString();
|
2021-02-12 19:16:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return name;
|
|
|
|
};
|
|
|
|
|
2022-02-19 06:31:34 +00:00
|
|
|
/**
|
|
|
|
* @param {ThreadItemSet} result
|
|
|
|
*/
|
|
|
|
Parser.prototype.buildThreads = function ( result ) {
|
2024-05-24 12:20:50 +00:00
|
|
|
let lastHeading = null;
|
|
|
|
const replies = [];
|
2021-04-08 13:46:09 +00:00
|
|
|
|
2024-05-24 12:33:02 +00:00
|
|
|
for ( let i = 0; i < result.threadItems.length; i++ ) {
|
|
|
|
const threadItem = result.threadItems[ i ];
|
2019-11-24 15:39:52 +00:00
|
|
|
|
2020-07-20 21:15:03 +00:00
|
|
|
if ( replies.length < threadItem.level ) {
|
2019-09-26 07:06:56 +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.warnings.push( 'Comment skips indentation level' );
|
|
|
|
while ( replies.length < threadItem.level ) {
|
2019-09-26 07:06:56 +00:00
|
|
|
replies[ replies.length ] = replies[ replies.length - 1 ];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-20 21:15:03 +00:00
|
|
|
if ( threadItem instanceof HeadingItem ) {
|
2020-05-12 23:21:02 +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.
|
2024-05-24 12:20:50 +00:00
|
|
|
let maybeParent = lastHeading;
|
2020-10-01 19:36:11 +00:00
|
|
|
while ( maybeParent && maybeParent.headingLevel >= threadItem.headingLevel ) {
|
|
|
|
maybeParent = maybeParent.parent;
|
|
|
|
}
|
|
|
|
if ( maybeParent ) {
|
|
|
|
threadItem.parent = maybeParent;
|
|
|
|
maybeParent.replies.push( threadItem );
|
|
|
|
}
|
2022-02-19 06:31:34 +00:00
|
|
|
lastHeading = threadItem;
|
2020-07-20 21:15:03 +00:00
|
|
|
} else if ( replies[ threadItem.level - 1 ] ) {
|
2020-05-12 23:21:02 +00:00
|
|
|
// Add as a reply to the closest less-nested comment
|
2020-07-20 21:15:03 +00:00
|
|
|
threadItem.parent = replies[ threadItem.level - 1 ];
|
|
|
|
threadItem.parent.replies.push( threadItem );
|
2019-09-26 07:06:56 +00:00
|
|
|
} else {
|
2020-07-20 21:15:03 +00:00
|
|
|
threadItem.warnings.push( 'Comment could not be connected to a thread' );
|
2019-09-26 07:06:56 +00:00
|
|
|
}
|
|
|
|
|
2020-07-20 21:15:03 +00:00
|
|
|
replies[ threadItem.level ] = threadItem;
|
2020-05-12 23:21:02 +00:00
|
|
|
// Cut off more deeply nested replies
|
2020-07-20 21:15:03 +00:00
|
|
|
replies.length = threadItem.level + 1;
|
2020-10-07 15:48:20 +00:00
|
|
|
}
|
2022-02-19 06:31:34 +00:00
|
|
|
};
|
2020-10-07 15:48:20 +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.
|
|
|
|
*
|
|
|
|
* @param {ThreadItemSet} result
|
|
|
|
*/
|
|
|
|
Parser.prototype.computeIdsAndNames = function ( result ) {
|
2024-05-24 12:20:50 +00:00
|
|
|
let i, threadItem;
|
2022-02-19 06:31:34 +00:00
|
|
|
for ( i = 0; i < result.threadItems.length; i++ ) {
|
|
|
|
threadItem = result.threadItems[ i ];
|
2020-10-01 20:13:47 +00:00
|
|
|
|
2024-05-24 12:20:50 +00:00
|
|
|
const name = this.computeName( threadItem );
|
2021-02-12 19:16:13 +00:00
|
|
|
threadItem.name = name;
|
|
|
|
|
2024-05-24 12:20:50 +00:00
|
|
|
const id = this.computeId( threadItem, result );
|
2020-10-01 20:13:47 +00:00
|
|
|
threadItem.id = id;
|
2022-02-19 06:31:34 +00:00
|
|
|
|
|
|
|
result.updateIdAndNameMaps( threadItem );
|
2019-09-26 07:06:56 +00:00
|
|
|
}
|
2020-10-07 15:48:20 +00:00
|
|
|
};
|
2019-09-26 07:06:56 +00:00
|
|
|
|
2020-10-07 15:48:20 +00:00
|
|
|
/**
|
|
|
|
* @param {ThreadItem} threadItem
|
2021-02-22 20:31:15 +00:00
|
|
|
* @return {CommentItem|null}
|
2020-10-07 15:48:20 +00:00
|
|
|
*/
|
2021-02-22 20:31:15 +00:00
|
|
|
Parser.prototype.getThreadStartComment = function ( threadItem ) {
|
2024-05-24 12:20:50 +00:00
|
|
|
let oldest = null;
|
2020-10-07 15:48:20 +00:00
|
|
|
if ( threadItem instanceof CommentItem ) {
|
2021-02-22 20:31:15 +00:00
|
|
|
oldest = threadItem;
|
2020-10-07 15:48:20 +00:00
|
|
|
}
|
|
|
|
// Check all replies. This can't just use the first comment because threads are often summarized
|
|
|
|
// at the top when the discussion is closed.
|
2024-05-24 12:20:50 +00:00
|
|
|
for ( let i = 0; i < threadItem.replies.length; i++ ) {
|
|
|
|
const comment = threadItem.replies[ i ];
|
2020-10-07 15:48:20 +00:00
|
|
|
// Don't include sub-threads to avoid changing the ID when threads are "merged".
|
|
|
|
if ( comment instanceof CommentItem ) {
|
2024-05-24 12:20:50 +00:00
|
|
|
const oldestInReplies = this.getThreadStartComment( comment );
|
2021-02-22 20:31:15 +00:00
|
|
|
if ( !oldest || oldestInReplies.timestamp.isBefore( oldest.timestamp ) ) {
|
|
|
|
oldest = oldestInReplies;
|
2020-10-07 15:48:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-22 20:31:15 +00:00
|
|
|
return oldest;
|
2019-09-26 07:06:56 +00:00
|
|
|
};
|
2020-07-20 21:15:03 +00:00
|
|
|
|
|
|
|
module.exports = Parser;
|