@@ -27,6 +27,8 @@ type WikipediaResponse<Slot extends string = 'main'> = {
2727 } ;
2828} ;
2929
30+ type Order = 'asc' | 'desc' ;
31+
3032export 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 */
97103export 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