Pywikibot
| tbd @ |
|---|
Background
It is very useful to have the option of automated edits on a MediaWiki. For this purpose, Pywikibot could be used.
- Help pages at Wiki Aventurica
- Hilfe:Pywikibot contains detailed instructions on how to install and operate a Pywikibot from a personal computer.
- Hilfe:Pywikibot-Scripte contains information about some Pywikibot-scripts.
- Hilfe:Regular Expressions contains information about regular expressions (regex), which is needed for /replace.py.
Pywikibot running directly on the server
It is possible to install the Pywikibot directly on the server, tie it to an user account, define some maintenance script which the bot should work on regularly, and call this script using Cron.
Benutzer:CronBot is operating in this way.
Basics
- Basic syntax
python pwb.py <script> <parameter>
| Parameter | Result |
|---|---|
-help
|
show help for the script |
-simulate
|
no edit is done, but the output in the console shows what would be changed without this parameter |
-summary:"(text)"
|
(text) will be set as the edit summary for the edits the script does. Without it, an automatic summary is used, which could be quite obtuse.
|
-always
|
edits will be done without asking for confirmation |
Scripts
Manual:Pywikibot/Scripts contains a sorted list of scripts.
replace.py
This is by far the most important script of Pywikibot. With this script it is possible to search for any wikitext in any page (possibly with a regex), and replace it with some other wikitext. /replace.py contains more information, as does Manual:Pywikibot/replace.py.
Do not use this script if your goal is just to refresh the page information; use #touch.py for this purpose instead.
It is possible to do more than one replacement in one command simply by adding more search/replacement pairs.
- Syntax
python pwb.py replace -targetgroup "(search)" "(replace)" [-option]
- Examples
python pwb.py replace -transcludes:"Ergebnisse" "{{Ergebnisse}}" "{{DPL-Ergebnisse}}" -summary:"interne Links" -always
python pwb.py replace -transcludes:"Infobox Schiff" -regex "\|Alter Kapitän=(.*?)\n\|Stapellauf=" "|Alter Kapitän=\1\n|Erbauer=\n|Stapellauf=" -summary:"Neuer Parameter"
python pwb.py replace -cat:"Person" "|Spezies=[[Mensch]]" "|Spezies=[[Menschen]]" "|Stand=[[Sklave]]" "|Stand=[[Sklaven]]" -summary:"Personengruppe in Mehrzahl" -always
python pwb.py replace -transcludes:"Infobox Gegenstandsgruppe" -regex "(?s)\|(DSA\d-)(Talent|Tradition|Merkmal|Repräsentation)=(.*?)\n(.*?)==" "\4==Regeltechnisches==\n\1\2=\3\n==" -summary:"entferne Regeltechnik aus Infobox"
| Parameter | Result |
|---|---|
-regex
|
(search) and (replace) will be interpreted as regex |
-nocase
|
search will be case insensitive |
-multiline
|
^ and $ will match begin and end of a line (otherwise it just matches begin and end of the page).
|
-dotall
|
. when used in a regex will also match a line break (otherwise it just matches every character except a line break).
|
-addcat:"(category)"
|
The category (category) will be added to each changed page. |
touch.py
This script edits wiki pages without changing them, triggering the page being parsed and thus all information about the page (categories and template inclusions) being refreshed. Manual:Pywikibot/touch.py contains more information.
- Syntax
python pwb.py touch -targetgroup
- Examples
python pwb.py touch -ref:"Vorlage:Infobox Person"
-targetgroup can be any of the target parameters mentioned on /replace.py.
redirect.py
This script fixes broken redirects (target does not exist) and fixes double redirects (redirects pointing to a redirect). Manual:Pywikibot/redirect.py contains more information.
At Wiki Aventurica, Benutzer:CronBot regularly fixes double and broken redirects with this script.
- Syntax
python pwb.py redirect action [-option]
- Examples
python pwb.py redirect double -always
python pwb.py redirect broken -delete
| Parameter | Result |
|---|---|
double
|
action: fixes double redirects |
broken
|
action: tries to fix broken redirects by using the last moved target of the destination page |
-delete
|
option for broken: if broken redirects cannot be fixed, delete them or mark them for deletion (the latter using Wikipedia code, thus being not so useful on wikis like Wiki Aventurica)
|
add_text.py
This script can be used to add wikitext to the top or bottom of a group of pages. This can also be achieved with #replace.py, but the syntax for that is a bit more complicated, so this script does have it's uses. Manual:Pywikibot/add_text.py contains more information.
- Syntax
python pwb.py add_text -targetgroup -text:"(text)" [-option]
- Examples
python pwb.py add_text -cat:"Der Letzte Held" -text:"{{Inoffizielle Publikation}}" -grepnot:"\{\{Inoffizielle Publikation\}\}" -up -always
python pwb.py add_text -catr:"Offizielle Publikation" -text:"[[Kategorie:Publikationsindex-{{subst:#sub:{{subst:PAGENAME}}|0|1}}]]" -grepnot:"Kategorie:Publikationsindex" -summary:"Hinzufügen der Kat:Publikationsindex"
| Parameter | Result |
|---|---|
-cat
|
targetgroup: work on every page in a category |
-catr
|
targetgroup: work on every page in a category, and every page in every subcategory of that category |
-text
|
The wikitext to add to each targeted page |
-grepnot
|
option: do not add the text to pages which contain wikitext matching this regex |
-up
|
option: add the text at the top of the page (default is bottom) |
category.py
This script can be used for adding or removing categories, or moving all pages from one category to another. Since in Wiki Aventurica we have mainly moved to automatically categorizing pages based on the content of the infobox, this script does not see much action there. Manual:Pywikibot/category.py contains more information.
- Syntax
python pwb.py category action [-option]
- Examples
python pwb.py category remove -from:"Artefakt" -summary:"entferne manuelle Kategorie"
python pwb.py category move -from:"Burg" -to:"Festung"
- Notes
- The actions "tidy", "add" and "tree" are interactive, and thus cannot easily be used in a script which runs unsupervised.
| Parameter | Result |
|---|---|
remove
|
action: removes category tag from all pages in the category |
move
|
action: move all pages in a category to another category |
-from:"(category)"
|
option for remove and move: category to remove
|
-to:"(category)"
|
option for move: category to add
|
-batch
|
option for remove and move: always delete emptied category
|
-match:"(regex)"
|
option for remove and move: only work on pages matching the regex
|
template.py
This script can be used to replace, remove or substitute a template. This also can be done with #replace.py, but this script is easier to use. Manual:Pywikibot/template.py contains more information.
- Syntax
python pwb.py template [-option] "oldTemplate" ["newTemplate"]
- Examples
python pwb.py template "Navigationsleiste Derischer Index" "Nav Derischer Index"
python pwb.py template -remove "Spoiler"
python pwb.py template -subst "Dreispaltig Abschluss"
| Parameter | Result |
|---|---|
-remove
|
option: removes the template from every page it is used in |
-subst
|
option: substitutes the template in each page |
-addedCat:"(category)"
|
option: adds the category (category) to each page that is edited |
movepages.py
This script moves pages. Although it is possible (but not really sensible) to move just a single page, the main use of this script is to move a lot of pages at once. This requires defining the pairs of pages in a text file with a given format, which is only sensible to do if you can generate a list of pages and edit it with a regex function. Manual:Pywikibot/movepages.py contains more information.
- Syntax
python pwb.py movepages [-option]
- Examples
python pwb.py movepages -pairsfile:"move.txt" -noredirect
| Parameter | Result |
|---|---|
-pairsfile:"(text file)"
|
Moves pages which are listed in (text file); the file must exist in the "core" folder of Pywikibot. The pairs of pages must be given in the following format: [[<old page>]] [[<new page>]] [[<old page>]] [[<new page>]]
|
-noredirect
|
suppress the creation of a redirect |
pagefromfile.py
This script creates new pages based on information given in a text file. As it is intended to create articles in Wikipedia, it does expect the pages to be formatted in a particular way, which in the use cases at Wiki Aventurica has to be corrected by using some options. As with #movepages.py, using this script is really only sensible if you can generate a list of pages and their content using some kind of regex. Manual:Pywikibot/pagefromfile.py contains more information.
- Syntax
python pwb.py pagefromfile -file:"(text file)" [-option]
- Examples
python pwb.py pagefromfile -file:"newpages.txt" -begin:"AAA" -end:"ZZZ" -titlestart:"@@@" -titleend:"€€€" -notitle
This would correctly create pages from the following content of the text file:
AAA @@@<first page name>€€€ <first page content> ZZZ AAA @@@<second page name>€€€ <second page content> ZZZ
| Parameter | Result |
|---|---|
-file:"(text file)"
|
The file containing the wikitext of the new pages; the file must exist in the "core" folder of Pywikibot |
-begin:"(string)"
|
The string marking the beginning of a page; default: {{-start-}}
|
-end:"(string)"
|
The string marking the end of a page; default: {{-stop-}}
|
-include
|
Include the begin and end markers into the new page. |
-titlestart:"(string)"
|
The string marking the beginning of the page title; default: '''
|
-titleend:"(string)"
|
The string marking the end of the page title; default: '''
|
-notitle
|
Do not add the page name and the strings marking it into the new page. |
delete.py
This script deletes a group of pages. The bot account needs to have delete rights in order for this to work. Manual:Pywikibot/delete.py contains more information.
- Syntax
python pwb.py delete -targetgroup [-option]
- Examples
python pwb.py delete -cat:"Veraltet" -always
-targetgroup can be any of the target parameters mentioned on /replace.py, below are the most sensible ones.
| Parameter | Result |
|---|---|
-cat:"(category)"
|
targetgroup: All pages in category (category) will be deleted. |
-ref:"(page)"
|
targetgroup: All pages referring to (page) will be deleted. |
-links:"(page)"
|
targetgroup: All pages linked from (page) will be deleted. |
weblinkchecker.py
This script checks the external links of the wiki. Links which are found to be broken are written into a local text file. Manual:Pywikibot/weblinkchecker.py contains more information.
We at Wiki Aventurica no longer use this script, we have replaced it with Extension:RottenLinks.
- Syntax
python pwb.py weblinkchecker [-option]
- Examples
python pwb.py weblinkchecker -start:!
python pwb.py weblinkchecker -repeat
| Parameter | Result |
|---|---|
-start:!
|
checks all pages in the article namespace |
-repeat
|
check all external links which were previously found to be broken |