7

I'm a wiki editor and an adminstrator at a wiki shared to another people. Some of them use excessive suppressing for possible redirects for a reason that remains unknown to me. For instance, if there is a page FOO:

#REDIRECT [[BAR]]

they often may use:

[[FOO|BAR]]

I find it very excessive and really unnecessary. The only other result of this approach, as far as I can see, is suppressing the message "Redirected from FOO", which I find informative.

How do I find all such suppressed redirects?

  • Do you have database access to this site? It shouldn't be to hard to do a database query for these if you have access. – Stephen Ostermiller Jun 23 '14 at 11:13
  • @StephenOstermiller no, I'm only allowed to use MediaWiki and have no database access. – Lyubomyr Shaydariv Jun 23 '14 at 20:23
  • It would be a good idea to remove the suppressredirect permission from the group those users belong to. – Nemo Oct 14 '15 at 21:22
  • @Nemo well, even if such a permission exists, it would be not very polite to remove the permission from another crew members that are administrators as well. But thank you for the suggestion. :) – Lyubomyr Shaydariv Oct 15 '15 at 7:04
  • @LyubomyrShaydariv unless your wiki has rules which mandate the deletion of redirects in certain cases, there is no reason to allow redirect suppression in the first place. Don't break the web! – Nemo Oct 15 '15 at 7:47
2

If you're able to write some lines in Python you should give the pywikibot framework a try.

You should take a look at redirect.py. pywikibot is especially powerful if you start using it interactively, e.g. through ipython. With wiki.allpages(includeredirects=True)) you can get a list of all pages and you can do e.g.

    incl_redir = map(lambda p: p.title(), wiki.allpages(includeredirects=True))
    excl_redir = map(lambda p: p.title(), wiki.allpages(includeredirects=False))
    redirects = filter(lambda p: not p in excl_redir, incl_redir)
    # We now have a list of all redirects.

    processed_redirects = ... # Here you need to find the exact strings of the redirects and turn them into a dict

    for p_title in excl_redir:
        page = Page(wiki, p_title)
        txt = page.get()
        for r in processed_redirects:
            if r['from'] in txt:
                # Here you want to process your txt and then
                page.put(txt, "Processed redirect")
| improve this answer | |
  • Thanks. Not sure if this answers the question. As far I understand Python, this code just extracts the redirects from the list of pages scope. I need something like text search for [[FOO|BAR]] at all pages, if there's a page "Foo" with #REDIRECT [[BAR]]. Or... does your code cover this case too? – Lyubomyr Shaydariv Aug 13 '14 at 18:49
  • Your case requires more code. these lines are just a beginning. I can take a look later for some more lives top help you get started. – kqw Aug 13 '14 at 18:55
  • I'll test it when I have more free time to experiment at my test wiki. Thanks for giving an idea. – Lyubomyr Shaydariv Aug 14 '14 at 7:17
  • Finally, I had some time to make the experiments a month later. Thanks for pointing the Pywikibot framework -- it's really cool. I've posted my solution in an answer to this question, but your help really helped a lot: your snipped was enough to make basic functionality due to lack of Pywikibot documentation (being a non-Pythonist, reading the source code of the framework is not that easy). Thanks! – Lyubomyr Shaydariv Sep 21 '14 at 1:54
  • I think this one should be accepted, not mine, since you gave me a very nice idea of how to implement what I needed. Thanks. :) – Lyubomyr Shaydariv Mar 9 '18 at 13:29
2

After some experiments I've ended up with the following Python script for Pywikibot. I'm very new to Python, so the following code might look not good. I tested it for a very very tiny test wiki, so I don't know what's the real perforance of the script. By the way, the page.put(...) takes extremely long for me (~15 s or so) -- don't even know what could be the reason. Maybe it helps someone else too.

Note: This script modifies the pages and considered not well-tested.

#!/usr/bin/python
# -*- coding: utf-8  -*-

import pywikibot
import re
import sys

try:
    # Win32
    from msvcrt import getch
except ImportError:
    # UNIX
    def getch():
        import sys, tty, termios
        fd = sys.stdin.fileno()
        old = termios.tcgetattr(fd)
        try:
            tty.setraw(fd)
            return sys.stdin.read(1)
        finally:
            termios.tcsetattr(fd, termios.TCSADRAIN, old)

def process_excessive_redirects(modify = False, pause = False):

    wiki = pywikibot.Site()
    alt_link_re = re.compile('\[\[\s*([^\|\]]+)\s*\|\s*([^\]]+)\s*\]\]')

    redirects_index = {}
    print 'Parsing redirects:'
    for redirect in wiki.allpages(filterredir = True):
        print '\t', redirect.title().encode('utf8'), '->',
        redirects_index[redirect.title()] = redirect.getRedirectTarget().title()
        print redirects_index[redirect.title()].encode('utf8')

    print 'Processing:'
    for page in wiki.allpages(filterredir = False):
        print '\t', page.title().encode('utf8'), '-',
        statistics = {'modification_count': 0} # python 3: nonlocal
        def fix_redirect(match_object):
            target = match_object.group(1)
            title = match_object.group(2)
            if title.replace("_", " ") in redirects_index.keys() and redirects_index[title] == target:
                if statistics['modification_count'] == 0:
                    print
                print '\t\texcessive redirect', target, '~~~>', title, '~~~>', target
                statistics['modification_count'] += 1
                return '[[' + title + ']]'
            return match_object.group(0)
        text = alt_link_re.sub(fix_redirect, page.get())
        if statistics['modification_count'] > 0:
            print "\t\t", statistics['modification_count'], 'excessive redirect(s) detected.',
            if modify:
                print 'Fixing redirects...',
                page.put(text, str(statistics['modification_count']) + ' excessive redirect(s) fixed')
                if pause:
                    print 'Press any key . . .'
                    getch()
            else:
                print
        else:
            print 'clean!'

def main(*args):
    modify = False
    pause = False
    for arg in pywikibot.handleArgs(*args):
        if arg == '--modify':
            modify = True
        elif arg == '--pause':
            pause = True
    process_excessive_redirects(modify = modify, pause = pause)

if __name__ == '__main__':
    main()
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.