Skip to content

Commit

Permalink
fix #233 unable to insert |image_has_rationale=yes when {{Non-free X}…
Browse files Browse the repository at this point in the history
…} template already has parameters
  • Loading branch information
NovemLinguae committed Nov 1, 2024
1 parent 2cba1e1 commit 0cf6896
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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 );
Expand Down Expand Up @@ -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;
Expand All @@ -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.
*/
Expand Down
8 changes: 8 additions & 0 deletions MarkFreeUseRationale/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"dependencies": {
"jest": "^27.4.2"
},
"scripts": {
"test": "jest"
}
}
29 changes: 29 additions & 0 deletions MarkFreeUseRationale/tests/MarkFreeUseRationale.test.js
Original file line number Diff line number Diff line change
@@ -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 );
} );

0 comments on commit 0cf6896

Please sign in to comment.