Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New method getArticleProperties() #345

Merged
merged 1 commit into from
Jul 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/bot.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {

// responses typing
ArticleInfo,
ArticleProperties,
PageEditedResult,
PageInCategory,
RedirectInfo,
Expand Down Expand Up @@ -43,6 +44,7 @@ declare class Bot {
edit( title: string, content: string, summary: string, minor: boolean, callback: NodeJSCallback<PageEditedResult> ): void;
getArticle( article: string, callback: NodeJSCallback<string> ): void;
getArticle( article: string, followRedirect: boolean, callback: NodeJSCallbackDouble<string, RedirectInfo> ): void;
getArticleProperties(title: string, callback: NodeJSCallback<ArticleProperties>): void;
getMediaWikiVersion( callback: NodeJSCallback<string> ): void;
getPagesInCategory( category: string, callback: NodeJSCallback<PageInCategory[]>): void;
logIn( callback: NodeJSCallback<any>): void;
Expand Down
26 changes: 26 additions & 0 deletions lib/bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,32 @@ class Bot {
} );
}

getArticleProperties( title, callback ) {
// https://en.wikipedia.org/w/api.php?action=query&format=json&redirects=1&titles=Saksun&prop=pageprops
const params = {
action: 'query',
prop: 'pageprops',
titles: title
};

this.api.call( params, ( err, data ) => {
if ( err ) {
callback( err );
return;
}

/**
* page_image_free: 'Einstein_1921_by_F_Schmutzer_-_restoration.jpg',
* 'wikibase-badge-Q17437798': '1',
* 'wikibase-shortdesc': 'German-born scientist (1879–1955)',
* wikibase_item: 'Q937'
*/
const properties = getFirstItem( data.pages );

callback( err, properties.pageprops );
} );
}

getArticleInfo( title, options, callback ) {
if ( typeof options === 'function' ) { // This is the callback; options was nonexistant
callback = options;
Expand Down
6 changes: 6 additions & 0 deletions lib/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,9 @@ export interface ArticleInfo {
displaytitle: string;
varianttitles: Map<string, string>;
}

export interface ArticleProperties {
page_image_free: string;
'wikibase-shortdesc': string;
wikibase_item: string;
}
10 changes: 10 additions & 0 deletions test/mediawiki-api-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ describe( 'MediaWiki API', () => {
done();
} );
}, 5000 );

it( 'getArticleProperties() returns article properties', ( done ) => {
client.getArticleProperties( TEST_ARTICLE, ( err, props ) => {
expect( err ).toBeNull();
expect( props.wikibase_item ).toEqual( 'Q937' );
expect( props[ 'wikibase-shortdesc' ] ).toContain( 'scientist' );

done();
} );
}, 5000 );
} );

// FIXME: use a proxy when running on CI
Expand Down