Support other ways to get the description of the page

Currently, the extension queries the prop=description api to fetch the
description, but this api is only available if Wikibaseclient is installed.

Let's support other sources to fetch the page description from, too, or
disable it (by default) to sabe 85 bytes from each response by avoiding the
warning returned from the api when prop=description is not recognized.

A new configuration setting has been added: RelatedArticlesDescriptionSource.
It defaults to false (no description is fetched), but can be set to one of
those string values:

- wikidata: What's being used right now on WMF wikis
- textextracts: Extension:TextExtracts provides the prop=extracts api.
- pagedescription: Extension:Description2 provides a description page property, that can be queried by prop=pageprops&ppprop=description

Bug: T230947
Change-Id: I34410334ba9d6db1f686c7efb5722e2a51957145
This commit is contained in:
Jesús Martínez Novo 2019-08-23 20:15:32 +02:00
parent 95a2d7a7ba
commit be3385e6d5
4 changed files with 30 additions and 6 deletions

View file

@ -101,7 +101,8 @@
"index.js",
{ "name": "data.json", "config": {
"useCirrusSearch": "RelatedArticlesUseCirrusSearch",
"onlyUseCirrusSearch": "RelatedArticlesOnlyUseCirrusSearch"
"onlyUseCirrusSearch": "RelatedArticlesOnlyUseCirrusSearch",
"descriptionSource": "RelatedArticlesDescriptionSource"
} }
],
"dependencies": [
@ -146,6 +147,8 @@
"RelatedArticlesCardLimit": 3,
"RelatedArticlesUseCirrusSearch": false,
"RelatedArticlesOnlyUseCirrusSearch": false,
"@RelatedArticlesDescriptionSource": "Source to get the page description from (string, or false to not fetch the description). Possible values: wikidata, textextracts, pagedescription.",
"RelatedArticlesDescriptionSource": false,
"@RelatedArticlesFooterWhitelistedSkins": "List of skin names (e.g. 'minerva') where related articles will be shown in the footer.",
"RelatedArticlesFooterWhitelistedSkins": []
},

View file

@ -7,7 +7,8 @@
mw.config.get( 'wgPageName' ),
mw.config.get( 'wgRelatedArticles' ),
data.useCirrusSearch,
data.onlyUseCirrusSearch
data.onlyUseCirrusSearch,
data.descriptionSource
),
// Make sure this is never undefined as I'm paranoid
LIMIT = mw.config.get( 'wgRelatedArticlesCardLimit', 3 ),

View file

@ -10,17 +10,20 @@
* @param {Array|null} editorCuratedPages a list of pages curated by editors for the current page
* @param {boolean} useCirrusSearch whether to hit the API when no editor-curated pages are available
* @param {boolean} [onlyUseCirrusSearch=false] whether to ignore the list of editor-curated pages
* @param {boolean|string} [descriptionSource=false] source to get the page description from
*/
function RelatedPagesGateway(
api,
currentPage,
editorCuratedPages,
useCirrusSearch,
onlyUseCirrusSearch
onlyUseCirrusSearch,
descriptionSource
) {
this.api = api;
this.currentPage = currentPage;
this.useCirrusSearch = useCirrusSearch;
this.descriptionSource = descriptionSource;
if ( onlyUseCirrusSearch ) {
editorCuratedPages = [];
@ -56,7 +59,7 @@
*
* * The ID of the page corresponding to the title
* * The thumbnail, if any
* * The Wikidata description, if any
* * The page description, if any
*
* @method
* @param {number} limit of pages to get. Should be between 1-20.
@ -66,13 +69,29 @@
var parameters = {
action: 'query',
formatversion: 2,
prop: 'pageimages|description',
prop: 'pageimages',
piprop: 'thumbnail',
pithumbsize: 160 // FIXME: Revert to 80 once pithumbmode is implemented
},
// Enforce limit
relatedPages = this.editorCuratedPages.slice( 0, limit );
switch ( this.descriptionSource ) {
case 'wikidata':
parameters.prop += '|description';
break;
case 'textextracts':
parameters.prop += '|extracts';
parameters.exsentences = '1';
parameters.exintro = '1';
parameters.explaintext = '1';
break;
case 'pagedescription':
parameters.prop += '|pageprops';
parameters.ppprop = 'description';
break;
}
if ( relatedPages.length ) {
parameters.pilimit = relatedPages.length;
parameters.continue = '';

View file

@ -18,7 +18,8 @@
title: page.title,
url: mw.util.getUrl( page.title ),
hasThumbnail: false,
extract: page.description
extract: ( page.description || page.extract ||
( page.pageprops ? page.pageprops.description : '' ) )
};
if ( page.thumbnail ) {