forked from MapComplete/MapComplete
		
	Also pickup wikimedia categories in the image tags, fix #433
This commit is contained in:
		
							parent
							
								
									8cbb693c98
								
							
						
					
					
						commit
						32cbd6e2c1
					
				
					 6 changed files with 55 additions and 31 deletions
				
			
		|  | @ -5,7 +5,6 @@ import GenericImageProvider from "./GenericImageProvider"; | |||
| import {UIEventSource} from "../UIEventSource"; | ||||
| import ImageProvider, {ProvidedImage} from "./ImageProvider"; | ||||
| import {WikidataImageProvider} from "./WikidataImageProvider"; | ||||
| import {Utils} from "../../Utils"; | ||||
| 
 | ||||
| /** | ||||
|  * A generic 'from the interwebz' image picker, without attribution | ||||
|  | @ -17,12 +16,12 @@ export default class AllImageProviders { | |||
|         Mapillary.singleton, | ||||
|         WikidataImageProvider.singleton, | ||||
|         WikimediaImageProvider.singleton, | ||||
|         new GenericImageProvider(Imgur.defaultValuePrefix)] | ||||
|         new GenericImageProvider([].concat(...Imgur.defaultValuePrefix, WikimediaImageProvider.commonsPrefix))] | ||||
| 
 | ||||
| 
 | ||||
|     private static _cache: Map<string, UIEventSource<ProvidedImage[]>> = new Map<string, UIEventSource<ProvidedImage[]>>() | ||||
| 
 | ||||
|     public static LoadImagesFor(tags: UIEventSource<any>, imagePrefix: string, loadSpecialSource: boolean): UIEventSource<ProvidedImage[]> { | ||||
|     public static LoadImagesFor(tags: UIEventSource<any>, imagePrefix?: string): UIEventSource<ProvidedImage[]> { | ||||
|         const id = tags.data.id | ||||
|         if (id === undefined) { | ||||
|             return undefined; | ||||
|  | @ -33,11 +32,14 @@ export default class AllImageProviders { | |||
|             return cached | ||||
|         } | ||||
| 
 | ||||
| 
 | ||||
|         const source = new UIEventSource([]) | ||||
|         this._cache.set(id, source) | ||||
|         const allSources = [] | ||||
|         for (const imageProvider of AllImageProviders.ImageAttributionSource) { | ||||
|             const singleSource = imageProvider.GetRelevantUrls(tags) | ||||
|             const singleSource = imageProvider.GetRelevantUrls(tags, { | ||||
|                 prefixes: imagePrefix !== undefined ? [imagePrefix] : undefined | ||||
|             }) | ||||
|             allSources.push(singleSource) | ||||
|             singleSource.addCallbackAndRunD(_ => { | ||||
|                 const all : ProvidedImage[] = [].concat(...allSources.map(source => source.data)) | ||||
|  |  | |||
|  | @ -11,12 +11,15 @@ import {LicenseInfo} from "./LicenseInfo"; | |||
| export class WikimediaImageProvider extends ImageProvider { | ||||
| 
 | ||||
| 
 | ||||
|     public readonly defaultKeyPrefixes = ["wikimedia_commons"] | ||||
|     private readonly commons_key = "wikimedia_commons" | ||||
|     public readonly defaultKeyPrefixes = [this.commons_key,"image"] | ||||
|     public static readonly singleton = new WikimediaImageProvider(); | ||||
|     public static readonly commonsPrefix = "https://commons.wikimedia.org/wiki/" | ||||
| 
 | ||||
|     private constructor() { | ||||
|         super(); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Recursively walks a wikimedia commons category in order to search for (image) files | ||||
|      * Returns (a promise of) a list of URLS | ||||
|  | @ -124,38 +127,42 @@ export class WikimediaImageProvider extends ImageProvider { | |||
| 
 | ||||
|     } | ||||
| 
 | ||||
|     private async UrlForImage(image: string): Promise<ProvidedImage>{ | ||||
|         if(!image.startsWith("File:")){ | ||||
|             image = "File:"+image | ||||
|     private async UrlForImage(image: string): Promise<ProvidedImage> { | ||||
|         if (!image.startsWith("File:")) { | ||||
|             image = "File:" + image | ||||
|         } | ||||
|         return {url: this.PrepareUrl(image), key: undefined, provider: this} | ||||
|     } | ||||
|      | ||||
| 
 | ||||
|     public async ExtractUrls(key: string, value: string): Promise<Promise<ProvidedImage>[]> { | ||||
|         const commonsPrefix = "https://commons.wikimedia.org/wiki/" | ||||
|         if(value.startsWith(commonsPrefix)){ | ||||
|             value = value.substring(commonsPrefix.length) | ||||
|         } else       if(value.startsWith("https://upload.wikimedia.org")){ | ||||
|             const result : ProvidedImage = { | ||||
|          | ||||
|         if(key !== this.commons_key && !value.startsWith(WikimediaImageProvider.commonsPrefix)){ | ||||
|             return [] | ||||
|         } | ||||
|          | ||||
|         if (value.startsWith(WikimediaImageProvider.commonsPrefix)) { | ||||
|             value = value.substring(WikimediaImageProvider.commonsPrefix.length) | ||||
|         } else if (value.startsWith("https://upload.wikimedia.org")) { | ||||
|             const result: ProvidedImage = { | ||||
|                 key: undefined, | ||||
|                 url: value, | ||||
|                 provider: this | ||||
|             } | ||||
|             return [Promise.resolve(result)] | ||||
|         } | ||||
|         if(value.startsWith("Category:")){ | ||||
|         if (value.startsWith("Category:")) { | ||||
|             const urls = await WikimediaImageProvider.GetImagesInCategory(value) | ||||
|             return urls.map(image => this.UrlForImage(image)) | ||||
|         } | ||||
|         if(value.startsWith("File:")){ | ||||
|         if (value.startsWith("File:")) { | ||||
|             return [this.UrlForImage(value)] | ||||
|         } | ||||
|         if(value.startsWith("http")){ | ||||
|         if (value.startsWith("http")) { | ||||
|             // PRobably an error
 | ||||
|             return [] | ||||
|         } | ||||
|         // We do a last effort and assume this is a file
 | ||||
|         return [this.UrlForImage("File:"+value)] | ||||
|         return [this.UrlForImage("File:" + value)] | ||||
|     } | ||||
| 
 | ||||
| 
 | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue