I started an example wiki scripts repo! (It’s all Python.)
Recently I realized that the most useful scripts to wiki admins wanting to branch out into Python development are also the simplest, most limited in scope scripts, which are also the scripts that are the most likely to be one-time use, not built into any other tools, and discarded by me after a single use. In other words, I’m probably not uploading them or preserving them anywhere.
In fact, it’s very deliberate that I don’t preserve code like this anywhere: The only way to ensure that throwaway, rigid, non-extensible code doesn’t make it into a permanent production environment is to literally throw it away after you’re done with it. That means making a scratch file that you don’t save, or overwriting a single scratch file after each single use is finished, or something along these lines.
Does this mean I’m writing bad code? Well…certainly a lot of the time I write one-off scripts that have a lot of hard-coded things, can’t take configurable parameters, can only be used on a single wiki, etc etc. If I were to consider them permanent additions to my code base, I would build a lot more structure to them, but since they are literally one-time-use and then they are gone forever, I just do what’s easy, and that’s totally okay and appropriate; it’s not worth the time it would take to overbuild for what is in fact just a once-and-done use case. So, no, they aren’t bad. They’re just, limited in purpose and lifespan.
But it can’t be denied that these once-and-done scripts could be useful to other people to see as examples of relatively simple Python scripts, and so I do actually want to preserve them somewhere, even if it’s none of my main repos. So I decided to create a new repo specifically for scripts like this and nothing else. Anything I put here, I won’t ever reuse, but you are free to read it and see how the libraries work, etc.
I will do my best to comment everything I upload enough that you can understand my motivation, and I’ll even attempt to go beyond that and explain how the script functions and logic behind various choices, as if I were putting the code into production. Once I reach about 20 or 30 files, I might even make detailed comments a requirement to upload files here, so that high-quality examples don’t get diluted by lower-quality examples.
Mostly everything here will use river_mwclient, not just vanilla mwclient. I am, however, planning to refactor my library into two separate repos and publish on PyPI in upcoming weeks, one of them will be a generic mwclient wrapper and the other one will be esports-wiki-specific, and you will be able to use the generic mwclient wrapper without any of the esports wiki stuff if you want. (I’ll keep backwards compatibility though.) I think my wrapper handles a lot of things much better than mwclient itself does, but I can understand not wanting to use the extra dependency.
If you do want to use river_mwclient, installation and setup instructions are available on the repo page. If you don’t, then just make a site object using regular mwclient and then for the most part there’s two main differences:
- Site methods are directly on the site object, i.e.
site.method
instead ofsite.client.method
- Methods that do actions to pages are methods on the page instead of on the site object, e.g.
page.delete(...)
instead ofsite.delete(page, ...)
You also won’t get my automatic retry handling (especially relevant on Fandom) but it’ll mostly work out other than that. Follow mwclient docs and you should be good to go.
If you use either package - or mwparserfromhell
- and want to add your own examples to this repo, I’d be happy to accept PRs! Just include a comment in the file saying who the author is and also comment the code explaining what’s going on. Each individual file should be completely standalone; the goal is to provide simple examples to developers who are super new to Python and want to learn how to interact with MediaWiki.
And if this is helpful to you I’d love to hear from you! Let me know what does and doesn’t work for you to learn from!