Skip to content

Commit

Permalink
handle existing banner shell
Browse files Browse the repository at this point in the history
  • Loading branch information
NovemLinguae committed Aug 22, 2024
1 parent 830472f commit 9dd2b4c
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 7 deletions.
28 changes: 21 additions & 7 deletions src/modules/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -1708,21 +1708,35 @@
// (e.g. pages in `Draft:` namespace with discussion)
talkText = talkTextPrefix + '\n\n' + talkText;

// Add banner shell, always. This is easier than maintaining 2 code paths: a code path for 1 banner and a code path for 2+ banners.
// If present, delete WikiProject banner shell. We'll re-add it later.
var bannerShellTemplates = 'WikiProject banner shell|WikiProjectBanners|WikiProject Banners|WPB|WPBS|WikiProject cooperation shell|Wikiprojectbannershell|WikiProject Banner Shell|Wpb|WPBannerShell|Wpbs|Wikiprojectbanners|WP Banner Shell|WP banner shell|Bannershell|Wikiproject banner shell|WIkiProjectBanner Shell|WikiProjectBannerShell|WikiProject BannerShell|Coopshell|WikiprojectBannerShell|WikiProject Shell|Scope shell|Project shell|WikiProject shell|WikiProject banner|Wpbannershell|Multiple wikiprojects|Wikiproject banner holder|Project banner holder|WikiProject banner shell\\/test1|Article assessment|WikiProject bannershell';
var bannerShellDetectionRegex = new RegExp( '{{(?:' + bannerShellTemplates + ')[^\n]*\n', 'i' );
var hasBannerShell = talkText.match( bannerShellDetectionRegex );
var bannerShellWasDeleted = false;
if ( hasBannerShell ) {
talkText = talkText.replace( hasBannerShell, '' );
bannerShellWasDeleted = true;
}

// Add WikiProject banner shell, always. This is easier than maintaining 2 code paths: a code path for 1 banner and a code path for 2+ banners.
var banners = talkText.match( /{{(?:wikiproject|subst:wpafc\/article|football)[^}]+}}/gi );
// https://en.wikipedia.org/wiki/Special:WhatLinksHere?target=Template%3AWikiProject+banner+shell&namespace=&hidetrans=1&hidelinks=1
var bannerShellDetectionRegex = /{{(?:WikiProject banner shell|WikiProjectBanners|WikiProject Banners|WPB|WPBS|WikiProject cooperation shell|Wikiprojectbannershell|WikiProject Banner Shell|Wpb|WPBannerShell|Wpbs|Wikiprojectbanners|WP Banner Shell|WP banner shell|Bannershell|Wikiproject banner shell|WIkiProjectBanner Shell|WikiProjectBannerShell|WikiProject BannerShell|Coopshell|WikiprojectBannerShell|WikiProject Shell|Scope shell|Project shell|WikiProject shell|WikiProject banner|Wpbannershell|Multiple wikiprojects|Wikiproject banner holder|Project banner holder|WikiProject banner shell\/test1|Article assessment|WikiProject bannershell)/i;
var hasBannerShell = talkText.match( bannerShellDetectionRegex );
bannerShellDetectionRegex = new RegExp( '{{(?:' + bannerShellTemplates + ')', 'i' );
hasBannerShell = talkText.match( bannerShellDetectionRegex );
if ( banners.length >= 1 && !hasBannerShell ) {
var bannerShellStart = '{{WikiProject banner shell|';
var bannerShellEnd = '}}';
var firstBanner = banners[ 0 ];
var lastBanner = banners.slice( -1 )[ 0 ];
talkText = talkText.replace( firstBanner, bannerShellStart + '\n' + firstBanner );
talkText = talkText.replace( lastBanner, lastBanner + '\n' + bannerShellEnd );

// If we deleted the banner shell above, we didn't delete its closing }}. Skip adding it here. Just recycle it.
if ( !bannerShellWasDeleted ) {
var bannerShellEnd = '}}';
var lastBanner = banners.slice( -1 )[ 0 ];
talkText = talkText.replace( lastBanner, lastBanner + '\n' + bannerShellEnd );
}
}

// If banner shell is present, comply with [[WP:PIQA]]. Add the class only to the banner shell. Delete any other class parameters.
// Comply with [[WP:PIQA]]. Add the |class= only to the banner shell. Delete all other class parameters (e.g. from WikiProject banners)
hasBannerShell = talkText.match( bannerShellDetectionRegex );
if ( hasBannerShell ) {
// delete all |class= from the entire talk page
Expand Down
47 changes: 47 additions & 0 deletions tests/test-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,53 @@ I have a question. Can you help answer it? –[[User:Novem Linguae|<span style="
`{{WikiProject banner shell|
{{subst:WPAFC/article|oldid=592507}}
{{WikiProject Women}}
{{WikiProject Women's sport}}
{{WikiProject Somalia}}
}}`
);
expect( output.countOfWikiProjectsAdded ).toBe( 0 );
expect( output.countOfWikiProjectsRemoved ).toBe( 0 );
expect( output.bannerCount ).toBe( 4 );
} );

it( 'talk page has existing WikiProject banner shell and banners', function () {
var talkText =
`{{WikiProject banner shell|
{{WikiProject Women}}
{{WikiProject Women's sport}}
{{WikiProject Somalia}}
}}`;
var newAssessment = '';
var revId = 592507;
var isBiography = false;
var newWikiProjects = [ 'WikiProject Somalia', 'WikiProject Women', 'WikiProject Women\'s sport' ];
var lifeStatus = 'unknown';
var subjectName = '';
var existingWikiProjects = [
{
displayName: 'Somalia',
templateName: 'WikiProject Somalia',
alreadyOnPage: true
},
{
displayName: 'Women',
templateName: 'WikiProject Women',
alreadyOnPage: true
},
{
displayName: 'Women\'s sport',
templateName: 'WikiProject Women\'s sport',
alreadyOnPage: true
}
];
var alreadyHasWPBio = false;
var existingWPBioTemplateName = null;
var output = AFCH.addTalkPageBanners( talkText, newAssessment, revId, isBiography, newWikiProjects, lifeStatus, subjectName, existingWikiProjects, alreadyHasWPBio, existingWPBioTemplateName );
expect( output.talkText ).toBe(
`{{WikiProject banner shell|
{{subst:WPAFC/article|oldid=592507}}
{{WikiProject Women}}
{{WikiProject Women's sport}}
{{WikiProject Somalia}}
Expand Down

0 comments on commit 9dd2b4c

Please sign in to comment.