Skip to content

Commit

Permalink
db-tb: update, escape comment fields
Browse files Browse the repository at this point in the history
  • Loading branch information
siddharthvp committed Jun 17, 2021
1 parent f86434c commit 63c007d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 25 deletions.
16 changes: 3 additions & 13 deletions TextExtractor.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const assert = require('assert');
const {bot, TextExtractor} = require('./botbase');
const {preprocessDraftForExtract} = require('./reports/commons')

describe('TextExtractor', () => {
before(function() {
Expand Down Expand Up @@ -33,22 +34,11 @@ Arthur was an fine tailor.
});

it('runs preprocessHook', function () {
let text = `[[User:Example]] 21:09, 30 May 2020 (UTC){{AFC submission|t||ts=20200530210953|u=Harshit567|ns=118|demo=}}
let text = `[[User:Example]] 21:09, 30 May 2020 (UTC){{AfC submission|t||ts=20200530210953|u=Harshit567|ns=118|demo=}}
==References==`;

let extract = TextExtractor.getExtract(text, 250, 500, function(text) {
let wkt = new bot.wikitext(text);
wkt.parseTemplates({
namePredicate: name => {
return /infobox/i.test(name) || name === 'AFC submission';
}
});
for (let template of wkt.templates) {
wkt.removeEntity(template);
}
return wkt.getText();
});
let extract = TextExtractor.getExtract(text, 250, 500, preprocessDraftForExtract);

assert.strictEqual(extract, `[[User:Example]] 21:09, 30 May 2020 (UTC)`);
});
Expand Down
28 changes: 17 additions & 11 deletions db-tabulator/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import { enwikidb, SQLError } from "../db";
import { Template } from "../../mwn/build/wikitext";
import { arrayChunk, lowerFirst, readFile, writeFile } from "../utils";
import { NS_CATEGORY, NS_FILE } from "../namespaces";
const {formatSummary} = require('../reports/commons');

export const BOT_NAME = 'SDZeroBot';
export const TEMPLATE = 'User:SDZeroBot/Database report';
export const TEMPLATE_END = 'User:SDZeroBot/Database report end';
export const TEMPLATE = 'Database report';
export const TEMPLATE_END = 'Database report end';
export const SUBSCRIPTIONS_CATEGORY = 'SDZeroBot database report subscriptions';
export const QUERY_TIMEOUT = 600;
export const FAKE_INPUT_FILE = 'fake-configs.wikitext';
Expand All @@ -27,10 +28,6 @@ export async function fetchQueries(): Promise<Record<string, Query[]>> {
if (pg.ns === 0) { // sanity check: don't work in mainspace
continue;
}
// Only work in bot/op userspaces until BRFA approval
if (!pg.title.startsWith('User:SD0001/') && !pg.title.startsWith('User:SDZeroBot/')) {
continue;
}
let text = pg.revisions[0].content;
allQueries[pg.title] = getQueriesFromText(text, pg.title);
}
Expand All @@ -56,10 +53,6 @@ export async function processQueries(allQueries: Record<string, Query[]>) {
}

export async function fetchQueriesForPage(page: string): Promise<Query[]> {
// Only work in bot/op userspaces until BRFA approval
if (!page.startsWith('User:SD0001/') && !page.startsWith('User:SDZeroBot/')) {
return null;
}
let text = (await bot.read(page))?.revisions?.[0]?.content;
if (!text) {
return null;
Expand Down Expand Up @@ -89,6 +82,7 @@ class Query {
sql: string;
wikilinkConfig: Array<{columnIndex: number, namespace: string, showNamespace: boolean}>;
excerptConfig: Array<{srcIndex: number, destIndex: number, namespace: string, charLimit: number, charHardLimit: number}>;
commentConfig: number[];
warnings: string[] = [];

constructor(template: Template, page: string) {
Expand Down Expand Up @@ -143,6 +137,11 @@ class Query {
return true;
}) || [];

// TODO: show warning on wrong syntax
this.commentConfig = this.getTemplateValue('comments')
?.split(',')
.map(e => parseInt(e.trim()) + 1) || [];

this.excerptConfig = this.getTemplateValue('excerpts')
?.split(',')
.map(e => {
Expand Down Expand Up @@ -255,7 +254,7 @@ class Query {
// Stringify everything
result = this.transformColumn(result, i, (value: string | number | null | Date) => {
if (value === null) return '';
if (value instanceof Date) return value.toISOString(); // is this ever possible?
if (value instanceof Date) return value.toISOString();
return String(value);
});
}
Expand Down Expand Up @@ -307,6 +306,13 @@ class Query {
});
});

// Format edit summaries / log action summaries
this.commentConfig.forEach(columnIndex => {
result = this.transformColumn(result, columnIndex, (value) => {
return formatSummary(value);
});
});

this.getTemplateValue('remove_underscores')?.split(',').forEach(num => {
let columnIndex = parseInt(num.trim());
if (isNaN(columnIndex)) {
Expand Down
2 changes: 2 additions & 0 deletions db-tabulator/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import { ENWIKI_DB_HOST } from "../db";
* Support linkification with ns numbers from another column
*
* Pending:
* Support pagination
* Create Module:Database report for syntax checking
*
*/

Expand Down
2 changes: 1 addition & 1 deletion reports/commons.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ function preprocessDraftForExtract(text) {
let wkt = new bot.wikitext(text);
wkt.parseTemplates({
namePredicate: name => {
return /infobox/i.test(name) || name === 'AFC submission';
return /infobox/i.test(name) || name === 'AfC submission';
}
});
for (let template of wkt.templates) {
Expand Down

0 comments on commit 63c007d

Please sign in to comment.