وحدة:Find sources
This module produces a list of links to search engines to help editors find sources about a given subject. It implements {{find sources}} and other similar templates. It is highly extensible; new templates and new link types can be added easily and by any user.
الاستخدام
[عدل]From wikitext
[عدل]Usually, from wikitext this module should be used via a template, e.g. {{find sources}}. However, it is also possible to use it directly from #invoke, like this:
خطأ لوا في السطر 88: invalid template name template; no template config found at Module:Find sources/templates/''template''.
- template is the name of the template that should be called. It must be the full page name of the template, without the namespace, and with the correct capitalisation. So, for قالب:Find sources, the template name should be "Find sources"; "find sources" with a lower-case "f" would cause an error. This parameter is required.
- search term 1, search term 2, etc., are the terms to be searched for in each of the search engine links. The first search term will appear in quotation marks, and subsequent search terms will be added without quotation marks. Each term will be separated by spaces. These parameters are all optional. If no parameters are given, the current page name will be used as a search term.
From Lua
[عدل]Load the module with the following code:
local mFindSources = require('Module:Find sources')
You can then produce the list of search links like this:
mFindSources._main(template, searchTerms)
- template is the template name, as outlined in the #From wikitext section above. This parameter is required.
- searchTerms is an array of search terms. Each item in the array corresponds to a numbered search term as outlined in the #From wikitext section above. This parameter is optional.
Example syntax:
mFindSources._main('Find sources', {'Albert Einstein', '-"Marilyn Monroe"', 'relativity', 'science'})
Available templates
[عدل]The following templates are available for use:
Template | Description | Example | Configuration |
---|---|---|---|
Find sources | The most widely used source-finding template. Until September 2014 it was transcluded on all AfD discussion pages. | جِد مصادر: جوجل (كتب · أخبار · الباحث العلمي · صور حرة · مصادر ويكيبيديا) · مصادر الصحف الإنجليزية الحرة · موقع JSTOR · نيويورك تايمز · مكتبة ويكيبيديا | main، documentation |
Find sources mainspace | No description available | جِد مصادر: "Example" – أخبار · صحف · كتب · الباحث العلمي · جايستور | main، documentation |
Find sources medical | No description available | Find medical sources: Source guidelines · PubMed · Cochrane · DOAJ · Gale · OpenMD · ScienceDirect · Springer · Trip · Wiley · WP Library | main، documentation |
Find sources video games | No description available | Find video game sources: "Example" – news · newspapers · books · scholar · JSTOR · free images · free news sources · WP Library · NYT · WP reference · VG/RS · VG/RL · WPVG/Talk | main، documentation |
Making new templates
[عدل]There are two basic ingredients to making a new source-finding template. The first is the template configuration module, and the second is the template invocation on the template page.
Template configuration page
[عدل]To find the name of the template configuration page, take the page name of your proposed template without the namespace prefix, and add it to the base page of "Module:Find sources/templates/". For example, for قالب:Find sources, the configuration page is located at وحدة:Find sources/templates/Find sources. Note that the template must be capitalised exactly as the page name is, otherwise the arguments will not be passed through from the template page to the module.
The template configuration page should look something like this:
return {
blurb = "Find sources for $1 – $2",
introLink = {code = 'google'},
links = {
{code = 'google news', display = 'news'},
{code = 'google newspapers', display = 'newspapers'}
},
separator = ', ',
isUsedInMainspace = true,
class = 'custom-class',
style = 'color: green; font-size: 110%;'
}
- blurb - the text presented to users. There are two parameters available in the blurb, $1 and $2. $1 is the intro link, if specified, and $2 represents the other links. This field is required.
- introLink - an optional introductory link. If specified, this must be a link table. If the display value in the link table is not set, the search terms that the user entered will be used instead.
- links - an array of link tables defining the main links to be used by the template. This field is required.
- separator - the text separating the search engine links. This field is optional; the default value is taken from ميدياويكي:Dot-separator.
- isUsedInMainspace - set this to true if the template will be used in the main namespace. If this is not set, when the template is used in the main namespace it will produce an error and be put in Category:Pages with templates in the wrong namespace.
- class - a custom HTML class to apply to the template text. The "plainlinks" class is added by default. This field is optional.
- style - custom CSS to apply to the template text. This field is optional.
Once you have created your template configuration page, you need to add the template to وحدة:Find sources/templates in order for it to show up in the table of templates on this documentation page.
Link tables
[عدل]Link tables are used in the "introLink" and "links" fields of the template configuration. They can contain two fields, "code" and "display". The "code" field is compulsory, and must be one of the link codes listed in the table below. The "display" field is optional, and specifies a custom display value for that link to be used by the template. If the display field is not set, a default value is set by the module. For the "introLink" field, the default value is the search text entered by the user; for the "links" field, the default value is defined in the link configuration modules.
The following table contains all the available link codes, with descriptions and examples.
Code | Description | Example | Configuration | Notes |
---|---|---|---|---|
Invalid link configuration detected on page Module:Find sources/links/archive.org. | ||||
bing | No description available | Bing | main، documentation | |
Invalid link configuration detected on page Module:Find sources/links/britannica. | ||||
Invalid link configuration detected on page Module:Find sources/links/british library. | ||||
Invalid link configuration detected on page Module:Find sources/links/ccsearch. | ||||
Invalid link configuration detected on page Module:Find sources/links/cochrane. | ||||
Invalid link configuration detected on page Module:Find sources/links/doaj. | ||||
duckduckgo | No description available | DuckDuckGo | main، documentation | |
Invalid link configuration detected on page Module:Find sources/links/eowb. | ||||
Invalid link configuration detected on page Module:Find sources/links/free news sources. | ||||
Invalid link configuration detected on page Module:Find sources/links/gale. | ||||
Invalid link configuration detected on page Module:Find sources/links/gin. | ||||
Invalid link configuration detected on page Module:Find sources/links/globe and mail. | ||||
No description available | main، documentation | |||
google books | No description available | Google Books | main، documentation | |
google free images | No description available | Free Google Images | main، documentation | |
google news | No description available | Google News | main، documentation | |
google newspapers | No description available | Google Newspapers | main، documentation | |
google scholar | No description available | Google Scholar | main، documentation | |
Invalid link configuration detected on page Module:Find sources/links/haaretz. | ||||
Invalid link configuration detected on page Module:Find sources/links/infoplease. | ||||
jstor | No description available | JSTOR | main، documentation | |
Invalid link configuration detected on page Module:Find sources/links/library of congress. | ||||
Invalid link configuration detected on page Module:Find sources/links/mail and guardian. | ||||
Invalid link configuration detected on page Module:Find sources/links/medrs. | ||||
Invalid link configuration detected on page Module:Find sources/links/muse. | ||||
new york times | No description available | New York Times | main، documentation | |
Invalid link configuration detected on page Module:Find sources/links/new zealand herald. | ||||
Invalid link configuration detected on page Module:Find sources/links/openlibrary. | ||||
Invalid link configuration detected on page Module:Find sources/links/openmd. | ||||
Invalid link configuration detected on page Module:Find sources/links/pubmed. | ||||
Invalid link configuration detected on page Module:Find sources/links/sciencedirect. | ||||
Invalid link configuration detected on page Module:Find sources/links/south china morning post. | ||||
Invalid link configuration detected on page Module:Find sources/links/springer. | ||||
Invalid link configuration detected on page Module:Find sources/links/statpearls. | ||||
Invalid link configuration detected on page Module:Find sources/links/straits times. | ||||
Invalid link configuration detected on page Module:Find sources/links/sydney morning herald. | ||||
Invalid link configuration detected on page Module:Find sources/links/the age. | ||||
Invalid link configuration detected on page Module:Find sources/links/the guardian. | ||||
Invalid link configuration detected on page Module:Find sources/links/the hindu. | ||||
Invalid link configuration detected on page Module:Find sources/links/the times. | ||||
Invalid link configuration detected on page Module:Find sources/links/times of india. | ||||
Invalid link configuration detected on page Module:Find sources/links/trip. | ||||
Invalid link configuration detected on page Module:Find sources/links/uptodate. | ||||
Invalid link configuration detected on page Module:Find sources/links/vgrl. | ||||
vgrs | No description available | VGRS | main، documentation | |
Invalid link configuration detected on page Module:Find sources/links/vgtalk. | ||||
wikipedia library | No description available | WP Library | main، documentation | |
wikipedia reference search | No description available | Wikipedia Reference Search | main، documentation | |
Invalid link configuration detected on page Module:Find sources/links/wiley. |
Template invocation
[عدل]The template invocation on the template page itself should look like this:
خطأ لوا في السطر 88: invalid template name template; no template config found at Module:Find sources/templates/''template''.<noinclude> خطأ لوا في وحدة:Find_sources/autodoc على السطر 314: invalid template name template; no template config found at Module:Find sources/templates/''template''. </noinclude>
This activates the template code and also provides automatic documentation. template is the name of the template without the namespace prefix, and must be correctly capitalised.
If you want to use custom documentation from a /doc subpage instead of the automatic documentation, use this invocation instead:
خطأ لوا في السطر 88: invalid template name template; no template config found at Module:Find sources/templates/''template''.<noinclude>
تم كشف حلقة قالب: وحدة:Find sources/شرح
<!-- Categories go on the /doc subpage, and interwikis go on Wikidata. --> </noinclude>
Adding new links
[عدل]To make a new link code available to use from source-finding templates, you need to make a link configuration module for it. The page name for this module should start with "Module:Find sources/links/" and end with the link code. Link codes should be short but descriptive, and should be in lower case. For example, the link code for Google search is "google", and the configuration page is at وحدة:Find sources/links/google.
The link configuration page should look something like this:
return {
url = '//www.google.com/search?q=$1',
display = 'Google',
separator = ','
}
- url - The url to perform the search. This field takes a parameter $1, which is the URL-encoded search text entered by the user. This field is required.
- display - If a display value is not specified in the template configuration, this value is used instead. This field is required.
- separator - This is used to separate the search terms entered by the user. This is optional, and defaults to "+" (a URL-encoded space).
Once you have created your link configuration page, you need to add the link to وحدة:Find sources/links in order for it to show up in the table of links on this documentation page.
Automatic documentation
[عدل]Automatic documentation is provided for templates based on this module; the documentation is generated by وحدة:Find sources/autodoc. The autodoc module uses the following pages:
- قالب:Find sources documentation - the template used for most of the documentation content.
- The template configuration page. Used to find the list of link codes, and to see if the template is used in articles.
- The template documentation configuration page. Used to find template shortcuts.
- The link documentation configuration page. Used to find descriptions of each of the search links.
Template documentation configuration page
[عدل]This page is located at the "/autodoc" subpage of the template configuration page. It is only used to generate the documentation, not to generate any of the actual template output, so it does not need to be protected. It should look something like this:
return {
shortcuts = {'shortcut1', 'shortcut2'},
description = 'This template is used to provide source links on [[ويكيبيديا:نقاش الحذف]] pages',
docIntro = 'This template produces a series of search-engine links to be used on [[ويكيبيديا:نقاش الحذف]] pages.'
}
- shortcuts - an array of shortcuts that redirect to the template page. These are displayed on the template documentation page using the {{اختصار قالب}} template.
- description - a brief description of what the template does. This is displayed on this documentation page in the table of available templates.
- docIntro - text to be used instead of the first sentence of the template documentation for individual source-finding templates. This is optional, and the default value is "This template produces a series of links to various search interfaces to help find additional reference material for articles."
Link documentation configuration page
[عدل]This page is located at the "/autodoc" subpage of the link configuration page. It is only used to generate the documentation, not to generate any of the actual template output, so it does not need to be protected. It should look something like this:
return {
description = '[[جوجل]], the flagship search engine from Google Inc.',
notes = 'This is the most commonly used search link.'
}
- description - a brief description of what the search engine link does. This is used in the template documentation to generate the list of link descriptions, and also to make the descriptions in the table of link codes above. This is optional, but recommended.
- notes - notes about the search engine link. These are put in the table of link codes above, but they are not put in the template documentation for individual source-finding templates. This field is optional.
Configuration
[عدل]The messages used in this module can be found at وحدة:Find sources/config and وحدة:Find sources/autodoc/config. This can be helpful for translating this module for use in other languages. Note that any template and link configuration used must also be translated.
-- This module implements {{find sources}} and other similar templates, and
-- also provides a mechanism to easily create new source-finding templates.
-- Define constants
local ROOT_PAGE = 'Module:Find sources'
local TEMPLATE_ROOT = ROOT_PAGE .. '/templates/' -- for template config modules
local LINK_CONFIG = ROOT_PAGE .. '/links' -- for link config modules
local CONFIG_PAGE = ROOT_PAGE .. '/config' -- for global config
-- Load required modules
local checkType = require('libraryUtil').checkType
local cfg = mw.loadData(CONFIG_PAGE)
local p = {}
local function maybeLoadData(page)
local success, data = pcall(mw.loadData, page)
return success and data
end
local function substituteParams(msg, ...)
return mw.message.newRawMessage(msg, ...):plain()
end
local function renderSearchString(searchTerms, separator, transformFunc)
-- This takes a table of search terms and turns it into a search string
-- that can be used in a URL or in a display value. The transformFunc
-- parameter can be used to transform each search term in some way (for
-- example, URL-encoding them).
local searchStrings = {}
for i, s in ipairs(searchTerms) do
searchStrings[i] = s
end
if transformFunc then
for i, s in ipairs(searchStrings) do
searchStrings[i] = transformFunc(s)
end
end
return table.concat(searchStrings, separator)
end
function p._renderLink(code, searchTerms, display, tooltip)
-- Renders the external link wikicode for one link, given the link code,
-- a table of search terms, and an optional display value and tooltip.
-- Get link config.
local links = maybeLoadData(LINK_CONFIG)
local linkCfg = links[code]
if not linkCfg then
error(string.format(
"invalid link code '%s'; no link config found at [[%s]]",
code,
LINK_CONFIG
))
end
-- Make URL.
local url
do
local separator = linkCfg.separator or "+"
local searchString = renderSearchString(
searchTerms,
separator,
mw.uri.encode
)
url = substituteParams(linkCfg.url, searchString)
end
if tooltip then
return string.format('<span title="%s" style="border-bottom: 1px dotted;">[%s %s]</span>',
mw.text.encode(tooltip), url, display or linkCfg.display)
else
return string.format('[%s %s]', url, display or linkCfg.display)
end
end
function p._main(template, args)
-- The main access point from Lua.
checkType('_main', 1, template, 'string')
checkType('_main', 2, args, 'table', true)
args = args or {}
local title = mw.title.getCurrentTitle()
-- Get the template config.
local templateCfgPage = TEMPLATE_ROOT .. template
local templateCfg = maybeLoadData(templateCfgPage)
if not templateCfg then
error(string.format(
"invalid template name '%s'; no template config found at [[%s]]",
template, templateCfgPage
))
end
-- Namespace check.
if not templateCfg.isUsedInMainspace and title.namespace == 0 then
local formatString = '<strong class="error">%s</strong>'
if cfg['namespace-error-category'] then
formatString = formatString .. '[[%s:%s]]'
end
return string.format(
formatString,
cfg['namespace-error'],
mw.site.namespaces[14].name,
cfg['namespace-error-category']
)
end
-- Get the search terms from the arguments.
local searchTerms = {}
for i, s in ipairs(args) do
searchTerms[i] = s
end
if not searchTerms[1] then
-- Use the current subpage name as the default search term, unless
-- another title is provided. If the page uses a disambiguator like
-- "Foo (bar)", make "Foo" the first term and "bar" the second.
local searchTitle = args.title or title.subpageText
local term, dab = searchTitle:match('^(.*) (%b())$')
if dab then
dab = dab:sub(2, -2) -- Remove parens
end
if term and dab then
searchTerms[1] = term
searchTerms[2] = dab
else
searchTerms[1] = searchTitle
end
end
searchTerms[1] = '"' .. searchTerms[1] .. '"'
-- Make the intro link
local introLink
if templateCfg.introLink then
local code = templateCfg.introLink.code
local display = templateCfg.introLink.display or renderSearchString(
searchTerms,
' '
)
local tooltip = templateCfg.introLink.tooltip
introLink = p._renderLink(code, searchTerms, display, tooltip)
else
introLink = ''
end
-- Make the other links
local links = {}
local separator = templateCfg.separator or cfg['default-separator']
local sep = ''
for i, t in ipairs(templateCfg.links) do
links[i] = sep .. p._renderLink(t.code, searchTerms, t.display, t.tooltip) ..
(t.afterDisplay or '')
sep = t.separator or separator
end
links = table.concat(links)
-- Make the blurb.
local blurb = substituteParams(templateCfg.blurb, introLink, links)
local span = mw.html.create('span')
span
:addClass('plainlinks')
:addClass(templateCfg.class)
:cssText(templateCfg.style)
:wikitext(blurb)
return tostring(span)
end
setmetatable(p, { __index = function(t, template)
-- The main access point from #invoke.
-- Invocations will look like {{#invoke:Find sources|template name}},
-- where "template name" is a subpage of [[Module:Find sources/templates]].
local tname = template
if tname:sub(-8) == '/sandbox' then
-- This makes {{Find sources/sandbox|Albert Einstein}} work.
tname = tname:sub(1, -9)
end
return function(frame)
local args = require('Module:Arguments').getArgs(frame, {
wrappers = mw.site.namespaces[10].name .. ':' .. tname
})
return t._main(template, args)
end
end})
return p