Repository: internetarchive/openlibrary

Issue:
Work page not always selecting an edition
<!-- What problem are we solving? What does the experience look like today? What are the symptoms? -->

@jimchamp it's possible our current edition-selection mechanism isn't working for work pages. Here's an example where an edition parameter is specified but doesn't seem to result in an/the edition being fetched. This seems like a P1 priority for us to investigate. 

### Relevant File(s)

**Affected file:**
https://github.com/internetarchive/openlibrary/blob/master/openlibrary/templates/type/edition/view.html#L22-L66

**Most recent changes:**
https://github.com/internetarchive/openlibrary/commit/8dcaa5a46187ff499878926c4c43b64ec61da902#diff-93ec72c37de495b619f24641f2b9defc4ea1205bb5450bb6baa33498831721efR22-R66

### Evidence / Screenshot / URL

Notice in the following link that no editions summary / omnibar appears, nor an editions table. Also, the edit buttons don't reflect that an edition is selected:

https://openlibrary.org/works/OL8193508W/Alice%27s_Adventures_in_Wonderland?edition=alicesadventures00carr_18&debug=true

Also able to repro across works:
https://openlibrary.org/works/OL9170454W/Hamlet?edition=ia%3Ahamletprinceofde00shakuoft

![openlibrary org_works_OL8193508W_Alice%27s_Adventures_in_Wonderland_edition=alicesadventures00carr_18 debug=true](https://user-images.githubusercontent.com/978325/160652206-40b879a7-8fec-4ed0-a600-ad422cf586c3.png)

(note missing omnibar)

### Stakeholders
<!-- @ tag stakeholders of this bug -->



Discussion:

Resolving patch:
diff --git a/openlibrary/book_providers.py b/openlibrary/book_providers.py
--- a/openlibrary/book_providers.py
+++ b/openlibrary/book_providers.py
@@ -18,9 +18,19 @@ class AbstractBookProvider:
     """
     identifier_key: str
 
+    def get_olids(self, identifier):
+        return web.ctx.site.things({
+            "type": "/type/edition",
+            self.db_selector: identifier
+        })
+
     @property
     def editions_query(self):
-        return {f"identifiers.{self.identifier_key}~": "*"}
+        return {f"{self.db_selector}~": "*"}
+
+    @property
+    def db_selector(self):
+        return f"identifiers.{self.identifier_key}"
 
     @property
     def solr_key(self):
@@ -72,8 +82,8 @@ class InternetArchiveProvider(AbstractBookProvider):
     identifier_key = 'ocaid'
 
     @property
-    def editions_query(self):
-        return {f"{self.identifier_key}~": "*"}
+    def db_selector(self):
+        return self.identifier_key
 
     @property
     def solr_key(self):
diff --git a/openlibrary/plugins/upstream/models.py b/openlibrary/plugins/upstream/models.py
--- a/openlibrary/plugins/upstream/models.py
+++ b/openlibrary/plugins/upstream/models.py
@@ -719,7 +719,7 @@ def get_representative_edition(self):
             if 'openlibrary_edition' in availability[work_id]:
                 return '/books/%s' % availability[work_id]['openlibrary_edition']
 
-    def get_sorted_editions(self, ebooks_only=False, covers_only=False, limit=None, keys=None):
+    def get_sorted_editions(self, ebooks_only=False, limit=None, keys=None):
         """
         Get this work's editions sorted by publication year
         :param bool ebooks_only:
@@ -727,11 +727,10 @@ def get_sorted_editions(self, ebooks_only=False, covers_only=False, limit=None,
         :param list[str] keys: ensure keys included in fetched editions
         :rtype: list[Edition]
         """
-        edition_keys = keys or []
         db_query = {"type": "/type/edition", "works": self.key}
-        if limit:
-            db_query['limit'] = limit
+        db_query['limit'] = limit or 10000
 
+        edition_keys = []
         if ebooks_only:
             if self._solr_data:
                 from openlibrary.book_providers import get_book_providers
@@ -744,10 +743,6 @@ def get_sorted_editions(self, ebooks_only=False, covers_only=False, limit=None,
                     edition_keys += web.ctx.site.things(query)
             else:
                 db_query["ocaid~"] = "*"
-        elif limit and covers_only:
-            # if we're going to be picky and there's no ebooks
-            # try to favor editions with covers
-            db_query["covers_i~"] = "*"
 
         if not edition_keys:
             solr_is_up_to_date = (
@@ -763,6 +758,7 @@ def get_sorted_editions(self, ebooks_only=False, covers_only=False, limit=None,
                 # given librarians are probably doing this, show all editions
                 edition_keys += web.ctx.site.things(db_query)
 
+        edition_keys.extend(keys or [])
         editions = web.ctx.site.get_many(list(set(edition_keys)))
         editions.sort(
             key=lambda ed: ed.get_publish_year() or -sys.maxsize, reverse=True


Test patch: