Pywikipediabot使用手冊: Difference between revisions

From Meta, a Wikimedia project coordination wiki
Content deleted Content added
Alexsh (talk | contribs)
New page: {{Bothelp}} Python Wikipediabot Framework (pywikipedia)是一組針對維基百科或維基媒體計劃等以MediaWiki架設的網站的自動執行更新工具。本頁將提供如何使...
 
fix movedtomediawiki
 
(37 intermediate revisions by 8 users not shown)
Line 1: Line 1:
{{MovedToMediaWiki|Manual:Pywikipediabot/zh-hant}}
{{Bothelp}}
Python Wikipediabot Framework (pywikipedia)是一組針對維基百科或維基媒體計劃等以MediaWiki架設的網站的自動執行更新工具。本頁將提供如何使用本軟體的一般資訊。
== 初始設定 ==
PyWikipediaBot has been written in [[:en:Python_(programming_language)|Python language]] and requires the Python program. You need to have at least Python version 2.4 installed on your computer to be able to run PyWikipediaBot. Although some of the code may work on Python version 2.3, support for older versions of Python is not planned.

ActivePython

ActivePython can be run on all common platforms (Unix, Linux, Mac, Windows).

*For '''windows''': Download '''ActivePython''', found [http://www.activestate.com/Products/activepython/features.plex here]. Download and install ActivePython.
*In '''Unix''', Python is already present on some Unix versions and does not need to be downloaded (although it might be necessary to update it if you have a very old version).
*In '''Mac''', Python is already included on Mac OS X.

=== Download ===
The easiest way to download PyWikipediaBot are the nightly releases available at [http://tools.wikimedia.de/~valhallasw/pywiki this site]. Outdated versions can be found at [http://sourceforge.net/projects/pywikipediabot/ Sourceforge]. All you have to do is download PyWikipedia to your computer and decompress the file, there is no further installation required.

On a '''Mac''', follow [http://www.rubyrobot.org/tutorial/subversion-with-mac-os-x these instructions]. The pywikipedia files are found [http://svn.wikimedia.org/svnroot/pywikipedia/trunk/pywikipedia/ here].<ref name="check">"Check out" is the process of getting a copy to work on from the repository</ref>

====Download with SVN====
You can use [[:en:Subversion (SVN)|SVN]] ([http://subversion.tigris.org/] subversion.tigris.org) to retrieve an up-to-date version of PyWikipediaBot. If you use Windows [http://tortoisesvn.net/downloads TortoiseSVN] is advised. On a Mac, you can follow [http://www.rubyrobot.org/tutorial/subversion-with-mac-os-x this instruction].

<div class="plainlinks">To check out the source code via SVN command line use the following command:

<tt><nowiki>$ svn checkout http://svn.wikimedia.org/svnroot/pywikipedia/trunk/pywikipedia/ pywikipedia</nowiki></tt>

Or, without the spell-checking files (saves a while):
$ svn checkout --ignore-externals http://svn.wikimedia.org/svnroot/pywikipedia/trunk/pywikipedia/ pywikipedia
</div>
In your current working directory there’s now created a new directory named “pywikipedia”.

For non command line tools, the only information needed is the repository path: http://svn.wikimedia.org/svnroot/pywikipedia/trunk/pywikipedia/

===Configuration===
You do not need to "install" this package to be able to make use of it. You can actually just run it from the directory where you unpacked it or where you have your copy of the SVN sources.

Before you run any of the programs, you need to create a file named
<tt>user-config.py</tt> in your <tt>pywikipedia</tt> directory. You can create it manually (it needs at least two or three varibles configured) or run the script <tt>generate_user_files.py</tt> that helps to complete this process.

Please note, PyWikipediaBot assumes your account uses the default Monobook skin, don't change it in your MediaWiki account setting if you want the bot to work properly.

====Configuration for Wikipedia====
Open a text editor (e.g. Notepad in Windows) and save the text file as <code>user-config.py</code>, in the <tt>pywikipedia</tt> folder.

Add the following two required lines to <code>user-config.py</code>:

{| cellpadding=5
|'''Code'''
|'''Explanation'''
|-
|valign=top width=400px|
mylang = 'xx'
|valign=top|
xx is the code for the language code you are working on, "en" is English.<ref name="lan">If you want to work with more than one language, choose the most common one, as you can override configured value in command line by <tt>-lang</tt> parameter.</ref>
|-
|valign=top|
usernames['wikipedia']['en'] = u'ExampleBot'<ref name="uni">The 'u' in front of the username stands for Unicode. The 'u' is important if your username contains non-ASCII characters.</ref>
|valign=top|
Your <code>user-config.py</code> file needs to specify the bot's username.

In this example, the user is working on English Wikipedia, and has created a bot account with the username "ExampleBot".
|-
|valign=top|
''(Optional)''

usernames['wikipedia']['de'] = u'BeispielBot'
usernames['wikipedia']['en'] = u'ExampleBot'
usernames['wiktionary']['de'] = u'BeispielBot'
|valign=top|
''(Optional)''

If you are working on more than one wikipedia project, you can also add several other usernames.
|}
Now save <code>user-config.py</code> again.

====Configuration for other MediaWiki wikis====
Add the following '''three''' required lines to <code>user-config.py</code>:

{| cellpadding=5
|'''Code'''
|'''Explanation'''
|-
|valign=top width=400px|
mylang = 'xx'
|valign=top|
xx is the code for the language code you are working on, "en" is English.<ref name="lan">If you want to work with more than one language, choose the most common one, as you can override configured value in command line by <tt>-lang</tt> parameter.</ref>
|-
|valign=top|
family = 'sitename'
|valign=top|
"Sitename" is the name of the site you're working on.

Currently, this can be wiktionary, wikibooks, wikiquote, as well as some non-[[Wikimedia]] projects like wikitravel (the full list is found in the pywikipedia/families folder).
|-
|valign=top|
usernames['memoryalpha']['en'] = u'ExampleBot'
|valign=top|

Your <code>user-config.py</code> file needs to specify the bot's username.

For example, if you want to work on the English Memory Alpha, and you have created a bot account with username "ExampleBot".
|-
|valign=top|
''(For Wikimedia Commons only)''

mylang='commons'
family='commons'
usernames['commons']['commons']='UserBot'

|valign=top|
''(For Wikimedia Commons only)''

If you work on Wikimedia Commons, specify <code>commons</code> both as language and as family.
|-
|valign=top|

|valign=top|
|}

Now save <code>user-config.py</code> again.

If your wiki is not listed in the families folder, create your own families .py file, see [[Pywikipedia bot on non-Wikimedia projects]].

====Configuration for non-wikimedia projects====
Refer to the article [[Pywikipedia bot on non-wikimedia projects]] for instructions on how to configure the bot to use it on your own mediawiki-powered project.

==Permission on wiki projects==
Make sure that your bot is approved by the wiki community where you are going to use it. Strictness of this differs greatly between various projects; at some you need to announce it in advance and get approval before you start, at others you can do whatever you want.

Using your normal browser, create a login name and password for the bot. It is best to use a name that makes clear that it is a bot, and preferably also who is operating it. A common method is to use your own login name and add the word 'bot' to it, but several other forms also exist.

On the English Wikipedia, bots are only allowed to be used if they are approved at [[:en:Wikipedia:Bots/Requests for approval]].

===Request a bot flag===
If you heavily use a bot, it will clutter recent changes. To avoid that, you can get your bot registered as such. In that case it will not be shown on Recent changes unless a user specifically asks to get bots included.

This can be done by a [[Bureaucrat]]. You can put a request to get your or someone else's bot registered at [[Requests for bot status]]. You will probably be asked for some kind of evidence that your local community agrees with your bot. On the English language Wikipedia, requests should be made at [[en:Wikipedia:Bots/Requests for approvals|Requests for approvals]]. It is probably good to get your bot registered whenever it will edit many pages in a single run.

==Use==
===Select and run a bot script===
Now we are ready to really start using the bot. You need to get to a textual interface to your Operating System.

On '''Windows''' this is done by opening the start menu, and clicking on 'Run'. You are asked to give the name of a program, type "cmd.exe".
* Change the root to C: by typing chdir C:\
* Type chdir \"name of the folder where pywikipedia bot has been downloaded" ''(For example: chdir \"pywikipedia" if the file is in the C: folder.)''

On '''Mac''', find Terminal.app in /Applications/Utilities.

On '''Linux''' or any other '''Unix''', use any terminal application such as gnome-terminal, konsole, xterm, or simply the text-mode console.

Run the script [[login.py]] by typing "python login.py".

Python will then return:

:<code>Checked for running processes. 1 processes currently running, including the current process.</code>
:<code>Password for user your_bot on your_site:en:</code>

Use the password you used for the bot's login name. The bot can't work anonymously. Unless you change your password, you normally need to run this program only once, the bot usually does not get logged off.

The bots are in the main pywikipedia folder when downloaded. But it necessary, use the command cd to go to the directory where the bot files are saved.

Now run any of the bots here by typing "python botname.py" (If you are using Windows, you can leave out "python").

===Scripts===
Here is a list of the existing bots with links to their descriptions:
{| cellspacing="0" cellpadding="0" border="0" width="100%"
|- align="left" valign="middle"
| style="width:1ex" | &nbsp;
!Main bot scripts
| style="width:4ex" | &nbsp;
!Other bot scripts
| style="width:2ex" | &nbsp;
|-
| ||
----
| ||
----
| ||
----
|- align="left" valign="top"
| ||
* [[category.py]] Script which manages categories
* [[imagetransfer.py]] Copy images to another wiki
* [[interwiki.py]] Creates Interlanguage links between a project
* [[redirect.py]] Fixes double redirects, and deletes broken redirects
* [[replace.py]] Replaces text
* [[solve_disambiguation.py]]
* [[table2wiki.py]] Converts HTML-tables to MediaWiki's language
* [[template.py]] Replaces a template with another
* [[upload.py]] Upload images to wiki
* [[weblinkchecker.py]] finds broken external links
* [[wikipedia.py]]
'''Auxiliary programs'''
* [[login.py]] Essential python program
| ||
* [[catall.py]] Adds or changes categories
* [[copyright.py]] Checks copyright text in search engines
* [[delete.py]] deletes pages en masse
* [[delinker.py]] Bot delinks and replaces images
* [[imageharvest.py]] Copies multiple images to a wiki
* [[movepages.py]] Moves page to another title
* [[pagefromfile.py]] Create pages from a textfile
* [[protect.py]]
* [[standardize_interwiki.py]] Moves the interwiki links
* [[standardize_notes.py]] Improves references and citations
* [[test.py]] Outputs logged-in account and wikifamily.
* <s>[[touch.py]]</s>
* <s>[[touchall.py]]</s>
* [[warnfile.py]]
* [[welcome.py]] Used to welcome new users
* [[version.py]] Outputs about Pywikipedia's revision number and so on
|}

===Command-line arguments===
Although many bot scripts have their own command line arguments, which should be documented on their respective pages (or in their source code), all bots unless specifically stated to the contrary recognize the following command line arguments:

; -help
: Print a list of global bot arguments (this list), followed by bot-specific help if available.
; <nowiki>-lang:xx</nowiki>
: Set the language of the wiki you want to work on to language code <code>xx</code>, overriding the configuration in <code>user-config.py</code>.
; <nowiki>-family:xyz</nowiki>
: Set the family of the wiki you want to work on, ''e.g.'', wikipedia, wiktionary, wikitravel, ... This will override the configuration in <code>user-config.py</code>.
; -log
: Enable the logfile. Logs will be stored in the logs subdirectory.
; <nowiki>-log:xyz</nowiki>
: Enable the logfile, using <code>xyz</code> as the filename.
; -nolog
: Disable the logfile (if it's enabled by default).
; <nowiki>-putthrottle:nn</nowiki>
: Set the minimum time (in seconds) the bot will wait between saving pages. The default value is 10.

For example, <code>python scriptname.py -family:wiktionary</code> will run the "scriptname" bot on wiktionary articles, overriding the default family setting in your user configuration.

===Bot mailing list===
It can be useful to subscribe to the bot mailing list (see http://lists.wikimedia.org/mailman/listinfo/Pywikipedia-l). Every time a file of the bot software is changed, a mail is sent to the list, so you know when you need to update to the new version.

==Update==
For further updates chdir into 'pywikipedia' and type

<tt>$ svn update</tt>

On a Mac, you can write the following:

<tt>svn update pywikipedia</tt>

==How to report a bug==
When you report a bug please try to include:

* PyWikipediaBot version in use. It's recommended to test if the bug is still present in latest SVN revision available.
* Python version (python -V) and operating system you use (e.g. Windows, Linux, MacOS...)
** For above purpose, [[version.py]] will be useful.
* A nice summary
* Full description of the problem/report
* How to reproduce bug full information (script, command line, family, and language used)
* Output data

To submit a new bug visit the [http://sourceforge.net/tracker/?group_id=93107 bug tracker] provided by SourceForge.

==Development==
If you have a function you want to have a bot for that is not yet provided by one of the bots, you can ask one of the programmers to write it for you. Or even better, you can try to work on the bots yourself. Python is a nice language, and not hard to learn. We will welcome you.

===Tips===
Here and in [[wikipedia.py]], there are some very basic tips for getting started writing your own bot:
*be sure you've set up your user-config.py file (see [[#Configuring for Wikipedia|above]])
*To gain access to the pywikipedia framework, use:
<source lang="python">
import wikipedia
</source>
*to retrieve a page, use the following, where pageName is, e.g., "Wikipedia:Bots" or "India":
<source lang="python">
site = wikipedia.getSite()
page = wikipedia.Page(site, u"pageName")
text = page.get(get_redirect = True)
</source>
*to update a page, use:
<source lang="python">
page.put(u"newText", u"Edit comment")
</source>
*look at some of the pywikipedia files for other ideas -- basic.py is relatively easy to read even if you're new to pywikipedia.
*you can find all available Page methods in the wikipedia.py file.
*basic.py gives you a setup that can be used for many different bots, all you have to do is define the string editing on the page text.
*To [[:w:iteration|iterate]] over a set of pages, see pagegenerators.py for some objects that return a set of pages. An example use of the CategoryPageGenerator that does something for each page in the [[:w:Category:Living people|Category:Living people]] category:
<source lang="python">
import catlib
import pagegenerators
site = wikipedia.getSite()
cat = catlib.Category(site,'Category:Living people')
gen = pagegenerators.CategorizedPageGenerator(cat)
for page in gen:
#Do something with the page object, for example:
text = page.get()
</source>

====Create a quick shortcut to run commands (Windows users) ====
How to make a quick shortcut to run commands (Windows users).

If you're installing Pywikipediabot in a folder such as "My Documents" it may be troublesome to use the "cd" command to go into the folder all the time to run the bots (For those who don't get what that means, this will help you a lot).

On Windows you can create a shortcut which will open the command box you can use to run bots easily. Just follow these simple steps to create one:
# Open up the folder pywikipedia is installed in, in a window.
# Under File > New select Shortcut.
# Type in "cmd.exe" and hit next.
# You can give a name to the shortcut here, just "Pywikipediabot" is good.
# In the address bar (The text bar above where your files are which tells you where you are) copy the path there.
# Right click on the new shortcut and hit properties and paste that path you copied into the "Start in" text field.
# Hit ok, and now you have a shortcut to open the command line to run bots from.

===Contributing changes===
If you changed the bot and want to send a patch to the maintainer,
# Update to the current version (it will merge your changes with the improvements already committed to the SVN Repository),
# Resolve any conflicts caused by the update (grep for "=====" ;-) and
# Type:
:<pre>$ svn diff > svn.diff</pre>

Review the diff to ensure it only includes the changes you want to contribute. The lines at the beginning starting with "?" should be removed.

If you are in direct contact with a Pywikipediabot developer, you can send the file svn.diff to him, but preferably attach the patch to a ticket in the [http://sourceforge.net/tracker/?atid=603138&group_id=93107&func=browse Pywikipedia bug tracking system].

===Multiple accounts===
It is a common need to run python wikipedia bot under different accounts (main and/or multiple bot accounts). It can be done in two ways.

====Separate pywikipedia distributions====
One can install completely separate instances of pywikipedia in different directories (1 for each account) and have diferent <code>user-config.py</code> files in each of them. However, when updating the installation ''via'' SVN, one needs to run <code>svn update</code> on each folder separately. Also, every installation takes some disk space, which might be a problem on accounts with limited quota.

====One pywikipedia distribution with symbolic links====

Let's assume user <code>foo</code> has a current SVN working copy of pywikipedia in <code>/home/foo/pywikipedia</code>. For each of the accounts, he creates a separate directory:

foo@bar:~$ '''mkdir foobot'''
foo@bar:~$ '''cd foobot'''

Pywikipedia needs then some symlinks to the main code tree created in the working directory:

foo@bar:~/foobot$ '''ln -s ~/pywikipedia/families'''
foo@bar:~/foobot$ '''ln -s ~/pywikipedia/userinterfaces'''
<!-- THAT'S ALL IT NEEDED IN MY CASE - ADD MORE IF NECESSARY -->

Then, <code>user-config.py</code> for this account must be created as described in [[#Configuration|Configuration section]] above.

Finally, the bot must be logged in the usual way:

foo@bar:~/foobot$ '''python ~/pywikipedia/login.py'''

The working directory is ready. The scripts will however require a slight modification to run (the path to the pywikipedia tree must be added to Python's path).

import sys, os
sys.path.append(os.environ['HOME'] + '/pywikipedia')
import wikipedia

That's all. Updating to the newest version of pywikipedia on ''all account at once'' is now a matter of running <code>svn update</code> only in the <code>~/pywikipedia</code> directory.

==Bot & Proxy==
There is probably (not tested!) draft workaround described
[http://nl.wikipedia.org/w/index.php?title=Overleg_gebruiker:Andre_Engels&oldid=10418345#pywikipedia_proxy here].
==Notes==
<references/>

==See also==
*[[Pywikipediabot general parameters]]
*[[Pywikipedia bot on non-Wikimedia projects]]
*[[botwiki:|Botwiki]]
*[[botwiki:Manual:Speed guide for pywikipediabot| Another guide]]
*[[w:Wikipedia:Creating a bot|Wikipedia:Creating a bot]]
*[[Using the python wikipediabot/Mac]], a Mac-specific guide.
*[[v:en:Pywikipediabot|Pywikipediabot course]] at [[Wikiversity]]

==External links==
*[http://pywikipediabot.sourceforge.net/ Project's webpage]
*[http://lists.wikimedia.org/mailman/listinfo/Pywikipedia-l PyWikipedia mailing-list]
*[irc://irc.freenode.net/pywikipediabot #pywikipediabot IRC channel]

<!--[[Category:MediaWiki tools]]
[[Category:Multilingualism]]
[[Category:Pywikipedia|!]]
-->

Latest revision as of 04:43, 9 March 2013