Skip to content

Commit

Permalink
add change tag support
Browse files Browse the repository at this point in the history
  • Loading branch information
ChlodAlejandro committed Sep 1, 2023
1 parent 5207cbb commit 9ecbc4a
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 12 deletions.
8 changes: 4 additions & 4 deletions src/Deputy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,8 @@ class Deputy {

/**
* This version of Deputy.
*
* @type {string}
*/
readonly version = deputyVersion;
readonly version: string = deputyVersion;
/**
* The current page as an mw.Title.
*/
Expand Down Expand Up @@ -129,12 +127,14 @@ class Deputy {
* sub-components as well.
*/
async init() {
// Attach modules to respective names
window.CopiedTemplateEditor = this.ante;
window.InfringementAssistant = this.ia;

mw.hook( 'deputy.preload' ).fire( this );

// Initialize the configuration
this.config = await UserConfiguration.load();
this.config = UserConfiguration.load();
window.deputyLang = this.config.core.language.get();

// Inject CSS
Expand Down
6 changes: 6 additions & 0 deletions src/config/WikiConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
listingWikitext
} from '../wiki/TemplatePolyfills';
import ConfigurationReloadBanner from '../ui/config/ConfigurationReloadBanner';
import changeTag from './changeTag';

export type WikiPageConfiguration = {
title: mw.Title,
Expand Down Expand Up @@ -233,6 +234,10 @@ export default class WikiConfiguration extends ConfigurationBase {
defaultValue: new URL( 'https://deputy.toolforge.org/' ),
displayOptions: { type: 'text' },
alwaysSave: true
} ),
changeTag: new Setting<string, string>( {
defaultValue: null,
displayOptions: { type: 'text' }
} )
};

Expand Down Expand Up @@ -462,6 +467,7 @@ export default class WikiConfiguration extends ConfigurationBase {
// Update last edited number
this.core.lastEdited.set( Date.now() );
await MwApi.action.postWithEditToken( {
...changeTag( await window.deputy.getWikiConfig() ),
action: 'edit',
title: this.sourcePage.getPrefixedText(),
text: this.serialize()
Expand Down
14 changes: 14 additions & 0 deletions src/config/changeTag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import WikiConfiguration from './WikiConfiguration';

/**
* Automatically applies a change tag to edits made by the user if
* a change tag was provided in the configuration.
*
* @param config
* @return A spreadable Object containing the `tags` parameter for `action=edit`.
*/
export default function changeTag( config: WikiConfiguration ): { tags?: string } {
return config.core.changeTag.get() ?
{ tags: config.core.changeTag.get() } :
{};
}
20 changes: 12 additions & 8 deletions src/modules/ante/ui/CopiedTemplateEditorDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import DeputyReviewDialog from '../../../ui/root/DeputyReviewDialog';
import normalizeTitle from '../../../wiki/util/normalizeTitle';
import getPageContent from '../../../wiki/util/getPageContent';
import openWindow from '../../../wiki/util/openWindow';
import changeTag from '../../../config/changeTag';

interface CopiedTemplateEditorDialogData {
main: CopiedTemplateEditor;
Expand Down Expand Up @@ -234,13 +235,13 @@ function initCopiedTemplateEditorDialog() {
framed: false,
invisibleLabel: true,
label: mw.msg( 'deputy.ante.mergeAll' ),
title: mw.msg( 'deputy.ante.mergeAll' ),
title: mw.msg( 'deputy.ante.mergeAll' )
} );
this.mergeButton.on( 'click', () => {
const notices = this.parsoid.findRowedNoticesByHref();
const noticeCount = Object.values(notices)
const noticeCount = Object.values( notices )
.filter( v => v.length > 1 )
.reduce( (p, n) => p + n.length, 0 );
.reduce( ( p, n ) => p + n.length, 0 );
return noticeCount ?
OO.ui.confirm(
mw.message(
Expand All @@ -252,8 +253,8 @@ function initCopiedTemplateEditorDialog() {
return;
}

for ( const noticeSet of Object.values(notices)) {
TemplateMerger.merge(noticeSet);
for ( const noticeSet of Object.values( notices ) ) {
TemplateMerger.merge( noticeSet );
}
} ) :
OO.ui.alert( 'There are no templates to merge.' );
Expand Down Expand Up @@ -324,13 +325,15 @@ function initCopiedTemplateEditorDialog() {

this.layout.on( 'remove', () => {
this.mergeButton.setDisabled(
!Object.values(this.parsoid.findRowedNoticesByHref()).some( v => v.length > 1 )
!Object.values( this.parsoid.findRowedNoticesByHref() )
.some( v => v.length > 1 )
);
deleteButton.setDisabled( this.parsoid.findNotices().length === 0 );
} );
this.parsoid.addEventListener( 'templateInsert', () => {
this.mergeButton.setDisabled(
!Object.values(this.parsoid.findRowedNoticesByHref()).some( v => v.length > 1 )
!Object.values( this.parsoid.findRowedNoticesByHref() )
.some( v => v.length > 1 )
);
deleteButton.setDisabled( this.parsoid.findNotices().length === 0 );
} );
Expand Down Expand Up @@ -439,7 +442,7 @@ function initCopiedTemplateEditorDialog() {

// Recheck state of merge button
this.mergeButton.setDisabled(
!Object.values(this.parsoid.findRowedNoticesByHref())
!Object.values( this.parsoid.findRowedNoticesByHref() )
.some( v => v.length > 1 )
);

Expand Down Expand Up @@ -478,6 +481,7 @@ function initCopiedTemplateEditorDialog() {
// Saves the page.
process.next( async () => {
return new mw.Api().postWithEditToken( {
...changeTag( await window.CopiedTemplateEditor.getWikiConfig() ),
action: 'edit',
format: 'json',
formatversion: '2',
Expand Down
5 changes: 5 additions & 0 deletions src/modules/ia/models/CopyrightProblemsListing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import normalizeTitle from '../../../wiki/util/normalizeTitle';
import anchorToTitle from '../../../wiki/util/anchorToTitle';
import decorateEditSummary from '../../../wiki/util/decorateEditSummary';
import MwApi from '../../../MwApi';
import changeTag from '../../../config/changeTag';

interface FullCopyrightProblemsListingData {
basic: false;
Expand Down Expand Up @@ -55,6 +56,9 @@ export default class CopyrightProblemsListing {
* @return A regular expression.
*/
static get articleCvRegex(): RegExp {
// Acceptable level of danger; global configuration is found only in trusted
// places (see WikiConfiguration documentation).
// eslint-disable-next-line security/detect-non-literal-regexp
return new RegExp( window.InfringementAssistant.wikiConfig.ia.listingWikitextMatch.get() );
}

Expand Down Expand Up @@ -391,6 +395,7 @@ export default class CopyrightProblemsListing {
const newWikitext = await this.addComment( message, indent );

await MwApi.action.postWithEditToken( {
...changeTag( await window.InfringementAssistant.getWikiConfig() ),
action: 'edit',
format: 'json',
formatversion: '2',
Expand Down
2 changes: 2 additions & 0 deletions src/modules/ia/models/CopyrightProblemsPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import normalizeTitle from '../../../wiki/util/normalizeTitle';
import getPageContent from '../../../wiki/util/getPageContent';
import decorateEditSummary from '../../../wiki/util/decorateEditSummary';
import MwApi from '../../../MwApi';
import changeTag from '../../../config/changeTag';

/**
* A class that represents a `Wikipedia:Copyright problems` page, a page that lists
Expand Down Expand Up @@ -172,6 +173,7 @@ export default class CopyrightProblemsPage {
// The `catch` statement here can theoretically create an infinite loop given
// enough race conditions. Don't worry about it too much, though.
await MwApi.action.postWithEditToken( {
...changeTag( await window.InfringementAssistant.getWikiConfig() ),
action: 'edit',
title: listingPage.getPrefixedText(),
...textParameters,
Expand Down
2 changes: 2 additions & 0 deletions src/modules/ia/ui/SinglePageWorkflowDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { TripleCompletionAction } from '../../shared/CompletionAction';
import equalTitle from '../../../util/equalTitle';
import msgEval from '../../../wiki/util/msgEval';
import CCICaseInputWidget from './CCICaseInputWidget';
import changeTag from '../../../config/changeTag';

export interface SinglePageWorkflowDialogData {
page: TitleLike;
Expand Down Expand Up @@ -577,6 +578,7 @@ function initSinglePageWorkflowDialog() {
}

await MwApi.action.postWithEditToken( {
...changeTag( await window.InfringementAssistant.getWikiConfig() ),
action: 'edit',
title: this.page.getPrefixedText(),
text: finalPageContent,
Expand Down
2 changes: 2 additions & 0 deletions src/ui/root/DeputyContributionSurveySection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { generateTrace } from '../../models/DeputyTrace';
import DeputyMessageWidget from '../shared/DeputyMessageWidget';
import sectionHeadingN from '../../wiki/util/sectionHeadingN';
import last from '../../util/last';
import changeTag from '../../config/changeTag';

/**
* The contribution survey section UI element. This includes a list of revisions
Expand Down Expand Up @@ -427,6 +428,7 @@ export default class DeputyContributionSurveySection implements DeputyUIElement
}

return MwApi.action.postWithEditToken( {
...changeTag( await window.deputy.getWikiConfig() ),
action: 'edit',
pageid: this.casePage.pageId,
section: sectionId,
Expand Down
3 changes: 3 additions & 0 deletions src/wiki/TalkPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import normalizeTitle from './util/normalizeTitle';
import decorateEditSummary from './util/decorateEditSummary';
import nsId from './util/nsId';
import MwApi from '../MwApi';
import changeTag from '../config/changeTag';
import WikiConfiguration from '../config/WikiConfiguration';

/**
* Options for performing edits with {@link TalkPage}.
Expand Down Expand Up @@ -57,6 +59,7 @@ export default class TalkPage {
nsId( 'user_talk' )
}, editOptions, {
// Non-overridable options
...changeTag( await WikiConfiguration.load() ),
action: 'edit',
title: this.talkPage.getPrefixedText(),
summary: decorateEditSummary( options.summary ),
Expand Down

0 comments on commit 9ecbc4a

Please sign in to comment.