User:Nardog/RefRenamer

From Wikipedia, the free encyclopedia
RefRenamer
DescriptionReplaces reference names like ":0" with descriptive ones like "Smith-2015"
UpdatedAugust 13, 2023
    (46 days ago)
Browser supportChrome, Firefox, etc. (ES2019+ needed)
Source

This is a script for replacing the name attributes of <ref> tags with something descriptive like "Smith-2015". It was developed with focus on names like ":0", ":1" and so on, which are generated by VisualEditor, so by default it only renames them, but you can rename other names used in the source as well.

Installing it adds the link "RefRenamer" to the Tools section of the sidebar on pages related to existing wikipages, which opens a dialog. If it's opened while editing, the source in the edit box will be parsed (slower); otherwise the latest version of the page will be analyzed.

Installation[edit]

Add the following to your common.js:

mw.loader.load('//en.wikipedia.org/w/index.php?title=User:Nardog/RefRenamer.js&action=raw&ctype=text/javascript');

Or you may enable the script installer in Preferences → Gadgets and click "Install" in the infobox on this page.

Usage[edit]

When the dialog is opened, only references with VE-style names (":0" etc.) are eligible for being renamed. Other named references are listed in the table at the bottom, and you can make them eligible by clicking "+" or "Add all".

The script relies on two main sources for generating new names. One is COinS metadata, which is output by CS1/CS2 templates ({{cite web}}, {{citation}}, etc.). The other is "First phrase", which takes whatever the first characters before any punctuation in the reference text are. The extraction of years works in a similar way. If a date is available in COinS, the greatest integer in it is treated as the year. In case it's not, you may opt in to use any first-occurring four-digit number in the reference text. To avoid collisions, autogenerated names are compared against each other and against the existing ref names within the source. If a name is already taken, a number ("increment") is appended. You can then manually edit the generated ref names in the table. Clicking Continue opens the editing page with the modified source and diff preloaded.

Options[edit]

Main fallback stack
Here you can choose which properties to look for and reorder the priority by dragging the items. If no properties specified here are available in a reference, the textbox in the table will be left blank (which means the name will not be replaced unless input manually).
Properties correspond to COinS as follows (see Module:Citation/CS1/COinS for the logic):
  • Last name: rft.aulast
  • First name: rft.aufirst
  • Author: rft.au
  • Periodical/website: rft.jtitle
  • Publisher: rft.pub, rft.inst
  • Article: rft.atitle, rft.title
  • Book: rft.btitle
  • Domain: Extracted from rft_id
If no COinS metadata is available, "Domain" is taken from the first link in the reference. "First phrase" takes whatever the first characters before any punctuation in the reference text are.
Lowercase
Remove diacritics
Remove punctuation
Replace space with
These affect the main string found through the fallback stack. You can remove spaces by leaving the "Replace space with" textbox blank.
Year
If this is checked, the greatest integer in COinS rft.date will be appended to the string.
Fall back on any 4-digit number
If this is checked and a COinS date is not availabe, any first-occurring four-digit number in the reference text will be appended to the string.
Convert to ASCII
If the year uses language-specific numerals (e.g. "۲۰۱۵"), convert it to ASCII Western Arabic numerals ("2015"). Shown only if a non-ASCII year has been found.
Use Latin letters for increments
If a year is found and the name is already used, append a lowercase Latin letter (e.g. "Jones-2023a") instead of a number.
Numeric increments start at
Minimum number to be appended in case a name is already used.
Always insert increments
If this is checked, the increment number is appended whether a collision has occurred or not.
Delimiter
String to be inserted before a year or increment. May be empty.
Insert delimiters only after numerals
If this is checked, the delimiters will be inserted only if the preceding character is a numeral (e.g. "Lee2015" instead of "Lee-2015").
Remove unreused names
If the reference is used only once (i.e. no <ref name="..." />), remove the  name="..." part entirely rather than replace the name.
Apply
Regenerates the new names using the above options. Any changes made in the table will be discarded.
Reset
Restores the script's default options. Does not regenerate new names yet.

Tables[edit]

Name
Original name of the reference. The references are sorted by this.
Reference
Output text of the reference.
New name
Here you can see and change the new name generated based on the options above. Leave it empty to keep the original name. You can use the tab key to jump to the next item. Hitting Enter here has the same effect as clicking the Continue button.
Checkbox
If a checkbox appears inside the textbox, it means the reference is used only once (i.e. no <ref name="..." />). Uncheck it to remove the  name="..." part entirely rather than replace the name.
Remove from references to rename
Moves the reference from "References to rename" to "Other named references".
Add to references to rename
Moves the reference from "Other named references" to "References to rename" and generates a new name based on the current options (even if they have been changed since "Apply" was last clicked).

Other[edit]

Add all
Adds all references in "Other named references" to "References to rename" and generates new names based on the current options (even if they have been changed since "Apply" was last clicked).
Reset selection
Adds all references that are of the ":0" type to "References to rename", and all others to "Other named references".
Continue
Opens the editing page with the modified source and diff preloaded. Saves the options to your browser's local storage so they will be loaded the next time you open the dialog. Gives you an error instead if there are duplicate names or if no names have been modified.

Localization[edit]

You may create a translated version of this script with the following code. Note though that the script is built on the assumption that COinS metadata is usually available, which may not be the case on your wiki, and that your wiki may have other needs or conventions in ref names the script does not support.

window.refrenamerMessages = {
	portlet: 'RefRenamer',
	loading: 'Loading RefRenamer...',
	loadingSource: 'Loading the source...',
	loadingHtml: 'Loading HTML...',
	parsing: 'Parsing wikitext...',
	opening: 'Opening the diff...',
	continue: 'Continue',
	main: 'Main fallback stack:',
	lastName: 'Last name',
	firstName: 'First name',
	author: 'Author',
	periodical: 'Periodical/website',
	publisher: 'Publisher',
	article: 'Article',
	book: 'Book',
	domain: 'Domain',
	firstPhrase: 'First phrase',
	lowercase: 'Lowercase',
	removeDia: 'Remove diacritics',
	removePunct: 'Remove punctuation',
	replaceSpace: 'Replace space with:',
	year: 'Year',
	yearFallback: 'Fall back on any 4-digit number',
	yearConvert: 'Convert to ASCII',
	latinIncrement: 'Use Latin letters for increments',
	latinStart: 'Start with:',
	increment: 'Numeric increments start at:',
	forceIncrement: 'Always insert increments',
	delimiter: 'Delimiter:',
	delimitConditional: 'Insert delimiters only after numerals',
	removeUnreused: 'Remove unreused names',
	apply: 'Apply',
	reset: 'Reset',
	tableName: 'Name',
	tableCaption: 'References to rename',
	tableRef: 'Reference',
	tableNewName: 'New name',
	tableAddRemove: '+/−',
	keepTooltip: 'Uncheck to remove',
	tableRemove: '(Remove)',
	removeTooltip: 'Remove from references to rename',
	otherTableCaption: 'Other named references',
	notReused: '(not reused)',
	addTooltip: 'Add to references to rename',
	addAll: 'Add all',
	resetSelection: 'Reset selection',
	noNamesAlert: 'The source does not contain ref names to rename.',
	noChangesError: 'No names have been modified.',
	duplicatesError: 'The following names are already used or input more than once:',
	summary: 'Replaced [[$1|VE ref names]] using [[$2|RefRenamer]]',
	genericSummary: 'Renamed references using [[$1|RefRenamer]]'
};

mw.loader.load('//en.wikipedia.org/w/index.php?title=User:Nardog/RefRenamer.js&action=raw&ctype=text/javascript');

Limitations[edit]

  • Ref names inside templates like {{refn}} are not detected.

Caveats[edit]

While the script has checks in place for duplicate names, there might be unforeseen cases where it removes or replaces something it shouldn't. Please use it at your own risk (as you must any script) and make sure the result is correct before/after saving.

Excessive use of this script may be considered WP:COSMETICBOT.