Skip to content

Commit d2cedac

Browse files
ante: add button to get latest rev of page
1 parent e14f49b commit d2cedac

File tree

3 files changed

+128
-2
lines changed

3 files changed

+128
-2
lines changed

i18n/ante/en.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@
5050
"deputy.ante.dateAuto.failed": "Could not pull date from revision: $1",
5151
"deputy.ante.dateAuto.missing": "The revision $1 could not be found. Its page may have been deleted.",
5252

53+
"deputy.ante.revisionAuto": "Latest",
54+
"deputy.ante.revisionAuto.title": "Pull the revision ID from the latest (current) revision of the page in `$1`.",
55+
"deputy.ante.revisionAuto.failed": "Could not pull revision ID from page: $1",
56+
"deputy.ante.revisionAuto.missing": "The page $1 could not be found. It may have been deleted.",
57+
5358
"deputy.ante.copied.label": "Copied $1",
5459

5560
"deputy.ante.copied.remove": "Remove notice",
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import MwApi from '../../../../MwApi';
2+
import getApiErrorText from '../../../../wiki/util/getApiErrorText';
3+
4+
export interface PageLatestRevisionGetButtonData extends OO.ui.ButtonWidget.ConfigOptions {
5+
titleInputWidget: OO.ui.TextInputWidget;
6+
revisionInputWidget: OO.ui.InputWidget;
7+
}
8+
9+
let InternalPageLatestRevisionGetButton: any;
10+
11+
/**
12+
* Initializes the process element.
13+
*/
14+
function initPageLatestRevisionGetButton() {
15+
InternalPageLatestRevisionGetButton = class PageLatestRevisionGetButton
16+
extends OO.ui.ButtonWidget {
17+
18+
titleInputWidget: OO.ui.TextInputWidget;
19+
revisionInputWidget: OO.ui.InputWidget;
20+
21+
/**
22+
* @param config Configuration to be passed to the element.
23+
*/
24+
constructor( config: PageLatestRevisionGetButtonData ) {
25+
super( Object.assign( {
26+
icon: 'download',
27+
invisibleLabel: true,
28+
disabled: true
29+
}, config ) );
30+
31+
this.titleInputWidget = config.titleInputWidget;
32+
this.revisionInputWidget = config.revisionInputWidget;
33+
34+
this.titleInputWidget.on( 'change', this.updateButton.bind( this ) );
35+
this.revisionInputWidget.on( 'change', this.updateButton.bind( this ) );
36+
this.on( 'click', this.setRevisionFromPageLatestRevision.bind( this ) );
37+
this.updateButton();
38+
}
39+
40+
/**
41+
* Update the disabled state of the button.
42+
*/
43+
updateButton(): void {
44+
this.setDisabled(
45+
this.titleInputWidget.getValue().trim().length === 0 ||
46+
this.revisionInputWidget.getValue().trim().length !== 0 ||
47+
!( this.titleInputWidget as any ).isQueryValid()
48+
);
49+
}
50+
51+
/**
52+
* Set the revision ID from the page provided in the value of
53+
* `this.titleInputWidget`.
54+
*/
55+
async setRevisionFromPageLatestRevision(): Promise<void> {
56+
this
57+
.setIcon( 'ellipsis' )
58+
.setDisabled( true );
59+
this.revisionInputWidget.setDisabled( true );
60+
const title = this.titleInputWidget.getValue();
61+
await MwApi.action.get( {
62+
action: 'query',
63+
prop: 'revisions',
64+
titles: title,
65+
rvprop: 'ids'
66+
} ).then( ( data ) => {
67+
if ( ( data.query as any ).pages[ 0 ].missing ) {
68+
mw.notify(
69+
mw.msg( 'deputy.ante.revisionAuto.missing', title ),
70+
{ type: 'error' }
71+
);
72+
this.updateButton();
73+
return;
74+
}
75+
76+
this.revisionInputWidget.setValue( data.query.pages[ 0 ].revisions[ 0 ].revid );
77+
this.revisionInputWidget.setDisabled( false );
78+
this.setIcon( 'download' );
79+
this.updateButton();
80+
}, ( _error, errorData ) => {
81+
mw.notify(
82+
83+
mw.msg( 'deputy.ante.revisionAuto.failed', getApiErrorText( errorData ) ),
84+
{ type: 'error' }
85+
);
86+
this.revisionInputWidget.setDisabled( false );
87+
this.setIcon( 'download' );
88+
this.updateButton();
89+
} );
90+
}
91+
92+
};
93+
}
94+
95+
/**
96+
* Creates a new PageLatestRevisionGetButton.
97+
*
98+
* @param config Configuration to be passed to the element.
99+
* @return A PageLatestRevisionGetButton object
100+
*/
101+
export default function ( config: PageLatestRevisionGetButtonData ) {
102+
if ( !InternalPageLatestRevisionGetButton ) {
103+
initPageLatestRevisionGetButton();
104+
}
105+
return new InternalPageLatestRevisionGetButton( config );
106+
}

src/modules/ante/ui/pages/CopiedTemplateRowPage.tsx

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import normalizeTitle from '../../../../wiki/util/normalizeTitle';
1414
import equalTitle from '../../../../util/equalTitle';
1515
import RevisionDateGetButton from '../components/RevisionDateGetButton';
1616
import SmartTitleInputWidget from '../components/SmartTitleInputWidget';
17+
import PageLatestRevisionGetButton from '../components/PageLatestRevisionGetButton';
1718

1819
export interface CopiedTemplateRowPageData {
1920
/**
@@ -335,6 +336,20 @@ function initCopiedTemplateRowPage() {
335336
revisionInputWidget: this.inputs.to_diff,
336337
dateInputWidget: this.inputs.date
337338
} );
339+
const revisionAutoFrom = PageLatestRevisionGetButton( {
340+
invisibleLabel: false,
341+
label: mw.msg( 'deputy.ante.revisionAuto' ),
342+
title: mw.msg( 'deputy.ante.revisionAuto.title', 'from' ),
343+
titleInputWidget: this.inputs.from,
344+
revisionInputWidget: this.inputs.from_oldid
345+
} );
346+
const revisionAutoTo = PageLatestRevisionGetButton( {
347+
invisibleLabel: false,
348+
label: mw.msg( 'deputy.ante.revisionAuto' ),
349+
title: mw.msg( 'deputy.ante.revisionAuto.title', 'to' ),
350+
titleInputWidget: this.inputs.to,
351+
revisionInputWidget: this.inputs.to_diff
352+
} );
338353

339354
this.fieldLayouts = {
340355
from: new OO.ui.FieldLayout( this.inputs.from, {
@@ -343,7 +358,7 @@ function initCopiedTemplateRowPage() {
343358
align: 'top',
344359
help: mw.msg( 'deputy.ante.copied.from.help' )
345360
} ),
346-
from_oldid: new OO.ui.FieldLayout( this.inputs.from_oldid, {
361+
from_oldid: new OO.ui.ActionFieldLayout( this.inputs.from_oldid, revisionAutoFrom, {
347362
$overlay: this.parent.$overlay,
348363
label: mw.msg( 'deputy.ante.copied.from_oldid.label' ),
349364
align: 'left',
@@ -355,7 +370,7 @@ function initCopiedTemplateRowPage() {
355370
align: 'top',
356371
help: mw.msg( 'deputy.ante.copied.to.help' )
357372
} ),
358-
to_diff: new OO.ui.FieldLayout( this.inputs.to_diff, {
373+
to_diff: new OO.ui.ActionFieldLayout( this.inputs.to_diff, revisionAutoTo, {
359374
$overlay: this.parent.$overlay,
360375
label: mw.msg( 'deputy.ante.copied.to_diff.label' ),
361376
align: 'left',

0 commit comments

Comments
 (0)