diff --git a/MarkFreeUseRationale.js b/MarkFreeUseRationale/MarkFreeUseRationale.js similarity index 86% rename from MarkFreeUseRationale.js rename to MarkFreeUseRationale/MarkFreeUseRationale.js index 9a84630..181f0cf 100644 --- a/MarkFreeUseRationale.js +++ b/MarkFreeUseRationale/MarkFreeUseRationale.js @@ -4,8 +4,8 @@ - Requested by https://en.wikipedia.org/wiki/User:Elli on 2024-04-22 - When it runs (all 3 conditions must be met): 1) In the file namespace, - 2) on pages that have have a https://en.wikipedia.org/wiki/Category:Wikipedia_non-free_file_copyright_templates, - 3) and the template does not have a paramter image_has_rationale=yes, + 2) on pages that have one of these 100-ish templates that follow the format {{Non-free X}}: https://en.wikipedia.org/wiki/Category:Wikipedia_non-free_file_copyright_templates, + 3) and the template does not have a parameter image_has_rationale=yes, - What it does: - shows a button that allows adding this parameter in one click - Motivation: @@ -24,7 +24,7 @@ class MarkFreeUseRationale { return; } - // Only run if there is a {{Non-free XYZABC}} template (https://en.wikipedia.org/wiki/Category:Wikipedia_non-free_file_copyright_templates) that isnt a rationale template ( {{Non-free use rationale ABCXYZ}} ) + // Only run if there is a {{Non-free X}} template (https://en.wikipedia.org/wiki/Category:Wikipedia_non-free_file_copyright_templates) that isnt a rationale template ( {{Non-free use rationale X}} ) const pageName = this.mw.config.get( 'wgPageName' ); const wikicode = await this.getWikicode( pageName ); const hasCopyrightTemplate = wikicode.match( /\{\{Non-free (?!use)/gi ); @@ -60,7 +60,7 @@ class MarkFreeUseRationale { const wikicode = await this.getWikicode( pageName ); // insert image_has_rationale=yes into wikicode somewhere (look at Elli's example diff) - const newWikicode = wikicode.replace( /({{Non-free (?!use)[^|}]+)(}})/i, '$1|image_has_rationale=yes$2' ); + const newWikicode = this.insertImageHasRationaleYes( wikicode ); if ( newWikicode === wikicode ) { this.mw.notify( this.wrapErrorText( 'ERROR: Unable to find a place to insert |image_has_rationale=yes' ) ); return; @@ -78,6 +78,10 @@ class MarkFreeUseRationale { this.mw.notify( 'Edit successful. Refresh to see changes.' ); } + insertImageHasRationaleYes( wikicode ) { + return wikicode.replace( /({{Non-free (?!use)[^|}]+)([|}])/i, '$1|image_has_rationale=yes$2' ); + } + /** * mw.notify likes to receive HTML as a jQuery object. If you pass it a string, it will escape and print the string. */ diff --git a/MarkFreeUseRationale/package.json b/MarkFreeUseRationale/package.json new file mode 100644 index 0000000..d7c3303 --- /dev/null +++ b/MarkFreeUseRationale/package.json @@ -0,0 +1,8 @@ +{ + "dependencies": { + "jest": "^27.4.2" + }, + "scripts": { + "test": "jest" + } +} \ No newline at end of file diff --git a/MarkFreeUseRationale/tests/MarkFreeUseRationale.test.js b/MarkFreeUseRationale/tests/MarkFreeUseRationale.test.js new file mode 100644 index 0000000..b300b61 --- /dev/null +++ b/MarkFreeUseRationale/tests/MarkFreeUseRationale.test.js @@ -0,0 +1,29 @@ +/* eslint-disable quotes */ + +function insertImageHasRationaleYes( wikicode ) { + return wikicode.replace( /({{Non-free (?!use)[^|}]+)([|}])/i, '$1|image_has_rationale=yes$2' ); +} + +test( `should touch {{Non-free X}}`, () => { + const wikitext = `{{Non-free biog-pic}}`; + const output = `{{Non-free biog-pic|image_has_rationale=yes}}`; + expect( insertImageHasRationaleYes( wikitext ) ).toBe( output ); +} ); + +test( `shouldn't touch {{Non-free use rationale X}}`, () => { + const wikitext = +`{{Non-free use rationale 2 +|Description = This is a photo of Harry Conover +}}`; + const output = +`{{Non-free use rationale 2 +|Description = This is a photo of Harry Conover +}}`; + expect( insertImageHasRationaleYes( wikitext ) ).toBe( output ); +} ); + +test( `should handle {{Non-free X}} with parameters`, () => { + const wikitext = `{{Non-free biog-pic|Harry Conover|year=1944}}`; + const output = `{{Non-free biog-pic|image_has_rationale=yes|Harry Conover|year=1944}}`; + expect( insertImageHasRationaleYes( wikitext ) ).toBe( output ); +} );