@@ -422,6 +422,18 @@ def isbns_from_record(rec: dict) -> list[str]:
422422 return isbns
423423
424424
425+ def find_wikisource_src (rec : dict ) -> str | None :
426+ if not rec .get ('source_records' ):
427+ return None
428+ ws_prefix = 'wikisource:'
429+ ws_match = next (
430+ (src for src in rec ['source_records' ] if src .startswith (ws_prefix )), None
431+ )
432+ if ws_match :
433+ return ws_match [len (ws_prefix ) :]
434+ return None
435+
436+
425437def build_pool (rec : dict ) -> dict [str , list [str ]]:
426438 """
427439 Searches for existing edition matches on title and bibliographic keys.
@@ -433,6 +445,13 @@ def build_pool(rec: dict) -> dict[str, list[str]]:
433445 pool = defaultdict (set )
434446 match_fields = ('title' , 'oclc_numbers' , 'lccn' , 'ocaid' )
435447
448+ if ws_match := find_wikisource_src (rec ):
449+ # If this is a wikisource import, ONLY consider a match if the same wikisource ID
450+ ekeys = set (editions_matched (rec , 'identifiers.wikisource' , ws_match ))
451+ if ekeys :
452+ pool ['wikisource' ] = ekeys
453+ return {k : list (v ) for k , v in pool .items () if v }
454+
436455 # Find records with matching fields
437456 for field in match_fields :
438457 pool [field ] = set (editions_matched (rec , field ))
@@ -458,6 +477,14 @@ def find_quick_match(rec: dict) -> str | None:
458477 if 'openlibrary' in rec :
459478 return '/books/' + rec ['openlibrary' ]
460479
480+ if ws_match := find_wikisource_src (rec ):
481+ # If this is a wikisource import, ONLY consider a match if the same wikisource ID
482+ ekeys = editions_matched (rec , 'identifiers.wikisource' , ws_match )
483+ if ekeys :
484+ return ekeys [0 ]
485+ else :
486+ return None
487+
461488 ekeys = editions_matched (rec , 'ocaid' )
462489 if ekeys :
463490 return ekeys [0 ]
@@ -498,7 +525,9 @@ def editions_matched(rec: dict, key: str, value=None) -> list[str]:
498525
499526 if value is None :
500527 value = rec [key ]
528+
501529 q = {'type' : '/type/edition' , key : value }
530+
502531 ekeys = list (web .ctx .site .things (q ))
503532 return ekeys
504533
0 commit comments