| 
									
										
										
										
											2021-01-02 16:04:16 +01:00
										 |  |  | import {ImagesInCategory, Wikidata, Wikimedia} from "../Web/Wikimedia"; | 
					
						
							|  |  |  | import {UIEventSource} from "../UIEventSource"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * There are multiple way to fetch images for an object | 
					
						
							|  |  |  |  * 1) There is an image tag | 
					
						
							|  |  |  |  * 2) There is an image tag, the image tag contains multiple ';'-seperated URLS | 
					
						
							|  |  |  |  * 3) there are multiple image tags, e.g. 'image', 'image:0', 'image:1', and 'image_0', 'image_1' - however, these are pretty rare so we are gonna ignore them | 
					
						
							|  |  |  |  * 4) There is a wikimedia_commons-tag, which either has a 'File': or a 'category:' containing images | 
					
						
							|  |  |  |  * 5) There is a wikidata-tag, and the wikidata item either has an 'image' attribute or has 'a link to a wikimedia commons category' | 
					
						
							|  |  |  |  * 6) There is a wikipedia article, from which we can deduct the wikidata item | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * For some images, author and license should be shown | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Class which search for all the possible locations for images and which builds a list of UI-elements for it. | 
					
						
							|  |  |  |  * Note that this list is embedded into an UIEVentSource, ready to put it into a carousel. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2021-03-26 03:24:58 +01:00
										 |  |  | export class ImageSearcher extends UIEventSource<{ key: string, url: string }[]> { | 
					
						
							| 
									
										
										
										
											2021-01-02 16:04:16 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-26 03:33:42 +01:00
										 |  |  |     private static _cache = new Map<string, ImageSearcher>(); | 
					
						
							| 
									
										
										
										
											2021-01-02 16:04:16 +01:00
										 |  |  |     private readonly _wdItem = new UIEventSource<string>(""); | 
					
						
							|  |  |  |     private readonly _commons = new UIEventSource<string>(""); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-26 03:24:58 +01:00
										 |  |  |     private constructor(tags: UIEventSource<any>, imagePrefix = "image", loadSpecial = true) { | 
					
						
							| 
									
										
										
										
											2021-01-29 03:23:53 +01:00
										 |  |  |         super([]) | 
					
						
							| 
									
										
										
										
											2021-01-02 16:04:16 +01:00
										 |  |  |         const self = this; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         function AddImages(images: { key: string, url: string }[]) { | 
					
						
							| 
									
										
										
										
											2021-01-29 03:23:53 +01:00
										 |  |  |             const oldUrls = self.data.map(kurl => kurl.url); | 
					
						
							| 
									
										
										
										
											2021-01-02 16:04:16 +01:00
										 |  |  |             let somethingChanged = false; | 
					
						
							|  |  |  |             for (const image of images) { | 
					
						
							|  |  |  |                 const url = image.url; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 if (url === undefined || url === null || url === "") { | 
					
						
							|  |  |  |                     continue; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 if (oldUrls.indexOf(url) >= 0) { | 
					
						
							|  |  |  |                     // Already exists
 | 
					
						
							|  |  |  |                     continue; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-29 03:23:53 +01:00
										 |  |  |                 self.data.push(image); | 
					
						
							| 
									
										
										
										
											2021-01-02 16:04:16 +01:00
										 |  |  |                 somethingChanged = true; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             if (somethingChanged) { | 
					
						
							| 
									
										
										
										
											2021-01-29 03:23:53 +01:00
										 |  |  |                 self.ping(); | 
					
						
							| 
									
										
										
										
											2021-01-02 16:04:16 +01:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-26 03:33:42 +01:00
										 |  |  |         function addImage(image: string) { | 
					
						
							|  |  |  |             AddImages([{url: image, key: undefined}]); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-02 16:04:16 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // By wrapping this in a UIEventSource, we prevent multiple queries of loadWikiData
 | 
					
						
							|  |  |  |         this._wdItem.addCallback(wdItemContents => { | 
					
						
							| 
									
										
										
										
											2021-03-26 03:33:42 +01:00
										 |  |  |             ImageSearcher.loadWikidata(wdItemContents, addImage); | 
					
						
							| 
									
										
										
										
											2021-01-02 16:04:16 +01:00
										 |  |  |         }); | 
					
						
							|  |  |  |         this._commons.addCallback(commonsData => { | 
					
						
							| 
									
										
										
										
											2021-03-26 03:33:42 +01:00
										 |  |  |             ImageSearcher.LoadCommons(commonsData, addImage) | 
					
						
							| 
									
										
										
										
											2021-01-02 16:04:16 +01:00
										 |  |  |         }); | 
					
						
							|  |  |  |         tags.addCallbackAndRun(tags => { | 
					
						
							| 
									
										
										
										
											2021-03-26 03:33:42 +01:00
										 |  |  |             AddImages(ImageSearcher.LoadImages(tags, imagePrefix)); | 
					
						
							| 
									
										
										
										
											2021-01-02 16:04:16 +01:00
										 |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (loadSpecial) { | 
					
						
							|  |  |  |             tags.addCallbackAndRun(tags => { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 const wdItem = tags.wikidata; | 
					
						
							|  |  |  |                 if (wdItem !== undefined) { | 
					
						
							|  |  |  |                     self._wdItem.setData(wdItem); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 const commons = tags.wikimedia_commons; | 
					
						
							|  |  |  |                 if (commons !== undefined) { | 
					
						
							|  |  |  |                     self._commons.setData(commons); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 if (tags.mapillary) { | 
					
						
							|  |  |  |                     let mapillary = tags.mapillary; | 
					
						
							|  |  |  |                     const prefix = "https://www.mapillary.com/map/im/"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-06 16:12:44 +02:00
										 |  |  |                     let regex = /https?:\/\/www.mapillary.com\/app\/.*pKey=([^&]*).*/ | 
					
						
							| 
									
										
										
										
											2021-01-02 16:04:16 +01:00
										 |  |  |                     let match = mapillary.match(regex); | 
					
						
							|  |  |  |                     if (match) { | 
					
						
							|  |  |  |                         mapillary = match[1]; | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     if (mapillary.indexOf(prefix) < 0) { | 
					
						
							|  |  |  |                         mapillary = prefix + mapillary; | 
					
						
							|  |  |  |                     } | 
					
						
							| 
									
										
										
										
											2021-03-26 03:24:58 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-02 16:04:16 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |                     AddImages([{url: mapillary, key: undefined}]); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             }) | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-26 03:33:42 +01:00
										 |  |  |     public static construct(tags: UIEventSource<any>, imagePrefix = "image", loadSpecial = true): ImageSearcher { | 
					
						
							| 
									
										
										
										
											2021-03-26 03:36:49 +01:00
										 |  |  |         const key = tags.data["id"] + " " + imagePrefix + loadSpecial; | 
					
						
							| 
									
										
										
										
											2021-03-26 03:33:42 +01:00
										 |  |  |         if (ImageSearcher._cache.has(key)) { | 
					
						
							|  |  |  |             return ImageSearcher._cache.get(key) | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const searcher = new ImageSearcher(tags, imagePrefix, loadSpecial); | 
					
						
							|  |  |  |         ImageSearcher._cache.set(key, searcher) | 
					
						
							|  |  |  |         return searcher; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private static loadWikidata(wikidataItem, addImage: ((url: string) => void)): void { | 
					
						
							| 
									
										
										
										
											2021-01-02 16:04:16 +01:00
										 |  |  |         // Load the wikidata item, then detect usage on 'commons'
 | 
					
						
							|  |  |  |         let allWikidataId = wikidataItem.split(";"); | 
					
						
							|  |  |  |         for (let wikidataId of allWikidataId) { | 
					
						
							|  |  |  |             // @ts-ignore
 | 
					
						
							|  |  |  |             if (wikidataId.startsWith("Q")) { | 
					
						
							|  |  |  |                 wikidataId = wikidataId.substr(1); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             Wikimedia.GetWikiData(parseInt(wikidataId), (wd: Wikidata) => { | 
					
						
							| 
									
										
										
										
											2021-03-26 03:33:42 +01:00
										 |  |  |                 addImage(wd.image); | 
					
						
							| 
									
										
										
										
											2021-01-02 16:04:16 +01:00
										 |  |  |                 Wikimedia.GetCategoryFiles(wd.commonsWiki, (images: ImagesInCategory) => { | 
					
						
							|  |  |  |                     for (const image of images.images) { | 
					
						
							|  |  |  |                         if (image.startsWith("File:")) { | 
					
						
							| 
									
										
										
										
											2021-03-26 03:33:42 +01:00
										 |  |  |                             addImage(image); | 
					
						
							| 
									
										
										
										
											2021-01-02 16:04:16 +01:00
										 |  |  |                         } | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                 }) | 
					
						
							|  |  |  |             }) | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-26 03:33:42 +01:00
										 |  |  |     private static LoadCommons(commonsData: string, addImage: ((url: string) => void)): void { | 
					
						
							| 
									
										
										
										
											2021-01-02 16:04:16 +01:00
										 |  |  |         const allCommons: string[] = commonsData.split(";"); | 
					
						
							|  |  |  |         for (const commons of allCommons) { | 
					
						
							|  |  |  |             if (commons.startsWith("Category:")) { | 
					
						
							|  |  |  |                 Wikimedia.GetCategoryFiles(commons, (images: ImagesInCategory) => { | 
					
						
							|  |  |  |                     for (const image of images.images) { | 
					
						
							|  |  |  |                         if (image.startsWith("File:")) { | 
					
						
							| 
									
										
										
										
											2021-03-26 03:33:42 +01:00
										 |  |  |                             addImage(image) | 
					
						
							| 
									
										
										
										
											2021-01-02 16:04:16 +01:00
										 |  |  |                         } | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                 }) | 
					
						
							| 
									
										
										
										
											2021-03-26 03:24:58 +01:00
										 |  |  |             } else { | 
					
						
							| 
									
										
										
										
											2021-01-02 16:04:16 +01:00
										 |  |  |                 if (commons.startsWith("File:")) { | 
					
						
							| 
									
										
										
										
											2021-03-26 03:33:42 +01:00
										 |  |  |                     addImage(commons) | 
					
						
							| 
									
										
										
										
											2021-01-02 16:04:16 +01:00
										 |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-26 03:33:42 +01:00
										 |  |  |     private static LoadImages(tags: any, imagePrefix: string): { key: string, url: string }[] { | 
					
						
							| 
									
										
										
										
											2021-01-02 16:04:16 +01:00
										 |  |  |         const imageTag = tags[imagePrefix]; | 
					
						
							|  |  |  |         const images: { key: string, url: string }[] = []; | 
					
						
							|  |  |  |         if (imageTag !== undefined) { | 
					
						
							|  |  |  |             const bareImages = imageTag.split(";"); | 
					
						
							|  |  |  |             for (const bareImage of bareImages) { | 
					
						
							|  |  |  |                 images.push({key: imagePrefix, url: bareImage}) | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         for (const key in tags) { | 
					
						
							|  |  |  |             if (key.startsWith(imagePrefix + ":")) { | 
					
						
							|  |  |  |                 const url = tags[key] | 
					
						
							|  |  |  |                 images.push({key: key, url: url}) | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return images; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |