模板:Database report/doc
| 此頁面是Template:Database report的模板文件。 此頁面可能包含了模板的使用說明、分類和其他內容。 |
| 此模板使用Lua語言: |
此模板可召喚機器人,依照所給資料庫查詢的結果來更新頁面。本模板必須搭配「{{Database report end}}」來收尾,機器人會將兩者之間內容替換為報表。同一頁面可以放置多份報告。
你可以隨時點擊「立即更新」連結,讓機器人更新報表,還可以設置|interval=N(每N天更新一次)啟用定期自動更新。
模板有部分格式字段,如可給頁面標題加上連結,或展示條目內容摘錄。若有複雜需求,還可使用row_template和postprocess_js字段,用維基標記、Lua、JavaScript自訂內容輸出格式。
若想創建多份配置類似的報表,則可使用Lua設定參數。詳情請見Module:Database report。
本模板目前由PexBot供養。調用本模板的頁面將歸入Category:PexBot資料庫報告訂閱。
不會寫SQL?別擔心,英文維基百科SDZeroBot的AutoSQL能幫你把人話(英語)改寫成SQL查詢。不過工具不包成功,如果還不行可以到WP:RAQ求助。
例子
[編輯]{{Database report
|sql =
SELECT page_title, rev_timestamp AS 最新版本id, actor_name AS 創建者, user_editcount
FROM pagetriage_page
JOIN page on page_id = ptrp_page_id
JOIN revision ON page_id = rev_page AND rev_parent_id = 0
JOIN actor ON rev_actor = actor_id
LEFT JOIN user ON user_id = actor_user
WHERE page_namespace = 0
AND page_is_redirect = 0
AND ptrp_reviewed = 0
|wikilinks = 1, 5:2 <!-- 將第1列連結到主空間,第5列連結到用戶空間(ns 2) -->
|excerpts =
|comments =
|widths = 2:9em, 3:15em <!-- 將第2列列寬設為9em,第三列列寬設為15em -->
|interval = 7 <!-- 每7日更新一次 -->
|table_style =
|pagination =
|max_pages =
}} ...(機械人將覆寫此處)... {{Database report end}}
參數們
[編輯]sql
[編輯]用來生成報告的SQL查詢。可以先用Quarry測試寫的SQL。如果查詢的結果數極多,可用LIMIT子句限制輸出大小,或用pagination分頁。要點:查詢必須在10分鐘內完成。機器人使用伺服器端逾時(MariaDB的max_statement_time變元)來避免資料庫過載。此處只認單條表達式,不要用分號塞多條表達式。
若SQL中有豎線(|),或視為wikitext語法字元(譬如{{和}}),請用<nowiki>...</nowiki>包裹SQL。
此為唯一必填參數。
資料庫表參考資料:page · revision · text · actor · user · main database layout manual page
wikilinks
[編輯]給頁面標題加維基連結。下述示例就是最好的解釋:
wikilinks = 4將第4列中的標題加連結。若該列為完整頁面標題,可採用此方式。wikilinks = 4:2將第4列中的標題連向用戶(ns:2)空間。若該列為不含命名空間的標題,可使用此方式。- 命名空間編號詳見Template:Namespaces。以下摘錄常用編號:0(條目)、2(用戶)、4(維基百科)、6(檔案)、10(模板)、14(分類)、118(草稿)。對應的討論空間再加1。
wikilinks = 5:2:show將第4列中的標題連向用戶(ns:2)空間,且顯示時補上命名空間前綴。wikilinks = 4:c3把第3列中的命名空間編號套用給第4列。多用於SELECT page_namespace, page_title from ...式查詢,即頁面標題或命名空間在兩列上。wikilinks = 4:c3:show同上,但補上命名空間前綴顯示。wikilinks = 2:2:show, 3:0給多列添加連結!各列配置間以分號分隔,每列設置方式見上。wikilinks = 1:0, 2:2:show, 3:4給三列添加連結:第1列連結到主空間,第2列連結到用戶空間且顯示「User:」前綴,第3列連結到WP空間(用管道連結除去前綴)。
comments
[編輯]- Comma-separated list of column numbers that contain edit summary comments or log action comments. Specifying this makes the bot escape the text so that edit summaries like "added {{infobox person}}" or "removed [[File:Example.jpg]]" are properly displayed without the template being transluded or images showing up on the report page.
widths
[編輯]明確指定欄寬。如widths = 2:10em, 4:20px即將第2欄設為10em寬,把第4欄目調整為20px寬。
table_style
[編輯]- The style attribute applied to the table element. By default this is
overflow-wrap: anywherewhich causes better overflow behaviour. To suppress the default, useoverflow-wrap: normal.
table_class
[編輯]- The class attribute applied to the table element. By default, this is
wikitable sortable.
excerpts
[編輯]- Show an excerpt of articles. Excerpt config takes the form:
srcColumnNumber : destColumnNumber : namespace : charLimit : charHardLimit
- Apart from srcColumnNumber everything is optional. However, optional parameters can only be used if the ones before were used, that is: to mention namespace, you also need to mention destColumnNumber first.
- srcColumnNumber: (required) The column number containing page names whose excerpts are to be shown
- destColumnNumber: The column number in which to place the excerpt. The original contents of that column will be shifted rightwards. If unspecified this takes the value (srcColumnNumber + 1)
- namespace: The namespace number to use for page names in srcColumnNumber. If unspecified, it's taken as 0 – which means that the page name in srcColumnNumber is treated as the full page name (appropriate for mainspace). Use
c4to get the namespace number from 4th column. This can be used as input to {{Namespace name}}, to return the actual name:"{{Namespace name|1}}:"will return "Template:Namespace name:" and"{{Namespace name|10}}:"will return "Template:Namespace name:". - charLimit: A soft limit for size of excerpt by number of characters. This is a soft limit, the excerpt can be longer to avoid cutting mid-sentence. (Default: 250)
- charHardLimit: A hard char limit for size of excerpt. Sentences may be cut in the middle (but in that case will end in ellipsis). (Default: 500)
- Most of the time, excerpt columns will mangle the layout of the table. Always use the widths parameter to tune the column widths.
- Since excerpts add columns to the table, note that the column numbers used by all other parameters (widths, wikilinks, comments, remove_underscores) take into account the added columns.
- Excerpts are only intended for articles and drafts. The Node.js code used for generating them is here – only the initial few lines of the first paragraph are kept with references, markup, images, infoboxes etc are removed.
- Excerpts for multiple columns are supported – in which case this parameter should be comma-separated list of excerpt configs.
remove_underscores
[編輯]- A comma-separated list of column numbers in which underscores are to be replaced with spaces. This is intended for columns containing page titles, since the database stores titles with underscores (_) in place of spaces. You can prettify it a bit by using this parameter. This is automatically done for columns with wikilinks or excerpts.
interval
[編輯]- (No default) Numerical value. Number of days to wait between automatic updates. Minimum allowed value: 1. If unspecified, no periodic updates are done.
pagination
[編輯]- (No default) Number of results to include in a page. Further results are saved to paginated subpages. For example, if
|pagination=1000is set and there are 4200 results, 1–1000 are saved to the page containing the query. 1001–2000 are saved to the/2subpage, and so on. If unspecified, all results are saved on a single page. See also max_pages. - Note: Pagination cannot used when there are multiple reports on a page.
max_pages
[編輯]- (Default: 5). For use with pagination only. Max number of report pages to create. This can be set upto 20.
hide
[編輯]- Hide a column. Normally columns can be hidden by just not including it in the SELECT statement. However, some columns like namespace number are necessary for generation of wikilinks but undesirable in final output. Specify the column number(s) of such column(s) here, comma-separated if multiple.
row_template
[編輯]- Generates a template call rather than a table row; the template must then generate the table row in turn or the table header/footer need to be suppressed using
skip_tableparameter. Values in the row are passed to the template as unnamed parameters (|1=,|2=, etc).- To have the template substituted instead of transcluded, put "subst:" before the template name.
- To use a Lua module instead of a template, put "#invoke:" before the module name.
row_template_named_params
[編輯]- While using row_template, instead of using unnamed parameters, use the column names as parameters. This is a boolean field - any value will work.
skip_table
[編輯]- Suppresses the table markup completely when using
row_template. Any value will work.
header_template
[編輯]- Generates a template call rather than a table header; the template must then generate the table header. No parameters are passed to this template. The template can be substituted as well - just specify "subst:" before the template name.
- If this is used without using
row_templateas well, then the header template will be placed over the table, and table headers will still be generated.
footer_template
[編輯]- Generates a footer calling the given template. For use with
skip_table. This could be used along withheader_templatefor collapsing (collapse top/collapse bottom) or creating columns (div col/div col end) in the generated content. No parameters are passed to the template. It can be substituted instead by specifying "subst:" before the template name. For quick reference:|}is {{end}}.
silent
[編輯]- Suppress visible output from this template. Only the table generated by the bot will be visible. The last update timestamp and query runtime are also not saved. This means that if the query result is unchanged, periodic updates won't reflect in the page history.
- This is a boolean field - any value will work.
postprocess_js
[編輯]- Experimental
- Custom postprocessing code, in JavaScript, which is executed by the bot on the raw db output. Format:
async function postprocess(rows) { // `rows` is an array. // Each element in `rows` is an object with column names as keys, and with values stringified // Eg. for SELECT page_namespace, page_title FROM page LIMIT 2 // the `rows` could be: // [ // { page_namespace: '0', page_title: 'Main Page' }, // { page_namespace: '1', page_title: 'Main Page' }, // ] // Write code to process rows here. // Return it after modifications: return rows; }
- The postprocess() function is applied on the query result before it is modified by other formatting options like wikilinks. It can be used to introduce new derived rows or columns (eg. a "Total" row at the bottom), dynamically modify rows based on content of other rows, etc. If the code contains wikimarkup-like syntax that could confuse the parser, wrap the whole code within
<nowiki>...</nowiki>. - The JS code is run on Node.js v22.16.0 sandboxed to use upto 16 MB of memory and 1.5 seconds of execution time. No network or filesystem access is allowed – with the exceptions below:
await bot.request('https://...')provides readonly access to various Wikimedia APIs. Supported endpoints: en.wikipedia.org/api/rest_v1, wikimedia.org/api/rest_v1, en.wikipedia.org/w/rest.php, en.wikipedia/org/w/api.php, api.wikimedia.org and query.wikidata.org. These can be used for fetching pageview data (from REST API), ORES scores (from Lift Wing), and so on.- For the Action API,
await bot.api({action: 'query', ...})can be alternatively used. Parameters action: 'query', format: 'json' and formatversion: 2 are set by default.
lua_source
[編輯]- Read the full configuration of the report from a Lua module. All setup should be done in the module through Module:Database report. See the module documentation for more details. If any parameter is configured in both Lua and the template, the value from the template takes precedence.
lua_function
[編輯]- (Default: main) Name of the lua function in lua_source which returns the generated configuration. Only applicable when lua_source is used.
lua_arg_*
[編輯]- Parameters can be passed to the Lua function. For example,
|lua_arg_foo=barpasses|foo=barto the module. Only applicable when lua_source is used.
SQL formatting tips
[編輯]- Datetime columns are stored in YYYYMMDDHHmmss format. Select them as
DATE_FORMAT(rev_timestamp, '%Y-%m-%d %H:%i')orDATE_FORMAT(rev_timestamp, '%Y-%m-%d'). - For generating a serial number column, select
ROW_NUMBER() OVER(ORDER BY (SELECT 1)) AS "No.". For example, see WP:Database reports/Fully salted titles with unusually long expiries. - To use this with {{static row numbers}}, put
header_template = static row numbersandtable_class = wikitable sortable static-row-numbers static-row-header-text. - To display full wikilinked page names where namespace can vary (as in
SELECT page_namespace, page_title FROM ...), use|wikilinks=2:c1:showand then hide the namespace column from display (|hide=1). - This template also plays well with {{Namespace name}}, which allows you to avoid extra queries: for example,
"{{Namespace name|1}}:"will return "Template:Namespace name:" and"{{Namespace name|8}}:"will return "Template:Namespace name:".
Advanced styling
[編輯]The default table_style is overflow-wrap:anywhere as it avoids columns becoming too wide due to presence of long non-breaking text. Sometimes, using overflow-wrap: anywhere; word-break: break-word can help instead. These may cause even the headers to break mid-word – use the widths setting to avoid that.
Another way to avoid the ugliness of headers breaking in the middle of a word is to apply the style on the <tbody> element instead. To do this, use table_class to apply a distinguishing class and then style it via TemplateStyles. You can also apply custom styles on the <thead> element this way.
The design of the bot and template were inspired from User:ListeriaBot and {{Wikidata list}}.
TemplateData
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Tabulate results of SQL queries against wiki replica databases
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||