Skip to content

Commit

Permalink
most-gans: fix credits for signatures with HTML entities in them
Browse files Browse the repository at this point in the history
  • Loading branch information
siddharthvp committed May 3, 2024
1 parent 15de771 commit d5b8a5e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 12 deletions.
22 changes: 14 additions & 8 deletions most-gans/generate-initial.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { bot, log } from '../botbase';
import { argv, bot, log } from '../botbase';
import { closeTunnels, createLocalSSHTunnel } from "../utils";
import { TOOLS_DB_HOST } from "../db";
import { processArticle, TABLE, db, runManualEdits } from "./model";
import { processArticle, db, runManualEdits } from "./model";
import { restartDeployment } from "../k8s";

bot.setOptions({
Expand All @@ -13,6 +13,8 @@ bot.setOptions({
});

(async function () {
const TABLE = 'nominators2';

await createLocalSSHTunnel(TOOLS_DB_HOST);
await bot.getSiteInfo();

Expand All @@ -37,11 +39,13 @@ bot.setOptions({
PRIMARY KEY (article)
) COLLATE 'utf8_unicode_ci'`);

// Restart the stream task, so that new GAs are processed
restartDeployment('stream').catch(err => {
log(`[E] Failed to restart stream task`);
log(err);
});
if (!argv.test) {
// Restart the stream task, so that new GAs are processed
restartDeployment('stream').catch(err => {
log(`[E] Failed to restart stream task`);
log(err);
});
}

let authorNotFound = [];

Expand Down Expand Up @@ -69,7 +73,9 @@ ${authorNotFound.map(p => `#[[${p}]]`).join('\n')}
`);
log(`[S] Saved error list on-wiki`);

await runManualEdits();
if (!argv.test) {
await runManualEdits();
}
closeTunnels();

})();
15 changes: 12 additions & 3 deletions most-gans/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { bot, log, toolsdb } from "../botbase";
import { NS_USER, NS_USER_TALK } from "../namespaces";
import { createLogStream } from "../utils";
import * as fs from "fs";
import { JSDOM } from 'jsdom';

export const db = new toolsdb('goodarticles_p');
export const TABLE = 'nominators';
Expand Down Expand Up @@ -82,11 +83,19 @@ function addToDb(article: string, nom: string, date, fallbackStrategy = false):
return Promise.resolve([nom, date_str, fallbackStrategy]);
}

const { document } = new JSDOM('<html></html>').window;

export function decodeHtmlEntities(str) {
const d = document.createElement('div');
d.innerHTML = str;
return d.innerHTML;
}

function getUsernameFromSignature(sig: string) {
if (typeof sig !== 'string') {
return;
}
let wkt = new bot.wikitext(sig);
let wkt = new bot.wikitext(decodeHtmlEntities(sig));
wkt.parseLinks();
let userPageLinks = [], userTalkPageLinks = [];
wkt.links.forEach(link => {
Expand Down Expand Up @@ -144,8 +153,8 @@ export async function getCurrentTitle(title: string, date: string): Promise<stri
export async function getCurrentUsername(username: string, date: string): Promise<string> {
// This is called during the stream task as well, avoid API call in such cases
const dateParsed = new bot.date(date);
// if now - dateParsed < 30 seconds
if (new bot.date().subtract(30, 'seconds').isBefore(dateParsed)) {
// if now - dateParsed < 2 minutes
if (new bot.date().subtract(2, 'minutes').isBefore(dateParsed)) {
return username;
}
const rename = (await bot.query({
Expand Down
12 changes: 11 additions & 1 deletion most-gans/test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import assert = require("assert");
import { bot } from "../botbase";
import {getCurrentUsername, processArticle, db, getCurrentTitle} from "./model";
import {getCurrentUsername, processArticle, db, getCurrentTitle, decodeHtmlEntities} from "./model";

describe('most-gans', () => {
before(() => {
Expand All @@ -15,6 +15,9 @@ describe('most-gans', () => {
['Fight for This Love', 'Lil-unique1', '2010-06-25'], // has rev-delled talkpage revs
['Norman Finkelstein', 'Giggy'],
['Etchmiadzin Cathedral', 'Yerevantsi', '2013-12-27'], // user renamed between nomination and promotion
// ['Panagiotis Stamatakis', 'UndercoverClassicist', '2023-02-04'],
// ['Serious Sam: The First Encounter', 'IceWelder', '2023-10-30'],
['The Wing of Madoola', 'KGRAMR', '2023-12-28'],
];
for (let [article, nomExpected, dateExpected] of testCases) {
const [nom, date] = await processArticle(article);
Expand Down Expand Up @@ -50,4 +53,11 @@ describe('most-gans', () => {
assert.strictEqual(await getCurrentTitle('Shivers (song)', '2014-11-19'), 'Shivers (The Boys Next Door song)');
});

it('decodeHtmlEntities', function () {
assert.strictEqual(
decodeHtmlEntities('[[User:NoD&#39;ohnuts|NoD&#39;ohnuts]] ([[User talk:NoD&#39;ohnuts|talk]])'),
"[[User:NoD'ohnuts|NoD'ohnuts]] ([[User talk:NoD'ohnuts|talk]])"
);
});

});

0 comments on commit d5b8a5e

Please sign in to comment.