Skip to content

Commit f18d56e

Browse files
committed
UnblockReview: handle unescaped equals by adding 1= to everything
or sometimes 2= related #272 unescaped equals sign bug, and reason starting with bold bug
1 parent a7c2641 commit f18d56e

File tree

2 files changed

+41
-9
lines changed

2 files changed

+41
-9
lines changed

UnblockReview/modules/UnblockReview.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,23 @@ export class UnblockReview {
4040
const templateName = initialText.match( /^\{\{([A-Za-z-]+)/i )[ 1 ];
4141
let wikitext2 = wikitext.replace(
4242
regEx,
43-
'{{' + templateName + ' reviewed|' + acceptOrDecline + '=' + acceptDeclineReason + '|' + paramsAndReason
43+
// Always add 1=. That way if there's an unescaped equals sign after it, it won't break things.
44+
'{{' + templateName + ' reviewed|' + acceptOrDecline + '=' + acceptDeclineReason + '|1=' + paramsAndReason
4445
);
4546

47+
// {{Unblock-spamun}} has an extra parameter for the new username. Example:
48+
// {{Unblock-spamun|NewUsername|Your reason here}}
49+
// So the output need to have a 2= instead of a 1=. Example:
50+
// {{Unblock-spamun reviewed|accept=I accept. ~~~~|NewUsername|2=Your reason here}}
51+
const hasExtraUsernameParameter = templateName.toLowerCase() === 'unblock-spamun';
52+
if ( hasExtraUsernameParameter ) {
53+
// delete the first 1=
54+
wikitext2 = wikitext2.replace( '|1=', '|' );
55+
// add a 2= after the third pipe
56+
const thirdPipePos = this.getPosition( wikitext2, '|', 3 );
57+
wikitext2 = wikitext2.slice( 0, thirdPipePos ) + '|2=' + wikitext2.slice( thirdPipePos + 1 );
58+
}
59+
4660
if ( wikitext === wikitext2 ) {
4761
throw new Error( 'Replacing text with unblock message failed!' );
4862
}
@@ -53,6 +67,13 @@ export class UnblockReview {
5367
return wikitext2;
5468
}
5569

70+
/**
71+
* @copyright Denys Seguret, CC BY-SA 4.0, https://stackoverflow.com/a/14480366/3480193
72+
*/
73+
getPosition( haystack, needle, nthOccurrence ) {
74+
return haystack.split( needle, nthOccurrence ).join( needle ).length;
75+
}
76+
5677
/**
5778
* 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.
5879
*

UnblockReview/tests/UnblockReview.test.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ describe( 'processAcceptOrDecline( wikitext, paramsAndReason, acceptDeclineReaso
131131
const paramsAndReason = `Your reason here `;
132132
const acceptDeclineReason = `Insufficient. ~~~~`;
133133
const acceptOrDecline = `decline`;
134-
const expected = `Test {{unblock reviewed|decline=Insufficient. ~~~~|Your reason here [[User:Filipe46|Filipe46]] ([[User talk:Filipe46#top|talk]]) 21:54, 25 November 2021 (UTC)}} Test`;
134+
const expected = `Test {{unblock reviewed|decline=Insufficient. ~~~~|1=Your reason here [[User:Filipe46|Filipe46]] ([[User talk:Filipe46#top|talk]]) 21:54, 25 November 2021 (UTC)}} Test`;
135135
expect( unblockReview.processAcceptOrDecline( wikitext, paramsAndReason, acceptDeclineReason, DEFAULT_DECLINE_REASON, acceptOrDecline ) ).toBe( expected );
136136
} );
137137

@@ -140,7 +140,7 @@ describe( 'processAcceptOrDecline( wikitext, paramsAndReason, acceptDeclineReaso
140140
const paramsAndReason = `Your reason here `;
141141
const acceptDeclineReason = `Insufficient. ~~~~`;
142142
const acceptOrDecline = `decline`;
143-
const expected = `{{unblock reviewed|decline=Insufficient. ~~~~|Your reason here [[User:Filipe46|Filipe46]] ([[User talk:Filipe46#top|talk]]) 21:54, 25 November 2021 (UTC)}}`;
143+
const expected = `{{unblock reviewed|decline=Insufficient. ~~~~|1=Your reason here [[User:Filipe46|Filipe46]] ([[User talk:Filipe46#top|talk]]) 21:54, 25 November 2021 (UTC)}}`;
144144
expect( unblockReview.processAcceptOrDecline( wikitext, paramsAndReason, acceptDeclineReason, DEFAULT_DECLINE_REASON, acceptOrDecline ) ).toBe( expected );
145145
} );
146146

@@ -149,7 +149,7 @@ describe( 'processAcceptOrDecline( wikitext, paramsAndReason, acceptDeclineReaso
149149
const paramsAndReason = `Your reason here `;
150150
const acceptDeclineReason = `Insufficient. ~~~~`;
151151
const acceptOrDecline = `decline`;
152-
const expected = `{{unblock reviewed|decline=Insufficient. ~~~~|Your reason here [[User:Filipe46|Filipe46]] ([[User talk:Filipe46#top|talk]]) 21:54, 25 November 2021 (UTC)}}`;
152+
const expected = `{{unblock reviewed|decline=Insufficient. ~~~~|1=Your reason here [[User:Filipe46|Filipe46]] ([[User talk:Filipe46#top|talk]]) 21:54, 25 November 2021 (UTC)}}`;
153153
expect( unblockReview.processAcceptOrDecline( wikitext, paramsAndReason, acceptDeclineReason, DEFAULT_DECLINE_REASON, acceptOrDecline ) ).toBe( expected );
154154
} );
155155

@@ -169,7 +169,7 @@ I would somewhat disagree with you there sir. `;
169169
const expected =
170170
`==Unblock request==
171171
172-
{{unblock reviewed|decline=Insufficient. ~~~~|"Clearly not here to build an encyclopedia"
172+
{{unblock reviewed|decline=Insufficient. ~~~~|1="Clearly not here to build an encyclopedia"
173173
174174
I would somewhat disagree with you there sir. [[User:Jean Zboncak|Jean Zboncak]] ([[User talk:Jean Zboncak#top|talk]]) 22:54, 30 October 2024 (UTC)}}
175175
Seem’st thou thrive if he did banish thee, arm against thy quarrel.`;
@@ -188,7 +188,7 @@ Seem’st thou thrive if he did banish thee, arm against thy quarrel.`;
188188
const expected =
189189
`<div class="user-block" style="padding: 5px; margin-bottom: 0.5em; border: 1px solid var(--border-color-base, #a2ab91); background-color: var(--background-color-warning-subtle, #fef6e7); color:inherit; min-height: 40px">[[File:Stop x nuvola.svg|40px|left|alt=Stop icon]]<div style="margin-left:45px">You have been '''[[WP:Blocking policy|blocked]]''' '''[[Wikipedia:Blocking_policy#Indefinite_blocks|indefinitely]]''' from editing for [[Wikipedia:Sockpuppetry|abusing multiple accounts]]. Note that multiple accounts are [[Wikipedia:Sockpuppetry#Legitimate uses|allowed]], but '''not for ''[[Wikipedia:Sockpuppetry#Inappropriate uses of alternative accounts|illegitimate]]'' reasons''', and any contributions made while evading blocks or bans may be [[Wikipedia:Banning policy#Edits by and on behalf of banned editors|reverted]] or [[Wikipedia:Criteria for speedy deletion#G5|deleted]]. </div><div style="margin-left:45px">If you think there are good reasons for being unblocked, please review Wikipedia's [[WP:Guide to appealing blocks|guide to appealing blocks]], then add the following text to the bottom of your talk page: <!-- Copy the text as it appears on your page, not as it appears in this edit area. --><code><nowiki>{{unblock|reason=Your reason here ~~~~}}</nowiki></code>. &nbsp;[[User:Ponyo|<span style="color: Navy;">'''Ponyo'''</span>]]<sup>[[User talk:Ponyo|<span style="color: Navy;">''bons mots''</span>]]</sup> 18:18, 25 October 2024 (UTC)</div></div><!-- Template:uw-sockblock -->
190190
191-
{{unblock reviewed|decline=Insufficient. ~~~~|Your reason here [[User:Rathoremohanrathore|Rathoremohanrathore]] ([[User talk:Rathoremohanrathore#top|talk]]) 12:12, 30 October 2024 (UTC)}}
191+
{{unblock reviewed|decline=Insufficient. ~~~~|1=Your reason here [[User:Rathoremohanrathore|Rathoremohanrathore]] ([[User talk:Rathoremohanrathore#top|talk]]) 12:12, 30 October 2024 (UTC)}}
192192
`;
193193
expect( unblockReview.processAcceptOrDecline( wikitext, paramsAndReason, acceptDeclineReason, DEFAULT_DECLINE_REASON, acceptOrDecline ) ).toBe( expected );
194194
} );
@@ -205,7 +205,7 @@ Seem’st thou thrive if he did banish thee, arm against thy quarrel.`;
205205
const expected =
206206
`{{unblock reviewed |1=Your reason here [[User:Rathoremohanrathore|Rathoremohanrathore]] ([[User talk:Rathoremohanrathore#top|talk]]) 12:06, 30 October 2024 (UTC) |decline = One open unblock request at a time, please. {{confirmed}} sockpuppetry to {{np|Lalmohanlal}}. You are now considered [[WP:CBAN|banned by the community]] under [[WP:3X]] due to your chronic [[WP:EVADE|block evasion]]. [[User:Yamla|Yamla]] ([[User talk:Yamla|talk]]) 12:10, 30 October 2024 (UTC)}}
207207
208-
{{unblock reviewed|decline=Insufficient. ~~~~|Your reason here [[User:Rathoremohanrathore|Rathoremohanrathore]] ([[User talk:Rathoremohanrathore#top|talk]]) 12:12, 30 October 2024 (UTC)}}
208+
{{unblock reviewed|decline=Insufficient. ~~~~|1=Your reason here [[User:Rathoremohanrathore|Rathoremohanrathore]] ([[User talk:Rathoremohanrathore#top|talk]]) 12:12, 30 October 2024 (UTC)}}
209209
`;
210210
expect( unblockReview.processAcceptOrDecline( wikitext, paramsAndReason, acceptDeclineReason, DEFAULT_DECLINE_REASON, acceptOrDecline ) ).toBe( expected );
211211
} );
@@ -218,7 +218,7 @@ Seem’st thou thrive if he did banish thee, arm against thy quarrel.`;
218218
const acceptDeclineReason = `Insufficient. ~~~~`;
219219
const acceptOrDecline = `decline`;
220220
const expected =
221-
`{{unblock reviewed|decline=Insufficient. ~~~~|}} why
221+
`{{unblock reviewed|decline=Insufficient. ~~~~|1=}} why
222222
`;
223223
expect( unblockReview.processAcceptOrDecline( wikitext, paramsAndReason, acceptDeclineReason, DEFAULT_DECLINE_REASON, acceptOrDecline ) ).toBe( expected );
224224
} );
@@ -231,8 +231,19 @@ Seem’st thou thrive if he did banish thee, arm against thy quarrel.`;
231231
const acceptDeclineReason = `I accept. ~~~~`;
232232
const acceptOrDecline = `accept`;
233233
const expected =
234-
`{{unblock-spamun reviewed|accept=I accept. ~~~~|AlexWhisker|Test}}
234+
`{{unblock-spamun reviewed|accept=I accept. ~~~~|AlexWhisker|2=Test}}
235235
`;
236236
expect( unblockReview.processAcceptOrDecline( wikitext, paramsAndReason, acceptDeclineReason, DEFAULT_DECLINE_REASON, acceptOrDecline ) ).toBe( expected );
237237
} );
238+
239+
test( 'Handle an unescaped equals sign (handle by making sure the parameter starts with 1= or 2=)', () => {
240+
const wikitext =
241+
`{{unblock|1=I was blocked a few days ago for... <span style="font-family:'Courier New', monospace;">[[User:Test]]</span>}}`;
242+
const paramsAndReason = `I was blocked a few days ago for...`;
243+
const acceptDeclineReason = `Please use your other account.`;
244+
const acceptOrDecline = `decline`;
245+
const expected =
246+
`{{unblock reviewed|decline=Please use your other account. ~~~~|1=I was blocked a few days ago for... <span style="font-family:'Courier New', monospace;">[[User:Test]]</span>}}`;
247+
expect( unblockReview.processAcceptOrDecline( wikitext, paramsAndReason, acceptDeclineReason, DEFAULT_DECLINE_REASON, acceptOrDecline ) ).toBe( expected );
248+
} );
238249
} );

0 commit comments

Comments
 (0)