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

92.65% Statements 290/313
90.47% Branches 76/84
100% Functions 11/11
92.65% Lines 290/313

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 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 5x 126x 126x 126x 126x 126x 126x 126x 126x 126x 6x 6x 6x 6x 6x 6x 10x 10x 10x 2x 10x 10x 10x 2x 10x 10x 10x   10x 10x 10x 2x 10x 10x 10x 2x 10x 10x 2x 10x 6x 6x 126x 126x 126x 126x 126x 126x 126x 126x 6x 6x 6x 6x 6x 6x 19x 19x 19x 4x 19x 19x 19x 4x 19x 19x 19x 2x 19x 19x 19x 5x 19x 19x 19x 4x 19x 19x   19x 6x 6x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 9x 9x 9x 9x 9x 9x 9x 9x 9x 2x 2x 9x 9x 9x 2x 2x 2x 2x 2x 2x 2x 1x 1x 2x 1x 2x 2x 9x 9x 9x                           9x 9x 9x 2x 2x 9x 9x 9x 2x 2x 9x 1x 9x 9x 9x 9x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 11x 11x 11x 11x 2x 2x 2x 9x 9x 9x 9x 10x 11x 11x 9x 9x 11x 11x 11x 11x 11x 11x 10x 10x 1x 1x 1x 8x 10x 10x 10x 10x 10x     10x 10x 1x 1x 1x 9x 9x 10x 10x 11x 11x 2x 2x 11x 9x 9x 9x 11x 11x 11x 11x 11x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 6x 6x 6x 2x 6x 6x 6x 1x 1x 1x 1x 1x 6x 6x 6x             6x 6x 6x 1x 6x 6x 6x 1x 6x 6x 1x 6x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 8x 8x 8x 8x 8x 8x 8x 126x 126x  
/*!
 * WikiLambda Pinia store: Wikidata Entities module
 *
 * @copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
 * @license MIT
 */
 
const { searchWikidataEntities, fetchWikidataEntities } = require( '../../../utils/apiUtils.js' );
const Constants = require( '../../../Constants.js' );
 
module.exports = {
	state: {},
	getters: {
		/**
		 * Returns the batch size for Wikidata API requests
		 *
		 * @return {number}
		 */
		getWikidataBatchSize: function () {
			return Constants.API_LIMIT_WIKIDATA;
		},
		/**
		 * Returns the label data for a Wikidata entity by type and id.
		 *
		 * @param {string} type - The Wikidata entity type.
		 * @param {string} id - The Wikidata entity id.
		 * @return {LabelData|undefined} The label data object, or undefined if not found.
		 */
		getWikidataEntityLabelData: function () {
			/**
			 * @param {string} type
			 * @param {string} id
			 * @return {LabelData|undefined}
			 */
			const findWikidataEntityLabelData = ( type, id ) => {
				switch ( type ) {
					case Constants.Z_WIKIDATA_LEXEME:
					case Constants.Z_WIKIDATA_REFERENCE_LEXEME:
						return this.getLexemeLabelData( id );
 
					case Constants.Z_WIKIDATA_LEXEME_FORM:
					case Constants.Z_WIKIDATA_REFERENCE_LEXEME_FORM:
						return this.getLexemeFormLabelData( id );
 
					case Constants.Z_WIKIDATA_LEXEME_SENSE:
					case Constants.Z_WIKIDATA_REFERENCE_LEXEME_SENSE:
						return this.getLexemeSenseLabelData( id );
 
					case Constants.Z_WIKIDATA_ITEM:
					case Constants.Z_WIKIDATA_REFERENCE_ITEM:
						return this.getItemLabelData( id );
 
					case Constants.Z_WIKIDATA_PROPERTY:
					case Constants.Z_WIKIDATA_REFERENCE_PROPERTY:
						return this.getPropertyLabelData( id );
 
					default:
						return undefined;
				}
			};
			return findWikidataEntityLabelData;
		},
 
		/**
		 * Returns the URL for a Wikidata entity by type and id.
		 *
		 * @return {Function}
		 */
		getWikidataEntityUrl: function () {
			/**
			 * @param {string} type
			 * @param {string} id
			 * @return {LabelData|undefined}
			 */
			const findWikidataEntityUrl = ( type, id ) => {
				switch ( type ) {
					case Constants.Z_WIKIDATA_LEXEME:
					case Constants.Z_WIKIDATA_REFERENCE_LEXEME:
						return this.getLexemeUrl( id );
 
					case Constants.Z_WIKIDATA_LEXEME_FORM:
					case Constants.Z_WIKIDATA_REFERENCE_LEXEME_FORM:
						return this.getLexemeFormUrl( id );
 
					case Constants.Z_WIKIDATA_LEXEME_SENSE:
					case Constants.Z_WIKIDATA_REFERENCE_LEXEME_SENSE:
						return this.getLexemeSenseUrl( id );
 
					case Constants.Z_WIKIDATA_ITEM:
					case Constants.Z_WIKIDATA_REFERENCE_ITEM:
						return this.getItemUrl( id );
 
					case Constants.Z_WIKIDATA_PROPERTY:
					case Constants.Z_WIKIDATA_REFERENCE_PROPERTY:
						return this.getPropertyUrl( id );
 
					default:
						return undefined;
				}
			};
			return findWikidataEntityUrl;
		},
 
		/**
		 * Returns a promise that resolves to the Wikidata entity data by type and id.
		 * If the entity is already cached, returns a resolved promise.
		 * If the entity is being fetched, returns the existing promise.
		 * If the entity hasn't been requested, returns a rejected promise.
		 *
		 * @return {Function}
		 */
		getWikidataEntityDataAsync: function () {
			/**
			 * @param {string} type
			 * @param {string} id
			 * @return {Promise<Object>}
			 */
			const findWikidataEntityDataAsync = ( type, id ) => {
				switch ( type ) {
					case Constants.Z_WIKIDATA_LEXEME:
					case Constants.Z_WIKIDATA_REFERENCE_LEXEME: {
						return this.getLexemeDataAsync( id );
					}
 
					case Constants.Z_WIKIDATA_LEXEME_FORM:
					case Constants.Z_WIKIDATA_REFERENCE_LEXEME_FORM: {
						const [ lexemeId ] = id.split( '-' );
						return this.getLexemeDataAsync( lexemeId ).then( () => {
							const lexemeData = this.getLexemeData( lexemeId );
 
							if ( lexemeData && lexemeData.forms ) {
								const formData = lexemeData.forms.find( ( item ) => item.id === id );
								if ( formData ) {
									return formData;
								}
							}
							throw new Error( `Lexeme form ${ id } not found` );
						} );
					}
 
					case Constants.Z_WIKIDATA_LEXEME_SENSE:
					case Constants.Z_WIKIDATA_REFERENCE_LEXEME_SENSE: {
						const [ lexemeId ] = id.split( '-' );
						return this.getLexemeSensesDataAsync( lexemeId ).then( () => {
							const sensesData = this.getLexemeSensesData( lexemeId );

							if ( sensesData && Array.isArray( sensesData ) ) {
								const senseData = sensesData.find( ( item ) => item.id === id );
								if ( senseData ) {
									return senseData;
								}
							}
							throw new Error( `Lexeme sense ${ id } not found` );
						} );
					}
 
					case Constants.Z_WIKIDATA_ITEM:
					case Constants.Z_WIKIDATA_REFERENCE_ITEM: {
						return this.getItemDataAsync( id );
					}
 
					case Constants.Z_WIKIDATA_PROPERTY:
					case Constants.Z_WIKIDATA_REFERENCE_PROPERTY: {
						return this.getPropertyDataAsync( id );
					}
					default:
						return Promise.reject( new Error( `Unknown entity type: ${ type }` ) );
				}
			};
			return findWikidataEntityDataAsync;
		}
	},
 
	actions: {
		/**
		 * Generic batching method for Wikidata entity fetching.
		 * Splits IDs into batches and makes parallel requests.
		 *
		 * @param {Object} payload
		 * @param {Array<string>} payload.ids - Array of Wikidata entity IDs to fetch
		 * @param {Function} payload.getData - Function to get cached data for an ID
		 * @param {Function} payload.setData - Function to set data for an ID
		 * @param {Function} payload.resetData - Function to reset data for IDs
		 * @return {Promise} - A promise that resolves when all batches complete
		 */
		fetchWikidataEntitiesBatched: function ( { ids, getData, setData, resetData } ) {
			// Filter out the fetched or fetching entity IDs
			const filteredIds = ids.filter( ( id ) => getData( id ) === undefined );
 
			if ( !filteredIds.length ) {
				// If list is empty, do nothing
				return Promise.resolve();
			}
 
			// Split into batches of up to Wikidata API limit
			const BATCH_SIZE = this.getWikidataBatchSize;
			const batches = [];
			for ( let i = 0; i < filteredIds.length; i += BATCH_SIZE ) {
				batches.push( filteredIds.slice( i, i + BATCH_SIZE ) );
			}
 
			const batchRequests = batches.map( ( batchIds ) => {
				const request = {
					language: this.getUserLangCode,
					ids: batchIds.join( '|' )
				};
				return fetchWikidataEntities( request )
					.then( ( data ) => {
						// It might return an error for invalid IDs
						if ( data.error ) {
							resetData( { ids: batchIds } );
							return data;
						}
						// Once received, store entity IDs with their data
						const fetched = data.entities ? Object.keys( data.entities ) : [];
						fetched.forEach( ( id ) => {
							const entity = data.entities[ id ];
							// If entity is undefined, do nothing
							if ( !entity ) {
								return;
							}
							// If entity has a 'missing' property, reset the data
							if ( typeof entity === 'object' && 'missing' in entity ) {
								resetData( { ids: [ id ] } );
								return;
							}
							// Otherwise, store the entity data
							setData( { id, data: entity } );
						} );
						return data;
					} )
					.catch( () => {
						// If fetch fails, remove the IDs from the state
						resetData( { ids: batchIds } );
					} );
			} );
 
			// If only one batch, return that directly, otherwise wait for all
			const resultPromise = batchRequests.length === 1 ? batchRequests[ 0 ] : Promise.all( batchRequests );
 
			// Store entity IDs with their resolving promise
			filteredIds.forEach( ( id ) => setData( { id, data: resultPromise } ) );
			return resultPromise;
		},
 
		/**
		 * Fetches Wikidata entities by type.
		 *
		 * For lexeme forms, transforms form IDs into lexeme IDs before fetching.
		 * Dispatches to the appropriate fetch action based on the entity type.
		 *
		 * @param {Object} payload
		 * @param {string} payload.type - The Wikidata entity type.
		 * @param {Array} payload.ids - The list of entity IDs to fetch.
		 * @return {Promise|undefined} A promise resolving to the fetched entities, or undefined for unknown types.
		 */
		fetchWikidataEntitiesByType: function ( payload ) {
			switch ( payload.type ) {
				case Constants.Z_WIKIDATA_LEXEME:
				case Constants.Z_WIKIDATA_REFERENCE_LEXEME:
					return this.fetchLexemes( payload );
 
				case Constants.Z_WIKIDATA_LEXEME_FORM:
				case Constants.Z_WIKIDATA_REFERENCE_LEXEME_FORM:
					// Transform lexeme form IDs into lexeme IDs:
					payload.ids = payload.ids
						.filter( ( id ) => typeof id === 'string' )
						.map( ( id ) => id.split( '-' )[ 0 ] );
					return this.fetchLexemes( payload );
 
				case Constants.Z_WIKIDATA_LEXEME_SENSE:
				case Constants.Z_WIKIDATA_REFERENCE_LEXEME_SENSE:
					// Transform lexeme sense IDs into lexeme IDs:
					payload.ids = payload.ids
						.filter( ( id ) => typeof id === 'string' )
						.map( ( id ) => id.split( '-' )[ 0 ] );
					this.fetchLexemes( payload );
					return this.fetchLexemeSenses( { lexemeIds: payload.ids } );
 
				case Constants.Z_WIKIDATA_ITEM:
				case Constants.Z_WIKIDATA_REFERENCE_ITEM:
					return this.fetchItems( payload );
 
				case Constants.Z_WIKIDATA_PROPERTY:
				case Constants.Z_WIKIDATA_REFERENCE_PROPERTY:
					return this.fetchProperties( payload );
 
				default:
					return undefined;
			}
		},
 
		/**
		 * Calls Wikidata Action API to search entities by
		 * matching the given searchTerm and entity type.
		 *
		 * @param {Object} payload
		 * @param {string} search
		 * @param {number} searchContinue
		 * @param {string|undefined} type
		 * @param {AbortSignal} [signal] Optional AbortSignal to cancel the request
		 * @return {Promise}
		 */
		lookupWikidataEntities: function ( payload ) {
			const request = Object.assign(
				{},
				payload,
				{ language: this.getUserLangCode }
			);
			return searchWikidataEntities( request );
		}
	}
};