Module:Cite source/doc

Module:Cite source is the module that forms the guts of. You should aim to be familiar with Lua before editing it.

Broadly, the module is broken up into 2 major sections. The first is for formatting and storing information gathered from. The second is for acessing this information and producing citations. The main method used by this module for storing data is LuaCache with Semantic MediaWiki as a backup. Variables is used for caching queries on individual pages.

The module opens by importing other util libraries. Currently, these are named in snake_case for some reason but this may be changed to camelCase in future. PREFIX is a constant used by Lua Cache and Variables to name the data stores used by the module. In the case of something going seriously wrong, incriment the number in the prefix. This will reset all data stored by the module. The p table stores the functions returned by the module. These are accesible by the wider wiki. The h table stores the functions only used by other functions in this module.

Following this, there is a short function taken from Stack Overflow that inverts tables, swapping keys and values.

Generating and saving citations
The first major part of this module deals with gathering information from, formatting it into citations and saving it. The main functions here are p.infoboxStore and p.variantStore, the others being called from these 2. The general idea is that each of these 2 functions put together a table containing raw data from the infobox that is then processed by various helper functions to produce the output. These will now be discussed in more detail in the same order as in the module.

h.getAndFormatUnlinkedItems
This function deals with anything that is enterred into the infobox unlinked and comma-separated in the case of multiple entries. Namely, these are the source's anthology, writer, publisher, network and the source that it was adapted from. None of these will be aplicable to every source but they are all handled at the same time by this function.

The function takes as an argument the full table of infobox arguments. A data table is then created with blank entries for each piece of data handled by this function. The keys in this table must match the corresponding keys in the args table and hence the names of the parameters in.

The function then iterates through each entry in this data table and checks if there's a non-empty corresponding entry in the args</tt> table. This way, it processes only the entries in the data</tt> that actually have information in the infobox. The potentially comma-separated string passed to the infobox is then converted into a table. This occurs whever there are actually multiple values for infobox field or not. Either way, we get a table, even if it only has 1 entry.

If this table has more than 4 items, only the first will be assigned to the relevant entry in the data</tt> table, followed by "et al.". This phrase is wrapped in a span with the other values in the table placed in the span's title attribute, displaying them when "et al." is hovered over on desktop. Otherwise (in most cases), all values will be listed in the relevant data</tt> table entry. Each of these are linked with the display text set to the undabbed page name.

The data</tt> table is then returned. This table will be added to by subsequent functions before being converted into an output string.

h.getAndFormatIssuesText</tt> (unused)
Following this is an old unused function which formatted issues for comics. However, this information is not currently being displayed in finished citations and so it has been removed.

It worked very similarly to the above but without links (and without the whole "et al." thing when more than 4 items are given.

h.getAndFormatSeriesText</tt>
This function creates the bit of text stating the source's series. There are multiple places that this could come from in the infobox, none of which are ideal or one-size-fits-tall, which are dealt with here. Therefore, it takes the args</tt> table as an argument.

The simplest case is if citation series</tt> is specified. This is an overide field that allows the exact series to be used to be specified. No further formatting occurs here.

The slightly more complex fallback is if range</tt> is specified. In this case, the field is linked and that is used.

Failing this, series</tt> is used. This infobox field is primarily used for navigation and so is unideal but is often the best that we have. The overrides</tt> (not variable is a data module, Module:Cite source/series overrides, that holds common overrides that should be applied automatically. For example, " Doctor Who television stories </tt>" is very commonly given in the series</tt> infobox field and this data module contains a rule to automatically convert this to " Doctor Who </tt>". If an override does exist, it is used. Otherwise, the raw text passed to series</tt> is used. <tt>series</tt> is a freeform variable which should already have links and hence no additional formatting is applied. Finally, the <tt>season number</tt> entry in the <tt>args</tt> table is checked and, if this exists, the season/series is added to the end of the series text.

The series text produced is this return. Note that, if no series is specified anywhere, this may be blank and this is fine.

<tt>h.getReleaseYearText</tt>
This function formats the release year presented in the infobox. This should be presented unlinked in accordance with and hence a lot of formatting must be done here.

Firstly, all date fields from the infobox are combined, using the first non-empty field in the following list: These fields are run through <tt>util_link.stripDab</tt> in order to remove parenthetical notes and are then split on "-" in order to separate the start and end release dates of serialised sources, these 2 dates being placed in a table.
 * <tt>release date</tt>
 * <tt>broadcast date</tt>
 * <tt>premiere</tt>
 * <tt>beta release date</tt>
 * <tt>cover date</tt>

If this table has 2 or more entries, it can be assumed that there is both a release date and a release end date. These are extracted and assigned to their respective variables. <tt>:gsub("%s+", "")</tt> strips any whitespace while <tt>:sub(-4)</tt> extracts the final 4 characters of the date which should be the year. One simple way to check is to see if the string contained the year can be converted into a number. This is done and, if it can't, the dates are readjusted accordingly.

If the table only has 1 entry, the source only has a single release date and the same process as above occurs on this, without the check to see if the year is a number.

The release date(s) are then formatted with links added and the resulting string is returned.

<tt>h.makeOutput</tt>
This is a key function in this half of the module which combines all of the processed data from all previous functions into a cohesive string of text. It takes as arguments a <tt>data</tt> table containing all of the processed data produced by the previous functions and an <tt>args</tt> table containg the raw arguments from the infobox.

If <tt>data["writer"]</tt> is non-empty, this is added to the output. A check is then made to see if <tt>data["adapted from"]</tt> is non-empty. If it is, this to is added to the output. Moreover, a SMW query is used to check if the source from which the current source was adapted from has a writer. If it does, this added to the output in brackets. If there was no writer, this bit is skipped and only the "adapted from" information is added to the output.

The remaining data is then formatted so that most of it is placed in brackets with the first piece of data outside of the brackets. For example, "Doctor Who season 1 (BBC tv, 1963-1964)". To achieve this, all possible pieces of data are looped through and, if they exist, they are added to a table called <tt>loopData</tt>. It is at this stage that italics are added to the source's anthology, if it was published within one. The remainder of the output is then built up through a series of if statements.

Following this, if the <tt>outputText</tt> variable is, for whatever reason, blank, it is set to an error message. Otherwise, a full stop is added. <tt>outputText</tt> is then returned.

<tt>h.storeText</tt>
This little function deals with storing the output of this half of the module. The first line stores it to Extension:Variables. The next line deletes any previous text for this source stored in Extension:LuaCache and the line following this stores the up to date output to LuaCache. The final line stores the output to Extension:Semantic MediaWiki in Property:Story info.

This function returns nothing.

<tt>p.infoboxStore</tt>
This function is the main way in which all of those discussed so far will be called. It is integrated into and will execute on any pages with this infobox. It is responsible for combining all prior functions to take all of the arguments from the infobox and process them into a cohesive string of text which is then stored.

It takes as an argument the frame object. If you are unsure what this is, have a look at this blog post. The arguments passed to the infobox are then extracted and assigned to the <tt>args</tt> table. The next line takes the first unamed argument passed to the module when it is invoked in the template. This is not the first argument passed to the infobox, rather something hardcoded and passed into the module directly by the template. This first unamed argument will be <tt> </tt>, a magic word that evaluates to the name of the page that the infobox is used on.

Following this, the <tt>citation text</tt> argument is checked and, if it exists, pretty much everything else is skipped and just this is used for the output.

Otherwise, a full citation is put together. Firstly, the <tt>separator</tt> argument is checked. This is used for whenever there is a list of multiple items. They aresplit on commas by default but other separators can be set if necassary, such as if commas appear in one of the items in the list.

The arguments passed into the various formatting functions documented above are actually not entirely the raw arguments passed into the infobox. A bit of pre-processing occurs here first. Firstly, <tt>anthology</tt> and <tt>audio anthology</tt> are combined. The same happens to <tt>adapted from</tt> and <tt>novelisation of</tt>.

Following this, the bulk of the processing to produce the finished additional info occurs by calling each of the functions documented above. The <tt>data</tt> table is created by <tt>h.getAndFormatUnlinkedItems(args)</tt> and then added to by each of the other 2 functions. Note the commented out line. This information is not currently included in the additional information but may be reincluded in the future.

Following this, each piece of data in the <tt>data</tt> table is assigned to a variable so that they can be reused in. The same occurs for the unprocessed <tt>adapted from</tt> argument.

Following this, <tt>h.makeOutput</tt> is run and the result of this is passed to <tt>h.storeText</tt> to be stored. Nothing is returned from the function.

<tt>p.variantStore</tt>
more technical documentation to be added


 * visit Module:Cite_source/doc to edit this page