All files / ext.wikilambda.app/store/stores/wikidata items.js

100% Statements 184/184
97.36% Branches 37/38
100% Functions 13/13
100% Lines 184/184

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 22x 22x 22x 22x 22x 22x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 11x 11x 11x 11x 11x 11x 11x 11x 11x 6x 6x 5x 5x 11x 1x 1x 4x 4x 4x 11x 11x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 5x 5x 5x 5x 5x 56x 56x 2x 2x 54x 54x 54x 56x 56x 11x 11x 11x 11x 11x 43x 43x 5x 5x 126x 126x 126x 126x 126x 126x 126x 126x 126x 7x 7x 7x 7x 7x 15x 7x 7x 7x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 5x 5x 3x 3x 3x 2x 2x 2x 126x 126x 126x 126x 126x 126x 126x 126x 126x 2x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 15x 15x 15x 11x 11x 8x 8x 8x 8x 8x 8x 8x 8x 11x 11x 11x 15x 15x 15x 126x 126x  
/*!
 * WikiLambda Pinia store: Wikidata Items module
 *
 * @copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
 * @license MIT
 */
'use strict';
 
const Constants = require( '../../../Constants.js' );
const LabelData = require( '../../classes/LabelData.js' );
const { isWikidataQid } = require( '../../../utils/wikidataUtils.js' );
 
module.exports = {
	state: {
		items: {},
		scheduledItems: [],
		scheduledItemsPromise: null
	},
 
	getters: {
		/**
		 * Returns the Wikidata Item data given its Id,
		 * the fetch Promise if the fetch request is on the fly,
		 * or undefined if it hasn't been requested yet.
		 *
		 * @param {Object} state
		 * @return {Function}
		 */
		getItemData: function ( state ) {
			/**
			 * @param {string} id
			 * @return {Object|Promise|undefined}
			 */
			const findItemData = ( id ) => state.items[ id ];
			return findItemData;
		},
 
		/**
		 * Returns a promise that resolves to the Wikidata Item data given its Id.
		 * If the item is already cached, returns a resolved promise.
		 * If the item is being fetched, returns the existing promise.
		 * If the item hasn't been requested, returns a rejected promise.
		 *
		 * @param {Object} state
		 * @return {Function}
		 */
		getItemDataAsync: function () {
			/**
			 * @param {string} id
			 * @return {Promise<Object>}
			 */
			const getItemDataAsync = ( id ) => {
				const itemData = this.getItemData( id );
 
				// If item is already cached (not a promise), return resolved promise
				if ( itemData && typeof itemData.then !== 'function' ) {
					return Promise.resolve( itemData );
				}
 
				// If item is being fetched (is a promise), return that promise
				if ( itemData && typeof itemData.then === 'function' ) {
					return itemData;
				}
 
				// If item hasn't been requested, return rejected promise
				return Promise.reject( new Error( `Item ${ id } not found` ) );
			};
			return getItemDataAsync;
		},
 
		/**
		 * Returns the LabelData object built from the available
		 * labels in the data object of the selected Wikidata Item.
		 * If an Item is selected but it has no labels, returns
		 * LabelData object with the Wikidata Item id as its label.
		 * If no Wikidata Item is selected, returns undefined.
		 *
		 * @param {Object} state
		 * @return {LabelData|undefined}
		 */
		getItemLabelData: function () {
			/**
			 * @param {string} id The item ID
			 * @return {LabelData} The `LabelData` object containing label, language code, and directionality.
			 */
			const findItemLabelData = ( id ) => {
				// If no selected item, return undefined
				if ( !id ) {
					return undefined;
				}
				// If no itemData yet, return item Id
				// Get best label from labels (if any)
				const itemData = this.getItemData( id );
				const langs = itemData ? Object.keys( itemData.labels || {} ) : {};
				if ( langs.length > 0 ) {
					const label = langs.includes( this.getUserLangCode ) ?
						itemData.labels[ this.getUserLangCode ] :
						itemData.labels[ langs[ 0 ] ];
					return new LabelData( id, label.value, null, label.language );
				}
				// Else, return item Id as label
				return new LabelData( id, id, null );
			};
			return findItemLabelData;
		},
 
		/**
		 * Returns the URL for a given item ID.
		 *
		 * @param {Object} state
		 * @return {Function}
		 */
		getItemUrl: function () {
			/**
			 * @param {string} id
			 * @return {string|undefined}
			 */
			const findItemUrl = ( id ) => isWikidataQid( id ) ?
				`${ Constants.WIKIDATA_BASE_URL }/wiki/${ id }` :
				undefined;
			return findItemUrl;
		}
	},
 
	actions: {
		/**
		 * Stores the Wikidata item data indexed by its Id
		 *
		 * @param {Object} payload
		 * @param {string} payload.id
		 * @param {Object} payload.data
		 * @return {undefined}
		 */
		setItemData: function ( payload ) {
			// If payload.data is a promise, store it directly
			if ( payload.data && typeof payload.data.then === 'function' ) {
				this.items[ payload.id ] = payload.data;
				return;
			}
			// Otherwise, unwrap the data to select only subset of Wikidata Item data; title, labels and descriptions
			const unwrap = ( { title, labels, descriptions } ) => ( { title, labels, descriptions } );
			this.items[ payload.id ] = unwrap( payload.data );
		},
 
		/**
		 * Removes the items for the given IDs
		 *
		 * @param {Object} payload
		 * @param {Array<string>} payload.ids - An array of Wikidata Item IDs
		 */
		resetItemData: function ( payload ) {
			payload.ids.forEach( ( id ) => delete this.items[ id ] );
		},
 
		/**
		 * Calls Wikidata Action API to fetch Wikidata Items
		 * given their Ids.
		 *
		 * @param {Object} payload
		 * @param {Array<string>} payload.ids - An array of Wikidata Item IDs to fetch.
		 * @return {Promise} - A promise that resolves to the fetched data.
		 */
		fetchItems: function ( { ids } ) {
			this.scheduledItems = [ ... new Set( [ ...this.scheduledItems, ...ids ] ) ];
 
			if ( !this.scheduledItemsPromise ) {
				this.scheduledItemsPromise = new Promise( ( resolve, reject ) => {
					setTimeout( () => {
						this.fetchWikidataEntitiesBatched( {
							ids: this.scheduledItems,
							getData: this.getItemData,
							setData: this.setItemData,
							resetData: this.resetItemData
						} ).then( resolve, reject );
						this.scheduledItems = [];
						this.scheduledItemsPromise = null;
					}, Constants.WIKIDATA_REQUEST_TIME_WINDOW );
				} );
			}
 
			return this.scheduledItemsPromise;
		}
	}
};