Wikipedia:RefToolbar/2.0

RefToolbar 2.0 is the current version of RefToolbar, which adds citing capability to the enhanced editing toolbar (the MediaWiki extension WikiEditor). It may appear two different ways depending on a user's preferences. See the troubleshooting section below for details.

Reftoolbar 2.0 consists of a series of JavaScript scripts that use the jQuery and jQuery UI JavaScript libraries. It also makes use of the JSON standard.

No installation required
RefToolbar 2.0 is now turned on by default for all users on English Wikipedia. You don't have to do anything to install it.

Disabling
To disable the refTools script from loading, add the following line to your common JS page or skin JS page:

Templates
On the left side of the Cite panel is a dropdown list of all available templates. Clicking one will open a dialog box with the fields for that template. Buttons on the bottom of the dialog are used to insert the ref into the article, preview the ref, reset the form, and close the dialog without changing the article. Clicking will insert the current date into the adjacent text box. Clicking will attempt to autofill citation details given an ID entered in the adjacent text box. An ISBN, DOI, or PMID can be used for autofilling.

Named refs
Clicking the "Named refs" button will open a dialog listing all of the named references in the article. Choosing one will show the wikitext of the reference with an option to preview how the reference looks after parsing. Clicking the "Insert" button will insert the named reference into the last cursor position in the article.

Error checking
To use error checking, click the Error check symbol, then select the error checks to run. A panel listing any errors found will be inserted underneath the edit area. Additional error checks can be added, see below for details.

Configuration
Unlike the old version of the script, this version is highly customizable sitewide and on a per-user basis.

Note that while the script is designed to be easily configurable, the configuration options do need to be valid JavaScript code - brackets and parentheses must be matched and strings must be wrapped in quotation marks with quotation marks in the string escaped as necessary.

Configuration is done using a global  object. Configuration options are stored in  and

Individual users
No customization is required, the script will use the sitewide defaults "out of the box". All user configuration options override the sitewide options.

Sitewide configuration
All global configuration is done in the MediaWiki:RefToolbarConfig.js script file. The script will set a default for all options, but it is recommended that site defaults include a value for all options. For all option sample code shown below, change "UserOptions" to "Options" for site configuration. The initial  line and the final   are not necessary for changes in RefToolbarConfig.js and should be omitted.

Automatic date insertion
Changing the date format used for automatically added dates (such as accessdate) is done using a simple string replacement system. "Special" words will be replaced with their corresponding date, all other characters will be left as-is.

The current date options available are:
 * — The day of the month (1-31)
 * — The day of the month (01-31), zero-padded to 2 digits
 * — The month number (1–12)
 * — The month number (01–12), zero-padded to 2 digits
 * — The month name
 * — The 4-digit year

Other options may be available on request. Currently only one format will be used. If there is demand for it, the automatic date system may be extended to allow multiple options in a dropdown selection box. The date is always in UTC.

The fields that have an automatic date option are stored in the "autodate fields" option.

To customize automatic date insertion, options can be set like so:

Modal dialogs
To set whether the dialogs should be modal, use the "modal" option. If set to true, opening one of the dialogs will block access to the rest of the edit page until the dialog is closed.

Automatic parsing
The script can be set to automatically provide a parsed preview when previewing a citation template through the "autoparse" option. Its not recommended to set this to true as a global setting as it may slow the script down for people with slow connections.

Template expansion
To be able to retrieve more references on a page, the script can also expand templates and parser functions on the page (except templates inside of ref tags) so that references from templates or references that use the  syntax will be available when inserting named refs or for error checking functions. This can also slow the script down for people with slow connections.

Month names
The month names used for automatic date insertion are also customizable, for translation to other languages, or to use abbreviations.

Citation templates
Citation templates can be added using an object syntax. To add a new template, simply create a new citeTemplate object. Each template has 4 required parameters. For sitewide settings, templates should be included in the RefToolbarConfig page. Users can also add their own templates with this system by adding them to their user script page. If the script is installed on the user script page (as opposed to using a gadget), the templates must be below where the script is imported.
 * 1) The template name (e.g. "cite web")
 * 2) A short name for the template, used for the template list in the script (this name should be a unique name for each template object, even if 2 template objects are based on the same template)
 * 3) A list of "basic" template options, using the syntax described below
 * 4) A list of the remaining options, any option the template supports that isn't in the "basic" list.

Object syntax for fields
Each field is described as an object in a list, using JavaScript Object Notation. The basic options are:
 * field – The actual field name as used in the template. This is the only required attribute.
 * label – If a "cite-fieldname-label" message is not specified in the MediaWiki:RefToolbarMessages-lang.js page (user interface language), the text that will be given as the label on the form. If neither are provided, the 'field' attribute will be used with the first letter capitalized
 * tooltip – If given, a ? will be put after the label that the user can mouse over to see a more detailed description of the field.

The object system allows other options for additional features, which are described below.

Automatic-incrementing fields
For fields such as authors and editors where it is frequently necessary to specify more than one, automatic-incrementing fields can be used. For each field, an "increment_group" option is set. This allows fields such as last/first to be incremented together, as long as they're in the same group. One field in each group should also have an "increment_button" option set - this field will have a button next to them to increment them. The name of the field should contain "", which will be replaced with the incrementing numbers when the template is generated. For the label on the dialog, the "" will be removed. For example, to create separate last/first, but a single editor field, the following would be specified: For authors, auto-incrementing fields can also be used with the autofilling options, see the section below for details.

Autofilling
The script can autofill citation details given an identifier for the work. Currently DOI, PMID, ISBN, and URL are supported. To add autofill support to a template, set the "autofillid" param on a field that will be used for the ID code, for example  The ID type should be given in lowercase. Then add "autofillprop" for the properties to be automatically filled, such as. The properties available vary depending on the ID. The table below shows the available properties for each ID. See the sample code below for a simplified example and MediaWiki:RefToolbarConfig.js for a full example as it is currently used here.

For multiple authors, the autofill system accepts several possible options. In order that the script checks for: If using the  /   or   option,   must also be set, otherwise some authors may be omitted if there are more authors than fields specified. For the automatic-incrementing options to work properly, they should be set up as auto-incrementing fields.
 * 1)   /   - Authors split by first and last name, using automatic-incrementing fields. Enough last/first fields for all the authors will be automatically added.
 * 2)   - One auto-incrementing field for each author. Enough fields will be added for all authors to be automatically added.
 * 3)   /   (with N replaced in the configuration by numbers starting at 1) - Authors split by last name and first name, with 2 fields for each author. If there are more authors than fields, the rest will be put into.
 * 4)   (with N replaced in the configuration by numbers starting at 1) - One field for each author. If there are more authors than fields, the rest will be put into.
 * 5)   - All authors will be combined into 1 field.

There are also several options for dates and the behavior of the script will depend on how complete a date is returned by the database. Not all DOI/PMIDs will return a full date. For DOI/PMID For DOI/PMID,  is required,   and   are optional. For ISBN, only  is used.
 * 1) If a full d/m/y date is returned by the database OR no   field is available, it will put it into the   field, using the   option, with any missing parts omitted.
 * 2) If a full date is not returned AND both   and   are available, the year and month (if returned) will be put into the   and   fields.
 * 3) As a fallback, the   field is filled with whatever is returned by the lookup script.

Error checking functions
Error checking functions can be added in a way similar to citation templates. The citeErrorCheck object requires one parameter, an object. This object should contain certain the information as described below. Currently all attributes are required. In the future, optional attributes may be added to support other features.
 * testname – A short name for the test, should be unique and should not contain spaces
 * desc – A description for the test
 * func – The function that will be called to execute the test (see below for details)
 * type – The type of test, see below for the options

There are currently 3 types of error checks. The differences between them are described below:
 * refcheck
 * The function given is passed each ref individually, it should return a single error object or not return.


 * reflist
 * The function is passed the entire list of references, it should return an array of error objects.


 * search
 * The function is passed the wikitext of the article, it should return an array of error objects.

An error object is a JavaScript object that should contain 2 attributes. The 'msg' attribute should be an error message describing the problem and the 'err' attribute should be a portion of the wikitext showing the error, such as the content of the reference with the problem.

Some example code for a simple function that checks whether each reference uses a template could be:

Troubleshooting
The refToolbar comes in three versions, depending on the settings at :


 * Symptoms and recommended actions

Porting to another wiki
This script is designed to be easily portable to other wikis that use a similar (template-based) reference system.

You should not need to copy the main script itself. You can, but it means that you will have to check for changes and manually update to receive bug fixes and new features. You do need to copy the following files to your wiki (this requires sysop access on the wiki the script is being ported to). Then you can customize the options, templates, error functions, and translate the messages as necessary.
 * MediaWiki:RefToolbarConfig.js (you can also use the smaller sample configuration)
 * MediaWiki:RefToolbarMessages-en.js and MediaWiki:RefToolbarMessages-de.js

Note: If you translate the "field" names in the "MediaWiki:RefToolbarConfig.js" page, you also need to change the corresponding message keys at "MediaWiki:RefToolbarMessages- .js", to avoid "null" descriptions in the popups.

Note that the script has not yet been tested using RTL (right to left) languages.

Install as an opt-in gadget
Ensure that the Gadgets extension has been installed in the LocalSettings.php file. Then follow these steps to install RefToolbar as a gadget:
 * 1) Copy the loader script to your MediaWiki:Gadget-refToolbar.js
 * 2) Create a corresponding MediaWiki:Gadget-refToolbar page
 * 3) Add the following line to your MediaWiki:Gadgets-definition

Install as an opt-out script
To enable this tool for everyone, follow the same steps as above but add the "default" option to the gadget's definition: * refToolbar[ResourceLoader | default |dependencies=user.options,mediawiki.util]|refToolbar.js

Information for developers
This section covers information relevant to developers that might not be covered above.

Descriptions of the  and   objects are described in detail in above sections.

The  object:
 * The  object is used to reduce the number of variables and functions put into the global scope. It contains all the functions and variables used by the script, except for the  /  object definitions. Most of this object is not loaded except on edit pages.

Reference objects:
 * Reference objects are created for each reference currently on the page, and whenever a reference is added by the script. Reference objects have several attributes:
 * – The 'name' attribute of the ref tag
 * – The 'group' attribute of the ref tag
 * – The content inside of the ref tags
 * –  or , whether the ref is a short tag with no content or a full reference tag

The script uses the jQuery library, which is well documented at http://docs.jquery.com. It also uses some modules from WikiEditor extension, which is documented at mw:Extension:WikiEditor/Toolbar customization.

The live code for RefToolbar can be found at the following pages:
 * MediaWiki:Gadget-refToolbar.js - Initialization
 * MediaWiki:Gadget-refToolbarBase.js - Shared code
 * MediaWiki:RefToolbar.js - Code for RefToolbar 2.0b (full-featured version)
 * MediaWiki:RefToolbarNoDialogs.js - Code for RefToolbar 2.0a (without dialog support)
 * MediaWiki:RefToolbarLegacy.js - Code for RefToolbar 1.0 (legacy support)
 * MediaWiki:RefToolbarConfig.js - Local configuration