Mediawiki notes

From Reboil

This page contains notes for using Mediawiki.

Stats

Emacs functions

Name link to a subpage after subpage basename. ([[../Chapter 1]] into [[../Chapter 1|Chapter 1]])

M-x query-replace-regexp
/\(.+\)]]
/\1|\1]]

Export/Import

Exporting

To export the wikicode of a given article, one heavyweight method involves using the maintenance/dumpBackup.php script to export an XML version of the target page, then parsing of the XML file with some program like grep.[1]

A lightweight method for exporting wikicode of a single article involves using the Mediawiki Revisions API[2] Command line tools such as curl may be used to authenticate via username an dpassword in order to obtain a session cookie; then the cookie may be used to authenticate an HTTP GET request requesting from a Mediawiki instance the wikicode contents of a single page.

An unverified ChatGPT-4 procedure is described below;

If your MediaWiki instance requires authentication, you can use the API with login credentials to fetch the wikicode of a page. You'll need to perform a login request to obtain a login token, then use that token in a second request to complete the login process. Finally, you can use the obtained session cookies to access the content of the page with authentication.

Here's an example using curl to perform these steps:

1. Get a login token:

$ curl -c cookies.txt "https://your-wiki-url.example.org/w/api.php?action=query&meta=tokens&type=login&format=json"
Replace your-wiki-url.example.org with your wiki's domain. The cookies.txt file is used to store the session cookies.

2. Extract the login token from the JSON response:

You can use a tool like jq to extract the login token from the JSON response:
$ login_token=$(curl -c cookies.txt "https://your-wiki-url.example.org/w/api.php?action=query&meta=tokens&type=login&format=json" | jq -r '.query.tokens.logintoken')

3. Log in using your username, password, and the obtained login token:

$ curl -b cookies.txt -c cookies.txt -d "action=clientlogin&username=YOUR_USERNAME&password=YOUR_PASSWORD&loginreturnurl=http://your-wiki-url.example.org/&logintoken=$login_token&format=json" "https://your-wiki-url.example.org/w/api.php"
Replace YOUR_USERNAME and YOUR_PASSWORD with your MediaWiki account's username and password.

4. Fetch the wikicode of a page with authentication:

Use the session cookies stored in cookies.txt to access the content of the page:
$ curl -b cookies.txt "https://your-wiki-url.example.org/w/api.php?action=query&prop=revisions&titles=Example_Page&rvprop=content&format=json"

5. Extract the wikicode content from the JSON response:

As before, you can use a tool like jq to extract the wikicode from the JSON response:
$ curl -b cookies.txt "https://your-wiki-url.example.org/w/api.php?action=query&prop=revisions&titles=Example_Page&rvprop=content&format=json" | jq -r '.query.pages[].revisions[0]["*"]' > example_page_wikicode.txt

This process allows you to authenticate with your MediaWiki instance and fetch the wikicode of a page, which can then be processed using your external script. After editing, you can import the modified content back into your MediaWiki installation using the API or other methods, like the importTextFiles.php script mentioned earlier.

Importing

Import text files (see 2022-09-01 example).[3]

$ cd /var/lib/mediawiki/maintenance  # NOTE: May need to be `root` or `www-data`
$ find ./wikicode/ -type f | parallel php importTextFiles.php -u DebianUser --bot '{}'
Note: do not use combining diacritical marks in article names. For example, use “á” (U+00E1) instead of “á” (U+0061 U+0301).

Extensions

Labeled Section Transclusion

Mark a selection for transclusion.

<section begin="chapter1" />this is a chapter 1<section end="chapter1" />

Transclude a marked selection.

{{#lst:articleX|chapter1}}

Transclude a section by section title.

{{#lsth:articleX|sectionX}}

Transclude a marked selection in a subpage.

{{#lst:{{PAGENAME}}/subpageX|chapter1}}

Transclude a section by section title in a subpage.

{{#lsth:{{PAGENAME}}/subpageX|sectionX}}

Formatting

See the Mediawiki reference pages:

Collapsible elements

A simple collapsible text box.[cmt 1]

<div class="toccolours mw-collapsible" style="width:400px; overflow:auto;">
This text is collapsible. {{Lorem}}
</div>

Fixed-width

Make short strings fixed-width with <code> tags. For example, "<code>foo</code>" becomes:

foo

Make glyphs in multiple lines fixed-width by inserting a single space before each line.

....o.......o.......o....
..o...o...o...o...o...o..
o.......o.......o.......o
..o...o...o...o...o...o..
....o.......o.......o....

Disable wrap of fixed-width lines via <pre style="white-space: pre;">foo</pre> tags.

 $ printf "==================================================================================================================================="

Fonts

For SVG fonts supported on Wikimedia Commons servers, see this page.

Increase size with <span> tags.

<span style="font-size:200%">Ł</span>
Renders as: Ł

Headings

Table of Contents

Inhibit the table of contents section by inserting __NOTOC__ anywhere on the page.[4]

Manually place the table of contents by inserting __TOC__ in the desired location on the page.[4]

Force the creation of a table of contents despite a low count of headings by inserting __FORCETOC__.[4]

HTML

Since Mediawiki is based on HTML, some characters may not appear correctly if pasted directly into wikicode. Ampersand (&) is one of those.[5]


To insert a literal ampersand, use &amp; (ampersand, latin small letter a, latin small letter m, latin small letter p, semicolon).

To insert literal less-than and greater-than symbols, (such as within a <pre> or <code> tag), use &lt; and &gt; respectively.

To insert square brackets, use &lbrack; and &rbrack; for [ and ].

To insert a literal vertical bar, use &verbar; for &verbar;. <nowiki>|</nowiki> also works.

To insert an anchor tag, use Template:anchor or <span id="NameOfAnchorHere"></span> (e.g. So tacking #NameOfAnchorHere to the end of a URL jumps a web browser to the anchor tag.).

Lists

Create nested lists with different format[fn 1]

<ol>
 <li> By the law of force
  <ol style="list-style-type:lower-alpha">
   <li> By surrender or capture in war </li>
   <li> By being the victim of raiding or kidnapping </li>
  </ol>
 </li>
 <li> As legal punishment for crimes (including debt) </li>
 <li> Through paternal authority (a father’s sale of his children) </li>
 <li> Through the voluntary sale of one’s self4</li>
</ol>

Create multiple columns of a list.[6]

<div style="column-count:2">
* apple
* carpet
* geography
* mountain
* nowhere
* postage
* ragged
* toast
</div>
yields:
  • apple
  • carpet
  • geography
  • mountain
  • nowhere
  • postage
  • ragged
  • toast

Unicode

See https://meta.wikimedia.org/wiki/Help:Special_characters


Tables

A basic table (ref):

{| class="wikitable" style="margin:auto"
|+ Caption text
|-
! Header text !! Header text !! Header text
|-
| Example || Example || Example
|-
| Example || Example || Example
|-
| Example || Example || Example
|}

A basic sortable table (ref):

{| class="wikitable sortable" style="margin:auto"
|+ Caption text
|-
! Header text !! Header text !! Header text
|-
| Example || Example || Example
|-
| Example || Example || Example
|-
| Example || Example || Example
|}

A table with padding.

{| class="standard"
|-
| style="padding: 10px;" | '''''AGLAIA''''' || style="padding: 10px;" | '''''BRILLIANCE'''''
|-
| style="padding: 10px;" | '''''EUPHROSYNE''''' || style="padding: 10px;" | '''''JOY'''''
|-
| style="padding: 10px;" | '''''THALIA''''' || style="padding: 10px;" | '''''BLOOM'''''
|}

A collapsed table. (ref)

{| class="wikitable mw-collapsible mw-collapsed"
|-
! Pub. date
! Title
! Authors
! ISBN
|-
| [[2021-01-20]]
| [[#Selfcare]]
| [[Annalee Newitz]]
|}

A table with cells with different alignments.

{| class="wikitable"
|- style="vertical-align:top;"
| style="height:100px; width:100px; text-align:left;" | A
| style="height:100px; width:100px; text-align:center;" | B
| style="height:100px; width:100px; text-align:right;" | C
|- style="vertical-align:middle;"
| style="height:100px; width:100px; text-align:left;" | D
| style="height:100px; width:100px; text-align:center;" | E
| style="height:100px; width:100px; text-align:right;" | F
|- style="vertical-align:bottom;"
| style="height:100px; width:100px; text-align:left;" | G
| style="height:100px; width:100px; text-align:center;" | H
| style="height:100px; width:100px; text-align:right;" | I
|}

A table with irregular merged rows and columns.

{| class="wikitable" style="margin:auto"
|+ Table with merged rows and columns
|-
! rowspan="2" | r1-2,c1
! rowspan="2" | r1-2,c2
! rowspan="2" | r1-2,c3
! colspan="2" | r1,c4-5
|-
! r2,c4
! r2,c5
|-
| r3,c1
| r3,c2
| r3,c3
| r3,c4
| r3,c5
|-
| r4,c1
| r4,c2
| r4,c3
| r4,c4
| r4,c5
|-
| r5,c1
| r5,c2
| r5,c3
| r5,c4
| r5,c5
|}
Table with merged rows and columns
r1-2,c1 r1-2,c2 r1-2,c3 r1,c4-5
r2,c4 r2,c5
r3,c1 r3,c2 r3,c3 r3,c4 r3,c5
r4,c1 r4,c2 r4,c3 r4,c4 r4,c5
r5,c1 r5,c2 r5,c3 r5,c4 r5,c5

Table with combined cells and columns

{| class="wikitable" style="margin:auto"
|+ 6x6 table with irregular headers
|-
! rowspan=2 colspan=2 | r1-2,c1-2
! colspan=4 | r1,c3-6
|-
! colspan=2 | r2,c3-4
! colspan=2 | r2,c5-6
|-
! rowspan=2 | r3-4,c1
! rowspan=2 | r3-4,c2
! colspan=2 | r3,c3-4
! r3,c5
! r3,c6
|-
! r4,c3
! r4,c4
! r4,c5
! r4,c6
|-
| r5,c1
| r5,c2
| r5,c3
| r5,c4
| r5,c5
| r5,c6
|}
6x6 table with irregular headers
r1-2,c1-2 r1,c3-6
r2,c3-4 r2,c5-6
r3-4,c1 r3-4,c2 r3,c3-4 r3,c5 r3,c6
r4,c3 r4,c4 r4,c5 r4,c6
r5,c1 r5,c2 r5,c3 r5,c4 r5,c5 r5,c6

Templates

Transclusion

See https://www.mediawiki.org/wiki/Help:Transclusion

Substitution

See https://www.mediawiki.org/wiki/Help:Substitution

Another page may be substituted into another using a one-time template syntax that replaces the template with the target page.[7] For example, by inserting the double curly-brace code snippet {{subst:FULLPAGENAME}}, the wikicode of the article FULLPAGENAME will be inserted in place of the snippet.

When using Extension:ParserFunctions substitutions within an Extension:Cite <ref> tag, one must use the {{#tag:…}} pattern. See link.

The following in a Mediawiki template:
<onlyinclude><ref group=cmt>Comment: {{{1|}}}</ref></onlyinclude>
should instead be:
<onlyinclude>{{#tag:ref|Comment: {{{1|}}}|group=cmt}}</onlyinclude>

Upgrading

See https://www.mediawiki.org/wiki/Manual:Upgrading

A mechanism for updating via a webUI exists at /mw-config/. For example, if your wiki is at http://example.org/w/index.php, then navigate to http://example.org/w/mw-config/. [8]

Special pages

MediaWiki:Copyright
A wiki-specific page containing copyright information to be displayed in the footer of every page. See also: https://www.mediawiki.org/wiki/Manual:$wgRightsText

History

See also


External links

References

  1. Manual:dumpBackup.php”. (2023-02-26). mediawiki.org. Accessed 2023-04-13.
  2. API:Revisions”. (2022-07-20). mediawiki.org. Accessed 2023-04-13.
  3. Manual:importTextFiles.php”. (2021-05-03). mediawiki.com. Accessed 2023-04-01.
  4. 4.0 4.1 4.2 Manual:Table of contents”. (n.d.). mediawiki.org. Accessed 2023-07-19. Archived on 2023-06-24.
  5. Named character references”. (2023-07-20). WHATWG. Accessed 2023-07-23. Archived from the original on 2023-07-17.
  6. [1]”. (2023-03-25). mediawiki.org. Accessed 2023-07-16. Archived from the original on 2023-07-05.
  7. Help:Substitution”. (2022-04-10). mediawiki.org. Accessed 2023-04-08.
  8. Manual:Upgrading”. (n.d.). mediawiki.org. Accessed 2023-06-13.

Footnotes

Comments