<noinclude>{{Documentation subpage}}</noinclude>
Add the following code before using any of the functions from the "Common" library.
<syntaxhighlight lang="lua">
local common = require("Module:Common")
General notes for all modules:
* '''Don't remove functions without discussing it first.''' A lot of scripts may break if you do so.
* Before adding any new functions, test it first if there are no syntax errors, so the new fragment of code won't break scripts that are using this library.
* When modifying existing functions, make sure that the new version is compatible with old one. Any modification may only extend functionality, but can't reduce or change behavior of current version.
* Creation of global functions and variables is no longer possible. Everything that's going to be exported has to be returned at the end of module.
* Once again, '''test it''' before doing any modifications.
The names are being sorted in alphabetical order.
=== cv(a, b, c) ===
Useful in cases, when we have to choose one of 2 values to pass to same variable or table element.<br/>
If '''a''' {{h:title|evaluates to ''true''|nil or false evaluate to false, everything else, even zero to true}} then function will return '''b''', otherwise it'll return '''c'''.<br/>
Part of code like
<syntaxhighlight lang="lua">
local tbl = {}
if condition then
  tbl[#tbl+1] = 'TRUE'
  tbl[#tbl+1] = 'FALSE'
can be simplified to
<syntaxhighlight lang="lua">
local tbl = {}
tbl[#tbl+1] =, 'TRUE', 'FALSE')
=== exists(name) ===
Checks if given Touhou Wiki page exists. It uses the [[wikipedia:Wikipedia:Template limits#Expensive parser function calls|expensive]] preprocessor function <nowiki>{{</nowiki>#ifexist:''Page name''<nowiki>}}</nowiki>. Limit of expensive function calls for Touhou Wiki is currently 100. If that number is exceeded then for next calls '''exists''' will return ''false'', whether page exists or not.
<syntaxhighlight lang="lua">
common.exists("Touhou Wiki")
will return true, unless the main page is going to be moved or deleted.
<syntaxhighlight lang="lua">
will return true or you'll get a nice, red "Script error" (unless you've implemented own '''exists''').
=== getPage(name) ===
Tries to get contents of a given page. No expensive functions are being used.<br/>
This function will most probably fail for most of special pages, so it's generally usable for article pages.<br/>
If a given page doesn't exist or contents can't be retrieved, it'll return ''nil''.<br/>
For example, following code will return the partially preprocessed text for main page:
<syntaxhighlight lang="lua">
local main = common.getPage("Touhou Wiki")
Page's content is partially preprocessed, so watch out for [[wikipedia:Help:Strip markers|strip markers]], especially in section headers. These are unique strings that start and end with 'DEL' character (127, 0x7f) and look like ⌦UNIQ3fc7e69e5822d9c0-nowiki-00000001-QINU⌫ (where ⌦ and ⌫ are starting and ending 'DEL' characters).<br/>
When retrieving page contents, section header '''<nowiki>== Documentation ==</nowiki>''' is actually returned as text similar to '''<nowiki>==⌦UNIQ3fc7e69e5822d9c0-h-6--QINU⌫ Documentation ==</nowiki>'''.<br/>
Usually it's not visible, because it's stripped from text displayed to users, but it's something to keep in mind when processing page contents in Lua (for example when searching for specific header).
Following code was used to expose the strip markers:
<syntaxhighlight lang="lua">
string.gsub(content, "%\127(UNIQ.-QINU)%\127", "⌦%1⌫")
=== isInTable(tbl, val) ===
Checks if given element exists in table and returns it's index. Returns ''nil'' if the element can't be found.
=== isset(val) ===
It will return ''false'' if passed argument is a ''nil'' value (undefined variable) or an empty string, and ''true'' in any other case.
=== partialTableCompare(tbl1, tbl2, s1, s2, n) ===
Compares '''n''' elements of two numbered tables, '''tbl1''' and '''tbl2''', starting with element '''s1''' in table '''tbl1''' and '''s2''' in table '''tbl2''' and returns ''true'' when appropriate elements in this range are equal, ''false'' if there's at least one difference.<br/>
In case that n=0 it'll return ''true'', because it didn't find any differences.
=== stripTags(text) ===
Removes any <u>valid</u> HTML code from given string. It works only for proper pairs of opening and closing tags (like &lt;div&gt; &lt;/div&gt;) or for closed tags (like &lt;br/&gt;).
=== sliceTable(values, i1, i2) ===
"Given a Lua table, extract a sub-array or slice. This function works like string.sub: if the end of the range is not specified, then everything up to the end is assumed; this argument can also be a negative number, where -1 is the end, -2 second-last, etc."
=== trunkTable(tbl) ===
The given table is sorted (using '''table.sort''') and repeating elements are being removed. It directly modifies the passed table and no value is returned.
[[Category:Lua Libraries]]
Community content is available under CC-BY-SA unless otherwise noted.