Skip to content

Commit dfe1d32

Browse files
authored
Merge pull request #18 from Wintus/search-ordering
search ordering
2 parents ac4edec + a1c3ebd commit dfe1d32

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

.vscode/settings.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
{
22
"cSpell.words": [
33
"formatversion",
4+
"oldid",
45
"pageid",
6+
"pageids",
57
"revid",
8+
"revids",
9+
"rvcontentformat",
610
"rvcontinue",
11+
"rvdir",
712
"rvlimit",
8-
"rvprop"
13+
"rvprop",
14+
"rvslots"
915
]
1016
}

src/App.tsx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,7 @@ import {
88
fetchPageId,
99
} from './services/WikipediaAPI';
1010
import { findOneOccurrence } from './utils/RevisionFinder';
11-
import {
12-
defaultSearchResult,
13-
SearchResult,
14-
OnSearchFn,
15-
} from './types';
11+
import { defaultSearchResult, SearchResult, OnSearchFn } from './types';
1612

1713
function App() {
1814
const [searchResult, setSearchResult] =

src/services/WikipediaAPI.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ type WikipediaResponse<Slot extends string = 'main'> = {
2727
};
2828
};
2929

30+
type Order = 'asc' | 'desc';
31+
3032
export const getBaseUrl = (lang: WikiLanguage): string =>
3133
`https://${lang}.wikipedia.org`;
3234

@@ -93,12 +95,18 @@ export async function fetchRevisionTexts(
9395

9496
/**
9597
* Fetches all revisions of a Wikipedia page in batches of 500.
98+
*
99+
* The default order is ascending (= newer last = older first), but can be changed to descending.
100+
*
101+
* see https://www.mediawiki.org/wiki/API:Revisions
96102
*/
97103
export async function fetchAllRevisions(
98104
baseUrl: string,
99-
pageId: number
105+
pageId: number,
106+
order: Order = 'asc'
100107
): Promise<ReadonlyArray<number>> {
101108
const revisions: number[] = [];
109+
const dir = order === 'desc' ? 'older' : 'newer';
102110
try {
103111
let continueParam: string | null = null;
104112
do {
@@ -108,6 +116,7 @@ export async function fetchAllRevisions(
108116
url.searchParams.append('pageids', pageId.toString());
109117
url.searchParams.append('rvprop', 'ids');
110118
url.searchParams.append('rvlimit', '500');
119+
url.searchParams.append('rvdir', dir);
111120
url.searchParams.append('formatversion', '2');
112121
url.searchParams.append('format', 'json');
113122
url.searchParams.append('origin', '*');

0 commit comments

Comments
 (0)