Skip to content

Commit

Permalink
addTalkPageBanners: handle banners that redirect to biography banner
Browse files Browse the repository at this point in the history
  • Loading branch information
NovemLinguae committed Sep 4, 2024
1 parent 2b33a3d commit ff9a203
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 23 deletions.
65 changes: 44 additions & 21 deletions src/modules/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -1673,27 +1673,6 @@
'}}'
);

// delete existing biography banner. when accepting, reviewer is forced to choose if it's a biography or not, so we'll add (or not add) our own biography banner later
banners = banners.filter( ( value ) => !value.match( /^{{WikiProject Biography/i ) );

// add biography banner to array. and add |living= and |listas= to banner shell
let bannerShellExtraParams = '';
if ( isBiography ) {
banners.push(
'{{WikiProject Biography}}'
);

if ( lifeStatus === 'living' ) {
bannerShellExtraParams += ' |living=yes';
} else if ( lifeStatus === 'dead' ) {
bannerShellExtraParams += ' |living=no';
}

if ( subjectName ) {
bannerShellExtraParams += ' |listas=' + subjectName;
}
}

// add disambiguation banner to array
if ( newAssessment === 'Disambig' ) {
banners.push( '{{WikiProject Disambiguation}}' );
Expand All @@ -1715,6 +1694,42 @@
newAssessment = '';
}

// Some banners such as {{WikiProject Musicians}}, when subst'd later by AnomieBOT, end up as something like {{WikiProject Biography |musician-work-group=yes}}. Do this ourselves to save a step and to prevent two {{WikiProject Biography}} banners.
const bannersThatRedirectToBiographyBanner = {
'{{WikiProject Musicians}}': ' |musician-work-group=yes',
'{{WikiProject Sportspeople}}': ' |sports-work-group=yes'
};
let workGroupString = '';
for ( const banner in bannersThatRedirectToBiographyBanner ) {
const workGroup = bannersThatRedirectToBiographyBanner[ banner ];
const hasBanner = AFCH.hasArrayValueCaseInsensitive( banners, banner );
if ( hasBanner ) {
banners = AFCH.deleteArrayValueCaseInsensitive( banners, banner );
workGroupString += workGroup;
}
}

// delete existing biography banner. when accepting, reviewer is forced to choose if it's a biography or not, so we'll add (or not add) our own biography banner later
banners = banners.filter( ( value ) => !value.match( /^{{WikiProject Biography/i ) );

// add biography banner to array. and add |living= and |listas= to banner shell
let bannerShellExtraParams = '';
if ( isBiography ) {
banners.push(
`{{WikiProject Biography${ workGroupString }}}`
);

if ( lifeStatus === 'living' ) {
bannerShellExtraParams += ' |living=yes';
} else if ( lifeStatus === 'dead' ) {
bannerShellExtraParams += ' |living=no';
}

if ( subjectName ) {
bannerShellExtraParams += ' |listas=' + subjectName;
}
}

// Convert array back to wikitext and append to top of talk page.
// Always add a shell even if it's just wrapping one banner, for code simplification reasons.
// Add |class= to shell.
Expand All @@ -1736,6 +1751,14 @@
return wikicode;
},

deleteArrayValueCaseInsensitive( array, value ) {
return array.filter( ( item ) => item.toLowerCase() !== value.toLowerCase() );
},

hasArrayValueCaseInsensitive( array, value ) {
return array.some( ( item ) => item.toLowerCase() === value.toLowerCase() );
},

/**
* In an array of templates, remove duplicate templates, case insensitive.
*
Expand Down
98 changes: 96 additions & 2 deletions tests/test-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,8 @@ I have a question. Can you help answer it? –[[User:Novem Linguae|<span style="
{{WikiProject Film}}
{{WikiProject Women}}
{{WikiProject Television}}
{{WikiProject Biography}}
{{WikiProject Romania}}
{{WikiProject Biography}}
}}`
);
} );
Expand Down Expand Up @@ -328,9 +328,9 @@ I have a question. Can you help answer it? –[[User:Novem Linguae|<span style="
expect( output ).toBe(
`{{WikiProject banner shell |class=B |living=yes |listas=Jones, Bob |1=
{{subst:WPAFC/article |oldid=592496}}
{{WikiProject Biography}}
{{WikiProject Africa}}
{{WikiProject Alabama}}
{{WikiProject Biography}}
}}`
);
} );
Expand Down Expand Up @@ -423,6 +423,61 @@ I have a question. Can you help answer it? –[[User:Novem Linguae|<span style="
{{translated page|pl|Katowice Załęże|version=|small=no|insertversion=|section=}}`
);
} );

it( 'when WikiProject Musicians is present, it should convert to {{WikiProject Biography |musician-work-group=yes}}', () => {
const wikicode = ``;
const newAssessment = '';
const revId = 592681;
const isBiography = true;
const newWikiProjects = [ 'WikiProject Musicians' ];
const lifeStatus = 'unknown';
const subjectName = '';
const output = AFCH.addTalkPageBanners( wikicode, newAssessment, revId, isBiography, newWikiProjects, lifeStatus, subjectName );
expect( output ).toBe(
`{{WikiProject banner shell |1=
{{subst:WPAFC/article |oldid=592681}}
{{WikiProject Biography |musician-work-group=yes}}
}}`
);
} );

it( 'when WikiProject Musicians is present, it should convert to {{WikiProject Biography |musician-work-group=yes}}, case sensitive', () => {
const wikicode =
`{{WikiProject Biography}}
{{WikiProject Musicians}}`;
const newAssessment = '';
const revId = 592681;
const isBiography = true;
const newWikiProjects = [ 'WikiProject Musicians' ];
const lifeStatus = 'unknown';
const subjectName = '';
const output = AFCH.addTalkPageBanners( wikicode, newAssessment, revId, isBiography, newWikiProjects, lifeStatus, subjectName );
expect( output ).toBe(
`{{WikiProject banner shell |1=
{{subst:WPAFC/article |oldid=592681}}
{{WikiProject Biography |musician-work-group=yes}}
}}`
);
} );

it( 'when WikiProject Musicians is present, it should convert to {{WikiProject Biography |musician-work-group=yes}}, case insensitive', () => {
const wikicode =
`{{wikiproject biography}}
{{wikiproject musicians}}`;
const newAssessment = '';
const revId = 592681;
const isBiography = true;
const newWikiProjects = [ 'WikiProject Musicians' ];
const lifeStatus = 'unknown';
const subjectName = '';
const output = AFCH.addTalkPageBanners( wikicode, newAssessment, revId, isBiography, newWikiProjects, lifeStatus, subjectName );
expect( output ).toBe(
`{{WikiProject banner shell |1=
{{subst:WPAFC/article |oldid=592681}}
{{WikiProject Biography |musician-work-group=yes}}
}}`
);
} );
} );

describe( 'AFCH.removeDuplicateBanners', () => {
Expand Down Expand Up @@ -482,3 +537,42 @@ describe( 'AFCH.removeDuplicateBanners', () => {
] );
} );
} );

describe( 'AFCH.deleteArrayValueCaseInsensitive', () => {
it( 'delete 1, case sensitive', () => {
const array = [ 'Test1', 'Test2', 'Test3' ];
const value = 'Test2';
const output = AFCH.deleteArrayValueCaseInsensitive( array, value );
expect( output ).toEqual( [ 'Test1', 'Test3' ] );
} );

it( 'delete 1, case insensitive', () => {
const array = [ 'Test1', 'Test2', 'Test3' ];
const value = 'test2';
const output = AFCH.deleteArrayValueCaseInsensitive( array, value );
expect( output ).toEqual( [ 'Test1', 'Test3' ] );
} );
} );

describe( 'AFCH.hasArrayValueCaseInsensitive', () => {
it( 'has 1, case sensitive', () => {
const array = [ 'Test1', 'Test2', 'Test3' ];
const value = 'Test2';
const output = AFCH.hasArrayValueCaseInsensitive( array, value );
expect( output ).toBe( true );
} );

it( 'has 1, case insensitive', () => {
const array = [ 'Test1', 'Test2', 'Test3' ];
const value = 'test2';
const output = AFCH.hasArrayValueCaseInsensitive( array, value );
expect( output ).toBe( true );
} );

it( 'has 0', () => {
const array = [ 'Test1', 'Test2', 'Test3' ];
const value = 'Test4';
const output = AFCH.hasArrayValueCaseInsensitive( array, value );
expect( output ).toBe( false );
} );
} );

0 comments on commit ff9a203

Please sign in to comment.