WikiRobots: Difference between revisions

From FreeCAD Documentation
No edit summary
(Marked this version for translation)
 
Line 1: Line 1:
<languages/>
<languages/>
<translate>
<translate>
<!--T:1-->
{{VeryImportantMessage|Robots are intrinsically dangerous as they can automatically do a lot of damage. Use with extreme care!}}
{{VeryImportantMessage|Robots are intrinsically dangerous as they can automatically do a lot of damage. Use with extreme care!}}


==Overview==
==Overview== <!--T:2-->


<!--T:3-->
Repetitive tasks can be automated using robots or bots, i.e. software programs operating on the wiki by themselves.
Repetitive tasks can be automated using robots or bots, i.e. software programs operating on the wiki by themselves.


<!--T:4-->
The natural and most commonly used robots for wiki sites are provided by MediaWiki, under the package name Pywikibot. See [http://www.mediawiki.org/wiki/Manual:Pywikibot Manual:Pywikibot] for the full information.
The natural and most commonly used robots for wiki sites are provided by MediaWiki, under the package name Pywikibot. See [http://www.mediawiki.org/wiki/Manual:Pywikibot Manual:Pywikibot] for the full information.


<!--T:5-->
In a nutshell, Pywikibot is a collection of Python scripts able to use the native wiki API to act on wiki sites. To see the API list for the FreeCAD wiki, visit http://www.freecadweb.org/wiki/api.php
In a nutshell, Pywikibot is a collection of Python scripts able to use the native wiki API to act on wiki sites. To see the API list for the FreeCAD wiki, visit http://www.freecadweb.org/wiki/api.php


<!--T:6-->
In order to use Pywikibot, you need to:
In order to use Pywikibot, you need to:


<!--T:7-->
# install the Pywikibot package
# install the Pywikibot package
# configure Pywikibot to work on the FreeCAD Wiki
# configure Pywikibot to work on the FreeCAD Wiki
# launch the scripts you need for the task at hand
# launch the scripts you need for the task at hand


<!--T:8-->
There is a wealth of information on how to install, configure and use Pywikibot. However, please be aware that this information, although useful, can be misleading, since it mixes instructions related to two different Pywikibot codebases, and different versions of the Pywikibot scripts collection.
There is a wealth of information on how to install, configure and use Pywikibot. However, please be aware that this information, although useful, can be misleading, since it mixes instructions related to two different Pywikibot codebases, and different versions of the Pywikibot scripts collection.


<!--T:9-->
In the following, you will find the basic instructions to set up and use Pywikibot on the FreeCAD wiki. This will allow you to perform the most common tasks. For advanced usage, refer to the [http://www.mediawiki.org/wiki/Manual:Pywikibot Manual:Pywikibot], and to the Python source code.
In the following, you will find the basic instructions to set up and use Pywikibot on the FreeCAD wiki. This will allow you to perform the most common tasks. For advanced usage, refer to the [http://www.mediawiki.org/wiki/Manual:Pywikibot Manual:Pywikibot], and to the Python source code.


==Installation==
==Installation== <!--T:10-->


<!--T:11-->
Go to http://tools.wmflabs.org/pywikibot/ and download {{FileName|package/pywikipedia/core.zip}} (the project is also under github, gerrit, etc. but this is a simple way to get a full self-contained package).
Go to http://tools.wmflabs.org/pywikibot/ and download {{FileName|package/pywikipedia/core.zip}} (the project is also under github, gerrit, etc. but this is a simple way to get a full self-contained package).


<!--T:12-->
Unzip the content in your preferred directory.
Unzip the content in your preferred directory.


<!--T:13-->
Unless you want to install the libraries into your Python libs, you are done (if you still want to install them, check the file {{FileName|INSTALL}} in the base directory).
Unless you want to install the libraries into your Python libs, you are done (if you still want to install them, check the file {{FileName|INSTALL}} in the base directory).


<!--T:14-->
Pywikibot works with Python 2.6 and 2.7 with no issues. Python 3 <s>has been not tested so far with FreeCAD wiki</s> works as well.
Pywikibot works with Python 2.6 and 2.7 with no issues. Python 3 <s>has been not tested so far with FreeCAD wiki</s> works as well.


==Configuration==
==Configuration== <!--T:15-->


<!--T:16-->
You must save the following Python code as a file with the name {{FileName|user-config.py}} in the base directory where you unzipped {{FileName|package/pywikipedia/core.zip}} (to be clear, in the same directory where you already find a file called {{FileName|user-config.py.sample}}).
You must save the following Python code as a file with the name {{FileName|user-config.py}} in the base directory where you unzipped {{FileName|package/pywikipedia/core.zip}} (to be clear, in the same directory where you already find a file called {{FileName|user-config.py.sample}}).


<!--T:17-->
{{Code|code=
{{Code|code=
# -*- coding: cp437 -*-
# -*- coding: cp437 -*-
Line 44: Line 58:
}}
}}


<!--T:18-->
In the above code:
In the above code:


<!--T:19-->
* replace ''<<yourWikiUserName>>'' with your Wiki user name
* replace ''<<yourWikiUserName>>'' with your Wiki user name
* replace ''cp437'' with your ''console_encoding''. To find out what is your console encoding, for Windows and Linux, launch the Python interpreter, enter {{SystemInput|import sys}} followed by {{SystemInput|print sys.stdout.encoding}}. Python will write your {{SystemOutput|console_encoding}} on the screen.
* replace ''cp437'' with your ''console_encoding''. To find out what is your console encoding, for Windows and Linux, launch the Python interpreter, enter {{SystemInput|import sys}} followed by {{SystemInput|print sys.stdout.encoding}}. Python will write your {{SystemOutput|console_encoding}} on the screen.




<!--T:20-->
Then you must save the following Python code as a file with the name {{FileName|freecadwiki_family.py}} under the sub-directory {{FileName|/pywikibot/families}} (together with the other {{FileName|family_xxx.py}} files).
Then you must save the following Python code as a file with the name {{FileName|freecadwiki_family.py}} under the sub-directory {{FileName|/pywikibot/families}} (together with the other {{FileName|family_xxx.py}} files).


<!--T:21-->
{{Code|code=
{{Code|code=
# -*- coding: utf-8 -*-
# -*- coding: utf-8 -*-


<!--T:22-->
__version__ = '$Id: 7f3891c3bbbfbd69c0b005de953514803d783d92 $'
__version__ = '$Id: 7f3891c3bbbfbd69c0b005de953514803d783d92 $'


<!--T:23-->
from pywikibot import family
from pywikibot import family




<!--T:24-->
# The MediaWiki family
# The MediaWiki family
# user-config.py: usernames['mediawiki']['mediawiki'] = 'User name'
# user-config.py: usernames['mediawiki']['mediawiki'] = 'User name'
Line 67: Line 88:
self.name = 'freecadwiki'
self.name = 'freecadwiki'


self.langs = {
<!--T:25-->
self.langs = {
'en': 'www.freecadweb.org',
'en': 'www.freecadweb.org',
}
}


<!--T:26-->
def scriptpath(self, code):
def scriptpath(self, code):
return 'wiki'
return 'wiki'


<!--T:27-->
def path(self, code):
def path(self, code):
return '/index.php' #The path of index.php, look at your wiki address.
return '/index.php' #The path of index.php, look at your wiki address.
Line 80: Line 104:
return '/api.php' #The path of api.php
return '/api.php' #The path of api.php


<!--T:28-->
def version(self, code):
def version(self, code):
# Replace with the actual version being run on your wiki
# Replace with the actual version being run on your wiki
return '1.20.3'
return '1.20.3'


<!--T:29-->
def protocol(self, code):
def protocol(self, code):
"""
"""
Can be overridden to return 'https'. Other protocols are not supported.
Can be overridden to return 'https'. Other protocols are not supported.
Line 92: Line 118:
}}
}}


==Usage==
==Usage== <!--T:30-->


<!--T:31-->
You are now all set to launch the Pywikibot scripts. The scripts themselves are contained in the {{FileName|/scripts}} subdirectory, from which you can know the names.
You are now all set to launch the Pywikibot scripts. The scripts themselves are contained in the {{FileName|/scripts}} subdirectory, from which you can know the names.


<!--T:32-->
To launch the scripts, open a shell and move to the base directory (the installation one, NOT the {{FileName|/scripts}} subdirectory), and write
To launch the scripts, open a shell and move to the base directory (the installation one, NOT the {{FileName|/scripts}} subdirectory), and write


<!--T:33-->
{{SystemInput|python pwb.py <<scriptname>>.py -<<parameter>>}}
{{SystemInput|python pwb.py <<scriptname>>.py -<<parameter>>}}


<!--T:34-->
where of course you replace ''<<scriptname>>'' with the name of the script you are interested in, and ''<<parameter>>'' with the parameter(s) required for the given script.
where of course you replace ''<<scriptname>>'' with the name of the script you are interested in, and ''<<parameter>>'' with the parameter(s) required for the given script.


<!--T:35-->
To have a description of the usage and parameters of any script, simply use the ''-help'' parameter. For instance, to have a description of the {{FileName|replace.py}} script (one of the most useful), type
To have a description of the usage and parameters of any script, simply use the ''-help'' parameter. For instance, to have a description of the {{FileName|replace.py}} script (one of the most useful), type


<!--T:36-->
{{SystemInput|python pwb.py replace.py -help}}
{{SystemInput|python pwb.py replace.py -help}}


<!--T:37-->
There is another very useful parameter, valid for all the scripts, called ''-simulate'', that allows you to test commands without harming the Wiki. Use it, before going 'live'.
There is another very useful parameter, valid for all the scripts, called ''-simulate'', that allows you to test commands without harming the Wiki. Use it, before going 'live'.


==Examples==
==Examples== <!--T:38-->


<!--T:39-->
This command will log into the wiki
This command will log into the wiki


<!--T:40-->
{{SystemInput|pwb.py login.py}}
{{SystemInput|pwb.py login.py}}




<!--T:41-->
This command will print a list of all the pages containing a link to SourceForge
This command will print a list of all the pages containing a link to SourceForge


<!--T:42-->
{{SystemInput|pwb.py listpages.py -weblink:sourceforge.net}}
{{SystemInput|pwb.py listpages.py -weblink:sourceforge.net}}




<!--T:43-->
This command will replace all the links to the old SourceForge Forum with a link to the new freecadweb.org-hosted Forum
This command will replace all the links to the old SourceForge Forum with a link to the new freecadweb.org-hosted Forum


<!--T:44-->
{{SystemInput|pwb.py replace.py -weblink:sourceforge.net/apps/phpbb/free-cad "sourceforge.net/apps/phpbb/free-cad" "forum.freecadweb.org"}}
{{SystemInput|pwb.py replace.py -weblink:sourceforge.net/apps/phpbb/free-cad "sourceforge.net/apps/phpbb/free-cad" "forum.freecadweb.org"}}




<!--T:45-->
This command will print a list of all the pages containing the word 'PartDesign', starting with the page titled "2d Drafting Module" and going on alphabetically
This command will print a list of all the pages containing the word 'PartDesign', starting with the page titled "2d Drafting Module" and going on alphabetically


<!--T:46-->
{{SystemInput|pwb.py listpages.py -start:"2d Drafting Module" -grep:PartDesign}}
{{SystemInput|pwb.py listpages.py -start:"2d Drafting Module" -grep:PartDesign}}




<!--T:47-->
This command will replace all the secure links to the old SourceForge Forum with a link to the new freecadweb.org-hosted Forum in the translated pages
This command will replace all the secure links to the old SourceForge Forum with a link to the new freecadweb.org-hosted Forum in the translated pages


<!--T:48-->
{{SystemInput|pwb.py replace.py -start:Translations:! "https://sourceforge.net/apps/phpbb/free-cad" "http://forum.freecadweb.org"}}
{{SystemInput|pwb.py replace.py -start:Translations:! "https://sourceforge.net/apps/phpbb/free-cad" "http://forum.freecadweb.org"}}


== FreeCAD Wiki Related Commands ==
== FreeCAD Wiki Related Commands == <!--T:49-->
Count all pages that a specific wiki templates is used in
Count all pages that a specific wiki templates is used in


<!--T:50-->
{{SystemInput|python3 pwb.py templatecount -count GuiCommand}}
{{SystemInput|python3 pwb.py templatecount -count GuiCommand}}




<!--T:51-->
List all pages that a specific wiki templates is used in
List all pages that a specific wiki templates is used in


<!--T:52-->
{{SystemInput|python3 pwb.py templatecount -list GuiCommand}}
{{SystemInput|python3 pwb.py templatecount -list GuiCommand}}


<!--T:53-->
Replace a string in all the pages listed in the Arch category (a/k/a [[Category:Arch]])
Replace a string in all the pages listed in the Arch category (a/k/a [[Category:Arch]])


<!--T:54-->
{{SystemInput|python3 pwb.py replace.py -cat:Arch}}
{{SystemInput|python3 pwb.py replace.py -cat:Arch}}
</translate>
</translate>

Latest revision as of 07:47, 25 August 2020

Robots are intrinsically dangerous as they can automatically do a lot of damage. Use with extreme care!

Overview

Repetitive tasks can be automated using robots or bots, i.e. software programs operating on the wiki by themselves.

The natural and most commonly used robots for wiki sites are provided by MediaWiki, under the package name Pywikibot. See Manual:Pywikibot for the full information.

In a nutshell, Pywikibot is a collection of Python scripts able to use the native wiki API to act on wiki sites. To see the API list for the FreeCAD wiki, visit http://www.freecadweb.org/wiki/api.php

In order to use Pywikibot, you need to:

  1. install the Pywikibot package
  2. configure Pywikibot to work on the FreeCAD Wiki
  3. launch the scripts you need for the task at hand

There is a wealth of information on how to install, configure and use Pywikibot. However, please be aware that this information, although useful, can be misleading, since it mixes instructions related to two different Pywikibot codebases, and different versions of the Pywikibot scripts collection.

In the following, you will find the basic instructions to set up and use Pywikibot on the FreeCAD wiki. This will allow you to perform the most common tasks. For advanced usage, refer to the Manual:Pywikibot, and to the Python source code.

Installation

Go to http://tools.wmflabs.org/pywikibot/ and download package/pywikipedia/core.zip (the project is also under github, gerrit, etc. but this is a simple way to get a full self-contained package).

Unzip the content in your preferred directory.

Unless you want to install the libraries into your Python libs, you are done (if you still want to install them, check the file INSTALL in the base directory).

Pywikibot works with Python 2.6 and 2.7 with no issues. Python 3 has been not tested so far with FreeCAD wiki works as well.

Configuration

You must save the following Python code as a file with the name user-config.py in the base directory where you unzipped package/pywikipedia/core.zip (to be clear, in the same directory where you already find a file called user-config.py.sample).

# -*- coding: cp437  -*-
family = 'freecadwiki'
mylang = 'en'
usernames['freecadwiki']['en'] = u'<<yourWikiUserName>>'
#usernames['freecadwiki']['freecadwiki'] = u'<<yourWikiUserName>>'
console_encoding = 'cp437'

In the above code:

  • replace <<yourWikiUserName>> with your Wiki user name
  • replace cp437 with your console_encoding. To find out what is your console encoding, for Windows and Linux, launch the Python interpreter, enter import sys followed by print sys.stdout.encoding. Python will write your console_encoding on the screen.


Then you must save the following Python code as a file with the name freecadwiki_family.py under the sub-directory /pywikibot/families (together with the other family_xxx.py files).

# -*- coding: utf-8  -*-

__version__ = '$Id: 7f3891c3bbbfbd69c0b005de953514803d783d92 $'

from pywikibot import family


# The MediaWiki family
# user-config.py: usernames['mediawiki']['mediawiki'] = 'User name'
class Family(family.WikimediaFamily):
    def __init__(self):
        super(Family, self).__init__()
        self.name = 'freecadwiki'

        self.langs = {
            'en': 'www.freecadweb.org',
        }

    def scriptpath(self, code):
        return 'wiki'

    def path(self, code):
        return '/index.php' #The path of index.php, look at your wiki address. 
	 
    def apipath(self, code):
        return '/api.php' #The path of api.php

    def version(self, code):
        # Replace with the actual version being run on your wiki
        return '1.20.3'

    def protocol(self, code):
        """
        Can be overridden to return 'https'. Other protocols are not supported.
        """
        return 'http'
        #return 'https' # My server uses https

Usage

You are now all set to launch the Pywikibot scripts. The scripts themselves are contained in the /scripts subdirectory, from which you can know the names.

To launch the scripts, open a shell and move to the base directory (the installation one, NOT the /scripts subdirectory), and write

python pwb.py <<scriptname>>.py -<<parameter>>

where of course you replace <<scriptname>> with the name of the script you are interested in, and <<parameter>> with the parameter(s) required for the given script.

To have a description of the usage and parameters of any script, simply use the -help parameter. For instance, to have a description of the replace.py script (one of the most useful), type

python pwb.py replace.py -help

There is another very useful parameter, valid for all the scripts, called -simulate, that allows you to test commands without harming the Wiki. Use it, before going 'live'.

Examples

This command will log into the wiki

pwb.py login.py


This command will print a list of all the pages containing a link to SourceForge

pwb.py listpages.py -weblink:sourceforge.net


This command will replace all the links to the old SourceForge Forum with a link to the new freecadweb.org-hosted Forum

pwb.py replace.py -weblink:sourceforge.net/apps/phpbb/free-cad "sourceforge.net/apps/phpbb/free-cad" "forum.freecadweb.org"


This command will print a list of all the pages containing the word 'PartDesign', starting with the page titled "2d Drafting Module" and going on alphabetically

pwb.py listpages.py -start:"2d Drafting Module" -grep:PartDesign


This command will replace all the secure links to the old SourceForge Forum with a link to the new freecadweb.org-hosted Forum in the translated pages

pwb.py replace.py -start:Translations:! "https://sourceforge.net/apps/phpbb/free-cad" "http://forum.freecadweb.org"

FreeCAD Wiki Related Commands

Count all pages that a specific wiki templates is used in

python3 pwb.py templatecount -count GuiCommand


List all pages that a specific wiki templates is used in

python3 pwb.py templatecount -list GuiCommand

Replace a string in all the pages listed in the Arch category (a/k/a)

python3 pwb.py replace.py -cat:Arch