User:Wesley/Bot guide/Examples

From JoCopedia
< User:Wesley‎ | Bot guide
Revision as of 07:35, 8 May 2008 by Wesley (talk | contribs) (And the <nowiki> goes around the template tag)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Here are some examples of what you can do with your bot.

Replacing text[edit]

The easiest and most common job faced with a bot is replacing text. When a bot connects to JoCopedia, the bot reads the wiki source code that makes up each page. This lets you do some nifty things with the bot.

Removing text[edit]

One of the first uses of a bot on JoCopedia was to remove the {{SongNav}} template from all the song pages. This can be done using the built-in script Simply type in the following:

python -cat:Songs "{{SongNav}}" ""

First, this line calls on Python to run the script. The next few items are described below

Tells the bot to only look at pages in the Songs category
The search string, or the thing you're looking for
The thing you're replacing it with (which is nothing)

Each time the bot is about to make a change, it will ask you for confirmation.

Change links[edit]

First, here's a little backstory. One of the bots on JoCopedia moved all the little This Day in JoCo boxes. For example, the box for April 1 used to be named Template:DATE April 1. Now it's named TDiJC:April 1. The old pages are now just redirects to the new pages. However, we want to get rid of them.

Using the built-in script, we can change all of those links. We could do this on many pages (such as on an entire category of pages), but we'll just do it to one page. The page we'll change is This Day in JoCo. Type in the following:

python -page:This_Day_in_JoCo "[[Template:DATE_" "[[TDiJC:"

The format is the same as before, except now we choose a single page by using -page:This_Day_in_JoCo. Remember to use underscores when a page title or a category name has spaces.

(In this case, it would have been easier to just copy the text into a text editor, do a Search and Replace, and then paste the text back in.)

Moving files[edit]

Apologies ahead of time for the complete mess in this section. As you'll soon be able to tell, it's a complete hackjob that will probably only ever suit this specific purpose.

First, go to Special:AllPages and choose the pages in namespace Template. Copy all the titles starting with DATE into a text file. Make them all into wikilinks, one on each line. For example DATE April 1 becomes [[Template:DATE April 1]]. Save the file as filelist.txt

Very inelegant hack coming up

Make a copy of and call it Change the following lines:

Line 119
-                newPageTitle = (u'%s:%s' % (namesp, newPageTitle))
+                newPageTitle = (u'TDiJC:%s' % newPageTitle)
Line 199
-        self.regexAll = False
+        self.regexAll = True
+        self.regex = re.compile("DATE ")
+        self.replacePattern = ""


Use the following command

python -file:filelist.txt