Skip to content

Commit

Permalink
remove queryAuthors() method which pulls from WikiWho API
Browse files Browse the repository at this point in the history
WikiWho has been dead for quite a while now.
  • Loading branch information
siddharthvp committed Jan 15, 2023
1 parent e20d7bd commit 3dde64e
Show file tree
Hide file tree
Showing 8 changed files with 12 additions and 148 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
Only breaking changes, deprecations and the like are documented in this change log.

#### 2.0.0

- mwn#queryAuthors() and `queryAuthors()` on page objects are removed. They relied on the WikiWho API which is now defunct.

#### 0.11.0

- mwn#queryAuthors() now requires `getSiteInfo()` to have run first. Also, it is deprecated in favour of using the `queryAuthors()` method on a page object.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Mwn works with both JavaScript and TypeScript. It is created with a design philo

This library provides TypeScript type definitions for all its functions, as well as for MediaWiki API request objects (MW core + several extensions). API responses are also typed for the common operations.

In addition to the MediaWiki Action API, methods are provided to talk to the Wikimedia EventStreams API, the ORES API, Pageviews API and WikiWho API.
In addition to the MediaWiki Action API, methods are provided to talk to the Wikimedia EventStreams API, the ORES API and Pageviews API.

This library uses mocha and chai for tests, and has [extensive test coverage](https://coveralls.io/github/siddharthvp/mwn?branch=master). Testing is automated using a CI workflow on GitHub Actions.

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mwn",
"version": "1.11.5",
"version": "2.0.0",
"description": "JavaScript & TypeScript MediaWiki bot framework for Node.js",
"main": "./build/bot.js",
"types": "./build/bot.d.ts",
Expand Down
15 changes: 1 addition & 14 deletions src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ import {
export { MwnDate, MwnTitle, MwnPage, MwnFile, MwnCategory, MwnWikitext, MwnUser, MwnStream, ApiPage, ApiRevision };
// Export, if only for the sake of getting generated documentation
export * from './api_response_types';
export type { PageViewData, PageViewOptions, AuthorshipData } from './page';
export type { PageViewData, PageViewOptions } from './page';
export type { TemplateConfig, Template, MwnWikitextStatic } from './wikitext';

export interface MwnOptions {
Expand Down Expand Up @@ -1887,19 +1887,6 @@ export class mwn {
});
}

/**
* Query the top contributors to the article using the WikiWho API.
* This API has a throttling of 2000 requests a day.
* Supported for EN, DE, ES, EU, TR Wikipedias only
* @see https://api.wikiwho.net/
* @deprecated Use queryAuthors on the page object directly instead
*/
async queryAuthors(
title: string
): Promise<{ totalBytes: number; users: { id: number; name: string; bytes: number; percent: number }[] }> {
return new this.page(title).queryAuthors();
}

/**
* Promisified version of setTimeout
* @param {number} duration - of sleep in milliseconds
Expand Down
103 changes: 1 addition & 102 deletions src/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,7 @@ export interface MwnPage extends MwnTitle {
* @param options
*/
pageViews(options?: PageViewOptions): Promise<PageViewData[]>;
/**
* Query the top contributors to the article using the WikiWho API.
* This API has a throttling of 2000 requests a day.
* Supported for EN, DE, ES, EU, TR Wikipedias only
* @see https://api.wikiwho.net/
*/
queryAuthors(): Promise<AuthorshipData>;

edit(transform: (rev: { content: string; timestamp: string }) => string | ApiEditPageParams): Promise<any>;
save(text: string, summary?: string, options?: ApiEditPageParams): Promise<any>;
newSection(header: string, message: string, additionalParams?: ApiEditPageParams): Promise<any>;
Expand Down Expand Up @@ -555,91 +549,6 @@ export default function (bot: mwn): MwnPageStatic {
});
}

/** @inheritDoc */
async queryAuthors(): Promise<AuthorshipData> {
let langcodematch = bot.options.apiUrl.match(/([^/]*?)\.wikipedia\.org/);
if (!langcodematch || !langcodematch[1]) {
throw new Error('WikiWho API is not supported for bot API URL. Re-check.');
}

let json;
try {
json = await bot
.rawRequest({
url: `https://api.wikiwho.net/${
langcodematch[1]
}/api/v1.0.0-beta/latest_rev_content/${encodeURIComponent(this.toString())}/?editor=true`,
headers: {
'User-Agent': bot.options.userAgent,
},
})
.then((response) => response.data);
} catch (err) {
throw new Error(err && err.response && err.response.data && err.response.data.Error);
}

const tokens = Object.values(json.revisions[0])[0].tokens;

let data: AuthorshipData = {
totalBytes: 0,
users: [],
};
let userdata: {
[editor: string]: {
name?: string;
bytes: number;
percent?: number;
};
} = {};

for (let token of tokens) {
data.totalBytes += token.str.length;
let editor = token['editor'];
if (!userdata[editor]) {
userdata[editor] = { bytes: 0 };
}
userdata[editor].bytes += token.str.length;
if (editor.startsWith('0|')) {
// IP
userdata[editor].name = editor.slice(2);
}
}

Object.entries(userdata).map(([userid, { bytes }]) => {
userdata[userid].percent = bytes / data.totalBytes;
if (userdata[userid].percent < 0.02) {
delete userdata[userid];
}
});

await bot
.request({
action: 'query',
list: 'users',
ususerids: Object.keys(userdata).filter((us) => !us.startsWith('0|')), // don't lookup IPs
})
.then((json) => {
json.query.users.forEach((us: any) => {
userdata[us.userid].name = us.name;
});
});

data.users = Object.entries(userdata)
.map(([userid, { bytes, name, percent }]) => {
return {
id: Number(userid),
name: name,
bytes: bytes,
percent: percent,
};
})
.sort((a, b) => {
return a.bytes < b.bytes ? 1 : -1;
});

return data;
}

/**** Post operations *****/
// Defined in bot.js

Expand Down Expand Up @@ -691,13 +600,3 @@ export interface PageViewData {
agent: string;
views: number;
}

export interface AuthorshipData {
totalBytes: number;
users: Array<{
id: number;
name: string;
bytes: number;
percent: number;
}>;
}
13 changes: 0 additions & 13 deletions tests/suppl.bot.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,4 @@ describe('supplementary functions', function () {
expect(views[0]).to.be.an('object').with.property('timestamp').that.equals('2021010100');
expect(views[1]).to.be.an('object').with.property('timestamp').that.equals('2021020100');
});

it('wikiwho', async function () {
this.timeout(10000);
await bot.getSiteInfo();
let page = new bot.page('Dairy in India');
let data = await page.queryAuthors();
expect(data).to.be.an('object').with.property('totalBytes').that.is.a('number');
expect(data).to.have.property('users').that.is.instanceOf(Array).of.length.greaterThan(1);
expect(data.users[0]).to.be.an('object').with.property('id').that.is.a('number');
expect(data.users[0]).to.have.property('name').that.is.a('string');
expect(data.users[0]).to.have.property('percent').that.is.a('number');
expect(data.users[0]).to.have.property('bytes').that.is.a('number');
});
});
17 changes: 2 additions & 15 deletions website/docs/11-integration-with-other-apis.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Integration with other APIs

Apart from the [MediaWiki API](https://www.mediawiki.org/wiki/API:Main_page), Mwn integrates with a few other APIs:
Apart from the [MediaWiki API](https://www.mediawiki.org/wiki/API:Main_page), Mwn integrates with a few other APIs for Wikimedia wikis:

### ORES

Expand All @@ -10,7 +10,7 @@ Get ORES scores for revisions:
```js
await bot.oresQueryRevisions(
'https://ores.wikimedia.org/', // ORES endpoint URL
['articlequality', 'drafttopic'], // ORES modes
['articlequality', 'drafttopic'], // ORES models
['76923582', '2387429'] // Revision IDs
);
```
Expand Down Expand Up @@ -46,16 +46,3 @@ const pageViewData = await page.pageViews({
```

The [PageViewOptions](https://mwn.toolforge.org/docs/api/interfaces/pageviewoptions.html) argument is optional. Return type is Promise<<a href="https://mwn.toolforge.org/docs/api/interfaces/pageviewdata.html">PageViewData</a>[]>.

### WikiWho

See <https://wikiwho.wmflabs.org/>

Fetch the list of top contributors to an article. Available for limited number of Wikipedias.

```js
const page = new bot.page('Lorem ipsum');
const contributorData = await page.queryAuthors();
```

Return type is Promise<<a href="https://mwn.toolforge.org/docs/api/interfaces/authorshipdata.html">AuthorshipData</a>>.

0 comments on commit 3dde64e

Please sign in to comment.