Using Wikireplicas from PAWS with Python

Using Wikireplicas is very similar to how you would do it from Toolforge.

In your home directory (.my.cnf), there is a file with your credentials. Be careful and do not publish them or share them.

In this notebook we will walk through how to connect to the databases of a few wikis, and doing a query onto them.

Library and helpers

We will use pymysql to connect to the DB servers.

We will make a make_connection helpers to connect to the hosts and DB.

And a query helper to wrap some of the boilerplate of querying. This helper returns all the results of a query, if the query is too big please see the PyMySQL API reference for using cursors in a more memory efficient way.

Example: Querying Wikimedia Commons

First we make a connection:

We query for pages with a string 'Alicante' in the title, limiting the results to 10:

Example: Querying Spanish Wikipedia

First we make a connection:

We query for users with a 'Alicante' in their user name, limiting the results to 10:

Fin

If you'd like to see more examples or have questions, reach out to #wikimedia-cloud on freenode IRC or email cloud@lists.wikimedia.org