Repository: internetarchive/openlibrary

Issue:
Update Merge Author UI to reflect current merge guidelines
<!-- What problem are we solving? What does the experience look like today? What are the symptoms? -->
At the moment, the Merge Authors page contradicts the merge documentation. As the information on the Merge Author page is repeatedly shown, librarians in training are much more likely to learn this information than the one in the documentation.

Furthermore, the current interface makes it extremely difficult to find the correct primary profile. The merger automatically selects the entry with most authors instead of the oldest entry.

### Evidence / Screenshot (if possible)

![Screenshot 2022-12-09 at 21-55-02 Merge Authors Open Library](https://user-images.githubusercontent.com/36878944/206794666-4b81dc49-baf1-4fe8-a698-bb96d15a68b2.png)

### Relevant url?
<!-- `https://openlibrary.org/...` -->
https://openlibrary.org/authors/merge?key=OL844254A&key=OL9695037A

### Steps to Reproduce
<!-- What steps caused you to find the bug? -->
1. Select duplicate author's with the magic wand
2. Activate the merge process

<!-- What actually happened after these steps? What did you expect to happen? -->
* Actual: I need to open each profile to check which is the oldest.
* Expected: The Merge interface automatically selects the oldest entry and gives me all information necessary to check this choice.

### Details

- **Logged in (Y/N)?**
- Y
- **Browser type/version?**
- Firefox 107.0.1
- **Operating system?**
- Suse Tumbleweed
- **Environment (prod/dev/local)?** prod
- prod
<!-- If not sure, put prod -->

### Proposal & Constraints
<!-- What is the proposed solution / implementation? Is there a precedent of this approach succeeding elsewhere? -->

1) The sentence under 1. should be changed to: Choose the oldest profile as primary
2) The merge UI should automatically select the oldest author profile for me. As far as I can see, the author IDs are sequential, and thus it should be rather easy to select the lowest number inside a string that starts with OL and ends with A.
    * Note: Essentially we want to sort the authors by their author ID. More specifically by the _number_ between the OL and A.
3) Add the Open Library ID (eg `OL123A` just above the author name)

### Related files
<!-- Files related to this issue; this is super useful for new contributors who might want to help! If you're not sure, leave this blank; a maintainer will add them. -->
#3469
#668

### Stakeholders
<!-- @ tag stakeholders of this bug -->
Librarians in training, Super librarians


Discussion:
Hey, Can I be assigned to this task?
The recent change to the default sort order for author searches affected the behavior of the author merge display as well. Now, instead of the author with the most works, the author who's the "best match" to the search string will be at the top.

The confirmation dialog for the merge is missing a display of authors, so that might be a better place to put the override (if allowed) of the primary author (merge target).

Proposed flow:
1. Search for authors and select "Merge duplicates"
2. Display search list in an author merge specific ordering (best would be clustered by similarity, but that's hard in the general case, so perhaps just alphabetical or birth date order or some other scheme)
3. User selects all duplicate authors without choosing a "primary"
4. Confirmation dialog with authors sorted in creation order and first (oldest) author record selected (radio button), asking "Please confirm the authors to be merged and the primary author", which each author including birth & death dates (if available), work count, and OLID.

Currently the records being merged are listed in the progress dialog, but at that point it's too late if they're wrong, so this would fix two birds with one stone.
Go for it @aryan-iitj ! The main part of this issue is that authors on the merge page should be sorted by open library id number. @tfmorris Note librarians can select authors from the author search results, and then hit "Merge" to bring up the merge ui. That's points 1-3 on your flow @tfmorris . Then we show the default author merge UI, with things sorted.
@cdrini Having merged thousands of author records, I'm actually pretty familiar with the current flow. If you try it yourself and compare it to my description, you'll see the differences.

For 2, I suggest that strict ID ordering might not be the best based on my extensive experience with the UI.
For 3, I suggest removing the concept of "primary" from the first round of selection
for 4, I suggest that the confirmation dialog should a) include enough information to actually make an informed decision and b) incorporate the selection of the primary **after** the set of duplicates has been selected
I'm not fully understanding the change in proposed merge. Wasn't implying you don't have experience with, just noting that the ability to select authors from the author search results page before going to the Merge Authors UI was a new feature added last year.

This is the current UI flow:

1. Search for authors
2. Select authors from the search results (wo choosing primary) and press "Merge authors" in the librarian blue bar
3. You are taken to the Merge Authors UI, where you can confirm which authors to include/exclude, and what is primary.
4. Press save, confirm.

![image](https://user-images.githubusercontent.com/6251786/213207553-c7acc6c6-f43f-412f-82d0-a4f879826b54.png)

If I understand correctly, your flow is proposing:

1. Search for authors
2. Press merge dupes. Note _all_ authors in the search results are included.
3. Have a screen that clusters authors based on similarity, and which lets you select authors for merge (without noting primary)
4. Have a confirmation screen that shows what will change, and lets you select primary.
5. Press Save

The flows aren't that dissimilar, it just shifts around _where_ the selection happens. Is there a reason you would prefer a separate screen to select authors as opposed to the Search Authors UI? Is there information there that is missing perhaps? Note that the confirmation screen in your flow is basically the entire Merge Authors UI in the current flow. And not that there _is_ no notion of primary in the first round of selection.

Note in the past, there was just a single "Merge Duplicates" button on the search authors page, which selected _all_ the authors in the result set to be included in the Merge Authors UI. We should remove this button, and everyone should use the flow I've outlined above.
So, I take it back. Despite having merged over 56,000 author records, I apparently **don't** know how author merging works. Where is this new mechanism documented? Certainly not in the [librarian documentation](https://docs.google.com/document/d/1ZY7hlzfOL4It7XNcEH31IF8jhGzxSTiG_BjHWit4UJk/edit) pointed to be the [FAQ page](https://openlibrary.org/help/faq/editing#duplicate-au). How was it announced? I don't remember getting any emails from @mekarpeles or anyone about it.

My comments were relative to the current flow as documented at the link above.

The alternate flow you describe makes enhancing the confirmation dialog less critical, at least for small merges of a few authors, because the important confirmation information is displayed, greyed out, behind the confirmation dialog. It seems  strange that one has to select the authors twice though since at that stage in the process all one really needs to do is optionally remove any accidentally selected records and select the primary record.

I'm guessing that this odd, undocumented, new selection mechanism is also why text cut & paste is broken all over the site. I was wondering what caused that.
@seabelis would you be able to create an issue for updating the librarian documentation Tom mentioned? If you notice any specific bugs around text selection @tfmorris please do create issues!
I updated the proposal to include the open library ID instead of the author creation date ; this should keep the UI a little more condensed.


Resolving patch:
diff --git a/openlibrary/plugins/upstream/merge_authors.py b/openlibrary/plugins/upstream/merge_authors.py
--- a/openlibrary/plugins/upstream/merge_authors.py
+++ b/openlibrary/plugins/upstream/merge_authors.py
@@ -263,6 +263,9 @@ def GET(self):
         # filter bad keys
         keys = self.filter_authors(keys)
 
+        # sort keys by lowest OL number
+        keys = sorted(keys, key=lambda key: int(key[2:-1]))
+
         user = get_current_user()
         can_merge = user and (
             user.is_admin() or user.is_usergroup_member('/usergroup/super-librarians')


Test patch: