CXuesong / WikiClientLibrary

/*🌻*/ Wiki Client Library is an asynchronous MediaWiki API client library targeting modern .NET platforms
https://github.com/CXuesong/WikiClientLibrary/wiki
Apache License 2.0
80 stars 16 forks source link

Issue with InterwikiMap #93

Closed rwv37 closed 2 years ago

rwv37 commented 2 years ago

I used WCL to pull the InterwikiMap from Wikipedia. I had the following code, to put a particular item from it in a collection of my own:

        if (this.ReferenceSite.InterwikiMap.Contains(namespaceOrWhatever))
        {
            this.AddWantedInterwiki(this.ReferenceSite.InterwikiMap[namespaceOrWhatever]);
        }

That throws the following exception:

Unhandled exception. System.Collections.Generic.KeyNotFoundException: The given key 'Toollabs' was not present in the dictionary.
   at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
   at WikiClientLibrary.Sites.InterwikiMap.get_Item(String prefix)
   at Rwv37.MediaWiki.SiteSetup.Coordinator.AddWantedColonfulWikiPage(WikiPage wikiPage) in C:\Users\bob\Bob\trunk\Dev\DotNet\Rwv37\MediaWiki\Rwv37.MediaWiki.SiteSetup\Coordinator.cs:line 250
   at Rwv37.MediaWiki.SiteSetup.Coordinator.AddWantedWikiPage(WikiPage wikiPage) in C:\Users\bob\Bob\trunk\Dev\DotNet\Rwv37\MediaWiki\Rwv37.MediaWiki.SiteSetup\Coordinator.cs:line 215
   at Rwv37.MediaWiki.SiteSetup.WantedItemsListRetriever.RetrievePagesAsync() in C:\Users\bob\Bob\trunk\Dev\DotNet\Rwv37\MediaWiki\Rwv37.MediaWiki.SiteSetup\WantedItemsListRetriever.cs:line 49
   at Rwv37.MediaWiki.SiteSetup.WantedItemsListRetriever.RetrievePagesAsync() in C:\Users\bob\Bob\trunk\Dev\DotNet\Rwv37\MediaWiki\Rwv37.MediaWiki.SiteSetup\WantedItemsListRetriever.cs:line 49
   at Rwv37.MediaWiki.SiteSetup.WantedItemsListRetriever.RetrieveAsync() in C:\Users\bob\Bob\trunk\Dev\DotNet\Rwv37\MediaWiki\Rwv37.MediaWiki.SiteSetup\WantedItemsListRetriever.cs:line 34
   at Rwv37.MediaWiki.SiteSetup.Coordinator.GoAsync() in C:\Users\bob\Bob\trunk\Dev\DotNet\Rwv37\MediaWiki\Rwv37.MediaWiki.SiteSetup\Coordinator.cs:line 54
   at Rwv37.MediaWiki.SiteSetup.Program.<Main>(String[] args)

Note that the exact same key is used in the calls to Contains and [], so it seems strange that Contains would say the key is in there while at the same time [] would say it is not.

Also note the key is "Toollabs" with a capital T, but Wikipedia's interwiki entries are (I think) all lowercase. So, on a hunch, I tried adding a ToLower call on one (and only one) of them:

        if (this.ReferenceSite.InterwikiMap.Contains(namespaceOrWhatever))
        {
            this.AddWantedInterwiki(this.ReferenceSite.InterwikiMap[namespaceOrWhatever.ToLower()]);
        }

And it started working fine. So I think there might be a mismatch between the case-sensitivity-or-not of Contains and [] (which, if correct, would contradict the documentation).

CXuesong commented 2 years ago

Bug fixed and released v0.8.0-int.5. Please try again with the latest package.