Manual:Pywikibot/PAWS/zh


 * 参见PAWS以获取更多信息. 

This document provides a quick interactive overview of  using a notebook hosted on the Wikimedia labs environment using PAWS (PAWS: A Web Shell).

创建维基媒体账户
To follow this walk-through, you only need a Wikipedia/Wikimedia account. Use Special:CreateAccount to create one.

Once you have created an account, please visit https://test.wikipedia.org/ and check that your username appears in the top right corner (this works around T120327).

登录笔记本
要启动托管笔记本，请访问https://paws.wmflabs.org

单击“使用MediaWiki登录”，然后在要求批准“使用OAuth进行身份验证”时单击“允许”. 首次访问PAWS时，需要创建服务器. 单击绿色的“启动我的服务器”按钮. 新服务器等待几分钟才能启动是正常的.

完成后，您将被重定向到 https://paws.wmflabs.org/paws/user/ /tree 这样的链接

启动终端
要开始一个新的互动终端，
 * 1) 前往你的PAWS home
 * 2) 点击右侧的“新建”，然后
 * 3) 选择“终端”.

这将打开一个新窗口，其URL为 https://paws.wmflabs.org/paws/user/ /terminals/1 ，并带有Linux '$'提示符.

您可以将此URL添加为书签，并随时返回终端，即使在您关闭浏览器或关闭自己的计算机之后也是如此.

This terminal is not an emulator. It is a real bash shell, as part of a real Linux installation sitting on a docker container, so you can use any bash command, and use any commands available on Linux that have been installed.

To see some of the commands available, use.

登录wiki
这将在服务器上建立您的帐户，并允许您从命令行登录. 以下命令应确认您可以登录testwiki. 它使用OAuth，因此无需输入密码.

You can connect pywikibot to a different wiki by creating a file named user-config.py in your  directory  and adding mylang and family variables:

创建一个页面
要创建页面，请在终端中输入以下命令，将“ ”替换为您的用户名，并在提示接受更改时按“Y”：

您已完成编辑. 在Web浏览器中打开 https://test.wikipedia.org/wiki/User_talk: 查看更改.

您可以使用'-help'命令行选项阅读有关每个命令行脚本的更多信息.

获取页面
使用“listpages”命令可以获取许多页面.

要获取您在上一章节中创建的页面的内容，请输入以下命令：

现在，如果您前往PAWS文件列表，则应该可以找到已保存的页面.

一个真实的脚本示例
When a website used on Wikipedia changes its URL, the links on Wikipedia become outdated, and possible also dead links if the website doesn't redirect from the old URLs to the new URLs. For example, Encyclopedia Britannica (EB) has changed their links, such as moving pages from http://www.britannica.com/EBchecked/media/ to http://www.britannica.com/topic/[topic name]/images-videos/*. You can find the list of usages of the old URL on English Wikipedia at https://en.wikipedia.org/wiki/Special:LinkSearch/http://www.britannica.com/EBchecked/media. Updating all those links manually will be very time consuming. Thankfully EB has maintained redirects from their old URLs to the new URLs, so this does not need to be fixed immediately.

For a simpler example, English Wikipedia currently contains links to http://britannica.com/EBchecked/ instead of http://www.britannica.com/EBchecked/ ; i.e. a 'www.' subdomain is missing in the URL.

There are currently 14 cases on English Wikipedia: https://en.wikipedia.org/wiki/Special:LinkSearch/http://britannica.com/EBchecked/

Wikipedia in other language also have this problem. e.g. there is one case on German Wikipedia: https://de.wikipedia.org/wiki/Spezial:Weblinksuche/http://britannica.com/EBchecked/

In order to fix those links, we can use Pywikibot replace.py script. In this demo we will use the '-simulate' argument to avoid writing to the wiki, as there are strict rules about automated editing of English Wikipedia.

First lets list all of the pages which link to http://britannica.com/EBchecked/.

Now we check those pages actually have the literal URL in the page; i.e. they are not using a template.

Now use replace to add the missing "www."

In PAWS, and any terminal that supports color, the diff of changes will show the added "www." in green text color, making it easier to find the proposed changes.

Inside Pywikibot
Next we will use the PAWS Python session.


 * 1) Go to your PAWS home,
 * 2) click 'New' on the right hand side, and
 * 3) select 'Python 3'.

This will open a new window.

In the text box, enter the following and in the Cell menu select 'Run' (or pressing shift+enter to run).

A new text box will appear below. Run the following to create an APISite object connected to https://test.wikipedia.org/:

Describe "site" by entering it into the new text box and selecting "Run".

It should show

Out[3]: APISite("test", "wikipedia")

创建页面对象：

通过运行检查它是否存在：

它应该输出

VERBOSE:pywiki:Found 1 wikipedia:test processes running, including this one. Out[5]: True

在页面上显示文字：

更改对象中的页面文本：

将页面保存到维基：

The response should be:

The interactive Python 3 notebook allows many lines to be run together. The above could be put into one text box and Run

The log of your interactive Python session can be saved or downloaded for future reference.

Accessing online documentation in PAWS
Pywikibot documentation may be found at https://doc.wikimedia.org/pywikibot/. It is primarily sourced from docstrings, which can be loaded in the interactive Python 3 notebook using the Python built-in function help.

For example, to look at the arguments for the save method above, run either: 或

编辑Pywikibot脚本
Pywikibot库和脚本位于/srv/paws中，并且是只读的. 无法在PAWS中修改已安装的Pywikibot库.

将脚本复制到PAWS主页后，可以进行修改脚本.

例如，要运行修改后的“checkimages.py”：


 * 1) 在终端中，输入
 * 2) In a browser, go to your PAWS home and click on the file.
 * 3) In the browser, you can edit the file. Edit the code -- for instance, just after the   code on line 1775, add a new line 1776 that will print out your name:
 * 4) In the editing interface, use the File menu and click Save to save your modifications.
 * 5) 在终端中，输入

参见

 * PAWS cheatsheet by one user (e.g. about API and database access)