11export class UnblockReview {
22 constructor ( ) {
33 this . SIGNATURE = '~~~~' ;
4+ this . templatesThatHaveUsernameParameters = [ 'unblock-un' , 'unblock-spamun' ] ;
45 }
56
67 /**
@@ -12,10 +13,10 @@ export class UnblockReview {
1213 * @param {string } acceptDeclineReason - The reason for accepting or declining the unblock request.
1314 * @param {string } DEFAULT_DECLINE_REASON - The default reason for declining the unblock request.
1415 * @param {string } acceptOrDecline - Either "accept" or "decline".
15- * @param {boolean } isSpamUn - Whether the request includes a separate username parameter.
16+ * @param {boolean } hasUsernameParameter - Whether the request includes a separate username parameter.
1617 * @return {string } wikitext
1718 */
18- processAcceptOrDecline ( wikitext , paramsAndReason , acceptDeclineReason , DEFAULT_DECLINE_REASON , acceptOrDecline , isSpamUn = false ) {
19+ processAcceptOrDecline ( wikitext , paramsAndReason , acceptDeclineReason , DEFAULT_DECLINE_REASON , acceptOrDecline , hasUsernameParameter = false ) {
1920 // HTML does one line break and wikitext does 2ish. Cut off all text after the first line break to avoid breaking our search algorithm.
2021 paramsAndReason = paramsAndReason . split ( '\n' ) [ 0 ] ;
2122
@@ -32,8 +33,8 @@ export class UnblockReview {
3233 }
3334 initialText = match [ 1 ] ;
3435 paramsAndReason = '' ;
35- } else {
36- initialText = this . getLeftHalfOfUnblockTemplate ( wikitext , paramsAndReason , isSpamUn ) ;
36+ } else if ( ! hasUsernameParameter ) {
37+ initialText = this . getLeftHalfOfUnblockTemplate ( wikitext , paramsAndReason ) ;
3738 }
3839
3940 // if reason is blank, use the default decline reason
@@ -54,26 +55,34 @@ export class UnblockReview {
5455 acceptDeclineReason = this . SIGNATURE ;
5556 }
5657
57- const regEx = new RegExp (
58- this . escapeRegExp ( initialText ) + '(' + this . getAppealReasonPattern ( paramsAndReason , isSpamUn ) + ')' ,
59- 'g'
60- ) ;
61- const nowikiRanges = this . getNowikiRanges ( wikitext ) ;
62- const matches = [ ...wikitext . matchAll ( regEx ) ] . filter (
63- ( match ) => ! this . isInsideNowiki ( match . index , nowikiRanges )
64- ) ;
58+ let matches ;
59+ if ( hasUsernameParameter ) {
60+ matches = this . getUsernameRequestMatches ( wikitext , paramsAndReason ) ;
61+ } else {
62+ const regEx = new RegExp (
63+ this . escapeRegExp ( initialText ) + '(' + this . getAppealReasonPattern ( paramsAndReason ) + ')' ,
64+ 'g'
65+ ) ;
66+ const nowikiRanges = this . getNowikiRanges ( wikitext ) ;
67+ matches = [ ...wikitext . matchAll ( regEx ) ] . filter (
68+ ( match ) => ! this . isInsideNowiki ( match . index , nowikiRanges )
69+ ) ;
70+ }
6571 if ( matches . length > 1 ) {
6672 throw new Error ( 'Too many matching unblock templates found' ) ;
6773 }
6874 if ( matches . length === 0 ) {
6975 throw new Error ( 'Replacing text with unblock message failed' ) ;
7076 }
7177
72- // If the match starts inside {{u|...}}, unwrap it without assuming how other templates render.
73- const templateName = initialText . match ( / ^ \{ \{ ( [ A - Z a - z - ] + ) / i ) [ 1 ] ;
7478 const match = matches [ 0 ] ;
75- let matchedReason = match [ 1 ] ;
79+ if ( hasUsernameParameter ) {
80+ initialText = match [ 1 ] ;
81+ }
82+ const templateName = initialText . match ( / ^ \{ \{ ( [ A - Z a - z - ] + ) / i ) [ 1 ] ;
83+ let matchedReason = hasUsernameParameter ? match [ 3 ] : match [ 1 ] ;
7684 let remainingText = wikitext . slice ( match . index + match [ 0 ] . length ) ;
85+ // If the match starts inside {{u|...}}, unwrap it without assuming how other templates render.
7786 const nestedStart = initialText . lastIndexOf ( '{{' ) ;
7887 if ( nestedStart > 0 && nestedStart > initialText . lastIndexOf ( '}}' ) ) {
7988 // The opening markup is in initialText, so remove its matching closing braces as well.
@@ -94,12 +103,23 @@ export class UnblockReview {
94103 // Always add 1=. That way if there's an unescaped equals sign after it, it won't break things.
95104 let replacement = '{{' + templateName + ' reviewed|' + acceptOrDecline + '=' + acceptDeclineReason + '|1=' + matchedReason ;
96105
97- // {{Unblock-spamun}} has an extra parameter for the new username. Example:
98- // {{Unblock-spamun|NewUsername|Your reason here}}
99- // So the output need to have a 2= instead of a 1=. Example:
100- // {{Unblock-spamun reviewed|accept=I accept. ~~~~|NewUsername|2=Your reason here}}
101- const hasExtraUsernameParameter = templateName . toLowerCase ( ) === 'unblock-spamun' ;
102- if ( hasExtraUsernameParameter ) {
106+ if ( hasUsernameParameter ) {
107+ const username = paramsAndReason . slice ( 0 , paramsAndReason . indexOf ( '|' ) ) ;
108+ const usernamePattern = this . getAppealReasonPattern ( username ) ;
109+ const parameterMatch = matchedReason . match ( new RegExp (
110+ '^(' + usernamePattern + ')(\\s*\\|)(\\s*)(?:(reason|2)(\\s*=\\s*))?'
111+ ) ) ;
112+ if ( ! parameterMatch ) {
113+ throw new Error ( 'Unable to identify the username and reason parameters' ) ;
114+ }
115+ const usernamePrefix = initialText . slice ( initialText . indexOf ( '|' ) + 1 ) ;
116+ const reasonPrefix = parameterMatch [ 4 ] ?
117+ parameterMatch [ 3 ] + parameterMatch [ 4 ] + parameterMatch [ 5 ] :
118+ '2=' + parameterMatch [ 3 ] ;
119+ replacement = '{{' + templateName + ' reviewed|' + acceptOrDecline + '=' + acceptDeclineReason +
120+ '|' + usernamePrefix + parameterMatch [ 1 ] + parameterMatch [ 2 ] + reasonPrefix +
121+ matchedReason . slice ( parameterMatch [ 0 ] . length ) ;
122+ } else if ( templateName . toLowerCase ( ) === 'unblock-spamun' ) {
103123 // delete the first 1=
104124 replacement = replacement . replace ( '|1=' , '|' ) ;
105125 // add a 2= after the third pipe
@@ -129,26 +149,62 @@ export class UnblockReview {
129149 return haystack . split ( needle , nthOccurrence ) . join ( needle ) . length ;
130150 }
131151
152+ getUsernameRequestMatches ( wikitext , paramsAndReason ) {
153+ const pattern = '(\\{\\{([A-Za-z-]+)\\s*\\|\\s*(?:1\\s*=\\s*)?)(' +
154+ this . getAppealReasonPattern ( paramsAndReason , true ) + ')' ;
155+ const nowikiRanges = this . getNowikiRanges ( wikitext ) ;
156+ return [ ...wikitext . matchAll ( new RegExp ( pattern , 'g' ) ) ] . filter ( ( match ) => {
157+ if ( ! this . templatesThatHaveUsernameParameters . includes ( match [ 2 ] . toLowerCase ( ) ) ||
158+ this . isInsideNowiki ( match . index , nowikiRanges ) ) {
159+ return false ;
160+ }
161+ let depth = 0 ;
162+ const end = match . index + match [ 0 ] . length ;
163+ for ( let i = match . index ; i < end - 1 ; i ++ ) {
164+ if ( this . isInsideNowiki ( i , nowikiRanges ) ) {
165+ continue ;
166+ }
167+ if ( wikitext . slice ( i , i + 2 ) === '{{' ) {
168+ depth ++ ;
169+ i ++ ;
170+ } else if ( wikitext . slice ( i , i + 2 ) === '}}' ) {
171+ depth -- ;
172+ i ++ ;
173+ if ( depth === 0 ) {
174+ return false ;
175+ }
176+ }
177+ }
178+ return true ;
179+ } ) ;
180+ }
181+
132182 /**
133183 * Given the wikitext of an entire page, and the |reason= parameter of one of the many unblock templates (e.g. {{Unblock}}, {{Unblock-un}}, {{Unblock-auto}}, {{Unblock-bot}}, etc.), return the wikitext of just the beginning of the template.
134184 *
135185 * For example, "Test {{unblock|reason=Your reason here [[User:Filipe46]]}} Test" as the wikitext and "Your reason here" as the appealReason will return "{{unblock|reason=".
136186 *
137187 * This can also handle 1=, and no parameter at all (just a pipe)
138188 */
139- getLeftHalfOfUnblockTemplate ( wikitext , appealReason , isSpamUn = false ) {
189+ getLeftHalfOfUnblockTemplate ( wikitext , appealReason , hasUsernameParameter = false ) {
190+ if ( hasUsernameParameter ) {
191+ const matches = this . getUsernameRequestMatches ( wikitext , appealReason ) ;
192+ if ( matches . length === 0 ) {
193+ throw new Error ( 'Searching for target text failed' ) ;
194+ }
195+ return matches [ 0 ] [ 1 ] ;
196+ }
140197 const nowikiRanges = this . getNowikiRanges ( wikitext ) ;
141198
142199 // Find all {{unblock...|reason=... patterns using matchAll
143200 const structuredMatches = [ ...wikitext . matchAll ( / \{ \{ u n b l o c k [ ^ } ] * \| r e a s o n \s * = \s * / gi ) ] . filter (
144- ( match ) => ! this . isInsideNowiki ( match . index , nowikiRanges ) &&
145- ( ! isSpamUn || / ^ \{ \{ u n b l o c k - s p a m u n \s * \| / i. test ( match [ 0 ] ) )
201+ ( match ) => ! this . isInsideNowiki ( match . index , nowikiRanges )
146202 ) ;
147203
148204 // Try each structured match to see if the reason after it matches appealReason
149205 for ( const unblockReasonMatch of structuredMatches ) {
150206 const reasonStartPos = unblockReasonMatch . index + unblockReasonMatch [ 0 ] . length ;
151- const reasonPattern = new RegExp ( this . getAppealReasonPattern ( appealReason , isSpamUn ) ) ;
207+ const reasonPattern = new RegExp ( this . getAppealReasonPattern ( appealReason ) ) ;
152208 const reasonText = wikitext . substring ( reasonStartPos ) ;
153209 const reasonMatch = reasonText . match ( reasonPattern ) ;
154210
@@ -179,7 +235,7 @@ export class UnblockReview {
179235 const matchPos = reasonStartPos + reasonMatch . index ;
180236
181237 // Verify the match is within this template (before the closing }})
182- if ( matchPos < closePos && ( ! isSpamUn || matchPos + reasonMatch [ 0 ] . length <= closePos ) ) {
238+ if ( matchPos < closePos ) {
183239 const initialText = wikitext . slice ( unblockReasonMatch . index , matchPos ) ;
184240
185241 if ( ! initialText . match ( / ^ \{ \{ u n b l o c k / i ) ) {
@@ -196,7 +252,7 @@ export class UnblockReview {
196252 // Isolate the reason, stripping out all template syntax. So `{{Unblock|reason=ABC}}` becomes matches = [ 'ABC ']
197253 // eslint-disable-next-line no-useless-concat
198254 const negativeLookbehinds = '(?<!<' + 'nowiki>{{unblock\\|reason=)(?<!reviewed ?\\|1=)' ;
199- const regEx = new RegExp ( negativeLookbehinds + this . getAppealReasonPattern ( appealReason , isSpamUn ) , 'g' ) ;
255+ const regEx = new RegExp ( negativeLookbehinds + this . getAppealReasonPattern ( appealReason ) , 'g' ) ;
200256 let matches = wikitext . matchAll ( regEx ) ;
201257 matches = [ ...matches ] ;
202258
@@ -226,9 +282,6 @@ export class UnblockReview {
226282 }
227283
228284 const initialText = wikitext . slice ( potentialStart , matchPos ) ;
229- if ( isSpamUn && ! / ^ \{ \{ u n b l o c k - s p a m u n \s * \| [ ^ { } ] * $ / i. test ( initialText ) ) {
230- continue ;
231- }
232285
233286 // If this is an unblock template, we found it
234287 if ( initialText . match ( / ^ \{ \{ u n b l o c k / i ) ) {
@@ -281,23 +334,24 @@ export class UnblockReview {
281334 * Return a regex pattern that matches appeal reason wikitext with tags, wikilinks, templates, or emphasis.
282335 *
283336 * @param {string } appealReason - The rendered appeal text.
284- * @param {boolean } isSpamUn - Whether the request includes a separate username parameter.
337+ * @param {boolean } hasUsernameParameter - Whether the request includes a separate username parameter.
285338 * @return {string } Regex pattern for matching the original wikitext.
286339 */
287- getAppealReasonPattern ( appealReason , isSpamUn = false ) {
340+ getAppealReasonPattern ( appealReason , hasUsernameParameter = false ) {
288341 // Matches tags, wikilink markup, templates, and emphasis apostrophes between rendered characters.
289342 // Template pattern allows both opening {{ and closing }} to appear between characters,
290343 // since rendered text from {{u|username}} renders as just "username" with the template markup invisible.
291344 // The brace depth check in getLeftHalfOfUnblockTemplate() ensures we don't match inside nested templates.
292345 const markup = '(?:<\\/?[a-zA-Z][a-zA-Z0-9:-]*(?:\\s+(?:"[^"]*"|\'[^\']*\'|[^\'"<>])*)?\\s*\\/?>|\\[\\[[^\\[\\]|\\n]*\\||\\[\\[|\\]\\]|\'{2,}(?!\')|\\{[^{}]*\\}|\\{\\{[^}]*\\}\\}|\\}\\})*' ;
293- // The HTML omits whitespace at the start of the second spam-unblock parameter .
294- let usernameSeparator = isSpamUn ;
346+ // The HTML omits parameter names and whitespace around the username separator .
347+ let usernameSeparator = hasUsernameParameter ;
295348 return Array . from ( appealReason , ( character ) => {
296349 const isSeparator = usernameSeparator && character === '|' ;
297350 if ( isSeparator ) {
298351 usernameSeparator = false ;
352+ return markup + '\\s*\\|\\s*(?:(?:reason|2)\\s*=\\s*)?' ;
299353 }
300- return markup + this . escapeRegExp ( character ) + ( isSeparator ? '\\s*' : '' ) ;
354+ return markup + this . escapeRegExp ( character ) ;
301355 } ) . join ( '' ) ;
302356 }
303357
0 commit comments